cosmos-sdk/simapp/state.go

145 lines
4.5 KiB
Go
Raw Normal View History

package simapp
// DONTCOVER
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"time"
"github.com/tendermint/tendermint/crypto/secp256k1"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/x/genaccounts"
Merge #4209: NFT Module * in sync with @okwme/cosmos-nft * remove tmp tx * structuring and minor changes * supply and client files * adding cli client * complete cli/tx and rest.go * cleanup and restructuring * restructure rest folder * minor updates on clients * update querier * encoding for clients and other changes * genesis, invariants, and keeper updates * update types * make golangcibot happy * renamed and removed bank keeper * remove handlers for editmetadata, mint, burn, buy * nft interface * minor cleanup * sort collections and nfts * balance and find * nft query and tx * touch ups * uint in place of int Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * little fixes: - fix error to err to avoid collision - error handling Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * module generalization changes * fixes * query with data * minor updates and TODOs * fix CLI tx * golang bot fixes * handlers and txs done * update module generalization * Added very basic tests which for some reason do not work * fix test Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * fixed test, now we should fix implementation, seems to fail Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * fix test, create new struct instead of changing the old one Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * fix handler with new logic Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * let's make it compile Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * single failing test example, need to be fixed and extended Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * single failing test example, need to be fixed and extended Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * reverting work, still problems unmarshalling inside iterator from test Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * Setter in nft.go should return NFT instead of BaseNFT Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * remove TODOS Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * comment out broken tests, we want at least a green mark here Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * little fixes Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * hopefully no conflict * minor changes for tests * change nft id to string, refactors * messy pause * Changes Balances to OWners add all necessary functions, updated Keeper with UpdateNFT as as well as MintNFT and made sure they all update Owners * pause dev to merge sdk master * go.mod changes * getting closer still need module.go * builds!!! * fix lint begin handler tests * stableish * re-order nft attributes, add back mint and burn msgs and handlers * add errors to minting the same NFT and burning an NFT that doesnt exist * first querier test * add simulations for nft msgs * handler tests check tags now (fixed a bug!) * update simulation * generic handler * need to check if it compiles on another machine * fix weird interface error * add back cli * wtfff * codec error fixed, logs removed. still returning empty arrays of IDs * Take empty input as yes answer Closes: #4564 * Add pending log entry * merged in master * marshall errors * build commands * working!!! * linting errors * remove unused func * pause * fix burn error * fix burn error * tests for querier * typo * tests for NFT types * module spec standard * tests for Collection and Collections types * merge w Fede * tests for Owner Type * added genesis tests and beefed up keeper, querier, handler & types tests * linting errors deadcode * DONT COVER test_common.go * add msg type tests * Update x/nft/internal/keeper/key.go Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update x/nft/genesis.go Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update x/nft/client/cli/query.go Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Apply suggestions from code review * typo * cleanup events * split events * more cleanup * remove restrictions from default handlers * not sure where these go mod changes came from * sim generated changes * make format * add mint and burn sims * move NFT interface to nft/exported * make format * NFT spec * Updates * more updates * update specs readme * fix sims * rest additions * rest additions * fix invariant * minimal nft without name, description or image * sim * fix sim * fix sim * fix Update methods * nothing * simplify update and remove Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * remove test on memory location Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * TEST to get logs, need to be removed Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * fix simulator editMetadata Msg type * owner not found start with empty collection Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * artifacts on errors in case of failure, else, no artifacts Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * add more invariant checks to handler_tests * never forget to overwrite * merge and update spec * colins feedback * code coverage test * code coverage test * code coverage test * spelling * clean up client * testing code coverage * testing code coverage * testing code coverage * testing code coverage * testing code coverage * Update docs/spec/nft/README.md Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * Apply suggestions from code review Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * minor changes * integration tests and fixes * minor golangCI fixes * Update simapp/app.go Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com>
2019-08-26 09:54:45 -07:00
nftsim "github.com/cosmos/cosmos-sdk/x/nft/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
)
// AppStateFn returns the initial application state using a genesis or the simulation parameters.
// It panics if the user provides files for both of them.
// If a file is not given for the genesis or the sim params, it creates a randomized one.
func AppStateFn(
r *rand.Rand, accs []simulation.Account, config simulation.Config,
) (appState json.RawMessage, simAccs []simulation.Account, chainID string, genesisTimestamp time.Time) {
cdc := MakeCodec()
if flagGenesisTimeValue == 0 {
genesisTimestamp = simulation.RandTimestamp(r)
} else {
genesisTimestamp = time.Unix(flagGenesisTimeValue, 0)
}
switch {
case config.ParamsFile != "" && config.GenesisFile != "":
panic("cannot provide both a genesis file and a params file")
case config.GenesisFile != "":
appState, simAccs, chainID = AppStateFromGenesisFileFn(r, config)
case config.ParamsFile != "":
appParams := make(simulation.AppParams)
bz, err := ioutil.ReadFile(config.ParamsFile)
if err != nil {
panic(err)
}
cdc.MustUnmarshalJSON(bz, &appParams)
appState, simAccs, chainID = AppStateRandomizedFn(r, accs, genesisTimestamp, appParams)
default:
appParams := make(simulation.AppParams)
appState, simAccs, chainID = AppStateRandomizedFn(r, accs, genesisTimestamp, appParams)
}
return appState, simAccs, chainID, genesisTimestamp
}
// AppStateRandomizedFn creates calls each module's GenesisState generator function
// and creates
func AppStateRandomizedFn(
r *rand.Rand, accs []simulation.Account, genesisTimestamp time.Time, appParams simulation.AppParams,
) (json.RawMessage, []simulation.Account, string) {
cdc := MakeCodec()
genesisState := NewDefaultGenesisState()
var (
amount int64
numInitiallyBonded int64
)
appParams.GetOrGenerate(cdc, StakePerAccount, &amount, r,
func(r *rand.Rand) { amount = int64(r.Intn(1e12)) })
appParams.GetOrGenerate(cdc, InitiallyBondedValidators, &amount, r,
func(r *rand.Rand) { numInitiallyBonded = int64(r.Intn(250)) })
numAccs := int64(len(accs))
if numInitiallyBonded > numAccs {
numInitiallyBonded = numAccs
}
fmt.Printf(
`Selected randomly generated parameters for simulated genesis:
{
stake_per_account: "%v",
initially_bonded_validators: "%v"
}
`, amount, numInitiallyBonded,
)
GenGenesisAccounts(cdc, r, accs, genesisTimestamp, amount, numInitiallyBonded, genesisState)
GenAuthGenesisState(cdc, r, appParams, genesisState)
GenBankGenesisState(cdc, r, appParams, genesisState)
GenSupplyGenesisState(cdc, amount, numInitiallyBonded, int64(len(accs)), genesisState)
GenGovGenesisState(cdc, r, appParams, genesisState)
GenMintGenesisState(cdc, r, appParams, genesisState)
Merge #4209: NFT Module * in sync with @okwme/cosmos-nft * remove tmp tx * structuring and minor changes * supply and client files * adding cli client * complete cli/tx and rest.go * cleanup and restructuring * restructure rest folder * minor updates on clients * update querier * encoding for clients and other changes * genesis, invariants, and keeper updates * update types * make golangcibot happy * renamed and removed bank keeper * remove handlers for editmetadata, mint, burn, buy * nft interface * minor cleanup * sort collections and nfts * balance and find * nft query and tx * touch ups * uint in place of int Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * little fixes: - fix error to err to avoid collision - error handling Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * module generalization changes * fixes * query with data * minor updates and TODOs * fix CLI tx * golang bot fixes * handlers and txs done * update module generalization * Added very basic tests which for some reason do not work * fix test Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * fixed test, now we should fix implementation, seems to fail Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * fix test, create new struct instead of changing the old one Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * fix handler with new logic Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * let's make it compile Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * single failing test example, need to be fixed and extended Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * single failing test example, need to be fixed and extended Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * reverting work, still problems unmarshalling inside iterator from test Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * Setter in nft.go should return NFT instead of BaseNFT Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * remove TODOS Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * comment out broken tests, we want at least a green mark here Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * little fixes Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * hopefully no conflict * minor changes for tests * change nft id to string, refactors * messy pause * Changes Balances to OWners add all necessary functions, updated Keeper with UpdateNFT as as well as MintNFT and made sure they all update Owners * pause dev to merge sdk master * go.mod changes * getting closer still need module.go * builds!!! * fix lint begin handler tests * stableish * re-order nft attributes, add back mint and burn msgs and handlers * add errors to minting the same NFT and burning an NFT that doesnt exist * first querier test * add simulations for nft msgs * handler tests check tags now (fixed a bug!) * update simulation * generic handler * need to check if it compiles on another machine * fix weird interface error * add back cli * wtfff * codec error fixed, logs removed. still returning empty arrays of IDs * Take empty input as yes answer Closes: #4564 * Add pending log entry * merged in master * marshall errors * build commands * working!!! * linting errors * remove unused func * pause * fix burn error * fix burn error * tests for querier * typo * tests for NFT types * module spec standard * tests for Collection and Collections types * merge w Fede * tests for Owner Type * added genesis tests and beefed up keeper, querier, handler & types tests * linting errors deadcode * DONT COVER test_common.go * add msg type tests * Update x/nft/internal/keeper/key.go Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update x/nft/genesis.go Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update x/nft/client/cli/query.go Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Apply suggestions from code review * typo * cleanup events * split events * more cleanup * remove restrictions from default handlers * not sure where these go mod changes came from * sim generated changes * make format * add mint and burn sims * move NFT interface to nft/exported * make format * NFT spec * Updates * more updates * update specs readme * fix sims * rest additions * rest additions * fix invariant * minimal nft without name, description or image * sim * fix sim * fix sim * fix Update methods * nothing * simplify update and remove Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * remove test on memory location Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * TEST to get logs, need to be removed Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * fix simulator editMetadata Msg type * owner not found start with empty collection Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * artifacts on errors in case of failure, else, no artifacts Signed-off-by: Karoly Albert Szabo <szabo.karoly.a@gmail.com> * add more invariant checks to handler_tests * never forget to overwrite * merge and update spec * colins feedback * code coverage test * code coverage test * code coverage test * spelling * clean up client * testing code coverage * testing code coverage * testing code coverage * testing code coverage * testing code coverage * Update docs/spec/nft/README.md Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * Apply suggestions from code review Co-Authored-By: frog power 4000 <rigel.rozanski@gmail.com> * minor changes * integration tests and fixes * minor golangCI fixes * Update simapp/app.go Co-Authored-By: Bot from GolangCI <42910462+golangcibot@users.noreply.github.com>
2019-08-26 09:54:45 -07:00
nftsim.GenNFTGenesisState(cdc, r, accs, appParams, genesisState)
GenDistrGenesisState(cdc, r, appParams, genesisState)
stakingGen := GenStakingGenesisState(cdc, r, accs, amount, numAccs, numInitiallyBonded, appParams, genesisState)
GenSlashingGenesisState(cdc, r, stakingGen, appParams, genesisState)
appState, err := MakeCodec().MarshalJSON(genesisState)
if err != nil {
panic(err)
}
return appState, accs, "simulation"
}
// AppStateFromGenesisFileFn util function to generate the genesis AppState
// from a genesis.json file
func AppStateFromGenesisFileFn(r *rand.Rand, config simulation.Config) (json.RawMessage, []simulation.Account, string) {
var genesis tmtypes.GenesisDoc
cdc := MakeCodec()
bytes, err := ioutil.ReadFile(config.GenesisFile)
if err != nil {
panic(err)
}
cdc.MustUnmarshalJSON(bytes, &genesis)
var appState GenesisState
cdc.MustUnmarshalJSON(genesis.AppState, &appState)
accounts := genaccounts.GetGenesisStateFromAppState(cdc, appState)
var newAccs []simulation.Account
for _, acc := range accounts {
// Pick a random private key, since we don't know the actual key
// This should be fine as it's only used for mock Tendermint validators
// and these keys are never actually used to sign by mock Tendermint.
privkeySeed := make([]byte, 15)
r.Read(privkeySeed)
privKey := secp256k1.GenPrivKeySecp256k1(privkeySeed)
2019-08-19 09:06:27 -07:00
newAccs = append(newAccs, simulation.Account{PrivKey: privKey, PubKey: privKey.PubKey(), Address: acc.Address})
}
return genesis.AppState, newAccs, genesis.ChainID
}