From ba78c0c748bb0cc0410eb51852f0f9867cf22a71 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Mon, 16 Dec 2024 13:55:49 -0600 Subject: [PATCH 01/12] H-01: deny duplicate submission for a pending validator --- errors.go | 1 + keeper/pending.go | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/errors.go b/errors.go index 4238e29..1791787 100644 --- a/errors.go +++ b/errors.go @@ -10,4 +10,5 @@ var ( ErrNotAnAuthority = sdkerrors.Register(ModuleName, 3, "not an authority") ErrUnsafePower = sdkerrors.Register(ModuleName, 4, "unsafe: msg.Power is >30%% of total power, set unsafe=true to override") ErrWithdrawDelegatorRewardsNotAllowed = sdkerrors.Register(ModuleName, 5, "withdraw delegator rewards is not allowed on this chain") + ErrValidatorAlreadyPending = sdkerrors.Register(ModuleName, 6, "this validator is already pending") ) diff --git a/keeper/pending.go b/keeper/pending.go index 0fa712b..dadf61c 100644 --- a/keeper/pending.go +++ b/keeper/pending.go @@ -32,6 +32,12 @@ func (k Keeper) AddPendingValidator(ctx context.Context, newVal stakingtypes.Val return err } + for _, val := range vals.Validators { + if val.OperatorAddress == poaVal.OperatorAddress { + return poa.ErrValidatorAlreadyPending + } + } + vals.Validators = append(vals.Validators, poaVal) return k.PendingValidators.Set(ctx, vals) From 18b99bb9415d5dbbd02ef8b7845450e4f4da2008 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Mon, 16 Dec 2024 13:56:26 -0600 Subject: [PATCH 02/12] M-01(refactor): simplify removing validators by grabbing directly --- keeper/msg_server.go | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/keeper/msg_server.go b/keeper/msg_server.go index c0f9306..8ed59d6 100644 --- a/keeper/msg_server.go +++ b/keeper/msg_server.go @@ -97,34 +97,23 @@ func (ms msgServer) RemoveValidator(ctx context.Context, msg *poa.MsgRemoveValid } } - vals, err := ms.k.stakingKeeper.GetAllValidators(ctx) + valAddr, err := sdk.ValAddressFromBech32(msg.ValidatorAddress) if err != nil { - return nil, err - } - - if len(vals) == 1 { - return nil, fmt.Errorf("cannot remove the last validator in the set") - } - - // Ensure the validator exists and is bonded. - found := false - for _, val := range vals { - if val.OperatorAddress == msg.ValidatorAddress { - if !val.IsBonded() { - return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "validator %s is not bonded", msg.ValidatorAddress) - } - - found = true - break - } + return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err) } - if !found { + val, err := ms.k.stakingKeeper.GetValidator(ctx, valAddr) + if err != nil { + // validator not found in the set. return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "validator %s does not exist", msg.ValidatorAddress) } + // Validator must exist and be bonded for us to set to remove it from the set + if !val.IsBonded() { + return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "validator %s is not bonded", msg.ValidatorAddress) + } // Remove the validator from the active set with 0 consensus power. - val, err := ms.k.SetPOAPower(ctx, msg.ValidatorAddress, 0) + val, err = ms.k.SetPOAPower(ctx, msg.ValidatorAddress, 0) if err != nil { return nil, err } From dbe2ff8e2b07f10877c517db56e443bcc1392a69 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Mon, 16 Dec 2024 13:56:42 -0600 Subject: [PATCH 03/12] test: pending and duplicate removal --- keeper/pending_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 keeper/pending_test.go diff --git a/keeper/pending_test.go b/keeper/pending_test.go new file mode 100644 index 0000000..f87a444 --- /dev/null +++ b/keeper/pending_test.go @@ -0,0 +1,40 @@ +package keeper_test + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/strangelove-ventures/poa" + "github.com/stretchr/testify/require" +) + +func TestAddPending(t *testing.T) { + f := SetupTest(t, 2_000_000) + require := require.New(t) + + val := GenAcc() + valAddr := sdk.ValAddress(val.addr) + v := poa.ConvertPOAToStaking(CreateNewValidator( + "myval", + valAddr.String(), + val.valKey.PubKey(), + int64(1_000_000), + )) + + // successful add + err := f.k.AddPendingValidator(f.ctx, v, val.valKey.PubKey()) + require.NoError(err) + + // duplicate (fails) + err = f.k.AddPendingValidator(f.ctx, v, val.valKey.PubKey()) + require.Error(err) + require.Equal(poa.ErrValidatorAlreadyPending, err) + + // remove pending + err = f.k.RemovePendingValidator(f.ctx, v.OperatorAddress) + require.NoError(err) + + pending, err := f.k.GetPendingValidators(f.ctx) + require.NoError(err) + require.Len(pending.Validators, 0) +} From e4aa51f209d365a1253d6d8a25807ee1d3bba6d6 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Mon, 16 Dec 2024 13:59:39 -0600 Subject: [PATCH 04/12] L-01: Params.Validate staking --- keeper/msg_server.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/keeper/msg_server.go b/keeper/msg_server.go index 8ed59d6..1f4a9bc 100644 --- a/keeper/msg_server.go +++ b/keeper/msg_server.go @@ -242,5 +242,9 @@ func (ms msgServer) UpdateStakingParams(ctx context.Context, msg *poa.MsgUpdateS MinCommissionRate: msg.Params.MinCommissionRate, } + if err := stakingParams.Validate(); err != nil { + return nil, err + } + return &poa.MsgUpdateStakingParamsResponse{}, ms.k.stakingKeeper.SetParams(ctx, stakingParams) } From eee762a95833f583148a05d7aea3147398061ee9 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Mon, 16 Dec 2024 14:04:48 -0600 Subject: [PATCH 05/12] L-02: panic if floor > ciel --- ante/ante_test.go | 6 ++++++ ante/commission_limit.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/ante/ante_test.go b/ante/ante_test.go index 0b4bf75..14a6af1 100644 --- a/ante/ante_test.go +++ b/ante/ante_test.go @@ -122,6 +122,12 @@ func TestAnteNested(t *testing.T) { ctx := sdk.Context{} ctx = setBlockHeader(ctx, 2) + t.Run("fail: floor > ceil on create panic", func(t *testing.T) { + require.Panics(t, func() { + NewCommissionLimitDecorator(true, math.LegacyMustNewDecFromStr("0.50"), math.LegacyMustNewDecFromStr("0.10")) + }) + }) + const invalidRequestErr = "messages contains *types.Any which is not a sdk.MsgRequest" cases := []struct { name string diff --git a/ante/commission_limit.go b/ante/commission_limit.go index 750cd75..d7a05d7 100644 --- a/ante/commission_limit.go +++ b/ante/commission_limit.go @@ -24,6 +24,10 @@ type CommissionLimitDecorator struct { } func NewCommissionLimitDecorator(doGenTxRateValidation bool, rateFloor, rateCiel math.LegacyDec) CommissionLimitDecorator { + if rateFloor.GT(rateCiel) { + panic(fmt.Sprintf("NewCommissionLimitDecorator: rateFloor %v is greater than rateCiel %v", rateFloor, rateCiel)) + } + return CommissionLimitDecorator{ DoGenTxRateValidation: doGenTxRateValidation, RateFloor: rateFloor, From a67adf9e4d0ffa215b1d74fd46dcb2998c8b4c87 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Mon, 16 Dec 2024 14:20:15 -0600 Subject: [PATCH 06/12] L-03: fix validators if commission is below updated commission rate --- keeper/expected_keepers.go | 1 + keeper/msg_server.go | 34 ++++++++++++++++++++++++++++ keeper/msg_server_test.go | 46 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/keeper/expected_keepers.go b/keeper/expected_keepers.go index a8ad1b3..d1d6c57 100644 --- a/keeper/expected_keepers.go +++ b/keeper/expected_keepers.go @@ -52,6 +52,7 @@ type StakingKeeper interface { MinCommissionRate(ctx context.Context) (math.LegacyDec, error) GetValidatorByConsAddr(ctx context.Context, consAddr sdk.ConsAddress) (validator stakingtypes.Validator, err error) SetParams(ctx context.Context, params stakingtypes.Params) error + GetParams(ctx context.Context) (stakingtypes.Params, error) GetAllDelegations(ctx context.Context) (delegations []stakingtypes.Delegation, err error) BondDenom(ctx context.Context) (string, error) ValidatorAddressCodec() addresscodec.Codec diff --git a/keeper/msg_server.go b/keeper/msg_server.go index 1f4a9bc..b885ca8 100644 --- a/keeper/msg_server.go +++ b/keeper/msg_server.go @@ -233,6 +233,40 @@ func (ms msgServer) UpdateStakingParams(ctx context.Context, msg *poa.MsgUpdateS return nil, poa.ErrNotAnAuthority } + prevStakingParams, err := ms.k.stakingKeeper.GetParams(ctx) + if err != nil { + return nil, err + } + + // https://github.com/liftedinit/cosmos-sdk/blob/a877e3e8048a5acb07a0bff92bd8498cd24d1a01/x/staking/keeper/msg_server.go#L619-L642 + // when min commission rate is updated, we need to update the commission rate of all validators + if !prevStakingParams.MinCommissionRate.Equal(msg.Params.MinCommissionRate) { + minRate := msg.Params.MinCommissionRate + + vals, err := ms.k.stakingKeeper.GetAllValidators(ctx) + if err != nil { + return nil, err + } + + blockTime := sdk.UnwrapSDKContext(ctx).BlockHeader().Time + + for _, val := range vals { + // set the commission rate to min rate + if val.Commission.CommissionRates.Rate.LT(minRate) { + val.Commission.CommissionRates.Rate = minRate + // set the max rate to minRate if it is less than min rate + if val.Commission.CommissionRates.MaxRate.LT(minRate) { + val.Commission.CommissionRates.MaxRate = minRate + } + + val.Commission.UpdateTime = blockTime + if err := ms.k.stakingKeeper.SetValidator(ctx, val); err != nil { + return nil, fmt.Errorf("failed to set validator after MinCommissionRate param change: %w", err) + } + } + } + } + stakingParams := stakingtypes.Params{ UnbondingTime: msg.Params.UnbondingTime, MaxValidators: msg.Params.MaxValidators, diff --git a/keeper/msg_server_test.go b/keeper/msg_server_test.go index 5c7b9c5..bfcf21f 100644 --- a/keeper/msg_server_test.go +++ b/keeper/msg_server_test.go @@ -61,6 +61,52 @@ func TestUpdateStakingParams(t *testing.T) { } } +// Verifies that if the minimum commission rate is brought up, all validators with too low of a commission are updated. +// i.e.: +// - MinCommission is 0, change is now 1%, all validators below 1% are updated to 1%. +// - MinCommission is 1%, but now changed back down to 0%. No changes. +func TestUpdateStakingParamChangeValidatorMinCommission(t *testing.T) { + f := SetupTest(t, 2_000_000) + require := require.New(t) + + // current staking params + sp, err := f.k.GetStakingKeeper().GetParams(f.ctx) + require.NoError(err) + require.True(sp.MinCommissionRate.Equal(sdkmath.LegacyZeroDec())) + + // verify the validator is at the current minimum commission rate (0%) + vals, err := f.stakingKeeper.GetValidators(f.ctx, 1) + require.NoError(err) + require.True(vals[0].Commission.Rate.Equal(sp.MinCommissionRate)) + + // increase the min commission rate to 1% + p := poa.DefaultStakingParams() + p.MinCommissionRate = sdkmath.LegacyMustNewDecFromStr("0.01") + _, err = f.msgServer.UpdateStakingParams(f.ctx, &poa.MsgUpdateStakingParams{ + Sender: f.authorityAddr, + Params: p, + }) + require.NoError(err) + + // valiadte the validator is now at 1% (the new updated mincommission) + vals, err = f.stakingKeeper.GetValidators(f.ctx, 1) + require.NoError(err) + require.True(vals[0].Commission.Rate.Equal(p.MinCommissionRate)) + + // set commission rate back to 0 + p.MinCommissionRate = sdkmath.LegacyZeroDec() + _, err = f.msgServer.UpdateStakingParams(f.ctx, &poa.MsgUpdateStakingParams{ + Sender: f.authorityAddr, + Params: p, + }) + require.NoError(err) + + // no updates made to the validator. It is still at 1%. + postChangeVals, err := f.stakingKeeper.GetValidators(f.ctx, 1) + require.NoError(err) + require.NotEqual(postChangeVals[0].Commission.Rate, p.MinCommissionRate) +} + func TestSetPowerAndCreateValidator(t *testing.T) { f := SetupTest(t, 2_000_000) require := require.New(t) From 8b31e6d0c0700bd59b3be3cb344b9d170dc28b1b Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Mon, 16 Dec 2024 14:22:42 -0600 Subject: [PATCH 07/12] L-04: duplicate validator verification on InitGenesis --- keeper/genesis.go | 9 +++++++++ keeper/genesis_test.go | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/keeper/genesis.go b/keeper/genesis.go index 9b6c205..6c657f2 100644 --- a/keeper/genesis.go +++ b/keeper/genesis.go @@ -2,12 +2,21 @@ package keeper import ( "context" + "fmt" "github.com/strangelove-ventures/poa" ) // InitGenesis initializes the module's state from a genesis state. func (k *Keeper) InitGenesis(ctx context.Context, data *poa.GenesisState) error { + found := make(map[string]bool) + for _, vals := range data.Vals { + if found[vals.OperatorAddress] { + return fmt.Errorf("duplicate validator found in genesis state: %s", vals.OperatorAddress) + } + found[vals.OperatorAddress] = true + } + if err := k.PendingValidators.Set(ctx, poa.Validators{ Validators: data.Vals, }); err != nil { diff --git a/keeper/genesis_test.go b/keeper/genesis_test.go index f3defd3..cac1e33 100644 --- a/keeper/genesis_test.go +++ b/keeper/genesis_test.go @@ -23,6 +23,18 @@ func TestInitGenesis(t *testing.T) { require.NoError(err) }) + t.Run("duplicate validators found in state", func(t *testing.T) { + data := &poa.GenesisState{ + Vals: []poa.Validator{{ + OperatorAddress: "cosmos1abc", + }, { + OperatorAddress: "cosmos1abc", + }}, + } + err := fixture.k.InitGenesis(fixture.ctx, data) + require.Error(err) + }) + t.Run("pending validator export", func(t *testing.T) { acc := GenAcc() valAddr := sdk.ValAddress(acc.addr) From 84bbfa16a560930826bcc4fb5916a4d7f109d540 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Tue, 17 Dec 2024 18:59:00 -0600 Subject: [PATCH 08/12] chore: lint --- keeper/pending_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/keeper/pending_test.go b/keeper/pending_test.go index f87a444..58a173a 100644 --- a/keeper/pending_test.go +++ b/keeper/pending_test.go @@ -3,9 +3,11 @@ package keeper_test import ( "testing" + "github.com/stretchr/testify/require" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/strangelove-ventures/poa" - "github.com/stretchr/testify/require" ) func TestAddPending(t *testing.T) { @@ -36,5 +38,5 @@ func TestAddPending(t *testing.T) { pending, err := f.k.GetPendingValidators(f.ctx) require.NoError(err) - require.Len(pending.Validators, 0) + require.Empty(pending.Validators) } From 8a0e7231b93194dc0a44a20b31dc14dedb0c7764 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Tue, 17 Dec 2024 19:10:02 -0600 Subject: [PATCH 09/12] fix: simulation (no duplicate create validators) --- simulation/operations.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/simulation/operations.go b/simulation/operations.go index 552fb68..d0de294 100644 --- a/simulation/operations.go +++ b/simulation/operations.go @@ -1,6 +1,7 @@ package simulation import ( + "fmt" "math" "math/rand" "os" @@ -119,6 +120,18 @@ func SimulateMsgCreateValidator(txGen client.TxConfig, k keeper.Keeper) simtypes description := generateRandomDescription(r) commission := generateRandomCommission(r) + vals, err := k.PendingValidators.Get(ctx) + if err != nil { + return simtypes.OperationMsg{}, nil, nil + } + for _, v := range vals.Validators { + if v.OperatorAddress == address.String() { + fmt.Println(v.OperatorAddress, address.String()) + fmt.Println("skipping due to validator already being in PendingValidators") + return simtypes.OperationMsg{}, nil, nil + } + } + msg, err := poatypes.NewMsgCreateValidator(address.String(), simAccount.ConsKey.PubKey(), description, commission, selfDelegation.Amount) if err != nil { return simtypes.NoOpMsg(poatypes.ModuleName, msgType, errors.WithMessage(err, "unable to create MsgCreateValidator").Error()), nil, err From 1410712b1c94608ee93a748f4e9b421e6a51acab Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Tue, 17 Dec 2024 19:11:17 -0600 Subject: [PATCH 10/12] fix lint --- simulation/operations.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simulation/operations.go b/simulation/operations.go index d0de294..ab03a33 100644 --- a/simulation/operations.go +++ b/simulation/operations.go @@ -122,7 +122,7 @@ func SimulateMsgCreateValidator(txGen client.TxConfig, k keeper.Keeper) simtypes vals, err := k.PendingValidators.Get(ctx) if err != nil { - return simtypes.OperationMsg{}, nil, nil + return simtypes.OperationMsg{}, nil, nil // nolint:nilerr } for _, v := range vals.Validators { if v.OperatorAddress == address.String() { From f839da3fa66d62ab65954a5d72ab9bf89508952d Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Tue, 17 Dec 2024 19:20:00 -0600 Subject: [PATCH 11/12] lint remove fmt prints --- simulation/operations.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/simulation/operations.go b/simulation/operations.go index ab03a33..4fcda3d 100644 --- a/simulation/operations.go +++ b/simulation/operations.go @@ -1,7 +1,6 @@ package simulation import ( - "fmt" "math" "math/rand" "os" @@ -126,8 +125,6 @@ func SimulateMsgCreateValidator(txGen client.TxConfig, k keeper.Keeper) simtypes } for _, v := range vals.Validators { if v.OperatorAddress == address.String() { - fmt.Println(v.OperatorAddress, address.String()) - fmt.Println("skipping due to validator already being in PendingValidators") return simtypes.OperationMsg{}, nil, nil } } From ac705e9e530620cb14a3f4cb576476a95c9d55ad Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Tue, 17 Dec 2024 19:23:58 -0600 Subject: [PATCH 12/12] refactor/lint: remove typecast from int64 -> uint64 --- ante/ante_test.go | 8 ++++---- keeper/keeper_test.go | 12 +++++++----- keeper/msg_server.go | 2 +- keeper/msg_server_test.go | 4 ++-- keeper/pending_test.go | 2 +- keeper/poa.go | 13 +++++++------ 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/ante/ante_test.go b/ante/ante_test.go index 14a6af1..b56827b 100644 --- a/ante/ante_test.go +++ b/ante/ante_test.go @@ -253,7 +253,7 @@ func TestAnteStakingFilter(t *testing.T) { }) t.Run(fmt.Sprintf("fail: staking action not allowed after gentx (%s)", k), func(t *testing.T) { - for h := uint64(2); h < 10; h++ { + for h := int64(2); h < 10; h++ { ctx = setBlockHeader(ctx, h) _, err := sf.AnteHandle(ctx, tx, false, EmptyAnte) require.Error(t, err) @@ -284,7 +284,7 @@ func TestAnteDisableWithdrawRewards(t *testing.T) { }) t.Run(fmt.Sprintf("fail: withdraw rewards not allowed after gentx (%s)", k), func(t *testing.T) { - for h := uint64(2); h < 10; h++ { + for h := int64(2); h < 10; h++ { ctx = setBlockHeader(ctx, h) _, err := dwr.AnteHandle(ctx, tx, false, EmptyAnte) require.Error(t, err) @@ -293,9 +293,9 @@ func TestAnteDisableWithdrawRewards(t *testing.T) { } } -func setBlockHeader(ctx sdk.Context, height uint64) sdk.Context { +func setBlockHeader(ctx sdk.Context, height int64) sdk.Context { h := ctx.BlockHeader() - h.Height = int64(height) + h.Height = height return ctx.WithBlockHeader(h) } diff --git a/keeper/keeper_test.go b/keeper/keeper_test.go index 7c4d1c2..a760100 100644 --- a/keeper/keeper_test.go +++ b/keeper/keeper_test.go @@ -221,7 +221,7 @@ func (f *testFixture) createBaseStakingValidators(t *testing.T, baseValShares in fmt.Sprintf("val-%d", idx), valAddr, pubKey, - bondCoin.Amount.Int64(), + bondCoin.Amount.Uint64(), )) if err := f.k.AddPendingValidator(f.ctx, val, pubKey); err != nil { @@ -277,7 +277,7 @@ func (f *testFixture) createBaseStakingValidators(t *testing.T, baseValShares in } } -func CreateNewValidator(moniker string, opAddr string, pubKey cryptotypes.PubKey, amt int64) poa.Validator { +func CreateNewValidator(moniker string, opAddr string, pubKey cryptotypes.PubKey, amt uint64) poa.Validator { var pkAny *codectypes.Any if pubKey != nil { var err error @@ -286,13 +286,15 @@ func CreateNewValidator(moniker string, opAddr string, pubKey cryptotypes.PubKey } } + sdkIntAmt := sdkmath.NewIntFromUint64(amt) + return poa.Validator{ OperatorAddress: opAddr, ConsensusPubkey: pkAny, Jailed: false, Status: poa.Bonded, - Tokens: sdkmath.NewInt(amt), - DelegatorShares: sdkmath.LegacyNewDecFromInt(sdkmath.NewInt(amt)), + Tokens: sdkIntAmt, + DelegatorShares: sdkmath.LegacyNewDecFromInt(sdkIntAmt), Description: poa.NewDescription(moniker, "", "", "", ""), UnbondingHeight: 0, UnbondingTime: time.Time{}, @@ -313,7 +315,7 @@ func (f *testFixture) CreatePendingValidator(name string, power uint64) sdk.ValA name, valAddr.String(), val.valKey.PubKey(), - int64(power), + power, )) if err := f.k.AddPendingValidator(f.ctx, v, val.valKey.PubKey()); err != nil { diff --git a/keeper/msg_server.go b/keeper/msg_server.go index b885ca8..0a160fb 100644 --- a/keeper/msg_server.go +++ b/keeper/msg_server.go @@ -45,7 +45,7 @@ func (ms msgServer) SetPower(ctx context.Context, msg *poa.MsgSetPower) (*poa.Ms } // Sets the new POA power to the validator. - if _, err := ms.k.SetPOAPower(ctx, msg.ValidatorAddress, int64(msg.Power)); err != nil { + if _, err := ms.k.SetPOAPower(ctx, msg.ValidatorAddress, msg.Power); err != nil { return nil, err } diff --git a/keeper/msg_server_test.go b/keeper/msg_server_test.go index bfcf21f..cb969c3 100644 --- a/keeper/msg_server_test.go +++ b/keeper/msg_server_test.go @@ -261,12 +261,12 @@ func TestRemoveValidator(t *testing.T) { require.NoError(err) for _, v := range vals { - power := 10_000_000 + var power uint64 = 10_000_000 _, err = f.msgServer.SetPower(f.ctx, &poa.MsgSetPower{ Sender: f.addrs[0].String(), ValidatorAddress: v.OperatorAddress, - Power: uint64(power), + Power: power, Unsafe: true, }) require.NoError(err) diff --git a/keeper/pending_test.go b/keeper/pending_test.go index 58a173a..f2c760c 100644 --- a/keeper/pending_test.go +++ b/keeper/pending_test.go @@ -20,7 +20,7 @@ func TestAddPending(t *testing.T) { "myval", valAddr.String(), val.valKey.PubKey(), - int64(1_000_000), + 1_000_000, )) // successful add diff --git a/keeper/poa.go b/keeper/poa.go index ca43c9e..f876a1c 100644 --- a/keeper/poa.go +++ b/keeper/poa.go @@ -16,9 +16,9 @@ import ( ) // UpdateValidatorSet updates a validator to their new share and consensus power, then updates the total power of the set. -func (k Keeper) UpdateValidatorSet(ctx context.Context, newShares, newConsensusPower int64, val stakingtypes.Validator, valAddr sdk.ValAddress) error { - newShare := sdkmath.LegacyNewDec(newShares) - newShareInt := sdkmath.NewIntFromUint64(uint64(newShares)) +func (k Keeper) UpdateValidatorSet(ctx context.Context, newShares uint64, newConsensusPower int64, val stakingtypes.Validator, valAddr sdk.ValAddress) error { + newShareInt := sdkmath.NewIntFromUint64(newShares) + newShare := sdkmath.LegacyNewDecFromInt(newShareInt) delAddr := sdk.AccAddress(valAddr.Bytes()) delegation := stakingtypes.Delegation{ @@ -52,9 +52,10 @@ func (k Keeper) UpdateValidatorSet(ctx context.Context, newShares, newConsensusP // - sets a single delegation for POA power // - updates the validator with the new shares, single delegation // - sets the last validator power to the new value. -func (k Keeper) SetPOAPower(ctx context.Context, valOpBech32 string, newShares int64) (stakingtypes.Validator, error) { +func (k Keeper) SetPOAPower(ctx context.Context, valOpBech32 string, newShares uint64) (stakingtypes.Validator, error) { // 1 Consenus Power = 1_000_000 shares by default - newBFTConsensusPower := k.stakingKeeper.TokensToConsensusPower(ctx, sdkmath.NewInt(newShares)) + amt := sdkmath.NewIntFromUint64(newShares) + newBFTConsensusPower := k.stakingKeeper.TokensToConsensusPower(ctx, amt) valAddr, err := sdk.ValAddressFromBech32(valOpBech32) if err != nil { @@ -79,7 +80,7 @@ func (k Keeper) SetPOAPower(ctx context.Context, valOpBech32 string, newShares i // When we SetValidatorByPowerIndex, the Tokens are used to get the shares of power for CometBFT consensus (voting_power). // We don't `k.stakingKeeper.SetValidator` since we only use this for CometBFT consensus power. - val.Tokens = sdkmath.NewIntFromUint64(uint64(newShares)) + val.Tokens = amt // slash all the validator's tokens (100%) if newShares == 0 && currentPower > 0 {