github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/eth/tracers/native/4byte.go (about)

     1  // Copyright 2021 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package native
    18  
    19  import (
    20  	"encoding/json"
    21  	"math/big"
    22  	"strconv"
    23  	"sync/atomic"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/core/vm"
    28  	"github.com/ethereum/go-ethereum/eth/tracers"
    29  )
    30  
    31  func init() {
    32  	register("4byteTracer", newFourByteTracer)
    33  }
    34  
    35  // fourByteTracer searches for 4byte-identifiers, and collects them for post-processing.
    36  // It collects the methods identifiers along with the size of the supplied data, so
    37  // a reversed signature can be matched against the size of the data.
    38  //
    39  // Example:
    40  //
    41  //	> debug.traceTransaction( "0x214e597e35da083692f5386141e69f47e973b2c56e7a8073b1ea08fd7571e9de", {tracer: "4byteTracer"})
    42  //	{
    43  //	  0x27dc297e-128: 1,
    44  //	  0x38cc4831-0: 2,
    45  //	  0x524f3889-96: 1,
    46  //	  0xadf59f99-288: 1,
    47  //	  0xc281d19e-0: 1
    48  //	}
    49  type fourByteTracer struct {
    50  	env               *vm.EVM
    51  	ids               map[string]int   // ids aggregates the 4byte ids found
    52  	interrupt         uint32           // Atomic flag to signal execution interruption
    53  	reason            error            // Textual reason for the interruption
    54  	activePrecompiles []common.Address // Updated on CaptureStart based on given rules
    55  }
    56  
    57  // newFourByteTracer returns a native go tracer which collects
    58  // 4 byte-identifiers of a tx, and implements vm.EVMLogger.
    59  func newFourByteTracer(ctx *tracers.Context, _ json.RawMessage) (tracers.Tracer, error) {
    60  	t := &fourByteTracer{
    61  		ids: make(map[string]int),
    62  	}
    63  	return t, nil
    64  }
    65  
    66  // isPrecompiled returns whether the addr is a precompile. Logic borrowed from newJsTracer in eth/tracers/js/tracer.go
    67  func (t *fourByteTracer) isPrecompiled(addr common.Address) bool {
    68  	for _, p := range t.activePrecompiles {
    69  		if p == addr {
    70  			return true
    71  		}
    72  	}
    73  	return false
    74  }
    75  
    76  // store saves the given identifier and datasize.
    77  func (t *fourByteTracer) store(id []byte, size int) {
    78  	key := bytesToHex(id) + "-" + strconv.Itoa(size)
    79  	t.ids[key] += 1
    80  }
    81  
    82  // CaptureStart implements the EVMLogger interface to initialize the tracing operation.
    83  func (t *fourByteTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
    84  	t.env = env
    85  
    86  	// Update list of precompiles based on current block
    87  	rules := env.ChainConfig().Rules(env.Context.BlockNumber, env.Context.Random != nil)
    88  	t.activePrecompiles = vm.ActivePrecompiles(rules)
    89  
    90  	// Save the outer calldata also
    91  	if len(input) >= 4 {
    92  		t.store(input[0:4], len(input)-4)
    93  	}
    94  }
    95  
    96  // CaptureState implements the EVMLogger interface to trace a single step of VM execution.
    97  func (t *fourByteTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
    98  }
    99  
   100  // CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
   101  func (t *fourByteTracer) CaptureEnter(op vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) {
   102  	// Skip if tracing was interrupted
   103  	if atomic.LoadUint32(&t.interrupt) > 0 {
   104  		t.env.Cancel()
   105  		return
   106  	}
   107  	if len(input) < 4 {
   108  		return
   109  	}
   110  	// primarily we want to avoid CREATE/CREATE2/SELFDESTRUCT
   111  	if op != vm.DELEGATECALL && op != vm.STATICCALL &&
   112  		op != vm.CALL && op != vm.CALLCODE {
   113  		return
   114  	}
   115  	// Skip any pre-compile invocations, those are just fancy opcodes
   116  	if t.isPrecompiled(to) {
   117  		return
   118  	}
   119  	t.store(input[0:4], len(input)-4)
   120  }
   121  
   122  // CaptureExit is called when EVM exits a scope, even if the scope didn't
   123  // execute any code.
   124  func (t *fourByteTracer) CaptureExit(output []byte, gasUsed uint64, err error) {
   125  }
   126  
   127  // CaptureFault implements the EVMLogger interface to trace an execution fault.
   128  func (t *fourByteTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
   129  }
   130  
   131  // CaptureEnd is called after the call finishes to finalize the tracing.
   132  func (t *fourByteTracer) CaptureEnd(output []byte, gasUsed uint64, _ time.Duration, err error) {
   133  }
   134  
   135  func (*fourByteTracer) CaptureTxStart(gasLimit uint64) {}
   136  
   137  func (*fourByteTracer) CaptureTxEnd(restGas uint64) {}
   138  
   139  // GetResult returns the json-encoded nested list of call traces, and any
   140  // error arising from the encoding or forceful termination (via `Stop`).
   141  func (t *fourByteTracer) GetResult() (json.RawMessage, error) {
   142  	res, err := json.Marshal(t.ids)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  	return res, t.reason
   147  }
   148  
   149  // Stop terminates execution of the tracer at the first opportune moment.
   150  func (t *fourByteTracer) Stop(err error) {
   151  	t.reason = err
   152  	atomic.StoreUint32(&t.interrupt, 1)
   153  }
   154  
   155  func bytesToHex(s []byte) string {
   156  	return "0x" + common.Bytes2Hex(s)
   157  }