Skip to content

Commit

Permalink
feat: use core genesis api (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Jul 27, 2023
1 parent cb0e322 commit 2c0432c
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 807 deletions.
548 changes: 27 additions & 521 deletions api/v1/types.pulsar.go

Large diffs are not rendered by default.

15 changes: 0 additions & 15 deletions genesis.go

This file was deleted.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ go 1.20

require (
cosmossdk.io/api v0.7.0
cosmossdk.io/collections v0.3.0
cosmossdk.io/collections v0.3.1-0.20230724154654-21020740248b
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-rc3
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/cosmos-sdk v0.50.0-beta.0
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-rc3 // 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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
cosmossdk.io/api v0.7.0 h1:QsEMIWuv9xWDbF2HZnW4Lpu1/SejCztPu0LQx7t6MN4=
cosmossdk.io/api v0.7.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/collections v0.3.1-0.20230724154654-21020740248b h1:onf6vvjKnO8OlU2Ii3Oq3G2SUajnqFpD/bn2YRsULvU=
cosmossdk.io/collections v0.3.1-0.20230724154654-21020740248b/go.mod h1:ttWHG8H0cgzNOoStKatGkzf1qxbJNUTdLIGeMymSskw=
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 @@ -13,7 +13,7 @@ require (
)

require (
cosmossdk.io/collections v0.3.0 // indirect
cosmossdk.io/collections v0.3.1-0.20230724154654-21020740248b // indirect
cosmossdk.io/errors v1.0.0 // indirect
cosmossdk.io/math v1.0.1 // indirect
cosmossdk.io/store v1.0.0-alpha.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
cosmossdk.io/api v0.7.0 h1:QsEMIWuv9xWDbF2HZnW4Lpu1/SejCztPu0LQx7t6MN4=
cosmossdk.io/api v0.7.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/collections v0.3.1-0.20230724154654-21020740248b h1:onf6vvjKnO8OlU2Ii3Oq3G2SUajnqFpD/bn2YRsULvU=
cosmossdk.io/collections v0.3.1-0.20230724154654-21020740248b/go.mod h1:ttWHG8H0cgzNOoStKatGkzf1qxbJNUTdLIGeMymSskw=
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
26 changes: 0 additions & 26 deletions keeper/genesis.go

This file was deleted.

48 changes: 48 additions & 0 deletions keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package keeper_test

import (
"bytes"
"encoding/json"
"testing"

"cosmossdk.io/core/genesis"
"github.com/cosmosregistry/example"
"github.com/stretchr/testify/require"
)

func TestDefaultGenesis(t *testing.T) {
fixture := initFixture(t)

target := &genesis.RawJSONTarget{}
err := fixture.k.Schema.DefaultGenesis(target.Target())
require.NoError(t, err)

result, err := target.JSON()
require.NoError(t, err)
buf := &bytes.Buffer{}
err = json.Compact(buf, result)
require.NoError(t, err)

require.Equal(t, `{"counter":[],"params":[]}`, buf.String())
}

func TestExportGenesis(t *testing.T) {
fixture := initFixture(t)

_, err := fixture.msgServer.IncrementCounter(fixture.ctx, &example.MsgIncrementCounter{
Sender: fixture.addrs[0].String(),
})
require.NoError(t, err)

target := &genesis.RawJSONTarget{}
err = fixture.k.Schema.ExportGenesis(fixture.ctx, target.Target())
require.NoError(t, err)

result, err := target.JSON()
require.NoError(t, err)
buf := &bytes.Buffer{}
err = json.Compact(buf, result)
require.NoError(t, err)

require.Equal(t, `{"counter":[{"key":"cosmos15ky9du8a2wlstz6fpx3p4mqpjyrm5cgqjwl8sq","value":"1"}],"params":[]}`, buf.String())
}
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
}
9 changes: 8 additions & 1 deletion keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package keeper_test
import (
"testing"

"cosmossdk.io/core/genesis"
storetypes "cosmossdk.io/store/types"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/stretchr/testify/require"

"github.com/cosmosregistry/example"
"github.com/cosmosregistry/example/keeper"
Expand All @@ -32,7 +34,12 @@ func initFixture(t *testing.T) *testFixture {
addrs := simtestutil.CreateIncrementalAccounts(3)

k := keeper.NewKeeper(encCfg.Codec, addresscodec.NewBech32Codec("cosmos"), storeService, addrs[0].String())
k.InitGenesis(testCtx.Ctx, example.NewGenesisState())

source, err := genesis.SourceFromRawJSON([]byte(`{"counter":[],"params":[]}`))
require.NoError(t, err)

err = k.Schema.InitGenesis(testCtx.Ctx, source)
require.NoError(t, err)

return &testFixture{
ctx: testCtx.Ctx,
Expand Down
4 changes: 4 additions & 0 deletions keeper/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (qs queryServer) Counter(ctx context.Context, req *example.QueryCounterRequ
func (qs queryServer) Params(ctx context.Context, req *example.QueryParamsRequest) (*example.QueryParamsResponse, error) {
params, err := qs.k.Params.Get(ctx)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return &example.QueryParamsResponse{Params: example.Params{}}, nil
}

return nil, status.Error(codes.Internal, err.Error())
}

Expand Down
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)
}
10 changes: 1 addition & 9 deletions proto/cosmosregistry/example/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ package cosmosregistry.example.v1;

option go_package = "github.com/cosmosregistry/example";

import "gogoproto/gogo.proto";
import "amino/amino.proto";

// Params defines the parameters of the module.
message Params { option (amino.name) = "cosmosregistry/example/Params"; }

// GenesisState is the state that must be provided at genesis.
message GenesisState {
// params defines all the parameters of the module.
Params params = 1
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}
message Params { option (amino.name) = "cosmosregistry/example/Params"; }
Loading

0 comments on commit 2c0432c

Please sign in to comment.