Skip to content

Commit

Permalink
update setup test
Browse files Browse the repository at this point in the history
  • Loading branch information
catShaark committed Jan 13, 2024
1 parent c11890f commit 792f43e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 56 deletions.
112 changes: 56 additions & 56 deletions testing/simapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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"
Expand Down Expand Up @@ -70,41 +69,19 @@ 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 {
privVal0 := mock.NewPV()
privVal1 := mock.NewPV()

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

// create validator set with single 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(testutil.MultistakingDenomA, sdk.NewInt(100000000000000)),
sdk.NewCoin(testutil.MultistakingDenomB, sdk.NewInt(100000000000000)),
),
}

app := SetupWithGenesisValSet(valSet, []authtypes.GenesisAccount{acc}, balance)
valSet := GenValSet()

app := SetupWithGenesisValSet(valSet)
return app
}

// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts
// that also act as delegators. For simplicity, each validator is bonded with a delegation
// of one consensus engine unit in the default token of the simapp from first genesis
// account. A Nop logger is set in SimApp.
func SetupWithGenesisValSet(valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *SimApp {
func SetupWithGenesisValSet(valSet *tmtypes.ValidatorSet) *SimApp {
app, genesisState := setup(true, 5)
genesisState = genesisStateWithValSet(app, genesisState, valSet, genAccs, balances...)
genesisState = genesisStateWithValSet(app, genesisState, valSet)

stateBytes, _ := json.MarshalIndent(genesisState, "", " ")

Expand All @@ -129,42 +106,50 @@ func SetupWithGenesisValSet(valSet *tmtypes.ValidatorSet, genAccs []authtypes.Ge
return app
}

func genesisStateWithValSet(app *SimApp, genesisState GenesisState,
valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount,
balances ...banktypes.Balance,
) GenesisState {
func genesisStateWithValSet(app *SimApp, genesisState GenesisState, valSet *tmtypes.ValidatorSet) GenesisState {
genAcc := GenAcc()
genAccs := []authtypes.GenesisAccount{genAcc}
balances := []banktypes.Balance{}

// set genesis accounts
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(),
Denom: MultiStakingCoinA.Denom,
BondWeight: MultiStakingCoinA.BondWeight,
}
msCoinBInfo := multistakingtypes.MultiStakingCoinInfo{
Denom: testutil.MultistakingDenomB,
BondWeight: sdk.MustNewDecFromStr("0.5"),
Denom: MultiStakingCoinB.Denom,
BondWeight: MultiStakingCoinB.BondWeight,
}
coinInfos := []multistakingtypes.MultiStakingCoinInfo{msCoinAInfo, msCoinBInfo}
validatorCoins := make([]multistakingtypes.ValidatorMultiStakingCoin, 0, len(valSet.Validators))
msCoinInfos := []multistakingtypes.MultiStakingCoinInfo{msCoinAInfo, msCoinBInfo}
validatorMsCoins := make([]multistakingtypes.ValidatorMultiStakingCoin, 0, len(valSet.Validators))
locks := make([]multistakingtypes.MultiStakingLock, 0, len(valSet.Validators))
lockCoins := sdk.NewCoins()

// staking genesis state
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
valMsCoin := MultiStakingCoinA
if i%2 == 1 {
valDenom = testutil.MultistakingDenomB
valMsCoin = MultiStakingCoinB
}
validatorCoins = append(validatorCoins, multistakingtypes.ValidatorMultiStakingCoin{

validatorMsCoins = append(validatorMsCoins, multistakingtypes.ValidatorMultiStakingCoin{
ValAddr: sdk.ValAddress(val.Address).String(),
CoinDenom: valDenom,
CoinDenom: valMsCoin.Denom,
})
valTokens := coinInfos[i%2].BondWeight.MulInt(bondAmt).RoundInt()

lockId := multistakingtypes.MultiStakingLockID(genAcc.GetAddress().String(), sdk.ValAddress(val.Address).String())
lockRecord := multistakingtypes.NewMultiStakingLock(&lockId, valMsCoin)

locks = append(locks, lockRecord)
lockCoins = lockCoins.Add(valMsCoin.ToCoin())

pk, _ := cryptocodec.FromTmPubKeyInterface(val.PubKey)
pkAny, _ := codectypes.NewAnyWithValue(pk)
Expand All @@ -173,7 +158,7 @@ func genesisStateWithValSet(app *SimApp, genesisState GenesisState,
ConsensusPubkey: pkAny,
Jailed: false,
Status: stakingtypes.Bonded,
Tokens: valTokens,
Tokens: valMsCoin.BondValue(),
DelegatorShares: sdk.OneDec(),
Description: stakingtypes.Description{},
UnbondingHeight: int64(0),
Expand All @@ -182,16 +167,10 @@ func genesisStateWithValSet(app *SimApp, genesisState GenesisState,
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()))
delegations = append(delegations, stakingtypes.NewDelegation(genAcc.GetAddress(), val.Address.Bytes(), sdk.OneDec()))

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

// set validators and delegations
Expand All @@ -201,8 +180,8 @@ func genesisStateWithValSet(app *SimApp, genesisState GenesisState,
multistakingGenesis := multistakingtypes.GenesisState{
MultiStakingLocks: locks,
MultiStakingUnlocks: []multistakingtypes.MultiStakingUnlock{},
MultiStakingCoinInfo: coinInfos,
ValidatorMultiStakingCoins: validatorCoins,
MultiStakingCoinInfo: msCoinInfos,
ValidatorMultiStakingCoins: validatorMsCoins,
StakingGenesisState: stakingGenesis,
}
genesisState[multistakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(&multistakingGenesis)
Expand Down Expand Up @@ -508,3 +487,24 @@ func FundAccount(app *SimApp, ctx sdk.Context, addr sdk.AccAddress, amounts sdk.
}
return app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, amounts)
}

func GenValSet() *tmtypes.ValidatorSet {
privVal0 := mock.NewPV()
privVal1 := mock.NewPV()

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

// create validator set with single validator
val0 := tmtypes.NewValidator(pubKey0, 1)
val1 := tmtypes.NewValidator(pubKey1, 1)

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

return valSet
}

func GenAcc() authtypes.GenesisAccount {
senderPrivKey := secp256k1.GenPrivKey()
return authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
}
17 changes: 17 additions & 0 deletions testing/simapp/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simapp

import (
multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -9,6 +10,22 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
)

var (
MultiStakingCoinA = multistakingtypes.MultiStakingCoin{
Denom: "ario",
Amount: sdk.NewIntFromUint64(100000000),
BondWeight: sdk.MustNewDecFromStr("1.23"),
}
MultiStakingCoinB = multistakingtypes.MultiStakingCoin{
Denom: "arst",
Amount: sdk.NewIntFromUint64(100000000),
BondWeight: sdk.MustNewDecFromStr("0.12"),
}
MultiStakingCoins = []multistakingtypes.MultiStakingCoin{
MultiStakingCoinA, MultiStakingCoinB,
}
)

// App implements the common methods for a Cosmos SDK-based application
// specific blockchain.
type App interface {
Expand Down

0 comments on commit 792f43e

Please sign in to comment.