Skip to content

Commit

Permalink
feat(baseapp): add txHash and message index context (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman authored Mar 20, 2023
1 parent a64b83f commit 2109765
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package baseapp

import (
"context"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -29,6 +30,11 @@ const (
runTxModeDeliver // Deliver a transaction
)

const (
TxHashContextKey = sdk.ContextKey("tx-hash")
TxMsgIdxContextKey = sdk.ContextKey("tx-msg-idx")
)

var _ abci.Application = (*BaseApp)(nil)

type (
Expand Down Expand Up @@ -591,14 +597,19 @@ func (app *BaseApp) getContextForTx(mode runTxMode, txBytes []byte) sdk.Context
// cacheTxContext returns a new context based off of the provided context with
// a branched multi-store.
func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context, sdk.CacheMultiStore) {
txHash, ok := ctx.Context().Value(TxHashContextKey).(string)
if !ok {
txHash = fmt.Sprintf("%X", tmhash.Sum(txBytes))
ctx = ctx.WithContext(context.WithValue(ctx.Context(), TxHashContextKey, txHash))
}
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", tmhash.Sum(txBytes)),
"txHash": txHash,
},
),
).(sdk.CacheMultiStore)
Expand Down Expand Up @@ -760,6 +771,7 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s
)

msgCtx := ctx.WithEventManager(sdk.NewEventManagerWithHistory(historicalEvents))
msgCtx = msgCtx.WithContext(context.WithValue(msgCtx.Context(), TxMsgIdxContextKey, i))
if handler := app.msgServiceRouter.Handler(msg); handler != nil {
// ADR 031 request type routing
msgResult, err = handler(msgCtx, msg)
Expand Down

0 comments on commit 2109765

Please sign in to comment.