From 9dcba88958de01dbfc5c33bd55b6169fb9d69b1f Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 6 Nov 2024 16:23:52 +0100 Subject: [PATCH 1/4] refactor(server/v2): remove serverv2.AppI --- server/v2/api/grpc/server.go | 14 ++++++-------- server/v2/api/grpc/service.go | 6 ++---- server/v2/api/grpc/service_test.go | 3 --- server/v2/server_test.go | 30 ++++++++++-------------------- server/v2/store/server.go | 12 ++++++------ server/v2/types.go | 26 -------------------------- simapp/v2/app_di.go | 9 --------- simapp/v2/simdv2/cmd/commands.go | 2 +- 8 files changed, 25 insertions(+), 77 deletions(-) delete mode 100644 server/v2/types.go diff --git a/server/v2/api/grpc/server.go b/server/v2/api/grpc/server.go index 12cc1e6add1a..f42648c54e8f 100644 --- a/server/v2/api/grpc/server.go +++ b/server/v2/api/grpc/server.go @@ -48,9 +48,7 @@ func New[T transaction.Tx]( logger log.Logger, interfaceRegistry server.InterfaceRegistry, queryHandlers map[string]appmodulev2.Handler, - queryable interface { - Query(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error) - }, + queryable func(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error), cfg server.ConfigMap, cfgOptions ...CfgOption, ) (*Server[T], error) { @@ -74,7 +72,7 @@ func New[T transaction.Tx]( // Reflection allows external clients to see what services and methods the gRPC server exposes. gogoreflection.Register(grpcSrv, slices.Collect(maps.Keys(queryHandlers)), logger.With("sub-module", "grpc-reflection")) - // Register V2 + // Register V2 grpc handlers RegisterServiceServer(grpcSrv, &v2Service{queryHandlers, queryable}) srv.grpcSrv = grpcSrv @@ -99,9 +97,9 @@ func (s *Server[T]) StartCmdFlags() *pflag.FlagSet { return flags } -func makeUnknownServiceHandler(handlers map[string]appmodulev2.Handler, querier interface { - Query(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error) -}, +func makeUnknownServiceHandler( + handlers map[string]appmodulev2.Handler, + querier func(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error), ) grpc.StreamHandler { getRegistry := sync.OnceValues(gogoproto.MergedRegistry) @@ -149,7 +147,7 @@ func makeUnknownServiceHandler(handlers map[string]appmodulev2.Handler, querier if err != nil { return status.Errorf(codes.InvalidArgument, "invalid get height from context: %v", err) } - resp, err := querier.Query(ctx, height, req) + resp, err := querier(ctx, height, req) if err != nil { return err } diff --git a/server/v2/api/grpc/service.go b/server/v2/api/grpc/service.go index e373fa910469..051e298a5c7e 100644 --- a/server/v2/api/grpc/service.go +++ b/server/v2/api/grpc/service.go @@ -15,9 +15,7 @@ import ( // v2Service implements the gRPC service interface for handling queries and listing handlers. type v2Service struct { queryHandlers map[string]appmodulev2.Handler - queryable interface { - Query(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error) - } + queryable func(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error) } // Query handles incoming query requests by unmarshaling the request, processing it, @@ -39,7 +37,7 @@ func (s v2Service) Query(ctx context.Context, request *QueryRequest) (*QueryResp return nil, status.Errorf(codes.InvalidArgument, "failed to unmarshal request: %v", err) } - queryResp, err := s.queryable.Query(ctx, 0, protoMsg) + queryResp, err := s.queryable(ctx, 0, protoMsg) if err != nil { return nil, status.Errorf(codes.Internal, "query failed: %v", err) } diff --git a/server/v2/api/grpc/service_test.go b/server/v2/api/grpc/service_test.go index 5c416f60c166..ebe53058c06f 100644 --- a/server/v2/api/grpc/service_test.go +++ b/server/v2/api/grpc/service_test.go @@ -12,7 +12,6 @@ import ( appmodulev2 "cosmossdk.io/core/appmodule/v2" "cosmossdk.io/core/transaction" - serverv2 "cosmossdk.io/server/v2" ) type MockRequestMessage struct { @@ -42,8 +41,6 @@ func (m *MockResponseMessage) ValidateBasic() error { type mockApp[T transaction.Tx] struct { mock.Mock - - serverv2.AppI[T] } func (m *mockApp[T]) QueryHandlers() map[string]appmodulev2.Handler { diff --git a/server/v2/server_test.go b/server/v2/server_test.go index d71416dfb3fd..c673a4ead3c0 100644 --- a/server/v2/server_test.go +++ b/server/v2/server_test.go @@ -12,7 +12,6 @@ import ( "github.com/stretchr/testify/require" appmodulev2 "cosmossdk.io/core/appmodule/v2" - coreserver "cosmossdk.io/core/server" "cosmossdk.io/core/transaction" "cosmossdk.io/log" serverv2 "cosmossdk.io/server/v2" @@ -21,6 +20,14 @@ import ( storev2 "cosmossdk.io/store/v2" ) +type mockStore struct { + storev2.RootStore +} + +func (*mockStore) Close() error { + return nil +} + type mockInterfaceRegistry struct{} func (*mockInterfaceRegistry) Resolve(typeUrl string) (gogoproto.Message, error) { @@ -32,22 +39,6 @@ func (*mockInterfaceRegistry) ListImplementations(ifaceTypeURL string) []string } func (*mockInterfaceRegistry) ListAllInterfaces() []string { panic("not implemented") } -type mockApp[T transaction.Tx] struct { - serverv2.AppI[T] -} - -func (*mockApp[T]) QueryHandlers() map[string]appmodulev2.Handler { - return map[string]appmodulev2.Handler{} -} - -func (*mockApp[T]) InterfaceRegistry() coreserver.InterfaceRegistry { - return &mockInterfaceRegistry{} -} - -func (*mockApp[T]) Store() storev2.RootStore { - return nil -} - func TestServer(t *testing.T) { currentDir, err := os.Getwd() require.NoError(t, err) @@ -62,12 +53,11 @@ func TestServer(t *testing.T) { logger := log.NewLogger(os.Stdout) ctx := serverv2.SetServerContext(context.Background(), v, logger) - app := &mockApp[transaction.Tx]{} - grpcServer, err := grpc.New[transaction.Tx](logger, app.InterfaceRegistry(), app.QueryHandlers(), app, cfg) + grpcServer, err := grpc.New[transaction.Tx](logger, &mockInterfaceRegistry{}, map[string]appmodulev2.Handler{}, nil, cfg) require.NoError(t, err) - storeServer, err := store.New[transaction.Tx](app.Store(), cfg) + storeServer, err := store.New[transaction.Tx](&mockStore{}, cfg) require.NoError(t, err) mockServer := &mockServer{name: "mock-server-1", ch: make(chan string, 100)} diff --git a/server/v2/store/server.go b/server/v2/store/server.go index 897773abf28e..fb327839859d 100644 --- a/server/v2/store/server.go +++ b/server/v2/store/server.go @@ -23,18 +23,18 @@ const ServerName = "store" // Server manages store config and contains prune & snapshot commands type Server[T transaction.Tx] struct { - config *root.Config - backend storev2.Backend + config *root.Config + store storev2.RootStore } -func New[T transaction.Tx](store storev2.Backend, cfg server.ConfigMap) (*Server[T], error) { +func New[T transaction.Tx](store storev2.RootStore, cfg server.ConfigMap) (*Server[T], error) { config, err := UnmarshalConfig(cfg) if err != nil { return nil, err } return &Server[T]{ - backend: store, - config: config, + store: store, + config: config, }, nil } @@ -47,7 +47,7 @@ func (s *Server[T]) Start(context.Context) error { } func (s *Server[T]) Stop(context.Context) error { - return nil + return s.store.Close() } func (s *Server[T]) CLICommands() serverv2.CLIConfig { diff --git a/server/v2/types.go b/server/v2/types.go deleted file mode 100644 index 40d51e42375c..000000000000 --- a/server/v2/types.go +++ /dev/null @@ -1,26 +0,0 @@ -package serverv2 - -import ( - "github.com/spf13/viper" - - appmodulev2 "cosmossdk.io/core/appmodule/v2" - "cosmossdk.io/core/server" - "cosmossdk.io/core/transaction" - "cosmossdk.io/log" - "cosmossdk.io/schema/decoding" - "cosmossdk.io/server/v2/appmanager" - "cosmossdk.io/store/v2" -) - -type AppCreator[T transaction.Tx] func(log.Logger, *viper.Viper) AppI[T] - -type AppI[T transaction.Tx] interface { - appmanager.AppManager[T] - - Name() string - InterfaceRegistry() server.InterfaceRegistry - QueryHandlers() map[string]appmodulev2.Handler - Store() store.RootStore - SchemaDecoderResolver() decoding.DecoderResolver - Close() error -} diff --git a/simapp/v2/app_di.go b/simapp/v2/app_di.go index 5fa97c224161..4d6c3078cbed 100644 --- a/simapp/v2/app_di.go +++ b/simapp/v2/app_di.go @@ -201,15 +201,6 @@ func (app *SimApp[T]) Store() store.RootStore { return app.store } -// Close overwrites the base Close method to close the stores. -func (app *SimApp[T]) Close() error { - if err := app.store.Close(); err != nil { - return err - } - - return app.App.Close() -} - func ProvideRootStoreConfig(config runtime.GlobalConfig) (*root.Config, error) { return serverstore.UnmarshalConfig(config) } diff --git a/simapp/v2/simdv2/cmd/commands.go b/simapp/v2/simdv2/cmd/commands.go index c001db4dff92..06c9702933b2 100644 --- a/simapp/v2/simdv2/cmd/commands.go +++ b/simapp/v2/simdv2/cmd/commands.go @@ -83,7 +83,7 @@ func InitRootCmd[T transaction.Tx]( // build full app! simApp := deps.SimApp - grpcServer, err := grpc.New[T](logger, simApp.InterfaceRegistry(), simApp.QueryHandlers(), simApp, deps.GlobalConfig) + grpcServer, err := grpc.New[T](logger, simApp.InterfaceRegistry(), simApp.QueryHandlers(), simApp.Query, deps.GlobalConfig) if err != nil { return nil, err } From 9d833372717c1067ee6d39412391bfcc115f0a19 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 6 Nov 2024 16:24:29 +0100 Subject: [PATCH 2/4] updates --- server/v2/api/grpc/server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/v2/api/grpc/server.go b/server/v2/api/grpc/server.go index f42648c54e8f..79d31f0fd9db 100644 --- a/server/v2/api/grpc/server.go +++ b/server/v2/api/grpc/server.go @@ -99,7 +99,7 @@ func (s *Server[T]) StartCmdFlags() *pflag.FlagSet { func makeUnknownServiceHandler( handlers map[string]appmodulev2.Handler, - querier func(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error), + queryable func(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error), ) grpc.StreamHandler { getRegistry := sync.OnceValues(gogoproto.MergedRegistry) @@ -147,7 +147,7 @@ func makeUnknownServiceHandler( if err != nil { return status.Errorf(codes.InvalidArgument, "invalid get height from context: %v", err) } - resp, err := querier(ctx, height, req) + resp, err := queryable(ctx, height, req) if err != nil { return err } From f3881ef8127441ce0b4bb34f6bb61901cf3f78cb Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 6 Nov 2024 16:28:01 +0100 Subject: [PATCH 3/4] go mod tidy --- server/v2/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/v2/go.mod b/server/v2/go.mod index 528f1d26c839..23253ed93b88 100644 --- a/server/v2/go.mod +++ b/server/v2/go.mod @@ -16,7 +16,6 @@ require ( cosmossdk.io/core v1.0.0-alpha.5 cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 cosmossdk.io/log v1.4.1 - cosmossdk.io/schema v0.3.1-0.20241010135032-192601639cac cosmossdk.io/server/v2/appmanager v0.0.0-00010101000000-000000000000 cosmossdk.io/store/v2 v2.0.0-00010101000000-000000000000 github.com/cosmos/cosmos-proto v1.0.0-beta.5 @@ -43,6 +42,7 @@ require ( require ( cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 // indirect + cosmossdk.io/schema v0.3.1-0.20241010135032-192601639cac // indirect github.com/DataDog/datadog-go v4.8.3+incompatible // indirect github.com/DataDog/zstd v1.5.5 // indirect github.com/Microsoft/go-winio v0.6.1 // indirect From 82cd2924ceb801c729bd83d49327e169f8e2e31b Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 6 Nov 2024 16:28:35 +0100 Subject: [PATCH 4/4] build --- server/v2/api/grpc/service_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/v2/api/grpc/service_test.go b/server/v2/api/grpc/service_test.go index ebe53058c06f..aed0fd3faadc 100644 --- a/server/v2/api/grpc/service_test.go +++ b/server/v2/api/grpc/service_test.go @@ -136,7 +136,7 @@ func TestQuery(t *testing.T) { tt.setupMock(mockApp) } - service := &v2Service{mockApp.QueryHandlers(), mockApp} + service := &v2Service{mockApp.QueryHandlers(), mockApp.Query} resp, err := service.Query(context.Background(), tt.request) if tt.expectError { @@ -192,7 +192,7 @@ func TestV2Service_ListQueryHandlers(t *testing.T) { tt.setupMock(mockApp) } - service := &v2Service{mockApp.QueryHandlers(), mockApp} + service := &v2Service{mockApp.QueryHandlers(), mockApp.Query} resp, err := service.ListQueryHandlers(context.Background(), &ListQueryHandlersRequest{}) assert.NoError(t, err)