-
Notifications
You must be signed in to change notification settings - Fork 76
/
tx_dynamic_fee.go
310 lines (255 loc) · 7.64 KB
/
tx_dynamic_fee.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
package txs
import (
"math/big"
"github.com/artela-network/aspect-core/djpm"
"github.com/artela-network/artela/ethereum/utils"
sdkmath "cosmossdk.io/math"
artela "github.com/artela-network/artela/ethereum/types"
"github.com/artela-network/artela/x/evm/types"
errorsmod "cosmossdk.io/errors"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
ethereum "github.com/ethereum/go-ethereum/core/types"
)
func newDynamicFeeTx(tx *ethereum.Transaction) (*DynamicFeeTx, error) {
txData := &DynamicFeeTx{
Nonce: tx.Nonce(),
Data: tx.Data(),
GasLimit: tx.Gas(),
}
v, r, s := tx.RawSignatureValues()
if to := tx.To(); to != nil {
txData.To = to.Hex()
}
if tx.Value() != nil {
amountInt, err := artela.SafeNewIntFromBigInt(tx.Value())
if err != nil {
return nil, err
}
txData.Amount = &amountInt
}
if tx.GasFeeCap() != nil {
gasFeeCapInt, err := artela.SafeNewIntFromBigInt(tx.GasFeeCap())
if err != nil {
return nil, err
}
txData.GasFeeCap = &gasFeeCapInt
}
if tx.GasTipCap() != nil {
gasTipCapInt, err := artela.SafeNewIntFromBigInt(tx.GasTipCap())
if err != nil {
return nil, err
}
txData.GasTipCap = &gasTipCapInt
}
if tx.AccessList() != nil {
al := tx.AccessList()
txData.Accesses = NewAccessList(&al)
}
if v != nil && r != nil && s != nil {
txData.SetSignatureValues(v, r, s)
}
txData.SetChainId(tx.ChainId())
return txData, nil
}
// TxType returns the txs type
func (tx *DynamicFeeTx) TxType() uint8 {
return ethereum.DynamicFeeTxType
}
// Copy returns an instance with the same field values
func (tx *DynamicFeeTx) Copy() TxData {
return &DynamicFeeTx{
ChainID: tx.ChainID,
Nonce: tx.Nonce,
GasTipCap: tx.GasTipCap,
GasFeeCap: tx.GasFeeCap,
GasLimit: tx.GasLimit,
To: tx.To,
Amount: tx.Amount,
Data: common.CopyBytes(tx.Data),
Accesses: tx.Accesses,
V: common.CopyBytes(tx.V),
R: common.CopyBytes(tx.R),
S: common.CopyBytes(tx.S),
}
}
// Validate performs a stateless validation of the txs fields.
func (tx DynamicFeeTx) Validate() error {
if tx.GasTipCap == nil {
return errorsmod.Wrap(types.ErrInvalidGasCap, "gas tip cap cannot nil")
}
if tx.GasFeeCap == nil {
return errorsmod.Wrap(types.ErrInvalidGasCap, "gas fee cap cannot nil")
}
if tx.GasTipCap.IsNegative() {
return errorsmod.Wrapf(types.ErrInvalidGasCap, "gas tip cap cannot be negative %s", tx.GasTipCap)
}
if tx.GasFeeCap.IsNegative() {
return errorsmod.Wrapf(types.ErrInvalidGasCap, "gas fee cap cannot be negative %s", tx.GasFeeCap)
}
if !artela.IsValidInt256(tx.GetGasTipCap()) {
return errorsmod.Wrap(types.ErrInvalidGasCap, "out of bound")
}
if !artela.IsValidInt256(tx.GetGasFeeCap()) {
return errorsmod.Wrap(types.ErrInvalidGasCap, "out of bound")
}
if tx.GasFeeCap.LT(*tx.GasTipCap) {
return errorsmod.Wrapf(
types.ErrInvalidGasCap, "max priority fee per gas higher than max fee per gas (%s > %s)",
tx.GasTipCap, tx.GasFeeCap,
)
}
if !artela.IsValidInt256(tx.Fee()) {
return errorsmod.Wrap(types.ErrInvalidGasFee, "out of bound")
}
amount := tx.GetValue()
// Amount can be 0
if amount != nil && amount.Sign() == -1 {
return errorsmod.Wrapf(types.ErrInvalidAmount, "amount cannot be negative %s", amount)
}
if !artela.IsValidInt256(amount) {
return errorsmod.Wrap(types.ErrInvalidAmount, "out of bound")
}
if tx.To != "" {
if err := artela.ValidateAddress(tx.To); err != nil {
return errorsmod.Wrap(err, "invalid to address")
}
}
chainID := tx.GetChainID()
if chainID == nil {
return errorsmod.Wrap(
errortypes.ErrInvalidChainID,
"chain ID must be present on AccessList txs",
)
}
if !(chainID.Cmp(big.NewInt(11820)) == 0 || chainID.Cmp(big.NewInt(11823)) == 0 || chainID.Cmp(big.NewInt(11822)) == 0 || chainID.Cmp(big.NewInt(11821)) == 0) {
return errorsmod.Wrapf(
errortypes.ErrInvalidChainID,
"chain ID must be 11822、11823、11821、11820 on Artela, got %s", chainID,
)
}
return nil
}
// GetChainID returns the chain id field from the DynamicFeeTx
func (tx *DynamicFeeTx) GetChainID() *big.Int {
if tx.ChainID == nil {
return nil
}
return tx.ChainID.BigInt()
}
// GetAccessList returns the AccessList field.
func (tx *DynamicFeeTx) GetAccessList() ethereum.AccessList {
if tx.Accesses == nil {
return nil
}
return *tx.Accesses.ToEthAccessList()
}
// GetData returns a copy of the input data bytes.
func (tx *DynamicFeeTx) GetData() []byte {
return common.CopyBytes(tx.Data)
}
// GetGas returns the gas limit.
func (tx *DynamicFeeTx) GetGas() uint64 {
return tx.GasLimit
}
// GetGasPrice returns the gas fee cap field.
func (tx *DynamicFeeTx) GetGasPrice() *big.Int {
return tx.GetGasFeeCap()
}
// GetGasTipCap returns the gas tip cap field.
func (tx *DynamicFeeTx) GetGasTipCap() *big.Int {
if tx.GasTipCap == nil {
return nil
}
return tx.GasTipCap.BigInt()
}
// GetGasFeeCap returns the gas fee cap field.
func (tx *DynamicFeeTx) GetGasFeeCap() *big.Int {
if tx.GasFeeCap == nil {
return nil
}
return tx.GasFeeCap.BigInt()
}
// Fee returns gasprice * gaslimit.
func (tx DynamicFeeTx) Fee() *big.Int {
return fee(tx.GetGasFeeCap(), tx.GasLimit)
}
// Cost returns amount + gasprice * gaslimit.
func (tx DynamicFeeTx) Cost() *big.Int {
return cost(tx.Fee(), tx.GetValue())
}
// EffectiveGasPrice returns the effective gas price
func (tx *DynamicFeeTx) EffectiveGasPrice(baseFee *big.Int) *big.Int {
return EffectiveGasPrice(baseFee, tx.GasFeeCap.BigInt(), tx.GasTipCap.BigInt())
}
// EffectiveFee returns effective_gasprice * gaslimit.
func (tx DynamicFeeTx) EffectiveFee(baseFee *big.Int) *big.Int {
return fee(tx.EffectiveGasPrice(baseFee), tx.GasLimit)
}
// EffectiveCost returns amount + effective_gasprice * gaslimit.
func (tx DynamicFeeTx) EffectiveCost(baseFee *big.Int) *big.Int {
return cost(tx.EffectiveFee(baseFee), tx.GetValue())
}
// GetValue returns the txs amount.
func (tx *DynamicFeeTx) GetValue() *big.Int {
if tx.Amount == nil {
return nil
}
return tx.Amount.BigInt()
}
// GetNonce returns the account sequence for the txs.
func (tx *DynamicFeeTx) GetNonce() uint64 { return tx.Nonce }
// GetTo returns the pointer to the recipient address.
func (tx *DynamicFeeTx) GetTo() *common.Address {
if tx.To == "" {
return nil
}
to := common.HexToAddress(tx.To)
return &to
}
// AsEthereumData returns an DynamicFeeTx txs txs from the proto-formatted
// TxData defined on the Cosmos EVM.
func (tx *DynamicFeeTx) AsEthereumData(stripCallData bool) ethereum.TxData {
v, r, s := tx.GetRawSignatureValues()
txData := ðereum.DynamicFeeTx{
ChainID: tx.GetChainID(),
Nonce: tx.GetNonce(),
GasTipCap: tx.GetGasTipCap(),
GasFeeCap: tx.GetGasFeeCap(),
Gas: tx.GetGas(),
To: tx.GetTo(),
Value: tx.GetValue(),
Data: tx.GetData(),
AccessList: tx.GetAccessList(),
V: v,
R: r,
S: s,
}
if stripCallData && utils.IsCustomizedVerification(ethereum.NewTx(txData)) {
_, txData.Data, _ = djpm.DecodeValidationAndCallData(tx.Data)
}
return txData
}
// GetRawSignatureValues returns the V, R, S signature values of the txs.
// The return values should not be modified by the caller.
func (tx *DynamicFeeTx) GetRawSignatureValues() (v, r, s *big.Int) {
return rawSignatureValues(tx.V, tx.R, tx.S)
}
// SetSignatureValues sets the signature values to the txs.
func (tx *DynamicFeeTx) SetSignatureValues(v, r, s *big.Int) {
if v != nil {
tx.V = v.Bytes()
}
if r != nil {
tx.R = r.Bytes()
}
if s != nil {
tx.S = s.Bytes()
}
}
func (tx *DynamicFeeTx) SetChainId(chainID *big.Int) {
if chainID != nil {
chainIDInt := sdkmath.NewIntFromBigInt(chainID)
tx.ChainID = &chainIDInt
}
}