-
Notifications
You must be signed in to change notification settings - Fork 1
Update gas target immediately after block execution #39
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
Changes from 4 commits
af596f7
178724f
19c3fb9
3b7e488
6851d5a
d8e67f3
b4450bb
f34665a
0373d0a
94e1fb9
5c4a6e1
16badd0
ad66e07
2339b16
cb79047
1f15693
b5873a6
ee5f973
cf23da5
5793181
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,64 @@ | ||
| // Copyright (C) 2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package hook_test | ||
StephenButtolph marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/ava-labs/avalanchego/vms/components/gas" | ||
| "github.com/ava-labs/libevm/core/types" | ||
| "github.com/ava-labs/libevm/params" | ||
| "github.com/ava-labs/strevm/gastime" | ||
| "github.com/ava-labs/strevm/hook/hookstest" | ||
| "github.com/ava-labs/strevm/saetest" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| . "github.com/ava-labs/strevm/hook" | ||
|
||
| ) | ||
|
|
||
| // TestTargetUpdateTiming verifies that the gas target is modified in AfterBlock | ||
| // rather than BeforeBlock. | ||
| func TestTargetUpdateTiming(t *testing.T) { | ||
| const ( | ||
| initialTime = 42 | ||
| initialTarget gas.Gas = 1_600_000 | ||
| initialExcess = 1_234_567_890 | ||
| ) | ||
| tm := gastime.New(initialTime, initialTarget, initialExcess) | ||
|
|
||
| const ( | ||
| newTime uint64 = initialTime + 1 | ||
| newTarget = initialTarget + 100_000 | ||
| ) | ||
| hook := &hookstest.Stub{ | ||
| Target: newTarget, | ||
| } | ||
| header := &types.Header{ | ||
| Time: newTime, | ||
| } | ||
| block := types.NewBlock(header, nil, nil, nil, saetest.TrieHasher()) | ||
|
|
||
| initialPrice := tm.Price() | ||
| require.NoError(t, BeforeBlock(hook, params.TestRules, nil, block, tm), "BeforeBlock()") | ||
| assert.Equal(t, newTime, tm.Unix(), "Unix time advanced by BeforeBlock()") | ||
| assert.Equal(t, initialTarget, tm.Target(), "Target not changed by BeforeBlock()") | ||
|
|
||
| enforcedPrice := tm.Price() | ||
| assert.LessOrEqual(t, enforcedPrice, initialPrice, "Price should not increase in BeforeBlock()") | ||
StephenButtolph marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if t.Failed() { | ||
| t.FailNow() | ||
| } | ||
|
|
||
| const ( | ||
| secondsOfGasUsed = 3 | ||
| initialRate = initialTarget * gastime.TargetToRate | ||
StephenButtolph marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| used gas.Gas = initialRate * secondsOfGasUsed | ||
| expectedEndTime = newTime + secondsOfGasUsed | ||
| ) | ||
| require.NoError(t, AfterBlock(hook, nil, block, tm, used, nil), "AfterBlock()") | ||
| assert.Equal(t, expectedEndTime, tm.Unix(), "Unix time advanced by AfterBlock()") | ||
StephenButtolph marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert.Equal(t, newTarget, tm.Target(), "Target updated by AfterBlock()") | ||
| assert.GreaterOrEqual(t, tm.Price(), enforcedPrice, "Price should not decrease in AfterBlock()") | ||
| } | ||
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.
Also (nit), something about the wording of "to consume after executing the provided block" feels off and it confused me for a split second. Perhaps "target to consume in subsequent blocks."? The current phrasing suggests immediacy, not long-term horizon.
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.
I reworded it, lmk what you think