Skip to content

Commit

Permalink
with valset in bench
Browse files Browse the repository at this point in the history
  • Loading branch information
vuong177 authored and faddat committed Dec 19, 2022
1 parent 01d7b54 commit 35b2a8f
Showing 1 changed file with 60 additions and 8 deletions.
68 changes: 60 additions & 8 deletions benchmarks/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@ import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/testutil/mock"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

"github.com/CosmWasm/wasmd/app"
"github.com/CosmWasm/wasmd/x/wasm"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
)

func setup(db dbm.DB, withGenesis bool, invCheckPeriod uint, opts ...wasm.Option) (*app.WasmApp, app.GenesisState) {
Expand All @@ -36,18 +45,61 @@ func setup(db dbm.DB, withGenesis bool, invCheckPeriod uint, opts ...wasm.Option

// SetupWithGenesisAccounts initializes a new WasmApp with the provided genesis
// accounts and possible balances.
func SetupWithGenesisAccounts(b testing.TB, db dbm.DB, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *app.WasmApp {
func SetupWithGenesisAccountsAndValSet(b testing.TB, db dbm.DB, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *app.WasmApp {
wasmApp, genesisState := setup(db, true, 0)
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
appCodec := wasmApp.AppCodec()

privVal := mock.NewPV()
pubKey, err := privVal.GetPubKey()
require.NoError(b, err)

genesisState[authtypes.ModuleName] = appCodec.MustMarshalJSON(authGenesis)

validator := tmtypes.NewValidator(pubKey, 1)
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})

validators := make([]stakingtypes.Validator, 0, len(valSet.Validators))
delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators))

bondAmt := sdk.DefaultPowerReduction

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,
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(),
}
validators = append(validators, validator)
delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec()))

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

totalSupply := sdk.NewCoins()

// 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)},
})
// update total supply
for _, b := range balances {
// add genesis acc tokens and delegated tokens to total supply
totalSupply = totalSupply.Add(b.Coins...)
}

bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, nil)
genesisState[banktypes.ModuleName] = appCodec.MustMarshalJSON(bankGenesis)

Expand Down Expand Up @@ -110,7 +162,7 @@ func InitializeWasmApp(b testing.TB, db dbm.DB, numAccounts int) AppInfo {
Coins: sdk.NewCoins(sdk.NewInt64Coin(denom, 100000000000)),
}
}
wasmApp := SetupWithGenesisAccounts(b, db, genAccs, bals...)
wasmApp := SetupWithGenesisAccountsAndValSet(b, db, genAccs, bals...)

// add wasm contract
height := int64(2)
Expand Down

0 comments on commit 35b2a8f

Please sign in to comment.