forked from umbracle/ethgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
structs_marshal_rlp.go
286 lines (251 loc) · 5.93 KB
/
structs_marshal_rlp.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
283
284
285
286
package ethgo
import (
"fmt"
"math/big"
"github.com/umbracle/fastrlp"
)
// GetHash returns the Hash of the transaction
func (t *Transaction) GetHash() (hash Hash, err error) {
var rlpEncode []byte
if rlpEncode, err = t.MarshalRLPTo(nil); err != nil {
return Hash{}, err
}
return BytesToHash(Keccak256(rlpEncode)), nil
}
// MarshalRLPTo marshals the transaction to a []byte destination
func (t *Transaction) MarshalRLPTo(dst []byte) ([]byte, error) {
raw, err := fastrlp.MarshalRLP(t)
if err != nil {
return nil, err
}
if t.Type == TransactionLegacy {
return raw, nil
}
// append type byte
return append([]byte{byte(t.Type)}, raw...), nil
}
// MarshalRLPWith marshals the transaction to RLP with a specific fastrlp.Arena
func (t *Transaction) MarshalRLPWith(arena *fastrlp.Arena) (*fastrlp.Value, error) {
vv := arena.NewArray()
if t.Type != 0 {
// either dynamic and access type
vv.Set(arena.NewBigInt(t.ChainID))
}
vv.Set(arena.NewUint(t.Nonce))
if t.Type == TransactionDynamicFee {
// dynamic fee uses
vv.Set(arena.NewBigInt(t.MaxPriorityFeePerGas))
vv.Set(arena.NewBigInt(t.MaxFeePerGas))
} else {
// legacy and access type use gas price
vv.Set(arena.NewUint(t.GasPrice))
}
vv.Set(arena.NewUint(t.Gas))
// Address may be empty
if t.To != nil {
vv.Set(arena.NewBytes((*t.To)[:]))
} else {
vv.Set(arena.NewNull())
}
vv.Set(arena.NewBigInt(t.Value))
vv.Set(arena.NewCopyBytes(t.Input))
if t.Type != 0 {
// either dynamic and access type
accessList, err := t.AccessList.MarshalRLPWith(arena)
if err != nil {
return nil, err
}
vv.Set(accessList)
}
// signature values
vv.Set(arena.NewCopyBytes(t.V))
vv.Set(arena.NewCopyBytes(t.R))
vv.Set(arena.NewCopyBytes(t.S))
if t.Type == TransactionLegacy {
return vv, nil
}
return vv, nil
}
func (t *Transaction) UnmarshalRLP(buf []byte) error {
t.Hash = BytesToHash(Keccak256(buf))
if len(buf) < 1 {
return fmt.Errorf("expecting 1 byte but 0 byte provided")
}
if buf[0] <= 0x7f {
// it includes a type byte
switch typ := buf[0]; typ {
case 1:
t.Type = TransactionAccessList
case 2:
t.Type = TransactionDynamicFee
default:
return fmt.Errorf("type byte %d not found", typ)
}
buf = buf[1:]
}
if err := fastrlp.UnmarshalRLP(buf, t); err != nil {
return err
}
return nil
}
func (t *Transaction) UnmarshalRLPWith(v *fastrlp.Value) error {
elems, err := v.GetElems()
if err != nil {
return err
}
getElem := func() *fastrlp.Value {
v := elems[0]
elems = elems[1:]
return v
}
var num int
switch t.Type {
case TransactionLegacy:
num = 9
case TransactionAccessList:
// legacy + chain id + access list
num = 11
case TransactionDynamicFee:
// access list txn + gas fee 1 + gas fee 2 - gas price
num = 12
default:
return fmt.Errorf("transaction type %d not found", t.Type)
}
if numElems := len(elems); numElems != num {
return fmt.Errorf("not enough elements to decode transaction, expected %d but found %d", num, numElems)
}
if t.Type != 0 {
t.ChainID = new(big.Int)
if err := getElem().GetBigInt(t.ChainID); err != nil {
return err
}
}
// nonce
if t.Nonce, err = getElem().GetUint64(); err != nil {
return err
}
if t.Type == TransactionDynamicFee {
// dynamic fee uses
t.MaxPriorityFeePerGas = new(big.Int)
if err := getElem().GetBigInt(t.MaxPriorityFeePerGas); err != nil {
return err
}
t.MaxFeePerGas = new(big.Int)
if err := getElem().GetBigInt(t.MaxFeePerGas); err != nil {
return err
}
} else {
// legacy and access type use gas price
if t.GasPrice, err = getElem().GetUint64(); err != nil {
return err
}
}
// gas
if t.Gas, err = getElem().GetUint64(); err != nil {
return err
}
// to
vv, _ := getElem().Bytes()
if len(vv) == 20 {
// address
addr := BytesToAddress(vv)
t.To = &addr
} else {
// reset To
t.To = nil
}
// value
t.Value = new(big.Int)
if err := getElem().GetBigInt(t.Value); err != nil {
return err
}
// input
if t.Input, err = getElem().GetBytes(t.Input[:0]); err != nil {
return err
}
if t.Type != 0 {
if err := t.AccessList.UnmarshalRLPWith(getElem()); err != nil {
return err
}
}
// V
if t.V, err = getElem().GetBytes(t.V); err != nil {
return err
}
// R
if t.R, err = getElem().GetBytes(t.R); err != nil {
return err
}
// S
if t.S, err = getElem().GetBytes(t.S); err != nil {
return err
}
return nil
}
func (a *AccessList) MarshalRLPTo(dst []byte) ([]byte, error) {
return fastrlp.MarshalRLP(a)
}
func (a *AccessList) MarshalRLPWith(arena *fastrlp.Arena) (*fastrlp.Value, error) {
if len(*a) == 0 {
return arena.NewNullArray(), nil
}
v := arena.NewArray()
for _, i := range *a {
acct := arena.NewArray()
acct.Set(arena.NewCopyBytes(i.Address[:]))
if len(i.Storage) == 0 {
acct.Set(arena.NewNullArray())
} else {
strV := arena.NewArray()
for _, v := range i.Storage {
strV.Set(arena.NewCopyBytes(v[:]))
}
acct.Set(strV)
}
v.Set(acct)
}
return v, nil
}
func (a *AccessList) UnmarshalRLP(buf []byte) error {
return fastrlp.UnmarshalRLP(buf, a)
}
func (a *AccessList) UnmarshalRLPWith(v *fastrlp.Value) error {
if v.Type() == fastrlp.TypeArrayNull {
// empty
return nil
}
elems, err := v.GetElems()
if err != nil {
return err
}
for _, elem := range elems {
entry := AccessEntry{}
acctElems, err := elem.GetElems()
if err != nil {
return err
}
if len(acctElems) != 2 {
return fmt.Errorf("two elems expected but %d found", len(acctElems))
}
// decode 'address'
if err = acctElems[0].GetAddr(entry.Address[:]); err != nil {
return err
}
// decode 'storage'
if acctElems[1].Type() != fastrlp.TypeArrayNull {
storageElems, err := acctElems[1].GetElems()
if err != nil {
return err
}
entry.Storage = make([]Hash, len(storageElems))
for indx, storage := range storageElems {
// decode storage
if err = storage.GetHash(entry.Storage[indx][:]); err != nil {
return err
}
}
}
(*a) = append((*a), entry)
}
return nil
}