package baseapp import ( "fmt" "reflect" "strings" "sync" "time" "github.com/armon/go-metrics" "github.com/ethereum/go-ethereum/common" "github.com/gogo/protobuf/proto" "github.com/holiman/uint256" "github.com/sei-protocol/sei-chain/sei-cosmos/codec/types" cryptotypes "github.com/sei-protocol/sei-chain/sei-cosmos/crypto/types" "github.com/sei-protocol/sei-chain/sei-cosmos/server/config" servertypes "github.com/sei-protocol/sei-chain/sei-cosmos/server/types" "github.com/sei-protocol/sei-chain/sei-cosmos/snapshots" "github.com/sei-protocol/sei-chain/sei-cosmos/store" "github.com/sei-protocol/sei-chain/sei-cosmos/telemetry" sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types" sdkerrors "github.com/sei-protocol/sei-chain/sei-cosmos/types/errors" "github.com/sei-protocol/sei-chain/sei-cosmos/utils/tracing" "github.com/sei-protocol/sei-chain/sei-cosmos/x/auth/legacy/legacytx" abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types" tmcfg "github.com/sei-protocol/sei-chain/sei-tendermint/config" tmproto "github.com/sei-protocol/sei-chain/sei-tendermint/proto/tendermint/types" sdbm "github.com/sei-protocol/sei-tm-db/backends" "github.com/sei-protocol/seilog" "github.com/spf13/cast" leveldbutils "github.com/syndtr/goleveldb/leveldb/util" dbm "github.com/tendermint/tm-db" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" otelmetric "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace/noop" ) const ( runTxModeCheck runTxMode = iota // Check a transaction runTxModeReCheck // Recheck a (pending) transaction after a commit runTxModeSimulate // Simulate a transaction runTxModeDeliver // Deliver a transaction ) var modeKeyToString = map[runTxMode]string{ runTxModeCheck: "check", runTxModeReCheck: "recheck", runTxModeSimulate: "simulate", runTxModeDeliver: "deliver", } const ( // archival related flags FlagArchivalVersion = "archival-version" FlagArchivalDBType = "archival-db-type" FlagArchivalArweaveIndexDBFullPath = "archival-arweave-index-db-full-path" FlagArchivalArweaveNodeURL = "archival-arweave-node-url" FlagChainID = "chain-id" FlagConcurrencyWorkers = "concurrency-workers" FlagOccEnabled = "occ-enabled" ) var ( logger = seilog.NewLogger("cosmos", "baseapp") _ abci.Application = (*BaseApp)(nil) ) type ( // Enum mode for app.runTx runTxMode uint8 // StoreLoader defines a customizable function to control how we load the CommitMultiStore // from disk. This is useful for state migration, when loading a datastore written with // an older version of the software. In particular, if a module changed the substore key name // (or removed a substore) between two versions of the software. StoreLoader func(ms sdk.CommitMultiStore) error DeliverTxHook func(sdk.Context, sdk.Tx, [32]byte, sdk.DeliverTxHookInput) ) func (app *BaseApp) EvmNonce(_ common.Address) uint64 { return 0 } func (app *BaseApp) EvmBalance(_ common.Address, _ []byte) uint256.Int { return uint256.Int{} } // BaseApp reflects the ABCI application implementation. type BaseApp struct { // initialized on creation name string // application name from abci.Info interfaceRegistry types.InterfaceRegistry txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx processProposalHandler sdk.ProcessProposalHandler finalizeBlocker sdk.FinalizeBlocker anteHandler sdk.AnteHandler // ante handler for fee and auth loadVersionHandler sdk.LoadVersionHandler preCommitHandler sdk.PreCommitHandler closeHandler sdk.CloseHandler inplaceTestnetInitializer sdk.InplaceTestnetInitializer txPrioritizer sdk.TxPrioritizer appStore baseappVersions snapshotData abciData moduleRouter // volatile states: // // checkState is set on InitChain and reset on Commit // deliverState is set on InitChain and BeginBlock and set to nil on Commit checkState *state // for CheckTx deliverState *state // for DeliverTx processProposalState *state processProposalCleanCtx sdk.Context // snapshot before optimistic processing stateToCommit *state // paramStore is used to query for ABCI consensus parameters from an // application parameter store. paramStore ParamStore // The minimum gas prices a validator is willing to accept for processing a // transaction. This is mainly used for DoS and spam prevention. minGasPrices sdk.DecCoins // initialHeight is the initial height at which we start the baseapp initialHeight int64 // flag for sealing options and parameters to a BaseApp sealed bool // block height at which to halt the chain and gracefully shutdown haltHeight uint64 // minimum block time (in Unix seconds) at which to halt the chain and gracefully shutdown haltTime uint64 // minRetainBlocks defines the minimum block height offset from the current // block being committed, such that all blocks past this offset are pruned // from Tendermint. It is used as part of the process of determining the // ResponseCommit.RetainHeight value during ABCI Commit. A value of 0 indicates // that no blocks should be pruned. // // Note: Tendermint block pruning is dependant on this parameter in conunction // with the unbonding (safety threshold) period, state pruning and state sync // snapshot parameters to determine the correct minimum value of // ResponseCommit.RetainHeight. minRetainBlocks uint64 // recovery handler for app.runTx method runTxRecoveryMiddleware recoveryMiddleware // trace set will return full stack traces for errors in ABCI Log field trace bool // indexEvents defines the set of events in the form {eventType}.{attributeKey}, // which informs Tendermint what to index. If empty, all events will be indexed. IndexEvents map[string]struct{} ChainID string commitLock *sync.Mutex checkTxStateLock *sync.RWMutex compactionInterval uint64 TmConfig *tmcfg.Config TracingInfo *tracing.Info concurrencyWorkers int occEnabled bool deliverTxHooks []DeliverTxHook execProcessProposalMs int64 execFinalizeBlockMs int64 execBlockTxCount int } type appStore struct { db dbm.DB // common DB backend cms sdk.CommitMultiStore // Main (uncached) state qms sdk.CommitMultiStore // Query multistore used for migration only migrationHeight int64 storeLoader StoreLoader // function to handle store loading, may be overridden with SetStoreLoader() // an inter-block write-through cache provided to the context during deliverState interBlockCache sdk.MultiStorePersistentCache fauxMerkleMode bool // if true, IAVL MountStores uses MountStoresDB for simulation speed. } type moduleRouter struct { router sdk.Router // handle any kind of message queryRouter sdk.QueryRouter // router for redirecting query calls grpcQueryRouter *GRPCQueryRouter // router for redirecting gRPC query calls msgServiceRouter *MsgServiceRouter // router for redirecting Msg service messages } type abciData struct { initChainer sdk.InitChainer // initialize state with validators and state blob midBlocker sdk.MidBlocker // logic to run after all txs, and to determine valset changes endBlocker sdk.EndBlocker // logic to run after all txs, and to determine valset changes } type baseappVersions struct { // application's version string version string // application's protocol version that increments on every upgrade // if BaseApp is passed to the upgrade keeper's NewKeeper method. appVersion uint64 } // should really get handled in some db struct // which then has a sub-item, persistence fields type snapshotData struct { // manages snapshots, i.e. dumps of app state at certain intervals snapshotManager *snapshots.Manager snapshotInterval uint64 // block interval between state sync snapshots snapshotKeepRecent uint32 // recent state sync snapshots to keep snapshotDirectory string // state sync snapshots directory } // NewBaseApp returns a reference to an initialized BaseApp. It accepts a // variadic number of option functions, which act on the BaseApp to set // configuration choices. // // NOTE: The db is used to store the version number for now. func NewBaseApp( name string, db dbm.DB, txDecoder sdk.TxDecoder, tmConfig *tmcfg.Config, appOpts servertypes.AppOptions, options ...func(*BaseApp), ) *BaseApp { cms := store.NewCommitMultiStore(db) archivalVersion := cast.ToInt64(appOpts.Get(FlagArchivalVersion)) if archivalVersion > 0 { switch cast.ToString(appOpts.Get(FlagArchivalDBType)) { case "arweave": indexDbPath := cast.ToString(appOpts.Get(FlagArchivalArweaveIndexDBFullPath)) arweaveNodeUrl := cast.ToString(appOpts.Get(FlagArchivalArweaveNodeURL)) arweaveDb, err := sdbm.NewArweaveDB(indexDbPath, arweaveNodeUrl) if err != nil { panic(err) } cms = store.NewCommitMultiStoreWithArchival(db, arweaveDb, archivalVersion) } } // Enable Tracing tp := noop.NewTracerProvider() otel.SetTracerProvider(noop.NewTracerProvider()) tr := tp.Tracer("component-main") tracingEnabled := cast.ToBool(appOpts.Get(tracing.FlagTracing)) if tracingEnabled { tp, err := tracing.DefaultTracerProvider() if err != nil { panic(err) } otel.SetTracerProvider(tp) tr = tp.Tracer("component-main") } app := &BaseApp{ name: name, appStore: appStore{ db: db, cms: cms, storeLoader: DefaultStoreLoader, fauxMerkleMode: false, }, moduleRouter: moduleRouter{ router: NewRouter(), queryRouter: NewQueryRouter(), grpcQueryRouter: NewGRPCQueryRouter(), msgServiceRouter: NewMsgServiceRouter(), }, txDecoder: txDecoder, TmConfig: tmConfig, TracingInfo: tracing.NewTracingInfo(tr, tracingEnabled), commitLock: &sync.Mutex{}, checkTxStateLock: &sync.RWMutex{}, deliverTxHooks: []DeliverTxHook{}, } for _, option := range options { option(app) } if app.interBlockCache != nil { app.cms.SetInterBlockCache(app.interBlockCache) } app.runTxRecoveryMiddleware = newDefaultRecoveryMiddleware() app.ChainID = cast.ToString(appOpts.Get(FlagChainID)) if app.ChainID == "" { panic("must pass --chain-id when calling 'seid start' or set in ~/.sei/config/client.toml") } app.startCompactionRoutine(db) // if no option overrode already, initialize to the flags value // this avoids forcing every implementation to pass an option, but allows it if app.concurrencyWorkers == 0 { app.concurrencyWorkers = cast.ToInt(appOpts.Get(FlagConcurrencyWorkers)) } // safely default this to the default value if 0 if app.concurrencyWorkers == 0 { app.concurrencyWorkers = config.DefaultConcurrencyWorkers } return app } // Name returns the name of the BaseApp. func (app *BaseApp) Name() string { return app.name } // AppVersion returns the application's protocol version. func (app *BaseApp) AppVersion() uint64 { return app.appVersion } // ConcurrencyWorkers returns the number of concurrent workers for the BaseApp. func (app *BaseApp) ConcurrencyWorkers() int { return app.concurrencyWorkers } // OccEnabled returns whether OCC is enabled for the BaseApp. func (app *BaseApp) OccEnabled() bool { return app.occEnabled } // Version returns the application's version string. func (app *BaseApp) Version() string { return app.version } // Trace returns the boolean value for logging error stack traces. func (app *BaseApp) Trace() bool { return app.trace } // MsgServiceRouter returns the MsgServiceRouter of a BaseApp. func (app *BaseApp) MsgServiceRouter() *MsgServiceRouter { return app.msgServiceRouter } // MountStores mounts all IAVL or DB stores to the provided keys in the BaseApp // multistore. func (app *BaseApp) MountStores(keys ...sdk.StoreKey) { for _, key := range keys { switch key.(type) { case *sdk.KVStoreKey: if !app.fauxMerkleMode { app.MountStore(key, sdk.StoreTypeIAVL) } else { // StoreTypeDB doesn't do anything upon commit, and it doesn't // retain history, but it's useful for faster simulation. app.MountStore(key, sdk.StoreTypeDB) } case *sdk.TransientStoreKey: app.MountStore(key, sdk.StoreTypeTransient) default: panic("Unrecognized store key type " + reflect.TypeOf(key).Name()) } } } // MountKVStores mounts all IAVL or DB stores to the provided keys in the // BaseApp multistore. func (app *BaseApp) MountKVStores(keys map[string]*sdk.KVStoreKey) { for _, key := range keys { if !app.fauxMerkleMode { app.MountStore(key, sdk.StoreTypeIAVL) } else { // StoreTypeDB doesn't do anything upon commit, and it doesn't // retain history, but it's useful for faster simulation. app.MountStore(key, sdk.StoreTypeDB) } } } // MountTransientStores mounts all transient stores to the provided keys in // the BaseApp multistore. func (app *BaseApp) MountTransientStores(keys map[string]*sdk.TransientStoreKey) { for _, key := range keys { app.MountStore(key, sdk.StoreTypeTransient) } } // MountMemoryStores mounts all in-memory KVStores with the BaseApp's internal // commit multi-store. func (app *BaseApp) MountMemoryStores(keys map[string]*sdk.MemoryStoreKey) { for _, memKey := range keys { app.MountStore(memKey, sdk.StoreTypeMemory) } } // MountStore mounts a store to the provided key in the BaseApp multistore, // using the default DB. func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) { app.cms.MountStoreWithDB(key, typ, nil) if app.qms != nil { app.qms.MountStoreWithDB(key, typ, nil) } } // LoadLatestVersion loads the latest application version. It will panic if // called more than once on a running BaseApp. func (app *BaseApp) LoadLatestVersion() error { err := app.storeLoader(app.cms) if err != nil { return fmt.Errorf("failed to load latest version: %w", err) } if app.qms != nil { err = app.storeLoader(app.qms) if err != nil { return fmt.Errorf("failed to load latest version: %w", err) } } return app.init() } // DefaultStoreLoader will be used by default and loads the latest version func DefaultStoreLoader(ms sdk.CommitMultiStore) error { return ms.LoadLatestVersion() } // CommitMultiStore returns the root multi-store. // App constructor can use this to access the `cms`. // UNSAFE: must not be used during the abci life cycle. func (app *BaseApp) CommitMultiStore() sdk.CommitMultiStore { return app.cms } // SnapshotManager returns the snapshot manager. // application use this to register extra extension snapshotters. func (app *BaseApp) SnapshotManager() *snapshots.Manager { return app.snapshotManager } // LoadVersion loads the BaseApp application version. It will panic if called // more than once on a running baseapp. func (app *BaseApp) LoadVersion(version int64) error { err := app.cms.LoadVersion(version) if err != nil { return fmt.Errorf("failed to load version %d: %w", version, err) } return app.init() } // LoadVersionWithoutInit loads the BaseApp application version, it doesn't call app.init any more, // specifically used by export genesis command. func (app *BaseApp) LoadVersionWithoutInit(version int64) error { err := app.cms.LoadVersion(version) app.setCheckState(tmproto.Header{}) return err } // LastCommitID returns the last CommitID of the multistore. func (app *BaseApp) LastCommitID() sdk.CommitID { return app.cms.LastCommitID() } // LastBlockHeight returns the last committed block height. func (app *BaseApp) LastBlockHeight() int64 { return app.cms.LastCommitID().Version } func (app *BaseApp) init() error { if app.sealed { panic("cannot call initFromMainStore: baseapp already sealed") } // needed for the export command which inits from store but never calls initchain app.setCheckState(tmproto.Header{}) app.Seal() return nil } func (app *BaseApp) setMinGasPrices(gasPrices sdk.DecCoins) { app.minGasPrices = gasPrices } func (app *BaseApp) setHaltHeight(haltHeight uint64) { app.haltHeight = haltHeight } func (app *BaseApp) setHaltTime(haltTime uint64) { app.haltTime = haltTime } func (app *BaseApp) setMinRetainBlocks(minRetainBlocks uint64) { app.minRetainBlocks = minRetainBlocks } func (app *BaseApp) setInterBlockCache(cache sdk.MultiStorePersistentCache) { app.interBlockCache = cache } func (app *BaseApp) setCompactionInterval(compactionInterval uint64) { app.compactionInterval = compactionInterval } func (app *BaseApp) setTrace(trace bool) { app.trace = trace } func (app *BaseApp) setIndexEvents(ie []string) { app.IndexEvents = make(map[string]struct{}) for _, e := range ie { app.IndexEvents[e] = struct{}{} } } // Router returns the router of the BaseApp. func (app *BaseApp) Router() sdk.Router { if app.sealed { // We cannot return a Router when the app is sealed because we can't have // any routes modified which would cause unexpected routing behavior. panic("Router() on sealed BaseApp") } return app.router } // QueryRouter returns the QueryRouter of a BaseApp. func (app *BaseApp) QueryRouter() sdk.QueryRouter { return app.queryRouter } // Seal seals a BaseApp. It prohibits any further modifications to a BaseApp. func (app *BaseApp) Seal() { app.sealed = true } // IsSealed returns true if the BaseApp is sealed and false otherwise. func (app *BaseApp) IsSealed() bool { return app.sealed } // setCheckState sets the BaseApp's checkState with a branched multi-store // (i.e. a CacheMultiStore) and a new Context with the same multi-store branch, // provided header, and minimum gas prices set. It is set on InitChain and reset // on Commit. func (app *BaseApp) setCheckState(header tmproto.Header) { ms := app.cms.CacheMultiStore() ctx := sdk.NewContext(ms, header, true).WithMinGasPrices(app.minGasPrices) app.checkTxStateLock.Lock() defer app.checkTxStateLock.Unlock() if app.checkState == nil { app.checkState = &state{ ms: ms, ctx: ctx, mtx: &sync.RWMutex{}, } return } app.checkState.SetMultiStore(ms) app.checkState.SetContext(ctx) } // setDeliverState sets the BaseApp's deliverState with a branched multi-store // (i.e. a CacheMultiStore) and a new Context with the same multi-store branch, // and provided header. It is set on InitChain and BeginBlock and set to nil on // Commit. func (app *BaseApp) setDeliverState(header tmproto.Header) { ms := app.cms.CacheMultiStore() ctx := sdk.NewContext(ms, header, false) if app.deliverState == nil { app.deliverState = &state{ ms: ms, ctx: ctx, mtx: &sync.RWMutex{}, } return } app.deliverState.SetMultiStore(ms) app.deliverState.SetContext(ctx) } func (app *BaseApp) setProcessProposalState(header tmproto.Header) { ms := app.cms.CacheMultiStore() ctx := sdk.NewContext(ms, header, false) if app.processProposalState == nil { app.processProposalState = &state{ ms: ms, ctx: ctx, mtx: &sync.RWMutex{}, } return } app.processProposalState.SetMultiStore(ms) app.processProposalState.SetContext(ctx) } func (app *BaseApp) resetStatesExceptCheckState() { app.processProposalState = nil app.deliverState = nil app.stateToCommit = nil } func (app *BaseApp) setProcessProposalHeader(header tmproto.Header) { app.processProposalState.SetContext(app.processProposalState.Context().WithBlockHeader(header)) } func (app *BaseApp) setDeliverStateHeader(header tmproto.Header) { app.deliverState.SetContext(app.deliverState.Context().WithBlockHeader(header).WithBlockHeight(header.Height)) } // GetProcessProposalCleanContext returns a context snapshotted at the start of // ProcessProposal, before the handler runs. It has the correct store state, // consensus params, and header, but is immune to speculative writes from // optimistic processing. func (app *BaseApp) GetProcessProposalCleanContext() sdk.Context { return app.processProposalCleanCtx } func (app *BaseApp) prepareProcessProposalState(headerHash []byte) { app.processProposalState.SetContext(app.processProposalState.Context(). WithHeaderHash(headerHash). WithConsensusParams(app.GetConsensusParams(app.processProposalState.Context()))) if app.processProposalState.MultiStore().TracingEnabled() { app.processProposalState.SetMultiStore(app.processProposalState.MultiStore().SetTracingContext(nil).(sdk.CacheMultiStore)) } } func (app *BaseApp) prepareDeliverState(headerHash []byte) { app.deliverState.SetContext(app.deliverState.Context(). WithHeaderHash(headerHash). WithConsensusParams(app.GetConsensusParams(app.deliverState.Context()))) } // GetConsensusParams returns the current consensus parameters from the BaseApp's // ParamStore. If the BaseApp has no ParamStore defined, nil is returned. func (app *BaseApp) GetConsensusParams(ctx sdk.Context) *tmproto.ConsensusParams { if app.paramStore == nil { return nil } cp := new(tmproto.ConsensusParams) if app.paramStore.Has(ctx, ParamStoreKeyBlockParams) { var bp tmproto.BlockParams app.paramStore.Get(ctx, ParamStoreKeyBlockParams, &bp) cp.Block = &bp } if app.paramStore.Has(ctx, ParamStoreKeyEvidenceParams) { var ep tmproto.EvidenceParams app.paramStore.Get(ctx, ParamStoreKeyEvidenceParams, &ep) cp.Evidence = &ep } if app.paramStore.Has(ctx, ParamStoreKeyValidatorParams) { var vp tmproto.ValidatorParams app.paramStore.Get(ctx, ParamStoreKeyValidatorParams, &vp) cp.Validator = &vp } if app.paramStore.Has(ctx, ParamStoreKeyVersionParams) { var vp tmproto.VersionParams app.paramStore.Get(ctx, ParamStoreKeyVersionParams, &vp) cp.Version = &vp } if app.paramStore.Has(ctx, ParamStoreKeySynchronyParams) { var vp tmproto.SynchronyParams app.paramStore.Get(ctx, ParamStoreKeySynchronyParams, &vp) cp.Synchrony = &vp } if app.paramStore.Has(ctx, ParamStoreKeyTimeoutParams) { var vp tmproto.TimeoutParams app.paramStore.Get(ctx, ParamStoreKeyTimeoutParams, &vp) cp.Timeout = &vp } if app.paramStore.Has(ctx, ParamStoreKeyABCIParams) { var vp tmproto.ABCIParams app.paramStore.Get(ctx, ParamStoreKeyABCIParams, &vp) cp.Abci = &vp } return cp } func (app *BaseApp) GetConsensusParamsForStateToCommit() *tmproto.ConsensusParams { if app.stateToCommit == nil { return nil } return app.GetConsensusParams(app.stateToCommit.Context()) } // AddRunTxRecoveryHandler adds custom app.runTx method panic handlers. func (app *BaseApp) AddRunTxRecoveryHandler(handlers ...RecoveryHandler) { for _, h := range handlers { app.runTxRecoveryMiddleware = newRecoveryMiddleware(h, app.runTxRecoveryMiddleware) } } // StoreConsensusParams sets the consensus parameters to the baseapp's param store. func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *tmproto.ConsensusParams) { if app.paramStore == nil { panic("cannot store consensus params with no params store set") } if cp == nil { return } app.paramStore.Set(ctx, ParamStoreKeyBlockParams, cp.Block) app.paramStore.Set(ctx, ParamStoreKeyEvidenceParams, cp.Evidence) app.paramStore.Set(ctx, ParamStoreKeyValidatorParams, cp.Validator) app.paramStore.Set(ctx, ParamStoreKeyVersionParams, cp.Version) app.paramStore.Set(ctx, ParamStoreKeySynchronyParams, cp.Synchrony) app.paramStore.Set(ctx, ParamStoreKeyTimeoutParams, cp.Timeout) app.paramStore.Set(ctx, ParamStoreKeyABCIParams, cp.Abci) } func (app *BaseApp) ValidateHeight(height int64) error { if height < 1 { return fmt.Errorf("invalid height: %d", height) } // expectedHeight holds the expected height to validate. var expectedHeight int64 if app.LastBlockHeight() == 0 && app.initialHeight > 1 { // In this case, we're validating the first block of the chain (no // previous commit). The height we're expecting is the initial height. expectedHeight = app.initialHeight } else { // This case can means two things: // - either there was already a previous commit in the store, in which // case we increment the version from there, // - or there was no previous commit, and initial version was not set, // in which case we start at version 1. expectedHeight = app.LastBlockHeight() + 1 } if height != expectedHeight { return fmt.Errorf("invalid height: %d; expected: %d", height, expectedHeight) } return nil } // validateBasicTxMsgs executes basic validator calls for messages. func validateBasicTxMsgs(msgs []sdk.Msg) error { if len(msgs) == 0 { return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "must contain at least one message") } for _, msg := range msgs { err := msg.ValidateBasic() if err != nil { return err } } return nil } // Returns the applications's deliverState if app is in runTxModeDeliver, // otherwise it returns the application's checkstate. func (app *BaseApp) getState(mode runTxMode) *state { if mode == runTxModeDeliver { return app.deliverState } return app.checkState } // retrieve the context for the tx w/ txBytes and other memoized values. func (app *BaseApp) getContextForTx(mode runTxMode, txBytes []byte) sdk.Context { ctx := app.getState(mode).Context(). WithTxBytes(txBytes) ctx = ctx.WithConsensusParams(app.GetConsensusParams(ctx)) if mode == runTxModeReCheck { ctx = ctx.WithIsReCheckTx(true) } if mode == runTxModeSimulate { ctx, _ = ctx.CacheContext() } return ctx } func (app *BaseApp) GetCheckTxContext(txBytes []byte, recheck bool) sdk.Context { mode := runTxModeCheck if recheck { mode = runTxModeReCheck } ctx := app.getState(mode).Context(). WithTxBytes(txBytes) if recheck { ctx = ctx.WithIsReCheckTx(true) } else { ctx = ctx.WithIsCheckTx(true) } return ctx.WithConsensusParams(app.GetConsensusParams(ctx)) } // CacheTxContext returns a new context based off of the provided context with // a branched multi-store. func (app *BaseApp) CacheTxContext(ctx sdk.Context, checksum [32]byte) (sdk.Context, sdk.CacheMultiStore) { ms := ctx.MultiStore() // TODO: https://github.com/cosmos/cosmos-sdk/issues/2824 msCache := ms.CacheMultiStore() if msCache.TracingEnabled() { msCache = msCache.SetTracingContext( sdk.TraceContext( map[string]interface{}{ "txHash": fmt.Sprintf("%X", checksum), }, ), ).(sdk.CacheMultiStore) } return ctx.WithMultiStore(msCache), msCache } type runTxResult struct { gasInfo sdk.GasInfo result *sdk.Result anteEvents []abci.Event ctx sdk.Context } // runTx processes a transaction within a given execution mode, encoded transaction // bytes, and the decoded transaction itself. All state transitions occur through // a cached Context depending on the mode provided. State only gets persisted // if all messages get executed successfully and the execution mode is DeliverTx. // Note, gas execution info is always returned. A reference to a Result is // returned if the tx does not run out of gas and if all the messages are valid // and execute successfully. An error is returned otherwise. func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, tx sdk.Tx, checksum [32]byte) (runTxRes runTxResult, err error) { runTxStart := time.Now() defer func() { baseappMetrics.runTxDuration.Record(ctx.Context(), time.Since(runTxStart).Seconds(), otelmetric.WithAttributes(attribute.String("mode", modeKeyToString[mode]))) // TODO(PLT-353): remove once baseapp_run_tx_duration verified telemetry.MeasureThroughputSinceWithLabels( telemetry.TxCount, []metrics.Label{ telemetry.NewLabel("mode", modeKeyToString[mode]), }, runTxStart, ) }() // check for existing parent tracer, and if applicable, use it spanCtx, span := app.TracingInfo.StartWithContext("RunTx", ctx.TraceSpanContext()) defer span.End() ctx = ctx.WithTraceSpanContext(spanCtx) if mode == runTxModeSimulate { ctx = ctx.WithIsSimulation(true) } span.SetAttributes(attribute.String("txHash", fmt.Sprintf("%X", checksum))) runTxRes.ctx = ctx // NOTE: GasWanted should be returned by the AnteHandler. GasUsed is // determined by the GasMeter. We need access to the context to get the gas // meter so we initialize upfront. var gasWanted uint64 var gasEstimate uint64 ms := ctx.MultiStore() blockGasMeter := ctx.GasMeter() defer func() { if r := recover(); r != nil { recoveryMW := newContextCancelledRecoveryMiddleware(ctx, app.runTxRecoveryMiddleware) recoveryMW = newOutOfGasRecoveryMiddleware(gasWanted, ctx, recoveryMW) recoveryMW = newOCCAbortRecoveryMiddleware(recoveryMW) // TODO: do we have to wrap with occ enabled check? err, runTxRes.result = processRecovery(r, recoveryMW), nil } if ctx.GasMeter() == blockGasMeter { return } runTxRes.gasInfo = sdk.GasInfo{GasWanted: gasWanted, GasUsed: ctx.GasMeter().GasConsumed(), GasEstimate: gasEstimate} }() if tx == nil { return runTxRes, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx decode error") } msgs := tx.GetMsgs() if err := validateBasicTxMsgs(msgs); err != nil { return runTxRes, err } if app.anteHandler != nil { var anteSpan trace.Span // trace AnteHandler _, anteSpan = app.TracingInfo.StartWithContext("AnteHandler", ctx.TraceSpanContext()) defer anteSpan.End() var ( anteCtx sdk.Context msCache sdk.CacheMultiStore ) // Branch context before AnteHandler call in case it aborts. // This is required for both CheckTx and DeliverTx. // Ref: https://github.com/cosmos/cosmos-sdk/issues/2772 // // NOTE: Alternatively, we could require that AnteHandler ensures that // writes do not happen if aborted/failed. This may have some // performance benefits, but it'll be more difficult to get right. anteCtx, msCache = app.CacheTxContext(ctx, checksum) anteCtx = anteCtx.WithEventManager(sdk.NewEventManager()) newCtx, err := app.anteHandler(anteCtx, tx, mode == runTxModeSimulate) if !newCtx.IsZero() { // At this point, newCtx.MultiStore() is a store branch, or something else // replaced by the AnteHandler. We want the original multistore. // // Also, in the case of the tx aborting, we need to track gas consumed via // the instantiated gas meter in the AnteHandler, so we update the context // prior to returning. // // This also replaces the GasMeter in the context where GasUsed was initialized 0 // and updated with gas consumed in the ante handler runs // The GasMeter is a pointer and its passed to the RunMsg and tracks the consumed // gas there too. ctx = newCtx.WithMultiStore(ms) runTxRes.ctx = ctx } defer func() { if newCtx.DeliverTxCallback() != nil { newCtx.DeliverTxCallback()(ctx.WithGasMeter(sdk.NewInfiniteGasMeterWithMultiplier(ctx))) } }() events := ctx.EventManager().Events() if err != nil { return runTxRes, err } // GasMeter expected to be set in AnteHandler gasWanted = ctx.GasMeter().Limit() gasEstimate = ctx.GasEstimate() msCache.Write() runTxRes.anteEvents = events.ToABCIEvents() anteSpan.End() } // Create a new Context based off of the existing Context with a MultiStore branch // in case message processing fails. At this point, the MultiStore // is a branch of a branch. runMsgCtx, msCache := app.CacheTxContext(ctx, checksum) // Attempt to execute all messages and only update state if all messages pass // and we're in DeliverTx. Note, RunMsgs will never return a reference to a // Result if any single message fails or does not have a registered Handler. runTxRes.result, err = app.RunMsgs(runMsgCtx, msgs) if err == nil { msCache.Write() } // we do this since we will only be looking at result in DeliverTx if runTxRes.result != nil && len(runTxRes.anteEvents) > 0 { // append the events in the order of occurrence runTxRes.result.Events = append(runTxRes.anteEvents, runTxRes.result.Events...) } // only apply hooks if no error if err == nil && (!ctx.IsEVM() || runTxRes.result.EvmError == "") { var evmTxInfo *abci.EvmTxInfo if ctx.IsEVM() { evmTxInfo = &abci.EvmTxInfo{ SenderAddress: ctx.EVMSenderAddress().Hex(), Nonce: ctx.EVMNonce(), TxHash: ctx.EVMTxHash().Hex(), VmError: runTxRes.result.EvmError, } } var events = []abci.Event{} if runTxRes.result != nil { events = sdk.MarkEventsToIndex(runTxRes.result.Events, app.IndexEvents) } for _, hook := range app.deliverTxHooks { hook(ctx, tx, checksum, sdk.DeliverTxHookInput{ EvmTxInfo: evmTxInfo, Events: events, }) } } return runTxRes, err } // RunMsgs iterates through a list of messages and executes them with the provided // Context and execution mode. Messages will only be executed during simulation // and DeliverTx. An error is returned if any single message fails or if a // Handler does not exist for a given message route. Otherwise, a reference to a // Result is returned. The caller must not commit state if an error is returned. func (app *BaseApp) RunMsgs(ctx sdk.Context, msgs []sdk.Msg) (*sdk.Result, error) { runMsgsStart := time.Now() defer func() { baseappMetrics.runMsgsDuration.Record(ctx.Context(), time.Since(runMsgsStart).Seconds()) // TODO(PLT-353): remove once baseapp_run_msgs_duration verified telemetry.MeasureThroughputSinceWithLabels( telemetry.MessageCount, []metrics.Label{ telemetry.NewLabel("mode", "deliver"), }, runMsgsStart, ) }() defer func() { if err := recover(); err != nil { fmt.Println(err) panic(err) } }() spanCtx, span := app.TracingInfo.StartWithContext("RunMsgs", ctx.TraceSpanContext()) defer span.End() ctx = ctx.WithTraceSpanContext(spanCtx) msgLogs := make(sdk.ABCIMessageLogs, 0, len(msgs)) events := sdk.EmptyEvents() txMsgData := &sdk.TxMsgData{ Data: make([]*sdk.MsgData, 0, len(msgs)), } var evmError string // NOTE: GasWanted is determined by the AnteHandler and GasUsed by the GasMeter. for i, msg := range msgs { var ( msgResult *sdk.Result eventMsgName string // name to use as value in event `message.action` err error ) msgCtx, msgMsCache := app.CacheTxContext(ctx, [32]byte{}) msgCtx = msgCtx.WithMessageIndex(i) startTime := time.Now() if handler := app.msgServiceRouter.Handler(msg); handler != nil { // ADR 031 request type routing msgResult, err = handler(msgCtx, msg) eventMsgName = sdk.MsgTypeURL(msg) baseappMetrics.runMsgLatency.Record(ctx.Context(), time.Since(startTime).Seconds(), otelmetric.WithAttributes(attribute.String("type", eventMsgName))) // TODO(PLT-353): remove once baseapp_run_msg_latency verified metrics.MeasureSinceWithLabels( []string{"sei", "cosmos", "run", "msg", "latency"}, startTime, []metrics.Label{{Name: "type", Value: eventMsgName}}, ) } else if legacyMsg, ok := msg.(legacytx.LegacyMsg); ok { // legacy sdk.Msg routing // Assuming that the app developer has migrated all their Msgs to // proto messages and has registered all `Msg services`, then this // path should never be called, because all those Msgs should be // registered within the `msgServiceRouter` already. msgRoute := legacyMsg.Route() eventMsgName = legacyMsg.Type() handler := app.router.Route(msgCtx, msgRoute) if handler == nil { return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized message route: %s; message index: %d", msgRoute, i) } msgResult, err = handler(msgCtx, msg) baseappMetrics.runMsgLatency.Record(ctx.Context(), time.Since(startTime).Seconds(), otelmetric.WithAttributes(attribute.String("type", eventMsgName))) // TODO(PLT-353): remove once baseapp_run_msg_latency verified metrics.MeasureSinceWithLabels( []string{"cosmos", "run", "msg", "latency"}, startTime, []metrics.Label{{Name: "type", Value: eventMsgName}}, ) } else { return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "can't route message %+v", msg) } if err != nil { return nil, sdkerrors.Wrapf(err, "failed to execute message; message index: %d", i) } msgEvents := sdk.Events{ sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyAction, eventMsgName)), } msgEvents = msgEvents.AppendEvents(msgResult.GetEvents()) // append message events, data and logs // // Note: Each message result's data must be length-prefixed in order to // separate each result. events = events.AppendEvents(msgEvents) txMsgData.Data = append(txMsgData.Data, &sdk.MsgData{MsgType: sdk.MsgTypeURL(msg), Data: msgResult.Data}) msgLogs = append(msgLogs, sdk.NewABCIMessageLog(uint32(i), msgResult.Log, msgEvents)) //nolint:gosec // loop range index msgMsCache.Write() if msgResult.EvmError != "" { evmError = msgResult.EvmError } } data, err := proto.Marshal(txMsgData) if err != nil { return nil, sdkerrors.Wrap(err, "failed to marshal tx data") } return &sdk.Result{ Data: data, Log: strings.TrimSpace(msgLogs.String()), Events: events.ToABCIEvents(), EvmError: evmError, }, nil } func (app *BaseApp) startCompactionRoutine(db dbm.DB) { if app.compactionInterval == 0 { return } go func() { if goleveldb, ok := db.(*dbm.GoLevelDB); ok { for { time.Sleep(time.Duration(app.compactionInterval) * time.Second) //nolint:gosec // compactionInterval is a small config value if err := goleveldb.DB().CompactRange(leveldbutils.Range{Start: nil, Limit: nil}); err != nil { logger.Error("Failed to compact DB", "err", err) } } } else { logger.Info("Exit compaction routine because underlying DB does not support compaction") } }() } func (app *BaseApp) Close() error { // we do not want to close when a commit is ongoing since commit writes to stores // and metadata in a non-atomic way app.commitLock.Lock() defer app.commitLock.Unlock() if app.db != nil { if err := app.db.Close(); err != nil { return err } } if err := app.cms.Close(); err != nil { return err } if app.snapshotManager != nil { if err := app.snapshotManager.Close(); err != nil { return err } } if app.closeHandler == nil { return nil } return app.closeHandler() } func (app *BaseApp) GetCheckCtx() sdk.Context { app.checkTxStateLock.RLock() defer app.checkTxStateLock.RUnlock() return app.checkState.ctx } func (app *BaseApp) RegisterDeliverTxHook(hook DeliverTxHook) { app.deliverTxHooks = append(app.deliverTxHooks, hook) } func (app *BaseApp) InplaceTestnetInitialize(pk cryptotypes.PubKey) { _ = app.inplaceTestnetInitializer(pk) }