-
Notifications
You must be signed in to change notification settings - Fork 74
/
tx_helper.go
426 lines (374 loc) · 10.5 KB
/
tx_helper.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package txs
import (
"errors"
"fmt"
"math/big"
"github.com/artela-network/artela-evm/vm"
sdkmath "cosmossdk.io/math"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core"
ethereum "github.com/ethereum/go-ethereum/core/types"
)
var (
_ TxData = &LegacyTx{}
_ TxData = &AccessListTx{}
_ TxData = &DynamicFeeTx{}
)
// TxData implements the Ethereum txs structure.
// See https://github.com/ethereum/go-ethereum/issues/23154
type TxData interface {
TxType() byte
Copy() TxData
GetChainID() *big.Int
GetAccessList() ethereum.AccessList
GetData() []byte
GetNonce() uint64
GetGas() uint64
GetGasPrice() *big.Int
GetGasTipCap() *big.Int
GetGasFeeCap() *big.Int
GetValue() *big.Int
GetTo() *common.Address
GetRawSignatureValues() (v, r, s *big.Int)
SetSignatureValues(v, r, s *big.Int)
SetChainId(chainID *big.Int)
AsEthereumData(stripCallData bool) ethereum.TxData
Validate() error
Fee() *big.Int
Cost() *big.Int
EffectiveGasPrice(baseFee *big.Int) *big.Int
EffectiveFee(baseFee *big.Int) *big.Int
EffectiveCost(baseFee *big.Int) *big.Int
}
// ===============================================================
// MsgEthereumTxResponse
// ===============================================================
// Failed returns if the contract execution failed in vm errors
func (m *MsgEthereumTxResponse) Failed() bool {
return len(m.VmError) > 0
}
// Return returns the data after execution if no error occurs
func (m *MsgEthereumTxResponse) Return() []byte {
if m.Failed() {
return nil
}
return common.CopyBytes(m.Ret)
}
// Revert returns the concrete revert reason if the execution is aborted by `REVERT` opcode
func (m *MsgEthereumTxResponse) Revert() []byte {
if m.VmError != vm.ErrExecutionReverted.Error() {
return nil
}
return common.CopyBytes(m.Ret)
}
// ===============================================================
// TransactionArgs
// ===============================================================
// TransactionArgs represents the arguments of a txs or message call
type TransactionArgs struct {
From *common.Address `json:"from"`
To *common.Address `json:"to"`
Gas *hexutil.Uint64 `json:"gas"`
GasPrice *hexutil.Big `json:"gasPrice"`
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"`
Value *hexutil.Big `json:"value"`
Nonce *hexutil.Uint64 `json:"nonce"`
// Issue detail: https://github.com/ethereum/go-ethereum/issues/15628
Data *hexutil.Bytes `json:"data"`
Input *hexutil.Bytes `json:"input"`
// Introduced by AccessListTxType txs.
AccessList *ethereum.AccessList `json:"accessList,omitempty"`
ChainID *hexutil.Big `json:"chainId,omitempty"`
}
// String returns the struct in a string format
func (args *TransactionArgs) String() string {
// Todo: There is currently a bug with hexutil.Big when the value its nil, printing would trigger an exception
return fmt.Sprintf("TransactionArgs{From:%v, To:%v, Gas:%v,"+
" Nonce:%v, Data:%v, Input:%v, AccessList:%v}",
args.From,
args.To,
args.Gas,
args.Nonce,
args.Data,
args.Input,
args.AccessList)
}
// GetFrom retrieves the transaction sender address.
func (args *TransactionArgs) GetFrom() common.Address {
if args.From == nil {
return common.Address{}
}
return *args.From
}
// GetData retrieves the transaction calldata. Input field is preferred.
func (args *TransactionArgs) GetData() []byte {
if args.Input != nil {
return *args.Input
}
if args.Data != nil {
return *args.Data
}
return nil
}
// ToTransaction converts the arguments to an ethereum transaction.
// This assumes that setTxDefaults has been called.
func (args *TransactionArgs) ToTransaction() *MsgEthereumTx {
var (
chainID, value, gasPrice, maxFeePerGas, maxPriorityFeePerGas sdkmath.Int
gas, nonce uint64
from, to string
)
// Set sender address or use zero address if none specified.
if args.ChainID != nil {
chainID = sdkmath.NewIntFromBigInt(args.ChainID.ToInt())
}
if args.Nonce != nil {
nonce = uint64(*args.Nonce)
}
if args.Gas != nil {
gas = uint64(*args.Gas)
}
if args.GasPrice != nil {
gasPrice = sdkmath.NewIntFromBigInt(args.GasPrice.ToInt())
}
if args.MaxFeePerGas != nil {
maxFeePerGas = sdkmath.NewIntFromBigInt(args.MaxFeePerGas.ToInt())
}
if args.MaxPriorityFeePerGas != nil {
maxPriorityFeePerGas = sdkmath.NewIntFromBigInt(args.MaxPriorityFeePerGas.ToInt())
}
if args.Value != nil {
value = sdkmath.NewIntFromBigInt(args.Value.ToInt())
}
if args.To != nil {
to = args.To.Hex()
}
var data TxData
switch {
case args.MaxFeePerGas != nil:
al := AccessList{}
if args.AccessList != nil {
al = NewAccessList(args.AccessList)
}
data = &DynamicFeeTx{
To: to,
ChainID: &chainID,
Nonce: nonce,
GasLimit: gas,
GasFeeCap: &maxFeePerGas,
GasTipCap: &maxPriorityFeePerGas,
Amount: &value,
Data: args.GetData(),
Accesses: al,
}
case args.AccessList != nil:
data = &AccessListTx{
To: to,
ChainID: &chainID,
Nonce: nonce,
GasLimit: gas,
GasPrice: &gasPrice,
Amount: &value,
Data: args.GetData(),
Accesses: NewAccessList(args.AccessList),
}
default:
data = &LegacyTx{
To: to,
Nonce: nonce,
GasLimit: gas,
GasPrice: &gasPrice,
Amount: &value,
Data: args.GetData(),
}
}
anyData, err := PackTxData(data)
if err != nil {
return nil
}
if args.From != nil {
from = args.From.Hex()
}
msg := MsgEthereumTx{
Data: anyData,
From: from,
}
msg.Hash = msg.AsTransaction().Hash().Hex()
return &msg
}
// ToMessage converts the arguments to the Message type used by the core evm.
// This assumes that setTxDefaults has been called.
func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (*core.Message, error) {
// Reject invalid combinations of pre- and post-1559 fee styles
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
return &core.Message{}, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
}
// Set sender address or use zero address if none specified.
addr := args.GetFrom()
// Set default gas & gas price if none were set
gas := globalGasCap
if gas == 0 {
gas = uint64(math.MaxUint64 / 2)
}
if args.Gas != nil {
gas = uint64(*args.Gas)
}
if globalGasCap != 0 && globalGasCap < gas {
gas = globalGasCap
}
var (
gasPrice *big.Int
gasFeeCap *big.Int
gasTipCap *big.Int
)
if baseFee == nil {
// If there's no basefee, then it must be a non-1559 execution
gasPrice = new(big.Int)
if args.GasPrice != nil {
gasPrice = args.GasPrice.ToInt()
}
gasFeeCap, gasTipCap = gasPrice, gasPrice
} else {
// A basefee is provided, necessitating 1559-type execution
if args.GasPrice != nil {
// User specified the legacy gas field, convert to 1559 gas typing
gasPrice = args.GasPrice.ToInt()
gasFeeCap, gasTipCap = gasPrice, gasPrice
} else {
// User specified 1559 gas feilds (or none), use those
gasFeeCap = new(big.Int)
if args.MaxFeePerGas != nil {
gasFeeCap = args.MaxFeePerGas.ToInt()
}
gasTipCap = new(big.Int)
if args.MaxPriorityFeePerGas != nil {
gasTipCap = args.MaxPriorityFeePerGas.ToInt()
}
// Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
gasPrice = new(big.Int)
if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 {
gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap)
}
}
}
value := new(big.Int)
if args.Value != nil {
value = args.Value.ToInt()
}
data := args.GetData()
var accessList ethereum.AccessList
if args.AccessList != nil {
accessList = *args.AccessList
}
nonce := uint64(0)
if args.Nonce != nil {
nonce = uint64(*args.Nonce)
}
msg := &core.Message{
From: addr,
To: args.To,
Nonce: nonce,
Value: value,
GasLimit: gas,
GasPrice: gasPrice,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Data: data,
AccessList: accessList,
SkipAccountChecks: true,
}
return msg, nil
}
// ===============================================================
// EvmTxArgs
// ===============================================================
// EvmTxArgs encapsulates all params for accessListTx, legacyTx, dynamicFeeTx
type EvmTxArgs struct {
Nonce uint64
GasLimit uint64
Input []byte
GasFeeCap *big.Int
GasPrice *big.Int
ChainID *big.Int
Amount *big.Int
GasTipCap *big.Int
To *common.Address
Accesses *ethereum.AccessList
}
// NewTx returns a reference to a new Ethereum txs message
func NewTx(
tx *EvmTxArgs,
) *MsgEthereumTx {
return newMsgEthereumTx(tx)
}
func newMsgEthereumTx(
tx *EvmTxArgs,
) *MsgEthereumTx {
var (
cid, amt, gp *sdkmath.Int
toAddr string
txData TxData
)
if tx.To != nil {
toAddr = tx.To.Hex()
}
if tx.Amount != nil {
amountInt := sdkmath.NewIntFromBigInt(tx.Amount)
amt = &amountInt
}
if tx.ChainID != nil {
chainIDInt := sdkmath.NewIntFromBigInt(tx.ChainID)
cid = &chainIDInt
}
if tx.GasPrice != nil {
gasPriceInt := sdkmath.NewIntFromBigInt(tx.GasPrice)
gp = &gasPriceInt
}
switch {
case tx.Accesses == nil:
txData = &LegacyTx{
To: toAddr,
Amount: amt,
GasPrice: gp,
Nonce: tx.Nonce,
GasLimit: tx.GasLimit,
Data: tx.Input,
}
case tx.Accesses != nil && tx.GasFeeCap != nil && tx.GasTipCap != nil:
gtc := sdkmath.NewIntFromBigInt(tx.GasTipCap)
gfc := sdkmath.NewIntFromBigInt(tx.GasFeeCap)
txData = &DynamicFeeTx{
ChainID: cid,
Amount: amt,
To: toAddr,
GasTipCap: >c,
GasFeeCap: &gfc,
Nonce: tx.Nonce,
GasLimit: tx.GasLimit,
Data: tx.Input,
Accesses: NewAccessList(tx.Accesses),
}
case tx.Accesses != nil:
txData = &AccessListTx{
ChainID: cid,
Nonce: tx.Nonce,
To: toAddr,
Amount: amt,
GasLimit: tx.GasLimit,
GasPrice: gp,
Data: tx.Input,
Accesses: NewAccessList(tx.Accesses),
}
default:
}
dataAny, err := PackTxData(txData)
if err != nil {
panic(err)
}
msg := MsgEthereumTx{Data: dataAny}
msg.Hash = msg.AsTransaction().Hash().Hex()
return &msg
}