-
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
Open
yihuang
wants to merge
5
commits into
cosmos:main
Choose a base branch
from
yihuang:proxygasmeter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8d642dc
feat: support executing sub-calls with a specific gas limit
yihuang 4622253
Update store/types/proxygasmeter.go
yihuang 5832919
emulate the behavior of fresh gas meter counting from 0
yihuang 0ddeccf
reverse order
yihuang efdbdee
Merge branch 'main' into proxygasmeter
aljo242 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.GasConsumed(), amount) | ||
| if overflow { | ||
| panic(ErrorGasOverflow{Descriptor: descriptor}) | ||
|
||
| } | ||
|
|
||
| if consumed > pgm.limit { | ||
| panic(ErrorOutOfGas{Descriptor: descriptor}) | ||
|
||
| } | ||
|
|
||
| pgm.GasMeter.ConsumeGas(amount, descriptor) | ||
| } | ||
|
|
||
| func (pgm ProxyGasMeter) String() string { | ||
| return fmt.Sprintf("ProxyGasMeter{consumed: %d, limit: %d}", pgm.GasConsumed(), pgm.limit) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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