/
eth_access_list_tx.go
282 lines (242 loc) · 8.14 KB
/
eth_access_list_tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package proto
import (
"io"
"math/big"
"github.com/pkg/errors"
"github.com/umbracle/fastrlp"
)
// EthereumAccessList is an EIP-2930 access list.
type EthereumAccessList []EthereumAccessTuple
func (al EthereumAccessList) copy() EthereumAccessList {
if al == nil {
return nil
}
cpy := make(EthereumAccessList, len(al))
copy(cpy, al)
return cpy
}
// EthereumAccessTuple is the element type of an access list.
type EthereumAccessTuple struct {
Address EthereumAddress `json:"address"`
StorageKeys []EthereumHash `json:"storageKeys"`
}
func (at *EthereumAccessTuple) unmarshalFromFastRLP(value *fastrlp.Value) error {
const accessTupleFieldsCount = 2
elems, err := value.GetElems()
if err != nil {
return errors.Wrapf(err, "expected RLP Array, but received %q", value.Type().String())
}
if len(elems) != accessTupleFieldsCount {
return errors.Errorf("expected %d elements, but recieved %d", accessTupleFieldsCount, len(elems))
}
var address EthereumAddress
if err := address.unmarshalFromFastRLP(elems[0]); err != nil {
return errors.Wrap(err, "failed to unmarshal EthereumAddress to fastRLP value for EthereumAccessTuple")
}
storageKeys, err := unmarshalHashesFromFastRLP(elems[1])
if err != nil {
return errors.Wrap(err, "failed to unmarshal StorageKeys to fastRLP value for EthereumAccessTuple")
}
*at = EthereumAccessTuple{
Address: address,
StorageKeys: storageKeys,
}
return nil
}
func (at *EthereumAccessTuple) marshalToFastRLP(arena *fastrlp.Arena) *fastrlp.Value {
array := arena.NewArray()
rlpAddr := at.Address.marshalToFastRLP(arena)
array.Set(rlpAddr)
storageKeys := marshalHashesToFastRLP(arena, at.StorageKeys)
array.Set(storageKeys)
return array
}
func unmarshalAccessListFastRLP(value *fastrlp.Value) (EthereumAccessList, error) {
elems, err := value.GetElems()
if err != nil {
return nil, errors.Wrap(err, "failed to get elements array")
}
// fast path
if len(elems) == 0 {
return nil, nil
}
hashes := make(EthereumAccessList, 0, len(elems))
for _, elem := range elems {
var accessTuple EthereumAccessTuple
if err := accessTuple.unmarshalFromFastRLP(elem); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal EthereumAccessTuple from fastRLP value")
}
hashes = append(hashes, accessTuple)
}
return hashes, nil
}
func marshalAccessListToFastRLP(arena *fastrlp.Arena, accessList EthereumAccessList) *fastrlp.Value {
array := arena.NewArray()
for _, accessTuple := range accessList {
val := accessTuple.marshalToFastRLP(arena)
array.Set(val)
}
return array
}
// EthereumAccessListTx is the data of EIP-2930 access list transactions.
type EthereumAccessListTx struct {
ChainID *big.Int // destination chain ID
Nonce uint64 // nonce of sender account
GasPrice *big.Int // wei per gas
Gas uint64 // gas limit
To *EthereumAddress // nil value means contract creation
Value *big.Int // wei amount
Data []byte // contract invocation input data
AccessList EthereumAccessList // EIP-2930 access list
V, R, S *big.Int // signature values
}
func (tx *EthereumAccessListTx) unmarshalFromFastRLP(value *fastrlp.Value) error {
const accessListTxFieldsCount = 11
elems, err := value.GetElems()
if err != nil {
return errors.Wrap(err, "failed to get elements array")
}
if len(elems) != accessListTxFieldsCount {
return errors.Errorf("expected %d elements, but recieved %d", accessListTxFieldsCount, len(elems))
}
var chainID big.Int
if err := elems[0].GetBigInt(&chainID); err != nil {
return errors.Wrap(err, "failed to parse ChainID")
}
nonce, err := elems[1].GetUint64()
if err != nil {
return errors.Wrap(err, "failed to parse nonce")
}
var gasPrice big.Int
if err := elems[2].GetBigInt(&gasPrice); err != nil {
return errors.Wrap(err, "failed to parse GasPrice")
}
gasLimit, err := elems[3].GetUint64()
if err != nil {
return errors.Wrap(err, "failed to parse Gas")
}
addrTo, err := unmarshalTransactionToFieldFastRLP(elems[4])
if err != nil {
return errors.Wrap(err, "failed to parse TO field")
}
var weiAmount big.Int
if err := elems[5].GetBigInt(&weiAmount); err != nil {
return errors.Wrap(err, "failed to parse wei amount")
}
contractData, err := elems[6].Bytes()
if err != nil {
return errors.Wrap(err, "failed to parse contract invocation input data")
}
accessList, err := unmarshalAccessListFastRLP(elems[7])
if err != nil {
return errors.Wrap(err, "failed to parse accessList")
}
v, r, s, err := unmarshalSignatureValuesFastRLP(elems[8], elems[9], elems[10])
if err != nil {
return errors.Wrap(err, "failed to parse signature value")
}
*tx = EthereumAccessListTx{
ChainID: &chainID,
Nonce: nonce,
GasPrice: &gasPrice,
Gas: gasLimit,
To: addrTo,
Value: &weiAmount,
Data: contractData,
AccessList: accessList,
V: &v,
R: &r,
S: &s,
}
return nil
}
func (tx *EthereumAccessListTx) marshalToFastRLP(arena *fastrlp.Arena) *fastrlp.Value {
values := [...]*fastrlp.Value{
arena.NewBigInt(tx.ChainID),
arena.NewUint(tx.Nonce),
arena.NewBigInt(tx.GasPrice),
arena.NewUint(tx.Gas),
arena.NewBytes(tx.To.tryToBytes()),
arena.NewBigInt(tx.Value),
arena.NewBytes(tx.Data),
marshalAccessListToFastRLP(arena, tx.AccessList),
arena.NewBigInt(tx.V),
arena.NewBigInt(tx.R),
arena.NewBigInt(tx.S),
}
array := arena.NewArray()
for _, value := range values {
array.Set(value)
}
return array
}
func (tx *EthereumAccessListTx) DecodeRLP(rlpData []byte) error {
parser := fastrlp.Parser{}
rlpVal, err := parser.Parse(rlpData)
if err != nil {
return err
}
if err := tx.unmarshalFromFastRLP(rlpVal); err != nil {
return errors.Wrap(err, "failed to parse EthereumAccessListTx from RLP encoded data")
}
return nil
}
func (tx *EthereumAccessListTx) EncodeRLP(w io.Writer) error {
arena := fastrlp.Arena{}
rlpVal := tx.marshalToFastRLP(&arena)
rlpData := rlpVal.MarshalTo(nil)
if _, err := w.Write(rlpData); err != nil {
return err
}
return nil
}
func (tx *EthereumAccessListTx) copy() EthereumTxData {
return &EthereumAccessListTx{
ChainID: copyBigInt(tx.ChainID),
Nonce: tx.Nonce,
GasPrice: copyBigInt(tx.GasPrice),
Gas: tx.Gas,
To: tx.To.copy(),
Value: copyBigInt(tx.Value),
Data: copyBytes(tx.Data),
AccessList: tx.AccessList.copy(),
V: copyBigInt(tx.V),
R: copyBigInt(tx.R),
S: copyBigInt(tx.S),
}
}
// accessors for innerTx.
func (tx *EthereumAccessListTx) ethereumTxType() EthereumTxType { return EthereumAccessListTxType }
func (tx *EthereumAccessListTx) chainID() *big.Int { return tx.ChainID }
func (tx *EthereumAccessListTx) accessList() EthereumAccessList { return tx.AccessList }
func (tx *EthereumAccessListTx) data() []byte { return tx.Data }
func (tx *EthereumAccessListTx) gas() uint64 { return tx.Gas }
func (tx *EthereumAccessListTx) gasPrice() *big.Int { return tx.GasPrice }
func (tx *EthereumAccessListTx) gasTipCap() *big.Int { return tx.GasPrice }
func (tx *EthereumAccessListTx) gasFeeCap() *big.Int { return tx.GasPrice }
func (tx *EthereumAccessListTx) value() *big.Int { return tx.Value }
func (tx *EthereumAccessListTx) nonce() uint64 { return tx.Nonce }
func (tx *EthereumAccessListTx) to() *EthereumAddress { return tx.To }
func (tx *EthereumAccessListTx) rawSignatureValues() (v, r, s *big.Int) {
return tx.V, tx.R, tx.S
}
func (tx *EthereumAccessListTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
}
func (tx *EthereumAccessListTx) signerHashFastRLP(chainID *big.Int, arena *fastrlp.Arena) *fastrlp.Value {
values := [...]*fastrlp.Value{
arena.NewBigInt(chainID),
arena.NewUint(tx.Nonce),
arena.NewBigInt(tx.GasPrice),
arena.NewUint(tx.Gas),
arena.NewBytes(tx.To.tryToBytes()),
arena.NewBigInt(tx.Value),
arena.NewBytes(tx.Data),
marshalAccessListToFastRLP(arena, tx.AccessList),
}
array := arena.NewArray()
for _, value := range values {
array.Set(value)
}
return array
}