access_list_tracer.go 6.1 KB

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