|
17 | 17 | package core |
18 | 18 |
|
19 | 19 | import ( |
20 | | - "math/big" |
21 | | - |
22 | | - "github.com/holiman/uint256" |
23 | | - |
24 | 20 | "github.com/ava-labs/libevm/log" |
25 | 21 | "github.com/ava-labs/libevm/params" |
26 | 22 | ) |
@@ -50,39 +46,16 @@ func (st *StateTransition) canExecuteTransaction() error { |
50 | 46 |
|
51 | 47 | // consumeMinimumGas updates the gas remaining to reflect the value returned by |
52 | 48 | // [params.RulesHooks.MinimumGasConsumption]. It MUST be called after all code |
53 | | -// that modifies gas consumption; i.e. `st.gasRemaining` MUST remain constant |
54 | | -// after consumeMinimumGas returns. |
| 49 | +// that modifies gas consumption but before the balance is returned for |
| 50 | +// remaining gas. |
55 | 51 | func (st *StateTransition) consumeMinimumGas() { |
56 | 52 | limit := st.msg.GasLimit |
57 | | - minConsume := st.rulesHooks().MinimumGasConsumption(st.msg.GasLimit) |
58 | | - minConsume = min(minConsume, limit) // as documented in [params.RulesHooks] |
59 | | - |
60 | | - maxRemaining := limit - minConsume |
61 | | - if st.gasRemaining < maxRemaining { |
62 | | - return |
63 | | - } |
64 | | - |
65 | | - diff := st.gasRemaining - maxRemaining |
66 | | - st.gasRemaining -= diff |
67 | | - if err := st.gp.SubGas(diff); err != nil { |
68 | | - // This would mean that the transaction wouldn't have been able to spend |
69 | | - // up to its limit. |
70 | | - log.Crit( |
71 | | - "Broken gas-charging invariant", |
72 | | - "tx limit", limit, |
73 | | - "min consume", minConsume, |
74 | | - "extra consume", diff, |
75 | | - "SubGas() error", err, |
76 | | - ) |
77 | | - } |
78 | | - |
79 | | - spend := new(big.Int).Mul(st.msg.GasPrice, new(big.Int).SetUint64(diff)) |
80 | | - st.state.SubBalance(st.msg.From, uint256.MustFromBig(spend)) |
81 | | - |
82 | | - log.Debug( |
83 | | - "Consumed extra gas to enforce minimum", |
84 | | - "tx_limit", limit, |
85 | | - "min_consumption", minConsume, |
86 | | - "extra_consumption", diff, |
| 53 | + minConsume := min( |
| 54 | + limit, // as documented in [params.RulesHooks] |
| 55 | + st.rulesHooks().MinimumGasConsumption(limit), |
| 56 | + ) |
| 57 | + st.gasRemaining = min( |
| 58 | + st.gasRemaining, |
| 59 | + limit-minConsume, |
87 | 60 | ) |
88 | 61 | } |
0 commit comments