github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/distribution/keeper/allocation_test.go (about)

     1  package keeper
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth"
    10  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    11  
    12  	"github.com/fibonacci-chain/fbc/x/distribution/types"
    13  	"github.com/fibonacci-chain/fbc/x/staking"
    14  )
    15  
    16  func TestAllocateTokensToValidatorWithCommission(t *testing.T) {
    17  	//init
    18  	ctx, _, k, sk, _ := CreateTestInputDefault(t, false, 1000)
    19  	val := sk.Validator(ctx, valOpAddr1)
    20  
    21  	// allocate tokens
    22  	tokens := NewTestSysCoins(1, sdk.Precision)
    23  	k.AllocateTokensToValidator(ctx, val, tokens)
    24  
    25  	// check commissions
    26  	expected := NewTestSysCoins(1, sdk.Precision)
    27  	require.Equal(t, expected, k.GetValidatorAccumulatedCommission(ctx, val.GetOperator()))
    28  }
    29  
    30  type testAllocationParam struct {
    31  	totalPower int64
    32  	isVote     []bool
    33  	isJailed   []bool
    34  	fee        sdk.SysCoins
    35  }
    36  
    37  func getTestAllocationParams() []testAllocationParam {
    38  	return []testAllocationParam{
    39  		{ //test the case when fee is zero
    40  			10,
    41  			[]bool{true, true, true, true}, []bool{false, false, false, false},
    42  			nil,
    43  		},
    44  		{ //test the case where total power is zero
    45  			0,
    46  			[]bool{true, true, true, true}, []bool{false, false, false, false},
    47  			NewTestSysCoins(123, 2),
    48  		},
    49  		{ //test the case where just the part of vals has voted
    50  			10,
    51  			[]bool{true, true, false, false}, []bool{false, false, false, false},
    52  			NewTestSysCoins(123, 2),
    53  		},
    54  		{ //test the case where two vals is jailed
    55  			10,
    56  			[]bool{true, true, false, false}, []bool{false, true, true, false},
    57  			NewTestSysCoins(123, 2),
    58  		},
    59  	}
    60  }
    61  
    62  func TestAllocateTokensToManyValidators(t *testing.T) {
    63  	tests := getTestAllocationParams()
    64  	_, _, valConsAddrs := GetTestAddrs()
    65  
    66  	//start test
    67  	for _, test := range tests {
    68  		ctx, ak, k, sk, _ := CreateTestInputDefault(t, false, 1000)
    69  		//set the fee
    70  		setTestFees(t, ctx, k, ak, test.fee)
    71  		// crate votes info
    72  		votes := createTestVotes(ctx, sk, test)
    73  
    74  		// allocate the tokens
    75  		k.AllocateTokens(ctx, test.totalPower, valConsAddrs[0], votes)
    76  		commissions := NewTestSysCoins(0, 0)
    77  		k.IterateValidatorAccumulatedCommissions(ctx,
    78  			func(val sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool) {
    79  				commissions = commissions.Add(commission...)
    80  				return false
    81  			})
    82  		totalCommissions := k.GetDistributionAccount(ctx).GetCoins()
    83  		communityCoins := k.GetFeePoolCommunityCoins(ctx)
    84  		require.Equal(t, totalCommissions, communityCoins.Add(commissions...))
    85  		require.Equal(t, test.fee, totalCommissions)
    86  	}
    87  }
    88  
    89  func setTestFees(t *testing.T, ctx sdk.Context, k Keeper, ak auth.AccountKeeper, fees sdk.SysCoins) {
    90  	feeCollector := k.supplyKeeper.GetModuleAccount(ctx, k.feeCollectorName)
    91  	require.NotNil(t, feeCollector)
    92  	err := feeCollector.SetCoins(fees)
    93  	require.NoError(t, err)
    94  	ak.SetAccount(ctx, feeCollector)
    95  }
    96  
    97  func createTestVotes(ctx sdk.Context, sk staking.Keeper, test testAllocationParam) []abci.VoteInfo {
    98  	_, valConsPks, valConsAddrs := GetTestAddrs()
    99  	var votes []abci.VoteInfo
   100  	for i := int64(0); i < int64(len(test.isVote)); i++ {
   101  		if test.isJailed[i] {
   102  			sk.Jail(ctx, valConsAddrs[i])
   103  		}
   104  		abciVal := abci.Validator{Address: valConsPks[i].Address(), Power: i + 1}
   105  		if test.isVote[i] {
   106  			votes = append(votes, abci.VoteInfo{Validator: abciVal, SignedLastBlock: true})
   107  		}
   108  	}
   109  	return votes
   110  }