|
1 | 1 | package types |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/ethereum/go-ethereum/common" |
5 | 4 | ethcmn "github.com/ethereum/go-ethereum/common" |
6 | 5 | ethtypes "github.com/ethereum/go-ethereum/core/types" |
7 | 6 | ) |
8 | 7 |
|
9 | | -// AccessListMappings is copied from go-ethereum |
10 | | -// https://github.com/ethereum/go-ethereum/blob/cf856ea1ad96ac39ea477087822479b63417036a/core/state/access_list.go#L23 |
11 | | -type AccessListMappings struct { |
12 | | - addresses map[common.Address]int |
13 | | - slots []map[common.Hash]struct{} |
14 | | -} |
15 | | - |
16 | | -// ContainsAddress returns true if the address is in the access list. |
17 | | -func (al *AccessListMappings) ContainsAddress(address common.Address) bool { |
18 | | - _, ok := al.addresses[address] |
19 | | - return ok |
20 | | -} |
21 | | - |
22 | | -// Contains checks if a slot within an account is present in the access list, returning |
23 | | -// separate flags for the presence of the account and the slot respectively. |
24 | | -func (al *AccessListMappings) Contains(address common.Address, slot common.Hash) (addressPresent bool, slotPresent bool) { |
25 | | - idx, ok := al.addresses[address] |
26 | | - if !ok { |
27 | | - // no such address (and hence zero slots) |
28 | | - return false, false |
29 | | - } |
30 | | - if idx == -1 { |
31 | | - // address yes, but no slots |
32 | | - return true, false |
33 | | - } |
34 | | - |
35 | | - if idx >= len(al.slots) { |
36 | | - // return in case of out-of-range |
37 | | - return true, false |
38 | | - } |
39 | | - |
40 | | - _, slotPresent = al.slots[idx][slot] |
41 | | - return true, slotPresent |
42 | | -} |
43 | | - |
44 | | -// newAccessList creates a new AccessListMappings. |
45 | | -func NewAccessListMappings() *AccessListMappings { |
46 | | - return &AccessListMappings{ |
47 | | - addresses: make(map[common.Address]int), |
48 | | - } |
49 | | -} |
50 | | - |
51 | | -// Copy creates an independent copy of an AccessListMappings. |
52 | | -func (al *AccessListMappings) Copy() *AccessListMappings { |
53 | | - cp := NewAccessListMappings() |
54 | | - for k, v := range al.addresses { |
55 | | - cp.addresses[k] = v |
56 | | - } |
57 | | - cp.slots = make([]map[common.Hash]struct{}, len(al.slots)) |
58 | | - for i, slotMap := range al.slots { |
59 | | - newSlotmap := make(map[common.Hash]struct{}, len(slotMap)) |
60 | | - for k := range slotMap { |
61 | | - newSlotmap[k] = struct{}{} |
62 | | - } |
63 | | - cp.slots[i] = newSlotmap |
64 | | - } |
65 | | - return cp |
66 | | -} |
67 | | - |
68 | | -// AddAddress adds an address to the access list, and returns 'true' if the operation |
69 | | -// caused a change (addr was not previously in the list). |
70 | | -func (al *AccessListMappings) AddAddress(address common.Address) bool { |
71 | | - if _, present := al.addresses[address]; present { |
72 | | - return false |
73 | | - } |
74 | | - al.addresses[address] = -1 |
75 | | - return true |
76 | | -} |
77 | | - |
78 | | -// AddSlot adds the specified (addr, slot) combo to the access list. |
79 | | -// Return values are: |
80 | | -// - address added |
81 | | -// - slot added |
82 | | -// For any 'true' value returned, a corresponding journal entry must be made. |
83 | | -func (al *AccessListMappings) AddSlot(address common.Address, slot common.Hash) (addrChange bool, slotChange bool) { |
84 | | - idx, addrPresent := al.addresses[address] |
85 | | - if !addrPresent || idx == -1 { |
86 | | - // Address not present, or addr present but no slots there |
87 | | - al.addresses[address] = len(al.slots) |
88 | | - slotmap := map[common.Hash]struct{}{slot: {}} |
89 | | - al.slots = append(al.slots, slotmap) |
90 | | - return !addrPresent, true |
91 | | - } |
92 | | - |
93 | | - if idx >= len(al.slots) { |
94 | | - // return in case of out-of-range |
95 | | - return false, false |
96 | | - } |
97 | | - |
98 | | - // There is already an (address,slot) mapping |
99 | | - slotmap := al.slots[idx] |
100 | | - if _, ok := slotmap[slot]; !ok { |
101 | | - slotmap[slot] = struct{}{} |
102 | | - // journal add slot change |
103 | | - return false, true |
104 | | - } |
105 | | - // No changes required |
106 | | - return false, false |
107 | | -} |
108 | | - |
109 | | -// DeleteSlot removes an (address, slot)-tuple from the access list. |
110 | | -// This operation needs to be performed in the same order as the addition happened. |
111 | | -// This method is meant to be used by the journal, which maintains ordering of |
112 | | -// operations. |
113 | | -func (al *AccessListMappings) DeleteSlot(address common.Address, slot common.Hash) { |
114 | | - idx, addrOk := al.addresses[address] |
115 | | - // There are two ways this can fail |
116 | | - if !addrOk { |
117 | | - panic("reverting slot change, address not present in list") |
118 | | - } |
119 | | - slotmap := al.slots[idx] |
120 | | - delete(slotmap, slot) |
121 | | - // If that was the last (first) slot, remove it |
122 | | - // Since additions and rollbacks are always performed in order, |
123 | | - // we can delete the item without worrying about screwing up later indices |
124 | | - if len(slotmap) == 0 { |
125 | | - al.slots = al.slots[:idx] |
126 | | - al.addresses[address] = -1 |
127 | | - } |
128 | | -} |
129 | | - |
130 | | -// DeleteAddress removes an address from the access list. This operation |
131 | | -// needs to be performed in the same order as the addition happened. |
132 | | -// This method is meant to be used by the journal, which maintains ordering of |
133 | | -// operations. |
134 | | -func (al *AccessListMappings) DeleteAddress(address common.Address) { |
135 | | - delete(al.addresses, address) |
136 | | -} |
137 | | - |
138 | 8 | // AccessList is an EIP-2930 access list that represents the slice of |
139 | 9 | // the protobuf AccessTuples. |
140 | 10 | type AccessList []AccessTuple |
|
0 commit comments