2020-04-21 14:33:56 -07:00
|
|
|
package simulation_test
|
2019-08-13 15:16:03 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
|
|
|
|
2020-04-21 14:33:56 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2019-08-13 15:16:03 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-07-30 07:53:02 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/kv"
|
2020-04-21 14:33:56 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/simulation"
|
2019-08-13 15:16:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
delPk1 = ed25519.GenPrivKey().PubKey()
|
|
|
|
delAddr1 = sdk.AccAddress(delPk1.Address())
|
|
|
|
valAddr1 = sdk.ValAddress(delPk1.Address())
|
|
|
|
consAddr1 = sdk.ConsAddress(delPk1.Address().Bytes())
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeDistributionStore(t *testing.T) {
|
2020-05-05 07:28:20 -07:00
|
|
|
cdc, _ := simapp.MakeCodecs()
|
2020-04-21 14:33:56 -07:00
|
|
|
dec := simulation.NewDecodeStore(cdc)
|
2019-08-13 15:16:03 -07:00
|
|
|
|
|
|
|
decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.OneDec())}
|
|
|
|
feePool := types.InitialFeePool()
|
|
|
|
feePool.CommunityPool = decCoins
|
|
|
|
info := types.NewDelegatorStartingInfo(2, sdk.OneDec(), 200)
|
2020-02-11 06:58:37 -08:00
|
|
|
outstanding := types.ValidatorOutstandingRewards{Rewards: decCoins}
|
|
|
|
commission := types.ValidatorAccumulatedCommission{Commission: decCoins}
|
2019-08-13 15:16:03 -07:00
|
|
|
historicalRewards := types.NewValidatorHistoricalRewards(decCoins, 100)
|
|
|
|
currentRewards := types.NewValidatorCurrentRewards(decCoins, 5)
|
|
|
|
slashEvent := types.NewValidatorSlashEvent(10, sdk.OneDec())
|
|
|
|
|
2020-07-30 07:53:02 -07:00
|
|
|
kvPairs := kv.Pairs{
|
|
|
|
kv.Pair{Key: types.FeePoolKey, Value: cdc.MustMarshalBinaryBare(&feePool)},
|
|
|
|
kv.Pair{Key: types.ProposerKey, Value: consAddr1.Bytes()},
|
|
|
|
kv.Pair{Key: types.GetValidatorOutstandingRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(&outstanding)},
|
|
|
|
kv.Pair{Key: types.GetDelegatorWithdrawAddrKey(delAddr1), Value: delAddr1.Bytes()},
|
|
|
|
kv.Pair{Key: types.GetDelegatorStartingInfoKey(valAddr1, delAddr1), Value: cdc.MustMarshalBinaryBare(&info)},
|
|
|
|
kv.Pair{Key: types.GetValidatorHistoricalRewardsKey(valAddr1, 100), Value: cdc.MustMarshalBinaryBare(&historicalRewards)},
|
|
|
|
kv.Pair{Key: types.GetValidatorCurrentRewardsKey(valAddr1), Value: cdc.MustMarshalBinaryBare(¤tRewards)},
|
|
|
|
kv.Pair{Key: types.GetValidatorAccumulatedCommissionKey(valAddr1), Value: cdc.MustMarshalBinaryBare(&commission)},
|
|
|
|
kv.Pair{Key: types.GetValidatorSlashEventKeyPrefix(valAddr1, 13), Value: cdc.MustMarshalBinaryBare(&slashEvent)},
|
|
|
|
kv.Pair{Key: []byte{0x99}, Value: []byte{0x99}},
|
2019-08-13 15:16:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
expectedLog string
|
|
|
|
}{
|
|
|
|
{"FeePool", fmt.Sprintf("%v\n%v", feePool, feePool)},
|
|
|
|
{"Proposer", fmt.Sprintf("%v\n%v", consAddr1, consAddr1)},
|
|
|
|
{"ValidatorOutstandingRewards", fmt.Sprintf("%v\n%v", outstanding, outstanding)},
|
|
|
|
{"DelegatorWithdrawAddr", fmt.Sprintf("%v\n%v", delAddr1, delAddr1)},
|
|
|
|
{"DelegatorStartingInfo", fmt.Sprintf("%v\n%v", info, info)},
|
|
|
|
{"ValidatorHistoricalRewards", fmt.Sprintf("%v\n%v", historicalRewards, historicalRewards)},
|
|
|
|
{"ValidatorCurrentRewards", fmt.Sprintf("%v\n%v", currentRewards, currentRewards)},
|
|
|
|
{"ValidatorAccumulatedCommission", fmt.Sprintf("%v\n%v", commission, commission)},
|
|
|
|
{"ValidatorSlashEvent", fmt.Sprintf("%v\n%v", slashEvent, slashEvent)},
|
|
|
|
{"other", ""},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2019-10-17 06:47:35 -07:00
|
|
|
i, tt := i, tt
|
2019-08-13 15:16:03 -07:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
switch i {
|
|
|
|
case len(tests) - 1:
|
2020-04-21 14:33:56 -07:00
|
|
|
require.Panics(t, func() { dec(kvPairs[i], kvPairs[i]) }, tt.name)
|
2019-08-13 15:16:03 -07:00
|
|
|
default:
|
2020-04-21 14:33:56 -07:00
|
|
|
require.Equal(t, tt.expectedLog, dec(kvPairs[i], kvPairs[i]), tt.name)
|
2019-08-13 15:16:03 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|