Skip to content

Commit

Permalink
handle error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed May 20, 2024
1 parent 9151f0d commit 4ec6c09
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 209 deletions.
1 change: 0 additions & 1 deletion app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func NewAnteHandlerAndDepGenerator(options HandlerOptions) (sdk.AnteHandler, sdk
sdk.DefaultWrappedAnteDecorator(evmante.NewGasLimitDecorator(options.EVMKeeper)),
}
evmAnteHandler, evmAnteDepGenerator := sdk.ChainAnteDecorators(evmAnteDecorators...)
evmAnteHandler = evmante.NewAnteErrorHandler(evmAnteHandler, options.EVMKeeper).Handle

router := evmante.NewEVMRouterDecorator(anteHandler, evmAnteHandler, anteDepGenerator, evmAnteDepGenerator)

Expand Down
2 changes: 2 additions & 0 deletions app/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ func TestEvmAnteErrorHandler(t *testing.T) {
require.NotEqual(t, 0, res.Code)
testkeeper.EVMTestApp.EvmKeeper.SetTxResults([]*abci.ExecTxResult{{
Code: res.Code,
Log: "nonce too high",
}})
testkeeper.EVMTestApp.EvmKeeper.SetMsgs([]*evmtypes.MsgEVMTransaction{req})
deferredInfo := testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx)
require.Equal(t, 1, len(deferredInfo))
require.Contains(t, deferredInfo[0].Error, "nonce too high")
Expand Down
12 changes: 12 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,7 @@ func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req BlockProcessRequ
beginBlockResp := app.BeginBlock(ctx, beginBlockReq)
events = append(events, beginBlockResp.Events...)

evmTxs := make([]*evmtypes.MsgEVMTransaction, len(txs)) // nil for non-EVM txs
txResults := make([]*abci.ExecTxResult, len(txs))
typedTxs := app.DecodeTransactionsConcurrently(ctx, txs)

Expand All @@ -1538,6 +1539,11 @@ func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req BlockProcessRequ
prioritizedResults, ctx := app.ExecuteTxsConcurrently(ctx, prioritizedTxs, prioritizedTypedTxs, prioritizedIndices)
for relativePrioritizedIndex, originalIndex := range prioritizedIndices {
txResults[originalIndex] = prioritizedResults[relativePrioritizedIndex]
if emsg := evmtypes.GetEVMTransactionMessage(prioritizedTypedTxs[relativePrioritizedIndex]); emsg != nil {
evmTxs[originalIndex] = emsg
} else {
evmTxs[originalIndex] = nil
}
}

// Finalize all Bank Module Transfers here so that events are included for prioritiezd txs
Expand All @@ -1550,8 +1556,14 @@ func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req BlockProcessRequ
otherResults, ctx := app.ExecuteTxsConcurrently(ctx, otherTxs, otherTypedTxs, otherIndices)
for relativeOtherIndex, originalIndex := range otherIndices {
txResults[originalIndex] = otherResults[relativeOtherIndex]
if emsg := evmtypes.GetEVMTransactionMessage(otherTypedTxs[relativeOtherIndex]); emsg != nil {
evmTxs[originalIndex] = emsg
} else {
evmTxs[originalIndex] = nil
}
}
app.EvmKeeper.SetTxResults(txResults)
app.EvmKeeper.SetMsgs(evmTxs)

// Finalize all Bank Module Transfers here so that events are included
lazyWriteEvents := app.BankKeeper.WriteDeferredBalances(ctx)
Expand Down
40 changes: 0 additions & 40 deletions x/evm/ante/error.go

This file was deleted.

124 changes: 0 additions & 124 deletions x/evm/ante/error_test.go

This file was deleted.

58 changes: 26 additions & 32 deletions x/evm/keeper/deferred.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,34 @@ import (

func (k *Keeper) GetEVMTxDeferredInfo(ctx sdk.Context) (res []*types.DeferredInfo) {
store := prefix.NewStore(ctx.TransientStore(k.tStoreKey), types.DeferredInfoPrefix)
iter := store.Iterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := binary.BigEndian.Uint64(iter.Key())
if key >= uint64(len(k.txResults)) {
ctx.Logger().Error(fmt.Sprintf("getting invalid tx index in EVM deferred info: %d, num of txs: %d", key, len(k.txResults)))
for txIdx, msg := range k.msgs {
if msg == nil {
continue
}
val := &types.DeferredInfo{}
if err := val.Unmarshal(iter.Value()); err != nil {
// unable to unmarshal deferred info is serious, because it could cause
// balance surplus to be mishandled and thus affect total supply
panic(err)
}
// cast is safe here because of the check above
if k.txResults[int(key)].Code == 0 || val.Error != "" {
res = append(res, val)
txRes := k.txResults[txIdx]
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, uint64(txIdx))
val := store.Get(key)
if val == nil {
// this means the transaction got reverted during execution, either in ante handler
// or due to a panic in msg server
etx, _ := msg.AsTransaction()
if txRes.Code == 0 {
ctx.Logger().Error(fmt.Sprintf("transaction %s has code 0 but no deferred info", etx.Hash().Hex()))
}
res = append(res, &types.DeferredInfo{
TxIndex: uint32(txIdx),
TxHash: etx.Hash().Bytes(),
Error: txRes.Log,
})
} else {
info := &types.DeferredInfo{}
if err := info.Unmarshal(val); err != nil {
// unable to unmarshal deferred info is serious, because it could cause
// balance surplus to be mishandled and thus affect total supply
panic(err)

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
}
res = append(res, info)
}
}
return
Expand All @@ -52,20 +63,3 @@ func (k *Keeper) AppendToEvmTxDeferredInfo(ctx sdk.Context, bloom ethtypes.Bloom
}
prefix.NewStore(ctx.TransientStore(k.tStoreKey), types.DeferredInfoPrefix).Set(key, bz)
}

func (k *Keeper) AppendErrorToEvmTxDeferredInfo(ctx sdk.Context, txHash common.Hash, err string) {
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, uint64(ctx.TxIndex()))
val := &types.DeferredInfo{
TxIndex: uint32(ctx.TxIndex()),
TxHash: txHash[:],
Error: err,
}
bz, e := val.Marshal()
if e != nil {
// unable to marshal deferred info is serious, because it could cause
// balance surplus to be mishandled and thus affect total supply
panic(e)
}
prefix.NewStore(ctx.TransientStore(k.tStoreKey), types.DeferredInfoPrefix).Set(key, bz)
}
5 changes: 5 additions & 0 deletions x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Keeper struct {
Paramstore paramtypes.Subspace

txResults []*abci.ExecTxResult
msgs []*types.MsgEVMTransaction

bankKeeper bankkeeper.Keeper
accountKeeper *authkeeper.AccountKeeper
Expand Down Expand Up @@ -348,6 +349,10 @@ func (k *Keeper) SetTxResults(txResults []*abci.ExecTxResult) {
k.txResults = txResults
}

func (k *Keeper) SetMsgs(msgs []*types.MsgEVMTransaction) {
k.msgs = msgs
}

// Test use only
func (k *Keeper) GetPendingTxs() map[string][]*PendingTx {
return k.pendingTxs
Expand Down
Loading

0 comments on commit 4ec6c09

Please sign in to comment.