From d741d2ee62e8d755a8d96ef7cf49ca246421381d Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Fri, 26 Jul 2024 12:40:40 +0200 Subject: [PATCH 1/3] Fixes to make init genesis pass --- runtime/v2/builder.go | 6 ++++-- runtime/v2/manager.go | 2 +- server/v2/stf/core_event_service.go | 3 ++- server/v2/store/commands.go | 2 +- simapp/v2/app_di.go | 4 ++-- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/runtime/v2/builder.go b/runtime/v2/builder.go index a073761cbcf1..b7869fa3f0a7 100644 --- a/runtime/v2/builder.go +++ b/runtime/v2/builder.go @@ -128,8 +128,10 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) { home := v.GetString(FlagHome) storeOpts := rootstore.DefaultStoreOptions() - if err := v.Sub("store.options").Unmarshal(&storeOpts); err != nil { - return nil, fmt.Errorf("failed to store options: %w", err) + if s := v.Sub("store.options"); s != nil { + if err := s.Unmarshal(&storeOpts); err != nil { + return nil, fmt.Errorf("failed to store options: %w", err) + } } scRawDb, err := db.NewDB(db.DBType(v.GetString("store.app-db-backend")), "application", filepath.Join(home, "data"), nil) diff --git a/runtime/v2/manager.go b/runtime/v2/manager.go index c64423ee9e20..303ef2f1497e 100644 --- a/runtime/v2/manager.go +++ b/runtime/v2/manager.go @@ -166,7 +166,7 @@ func (m *MM[T]) InitGenesisJSON( case appmodulev2.HasGenesis: m.logger.Debug("running initialization for module", "module", moduleName) if err := module.InitGenesis(ctx, genesisData[moduleName]); err != nil { - return err + return fmt.Errorf("init module %s: %w", moduleName, err) } case appmodulev2.HasABCIGenesis: m.logger.Debug("running initialization for module", "module", moduleName) diff --git a/server/v2/stf/core_event_service.go b/server/v2/stf/core_event_service.go index 5bd94c6ea746..f971e756a087 100644 --- a/server/v2/stf/core_event_service.go +++ b/server/v2/stf/core_event_service.go @@ -5,6 +5,7 @@ import ( "encoding/json" "slices" + "github.com/cosmos/cosmos-sdk/codec" gogoproto "github.com/cosmos/gogoproto/proto" "golang.org/x/exp/maps" @@ -55,7 +56,7 @@ func (em *eventManager) EmitNonConsensus(event gogoproto.Message) error { // TypedEventToEvent takes typed event and converts to Event object func TypedEventToEvent(tev gogoproto.Message) (event.Event, error) { evtType := gogoproto.MessageName(tev) - evtJSON, err := gogoproto.Marshal(tev) + evtJSON, err := codec.ProtoMarshalJSON(tev, nil) if err != nil { return event.Event{}, err } diff --git a/server/v2/store/commands.go b/server/v2/store/commands.go index e1c8a20dfb60..820eb8485e39 100644 --- a/server/v2/store/commands.go +++ b/server/v2/store/commands.go @@ -129,7 +129,7 @@ func createRootStore(cmd *cobra.Command, rootDir string, v *viper.Viper, logger } storeOpts := root.DefaultStoreOptions() - if v != nil { + if v != nil && v.Sub("store.options") != nil { if err := v.Sub("store.options").Unmarshal(&storeOpts); err != nil { return nil, 0, fmt.Errorf("failed to store options: %w", err) } diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index 7219193935f7..79b413f16569 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -3,8 +3,6 @@ package simapp import ( _ "embed" - "github.com/spf13/viper" - clienthelpers "cosmossdk.io/client/v2/helpers" coreapp "cosmossdk.io/core/app" "cosmossdk.io/core/legacy" @@ -31,6 +29,8 @@ import ( slashingkeeper "cosmossdk.io/x/slashing/keeper" stakingkeeper "cosmossdk.io/x/staking/keeper" upgradekeeper "cosmossdk.io/x/upgrade/keeper" + _ "github.com/cosmos/cosmos-sdk/x/genutil" + "github.com/spf13/viper" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" From b458062c87f814cc06df09fd8b2502374041d3c1 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Fri, 26 Jul 2024 14:51:08 +0200 Subject: [PATCH 2/3] Review feedback --- server/v2/stf/core_event_service.go | 12 +++++++----- simapp/v2/app_di.go | 3 ++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/server/v2/stf/core_event_service.go b/server/v2/stf/core_event_service.go index f971e756a087..46b625548c05 100644 --- a/server/v2/stf/core_event_service.go +++ b/server/v2/stf/core_event_service.go @@ -1,11 +1,13 @@ package stf import ( + "bytes" "context" "encoding/json" "slices" - "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/gogoproto/jsonpb" + gogoproto "github.com/cosmos/gogoproto/proto" "golang.org/x/exp/maps" @@ -56,14 +58,14 @@ func (em *eventManager) EmitNonConsensus(event gogoproto.Message) error { // TypedEventToEvent takes typed event and converts to Event object func TypedEventToEvent(tev gogoproto.Message) (event.Event, error) { evtType := gogoproto.MessageName(tev) - evtJSON, err := codec.ProtoMarshalJSON(tev, nil) - if err != nil { + buf := new(bytes.Buffer) + jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: true, AnyResolver: nil} + if err := jm.Marshal(buf, tev); err != nil { return event.Event{}, err } var attrMap map[string]json.RawMessage - err = json.Unmarshal(evtJSON, &attrMap) - if err != nil { + if err := json.Unmarshal(buf.Bytes(), &attrMap); err != nil { return event.Event{}, err } diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index 79b413f16569..59743d0e2c10 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -3,6 +3,8 @@ package simapp import ( _ "embed" + "github.com/spf13/viper" + clienthelpers "cosmossdk.io/client/v2/helpers" coreapp "cosmossdk.io/core/app" "cosmossdk.io/core/legacy" @@ -30,7 +32,6 @@ import ( stakingkeeper "cosmossdk.io/x/staking/keeper" upgradekeeper "cosmossdk.io/x/upgrade/keeper" _ "github.com/cosmos/cosmos-sdk/x/genutil" - "github.com/spf13/viper" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" From 26cddef1ba9699b40b5468cf98f6f5f9c5063ac0 Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Sat, 27 Jul 2024 19:58:03 +0200 Subject: [PATCH 3/3] linting --- schema/appdata/async.go | 2 +- schema/appdata/mux_test.go | 1 + schema/module_schema_test.go | 22 ++++++++++--------- .../client/grpc/cmtservice/service.go | 3 ++- server/v2/stf/core_event_service.go | 1 - simapp/v2/app_di.go | 2 +- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/schema/appdata/async.go b/schema/appdata/async.go index 0d4126ed2a03..4112ae839fe9 100644 --- a/schema/appdata/async.go +++ b/schema/appdata/async.go @@ -15,7 +15,7 @@ type AsyncListenerOptions struct { BufferSize int // DoneWaitGroup is an optional wait-group that listener goroutines will notify via Add(1) when they are started - // and Done() after they are cancelled and completed. + // and Done() after they are canceled and completed. DoneWaitGroup *sync.WaitGroup } diff --git a/schema/appdata/mux_test.go b/schema/appdata/mux_test.go index b5e3a95dd569..e5fbad0b64e1 100644 --- a/schema/appdata/mux_test.go +++ b/schema/appdata/mux_test.go @@ -119,6 +119,7 @@ func callCollector(i int, onCall func(string, int, Packet)) Listener { } func checkExpectedCallOrder(t *testing.T, actual, expected []string) { + t.Helper() if len(actual) != len(expected) { t.Fatalf("expected %d calls, got %d", len(expected), len(actual)) } diff --git a/schema/module_schema_test.go b/schema/module_schema_test.go index ebd44e653632..2e4a927acc5d 100644 --- a/schema/module_schema_test.go +++ b/schema/module_schema_test.go @@ -105,19 +105,20 @@ func TestModuleSchema_Validate(t *testing.T) { }, { name: "same enum", - objectTypes: []ObjectType{{ - Name: "object1", - KeyFields: []Field{ - { - Name: "k", - Kind: EnumKind, - EnumType: EnumType{ - Name: "enum1", - Values: []string{"a", "b"}, + objectTypes: []ObjectType{ + { + Name: "object1", + KeyFields: []Field{ + { + Name: "k", + Kind: EnumKind, + EnumType: EnumType{ + Name: "enum1", + Values: []string{"a", "b"}, + }, }, }, }, - }, { Name: "object2", KeyFields: []Field{ @@ -296,6 +297,7 @@ func TestModuleSchema_LookupType(t *testing.T) { } func exampleSchema(t *testing.T) ModuleSchema { + t.Helper() return requireModuleSchema(t, []ObjectType{ { Name: "object1", diff --git a/server/v2/cometbft/client/grpc/cmtservice/service.go b/server/v2/cometbft/client/grpc/cmtservice/service.go index 3e0188c2a7e9..2e1638eaa7a1 100644 --- a/server/v2/cometbft/client/grpc/cmtservice/service.go +++ b/server/v2/cometbft/client/grpc/cmtservice/service.go @@ -4,7 +4,6 @@ import ( "context" "strings" - "cosmossdk.io/server/v2/cometbft/client/rpc" abci "github.com/cometbft/cometbft/api/cometbft/abci/v1" coretypes "github.com/cometbft/cometbft/rpc/core/types" gogogrpc "github.com/cosmos/gogoproto/grpc" @@ -13,6 +12,8 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "cosmossdk.io/server/v2/cometbft/client/rpc" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" diff --git a/server/v2/stf/core_event_service.go b/server/v2/stf/core_event_service.go index 46b625548c05..137f654ec78f 100644 --- a/server/v2/stf/core_event_service.go +++ b/server/v2/stf/core_event_service.go @@ -7,7 +7,6 @@ import ( "slices" "github.com/cosmos/gogoproto/jsonpb" - gogoproto "github.com/cosmos/gogoproto/proto" "golang.org/x/exp/maps" diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index 59743d0e2c10..559138678087 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -31,12 +31,12 @@ import ( slashingkeeper "cosmossdk.io/x/slashing/keeper" stakingkeeper "cosmossdk.io/x/staking/keeper" upgradekeeper "cosmossdk.io/x/upgrade/keeper" - _ "github.com/cosmos/cosmos-sdk/x/genutil" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/std" + _ "github.com/cosmos/cosmos-sdk/x/genutil" ) // DefaultNodeHome default home directories for the application daemon