cosmos-sdk/x/nft/simulation/genesis.go
Zhiqiang Zhang ba843a351c
feat: add simulation for nft module (#10522)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Add nft module simulation and unit test,refer #9826

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2021-11-11 15:44:41 +00:00

68 lines
1.8 KiB
Go

package simulation
import (
"math/rand"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/nft"
)
// genClasses returns a slice of nft class.
func genClasses(r *rand.Rand, accounts []simtypes.Account) []*nft.Class {
classes := make([]*nft.Class, len(accounts)-1)
for i := 0; i < len(accounts)-1; i++ {
classes[i] = &nft.Class{
Id: simtypes.RandStringOfLength(r, 10),
Name: simtypes.RandStringOfLength(r, 10),
Symbol: simtypes.RandStringOfLength(r, 10),
Description: simtypes.RandStringOfLength(r, 10),
Uri: simtypes.RandStringOfLength(r, 10),
}
}
return classes
}
// genNFT returns a slice of nft.
func genNFT(r *rand.Rand, classID string, accounts []simtypes.Account) []*nft.Entry {
entries := make([]*nft.Entry, len(accounts)-1)
for i := 0; i < len(accounts)-1; i++ {
owner := accounts[i]
entries[i] = &nft.Entry{
Owner: owner.Address.String(),
Nfts: []*nft.NFT{
{
ClassId: classID,
Id: simtypes.RandStringOfLength(r, 10),
Uri: simtypes.RandStringOfLength(r, 10),
},
},
}
}
return entries
}
// RandomizedGenState generates a random GenesisState for nft.
func RandomizedGenState(simState *module.SimulationState) {
var classes []*nft.Class
simState.AppParams.GetOrGenerate(
simState.Cdc, "nft", &classes, simState.Rand,
func(r *rand.Rand) { classes = genClasses(r, simState.Accounts) },
)
var entries []*nft.Entry
simState.AppParams.GetOrGenerate(
simState.Cdc, "nft", &entries, simState.Rand,
func(r *rand.Rand) {
class := classes[r.Int63n(int64(len(classes)))]
entries = genNFT(r, class.Id, simState.Accounts)
},
)
nftGenesis := &nft.GenesisState{
Classes: classes,
Entries: entries,
}
simState.GenState[nft.ModuleName] = simState.Cdc.MustMarshalJSON(nftGenesis)
}