-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat: support executing sub-calls with a different gas limit #25303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
8d642dc
4622253
5832919
0ddeccf
efdbdee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package types | ||
|
|
||
| import ( | ||
| fmt "fmt" | ||
| ) | ||
|
|
||
| var _ GasMeter = &ProxyGasMeter{} | ||
|
|
||
| // ProxyGasMeter wraps another GasMeter, but enforces a lower gas limit. | ||
| // Gas consumption is delegated to the wrapped GasMeter, so it won't risk losing gas accounting compared to standalone | ||
| // gas meter. | ||
| type ProxyGasMeter struct { | ||
| GasMeter | ||
|
|
||
| limit Gas | ||
| } | ||
|
|
||
| // NewProxyGasMeter returns a new GasMeter which wraps the provided gas meter. | ||
| // The remaining is the maximum gas that can be consumed on top of current consumed | ||
| // gas of the wrapped gas meter. | ||
| // | ||
| // If the new remaining is greater than or equal to the existing remaining gas, no wrapping is needed | ||
| // and the original gas meter is returned. | ||
| func NewProxyGasMeter(gasMeter GasMeter, remaining Gas) GasMeter { | ||
| if remaining >= gasMeter.GasRemaining() { | ||
| return gasMeter | ||
| } | ||
|
|
||
| return &ProxyGasMeter{ | ||
| GasMeter: gasMeter, | ||
| limit: remaining + gasMeter.GasConsumed(), | ||
| } | ||
| } | ||
|
|
||
| func (pgm ProxyGasMeter) GasRemaining() Gas { | ||
| if pgm.IsPastLimit() { | ||
| return 0 | ||
| } | ||
| return pgm.limit - pgm.GasConsumed() | ||
| } | ||
|
|
||
| func (pgm ProxyGasMeter) Limit() Gas { | ||
| return pgm.limit | ||
| } | ||
|
|
||
| func (pgm ProxyGasMeter) IsPastLimit() bool { | ||
| return pgm.GasConsumed() > pgm.limit | ||
| } | ||
|
|
||
| func (pgm ProxyGasMeter) IsOutOfGas() bool { | ||
| return pgm.GasConsumed() >= pgm.limit | ||
| } | ||
|
|
||
| func (pgm ProxyGasMeter) ConsumeGas(amount Gas, descriptor string) { | ||
| consumed, overflow := addUint64Overflow(pgm.GasMeter.GasConsumed(), amount) | ||
yihuang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if overflow { | ||
| panic(ErrorGasOverflow{Descriptor: descriptor}) | ||
|
||
| } | ||
|
|
||
| if consumed > pgm.limit { | ||
| panic(ErrorOutOfGas{Descriptor: descriptor}) | ||
|
||
| } | ||
|
|
||
| pgm.GasMeter.ConsumeGas(amount, descriptor) | ||
| } | ||
|
|
||
|
Comment on lines
39
to
43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix CI lint failure (staticcheck QF1008) by removing the embedded selector. This is currently failing the lint job; use the promoted method instead. -func (pgm ProxyGasMeter) ConsumeGas(amount Gas, descriptor string) {
- consumed, overflow := addUint64Overflow(pgm.GasMeter.GasConsumed(), amount)
+func (pgm ProxyGasMeter) ConsumeGas(amount Gas, descriptor string) {
+ consumed, overflow := addUint64Overflow(pgm.GasConsumed(), amount)
if overflow {
panic(ErrorGasOverflow{Descriptor: descriptor})
}
if consumed > pgm.limit {
panic(ErrorOutOfGas{Descriptor: descriptor})
}
pgm.GasMeter.ConsumeGas(amount, descriptor)
}🧰 Tools🪛 GitHub Check: golangci-lint[failure] 55-55: 🪛 GitHub Actions: Lint[error] 55-55: Staticcheck: QF1008 - could remove embedded field 'GasMeter' from selector. 🤖 Prompt for AI Agents |
||
| func (pgm ProxyGasMeter) String() string { | ||
| return fmt.Sprintf("ProxyGasMeter{consumed: %d, limit: %d}", pgm.GasConsumed(), pgm.limit) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package types | ||
|
|
||
| import ( | ||
| math "math" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestProxyGasMeterBasic(t *testing.T) { | ||
| baseGas := uint64(1000) | ||
| limit := uint64(300) | ||
|
|
||
| bgm := NewGasMeter(baseGas) | ||
| pgm := NewProxyGasMeter(bgm, limit) | ||
|
|
||
| require.Equal(t, Gas(0), pgm.GasConsumed()) | ||
| require.Equal(t, limit, pgm.Limit()) | ||
| require.Equal(t, limit, pgm.GasRemaining()) | ||
|
|
||
| pgm.ConsumeGas(100, "test") | ||
| require.Equal(t, Gas(100), pgm.GasConsumed()) | ||
| require.Equal(t, Gas(100), bgm.GasConsumed()) | ||
| require.Equal(t, limit-100, pgm.GasRemaining()) | ||
| require.False(t, pgm.IsOutOfGas()) | ||
| require.False(t, pgm.IsPastLimit()) | ||
|
|
||
| pgm.ConsumeGas(200, "test") | ||
| require.Equal(t, Gas(300), pgm.GasConsumed()) | ||
| require.Equal(t, Gas(300), bgm.GasConsumed()) | ||
| require.Equal(t, Gas(0), pgm.GasRemaining()) | ||
| require.Equal(t, Gas(700), bgm.GasRemaining()) | ||
| require.True(t, pgm.IsOutOfGas()) | ||
| require.False(t, pgm.IsPastLimit()) | ||
|
|
||
| require.Panics(t, func() { | ||
| pgm.ConsumeGas(1, "test") | ||
| }) | ||
| require.Equal(t, Gas(700), bgm.GasRemaining()) | ||
|
|
||
| pgm.RefundGas(1, "test") | ||
| require.Equal(t, Gas(299), pgm.GasConsumed()) | ||
| require.Equal(t, Gas(1), pgm.GasRemaining()) | ||
| require.False(t, pgm.IsOutOfGas()) | ||
| require.False(t, pgm.IsPastLimit()) | ||
| } | ||
|
|
||
| func TestProxyGasMeterInfinit(t *testing.T) { | ||
| limit := uint64(300) | ||
|
|
||
| bgm := NewInfiniteGasMeter() | ||
| pgm := NewProxyGasMeter(bgm, limit) | ||
|
|
||
| require.Equal(t, Gas(0), pgm.GasConsumed()) | ||
| require.Equal(t, limit, pgm.Limit()) | ||
| require.Equal(t, limit, pgm.GasRemaining()) | ||
|
|
||
| pgm.ConsumeGas(100, "test") | ||
| require.Equal(t, Gas(100), pgm.GasConsumed()) | ||
| require.Equal(t, Gas(100), bgm.GasConsumed()) | ||
| require.Equal(t, limit-100, pgm.GasRemaining()) | ||
| require.False(t, pgm.IsOutOfGas()) | ||
| require.False(t, pgm.IsPastLimit()) | ||
|
|
||
| pgm.ConsumeGas(200, "test") | ||
| require.Equal(t, Gas(300), pgm.GasConsumed()) | ||
| require.Equal(t, Gas(300), bgm.GasConsumed()) | ||
| require.Equal(t, Gas(0), pgm.GasRemaining()) | ||
| require.Equal(t, Gas(math.MaxUint64), bgm.GasRemaining()) | ||
| require.True(t, pgm.IsOutOfGas()) | ||
| require.False(t, pgm.IsPastLimit()) | ||
|
|
||
| require.Panics(t, func() { | ||
| pgm.ConsumeGas(1, "test") | ||
| }) | ||
| require.Equal(t, Gas(math.MaxUint64), bgm.GasRemaining()) | ||
|
|
||
| pgm.RefundGas(1, "test") | ||
| require.Equal(t, Gas(299), pgm.GasConsumed()) | ||
| require.Equal(t, Gas(1), pgm.GasRemaining()) | ||
| require.False(t, pgm.IsOutOfGas()) | ||
| require.False(t, pgm.IsPastLimit()) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Guard against uint64 overflow when computing the absolute limit.
If the wrapped meter has very high consumption (e.g., infinite meter),
consumed + remainingcan overflow and silently wrap. Panic consistently with ConsumeGas on overflow.func NewProxyGasMeter(gasMeter GasMeter, remaining Gas) GasMeter { if remaining >= gasMeter.GasRemaining() { return gasMeter } - return &ProxyGasMeter{ - GasMeter: gasMeter, - limit: remaining + gasMeter.GasConsumed(), - } + limit, overflow := addUint64Overflow(gasMeter.GasConsumed(), remaining) + if overflow { + panic(ErrorGasOverflow{Descriptor: "NewProxyGasMeter"}) + } + return &ProxyGasMeter{ + GasMeter: gasMeter, + limit: limit, + } }🤖 Prompt for AI Agents