Skip to content

Commit

Permalink
feat(evmstaking): deterministic store for period delegations (cosmos#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezreal1997 authored Oct 16, 2024
1 parent 11ee810 commit 18efdf9
Show file tree
Hide file tree
Showing 64 changed files with 4,423 additions and 7,313 deletions.
244 changes: 28 additions & 216 deletions api/cosmos/staking/v1beta1/genesis.pulsar.go

Large diffs are not rendered by default.

7,022 changes: 2,603 additions & 4,419 deletions api/cosmos/staking/v1beta1/staking.pulsar.go

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions proto/cosmos/staking/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ message GenesisState {

// exported defines a bool to identify whether the chain dealing with exported or initialized genesis.
bool exported = 8;

// periods defines the supported staking periods.
Periods periods = 9 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];

TokenTypes token_types = 10 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

// LastValidatorPower required for validator set update logic.
Expand Down
10 changes: 0 additions & 10 deletions proto/cosmos/staking/v1beta1/staking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ message TokenTypeInfo {
];
}

message TokenTypes {
map<int32, TokenTypeInfo> token_type_info_map = 1;
}

// Validator defines a validator, together with the total amount of the
// Validator's bond shares and their exchange rate to coins. Slashing results in
// a decrease in the exchange rate, allowing correct calculation of future
Expand Down Expand Up @@ -238,10 +234,6 @@ message Period {
];
}

message Periods {
map<int32, Period> period_map = 1;
}

message PeriodDelegation {
string period_delegation_id = 1 [(amino.dont_omitempty) = true];

Expand Down Expand Up @@ -286,8 +278,6 @@ message Delegation {
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];

map<string, PeriodDelegation> period_delegations = 5;
}

// UnbondingDelegation stores all of a single delegator's unbonding bonds
Expand Down
36 changes: 0 additions & 36 deletions tests/fixtures/adr-024-coin-metadata_genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,42 +211,6 @@
"delegations": null,
"unbonding_delegations": null,
"redelegations": null,
"periods": {
"period_map": {
"0":{
"duration": "0s",
"period_type": 0,
"rewards_multiplier": "1.000000000000000000"
},
"1": {
"duration": "7776000s",
"period_type": 1,
"rewards_multiplier": "1.051000000000000000"
},
"2": {
"duration": "31536000s",
"period_type": 2,
"rewards_multiplier": "1.160000000000000000"
},
"3": {
"duration": "46656000s",
"period_type": 3,
"rewards_multiplier": "1.340000000000000000"
}
}
},
"token_types": {
"token_type_info_map": {
"0": {
"rewards_multiplier": "0.500000000000000000",
"token_type": 0
},
"1": {
"rewards_multiplier": "1.000000000000000000",
"token_type": 1
}
}
},
"exported": false
}
}
Expand Down
14 changes: 10 additions & 4 deletions tests/integration/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,11 +509,17 @@ func TestGRPCDelegationRewards(t *testing.T) {
// setup delegation
delTokens := sdk.TokensFromConsensusPower(2, sdk.DefaultPowerReduction)
validator, issuedShares, _ := val.AddTokensFromDel(delTokens, math.LegacyOneDec())
delegation := stakingtypes.NewDelegation(delAddr.String(), f.valAddr.String(), issuedShares, issuedShares,
stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE,
time.Unix(0, 0),
)
delegation := stakingtypes.NewDelegation(delAddr.String(), f.valAddr.String(), issuedShares, issuedShares)
assert.NilError(t, f.stakingKeeper.SetDelegation(f.sdkCtx, delegation))
periodDel := stakingtypes.NewPeriodDelegation(
stakingtypes.FlexiblePeriodDelegationID,
issuedShares,
issuedShares,
stakingtypes.PeriodType_FLEXIBLE,
time.Time{},
)
assert.NilError(t, f.stakingKeeper.SetPeriodDelegation(f.sdkCtx, delAddr, f.valAddr, periodDel))

valBz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
assert.NilError(t, err)
assert.NilError(t, f.distrKeeper.SetDelegatorStartingInfo(f.sdkCtx, valBz, delAddr, types.NewDelegatorStartingInfo(2, math.LegacyNewDec(initialStake), 20)))
Expand Down
13 changes: 9 additions & 4 deletions tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,16 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) {

valBz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
require.NoError(t, err)
delegation := stakingtypes.NewDelegation(delAddr.String(), validator.GetOperator(), issuedShares, issuedShares,
stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE,
time.Unix(0, 0),
)
delegation := stakingtypes.NewDelegation(delAddr.String(), validator.GetOperator(), issuedShares, issuedShares)
require.NoError(t, f.stakingKeeper.SetDelegation(f.sdkCtx, delegation))
periodDel := stakingtypes.NewPeriodDelegation(
stakingtypes.FlexiblePeriodDelegationID,
issuedShares,
issuedShares,
stakingtypes.PeriodType_FLEXIBLE,
time.Time{},
)
assert.NilError(t, f.stakingKeeper.SetPeriodDelegation(f.sdkCtx, delAddr, f.valAddr, periodDel))
require.NoError(t, f.distrKeeper.SetDelegatorStartingInfo(f.sdkCtx, valBz, delAddr, distrtypes.NewDelegatorStartingInfo(2, math.LegacyOneDec(), 20)))
// setup validator rewards
decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyOneDec())}
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func createValidators(t *testing.T, f *fixture, powers []int64) ([]sdk.AccAddres
f.stakingKeeper.SetNewValidatorByPowerIndex(f.ctx, val2)
f.stakingKeeper.SetNewValidatorByPowerIndex(f.ctx, val3)

_, _, _ = f.stakingKeeper.Delegate(f.ctx, addrs[0], f.stakingKeeper.TokensFromConsensusPower(f.ctx, powers[0]), stakingtypes.Unbonded, val1, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, _ = f.stakingKeeper.Delegate(f.ctx, addrs[1], f.stakingKeeper.TokensFromConsensusPower(f.ctx, powers[1]), stakingtypes.Unbonded, val2, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, _ = f.stakingKeeper.Delegate(f.ctx, addrs[2], f.stakingKeeper.TokensFromConsensusPower(f.ctx, powers[2]), stakingtypes.Unbonded, val3, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, _ = f.stakingKeeper.Delegate(f.ctx, addrs[0], f.stakingKeeper.TokensFromConsensusPower(f.ctx, powers[0]), stakingtypes.Unbonded, val1, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, _ = f.stakingKeeper.Delegate(f.ctx, addrs[1], f.stakingKeeper.TokensFromConsensusPower(f.ctx, powers[1]), stakingtypes.Unbonded, val2, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, _ = f.stakingKeeper.Delegate(f.ctx, addrs[2], f.stakingKeeper.TokensFromConsensusPower(f.ctx, powers[2]), stakingtypes.Unbonded, val3, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))

f.stakingKeeper.EndBlocker(f.ctx)

Expand Down
18 changes: 9 additions & 9 deletions tests/integration/gov/keeper/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func TestTallyDelgatorOverride(t *testing.T) {
val1, found := f.stakingKeeper.GetValidator(ctx, valAddrs[0])
assert.Assert(t, found)

_, _, err := f.stakingKeeper.Delegate(ctx, addrs[4], delTokens, stakingtypes.Unbonded, val1, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, err := f.stakingKeeper.Delegate(ctx, addrs[4], delTokens, stakingtypes.Unbonded, val1, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
assert.NilError(t, err)

f.stakingKeeper.EndBlocker(ctx)
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestTallyDelgatorInherit(t *testing.T) {
val3, found := f.stakingKeeper.GetValidator(ctx, vals[2])
assert.Assert(t, found)

_, _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
assert.NilError(t, err)

f.stakingKeeper.EndBlocker(ctx)
Expand Down Expand Up @@ -358,9 +358,9 @@ func TestTallyDelgatorMultipleOverride(t *testing.T) {
val2, found := f.stakingKeeper.GetValidator(ctx, vals[1])
assert.Assert(t, found)

_, _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val1, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val1, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
assert.NilError(t, err)
_, _, err = f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, err = f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
assert.NilError(t, err)

f.stakingKeeper.EndBlocker(ctx)
Expand Down Expand Up @@ -403,9 +403,9 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) {
val3, found := f.stakingKeeper.GetValidator(ctx, vals[2])
assert.Assert(t, found)

_, _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
assert.NilError(t, err)
_, _, err = f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, err = f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
assert.NilError(t, err)

f.stakingKeeper.EndBlocker(ctx)
Expand Down Expand Up @@ -445,9 +445,9 @@ func TestTallyJailedValidator(t *testing.T) {
val3, found := f.stakingKeeper.GetValidator(ctx, valAddrs[2])
assert.Assert(t, found)

_, _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, err := f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
assert.NilError(t, err)
_, _, err = f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, err = f.stakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
assert.NilError(t, err)

f.stakingKeeper.EndBlocker(ctx)
Expand Down Expand Up @@ -489,7 +489,7 @@ func TestTallyValidatorMultipleDelegations(t *testing.T) {
val2, found := f.stakingKeeper.GetValidator(ctx, valAddrs[1])
assert.Assert(t, found)

_, _, err := f.stakingKeeper.Delegate(ctx, addrs[0], delTokens, stakingtypes.Unbonded, val2, true, stakingtypes.FlexibleDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
_, _, err := f.stakingKeeper.Delegate(ctx, addrs[0], delTokens, stakingtypes.Unbonded, val2, true, stakingtypes.FlexiblePeriodDelegationID, stakingtypes.PeriodType_FLEXIBLE, time.Unix(0, 0))
assert.NilError(t, err)

tp := TestProposal
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/staking/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ func createValidators(t *testing.T, f *fixture, powers []int64) ([]sdk.AccAddres

_, _, err := f.stakingKeeper.Delegate(
f.sdkCtx, addrs[0], f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, powers[0]), types.Unbonded, val1,
true, types.FlexibleDelegationID, types.PeriodType_FLEXIBLE, time.Unix(0, 0),
true, types.FlexiblePeriodDelegationID, types.PeriodType_FLEXIBLE, time.Unix(0, 0),
)
assert.NilError(t, err)
_, _, err = f.stakingKeeper.Delegate(
f.sdkCtx, addrs[1], f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, powers[1]), types.Unbonded, val2,
true, types.FlexibleDelegationID, types.PeriodType_FLEXIBLE, time.Unix(0, 0),
true, types.FlexiblePeriodDelegationID, types.PeriodType_FLEXIBLE, time.Unix(0, 0),
)
assert.NilError(t, err)
_, _, err = f.stakingKeeper.Delegate(
f.sdkCtx, addrs[0], f.stakingKeeper.TokensFromConsensusPower(f.sdkCtx, powers[2]), types.Unbonded, val2,
true, types.FlexibleDelegationID, types.PeriodType_FLEXIBLE, time.Unix(0, 0),
true, types.FlexiblePeriodDelegationID, types.PeriodType_FLEXIBLE, time.Unix(0, 0),
)
assert.NilError(t, err)
applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, -1)
Expand Down
17 changes: 11 additions & 6 deletions tests/integration/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
assert.Assert(t, validator.IsBonded())

delegation := types.NewDelegation(
addrDel.String(), addrVal.String(), issuedShares, issuedShares, types.FlexibleDelegationID,
addrDel.String(), addrVal.String(), issuedShares, issuedShares)
assert.NilError(t, f.stakingKeeper.SetDelegation(ctx, delegation))
periodDel := types.NewPeriodDelegation(
types.FlexiblePeriodDelegationID,
issuedShares,
issuedShares,
types.PeriodType_FLEXIBLE,
time.Unix(0, 0),
time.Time{},
)
assert.NilError(t, f.stakingKeeper.SetDelegation(ctx, delegation))
assert.NilError(t, f.stakingKeeper.SetPeriodDelegation(f.sdkCtx, addrDel, addrVal, periodDel))

maxEntries, err := f.stakingKeeper.MaxEntries(ctx)
assert.NilError(t, err)
Expand All @@ -71,7 +76,7 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
var err error
ctx = ctx.WithBlockHeight(i)
var amount math.Int
completionTime, amount, err = f.stakingKeeper.Undelegate(ctx, addrDel, addrVal, types.FlexibleDelegationID, math.LegacyNewDec(1))
completionTime, amount, err = f.stakingKeeper.Undelegate(ctx, addrDel, addrVal, types.FlexiblePeriodDelegationID, math.LegacyNewDec(1))
assert.NilError(t, err)
totalUnbonded = totalUnbonded.Add(amount)
}
Expand All @@ -87,7 +92,7 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
oldNotBonded = f.bankKeeper.GetBalance(ctx, f.stakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount

// an additional unbond should fail due to max entries
_, _, err = f.stakingKeeper.Undelegate(ctx, addrDel, addrVal, types.FlexibleDelegationID, math.LegacyNewDec(1))
_, _, err = f.stakingKeeper.Undelegate(ctx, addrDel, addrVal, types.FlexiblePeriodDelegationID, math.LegacyNewDec(1))
assert.Error(t, err, "too many unbonding delegation entries for (delegator, validator) tuple")

newBonded = f.bankKeeper.GetBalance(ctx, f.stakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount
Expand All @@ -109,7 +114,7 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
oldNotBonded = f.bankKeeper.GetBalance(ctx, f.stakingKeeper.GetNotBondedPool(ctx).GetAddress(), bondDenom).Amount

// unbonding should work again
_, _, err = f.stakingKeeper.Undelegate(ctx, addrDel, addrVal, types.FlexibleDelegationID, math.LegacyNewDec(1))
_, _, err = f.stakingKeeper.Undelegate(ctx, addrDel, addrVal, types.FlexiblePeriodDelegationID, math.LegacyNewDec(1))
assert.NilError(t, err)

newBonded = f.bankKeeper.GetBalance(ctx, f.stakingKeeper.GetBondedPool(ctx).GetAddress(), bondDenom).Amount
Expand Down
Loading

0 comments on commit 18efdf9

Please sign in to comment.