cosmos-sdk/x/group/keeper/genesis.go
Marie Gauthier f52d87003c
feat: Add simulations to group module (#10723)
## Description

Closes: #9901



---

### 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...

- [x] 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
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] 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)
- [x] 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`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] 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)
2022-01-21 10:52:26 +00:00

90 lines
2.6 KiB
Go

package keeper
import (
"encoding/json"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/group"
)
func (k Keeper) InitGenesis(ctx types.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState group.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
if err := k.groupTable.Import(ctx.KVStore(k.key), genesisState.Groups, genesisState.GroupSeq); err != nil {
panic(errors.Wrap(err, "groups"))
}
if err := k.groupMemberTable.Import(ctx.KVStore(k.key), genesisState.GroupMembers, 0); err != nil {
panic(errors.Wrap(err, "group members"))
}
if err := k.groupPolicyTable.Import(ctx.KVStore(k.key), genesisState.GroupPolicies, 0); err != nil {
panic(errors.Wrap(err, "group policies"))
}
if err := k.groupPolicySeq.InitVal(ctx.KVStore(k.key), genesisState.GroupPolicySeq); err != nil {
panic(errors.Wrap(err, "group policy account seq"))
}
if err := k.proposalTable.Import(ctx.KVStore(k.key), genesisState.Proposals, genesisState.ProposalSeq); err != nil {
panic(errors.Wrap(err, "proposals"))
}
if err := k.voteTable.Import(ctx.KVStore(k.key), genesisState.Votes, 0); err != nil {
panic(errors.Wrap(err, "votes"))
}
return []abci.ValidatorUpdate{}
}
func (k Keeper) ExportGenesis(ctx types.Context, cdc codec.JSONCodec) *group.GenesisState {
genesisState := group.NewGenesisState()
var groups []*group.GroupInfo
groupSeq, err := k.groupTable.Export(ctx.KVStore(k.key), &groups)
if err != nil {
panic(errors.Wrap(err, "groups"))
}
genesisState.Groups = groups
genesisState.GroupSeq = groupSeq
var groupMembers []*group.GroupMember
_, err = k.groupMemberTable.Export(ctx.KVStore(k.key), &groupMembers)
if err != nil {
panic(errors.Wrap(err, "group members"))
}
genesisState.GroupMembers = groupMembers
var groupPolicies []*group.GroupPolicyInfo
_, err = k.groupPolicyTable.Export(ctx.KVStore(k.key), &groupPolicies)
if err != nil {
panic(errors.Wrap(err, "group policies"))
}
genesisState.GroupPolicies = groupPolicies
genesisState.GroupPolicySeq = k.groupPolicySeq.CurVal(ctx.KVStore(k.key))
var proposals []*group.Proposal
proposalSeq, err := k.proposalTable.Export(ctx.KVStore(k.key), &proposals)
if err != nil {
panic(errors.Wrap(err, "proposals"))
}
genesisState.Proposals = proposals
genesisState.ProposalSeq = proposalSeq
var votes []*group.Vote
_, err = k.voteTable.Export(ctx.KVStore(k.key), &votes)
if err != nil {
panic(errors.Wrap(err, "votes"))
}
genesisState.Votes = votes
return genesisState
}