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

feat: use core genesis api #18

Merged
merged 10 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ module github.com/cosmosregistry/example
go 1.20

require (
cosmossdk.io/api v0.6.0
cosmossdk.io/api v0.6.1
cosmossdk.io/collections v0.3.0
cosmossdk.io/core v0.9.0
cosmossdk.io/depinject v1.0.0-alpha.3
cosmossdk.io/store v1.0.0-alpha.1
github.com/cometbft/cometbft v0.38.0-rc2
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/cosmos-sdk v0.50.0-alpha.1.0.20230713184559-d4284d7f14e4
github.com/cosmos/gogoproto v1.4.10
Expand Down Expand Up @@ -40,6 +39,7 @@ require (
github.com/cockroachdb/pebble v0.0.0-20230711190327-88bbab59ff4f // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230613231145-182959a1fad6 // indirect
github.com/cometbft/cometbft v0.38.0-rc2 // indirect
github.com/cometbft/cometbft-db v0.7.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v1.0.0 // indirect
Expand Down Expand Up @@ -152,3 +152,5 @@ require (
pgregory.net/rapid v1.0.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace cosmossdk.io/collections => cosmossdk.io/collections v0.3.1-0.20230717091803-0ce499068900
Copy link
Member Author

Choose a reason for hiding this comment

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

8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
cosmossdk.io/api v0.6.0 h1:AEpx/sRZaQEusARZqAxbg20zRXnS6e1F+uoal39trOM=
cosmossdk.io/api v0.6.0/go.mod h1:kJFAEMLN57y0viszHDPLMmieF0471o5QAwwApa+270M=
cosmossdk.io/collections v0.3.0 h1:v0eEqLBxebAV+t+Ahwf9tSJOu95HVLINwROXx2TTZ08=
cosmossdk.io/collections v0.3.0/go.mod h1:CHE1+niUElL9ikCpevRZcp0yqQ4TU0TrEEGirN0mvIg=
cosmossdk.io/api v0.6.1 h1:F+tr/ntynNk2WvUcUtc556DsUnqNOvtOwPBQWdhZQlg=
cosmossdk.io/api v0.6.1/go.mod h1:kJFAEMLN57y0viszHDPLMmieF0471o5QAwwApa+270M=
cosmossdk.io/collections v0.3.1-0.20230717091803-0ce499068900 h1:kWIP/pX/oRyCNs0+uw5SIkxaoU9xhRaOi2yYFwfgAeo=
cosmossdk.io/collections v0.3.1-0.20230717091803-0ce499068900/go.mod h1:vsX2d4wS5uTNvr9m7SdYe2ik1EqRb4fjKeNasMtaw+M=
cosmossdk.io/core v0.9.0 h1:30ScAOHDIUOCg1DKAwqkho9wuQJnu7GUrMcg0XLioic=
cosmossdk.io/core v0.9.0/go.mod h1:NFgl5r41Q36+RixTvyrfsS6qQ65agCbZ1FTpnN7/G1Y=
cosmossdk.io/depinject v1.0.0-alpha.3 h1:6evFIgj//Y3w09bqOUOzEpFj5tsxBqdc5CfkO7z+zfw=
Expand Down
2 changes: 1 addition & 1 deletion integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/cosmosregistry/example/integration
go 1.20

require (
cosmossdk.io/api v0.6.0
cosmossdk.io/api v0.6.1
cosmossdk.io/core v0.9.0
cosmossdk.io/depinject v1.0.0-alpha.3
cosmossdk.io/log v1.1.1-0.20230704160919-88f2c830b0ca
Expand Down
4 changes: 2 additions & 2 deletions integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
cosmossdk.io/api v0.6.0 h1:AEpx/sRZaQEusARZqAxbg20zRXnS6e1F+uoal39trOM=
cosmossdk.io/api v0.6.0/go.mod h1:kJFAEMLN57y0viszHDPLMmieF0471o5QAwwApa+270M=
cosmossdk.io/api v0.6.1 h1:F+tr/ntynNk2WvUcUtc556DsUnqNOvtOwPBQWdhZQlg=
cosmossdk.io/api v0.6.1/go.mod h1:kJFAEMLN57y0viszHDPLMmieF0471o5QAwwApa+270M=
cosmossdk.io/collections v0.3.0 h1:v0eEqLBxebAV+t+Ahwf9tSJOu95HVLINwROXx2TTZ08=
cosmossdk.io/collections v0.3.0/go.mod h1:CHE1+niUElL9ikCpevRZcp0yqQ4TU0TrEEGirN0mvIg=
cosmossdk.io/core v0.9.0 h1:30ScAOHDIUOCg1DKAwqkho9wuQJnu7GUrMcg0XLioic=
Expand Down
5 changes: 5 additions & 0 deletions keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"cosmossdk.io/collections"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
storetypes "cosmossdk.io/core/store"
"github.com/cosmos/cosmos-sdk/codec"

Expand Down Expand Up @@ -54,3 +55,7 @@ func NewKeeper(cdc codec.BinaryCodec, addressCodec address.Codec, storeService s
func (k Keeper) GetAuthority() string {
return k.authority
}

func (k Keeper) GenesisHandler() appmodule.HasGenesis {
return k.Schema
}
2 changes: 1 addition & 1 deletion module/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
}

k := keeper.NewKeeper(in.Cdc, in.AddressCodec, in.StoreService, authority.String())
m := NewAppModule(in.Cdc, k)
m := NewAppModule(k)

return ModuleOutputs{Module: m, Keeper: k}
}
47 changes: 7 additions & 40 deletions module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package module

import (
"context"
"encoding/json"
"fmt"

abci "github.com/cometbft/cometbft/abci/types"
"cosmossdk.io/core/appmodule"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

"github.com/cosmosregistry/example"
Expand All @@ -22,15 +19,17 @@ import (
const ConsensusVersion = 1

type AppModule struct {
cdc codec.Codec
appmodule.HasGenesis

keeper keeper.Keeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule {
func NewAppModule(keeper keeper.Keeper) AppModule {
return AppModule{
cdc: cdc,
keeper: keeper,
// using the keeper's schema as the genesis schema, this means all state must use collections
HasGenesis: keeper.Schema,
keeper: keeper,
}
}

Expand Down Expand Up @@ -69,35 +68,3 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
// panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", example.ModuleName, err))
// }
}

// DefaultGenesis returns default genesis state as raw bytes for the module.
func (AppModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(example.NewGenesisState())
}

// ValidateGenesis performs genesis state validation for the circuit module.
func (AppModule) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error {
var data example.GenesisState
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", example.ModuleName, err)
}

return data.Validate()
}

// InitGenesis performs genesis initialization for the example module.
// It returns no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState example.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)

am.keeper.InitGenesis(ctx, &genesisState)
return nil
}

// ExportGenesis returns the exported genesis state as raw bytes for the circuit
// module.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
gs := am.keeper.ExportGenesis(ctx)
return cdc.MustMarshalJSON(gs)
}