-
Notifications
You must be signed in to change notification settings - Fork 9
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
Changes from 5 commits
924170b
e1ef949
67922b4
405c0eb
c11890f
792f43e
3031ac1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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) | ||
|
@@ -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} | ||
validatorCoins := make([]multistakingtypes.ValidatorMultiStakingCoin, 0, len(valSet.Validators)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{}) | ||
|
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" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
msCoinInfos