2020-08-19 11:46:20 +00:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
2021-04-06 09:52:53 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/consensus"
|
2020-08-19 11:46:20 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/state"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/vm"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/eth/tracers"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/internal/ethapi"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// defaultTraceTimeout is the amount of time a single transaction can execute
|
|
|
|
// by default before being forcefully aborted.
|
|
|
|
defaultTraceTimeout = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
type BlockGetter interface {
|
|
|
|
// GetBlockByHash retrieves a block from the database by hash, caching it if found.
|
2020-10-24 06:57:09 +00:00
|
|
|
GetBlockByHash(hash common.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.
|
|
|
|
GetBlock(hash common.Hash, number uint64) *types.Block
|
|
|
|
}
|
|
|
|
|
|
|
|
// computeTxEnv returns the execution environment of a certain transaction.
|
2021-04-21 02:18:05 +00:00
|
|
|
func ComputeTxEnv(ctx context.Context, blockGetter BlockGetter, cfg *params.ChainConfig, getHeader func(hash common.Hash, number uint64) *types.Header, engine consensus.Engine, dbtx ethdb.Tx, blockHash common.Hash, txIndex uint64) (core.Message, vm.BlockContext, vm.TxContext, *state.IntraBlockState, *state.PlainDBState, error) {
|
2020-08-19 11:46:20 +00:00
|
|
|
// Create the parent state database
|
2020-10-24 06:57:09 +00:00
|
|
|
block, err := blockGetter.GetBlockByHash(blockHash)
|
|
|
|
if err != nil {
|
2021-03-14 18:52:15 +00:00
|
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, err
|
2020-10-24 06:57:09 +00:00
|
|
|
}
|
2020-08-19 11:46:20 +00:00
|
|
|
if block == nil {
|
2021-03-14 18:52:15 +00:00
|
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, fmt.Errorf("block %x not found", blockHash)
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
parent := blockGetter.GetBlock(block.ParentHash(), block.NumberU64()-1)
|
|
|
|
if parent == nil {
|
2021-03-14 18:52:15 +00:00
|
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, fmt.Errorf("parent %x not found", block.ParentHash())
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 02:18:05 +00:00
|
|
|
reader := state.NewPlainDBState(ethdb.NewRoTxDb(dbtx), parent.NumberU64())
|
|
|
|
statedb := state.New(reader)
|
2020-08-19 11:46:20 +00:00
|
|
|
|
|
|
|
if txIndex == 0 && len(block.Transactions()) == 0 {
|
2021-03-14 18:52:15 +00:00
|
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, statedb, reader, nil
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
// Recompute transactions up to the target index.
|
|
|
|
signer := types.MakeSigner(cfg, block.Number())
|
|
|
|
|
|
|
|
for idx, tx := range block.Transactions() {
|
|
|
|
select {
|
|
|
|
default:
|
|
|
|
case <-ctx.Done():
|
2021-03-14 18:52:15 +00:00
|
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, ctx.Err()
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
statedb.Prepare(tx.Hash(), blockHash, idx)
|
|
|
|
|
|
|
|
// Assemble the transaction call message and return if the requested offset
|
|
|
|
msg, _ := tx.AsMessage(signer)
|
2021-04-06 09:52:53 +00:00
|
|
|
BlockContext := core.NewEVMBlockContext(block.Header(), getHeader, engine, nil)
|
2021-03-14 18:52:15 +00:00
|
|
|
TxContext := core.NewEVMTxContext(msg)
|
2020-08-19 11:46:20 +00:00
|
|
|
if idx == int(txIndex) {
|
2021-03-14 18:52:15 +00:00
|
|
|
return msg, BlockContext, TxContext, statedb, reader, nil
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
// Not yet the searched for transaction, execute on top of the current state
|
2021-03-14 18:52:15 +00:00
|
|
|
vmenv := vm.NewEVM(BlockContext, TxContext, statedb, cfg, vm.Config{})
|
2021-02-12 16:47:32 +00:00
|
|
|
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas()), true /* refunds */, false /* gasBailout */); err != nil {
|
2021-03-14 18:52:15 +00:00
|
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, fmt.Errorf("transaction %x failed: %v", tx.Hash(), err)
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
// Ensure any modifications are committed to the state
|
|
|
|
// Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
|
2021-04-20 06:23:16 +00:00
|
|
|
_ = statedb.FinalizeTx(vmenv.ChainConfig().WithEIPsFlags(context.Background(), block.Number()), state.NewNoopWriter())
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
2021-03-14 18:52:15 +00:00
|
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %x", txIndex, blockHash)
|
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-03-14 18:52:15 +00:00
|
|
|
func TraceTx(ctx context.Context, message core.Message, blockCtx vm.BlockContext, txCtx vm.TxContext, ibs vm.IntraBlockState, config *tracers.TraceConfig, chainConfig *params.ChainConfig) (interface{}, error) {
|
2020-08-19 11:46:20 +00:00
|
|
|
// Assemble the structured logger or the JavaScript tracer
|
|
|
|
var (
|
|
|
|
tracer vm.Tracer
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch {
|
|
|
|
case config != nil && config.Tracer != nil:
|
|
|
|
// Define a meaningful timeout of a single transaction trace
|
|
|
|
timeout := defaultTraceTimeout
|
|
|
|
if config.Timeout != nil {
|
|
|
|
if timeout, err = time.ParseDuration(*config.Timeout); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Constuct the JavaScript tracer to execute with
|
2021-03-14 18:52:15 +00:00
|
|
|
if tracer, err = tracers.New(*config.Tracer, txCtx); err != nil {
|
2020-08-19 11:46:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Handle timeouts and RPC cancellations
|
|
|
|
deadlineCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
go func() {
|
|
|
|
<-deadlineCtx.Done()
|
|
|
|
tracer.(*tracers.Tracer).Stop(errors.New("execution timeout"))
|
|
|
|
}()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
case config == nil:
|
|
|
|
tracer = vm.NewStructLogger(nil)
|
|
|
|
|
|
|
|
default:
|
|
|
|
tracer = vm.NewStructLogger(config.LogConfig)
|
|
|
|
}
|
|
|
|
// 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})
|
2020-08-19 11:46:20 +00:00
|
|
|
|
2020-12-09 18:24:08 +00:00
|
|
|
var refunds bool = true
|
|
|
|
if config != nil && config.NoRefunds != nil && *config.NoRefunds {
|
|
|
|
refunds = false
|
|
|
|
}
|
2021-02-12 16:47:32 +00:00
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("tracing failed: %v", err)
|
|
|
|
}
|
|
|
|
// Depending on the tracer type, format and return the output
|
|
|
|
switch tracer := tracer.(type) {
|
|
|
|
case *vm.StructLogger:
|
|
|
|
return ðapi.ExecutionResult{
|
|
|
|
Gas: result.UsedGas,
|
|
|
|
Failed: result.Failed(),
|
|
|
|
ReturnValue: fmt.Sprintf("%x", result.Return()),
|
|
|
|
StructLogs: ethapi.FormatLogs(tracer.StructLogs()),
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
case *tracers.Tracer:
|
|
|
|
return tracer.GetResult()
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("bad tracer type %T", tracer))
|
|
|
|
}
|
|
|
|
}
|