github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/eth/tracers/logger/access_list_tracer.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 logger
    18  
    19  import (
    20  	"maps"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/core/tracing"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/core/vm"
    26  )
    27  
    28  // accessList is an accumulator for the set of accounts and storage slots an EVM
    29  // contract execution touches.
    30  type accessList map[common.Address]accessListSlots
    31  
    32  // accessListSlots is an accumulator for the set of storage slots within a single
    33  // contract that an EVM contract execution touches.
    34  type accessListSlots map[common.Hash]struct{}
    35  
    36  // newAccessList creates a new accessList.
    37  func newAccessList() accessList {
    38  	return make(map[common.Address]accessListSlots)
    39  }
    40  
    41  // addAddress adds an address to the accesslist.
    42  func (al accessList) addAddress(address common.Address) {
    43  	// Set address if not previously present
    44  	if _, present := al[address]; !present {
    45  		al[address] = make(map[common.Hash]struct{})
    46  	}
    47  }
    48  
    49  // addSlot adds a storage slot to the accesslist.
    50  func (al accessList) addSlot(address common.Address, slot common.Hash) {
    51  	// Set address if not previously present
    52  	al.addAddress(address)
    53  
    54  	// Set the slot on the surely existent storage set
    55  	al[address][slot] = struct{}{}
    56  }
    57  
    58  // equal checks if the content of the current access list is the same as the
    59  // content of the other one.
    60  func (al accessList) equal(other accessList) bool {
    61  	// Cross reference the accounts first
    62  	if len(al) != len(other) {
    63  		return false
    64  	}
    65  	// Given that len(al) == len(other), we only need to check that
    66  	// all the items from al are in other.
    67  	for addr := range al {
    68  		if _, ok := other[addr]; !ok {
    69  			return false
    70  		}
    71  	}
    72  
    73  	// Accounts match, cross reference the storage slots too
    74  	for addr, slots := range al {
    75  		otherslots := other[addr]
    76  		if !maps.Equal(slots, otherslots) {
    77  			return false
    78  		}
    79  	}
    80  	return true
    81  }
    82  
    83  // accessList converts the accesslist to a types.AccessList.
    84  func (al accessList) accessList() types.AccessList {
    85  	acl := make(types.AccessList, 0, len(al))
    86  	for addr, slots := range al {
    87  		tuple := types.AccessTuple{Address: addr, StorageKeys: []common.Hash{}}
    88  		for slot := range slots {
    89  			tuple.StorageKeys = append(tuple.StorageKeys, slot)
    90  		}
    91  		acl = append(acl, tuple)
    92  	}
    93  	return acl
    94  }
    95  
    96  // AccessListTracer is a tracer that accumulates touched accounts and storage
    97  // slots into an internal set.
    98  type AccessListTracer struct {
    99  	excl map[common.Address]struct{} // Set of account to exclude from the list
   100  	list accessList                  // Set of accounts and storage slots touched
   101  }
   102  
   103  // NewAccessListTracer creates a new tracer that can generate AccessLists.
   104  // An optional AccessList can be specified to occupy slots and addresses in
   105  // the resulting accesslist.
   106  func NewAccessListTracer(acl types.AccessList, from, to common.Address, precompiles []common.Address) *AccessListTracer {
   107  	excl := map[common.Address]struct{}{
   108  		from: {}, to: {},
   109  	}
   110  	for _, addr := range precompiles {
   111  		excl[addr] = struct{}{}
   112  	}
   113  	list := newAccessList()
   114  	for _, al := range acl {
   115  		if _, ok := excl[al.Address]; !ok {
   116  			list.addAddress(al.Address)
   117  		}
   118  		for _, slot := range al.StorageKeys {
   119  			list.addSlot(al.Address, slot)
   120  		}
   121  	}
   122  	return &AccessListTracer{
   123  		excl: excl,
   124  		list: list,
   125  	}
   126  }
   127  
   128  func (a *AccessListTracer) Hooks() *tracing.Hooks {
   129  	return &tracing.Hooks{
   130  		OnOpcode: a.OnOpcode,
   131  	}
   132  }
   133  
   134  // OnOpcode captures all opcodes that touch storage or addresses and adds them to the accesslist.
   135  func (a *AccessListTracer) OnOpcode(pc uint64, opcode byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
   136  	stackData := scope.StackData()
   137  	stackLen := len(stackData)
   138  	op := vm.OpCode(opcode)
   139  	if (op == vm.SLOAD || op == vm.SSTORE) && stackLen >= 1 {
   140  		slot := common.Hash(stackData[stackLen-1].Bytes32())
   141  		a.list.addSlot(scope.Address(), slot)
   142  	}
   143  	if (op == vm.EXTCODECOPY || op == vm.EXTCODEHASH || op == vm.EXTCODESIZE || op == vm.BALANCE || op == vm.SELFDESTRUCT) && stackLen >= 1 {
   144  		addr := common.Address(stackData[stackLen-1].Bytes20())
   145  		if _, ok := a.excl[addr]; !ok {
   146  			a.list.addAddress(addr)
   147  		}
   148  	}
   149  	if (op == vm.DELEGATECALL || op == vm.CALL || op == vm.STATICCALL || op == vm.CALLCODE) && stackLen >= 5 {
   150  		addr := common.Address(stackData[stackLen-2].Bytes20())
   151  		if _, ok := a.excl[addr]; !ok {
   152  			a.list.addAddress(addr)
   153  		}
   154  	}
   155  }
   156  
   157  // AccessList returns the current accesslist maintained by the tracer.
   158  func (a *AccessListTracer) AccessList() types.AccessList {
   159  	return a.list.accessList()
   160  }
   161  
   162  // Equal returns if the content of two access list traces are equal.
   163  func (a *AccessListTracer) Equal(other *AccessListTracer) bool {
   164  	return a.list.equal(other.list)
   165  }