/
eth_dynamic_fee_tx.go
204 lines (177 loc) · 5.96 KB
/
eth_dynamic_fee_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
package proto
import (
"io"
"math/big"
"github.com/pkg/errors"
"github.com/umbracle/fastrlp"
)
type EthereumDynamicFeeTx struct {
ChainID *big.Int // destination chain ID
Nonce uint64 // nonce of sender account
GasTipCap *big.Int // EIP-1559 value
GasFeeCap *big.Int // EIP-1559 value
Gas uint64 // gas limit
To *EthereumAddress // nil 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 *EthereumDynamicFeeTx) unmarshalFromFastRLP(value *fastrlp.Value) error {
const dynamicFeeTxFieldsCount = 12
elems, err := value.GetElems()
if err != nil {
return errors.Wrap(err, "failed to get elements array")
}
if len(elems) != dynamicFeeTxFieldsCount {
return errors.Errorf("expected %d elements, but recieved %d", dynamicFeeTxFieldsCount, 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 gasTipCap big.Int
if err := elems[2].GetBigInt(&gasTipCap); err != nil {
return errors.Wrap(err, "failed to parse gasTipCap")
}
var gasFeeCap big.Int
if err := elems[3].GetBigInt(&gasFeeCap); err != nil {
return errors.Wrap(err, "failed to parse gasFeeCap")
}
gasLimit, err := elems[4].GetUint64()
if err != nil {
return errors.Wrap(err, "failed to parse Gas")
}
addrTo, err := unmarshalTransactionToFieldFastRLP(elems[5])
if err != nil {
return errors.Wrap(err, "failed to parse TO field")
}
var weiAmount big.Int
if err := elems[6].GetBigInt(&weiAmount); err != nil {
return errors.Wrap(err, "failed to parse wei amount")
}
contractData, err := elems[7].Bytes()
if err != nil {
return errors.Wrap(err, "failed to parse contract invocation input data")
}
accessList, err := unmarshalAccessListFastRLP(elems[8])
if err != nil {
return errors.Wrap(err, "failed to parse accessList")
}
v, r, s, err := unmarshalSignatureValuesFastRLP(elems[9], elems[10], elems[11])
if err != nil {
return errors.Wrap(err, "failed to parse signature value")
}
*tx = EthereumDynamicFeeTx{
ChainID: &chainID,
Nonce: nonce,
GasTipCap: &gasTipCap,
GasFeeCap: &gasFeeCap,
Gas: gasLimit,
To: addrTo,
Value: &weiAmount,
Data: contractData,
AccessList: accessList,
V: &v,
R: &r,
S: &s,
}
return nil
}
func (tx *EthereumDynamicFeeTx) marshalToFastRLP(arena *fastrlp.Arena) *fastrlp.Value {
values := [...]*fastrlp.Value{
arena.NewBigInt(tx.ChainID),
arena.NewUint(tx.Nonce),
arena.NewBigInt(tx.GasTipCap),
arena.NewBigInt(tx.GasFeeCap),
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 *EthereumDynamicFeeTx) 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 EthereumDynamicFeeTx from RLP encoded data")
}
return nil
}
func (tx *EthereumDynamicFeeTx) 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 *EthereumDynamicFeeTx) copy() EthereumTxData {
return &EthereumDynamicFeeTx{
ChainID: copyBigInt(tx.ChainID),
Nonce: tx.Nonce,
GasTipCap: copyBigInt(tx.GasTipCap),
GasFeeCap: copyBigInt(tx.GasFeeCap),
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 *EthereumDynamicFeeTx) ethereumTxType() EthereumTxType { return EthereumDynamicFeeTxType }
func (tx *EthereumDynamicFeeTx) chainID() *big.Int { return tx.ChainID }
func (tx *EthereumDynamicFeeTx) accessList() EthereumAccessList { return tx.AccessList }
func (tx *EthereumDynamicFeeTx) data() []byte { return tx.Data }
func (tx *EthereumDynamicFeeTx) gas() uint64 { return tx.Gas }
func (tx *EthereumDynamicFeeTx) gasFeeCap() *big.Int { return tx.GasFeeCap }
func (tx *EthereumDynamicFeeTx) gasTipCap() *big.Int { return tx.GasTipCap }
func (tx *EthereumDynamicFeeTx) gasPrice() *big.Int { return tx.GasFeeCap }
func (tx *EthereumDynamicFeeTx) value() *big.Int { return tx.Value }
func (tx *EthereumDynamicFeeTx) nonce() uint64 { return tx.Nonce }
func (tx *EthereumDynamicFeeTx) to() *EthereumAddress { return tx.To }
func (tx *EthereumDynamicFeeTx) rawSignatureValues() (v, r, s *big.Int) {
return tx.V, tx.R, tx.S
}
func (tx *EthereumDynamicFeeTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
}
func (tx *EthereumDynamicFeeTx) signerHashFastRLP(chainID *big.Int, arena *fastrlp.Arena) *fastrlp.Value {
values := [...]*fastrlp.Value{
arena.NewBigInt(chainID),
arena.NewUint(tx.Nonce),
arena.NewBigInt(tx.GasTipCap),
arena.NewBigInt(tx.GasFeeCap),
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
}