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

chore: remove appmanager poc #22422

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 core/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package context
type (
execModeKey struct{}
cometInfoKey struct{}
headerInfoKey struct{}
initInfoKey struct{}
environmentKey struct{}
)
Expand All @@ -12,6 +13,8 @@ var (
ExecModeKey = execModeKey{}
// CometInfoKey is the context key for allowing modules to get CometInfo.
CometInfoKey = cometInfoKey{}
// HeaderInfoKey is the context key for allowing modules to get HeaderInfo.
HeaderInfoKey = headerInfoKey{}
// CometParamsInitInfoKey is the context key for setting consensus params from genesis in the consensus module.
CometParamsInitInfoKey = initInfoKey{}

Expand Down
7 changes: 6 additions & 1 deletion runtime/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// done declaratively with an app config and the rest of it is done the old way.
// See simapp/app_v2.go for an example of this setup.
type App[T transaction.Tx] struct {
appmanager.AppManager[T]
appmanager.GenesisManager[T]

// app configuration
logger log.Logger
Expand Down Expand Up @@ -88,6 +88,11 @@ func (a *App[T]) QueryHandlers() map[string]appmodulev2.Handler {
return a.queryHandlers
}

// StateTransitionFunction returns the state transition function.
func (a *App[T]) StateTransitionFunction() *stf.STF[T] {
Copy link
Member

Choose a reason for hiding this comment

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

i don't think we should return the concrete type here

return a.stf
}

// SchemaDecoderResolver returns the module schema resolver.
func (a *App[T]) SchemaDecoderResolver() decoding.DecoderResolver {
moduleSet := map[string]any{}
Expand Down
9 changes: 1 addition & 8 deletions runtime/v2/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,7 @@ func (a *AppBuilder[T]) Build(opts ...AppBuilderOption[T]) (*App[T], error) {
}
a.app.stf = stf

a.app.AppManager = appmanager.New[T](
appmanager.Config{
Copy link
Member

Choose a reason for hiding this comment

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

We should remove that from runtime v2 config then and from the the appconfig

ValidateTxGasLimit: a.app.config.GasConfig.ValidateTxGasLimit,
QueryGasLimit: a.app.config.GasConfig.QueryGasLimit,
SimulationGasLimit: a.app.config.GasConfig.SimulationGasLimit,
},
a.app.db,
a.app.stf,
a.app.GenesisManager = appmanager.New[T](
a.initGenesis,
a.exportGenesis,
)
Expand Down
13 changes: 12 additions & 1 deletion runtime/v2/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"context"
"fmt"
"os"
"slices"
Expand All @@ -16,8 +17,10 @@ import (
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/comet"
corecontext "cosmossdk.io/core/context"
"cosmossdk.io/core/event"
"cosmossdk.io/core/header"
coreheader "cosmossdk.io/core/header"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
Expand Down Expand Up @@ -247,7 +250,7 @@ func DefaultServiceBindings() depinject.Config {
)
}
cometService comet.Service = &services.ContextAwareCometInfoService{}
headerService = services.NewGenesisHeaderService(stf.HeaderService{})
headerService = services.NewGenesisHeaderService(HeaderService{})
eventService = services.NewGenesisEventService(stf.NewEventService())
storeBuilder = root.NewBuilder()
)
Expand All @@ -259,3 +262,11 @@ func DefaultServiceBindings() depinject.Config {
storeBuilder,
)
}

var _ header.Service = (*HeaderService)(nil)

type HeaderService struct{}

func (h HeaderService) HeaderInfo(ctx context.Context) header.Info {
return ctx.Value(corecontext.HeaderInfoKey).(coreheader.Info)
}
35 changes: 27 additions & 8 deletions server/v2/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (

appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/server"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
serverv2 "cosmossdk.io/server/v2"
"cosmossdk.io/server/v2/api/grpc/gogoreflection"
"cosmossdk.io/server/v2/appmanager"
)

const (
Expand All @@ -45,9 +47,11 @@ type Server[T transaction.Tx] struct {
// New creates a new grpc server.
func New[T transaction.Tx](
logger log.Logger,
store serverv2.Store,
stf appmanager.StateTransitionFunction[T],
gasLimit uint64,
interfaceRegistry server.InterfaceRegistry,
queryHandlers map[string]appmodulev2.Handler,
queryable func(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error),
cfg server.ConfigMap,
cfgOptions ...CfgOption,
) (*Server[T], error) {
Expand All @@ -65,14 +69,19 @@ func New[T transaction.Tx](
grpc.ForceServerCodec(newProtoCodec(interfaceRegistry).GRPCCodec()),
grpc.MaxSendMsgSize(serverCfg.MaxSendMsgSize),
grpc.MaxRecvMsgSize(serverCfg.MaxRecvMsgSize),
grpc.UnknownServiceHandler(makeUnknownServiceHandler(queryHandlers, queryable)),
grpc.UnknownServiceHandler(makeUnknownServiceHandler(queryHandlers, stf, store, gasLimit)),
)

// 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 grpc handlers
RegisterServiceServer(grpcSrv, &v2Service{queryHandlers, queryable})
RegisterServiceServer(grpcSrv, &v2Service[T]{
queryHandlers,
stf,
store,
gasLimit,
})

srv.grpcSrv = grpcSrv
srv.config = serverCfg
Expand All @@ -96,10 +105,7 @@ func (s *Server[T]) StartCmdFlags() *pflag.FlagSet {
return flags
}

func makeUnknownServiceHandler(
handlers map[string]appmodulev2.Handler,
queryable func(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error),
) grpc.StreamHandler {
func makeUnknownServiceHandler[T transaction.Tx](handlers map[string]appmodulev2.Handler, stf appmanager.StateTransitionFunction[T], store serverv2.Store, gasLimit uint64) grpc.StreamHandler {
getRegistry := sync.OnceValues(gogoproto.MergedRegistry)

return func(srv any, stream grpc.ServerStream) error {
Expand Down Expand Up @@ -146,7 +152,20 @@ func makeUnknownServiceHandler(
if err != nil {
return status.Errorf(codes.InvalidArgument, "invalid get height from context: %v", err)
}
resp, err := queryable(ctx, height, req)

var state corestore.ReaderMap
if height == 0 {
_, state, err = store.StateLatest()
if err != nil {
return status.Errorf(codes.Internal, "failed to get latest state: %v", err)
}
} else {
state, err = store.StateAt(height)
if err != nil {
return status.Errorf(codes.Internal, "failed to get state at height %d: %v", height, err)
}
}
resp, err := stf.Query(ctx, state, gasLimit, req)
if err != nil {
return err
}
Expand Down
33 changes: 28 additions & 5 deletions server/v2/api/grpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ import (
"google.golang.org/grpc/status"

appmodulev2 "cosmossdk.io/core/appmodule/v2"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
serverv2 "cosmossdk.io/server/v2"
"cosmossdk.io/server/v2/appmanager"
)

// v2Service implements the gRPC service interface for handling queries and listing handlers.
type v2Service struct {
type v2Service[T transaction.Tx] struct {
queryHandlers map[string]appmodulev2.Handler
queryable func(ctx context.Context, version uint64, msg transaction.Msg) (transaction.Msg, error)
stf appmanager.StateTransitionFunction[T]
store serverv2.Store
gaslimit uint64
}

// Query handles incoming query requests by unmarshaling the request, processing it,
// and returning the response in an Any protobuf message.
func (s v2Service) Query(ctx context.Context, request *QueryRequest) (*QueryResponse, error) {
func (s v2Service[T]) Query(ctx context.Context, request *QueryRequest) (*QueryResponse, error) {
if request == nil || request.Request == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
Expand All @@ -37,7 +42,25 @@ 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(ctx, 0, protoMsg)
height, err := getHeightFromCtx(ctx)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid get height from context: %v", err)
}

var state corestore.ReaderMap
if height == 0 {
_, state, err = s.store.StateLatest()
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get latest state: %v", err)
}
} else {
state, err = s.store.StateAt(height)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get state at height %d: %v", height, err)
}
}

queryResp, err := s.stf.Query(ctx, state, s.gaslimit, protoMsg)
if err != nil {
return nil, status.Errorf(codes.Internal, "query failed: %v", err)
}
Expand All @@ -55,7 +78,7 @@ func (s v2Service) Query(ctx context.Context, request *QueryRequest) (*QueryResp
return &QueryResponse{Response: anyResp}, nil
}

func (s v2Service) ListQueryHandlers(_ context.Context, _ *ListQueryHandlersRequest) (*ListQueryHandlersResponse, error) {
func (s v2Service[T]) ListQueryHandlers(_ context.Context, _ *ListQueryHandlersRequest) (*ListQueryHandlersResponse, error) {
var handlerDescriptors []*Handler
for handlerName := range s.queryHandlers {
msg := s.queryHandlers[handlerName].MakeMsg()
Expand Down
43 changes: 37 additions & 6 deletions server/v2/api/rest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,36 @@ import (
"io"
"net/http"
"reflect"
"strconv"
"strings"

"github.com/cosmos/gogoproto/jsonpb"
gogoproto "github.com/cosmos/gogoproto/proto"

"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
serverv2 "cosmossdk.io/server/v2"
"cosmossdk.io/server/v2/appmanager"
)

const (
ContentTypeJSON = "application/json"
MaxBodySize = 1 << 20 // 1 MB
ContentTypeJSON = "application/json"
MaxBodySize = 1 << 20 // 1 MB
BlockHeightHeader = "x-cosmos-block-height"
)

func NewDefaultHandler[T transaction.Tx](appManager appmanager.AppManager[T]) http.Handler {
return &DefaultHandler[T]{appManager: appManager}
func NewDefaultHandler[T transaction.Tx](stf appmanager.StateTransitionFunction[T], store serverv2.Store, gasLimit uint64) http.Handler {
return &DefaultHandler[T]{
stf: stf,
store: store,
gasLimit: gasLimit,
}
}

type DefaultHandler[T transaction.Tx] struct {
appManager appmanager.AppManager[T]
stf appmanager.StateTransitionFunction[T]
store serverv2.Store
gasLimit uint64
}

func (h *DefaultHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -45,7 +55,28 @@ func (h *DefaultHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

query, err := h.appManager.Query(r.Context(), 0, msg)
var reader store.ReaderMap
height := w.Header().Get(BlockHeightHeader)
if height == "" {
_, reader, err = h.store.StateLatest()
if err != nil {
http.Error(w, "Error getting latest state", http.StatusInternalServerError)
return
}
} else {
ih, err := strconv.ParseUint(height, 10, 64)
if err != nil {
http.Error(w, "Error parsing block height", http.StatusBadRequest)
return
}
reader, err = h.store.StateAt(ih)
if err != nil {
http.Error(w, "Error getting state at height", http.StatusInternalServerError)
return
}
}

query, err := h.stf.Query(r.Context(), reader, h.gasLimit, msg)
if err != nil {
http.Error(w, "Error querying", http.StatusInternalServerError)
return
Expand Down
6 changes: 4 additions & 2 deletions server/v2/api/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ type Server[T transaction.Tx] struct {

func New[T transaction.Tx](
logger log.Logger,
appManager appmanager.AppManager[T],
stf appmanager.StateTransitionFunction[T],
store serverv2.Store,
gasLimit uint64,
cfg server.ConfigMap,
cfgOptions ...CfgOption,
) (*Server[T], error) {
Expand All @@ -37,7 +39,7 @@ func New[T transaction.Tx](
router: http.NewServeMux(),
}

srv.router.Handle("/", NewDefaultHandler(appManager))
srv.router.Handle("/", NewDefaultHandler(stf, store, gasLimit))

serverCfg := srv.Config().(*Config)
if len(cfg) > 0 {
Expand Down
Loading
Loading