Skip to content

Commit

Permalink
Fixed failed receipt to fit with RCP output
Browse files Browse the repository at this point in the history
  • Loading branch information
maoueh committed Dec 10, 2024
1 parent 458a8cd commit 9e69395
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
54 changes: 37 additions & 17 deletions x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"math"
"math/big"
"os"
"runtime/debug"
"strings"

Expand Down Expand Up @@ -243,8 +244,16 @@ func (k *Keeper) applyEVMTx(ctx sdk.Context, tx *ethtypes.Transaction, msg *core
onEnd = func(res *core.ExecutionResult, err error) {
var receipt *ethtypes.Receipt
if res != nil {
// Try to build a receipt if as soon as we have an ExecutionResult set
receipt = getEthReceipt(ctx, tx, msg, res, stateDB)
} else if err != nil {
receipt = getEthFailedReceipt(ctx, tx, msg)
} else {
panic("onEnd called with nil result and nil error")
}

out, err := json.Marshal(receipt)
if err == nil {
fmt.Fprintf(os.Stderr, "[Firehose] Receipt %s", string(out))
}

var txErr = err
Expand Down Expand Up @@ -315,12 +324,7 @@ func (k *Keeper) applyEVMMessageWithTracing(
recoveredErr = fmt.Errorf("%v", r)
}

onEnd(&core.ExecutionResult{
UsedGas: msg.GasLimit,
RefundedGas: 0,
ReturnData: nil,
Err: recoveredErr,
}, recoveredErr)
onEnd(nil, recoveredErr)
panic(r)
} else {
onEnd(res, err)
Expand Down Expand Up @@ -431,17 +435,39 @@ func (server msgServer) AssociateContractAddress(goCtx context.Context, msg *typ
}

func getEthReceipt(ctx sdk.Context, tx *ethtypes.Transaction, msg *core.Message, res *core.ExecutionResult, stateDB *state.DBImpl) *ethtypes.Receipt {
ethLogs := stateDB.GetAllLogs()
receipt := getEthCommonReceipt(ctx, tx, msg)
receipt.GasUsed = res.UsedGas
receipt.Logs = stateDB.GetAllLogs()
receipt.Bloom = ethtypes.CreateBloom(ethtypes.Receipts{receipt})

if res.Err == nil {
receipt.Status = ethtypes.ReceiptStatusSuccessful
} else {
receipt.Status = ethtypes.ReceiptStatusFailed
}

return receipt
}

// getEthFailedReceipt returns a receipt for a transaction that had no execution result and ended with an error. This
// usually happens when the transaction panicked due to out of gas error and later recovered.
func getEthFailedReceipt(ctx sdk.Context, tx *ethtypes.Transaction, msg *core.Message) *ethtypes.Receipt {
receipt := getEthCommonReceipt(ctx, tx, msg)
receipt.Status = ethtypes.ReceiptStatusFailed

return receipt
}

// getEthFailedReceipt returns a receipt for a transaction that had no execution result and ended with an error. This
// usually happens when the transaction panicked due to out of gas error and later recovered.
func getEthCommonReceipt(ctx sdk.Context, tx *ethtypes.Transaction, msg *core.Message) *ethtypes.Receipt {
receipt := &ethtypes.Receipt{
Type: tx.Type(),
CumulativeGasUsed: uint64(0),
Logs: ethLogs,
TxHash: tx.Hash(),
GasUsed: res.UsedGas,
EffectiveGasPrice: tx.GasPrice(),
TransactionIndex: uint(ctx.TxIndex()),
}
receipt.Bloom = ethtypes.CreateBloom(ethtypes.Receipts{receipt})

if msg.To == nil {
receipt.ContractAddress = crypto.CreateAddress(msg.From, msg.Nonce)
Expand All @@ -451,12 +477,6 @@ func getEthReceipt(ctx sdk.Context, tx *ethtypes.Transaction, msg *core.Message,
}
}

if res.Err == nil {
receipt.Status = ethtypes.ReceiptStatusSuccessful
} else {
receipt.Status = ethtypes.ReceiptStatusFailed
}

return receipt
}

Expand Down
4 changes: 4 additions & 0 deletions x/evm/tracers/firehose.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ func getActivePrecompilesChecker(rules params.Rules) func(addr common.Address) b
}

func (f *Firehose) OnBlockEnd(err error) {
if f.block.Number >= 119822071 {
panic("Do not go above 119822071 for now")
}

blockNumber := f.block.Number
firehoseInfo("block ending (number=%d, trx=%d, err=%s)", blockNumber, len(f.block.TransactionTraces), errorView(err))

Expand Down

0 comments on commit 9e69395

Please sign in to comment.