Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

!test: add genesis state for multi staking module to simapp #99

Merged
merged 7 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 67 additions & 18 deletions testing/simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"testing"
"time"

"github.com/realio-tech/multi-staking-module/testutil"
multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -68,19 +70,27 @@ func setup(withGenesis bool, invCheckPeriod uint) (*SimApp, GenesisState) {

// Setup initializes a new SimApp. A Nop logger is set in SimApp.
func Setup(isCheckTx bool) *SimApp {
privVal := mock.NewPV()
pubKey, _ := privVal.GetPubKey()
privVal0 := mock.NewPV()
privVal1 := mock.NewPV()

pubKey0, _ := privVal0.GetPubKey()
pubKey1, _ := privVal1.GetPubKey()

// create validator set with single validator
validator := tmtypes.NewValidator(pubKey, 1)
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})
val0 := tmtypes.NewValidator(pubKey0, 1)
val1 := tmtypes.NewValidator(pubKey1, 1)

valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{val0, val1})

// generate genesis account
senderPrivKey := secp256k1.GenPrivKey()
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
Coins: sdk.NewCoins(
sdk.NewCoin(testutil.MultistakingDenomA, sdk.NewInt(100000000000000)),
sdk.NewCoin(testutil.MultistakingDenomB, sdk.NewInt(100000000000000)),
),
}

app := SetupWithGenesisValSet(valSet, []authtypes.GenesisAccount{acc}, balance)
Expand Down Expand Up @@ -127,51 +137,90 @@ func genesisStateWithValSet(app *SimApp, genesisState GenesisState,
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis)

// set multi staking genesis state
msCoinAInfo := multistakingtypes.MultiStakingCoinInfo{
Denom: testutil.MultistakingDenomA,
BondWeight: sdk.OneDec(),
}
msCoinBInfo := multistakingtypes.MultiStakingCoinInfo{
Denom: testutil.MultistakingDenomB,
BondWeight: sdk.MustNewDecFromStr("0.5"),
}
coinInfos := []multistakingtypes.MultiStakingCoinInfo{msCoinAInfo, msCoinBInfo}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

msCoinInfos

validatorCoins := make([]multistakingtypes.ValidatorMultiStakingCoin, 0, len(valSet.Validators))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validatorMsCoins

validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))
locks := make([]multistakingtypes.MultiStakingLock, 0, len(valSet.Validators))
bondCoins := sdk.NewCoins()
lockCoins := sdk.NewCoins()

bondAmt := sdk.DefaultPowerReduction
for i, val := range valSet.Validators {
valDenom := testutil.MultistakingDenomA
Copy link
Collaborator

@catShaark catShaark Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valMsDenom

if i%2 == 1 {
valDenom = testutil.MultistakingDenomB
}
validatorCoins = append(validatorCoins, multistakingtypes.ValidatorMultiStakingCoin{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valMsCoins

ValAddr: sdk.ValAddress(val.Address).String(),
CoinDenom: valDenom,
})
valTokens := coinInfos[i%2].BondWeight.MulInt(bondAmt).RoundInt()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valBondCoin


for _, val := range valSet.Validators {
pk, _ := cryptocodec.FromTmPubKeyInterface(val.PubKey)
pkAny, _ := codectypes.NewAnyWithValue(pk)
validator := stakingtypes.Validator{
OperatorAddress: sdk.ValAddress(val.Address).String(),
ConsensusPubkey: pkAny,
Jailed: false,
Status: stakingtypes.Bonded,
Tokens: bondAmt,
Tokens: valTokens,
DelegatorShares: sdk.OneDec(),
Description: stakingtypes.Description{},
UnbondingHeight: int64(0),
UnbondingTime: time.Unix(0, 0).UTC(),
Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
MinSelfDelegation: sdk.ZeroInt(),
}

lockId := multistakingtypes.MultiStakingLockID(genAccs[0].GetAddress().String(), sdk.ValAddress(val.Address).String())
lockCoin := multistakingtypes.NewMultiStakingCoin(coinInfos[i%2].Denom, bondAmt, coinInfos[i%2].BondWeight)
lockRecord := multistakingtypes.NewMultiStakingLock(&lockId, lockCoin)

locks = append(locks, lockRecord)
validators = append(validators, validator)
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))

lockCoins = lockCoins.Add(sdk.NewCoin(lockCoin.Denom, lockCoin.Amount))
bondCoins = bondCoins.Add(sdk.NewCoin(sdk.DefaultBondDenom, valTokens))
}

// set validators and delegations
stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations)
genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis)

totalSupply := sdk.NewCoins()
for _, b := range balances {
// add genesis acc tokens to total supply
totalSupply = totalSupply.Add(b.Coins...)
}

for range delegations {
// add delegated tokens to total supply
totalSupply = totalSupply.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))
multistakingGenesis := multistakingtypes.GenesisState{
MultiStakingLocks: locks,
MultiStakingUnlocks: []multistakingtypes.MultiStakingUnlock{},
MultiStakingCoinInfo: coinInfos,
ValidatorMultiStakingCoins: validatorCoins,
StakingGenesisState: stakingGenesis,
}
genesisState[multistakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(&multistakingGenesis)

// add bonded amount to bonded pool module account
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)},
Coins: bondCoins,
})
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(multistakingtypes.ModuleName).String(),
Coins: lockCoins,
})

totalSupply := sdk.NewCoins()
for _, b := range balances {
// add genesis acc tokens to total supply
totalSupply = totalSupply.Add(b.Coins...)
}

// update total supply
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{})
Expand Down
9 changes: 9 additions & 0 deletions testutil/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testutil

const (
// MultistakingDenomA defines the multi staking denomination A
MultistakingDenomA = "ario"

// MultistakingDenomB defines the multi staking denomination B
MultistakingDenomB = "arst"
)
Loading