2020-08-19 11:46:20 +00:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-17 19:50:52 +00:00
|
|
|
"encoding/hex"
|
2022-12-23 18:10:37 +00:00
|
|
|
"encoding/json"
|
2020-08-19 11:46:20 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-05-06 17:37:38 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2023-02-20 08:04:11 +00:00
|
|
|
ethereum "github.com/ledgerwatch/erigon"
|
2023-01-13 18:12:18 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/chain"
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
Fix trace error in Polygon | Pass Engin to the Base API (#6131)
So there is an issue with tracing certain blocks/transactions on
Polygon, for example:
```
> '{"method": "trace_transaction","params":["0xb198d93f640343a98f90d93aa2b74b4fc5c64f3a649f1608d2bfd1004f9dee0e"],"id":1,"jsonrpc":"2.0"}'
```
gives the error `first run for txIndex 1 error: insufficient funds for
gas * price + value: address 0x10AD27A96CDBffC90ab3b83bF695911426A69f5E
have 16927727762862809 want 17594166808296934`
The reason is that this transaction is from the author of the block,
which doesn't have enough ETH to pay for the gas fee + tx value if he's
not the block author receiving transactions fees.
The issue is that currently the APIs are using `ethash.NewFaker()`
Engine for running traces, etc. which doesn't know how to get the author
for a specific block (which is consensus dependant); as it was noting in
several TODO comments.
The fix is to pass the Engine to the BaseAPI, which can then be used to
create the right Block Context. I chose to split the current Engine
interface in 2, with Reader and Writer, so that the BaseAPI only
receives the Reader one, which might be safer (even though it's only
used for getting the block Author).
2022-12-04 05:17:39 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus"
|
2023-02-20 08:04:11 +00:00
|
|
|
"github.com/ledgerwatch/erigon/consensus/bor/statefull"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/core/state"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/core/vm"
|
2022-11-30 01:31:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
|
2022-12-14 13:35:28 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/stagedsync"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/tracers"
|
2022-12-26 04:56:39 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/tracers/logger"
|
2022-10-31 05:31:38 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/rpchelper"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/services"
|
2020-08-19 11:46:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BlockGetter interface {
|
|
|
|
// GetBlockByHash retrieves a block from the database by hash, caching it if found.
|
2023-01-13 18:12:18 +00:00
|
|
|
GetBlockByHash(hash libcommon.Hash) (*types.Block, error)
|
2020-08-19 11:46:20 +00:00
|
|
|
// GetBlock retrieves a block from the database by hash and number,
|
|
|
|
// caching it if found.
|
2023-01-13 18:12:18 +00:00
|
|
|
GetBlock(hash libcommon.Hash, number uint64) *types.Block
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 06:47:05 +00:00
|
|
|
// ComputeTxEnv returns the execution environment of a certain transaction.
|
2023-01-27 09:05:09 +00:00
|
|
|
func ComputeTxEnv(ctx context.Context, engine consensus.EngineReader, block *types.Block, cfg *chain.Config, headerReader services.HeaderReader, dbtx kv.Tx, txIndex int, historyV3 bool) (core.Message, evmtypes.BlockContext, evmtypes.TxContext, *state.IntraBlockState, state.StateReader, error) {
|
2023-01-22 09:42:24 +00:00
|
|
|
reader, err := rpchelper.CreateHistoryStateReader(dbtx, block.NumberU64(), txIndex, historyV3, cfg.ChainName)
|
2022-10-31 05:31:38 +00:00
|
|
|
if err != nil {
|
2022-11-30 01:31:13 +00:00
|
|
|
return nil, evmtypes.BlockContext{}, evmtypes.TxContext{}, nil, nil, err
|
2022-10-31 05:31:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
// Create the parent state database
|
2021-04-21 02:18:05 +00:00
|
|
|
statedb := state.New(reader)
|
2020-08-19 11:46:20 +00:00
|
|
|
|
|
|
|
if txIndex == 0 && len(block.Transactions()) == 0 {
|
2022-11-30 01:31:13 +00:00
|
|
|
return nil, evmtypes.BlockContext{}, evmtypes.TxContext{}, statedb, reader, nil
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
2023-01-24 04:18:32 +00:00
|
|
|
getHeader := func(hash libcommon.Hash, n uint64) *types.Header {
|
|
|
|
h, _ := headerReader.HeaderByNumber(ctx, dbtx, n)
|
|
|
|
return h
|
|
|
|
}
|
2023-01-27 12:09:51 +00:00
|
|
|
header := block.HeaderNoCopy()
|
2023-01-24 04:18:32 +00:00
|
|
|
BlockContext := core.NewEVMBlockContext(header, core.GetHashFn(header, getHeader), engine, nil)
|
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
// Recompute transactions up to the target index.
|
2021-04-22 17:11:37 +00:00
|
|
|
signer := types.MakeSigner(cfg, block.NumberU64())
|
2023-01-24 04:18:32 +00:00
|
|
|
if historyV3 {
|
|
|
|
rules := cfg.Rules(BlockContext.BlockNumber, BlockContext.Time)
|
|
|
|
txn := block.Transactions()[txIndex]
|
2023-01-27 12:09:51 +00:00
|
|
|
statedb.Prepare(txn.Hash(), block.Hash(), txIndex)
|
2023-01-24 04:18:32 +00:00
|
|
|
msg, _ := txn.AsMessage(*signer, block.BaseFee(), rules)
|
|
|
|
if msg.FeeCap().IsZero() && engine != nil {
|
|
|
|
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
|
|
|
|
return core.SysCallContract(contract, data, *cfg, statedb, header, engine, true /* constCall */)
|
|
|
|
}
|
|
|
|
msg.SetIsFree(engine.IsServiceTransaction(msg.From(), syscall))
|
|
|
|
}
|
2020-08-19 11:46:20 +00:00
|
|
|
|
2023-01-24 04:18:32 +00:00
|
|
|
TxContext := core.NewEVMTxContext(msg)
|
|
|
|
return msg, BlockContext, TxContext, statedb, reader, nil
|
|
|
|
}
|
2022-11-30 01:31:13 +00:00
|
|
|
vmenv := vm.NewEVM(BlockContext, evmtypes.TxContext{}, statedb, cfg, vm.Config{})
|
2022-05-26 16:20:34 +00:00
|
|
|
rules := vmenv.ChainRules()
|
2022-12-14 13:35:28 +00:00
|
|
|
|
|
|
|
consensusHeaderReader := stagedsync.NewChainReaderImpl(cfg, dbtx, nil)
|
|
|
|
|
|
|
|
core.InitializeBlockExecution(engine.(consensus.Engine), consensusHeaderReader, nil, header, block.Transactions(), block.Uncles(), cfg, statedb)
|
|
|
|
|
2023-01-24 04:18:32 +00:00
|
|
|
for idx, txn := range block.Transactions() {
|
2020-08-19 11:46:20 +00:00
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-ctx.Done():
|
2022-11-30 01:31:13 +00:00
|
|
|
return nil, evmtypes.BlockContext{}, evmtypes.TxContext{}, nil, nil, ctx.Err()
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
2023-01-24 04:18:32 +00:00
|
|
|
statedb.Prepare(txn.Hash(), block.Hash(), idx)
|
2020-08-19 11:46:20 +00:00
|
|
|
|
|
|
|
// Assemble the transaction call message and return if the requested offset
|
2023-01-24 04:18:32 +00:00
|
|
|
msg, _ := txn.AsMessage(*signer, block.BaseFee(), rules)
|
2022-12-14 13:35:28 +00:00
|
|
|
if msg.FeeCap().IsZero() && engine != nil {
|
2023-01-13 18:12:18 +00:00
|
|
|
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
|
2022-12-14 13:35:28 +00:00
|
|
|
return core.SysCallContract(contract, data, *cfg, statedb, header, engine, true /* constCall */)
|
|
|
|
}
|
|
|
|
msg.SetIsFree(engine.IsServiceTransaction(msg.From(), syscall))
|
|
|
|
}
|
|
|
|
|
2021-03-14 18:52:15 +00:00
|
|
|
TxContext := core.NewEVMTxContext(msg)
|
2023-01-27 12:09:51 +00:00
|
|
|
if idx == txIndex {
|
2021-03-14 18:52:15 +00:00
|
|
|
return msg, BlockContext, TxContext, statedb, reader, nil
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
2021-07-03 12:55:23 +00:00
|
|
|
vmenv.Reset(TxContext, statedb)
|
2020-08-19 11:46:20 +00:00
|
|
|
// Not yet the searched for transaction, execute on top of the current state
|
2023-01-24 04:18:32 +00:00
|
|
|
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(txn.GetGas()), true /* refunds */, false /* gasBailout */); err != nil {
|
|
|
|
return nil, evmtypes.BlockContext{}, evmtypes.TxContext{}, nil, nil, fmt.Errorf("transaction %x failed: %w", txn.Hash(), err)
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
// Ensure any modifications are committed to the state
|
2022-05-26 10:08:59 +00:00
|
|
|
// Only delete empty objects if EIP161 (part of Spurious Dragon) is in effect
|
2022-10-31 05:31:38 +00:00
|
|
|
_ = statedb.FinalizeTx(rules, reader.(*state.PlainState))
|
2021-12-29 03:36:41 +00:00
|
|
|
|
|
|
|
if idx+1 == len(block.Transactions()) {
|
|
|
|
// Return the state from evaluating all txs in the block, note no msg or TxContext in this case
|
2022-11-30 01:31:13 +00:00
|
|
|
return nil, BlockContext, evmtypes.TxContext{}, statedb, reader, nil
|
2021-12-29 03:36:41 +00:00
|
|
|
}
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
2022-11-30 01:31:13 +00:00
|
|
|
return nil, evmtypes.BlockContext{}, evmtypes.TxContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %x", txIndex, block.Hash())
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TraceTx configures a new tracer according to the provided configuration, and
|
|
|
|
// executes the given message in the provided environment. The return value will
|
|
|
|
// be tracer dependent.
|
2021-05-06 17:37:38 +00:00
|
|
|
func TraceTx(
|
|
|
|
ctx context.Context,
|
|
|
|
message core.Message,
|
2022-11-30 01:31:13 +00:00
|
|
|
blockCtx evmtypes.BlockContext,
|
|
|
|
txCtx evmtypes.TxContext,
|
|
|
|
ibs evmtypes.IntraBlockState,
|
2021-05-06 17:37:38 +00:00
|
|
|
config *tracers.TraceConfig,
|
2023-01-13 18:12:18 +00:00
|
|
|
chainConfig *chain.Config,
|
2021-05-06 17:37:38 +00:00
|
|
|
stream *jsoniter.Stream,
|
2022-09-17 06:25:27 +00:00
|
|
|
callTimeout time.Duration,
|
2021-05-06 17:37:38 +00:00
|
|
|
) error {
|
2020-08-19 11:46:20 +00:00
|
|
|
// Assemble the structured logger or the JavaScript tracer
|
|
|
|
var (
|
2022-12-23 05:43:08 +00:00
|
|
|
tracer vm.EVMLogger
|
2020-08-19 11:46:20 +00:00
|
|
|
err error
|
|
|
|
)
|
2021-05-06 17:37:38 +00:00
|
|
|
var streaming bool
|
2020-08-19 11:46:20 +00:00
|
|
|
switch {
|
|
|
|
case config != nil && config.Tracer != nil:
|
|
|
|
// Define a meaningful timeout of a single transaction trace
|
2021-07-15 10:25:32 +00:00
|
|
|
timeout := callTimeout
|
2020-08-19 11:46:20 +00:00
|
|
|
if config.Timeout != nil {
|
|
|
|
if timeout, err = time.ParseDuration(*config.Timeout); err != nil {
|
2021-06-16 17:24:56 +00:00
|
|
|
stream.WriteNil()
|
2021-05-06 17:37:38 +00:00
|
|
|
return err
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-15 13:19:58 +00:00
|
|
|
// Construct the JavaScript tracer to execute with
|
2023-02-07 15:50:11 +00:00
|
|
|
cfg := json.RawMessage("{}")
|
|
|
|
if config != nil && config.TracerConfig != nil {
|
|
|
|
cfg = *config.TracerConfig
|
|
|
|
}
|
2021-12-15 13:19:58 +00:00
|
|
|
if tracer, err = tracers.New(*config.Tracer, &tracers.Context{
|
|
|
|
TxHash: txCtx.TxHash,
|
2023-02-07 15:50:11 +00:00
|
|
|
}, cfg); err != nil {
|
2021-06-16 17:24:56 +00:00
|
|
|
stream.WriteNil()
|
2021-05-06 17:37:38 +00:00
|
|
|
return err
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
// Handle timeouts and RPC cancellations
|
|
|
|
deadlineCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
go func() {
|
|
|
|
<-deadlineCtx.Done()
|
2022-12-23 18:10:37 +00:00
|
|
|
tracer.(tracers.Tracer).Stop(errors.New("execution timeout"))
|
2020-08-19 11:46:20 +00:00
|
|
|
}()
|
|
|
|
defer cancel()
|
2021-05-06 17:37:38 +00:00
|
|
|
streaming = false
|
2020-08-19 11:46:20 +00:00
|
|
|
|
|
|
|
case config == nil:
|
2022-12-26 04:56:39 +00:00
|
|
|
tracer = logger.NewJsonStreamLogger(nil, ctx, stream)
|
2021-05-06 17:37:38 +00:00
|
|
|
streaming = true
|
2020-08-19 11:46:20 +00:00
|
|
|
|
|
|
|
default:
|
2022-12-26 04:56:39 +00:00
|
|
|
tracer = logger.NewJsonStreamLogger(config.LogConfig, ctx, stream)
|
2021-05-06 17:37:38 +00:00
|
|
|
streaming = true
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
// Run the transaction with tracing enabled.
|
2021-03-14 18:52:15 +00:00
|
|
|
vmenv := vm.NewEVM(blockCtx, txCtx, ibs, chainConfig, vm.Config{Debug: true, Tracer: tracer})
|
2022-08-10 12:04:13 +00:00
|
|
|
var refunds = true
|
2020-12-09 18:24:08 +00:00
|
|
|
if config != nil && config.NoRefunds != nil && *config.NoRefunds {
|
|
|
|
refunds = false
|
|
|
|
}
|
2021-05-06 17:37:38 +00:00
|
|
|
if streaming {
|
|
|
|
stream.WriteObjectStart()
|
|
|
|
stream.WriteObjectField("structLogs")
|
|
|
|
stream.WriteArrayStart()
|
|
|
|
}
|
2023-02-20 08:04:11 +00:00
|
|
|
|
|
|
|
var result *core.ExecutionResult
|
2023-03-02 10:34:29 +00:00
|
|
|
if config != nil && config.BorTx != nil && *config.BorTx {
|
2023-02-20 08:04:11 +00:00
|
|
|
callmsg := prepareCallMessage(message)
|
|
|
|
result, err = statefull.ApplyBorMessage(*vmenv, callmsg)
|
|
|
|
} else {
|
|
|
|
result, err = core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas()), refunds, false /* gasBailout */)
|
|
|
|
}
|
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
if err != nil {
|
2021-06-16 17:24:56 +00:00
|
|
|
if streaming {
|
|
|
|
stream.WriteArrayEnd()
|
|
|
|
stream.WriteObjectEnd()
|
2023-02-20 09:42:40 +00:00
|
|
|
stream.WriteMore()
|
|
|
|
stream.WriteObjectField("resultHack") // higher-level func will assing it to NULL
|
2023-02-09 08:32:38 +00:00
|
|
|
} else {
|
|
|
|
stream.WriteNil()
|
2021-06-16 17:24:56 +00:00
|
|
|
}
|
2021-10-04 15:16:52 +00:00
|
|
|
return fmt.Errorf("tracing failed: %w", err)
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
// Depending on the tracer type, format and return the output
|
2021-05-06 17:37:38 +00:00
|
|
|
if streaming {
|
|
|
|
stream.WriteArrayEnd()
|
|
|
|
stream.WriteMore()
|
|
|
|
stream.WriteObjectField("gas")
|
|
|
|
stream.WriteUint64(result.UsedGas)
|
|
|
|
stream.WriteMore()
|
|
|
|
stream.WriteObjectField("failed")
|
|
|
|
stream.WriteBool(result.Failed())
|
|
|
|
stream.WriteMore()
|
2021-07-03 13:34:23 +00:00
|
|
|
// If the result contains a revert reason, return it.
|
2022-12-05 02:06:16 +00:00
|
|
|
returnVal := hex.EncodeToString(result.Return())
|
2021-07-03 13:34:23 +00:00
|
|
|
if len(result.Revert()) > 0 {
|
2022-12-05 02:06:16 +00:00
|
|
|
returnVal = hex.EncodeToString(result.Revert())
|
2021-07-03 13:34:23 +00:00
|
|
|
}
|
2021-05-06 17:37:38 +00:00
|
|
|
stream.WriteObjectField("returnValue")
|
2021-07-03 13:34:23 +00:00
|
|
|
stream.WriteString(returnVal)
|
2021-05-06 17:37:38 +00:00
|
|
|
stream.WriteObjectEnd()
|
|
|
|
} else {
|
2022-12-23 18:10:37 +00:00
|
|
|
if r, err1 := tracer.(tracers.Tracer).GetResult(); err1 == nil {
|
2021-05-06 17:37:38 +00:00
|
|
|
stream.Write(r)
|
|
|
|
} else {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-20 08:04:11 +00:00
|
|
|
|
|
|
|
func prepareCallMessage(msg core.Message) statefull.Callmsg {
|
|
|
|
return statefull.Callmsg{
|
|
|
|
CallMsg: ethereum.CallMsg{
|
|
|
|
From: msg.From(),
|
|
|
|
To: msg.To(),
|
|
|
|
Gas: msg.Gas(),
|
|
|
|
GasPrice: msg.GasPrice(),
|
|
|
|
Value: msg.Value(),
|
|
|
|
Data: msg.Data(),
|
|
|
|
AccessList: msg.AccessList(),
|
|
|
|
}}
|
|
|
|
}
|