From 9fb884a832084c5f0644589aa5c3dc6f47f98390 Mon Sep 17 00:00:00 2001 From: kamuikatsurgi Date: Fri, 17 Jan 2025 20:05:22 +0530 Subject: [PATCH] chore: randomly select validator to send remaining amount --- x/gov/keeper/common_test.go | 5 ++++- x/gov/keeper/deposit.go | 10 +++++++--- x/gov/keeper/deposit_test.go | 24 +++++++++++++++++++----- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/x/gov/keeper/common_test.go b/x/gov/keeper/common_test.go index 2a952d89dae..1c7f0e33341 100644 --- a/x/gov/keeper/common_test.go +++ b/x/gov/keeper/common_test.go @@ -10,14 +10,16 @@ import ( milestoneTypes "github.com/0xPolygon/heimdall-v2/x/milestone/types" stakingtypes "github.com/0xPolygon/heimdall-v2/x/stake/types" topupTypes "github.com/0xPolygon/heimdall-v2/x/topup/types" + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" cmttime "github.com/cometbft/cometbft/types/time" - consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" + "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/runtime" @@ -27,6 +29,7 @@ import ( moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types" disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types" "github.com/cosmos/cosmos-sdk/x/gov/keeper" govtestutil "github.com/cosmos/cosmos-sdk/x/gov/testutil" diff --git a/x/gov/keeper/deposit.go b/x/gov/keeper/deposit.go index 0e628a4ee01..96ad7cb28a1 100644 --- a/x/gov/keeper/deposit.go +++ b/x/gov/keeper/deposit.go @@ -3,6 +3,7 @@ package keeper import ( "context" "fmt" + "math/rand" "strings" "cosmossdk.io/collections" @@ -359,10 +360,13 @@ func (keeper Keeper) DistributeAndDeleteDeposits(ctx context.Context, proposalID } if !remainingAmount.IsZero() { - // Send remaining amount to the first validator - err = keeper.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, validatorAddresses[0], remainingAmount) + // Send remaining amount to a random validator + rand := rand.New(rand.NewSource(int64(numValidators))) + randomValidator := validatorAddresses[rand.Intn(numValidators)] + + err = keeper.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, randomValidator, remainingAmount) if err != nil { - keeper.Logger(ctx).Error("Failed to send remaining coins to first validator", "error", err) + keeper.Logger(ctx).Error("Failed to send remaining coins to a random validator", "validator", randomValidator, "error", err) return err } } diff --git a/x/gov/keeper/deposit_test.go b/x/gov/keeper/deposit_test.go index 6ac5e1fabe6..3097ac74c41 100644 --- a/x/gov/keeper/deposit_test.go +++ b/x/gov/keeper/deposit_test.go @@ -481,7 +481,7 @@ func TestDistributeAndDeleteDeposits(t *testing.T) { numValidators: 2, }, { - name: "Edge case: Distribution of the remaining amount in case of truncation", + name: "Edge case: Distribution of the remaining amount to a random validator", numValidators: 3, }, } @@ -582,10 +582,24 @@ func TestDistributeAndDeleteDeposits(t *testing.T) { require.Equal(t, addr2Initial.Add(stakeAmount...), bankKeeper.GetAllBalances(ctx, TestAddrs[2])) require.Equal(t, addr3Initial.Add(stakeAmount...), bankKeeper.GetAllBalances(ctx, TestAddrs[3])) case tc.numValidators == 3: - // First validator will get a little bit of pol (NOT in the power 10**18) more because of truncation in division - require.Equal(t, addr2Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333336))), bankKeeper.GetAllBalances(ctx, TestAddrs[2])) - require.Equal(t, addr3Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333332))), bankKeeper.GetAllBalances(ctx, TestAddrs[3])) - require.Equal(t, addr4Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333332))), bankKeeper.GetAllBalances(ctx, TestAddrs[4])) + // A random validator will get a little bit of pol (NOT in the power 10**18) more because of truncation in division + addr2After := bankKeeper.GetAllBalances(ctx, TestAddrs[2]) + addr3After := bankKeeper.GetAllBalances(ctx, TestAddrs[3]) + addr4After := bankKeeper.GetAllBalances(ctx, TestAddrs[4]) + + if addr2After.AmountOf("pol").GT(addr3After.AmountOf("pol")) && addr2After.AmountOf("pol").GT(addr4After.AmountOf("pol")) { + require.Equal(t, addr2Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333336))), addr2After) + require.Equal(t, addr3Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333332))), addr3After) + require.Equal(t, addr4Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333332))), addr4After) + } else if addr3After.AmountOf("pol").GT(addr2After.AmountOf("pol")) && addr3After.AmountOf("pol").GT(addr4After.AmountOf("pol")) { + require.Equal(t, addr2Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333332))), addr2After) + require.Equal(t, addr3Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333336))), addr3After) + require.Equal(t, addr4Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333332))), addr4After) + } else { + require.Equal(t, addr2Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333332))), addr2After) + require.Equal(t, addr3Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333332))), addr3After) + require.Equal(t, addr4Initial.Add(sdk.NewCoin("pol", sdkmath.NewInt(3333333333333333336))), addr4After) + } } }) }