Skip to content

Commit

Permalink
feat: export baseapp.DeliverTx
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey committed Mar 9, 2024
1 parent 9abd946 commit cfc1dbb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
48 changes: 24 additions & 24 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
// done after the deliver state and context have been set as it's persisted
// to state.
if req.ConsensusParams != nil {
app.StoreConsensusParams(app.deliverState.ctx, req.ConsensusParams)
app.StoreConsensusParams(app.DeliverState.ctx, req.ConsensusParams)
}

if app.initChainer == nil {
return
}

// add block gas meter for any genesis transactions (allow infinite gas)
app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())
app.DeliverState.ctx = app.DeliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())

res = app.initChainer(app.deliverState.ctx, req)
res = app.initChainer(app.DeliverState.ctx, req)

// sanity check
if len(req.Validators) > 0 {
Expand Down Expand Up @@ -96,7 +96,7 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
}

// NOTE: We don't commit, but BeginBlock for block `initial_height` starts from this
// deliverState.
// DeliverState.
return abci.ResponseInitChain{
ConsensusParams: res.ConsensusParams,
Validators: res.Validators,
Expand Down Expand Up @@ -138,32 +138,32 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
}

// Initialize the DeliverTx state. If this is the first block, it should
// already be initialized in InitChain. Otherwise app.deliverState will be
// already be initialized in InitChain. Otherwise app.DeliverState will be
// nil, since it is reset on Commit.
if app.deliverState == nil {
if app.DeliverState == nil {
app.setDeliverState(req.Header)
} else {
// In the first block, app.deliverState.ctx will already be initialized
// In the first block, app.DeliverState.ctx will already be initialized
// by InitChain. Context is now updated with Header information.
app.deliverState.ctx = app.deliverState.ctx.
app.DeliverState.ctx = app.DeliverState.ctx.
WithBlockHeader(req.Header).
WithBlockHeight(req.Header.Height)
}

// add block gas meter
var gasMeter sdk.GasMeter
if maxGas := app.getMaximumBlockGas(app.deliverState.ctx); maxGas > 0 {
if maxGas := app.getMaximumBlockGas(app.DeliverState.ctx); maxGas > 0 {
gasMeter = sdk.NewGasMeter(maxGas)
} else {
gasMeter = sdk.NewInfiniteGasMeter()
}

// NOTE: header hash is not set in NewContext, so we manually set it here

app.deliverState.ctx = app.deliverState.ctx.
app.DeliverState.ctx = app.DeliverState.ctx.
WithBlockGasMeter(gasMeter).
WithHeaderHash(req.Hash).
WithConsensusParams(app.GetConsensusParams(app.deliverState.ctx))
WithConsensusParams(app.GetConsensusParams(app.DeliverState.ctx))

// we also set block gas meter to checkState in case the application needs to
// verify gas consumption during (Re)CheckTx
Expand All @@ -174,15 +174,15 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
}

if app.beginBlocker != nil {
res = app.beginBlocker(app.deliverState.ctx, req)
res = app.beginBlocker(app.DeliverState.ctx, req)
res.Events = sdk.MarkEventsToIndex(res.Events, app.indexEvents)
}
// set the signed validators for addition to context in deliverTx
app.voteInfos = req.LastCommitInfo.GetVotes()

// call the hooks with the BeginBlock messages
for _, streamingListener := range app.abciListeners {
goCtx := sdk.WrapSDKContext(app.deliverState.ctx)
goCtx := sdk.WrapSDKContext(app.DeliverState.ctx)
if err := streamingListener.ListenBeginBlock(goCtx, req, res); err != nil {
panic(fmt.Errorf("BeginBlock listening hook failed, height: %d, err: %w", req.Header.Height, err))
}
Expand All @@ -195,22 +195,22 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
defer telemetry.MeasureSince(time.Now(), "abci", "end_block")

if app.deliverState.ms.TracingEnabled() {
app.deliverState.ms = app.deliverState.ms.SetTracingContext(nil).(sdk.CacheMultiStore)
if app.DeliverState.ms.TracingEnabled() {
app.DeliverState.ms = app.DeliverState.ms.SetTracingContext(nil).(sdk.CacheMultiStore)
}

if app.endBlocker != nil {
res = app.endBlocker(app.deliverState.ctx, req)
res = app.endBlocker(app.DeliverState.ctx, req)
res.Events = sdk.MarkEventsToIndex(res.Events, app.indexEvents)
}

if cp := app.GetConsensusParams(app.deliverState.ctx); cp != nil {
if cp := app.GetConsensusParams(app.DeliverState.ctx); cp != nil {
res.ConsensusParamUpdates = cp
}

// call the streaming service hooks with the EndBlock messages
for _, streamingListener := range app.abciListeners {
goCtx := sdk.WrapSDKContext(app.deliverState.ctx)
goCtx := sdk.WrapSDKContext(app.DeliverState.ctx)
if err := streamingListener.ListenEndBlock(goCtx, req, res); err != nil {
panic(fmt.Errorf("EndBlock listening hook failed, height: %d, err: %w", req.Height, err))
}
Expand Down Expand Up @@ -265,7 +265,7 @@ func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) (res abci.ResponseDeliv

defer func() {
for _, streamingListener := range app.abciListeners {
goCtx := sdk.WrapSDKContext(app.deliverState.ctx)
goCtx := sdk.WrapSDKContext(app.DeliverState.ctx)
if err := streamingListener.ListenDeliverTx(goCtx, req, res); err != nil {
panic(fmt.Errorf("DeliverTx listening hook failed: %w", err))
}
Expand Down Expand Up @@ -307,13 +307,13 @@ func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) (res abci.ResponseDeliv
func (app *BaseApp) Commit() abci.ResponseCommit {
defer telemetry.MeasureSince(time.Now(), "abci", "commit")

header := app.deliverState.ctx.BlockHeader()
header := app.DeliverState.ctx.BlockHeader()
retainHeight := app.GetBlockRetentionHeight(header.Height)

// Write the DeliverTx state into branched storage and commit the MultiStore.
// The write to the DeliverTx state writes all state transitions to the root
// MultiStore (app.cms) so when Commit() is called is persists those values.
app.deliverState.ms.Write()
app.DeliverState.ms.Write()
commitID := app.cms.Commit()

res := abci.ResponseCommit{
Expand All @@ -323,7 +323,7 @@ func (app *BaseApp) Commit() abci.ResponseCommit {

// call the hooks with the Commit message
for _, streamingListener := range app.abciListeners {
goCtx := sdk.WrapSDKContext(app.deliverState.ctx)
goCtx := sdk.WrapSDKContext(app.DeliverState.ctx)
if err := streamingListener.ListenCommit(goCtx, res); err != nil {
panic(fmt.Errorf("Commit listening hook failed, height: %d, err: %w", header.Height, err))
}
Expand All @@ -338,7 +338,7 @@ func (app *BaseApp) Commit() abci.ResponseCommit {
app.setCheckState(header)

// empty/reset the deliver state
app.deliverState = nil
app.DeliverState = nil

var halt bool

Expand Down Expand Up @@ -735,7 +735,7 @@ func (app *BaseApp) GetBlockRetentionHeight(commitHeight int64) int64 {
// evidence parameters instead of computing an estimated nubmer of blocks based
// on the unbonding period and block commitment time as the two should be
// equivalent.
cp := app.GetConsensusParams(app.deliverState.ctx)
cp := app.GetConsensusParams(app.DeliverState.ctx)
if cp != nil && cp.Evidence != nil && cp.Evidence.MaxAgeNumBlocks > 0 {
retentionHeight = commitHeight - cp.Evidence.MaxAgeNumBlocks
}
Expand Down
14 changes: 7 additions & 7 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ type BaseApp struct { //nolint: maligned
// volatile states:
//
// checkState is set on InitChain and reset on Commit
// deliverState is set on InitChain and BeginBlock and set to nil on Commit
// DeliverState is set on InitChain and BeginBlock and set to nil on Commit
checkState *state // for CheckTx
deliverState *state // for DeliverTx
DeliverState *state // for DeliverTx

// paramStore is used to query for ABCI consensus parameters from an
// application parameter store.
Expand Down Expand Up @@ -118,7 +118,7 @@ type appStore struct {
cms sdk.CommitMultiStore // Main (uncached) state
storeLoader StoreLoader // function to handle store loading, may be overridden with SetStoreLoader()

// an inter-block write-through cache provided to the context during deliverState
// 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.
Expand Down Expand Up @@ -424,13 +424,13 @@ func (app *BaseApp) setCheckState(header tmproto.Header) {
}
}

// setDeliverState sets the BaseApp's deliverState with a branched multi-store
// 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()
app.deliverState = &state{
app.DeliverState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, false, app.logger),
}
Expand Down Expand Up @@ -557,11 +557,11 @@ func validateBasicTxMsgs(msgs []sdk.Msg) error {
return nil
}

// Returns the applications's deliverState if app is in runTxModeDeliver,
// 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.DeliverState
}

return app.checkState
Expand Down
8 changes: 4 additions & 4 deletions baseapp/deliver_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestMultiMsgDeliverTx(t *testing.T) {
res := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))

store := app.deliverState.ctx.KVStore(capKey1)
store := app.DeliverState.ctx.KVStore(capKey1)

// tx counter only incremented once
txCounter := getIntFromStore(store, anteKey)
Expand All @@ -444,7 +444,7 @@ func TestMultiMsgDeliverTx(t *testing.T) {
res = app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))

store = app.deliverState.ctx.KVStore(capKey1)
store = app.DeliverState.ctx.KVStore(capKey1)

// tx counter only incremented once
txCounter = getIntFromStore(store, anteKey)
Expand Down Expand Up @@ -1066,8 +1066,8 @@ func TestInitChainer(t *testing.T) {
)

// assert that chainID is set correctly in InitChain
chainID := app.deliverState.ctx.ChainID()
require.Equal(t, "test-chain-id", chainID, "ChainID in deliverState not set correctly in InitChain")
chainID := app.DeliverState.ctx.ChainID()
require.Equal(t, "test-chain-id", chainID, "ChainID in DeliverState not set correctly in InitChain")

chainID = app.checkState.ctx.ChainID()
require.Equal(t, "test-chain-id", chainID, "ChainID in checkState not set correctly in InitChain")
Expand Down
2 changes: 1 addition & 1 deletion baseapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (app *BaseApp) NewContext(isCheckTx bool, header tmproto.Header) sdk.Contex
WithMinGasPrices(app.minGasPrices)
}

return sdk.NewContext(app.deliverState.ms, header, false, app.logger)
return sdk.NewContext(app.DeliverState.ms, header, false, app.logger)
}

func (app *BaseApp) NewUncachedContext(isCheckTx bool, header tmproto.Header) sdk.Context {
Expand Down

0 comments on commit cfc1dbb

Please sign in to comment.