github.com/amazechain/amc@v0.1.3/internal/tracers/native/4byte.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package native
    18  
    19  import (
    20  	"encoding/json"
    21  	"github.com/holiman/uint256"
    22  	"strconv"
    23  	"sync/atomic"
    24  
    25  	common "github.com/amazechain/amc/common/types"
    26  	"github.com/amazechain/amc/internal/tracers"
    27  	"github.com/amazechain/amc/internal/vm"
    28  )
    29  
    30  func init() {
    31  	tracers.DefaultDirectory.Register("4byteTracer", newFourByteTracer, false)
    32  }
    33  
    34  // fourByteTracer searches for 4byte-identifiers, and collects them for post-processing.
    35  // It collects the methods identifiers along with the size of the supplied data, so
    36  // a reversed signature can be matched against the size of the data.
    37  //
    38  // Example:
    39  //
    40  //	> debug.traceTransaction( "0x214e597e35da083692f5386141e69f47e973b2c56e7a8073b1ea08fd7571e9de", {tracer: "4byteTracer"})
    41  //	{
    42  //	  0x27dc297e-128: 1,
    43  //	  0x38cc4831-0: 2,
    44  //	  0x524f3889-96: 1,
    45  //	  0xadf59f99-288: 1,
    46  //	  0xc281d19e-0: 1
    47  //	}
    48  type fourByteTracer struct {
    49  	noopTracer
    50  	ids               map[string]int   // ids aggregates the 4byte ids found
    51  	interrupt         uint32           // Atomic flag to signal execution interruption
    52  	reason            error            // Textual reason for the interruption
    53  	activePrecompiles []common.Address // Updated on CaptureStart based on given rules
    54  }
    55  
    56  // newFourByteTracer returns a native go tracer which collects
    57  // 4 byte-identifiers of a tx, and implements vm.EVMLogger.
    58  func newFourByteTracer(ctx *tracers.Context, _ json.RawMessage) (tracers.Tracer, error) {
    59  	t := &fourByteTracer{
    60  		ids: make(map[string]int),
    61  	}
    62  	return t, nil
    63  }
    64  
    65  // isPrecompiled returns whether the addr is a precompile. Logic borrowed from newJsTracer in eth/tracers/js/tracer.go
    66  func (t *fourByteTracer) isPrecompiled(addr common.Address) bool {
    67  	for _, p := range t.activePrecompiles {
    68  		if p == addr {
    69  			return true
    70  		}
    71  	}
    72  	return false
    73  }
    74  
    75  // store saves the given identifier and datasize.
    76  func (t *fourByteTracer) store(id []byte, size int) {
    77  	key := bytesToHex(id) + "-" + strconv.Itoa(size)
    78  	t.ids[key] += 1
    79  }
    80  
    81  // CaptureStart implements the EVMLogger interface to initialize the tracing operation.
    82  func (t *fourByteTracer) CaptureStart(env vm.VMInterface, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *uint256.Int) {
    83  	// Update list of precompiles based on current block
    84  	rules := env.ChainConfig().Rules(env.Context().BlockNumber)
    85  	t.activePrecompiles = vm.ActivePrecompiles(rules)
    86  
    87  	// Save the outer calldata also
    88  	if len(input) >= 4 {
    89  		t.store(input[0:4], len(input)-4)
    90  	}
    91  }
    92  
    93  // CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
    94  func (t *fourByteTracer) CaptureEnter(op vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *uint256.Int) {
    95  	// Skip if tracing was interrupted
    96  	if atomic.LoadUint32(&t.interrupt) > 0 {
    97  		return
    98  	}
    99  	if len(input) < 4 {
   100  		return
   101  	}
   102  	// primarily we want to avoid CREATE/CREATE2/SELFDESTRUCT
   103  	if op != vm.DELEGATECALL && op != vm.STATICCALL &&
   104  		op != vm.CALL && op != vm.CALLCODE {
   105  		return
   106  	}
   107  	// Skip any pre-compile invocations, those are just fancy opcodes
   108  	if t.isPrecompiled(to) {
   109  		return
   110  	}
   111  	t.store(input[0:4], len(input)-4)
   112  }
   113  
   114  // GetResult returns the json-encoded nested list of call traces, and any
   115  // error arising from the encoding or forceful termination (via `Stop`).
   116  func (t *fourByteTracer) GetResult() (json.RawMessage, error) {
   117  	res, err := json.Marshal(t.ids)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	return res, t.reason
   122  }
   123  
   124  // Stop terminates execution of the tracer at the first opportune moment.
   125  func (t *fourByteTracer) Stop(err error) {
   126  	t.reason = err
   127  	atomic.StoreUint32(&t.interrupt, 1)
   128  }
   129  
   130  func bytesToHex(s []byte) string {
   131  	return "0x" + common.Bytes2Hex(s)
   132  }