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

     1  //go:build ignore
     2  
     3  package keeper
     4  
     5  import (
     6  	"encoding/binary"
     7  	"testing"
     8  
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    10  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    11  	"github.com/fibonacci-chain/fbc/x/common"
    12  	"github.com/fibonacci-chain/fbc/x/farm/types"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestPoolCurrentReward(t *testing.T) {
    17  	ctx, k := GetKeeper(t)
    18  	cdc := codec.New()
    19  
    20  	poolNames := []string{"pool1", "pool2"}
    21  	for _, poolName := range poolNames {
    22  		poolCur := types.NewPoolCurrentRewards(100, 3, sdk.SysCoins{})
    23  		k.Keeper.SetPoolCurrentRewards(ctx, poolName, poolCur)
    24  		poolHis1 := types.NewPoolHistoricalRewards(sdk.SysCoins{}, 1)
    25  		k.Keeper.SetPoolHistoricalRewards(ctx, poolName, 1, poolHis1)
    26  		poolHis2 := types.NewPoolHistoricalRewards(sdk.SysCoins{}, 1)
    27  		k.Keeper.SetPoolHistoricalRewards(ctx, poolName, 2, poolHis2)
    28  	}
    29  
    30  	require.True(t, k.Keeper.HasPoolCurrentRewards(ctx, poolNames[0]))
    31  	require.True(t, k.Keeper.HasPoolCurrentRewards(ctx, poolNames[1]))
    32  
    33  	var curs []types.PoolCurrentRewards
    34  	k.Keeper.IterateAllPoolCurrentRewards(ctx, func(poolName string, rewards types.PoolCurrentRewards) bool {
    35  		curs = append(curs, rewards)
    36  		return false
    37  	})
    38  	require.Equal(t, len(poolNames), len(curs))
    39  
    40  	var historicals []types.PoolHistoricalRewards
    41  	k.Keeper.IteratePoolHistoricalRewards(
    42  		ctx, poolNames[0],
    43  		func(store sdk.KVStore, key []byte, value []byte) (stop bool) {
    44  			var rewards types.PoolHistoricalRewards
    45  			cdc.MustUnmarshalBinaryLengthPrefixed(value, &rewards)
    46  			historicals = append(historicals, rewards)
    47  			return false
    48  		},
    49  	)
    50  	require.Equal(t, 2, len(historicals))
    51  
    52  	historicals = make([]types.PoolHistoricalRewards, 0)
    53  	k.Keeper.IterateAllPoolHistoricalRewards(
    54  		ctx,
    55  		func(poolName string, period uint64, rewards types.PoolHistoricalRewards) (stop bool) {
    56  			historicals = append(historicals, rewards)
    57  			return false
    58  		},
    59  	)
    60  	require.Equal(t, 4, len(historicals))
    61  
    62  	k.Keeper.DeletePoolCurrentRewards(ctx, poolNames[0])
    63  	k.Keeper.DeletePoolCurrentRewards(ctx, poolNames[1])
    64  	require.False(t, k.Keeper.HasPoolCurrentRewards(ctx, poolNames[0]))
    65  	require.False(t, k.Keeper.HasPoolCurrentRewards(ctx, poolNames[1]))
    66  }
    67  
    68  func TestGetPoolHistoricalRewardsPoolNamePeriod(t *testing.T) {
    69  	period := uint64(10)
    70  	poolName := common.GetFixedLengthRandomString(types.MaxPoolNameLength + 1)
    71  	key := types.GetPoolHistoricalRewardsKey(poolName, period)
    72  	require.Panics(t, func() { GetPoolHistoricalRewardsPoolNamePeriod(key) })
    73  
    74  	b := make([]byte, 4)
    75  	binary.LittleEndian.PutUint32(b, uint32(period))
    76  	key = append(types.PoolHistoricalRewardsPrefix, append([]byte("pool"), b...)...)
    77  	require.Panics(t, func() { GetPoolHistoricalRewardsPoolNamePeriod(key) })
    78  
    79  	poolName = "pool"
    80  	key = types.GetPoolHistoricalRewardsKey(poolName, period)
    81  	retPoolName, retPeriod := GetPoolHistoricalRewardsPoolNamePeriod(key)
    82  	require.Equal(t, poolName, retPoolName)
    83  	require.Equal(t, period, retPeriod)
    84  }