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

Rebase evm-temp to main #392

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 19 additions & 7 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
// internal CheckTx state if the AnteHandler passes. Otherwise, the ResponseCheckTx
// will contain releveant error information. Regardless of tx execution outcome,
// the ResponseCheckTx will contain relevant gas execution context.
func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abci.ResponseCheckTx, error) {
func (app *BaseApp) CheckTx(ctx context.Context, req *abci.RequestCheckTx) (*abci.ResponseCheckTxV2, error) {
defer telemetry.MeasureSince(time.Now(), "abci", "check_tx")

var mode runTxMode
Expand All @@ -221,25 +221,37 @@
}

sdkCtx := app.getContextForTx(mode, req.Tx)
gInfo, result, _, priority, err := app.runTx(sdkCtx, mode, req.Tx)
tx, err := app.txDecoder(req.Tx)
if err != nil {
res := sdkerrors.ResponseCheckTx(err, 0, 0, app.trace)
return &abci.ResponseCheckTxV2{ResponseCheckTx: &res}, err
}

Check warning on line 228 in baseapp/abci.go

View check run for this annotation

Codecov / codecov/patch

baseapp/abci.go#L226-L228

Added lines #L226 - L228 were not covered by tests
gInfo, result, _, priority, pendingTxChecker, expireTxHandler, err := app.runTx(sdkCtx, mode, tx, sha256.Sum256(req.Tx))
if err != nil {
res := sdkerrors.ResponseCheckTx(err, gInfo.GasWanted, gInfo.GasUsed, app.trace)
return &res, err
return &abci.ResponseCheckTxV2{ResponseCheckTx: &res}, err

Check warning on line 232 in baseapp/abci.go

View check run for this annotation

Codecov / codecov/patch

baseapp/abci.go#L232

Added line #L232 was not covered by tests
}

return &abci.ResponseCheckTx{
res := &abci.ResponseCheckTxV2{ResponseCheckTx: &abci.ResponseCheckTx{
GasWanted: int64(gInfo.GasWanted), // TODO: Should type accept unsigned ints?
Data: result.Data,
Priority: priority,
}, nil
}}
if pendingTxChecker != nil {
res.IsPendingTransaction = true
res.Checker = pendingTxChecker
}

Check warning on line 243 in baseapp/abci.go

View check run for this annotation

Codecov / codecov/patch

baseapp/abci.go#L241-L243

Added lines #L241 - L243 were not covered by tests
res.ExpireTxHandler = expireTxHandler

return res, nil
}

// DeliverTx implements the ABCI interface and executes a tx in DeliverTx mode.
// State only gets persisted if all messages are valid and get executed successfully.
// Otherwise, the ResponseDeliverTx will contain releveant error information.
// Regardless of tx execution outcome, the ResponseDeliverTx will contain relevant
// gas execution context.
func (app *BaseApp) DeliverTx(ctx sdk.Context, req abci.RequestDeliverTx) (res abci.ResponseDeliverTx) {
func (app *BaseApp) DeliverTx(ctx sdk.Context, req abci.RequestDeliverTx, tx sdk.Tx, checksum [32]byte) (res abci.ResponseDeliverTx) {
defer telemetry.MeasureSince(time.Now(), "abci", "deliver_tx")
defer func() {
for _, streamingListener := range app.abciListeners {
Expand All @@ -259,7 +271,7 @@
telemetry.SetGauge(float32(gInfo.GasWanted), "tx", "gas", "wanted")
}()

gInfo, result, anteEvents, _, err := app.runTx(ctx.WithTxBytes(req.Tx).WithVoteInfos(app.voteInfos), runTxModeDeliver, req.Tx)
gInfo, result, anteEvents, _, _, _, err := app.runTx(ctx.WithTxBytes(req.Tx).WithVoteInfos(app.voteInfos), runTxModeDeliver, tx, checksum)
if err != nil {
resultStr = "failed"
// if we have a result, use those events instead of just the anteEvents
Expand Down
56 changes: 36 additions & 20 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import (
"context"
"crypto/sha256"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -155,8 +154,9 @@

ChainID string

votesInfoLock sync.RWMutex
commitLock *sync.Mutex
votesInfoLock sync.RWMutex
commitLock *sync.Mutex
checkTxStateLock *sync.RWMutex

compactionInterval uint64

Expand Down Expand Up @@ -266,7 +266,8 @@
TracingInfo: &tracing.Info{
Tracer: &tr,
},
commitLock: &sync.Mutex{},
commitLock: &sync.Mutex{},
checkTxStateLock: &sync.RWMutex{},
}

app.TracingInfo.SetContext(context.Background())
Expand Down Expand Up @@ -501,6 +502,8 @@
func (app *BaseApp) setCheckState(header tmproto.Header) {
ms := app.cms.CacheMultiStore()
ctx := sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices)
app.checkTxStateLock.Lock()
defer app.checkTxStateLock.Unlock()
if app.checkState == nil {
app.checkState = &state{
ms: ms,
Expand Down Expand Up @@ -795,15 +798,15 @@

// 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) {
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", sha256.Sum256(txBytes)),
"txHash": fmt.Sprintf("%X", checksum),

Check warning on line 809 in baseapp/baseapp.go

View check run for this annotation

Codecov / codecov/patch

baseapp/baseapp.go#L809

Added line #L809 was not covered by tests
},
),
).(sdk.CacheMultiStore)
Expand All @@ -819,8 +822,15 @@
// 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, txBytes []byte) (gInfo sdk.GasInfo, result *sdk.Result, anteEvents []abci.Event, priority int64, err error) {

func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, tx sdk.Tx, checksum [32]byte) (
gInfo sdk.GasInfo,
result *sdk.Result,
anteEvents []abci.Event,
priority int64,
pendingTxChecker abci.PendingTxChecker,
expireHandler abci.ExpireTxHandler,
err error,
) {
defer telemetry.MeasureThroughputSinceWithLabels(
telemetry.TxCount,
[]metrics.Label{
Expand All @@ -843,7 +853,7 @@
spanCtx, span := app.TracingInfo.StartWithContext("RunTx", ctx.TraceSpanContext())
defer span.End()
ctx = ctx.WithTraceSpanContext(spanCtx)
span.SetAttributes(attribute.String("txHash", fmt.Sprintf("%X", sha256.Sum256(txBytes))))
span.SetAttributes(attribute.String("txHash", fmt.Sprintf("%X", checksum)))

// NOTE: GasWanted should be returned by the AnteHandler. GasUsed is
// determined by the GasMeter. We need access to the context to get the gas
Expand All @@ -854,7 +864,7 @@

// only run the tx if there is block gas remaining
if mode == runTxModeDeliver && ctx.BlockGasMeter().IsOutOfGas() {
return gInfo, nil, nil, -1, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx")
return gInfo, nil, nil, -1, nil, nil, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx")
}

defer func() {
Expand Down Expand Up @@ -890,15 +900,14 @@
defer consumeBlockGas()
}

tx, err := app.txDecoder(txBytes)
if err != nil {
return sdk.GasInfo{}, nil, nil, 0, err
if tx == nil {
return sdk.GasInfo{}, nil, nil, 0, nil, nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx decode error")

Check warning on line 904 in baseapp/baseapp.go

View check run for this annotation

Codecov / codecov/patch

baseapp/baseapp.go#L904

Added line #L904 was not covered by tests
}

msgs := tx.GetMsgs()

if err := validateBasicTxMsgs(msgs); err != nil {
return sdk.GasInfo{}, nil, nil, 0, err
return sdk.GasInfo{}, nil, nil, 0, nil, nil, err
}

if app.anteHandler != nil {
Expand All @@ -916,7 +925,7 @@
// 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, txBytes)
anteCtx, msCache = app.cacheTxContext(ctx, checksum)
anteCtx = anteCtx.WithEventManager(sdk.NewEventManager())
newCtx, err := app.anteHandler(anteCtx, tx, mode == runTxModeSimulate)

Expand All @@ -940,7 +949,7 @@
// GasMeter expected to be set in AnteHandler
gasWanted = ctx.GasMeter().Limit()
if err != nil {
return gInfo, nil, nil, 0, err
return gInfo, nil, nil, 0, nil, nil, err
}

// Dont need to validate in checkTx mode
Expand All @@ -955,11 +964,13 @@
op.EmitValidationFailMetrics()
}
errMessage := fmt.Sprintf("Invalid Concurrent Execution antehandler missing %d access operations", len(missingAccessOps))
return gInfo, nil, nil, 0, sdkerrors.Wrap(sdkerrors.ErrInvalidConcurrencyExecution, errMessage)
return gInfo, nil, nil, 0, nil, nil, sdkerrors.Wrap(sdkerrors.ErrInvalidConcurrencyExecution, errMessage)

Check warning on line 967 in baseapp/baseapp.go

View check run for this annotation

Codecov / codecov/patch

baseapp/baseapp.go#L967

Added line #L967 was not covered by tests
}
}

priority = ctx.Priority()
pendingTxChecker = ctx.PendingTxChecker()
expireHandler = ctx.ExpireTxHandler()
msCache.Write()
anteEvents = events.ToABCIEvents()
anteSpan.End()
Expand All @@ -968,7 +979,7 @@
// 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, txBytes)
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
Expand All @@ -986,7 +997,10 @@
// append the events in the order of occurrence
result.Events = append(anteEvents, result.Events...)
}
return gInfo, result, anteEvents, priority, err
if ctx.CheckTxCallback() != nil {
ctx.CheckTxCallback()(err)
}

Check warning on line 1002 in baseapp/baseapp.go

View check run for this annotation

Codecov / codecov/patch

baseapp/baseapp.go#L1001-L1002

Added lines #L1001 - L1002 were not covered by tests
return gInfo, result, anteEvents, priority, pendingTxChecker, expireHandler, err
}

// runMsgs iterates through a list of messages and executes them with the provided
Expand Down Expand Up @@ -1031,7 +1045,7 @@
err error
)

msgCtx, msgMsCache := app.cacheTxContext(ctx, []byte{})
msgCtx, msgMsCache := app.cacheTxContext(ctx, [32]byte{})
msgCtx = msgCtx.WithMessageIndex(i)

startTime := time.Now()
Expand Down Expand Up @@ -1172,5 +1186,7 @@
}

func (app *BaseApp) GetCheckCtx() sdk.Context {
app.checkTxStateLock.RLock()
defer app.checkTxStateLock.RUnlock()

Check warning on line 1190 in baseapp/baseapp.go

View check run for this annotation

Codecov / codecov/patch

baseapp/baseapp.go#L1189-L1190

Added lines #L1189 - L1190 were not covered by tests
return app.checkState.ctx
}
35 changes: 21 additions & 14 deletions baseapp/deliver_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package baseapp
import (
"bytes"
"context"
"crypto/sha256"
"encoding/binary"
"fmt"
"math/rand"
Expand Down Expand Up @@ -229,7 +230,8 @@ func TestWithRouter(t *testing.T) {
txBytes, err := codec.Marshal(tx)
require.NoError(t, err)

res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
decoded, _ := app.txDecoder(txBytes)
res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}, decoded, sha256.Sum256(txBytes))
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
}

Expand Down Expand Up @@ -439,7 +441,8 @@ func TestMultiMsgDeliverTx(t *testing.T) {
tx := newTxCounter(0, 0, 1, 2)
txBytes, err := codec.Marshal(tx)
require.NoError(t, err)
res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
decoded, _ := app.txDecoder(txBytes)
res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}, decoded, sha256.Sum256(txBytes))
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))

store := app.deliverState.ctx.KVStore(capKey1)
Expand All @@ -459,7 +462,8 @@ func TestMultiMsgDeliverTx(t *testing.T) {
tx.Msgs = append(tx.Msgs, msgCounter2{1})
txBytes, err = codec.Marshal(tx)
require.NoError(t, err)
res = app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
decoded, _ = app.txDecoder(txBytes)
res = app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}, decoded, sha256.Sum256(txBytes))
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))

store = app.deliverState.ctx.KVStore(capKey1)
Expand Down Expand Up @@ -658,9 +662,8 @@ func TestRunInvalidTransaction(t *testing.T) {
txBytes, err := newCdc.Marshal(tx)
require.NoError(t, err)

res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
require.EqualValues(t, sdkerrors.ErrTxDecode.ABCICode(), res.Code)
require.EqualValues(t, sdkerrors.ErrTxDecode.Codespace(), res.Codespace)
_, err = app.txDecoder(txBytes)
require.NotNil(t, err)
}
}

Expand Down Expand Up @@ -932,7 +935,8 @@ func TestBaseAppAnteHandler(t *testing.T) {
tx.setFailOnAnte(true)
txBytes, err := cdc.Marshal(tx)
require.NoError(t, err)
res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
decoded, _ := app.txDecoder(txBytes)
res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}, decoded, sha256.Sum256(txBytes))
require.Empty(t, res.Events)
require.False(t, res.IsOK(), fmt.Sprintf("%v", res))

Expand All @@ -948,7 +952,8 @@ func TestBaseAppAnteHandler(t *testing.T) {
txBytes, err = cdc.Marshal(tx)
require.NoError(t, err)

res = app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
decoded, _ = app.txDecoder(txBytes)
res = app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}, decoded, sha256.Sum256(txBytes))
// should emit ante event
require.NotEmpty(t, res.Events)
require.False(t, res.IsOK(), fmt.Sprintf("%v", res))
Expand All @@ -965,7 +970,8 @@ func TestBaseAppAnteHandler(t *testing.T) {
txBytes, err = cdc.Marshal(tx)
require.NoError(t, err)

res = app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
decoded, _ = app.txDecoder(txBytes)
res = app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}, decoded, sha256.Sum256(txBytes))
require.NotEmpty(t, res.Events)
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))

Expand Down Expand Up @@ -1043,15 +1049,15 @@ func TestGasConsumptionBadTx(t *testing.T) {
txBytes, err := cdc.Marshal(tx)
require.NoError(t, err)

res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}, tx, sha256.Sum256(txBytes))
require.False(t, res.IsOK(), fmt.Sprintf("%v", res))

// require next tx to fail due to black gas limit
tx = newTxCounter(5, 0)
txBytes, err = cdc.Marshal(tx)
require.NoError(t, err)

res = app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
res = app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}, tx, sha256.Sum256(txBytes))
require.False(t, res.IsOK(), fmt.Sprintf("%v", res))
}

Expand Down Expand Up @@ -1520,7 +1526,8 @@ func TestDeliverTx(t *testing.T) {
txBytes, err := codec.Marshal(tx)
require.NoError(t, err)

res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
decoded, _ := app.txDecoder(txBytes)
res := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}, decoded, sha256.Sum256(txBytes))
require.True(t, res.IsOK(), fmt.Sprintf("%v", res))
events := res.GetEvents()
require.Len(t, events, 3, "should contain ante handler, message type and counter events respectively")
Expand Down Expand Up @@ -1724,12 +1731,12 @@ func setupBaseAppWithSnapshots(t *testing.T, blocks uint, blockTxs int, options
value := make([]byte, 10000)
_, err := r.Read(value)
require.NoError(t, err)
tx.Msgs = append(tx.Msgs, msgKeyValue{Key: key, Value: value})
tx.Msgs = append(tx.Msgs, &msgKeyValue{Key: key, Value: value})
keyCounter++
}
txBytes, err := codec.Marshal(tx)
require.NoError(t, err)
resp := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes})
resp := app.DeliverTx(app.deliverState.ctx, abci.RequestDeliverTx{Tx: txBytes}, tx, sha256.Sum256(txBytes))
require.True(t, resp.IsOK(), "%v", resp.String())
}
app.EndBlock(app.deliverState.ctx, abci.RequestEndBlock{Height: height})
Expand Down
Loading
Loading