feat: decouple x/nft from simapp (#12233)
This commit is contained in:
@@ -132,16 +132,31 @@ func DefaultConfig() Config {
|
||||
|
||||
func DefaultConfigWithAppConfig(appConfig depinject.Config) (Config, error) {
|
||||
cfg := DefaultConfig()
|
||||
var appBuilder *runtime.AppBuilder
|
||||
var msgServiceRouter *baseapp.MsgServiceRouter
|
||||
|
||||
var (
|
||||
appBuilder *runtime.AppBuilder
|
||||
msgServiceRouter *baseapp.MsgServiceRouter
|
||||
txConfig client.TxConfig
|
||||
legacyAmino *codec.LegacyAmino
|
||||
codec codec.Codec
|
||||
interfaceRegistry codectypes.InterfaceRegistry
|
||||
)
|
||||
|
||||
if err := depinject.Inject(appConfig,
|
||||
&appBuilder,
|
||||
&msgServiceRouter,
|
||||
&txConfig,
|
||||
&codec,
|
||||
&legacyAmino,
|
||||
&interfaceRegistry,
|
||||
); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
cfg.Codec = codec
|
||||
cfg.TxConfig = txConfig
|
||||
cfg.LegacyAmino = legacyAmino
|
||||
cfg.InterfaceRegistry = interfaceRegistry
|
||||
cfg.GenesisState = appBuilder.DefaultGenesis()
|
||||
cfg.AppConstructor = func(val Validator) servertypes.Application {
|
||||
app := appBuilder.Build(
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package sims
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
||||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||
)
|
||||
|
||||
type GenerateAccountStrategy func(int) []sdk.AccAddress
|
||||
|
||||
// AddTestAddrsIncremental constructs and returns accNum amount of accounts with an initial balance of accAmt in random order
|
||||
func AddTestAddrsIncremental(bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
|
||||
return addTestAddrs(bankKeeper, stakingKeeper, ctx, accNum, accAmt, CreateIncrementalAccounts)
|
||||
}
|
||||
|
||||
func addTestAddrs(bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, ctx sdk.Context, accNum int, accAmt math.Int, strategy GenerateAccountStrategy) []sdk.AccAddress {
|
||||
testAddrs := strategy(accNum)
|
||||
initCoins := sdk.NewCoins(sdk.NewCoin(stakingKeeper.BondDenom(ctx), accAmt))
|
||||
|
||||
for _, addr := range testAddrs {
|
||||
initAccountWithCoins(bankKeeper, ctx, addr, initCoins)
|
||||
}
|
||||
|
||||
return testAddrs
|
||||
}
|
||||
|
||||
func initAccountWithCoins(bankKeeper bankkeeper.Keeper, ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) {
|
||||
if err := bankKeeper.MintCoins(ctx, minttypes.ModuleName, coins); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := bankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, coins); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// createIncrementalAccounts is a strategy used by addTestAddrs() in order to generated addresses in ascending order.
|
||||
func CreateIncrementalAccounts(accNum int) []sdk.AccAddress {
|
||||
var addresses []sdk.AccAddress
|
||||
var buffer bytes.Buffer
|
||||
|
||||
// start at 100 so we can make up to 999 test addresses with valid test addresses
|
||||
for i := 100; i < (accNum + 100); i++ {
|
||||
numString := strconv.Itoa(i)
|
||||
buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") // base address string
|
||||
|
||||
buffer.WriteString(numString) // adding on final two digits to make addresses unique
|
||||
res, _ := sdk.AccAddressFromHexUnsafe(buffer.String())
|
||||
bech := res.String()
|
||||
addr, _ := TestAddr(buffer.String(), bech)
|
||||
|
||||
addresses = append(addresses, addr)
|
||||
buffer.Reset()
|
||||
}
|
||||
|
||||
return addresses
|
||||
}
|
||||
|
||||
// CreateRandomAccounts is a strategy used by addTestAddrs() in order to generated addresses in random order.
|
||||
func CreateRandomAccounts(accNum int) []sdk.AccAddress {
|
||||
testAddrs := make([]sdk.AccAddress, accNum)
|
||||
for i := 0; i < accNum; i++ {
|
||||
pk := ed25519.GenPrivKey().PubKey()
|
||||
testAddrs[i] = sdk.AccAddress(pk.Address())
|
||||
}
|
||||
|
||||
return testAddrs
|
||||
}
|
||||
|
||||
func TestAddr(addr string, bech string) (sdk.AccAddress, error) {
|
||||
res, err := sdk.AccAddressFromHexUnsafe(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bechexpected := res.String()
|
||||
if bech != bechexpected {
|
||||
return nil, fmt.Errorf("bech encoding doesn't match reference")
|
||||
}
|
||||
|
||||
bechres, err := sdk.AccAddressFromBech32(bech)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !bytes.Equal(bechres, res) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package sims
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
tmjson "github.com/tendermint/tendermint/libs/json"
|
||||
"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/baseapp"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
||||
"github.com/cosmos/cosmos-sdk/depinject"
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
"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"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
// DefaultConsensusParams defines the default Tendermint consensus params used in
|
||||
// SimApp testing.
|
||||
var DefaultConsensusParams = &tmproto.ConsensusParams{
|
||||
Block: &tmproto.BlockParams{
|
||||
MaxBytes: 200000,
|
||||
MaxGas: 2000000,
|
||||
},
|
||||
Evidence: &tmproto.EvidenceParams{
|
||||
MaxAgeNumBlocks: 302400,
|
||||
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
|
||||
MaxBytes: 10000,
|
||||
},
|
||||
Validator: &tmproto.ValidatorParams{
|
||||
PubKeyTypes: []string{
|
||||
tmtypes.ABCIPubKeyTypeEd25519,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Setup initializes a new runtime.App. A Nop logger is set in runtime.App.
|
||||
// appConfig usually load from a `app.yaml` with `appconfig.LoadYAML`, defines the application configuration.
|
||||
// extraOutputs defines the extra outputs to be assigned by the dependency injector (depinject).
|
||||
func Setup(appConfig depinject.Config, extraOutputs ...interface{}) (*runtime.App, error) {
|
||||
//
|
||||
// create app
|
||||
//
|
||||
var appBuilder *runtime.AppBuilder
|
||||
var msgServiceRouter *baseapp.MsgServiceRouter
|
||||
var codec codec.Codec
|
||||
|
||||
if err := depinject.Inject(
|
||||
appConfig,
|
||||
append(extraOutputs, &appBuilder, &msgServiceRouter, &codec)...,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("failed to inject dependencies: %w", err)
|
||||
}
|
||||
|
||||
app := appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil, msgServiceRouter)
|
||||
if err := app.Load(true); err != nil {
|
||||
return nil, fmt.Errorf("failed to load app: %w", err)
|
||||
}
|
||||
|
||||
//
|
||||
// create genesis and validator
|
||||
//
|
||||
privVal := mock.NewPV()
|
||||
pubKey, err := privVal.GetPubKey(context.TODO())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get pub key: %w", err)
|
||||
}
|
||||
|
||||
// create validator set with single validator
|
||||
validator := tmtypes.NewValidator(pubKey, 1)
|
||||
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})
|
||||
|
||||
// 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))),
|
||||
}
|
||||
|
||||
genesisState, err := GenesisStateWithValSet(codec, appBuilder.DefaultGenesis(), valSet, []authtypes.GenesisAccount{acc}, balance)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create genesis state: %w", err)
|
||||
}
|
||||
|
||||
// init chain must be called to stop deliverState from being nil
|
||||
stateBytes, err := tmjson.MarshalIndent(genesisState, "", " ")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal default genesis state: %w", err)
|
||||
}
|
||||
|
||||
// init chain will set the validator set and initialize the genesis accounts
|
||||
app.InitChain(
|
||||
abci.RequestInitChain{
|
||||
Validators: []abci.ValidatorUpdate{},
|
||||
ConsensusParams: DefaultConsensusParams,
|
||||
AppStateBytes: stateBytes,
|
||||
},
|
||||
)
|
||||
|
||||
return app, nil
|
||||
}
|
||||
|
||||
// GenesisStateWithValSet returns a new genesis state with the validator set
|
||||
func GenesisStateWithValSet(codec codec.Codec, genesisState map[string]json.RawMessage,
|
||||
valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount,
|
||||
balances ...banktypes.Balance,
|
||||
) (map[string]json.RawMessage, error) {
|
||||
// set genesis accounts
|
||||
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
|
||||
genesisState[authtypes.ModuleName] = codec.MustMarshalJSON(authGenesis)
|
||||
|
||||
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, err := cryptocodec.FromTmPubKeyInterface(val.PubKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to convert pubkey: %w", err)
|
||||
}
|
||||
|
||||
pkAny, err := codectypes.NewAnyWithValue(pk)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create new any: %w", err)
|
||||
}
|
||||
|
||||
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] = codec.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))
|
||||
}
|
||||
|
||||
// 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
|
||||
bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}, []banktypes.SendEnabled{})
|
||||
genesisState[banktypes.ModuleName] = codec.MustMarshalJSON(bankGenesis)
|
||||
|
||||
return genesisState, nil
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package sims
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
// DefaultConsensusParams defines the default Tendermint consensus params used in
|
||||
// SimApp testing.
|
||||
var DefaultConsensusParams = &tmproto.ConsensusParams{
|
||||
Block: &tmproto.BlockParams{
|
||||
MaxBytes: 200000,
|
||||
MaxGas: 2000000,
|
||||
},
|
||||
Evidence: &tmproto.EvidenceParams{
|
||||
MaxAgeNumBlocks: 302400,
|
||||
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
|
||||
MaxBytes: 10000,
|
||||
},
|
||||
Validator: &tmproto.ValidatorParams{
|
||||
PubKeyTypes: []string{
|
||||
tmtypes.ABCIPubKeyTypeEd25519,
|
||||
},
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user