core/vm: rename

This commit is contained in:
Gary Rong 2026-06-17 09:51:22 +08:00
parent 906488333c
commit a08e1fa93c

View file

@ -66,10 +66,10 @@ type GasBudget struct {
UsedRegularGas uint64 // gross regular gas consumed in this frame UsedRegularGas uint64 // gross regular gas consumed in this frame
UsedStateGas int64 // signed net state-gas consumed in this frame UsedStateGas int64 // signed net state-gas consumed in this frame
// StateGasFromGasLeft tracks how much of this frame's regular gas (gas_left) // Spilled tracks how much of this frame's regular gas (gas_left)
// has been borrowed to cover state-gas charges that exceeded the reservoir // has been borrowed to cover state-gas charges that exceeded the
// (the "spillover"). // reservoir.
StateGasFromGasLeft uint64 Spilled uint64
} }
// NewGasBudget initializes a fresh GasBudget for execution / forwarding, // NewGasBudget initializes a fresh GasBudget for execution / forwarding,
@ -87,7 +87,7 @@ func (g GasBudget) Used(initial GasBudget) uint64 {
// String returns a visual representation of the budget. // String returns a visual representation of the budget.
func (g GasBudget) String() string { func (g GasBudget) String() string {
return fmt.Sprintf("<%v,%v,used=<%v,%v>,borrowed=%v>", g.RegularGas, g.StateGas, g.UsedRegularGas, g.UsedStateGas, g.StateGasFromGasLeft) return fmt.Sprintf("<%v,%v,used=<%v,%v>,borrowed=%v>", g.RegularGas, g.StateGas, g.UsedRegularGas, g.UsedStateGas, g.Spilled)
} }
// CanAfford reports whether the running balance can cover the given cost. // CanAfford reports whether the running balance can cover the given cost.
@ -125,7 +125,7 @@ func (g *GasBudget) Charge(cost GasCosts) (GasBudget, bool) {
// Record the regular gas borrowed to cover the overflowing state gas // Record the regular gas borrowed to cover the overflowing state gas
// so a later inline refund can repay it before topping up the reservoir. // so a later inline refund can repay it before topping up the reservoir.
g.StateGasFromGasLeft += spillover g.Spilled += spillover
} else { } else {
g.StateGas -= cost.StateGas g.StateGas -= cost.StateGas
} }
@ -157,7 +157,7 @@ func (g *GasBudget) IsZero() bool {
// RefundState applies an inline state-gas refund (e.g., SSTORE 0->A->0). // RefundState applies an inline state-gas refund (e.g., SSTORE 0->A->0).
// //
// Per EIP-8037, the refund repays the regular gas previously borrowed for // Per EIP-8037, the refund repays the regular gas previously borrowed for
// state-gas spillover (tracked by StateGasFromGasLeft) before crediting the // state-gas spillover (tracked by Spilled) before crediting the
// reservoir: it is returned to RegularGas up to the outstanding borrowed // reservoir: it is returned to RegularGas up to the outstanding borrowed
// amount, and only the remainder tops up StateGas. This keeps the regular and // amount, and only the remainder tops up StateGas. This keeps the regular and
// state pools from drifting into one another. // state pools from drifting into one another.
@ -165,14 +165,14 @@ func (g *GasBudget) IsZero() bool {
// The signed usage counter is decremented by the full refund regardless of the // The signed usage counter is decremented by the full refund regardless of the
// split, preserving the per-frame invariant: // split, preserving the per-frame invariant:
// //
// StateGas + UsedStateGas == initialStateGas + StateGasFromGasLeft // StateGas + UsedStateGas == initialStateGas + Spilled
// //
// which the revert and halt paths rely on for the correct gross refund. // which the revert and halt paths rely on for the correct gross refund.
func (g *GasBudget) RefundState(s uint64) { func (g *GasBudget) RefundState(s uint64) {
// Repay the borrowed regular gas first, capped at the outstanding amount. // Repay the borrowed regular gas first, capped at the outstanding amount.
repay := min(s, g.StateGasFromGasLeft) repay := min(s, g.Spilled)
g.RegularGas += repay g.RegularGas += repay
g.StateGasFromGasLeft -= repay g.Spilled -= repay
// Whatever is left tops up the reservoir. // Whatever is left tops up the reservoir.
g.StateGas += s - repay g.StateGas += s - repay
@ -225,20 +225,20 @@ func (g GasBudget) ExitSuccess() GasBudget {
// ExitRevert produces the leftover for a REVERT exit. The frame's state // ExitRevert produces the leftover for a REVERT exit. The frame's state
// changes are discarded, so all state gas it charged is refilled to its origin // changes are discarded, so all state gas it charged is refilled to its origin
// (EIP-8037): up to StateGasFromGasLeft is returned to RegularGas (the regular // (EIP-8037): up to Spilled is returned to RegularGas (the regular
// gas it borrowed), and the remainder restores the reservoir. Because the // gas it borrowed), and the remainder restores the reservoir. Because the
// borrowed regular gas is repaid first, the reservoir is made whole back to its // borrowed regular gas is repaid first, the reservoir is made whole back to its
// start-of-frame value: // start-of-frame value:
func (g GasBudget) ExitRevert() GasBudget { func (g GasBudget) ExitRevert() GasBudget {
reservoir := int64(g.StateGas) + g.UsedStateGas - int64(g.StateGasFromGasLeft) reservoir := int64(g.StateGas) + g.UsedStateGas - int64(g.Spilled)
if reservoir < 0 { if reservoir < 0 {
// Reservoir should never be negative. By construction it equals // Reservoir should never be negative. By construction it equals
// the initial state-gas allocation. // the initial state-gas allocation.
reservoir = 0 reservoir = 0
log.Warn("Negative reservoir at revert", "remaining", g.StateGas, "used", g.UsedStateGas, "borrowed", g.StateGasFromGasLeft) log.Warn("Negative reservoir at revert", "remaining", g.StateGas, "used", g.UsedStateGas, "borrowed", g.Spilled)
} }
return GasBudget{ return GasBudget{
RegularGas: g.RegularGas + g.StateGasFromGasLeft, RegularGas: g.RegularGas + g.Spilled,
StateGas: uint64(reservoir), StateGas: uint64(reservoir),
UsedRegularGas: g.UsedRegularGas, UsedRegularGas: g.UsedRegularGas,
UsedStateGas: 0, UsedStateGas: 0,
@ -252,17 +252,17 @@ func (g GasBudget) ExitRevert() GasBudget {
// with the rest of gas_left, leaving only the reservoir portion to survive, // with the rest of gas_left, leaving only the reservoir portion to survive,
// which equals the reservoir's value at the start of the frame. // which equals the reservoir's value at the start of the frame.
func (g GasBudget) ExitHalt() GasBudget { func (g GasBudget) ExitHalt() GasBudget {
reservoir := int64(g.StateGas) + g.UsedStateGas - int64(g.StateGasFromGasLeft) reservoir := int64(g.StateGas) + g.UsedStateGas - int64(g.Spilled)
if reservoir < 0 { if reservoir < 0 {
// Reservoir should never be negative. By construction it equals // Reservoir should never be negative. By construction it equals
// the initial state-gas allocation. // the initial state-gas allocation.
reservoir = 0 reservoir = 0
log.Warn("Negative reservoir at halt", "remaining", g.StateGas, "used", g.UsedStateGas, "borrowed", g.StateGasFromGasLeft) log.Warn("Negative reservoir at halt", "remaining", g.StateGas, "used", g.UsedStateGas, "borrowed", g.Spilled)
} }
return GasBudget{ return GasBudget{
RegularGas: 0, RegularGas: 0,
StateGas: uint64(reservoir), StateGas: uint64(reservoir),
UsedRegularGas: g.UsedRegularGas + g.RegularGas + g.StateGasFromGasLeft, UsedRegularGas: g.UsedRegularGas + g.RegularGas + g.Spilled,
UsedStateGas: 0, UsedStateGas: 0,
} }
} }
@ -294,6 +294,6 @@ func (g *GasBudget) Absorb(child GasBudget) {
g.StateGas = child.StateGas g.StateGas = child.StateGas
g.UsedStateGas += child.UsedStateGas g.UsedStateGas += child.UsedStateGas
g.UsedRegularGas -= child.StateGasFromGasLeft g.UsedRegularGas -= child.Spilled
g.StateGasFromGasLeft += child.StateGasFromGasLeft g.Spilled += child.Spilled
} }