Skip to content

Commit

Permalink
chore: cleanup core/app (cosmos#21368)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Aug 27, 2024
1 parent 58af7ee commit 355f748
Show file tree
Hide file tree
Showing 28 changed files with 296 additions and 232 deletions.
24 changes: 0 additions & 24 deletions core/app/codec.go

This file was deleted.

6 changes: 0 additions & 6 deletions core/app/identity.go

This file was deleted.

52 changes: 17 additions & 35 deletions core/app/app.go → core/server/app.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package server

import (
"context"
Expand All @@ -9,17 +9,7 @@ import (
"cosmossdk.io/core/transaction"
)

type QueryRequest struct {
Height int64
Path string
Data []byte
}

type QueryResponse struct {
Height int64
Value []byte
}

// BlockRequest defines the request structure for a block coming from consensus server to the state transition function.
type BlockRequest[T transaction.Tx] struct {
Height uint64
Time time.Time
Expand All @@ -32,39 +22,31 @@ type BlockRequest[T transaction.Tx] struct {
IsGenesis bool
}

// BlockResponse defines the response structure for a block coming from the state transition function to consensus server.
type BlockResponse struct {
Apphash []byte
ValidatorUpdates []appmodulev2.ValidatorUpdate
PreBlockEvents []event.Event
BeginBlockEvents []event.Event
TxResults []TxResult
EndBlockEvents []event.Event
}

type RequestInitChain struct {
Time time.Time
ChainId string
Validators []appmodulev2.ValidatorUpdate
AppStateBytes []byte
InitialHeight int64
}

type ResponseInitChain struct {
Validators []appmodulev2.ValidatorUpdate
AppHash []byte
}

// TxResult defines the result of a transaction execution.
type TxResult struct {
Events []event.Event
Resp []transaction.Msg
Error error
Code uint32
Data []byte
Log string
Info string
// Events produced by the transaction.
Events []event.Event
// Response messages produced by the transaction.
Resp []transaction.Msg
// Error produced by the transaction.
Error error
// Code produced by the transaction.
// A non-zero code is an error that is either define by the module via the cosmossdk.io/errors/v2 package
// or injected through the antehandler along the execution of the transaction.
Code uint32
// GasWanted is the maximum units of work we allow this tx to perform.
GasWanted uint64
GasUsed uint64
Codespace string
// GasUsed is the amount of gas actually consumed.
GasUsed uint64
}

// VersionModifier defines the interface fulfilled by BaseApp
Expand Down
20 changes: 20 additions & 0 deletions core/server/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package server

import (
"cosmossdk.io/core/transaction"
)

// InterfaceRegistry defines the interface for resolving interfaces
// The interface registry is used to resolve interfaces from type URLs,
// this is only used for the server and not for modules
type InterfaceRegistry interface {
AnyResolver
ListImplementations(ifaceTypeURL string) []string
ListAllInterfaces() []string
}

// AnyResolver defines the interface for resolving interfaces
// This is used to avoid the gogoproto import in core
type AnyResolver = interface {
Resolve(typeUrl string) (transaction.Msg, error)
}
4 changes: 4 additions & 0 deletions core/server/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Server Defines types and interfaces which are shared between Consensus, Appmanager and Stf
// This package is not meant to be used directly by modules instead if an advanced user would like
// to create a custom server or replace a component in the server they will need to use the app package.
package server
4 changes: 2 additions & 2 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/core/app"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
Expand Down Expand Up @@ -288,7 +288,7 @@ func ProvideTransientStoreService(
return transientStoreService{key: storeKey}
}

func ProvideAppVersionModifier(app *AppBuilder) app.VersionModifier {
func ProvideAppVersionModifier(app *AppBuilder) server.VersionModifier {
return app.app
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/core/app"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
Expand Down Expand Up @@ -240,8 +240,8 @@ func ProvideCometService() comet.Service {
return &services.ContextAwareCometInfoService{}
}

// ProvideAppVersionModifier returns nil, `app.VersionModifier` is a feature of BaseApp and neither used nor required for runtim/v2.
// ProvideAppVersionModifier returns nil, `app.VersionModifier` is a feature of BaseApp and neither used nor required for runtime/v2.
// nil is acceptable, see: https://github.com/cosmos/cosmos-sdk/blob/0a6ee406a02477ae8ccbfcbe1b51fc3930087f4c/x/upgrade/keeper/keeper.go#L438
func ProvideAppVersionModifier[T transaction.Tx](app *AppBuilder[T]) app.VersionModifier {
func ProvideAppVersionModifier[T transaction.Tx](app *AppBuilder[T]) server.VersionModifier {
return nil
}
16 changes: 12 additions & 4 deletions server/v2/api/grpc/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ import (
"google.golang.org/protobuf/proto"

_ "cosmossdk.io/api/amino" // Import amino.proto file for reflection
appmanager "cosmossdk.io/core/app"
"cosmossdk.io/core/server"
"cosmossdk.io/core/transaction"
)

// protocdc defines the interface for marshaling and unmarshaling messages in server/v2
type protocdc interface {
Marshal(v transaction.Msg) ([]byte, error)
Unmarshal(data []byte, v transaction.Msg) error
Name() string
}

type protoCodec struct {
interfaceRegistry appmanager.InterfaceRegistry
interfaceRegistry server.InterfaceRegistry
}

// newProtoCodec returns a reference to a new ProtoCodec
func newProtoCodec(interfaceRegistry appmanager.InterfaceRegistry) *protoCodec {
func newProtoCodec(interfaceRegistry server.InterfaceRegistry) *protoCodec {
return &protoCodec{
interfaceRegistry: interfaceRegistry,
}
Expand Down Expand Up @@ -62,7 +70,7 @@ func (pc *protoCodec) GRPCCodec() encoding.Codec {

// grpcProtoCodec is the implementation of the gRPC proto codec.
type grpcProtoCodec struct {
cdc appmanager.ProtoCodec
cdc protocdc
}

var errUnknownProtoType = errors.New("codec: unknown proto type") // sentinel error
Expand Down
18 changes: 9 additions & 9 deletions server/v2/appmanager/appmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"errors"
"fmt"

appmanager "cosmossdk.io/core/app"
"cosmossdk.io/core/server"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
)
Expand Down Expand Up @@ -39,10 +39,10 @@ type AppManager[T transaction.Tx] struct {
// InitGenesis initializes the genesis state of the application.
func (a AppManager[T]) InitGenesis(
ctx context.Context,
blockRequest *appmanager.BlockRequest[T],
blockRequest *server.BlockRequest[T],
initGenesisJSON []byte,
txDecoder transaction.Codec[T],
) (*appmanager.BlockResponse, corestore.WriterMap, error) {
) (*server.BlockResponse, corestore.WriterMap, error) {
v, zeroState, err := a.db.StateLatest()
if err != nil {
return nil, nil, fmt.Errorf("unable to get latest state: %w", err)
Expand Down Expand Up @@ -116,8 +116,8 @@ func (a AppManager[T]) ExportGenesis(ctx context.Context, version uint64) ([]byt

func (a AppManager[T]) DeliverBlock(
ctx context.Context,
block *appmanager.BlockRequest[T],
) (*appmanager.BlockResponse, corestore.WriterMap, error) {
block *server.BlockRequest[T],
) (*server.BlockResponse, corestore.WriterMap, error) {
latestVersion, currentState, err := a.db.StateLatest()
if err != nil {
return nil, nil, fmt.Errorf("unable to create new state for height %d: %w", block.Height, err)
Expand All @@ -138,19 +138,19 @@ func (a AppManager[T]) DeliverBlock(
// ValidateTx will validate the tx against the latest storage state. This means that
// only the stateful validation will be run, not the execution portion of the tx.
// If full execution is needed, Simulate must be used.
func (a AppManager[T]) ValidateTx(ctx context.Context, tx T) (appmanager.TxResult, error) {
func (a AppManager[T]) ValidateTx(ctx context.Context, tx T) (server.TxResult, error) {
_, latestState, err := a.db.StateLatest()
if err != nil {
return appmanager.TxResult{}, err
return server.TxResult{}, err
}
return a.stf.ValidateTx(ctx, latestState, a.config.ValidateTxGasLimit, tx), nil
}

// Simulate runs validation and execution flow of a Tx.
func (a AppManager[T]) Simulate(ctx context.Context, tx T) (appmanager.TxResult, corestore.WriterMap, error) {
func (a AppManager[T]) Simulate(ctx context.Context, tx T) (server.TxResult, corestore.WriterMap, error) {
_, state, err := a.db.StateLatest()
if err != nil {
return appmanager.TxResult{}, nil, err
return server.TxResult{}, nil, err
}
result, cs := a.stf.Simulate(ctx, state, a.config.SimulationGasLimit, tx) // TODO: check if this is done in the antehandler
return result, cs, nil
Expand Down
10 changes: 5 additions & 5 deletions server/v2/appmanager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package appmanager
import (
"context"

appmanager "cosmossdk.io/core/app"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
)
Expand All @@ -13,25 +13,25 @@ type StateTransitionFunction[T transaction.Tx] interface {
// DeliverBlock executes a block of transactions.
DeliverBlock(
ctx context.Context,
block *appmanager.BlockRequest[T],
block *server.BlockRequest[T],
state store.ReaderMap,
) (blockResult *appmanager.BlockResponse, newState store.WriterMap, err error)
) (blockResult *server.BlockResponse, newState store.WriterMap, err error)

// ValidateTx validates a transaction.
ValidateTx(
ctx context.Context,
state store.ReaderMap,
gasLimit uint64,
tx T,
) appmanager.TxResult
) server.TxResult

// Simulate executes a transaction in simulation mode.
Simulate(
ctx context.Context,
state store.ReaderMap,
gasLimit uint64,
tx T,
) (appmanager.TxResult, store.WriterMap)
) (server.TxResult, store.WriterMap)

// Query executes a query on the application.
Query(
Expand Down
12 changes: 4 additions & 8 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
abciproto "github.com/cometbft/cometbft/api/cometbft/abci/v1"
gogoproto "github.com/cosmos/gogoproto/proto"

coreappmgr "cosmossdk.io/core/app"
"cosmossdk.io/core/comet"
corecontext "cosmossdk.io/core/context"
"cosmossdk.io/core/event"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -134,10 +134,6 @@ func (c *Consensus[T]) CheckTx(ctx context.Context, req *abciproto.CheckTxReques
GasWanted: uint64ToInt64(resp.GasWanted),
GasUsed: uint64ToInt64(resp.GasUsed),
Events: intoABCIEvents(resp.Events, c.indexedEvents),
Info: resp.Info,
Data: resp.Data,
Log: resp.Log,
Codespace: resp.Codespace,
}
if resp.Error != nil {
cometResp.Code = 1
Expand Down Expand Up @@ -268,7 +264,7 @@ func (c *Consensus[T]) InitChain(ctx context.Context, req *abciproto.InitChainRe
// populate hash with empty byte slice instead of nil
bz := sha256.Sum256([]byte{})

br := &coreappmgr.BlockRequest[T]{
br := &server.BlockRequest[T]{
Height: uint64(req.InitialHeight - 1),
Time: req.Time,
Hash: bz[:],
Expand All @@ -289,7 +285,7 @@ func (c *Consensus[T]) InitChain(ctx context.Context, req *abciproto.InitChainRe
// TODO necessary? where should this WARN live if it all. helpful for testing
for _, txRes := range blockresponse.TxResults {
if txRes.Error != nil {
c.logger.Warn("genesis tx failed", "code", txRes.Code, "log", txRes.Log, "error", txRes.Error)
c.logger.Warn("genesis tx failed", "code", txRes.Code, "error", txRes.Error)
}
}

Expand Down Expand Up @@ -441,7 +437,7 @@ func (c *Consensus[T]) FinalizeBlock(
return nil, err
}

blockReq := &coreappmgr.BlockRequest[T]{
blockReq := &server.BlockRequest[T]{
Height: uint64(req.Height),
Time: req.Time,
Hash: req.Hash,
Expand Down
4 changes: 2 additions & 2 deletions server/v2/cometbft/handlers/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"github.com/cosmos/gogoproto/proto"

consensusv1 "cosmossdk.io/api/cosmos/consensus/v1"
appmanager "cosmossdk.io/core/app"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
"cosmossdk.io/server/v2/cometbft/mempool"
)

type AppManager[T transaction.Tx] interface {
ValidateTx(ctx context.Context, tx T) (appmanager.TxResult, error)
ValidateTx(ctx context.Context, tx T) (server.TxResult, error)
Query(ctx context.Context, version uint64, request transaction.Msg) (response transaction.Msg, err error)
}

Expand Down
Loading

0 comments on commit 355f748

Please sign in to comment.