Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soft opt out diff tests #847

Merged
merged 8 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/difference/core/driver/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ func (b *Builder) createConsumerGenesis(client *ibctmtypes.ClientState) *consume
consumertypes.DefaultConsumerRedistributeFrac,
consumertypes.DefaultHistoricalEntries,
b.initState.UnbondingC,
"0.05",
"0", // disable soft opt-out
)
return consumertypes.NewInitialGenesisState(client, providerConsState, valUpdates, params)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/difference/core/driver/traces.json

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions tests/difference/core/model/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,50 @@ class CCVProvider {
return;
}

//
// Soft opt out logic
//

// Sort token powers from lowest to highest
const tokens = this.m.staking.tokens;
const sortedTokens = Object.values(tokens).sort((a, b) => a - b);

// Get total power (token is 1:1 to power)
let totalPower = 0;
sortedTokens.forEach((token, _) => {
totalPower += token;
});

let smallestNonOptOutPower = -1;

// Soft opt out threshold is set as 0 as for now soft opt-out is disabled.
// See createConsumerGenesis() in diff test setup.go
const softOptOutThreshold = 0;

if (softOptOutThreshold == 0) {
smallestNonOptOutPower = 0
} else {
// get power of the smallest validator that cannot soft opt out
let powerSum = 0;

for (let i = 0; i < sortedTokens.length; i++) {
powerSum += sortedTokens[i];
if (powerSum / totalPower > softOptOutThreshold) {
smallestNonOptOutPower = sortedTokens[i];
break;
}
}
}

if (smallestNonOptOutPower == -1) {
throw new Error('control flow should not reach here');
}

if (this.m.staking.tokens[data.val] < smallestNonOptOutPower) {
// soft opt out if validator power is smaller than smallest power which needs to be up
return;
}

this.m.events.push(Event.RECEIVE_DOWNTIME_SLASH_REQUEST);


Expand Down
12 changes: 11 additions & 1 deletion x/ccv/consumer/keeper/soft_opt_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ func (k Keeper) SetSmallestNonOptOutPower(ctx sdk.Context, power uint64) {
// This is the smallest validator power such that the sum of the power of all validators with a lower power
// is less than [SoftOptOutThreshold] of the total power of all validators.
func (k Keeper) UpdateSmallestNonOptOutPower(ctx sdk.Context) {
// get soft opt-out threshold
optOutThreshold := sdk.MustNewDecFromStr(k.GetSoftOptOutThreshold(ctx))
if optOutThreshold.IsZero() {
// If the SoftOptOutThreshold is zero, then soft opt-out is disable.
// Setting the smallest non-opt-out power to zero, fixes the diff-testing
// when soft opt-out is disable.
k.SetSmallestNonOptOutPower(ctx, uint64(0))
return
}

// get all validators
valset := k.GetAllCCValidator(ctx)

Expand All @@ -43,7 +53,7 @@ func (k Keeper) UpdateSmallestNonOptOutPower(ctx sdk.Context) {
for _, val := range valset {
powerSum = powerSum.Add(sdk.NewDecFromInt(sdk.NewInt(val.Power)))
// if powerSum / totalPower > SoftOptOutThreshold
if powerSum.Quo(totalPower).GT(sdk.MustNewDecFromStr(k.GetSoftOptOutThreshold(ctx))) {
if powerSum.Quo(totalPower).GT(optOutThreshold) {
// set smallest non opt out power
k.SetSmallestNonOptOutPower(ctx, uint64(val.Power))
k.Logger(ctx).Info("smallest non opt out power updated", "power", val.Power)
Expand Down
14 changes: 14 additions & 0 deletions x/ccv/consumer/keeper/soft_opt_out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ func TestUpdateSmallestNonOptOutPower(t *testing.T) {
// 262 total power, (50 + 1 + 1) / 262 ~= 0.19, validator with 51 passes 0.199999 threshold and cannot opt out
expSmallestNonOptOutValPower: 51,
},
{
name: "soft opt-out disabled",
optOutThresh: "0",
validators: []*tmtypes.Validator{
tmtypes.NewValidator(cIds[0].TMCryptoPubKey(), 54),
tmtypes.NewValidator(cIds[1].TMCryptoPubKey(), 53),
tmtypes.NewValidator(cIds[2].TMCryptoPubKey(), 52),
tmtypes.NewValidator(cIds[3].TMCryptoPubKey(), 51),
tmtypes.NewValidator(cIds[4].TMCryptoPubKey(), 50),
tmtypes.NewValidator(cIds[5].TMCryptoPubKey(), 1),
tmtypes.NewValidator(cIds[6].TMCryptoPubKey(), 1),
},
expSmallestNonOptOutValPower: 0,
},
}

for _, tc := range testCases {
Expand Down