Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable data in geneis state to be exported #310

Merged
merged 6 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions proto/nftmint/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ message GenesisState {
(gogoproto.moretags) = "yaml:\"params\"",
(gogoproto.nullable) = false
];
repeated ClassAttributes class_attributes_list = 2 [ (gogoproto.moretags) = "yaml:\"class_attributes_list\"" ];
repeated OwningClassIdList owning_class_id_lists = 3 [ (gogoproto.moretags) = "yaml:\"owning_class_id_lists\"" ];
repeated ClassNameIdList class_name_id_lists = 4 [ (gogoproto.moretags) = "yaml:\"class_name_id_lists\"" ];
}

29 changes: 27 additions & 2 deletions x/nftmint/genesis.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
package nftmint

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/UnUniFi/chain/x/nftmint/keeper"
"github.com/UnUniFi/chain/x/nftmint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// InitGenesis initializes the store state from a genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, accountKeeper types.AccountKeeper, gs types.GenesisState) {
k.SetParamSet(ctx, gs.Params)
for _, classAttributes := range gs.ClassAttributesList {
if err := k.SetClassAttributes(ctx, *classAttributes); err != nil {
panic(err)
}
}

for _, classNameIdList := range gs.ClassNameIdLists {
if err := k.SetClassNameIdList(ctx, *classNameIdList); err != nil {
panic(err)
}
}

for _, owningClassIdList := range gs.OwningClassIdLists {
if err := k.SetOwningClassIdList(ctx, *owningClassIdList); err != nil {
panic(err)
}
}
}

// ExportGenesis export genesis state for nftmarket module
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
classAttributesList := k.GetClassAttributesList(ctx)
owningClassIdLists := k.GetOwningClassIdLists(ctx)
classNameIdLists := k.GetClassNameIdLists(ctx)

return types.GenesisState{
Params: k.GetParamSet(ctx),
Params: k.GetParamSet(ctx),
ClassAttributesList: classAttributesList,
OwningClassIdLists: owningClassIdLists,
ClassNameIdLists: classNameIdLists,
}
}
39 changes: 39 additions & 0 deletions x/nftmint/keeper/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,45 @@ func (k Keeper) GetClassNameIdList(ctx sdk.Context, className string) (types.Cla
return classNameIdList, true
}

func (k Keeper) GetClassAttributesList(ctx sdk.Context) (classAttributesList []*types.ClassAttributes) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixClassAttributes)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var classAttributes types.ClassAttributes
k.cdc.MustUnmarshal(iterator.Value(), &classAttributes)
classAttributesList = append(classAttributesList, &classAttributes)
}

return
}

func (k Keeper) GetOwningClassIdLists(ctx sdk.Context) (owningClassIdLists []*types.OwningClassIdList) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixOwningClassIdList)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var owningClassIdList types.OwningClassIdList
k.cdc.MustUnmarshal(iterator.Value(), &owningClassIdList)
owningClassIdLists = append(owningClassIdLists, &owningClassIdList)
}

return
}

func (k Keeper) GetClassNameIdLists(ctx sdk.Context) (classNameIdLists []*types.ClassNameIdList) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixClassNameIdList)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var classNameIdList types.ClassNameIdList
k.cdc.MustUnmarshal(iterator.Value(), &classNameIdList)
classNameIdLists = append(classNameIdLists, &classNameIdList)
}

return
}

func (k Keeper) AddClassIDToOwningClassIdList(ctx sdk.Context, owner sdk.AccAddress, classID string) types.OwningClassIdList {
owningClassIdList, exists := k.GetOwningClassIdList(ctx, owner)
if !exists {
Expand Down
6 changes: 6 additions & 0 deletions x/nftmint/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func (gs GenesisState) Validate() error {
return err
}

for _, classAttributes := range gs.ClassAttributesList {
if err := ValidateClassAttributes(*classAttributes, gs.Params); err != nil {
return err
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think more validations will need to be added here for ClassNameIdLists and OwningClassIdLists.
As well as usage of usage of class ids on OwningClassIdLists that's not on ClassNameIdLists

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i know.
was thinking about whether i should add that kind of validation for those.
But, eventually i didn't do that since x/nftmarket doesn't have those type of validation, too, right?
do you even think i should add here now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a mandatory one but would be good to add for completeness.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkXultra
what do you think?
should i do this for this PR or do i just put this as an issue and give someone else this task?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taiki1frsh
How long will it take?

Copy link
Collaborator Author

@taiki1frsh taiki1frsh Dec 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkXultra
without test, it doesn't take an hour.
with out, i guess around four or five hour.


return nil
}

Expand Down
225 changes: 210 additions & 15 deletions x/nftmint/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading