Skip to content

Commit

Permalink
chore: randomly select validator to send remaining amount
Browse files Browse the repository at this point in the history
  • Loading branch information
kamuikatsurgi committed Jan 17, 2025
1 parent 65d51d9 commit 9fb884a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
5 changes: 4 additions & 1 deletion x/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
10 changes: 7 additions & 3 deletions x/gov/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"fmt"
"math/rand"
"strings"

"cosmossdk.io/collections"
Expand Down Expand Up @@ -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
}
}
Expand Down
24 changes: 19 additions & 5 deletions x/gov/keeper/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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)
}
}
})
}
Expand Down

0 comments on commit 9fb884a

Please sign in to comment.