-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathaa_gas.go
More file actions
95 lines (76 loc) · 2.75 KB
/
aa_gas.go
File metadata and controls
95 lines (76 loc) · 2.75 KB
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
package aa
import (
"fmt"
"github.com/holiman/uint256"
"github.com/erigontech/erigon/common"
"github.com/erigontech/erigon/common/u256"
"github.com/erigontech/erigon/execution/protocol"
"github.com/erigontech/erigon/execution/protocol/params"
"github.com/erigontech/erigon/execution/state"
"github.com/erigontech/erigon/execution/tracing"
"github.com/erigontech/erigon/execution/types"
"github.com/erigontech/erigon/execution/types/accounts"
)
func chargeGas(
header *types.Header,
tx *types.AccountAbstractionTransaction,
gasPool *protocol.GasPool,
ibs *state.IntraBlockState,
preTxCost uint64,
) error {
baseFee := header.BaseFee
effectiveGasTip := tx.GetEffectiveGasTip(baseFee)
effectiveGasPrice := new(uint256.Int).Add(baseFee, &effectiveGasTip)
totalGasLimit := preTxCost + tx.ValidationGasLimit + tx.PaymasterValidationGasLimit + tx.GasLimit + tx.PostOpGasLimit
preCharge := new(uint256.Int).SetUint64(totalGasLimit)
preCharge = preCharge.Mul(preCharge, effectiveGasPrice)
chargeFrom := tx.GasPayer()
balance, err := ibs.GetBalance(chargeFrom)
if err != nil {
return err
}
if balance.Cmp(preCharge) < 0 {
return fmt.Errorf("%w: RIP-7560 address %v have %v want %v", protocol.ErrInsufficientFunds, chargeFrom.String(), &balance, preCharge)
}
if err := ibs.SubBalance(chargeFrom, *preCharge, 0); err != nil {
return err
}
if err := gasPool.SubGas(totalGasLimit); err != nil {
return newValidationPhaseError(err, nil, "block gas limit", false)
}
return nil
}
func refundGas(
header *types.Header,
tx *types.AccountAbstractionTransaction,
ibs *state.IntraBlockState,
gasUsed uint64,
) error {
baseFee := header.BaseFee
effectiveGasTip := tx.GetEffectiveGasTip(baseFee)
effectiveGasPrice := new(uint256.Int).Add(baseFee, &effectiveGasTip)
actualGasCost := new(uint256.Int).Mul(effectiveGasPrice, new(uint256.Int).SetUint64(gasUsed))
totalGasLimit := params.TxAAGas + tx.ValidationGasLimit + tx.PaymasterValidationGasLimit + tx.GasLimit + tx.PostOpGasLimit
preCharge := new(uint256.Int).SetUint64(totalGasLimit)
preCharge = preCharge.Mul(preCharge, effectiveGasPrice)
refund := new(uint256.Int).Sub(preCharge, actualGasCost)
chargeFrom := tx.GasPayer()
if err := ibs.AddBalance(chargeFrom, *refund, tracing.BalanceIncreaseGasReturn); err != nil {
return err
}
return nil
}
func payCoinbase(
header *types.Header,
tx *types.AccountAbstractionTransaction,
ibs *state.IntraBlockState,
gasUsed uint64,
coinbase common.Address,
) error {
baseFee := header.BaseFee
effectiveTip := u256.Num0
if tx.FeeCap.Gt(baseFee) {
effectiveTip = u256.Min(*tx.Tip, u256.Sub(*tx.FeeCap, *baseFee))
}
return ibs.AddBalance(accounts.InternAddress(coinbase), u256.Mul(u256.U64(gasUsed), effectiveTip), tracing.BalanceIncreaseRewardTransactionFee)
}