mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-29 07:07:16 +00:00
12cde41772
* Where I am at * Refactoring of transaction types * More refactoring * Use Homested signer in rpc daemon * Unified signer * Continue unified signer * A bit more * Fixes and down the rabbit hole... * More tx pool fixes * More refactoring fixes * More fixes' * more fixes * More fixes * More compile fixes * More RLP hand-writing * Finish RLP encoding/decoding of transactions * Fixes to header encoding, start on protocol packets * Transaction decoding * Use DecodeTransaction function * Decoding BlockBodyPacket * Encode and decode for pool txs * Start fixing tests * Introduce SigningHash * Fixes to SignHash * RLP encoding fixes * Fixes for encoding/decoding * More test fixes * Fix more tests * More test fixes * More test fixes * Fix core tests * More fixes for signer * Fix for tx * Fixes to string encoding/size * Fix eip2930 test * Fix rest of ./tests * More fixes * Fix compilation * More test fixes * More test fixes * Test fixes * More fixes * Reuse EncodingSize in EncodeRLP for accessList * Rearrange things in dynamic fee tx * Add MarshalBinary * More fixes * Make V,R,S non-pointers * More NPE fixes * More fixes * Receipt fixes * Fix core/types * Fix ./eth * More compile fixes for tests * More test fixes * More test fixes * Try to see lint errors better * Try to see lint errors better * Fix lint * Debugging eip1559 test * Fix TestEIP1559Transition test * Fix NewBlockPacket encoding/decoding * Fix calculation of TxHash * Fix perf problem with senders * Update aleut config values * Try adding static peers * Add staticpeers to defaul flags * Change aleut networkID * Fix test Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local> Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
150 lines
5.7 KiB
Go
150 lines
5.7 KiB
Go
package transactions
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
"github.com/ledgerwatch/turbo-geth/consensus"
|
|
"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.
|
|
GetBlockByHash(hash common.Hash) (*types.Block, error)
|
|
// 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.
|
|
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) {
|
|
// Create the parent state database
|
|
block, err := blockGetter.GetBlockByHash(blockHash)
|
|
if err != nil {
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, err
|
|
}
|
|
if block == nil {
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, fmt.Errorf("block %x not found", blockHash)
|
|
}
|
|
parent := blockGetter.GetBlock(block.ParentHash(), block.NumberU64()-1)
|
|
if parent == nil {
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, fmt.Errorf("parent %x not found", block.ParentHash())
|
|
}
|
|
|
|
reader := state.NewPlainDBState(ethdb.NewRoTxDb(dbtx), parent.NumberU64())
|
|
statedb := state.New(reader)
|
|
|
|
if txIndex == 0 && len(block.Transactions()) == 0 {
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, statedb, reader, nil
|
|
}
|
|
// Recompute transactions up to the target index.
|
|
signer := types.MakeSigner(cfg, block.NumberU64())
|
|
|
|
for idx, tx := range block.Transactions() {
|
|
select {
|
|
default:
|
|
case <-ctx.Done():
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, ctx.Err()
|
|
}
|
|
statedb.Prepare(tx.Hash(), blockHash, idx)
|
|
|
|
// Assemble the transaction call message and return if the requested offset
|
|
msg, _ := tx.AsMessage(*signer)
|
|
BlockContext := core.NewEVMBlockContext(block.Header(), getHeader, engine, nil)
|
|
TxContext := core.NewEVMTxContext(msg)
|
|
if idx == int(txIndex) {
|
|
return msg, BlockContext, TxContext, statedb, reader, nil
|
|
}
|
|
// Not yet the searched for transaction, execute on top of the current state
|
|
vmenv := vm.NewEVM(BlockContext, TxContext, statedb, cfg, vm.Config{})
|
|
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.GetGas()), true /* refunds */, false /* gasBailout */); err != nil {
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, fmt.Errorf("transaction %x failed: %v", tx.Hash(), err)
|
|
}
|
|
// Ensure any modifications are committed to the state
|
|
// Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
|
|
_ = statedb.FinalizeTx(vmenv.ChainConfig().WithEIPsFlags(context.Background(), block.NumberU64()), state.NewNoopWriter())
|
|
}
|
|
return nil, vm.BlockContext{}, vm.TxContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %x", txIndex, blockHash)
|
|
}
|
|
|
|
// 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.
|
|
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) {
|
|
// 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
|
|
if tracer, err = tracers.New(*config.Tracer, txCtx); err != nil {
|
|
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.
|
|
vmenv := vm.NewEVM(blockCtx, txCtx, ibs, chainConfig, vm.Config{Debug: true, Tracer: tracer})
|
|
|
|
var refunds bool = true
|
|
if config != nil && config.NoRefunds != nil && *config.NoRefunds {
|
|
refunds = false
|
|
}
|
|
result, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas()), refunds, false /* gasBailout */)
|
|
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))
|
|
}
|
|
}
|