diff --git a/x/accumulator/abci.go b/x/accumulator/abci.go index ed1e3c177..715d1cce6 100644 --- a/x/accumulator/abci.go +++ b/x/accumulator/abci.go @@ -11,18 +11,19 @@ import ( // update vesting state for each admin func EndBlocker(ctx sdk.Context, k keeper.Keeper) { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) - for _, admin := range k.GetAllAdmins(ctx) { if ctx.BlockTime().Unix()-admin.LastVestingTime < admin.VestingPeriod { - return + continue } if admin.VestingCounter >= admin.VestingPeriodsCount { - return + continue } - - address, _ := sdk.AccAddressFromBech32(admin.Address) - err := k.DistributeToAccount(ctx, types.AdminPoolName, sdk.NewCoins(sdk.NewCoin(admin.Denom, admin.RewardPerPeriod.Amount)), address) + address, err := sdk.AccAddressFromBech32(admin.Address) + if err != nil { + panic(err) + } + err = k.DistributeToAccount(ctx, types.AdminPoolName, sdk.NewCoins(sdk.NewCoin(admin.Denom, admin.RewardPerPeriod.Amount)), address) if err != nil { return } diff --git a/x/accumulator/abci_test.go b/x/accumulator/abci_test.go new file mode 100644 index 000000000..1f0e8116a --- /dev/null +++ b/x/accumulator/abci_test.go @@ -0,0 +1,117 @@ +package accumulator_test + +import ( + "github.com/cosmos/cosmos-sdk/x/accumulator" + "github.com/cosmos/cosmos-sdk/x/accumulator/types" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + "testing" + "time" + + "github.com/cosmos/cosmos-sdk/simapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" +) + +func TestVerifyVesting(t *testing.T) { + app := simapp.Setup(t, false) + ctx := app.BaseApp.NewContext(true, tmproto.Header{ + Time: time.Now(), + }) + + admins := app.AccumulatorKeeper.GetAllAdmins(ctx) + require.Empty(t, admins) + + adminPool, _ := sdk.AccAddressFromBech32("cosmos1mr9ydjyr7qjuuu39qur9756pe5l8dpqlfhxd7efd2wxj58zdm9dqw07l5t") + + app.BankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000000)))) + app.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, adminPool, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000000)))) + newAdmin := types.Admin{ + Address: "cosmos1k6vrturm2249y705ygsr9lz79y30aq90sltvyr", + VestingPeriod: 1, + RewardPerPeriod: sdk.NewCoin("stake", sdk.NewInt(1000)), + VestingPeriodsCount: 10, + VestingCounter: 0, + LastVestingTime: 0, + Denom: "stake", + } + + adminAddress, _ := sdk.AccAddressFromBech32(newAdmin.Address) + app.AccumulatorKeeper.SetAdmin(ctx, newAdmin) + + for i := 0; i < int(newAdmin.VestingPeriodsCount); i++ { + accumulator.EndBlocker(ctx, app.AccumulatorKeeper) + + balance := app.BankKeeper.GetBalance(ctx, adminAddress, "stake") + require.Equal(t, balance.Amount, newAdmin.RewardPerPeriod.Amount.MulRaw(int64(i+1))) + + time.Sleep(time.Duration(newAdmin.VestingPeriod) * time.Second) + ctx = app.BaseApp.NewContext(true, tmproto.Header{ + Time: time.Now(), + }) + } + + balance := app.BankKeeper.GetBalance(ctx, adminAddress, "stake") + require.Equal(t, balance.Amount, newAdmin.RewardPerPeriod.Amount.MulRaw(newAdmin.VestingPeriodsCount)) +} + +func TestVerifyVestingForListOfAdmins(t *testing.T) { + app := simapp.Setup(t, false) + ctx := app.BaseApp.NewContext(true, tmproto.Header{ + Time: time.Now(), + }) + + admins := app.AccumulatorKeeper.GetAllAdmins(ctx) + require.Empty(t, admins) + + adminPool, _ := sdk.AccAddressFromBech32("cosmos1mr9ydjyr7qjuuu39qur9756pe5l8dpqlfhxd7efd2wxj58zdm9dqw07l5t") + app.BankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000000)))) + app.BankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, adminPool, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000000000)))) + + admin1 := types.Admin{ + Address: "cosmos1k6vrturm2249y705ygsr9lz79y30aq90sltvyr", + VestingPeriod: 1, + RewardPerPeriod: sdk.NewCoin("stake", sdk.NewInt(1000)), + VestingPeriodsCount: 10, + VestingCounter: 0, + LastVestingTime: 0, + Denom: "stake", + } + + admin2 := types.Admin{ + Address: "cosmos19tfnk0s4m095828tlm22c5q0awqf7nd97szd0z", + VestingPeriod: 3, + RewardPerPeriod: sdk.NewCoin("stake", sdk.NewInt(123)), + VestingPeriodsCount: 12, + VestingCounter: 0, + LastVestingTime: 0, + Denom: "stake", + } + admin3 := types.Admin{ + Address: "cosmos1k9ulywn8fhupem5cdcyp3rlrx2mg2n9larm2ha", + VestingPeriod: 6, + RewardPerPeriod: sdk.NewCoin("stake", sdk.NewInt(323)), + VestingPeriodsCount: 8, + VestingCounter: 0, + LastVestingTime: 0, + Denom: "stake", + } + + app.AccumulatorKeeper.SetAdmin(ctx, admin1) + app.AccumulatorKeeper.SetAdmin(ctx, admin2) + app.AccumulatorKeeper.SetAdmin(ctx, admin3) + + for i := 0; i < 50; i++ { + accumulator.EndBlocker(ctx, app.AccumulatorKeeper) + time.Sleep(time.Duration(1) * time.Second) + ctx = app.BaseApp.NewContext(true, tmproto.Header{ + Time: time.Now(), + }) + } + + for _, admin := range app.AccumulatorKeeper.GetAllAdmins(ctx) { + address, _ := sdk.AccAddressFromBech32(admin.Address) + balance := app.BankKeeper.GetBalance(ctx, address, "stake") + require.Equal(t, balance.Amount, admin.RewardPerPeriod.Amount.MulRaw(admin.VestingPeriodsCount)) + + } +} diff --git a/x/accumulator/keeper/token_distribution.go b/x/accumulator/keeper/token_distribution.go index fadaf1a70..8e990c624 100644 --- a/x/accumulator/keeper/token_distribution.go +++ b/x/accumulator/keeper/token_distribution.go @@ -17,7 +17,6 @@ func (k BaseKeeper) DistributeToModule(ctx sdk.Context, pool string, amount sdk. } func (k BaseKeeper) DistributeToAccount(ctx sdk.Context, pool string, amount sdk.Coins, receiver sdk.AccAddress) error { - poolAddress := GetPoolAddress(pool) if poolAddress == nil { return types.ErrInvalidPool diff --git a/x/mint/types/params.go b/x/mint/types/params.go index 12b41b0cf..62c507b8f 100644 --- a/x/mint/types/params.go +++ b/x/mint/types/params.go @@ -41,6 +41,7 @@ func DefaultParams() Params { MintDenom: sdk.DefaultBondDenom, BlocksPerMonth: uint64(60 * 60 * 24 * 30 / 5), // assuming 5 second block times MonthReward: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(223696209754194)), + EndBlock: 100000, } }