-
Notifications
You must be signed in to change notification settings - Fork 840
Implement ACP-226 Math #4289
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
Merged
Merged
Implement ACP-226 Math #4289
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f72603e
add acp-226 math package
ceyonur a4455a5
rename state
ceyonur 0f5fa43
review
ceyonur b99a99b
further cleanup
ceyonur dec3615
remove size stuff
ceyonur d997c1a
Update vms/evm/acp226/acp226.go
ceyonur 960ab2d
Update vms/evm/acp226/acp226.go
ceyonur 473e120
Update vms/evm/acp226/acp226.go
ceyonur 1fd9ca6
Update vms/evm/acp226/acp226.go
ceyonur 2e260b1
Update vms/evm/acp226/acp226.go
ceyonur b1dae65
Merge branch 'master' into ceyonur/acp-226-math-package
ceyonur 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| // ACP-226 implements the dynamic minimum block delay mechanism specified here: | ||
| // https://github.com/avalanche-foundation/ACPs/blob/main/ACPs/226-dynamic-minimum-block-times/README.md | ||
| package acp226 | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "errors" | ||
| "fmt" | ||
| "sort" | ||
|
|
||
| "github.com/ava-labs/avalanchego/utils/wrappers" | ||
| "github.com/ava-labs/avalanchego/vms/components/gas" | ||
|
|
||
| safemath "github.com/ava-labs/avalanchego/utils/math" | ||
| ) | ||
|
|
||
| const ( | ||
| // MinTargetDelayMilliseconds (M) is the minimum target block delay in milliseconds | ||
| MinTargetDelayMilliseconds = 1 // ms | ||
| // TargetConversion (D) is the conversion factor for exponential calculations | ||
| TargetConversion = 1 << 20 | ||
| // MaxTargetDelayExcessDiff (Q) is the maximum change in target excess per update | ||
| MaxTargetDelayExcessDiff = 200 | ||
|
|
||
| StateSize = wrappers.LongLen | ||
|
|
||
| maxTargetDelayExcess = 46_516_320 // TargetConversion * ln(MaxUint64 / MinTargetDelayMilliseconds) + 1 | ||
| ) | ||
|
|
||
| var ErrStateInsufficientLength = errors.New("insufficient length for block delay state") | ||
|
|
||
| // TargetDelayExcess represents the target excess for delay calculation in the dynamic minimum block delay mechanism. | ||
| type TargetDelayExcess uint64 | ||
|
|
||
| // ParseTargetDelayExcess returns the target delay excess from the provided bytes. It is the inverse of | ||
| // [TargetDelayExcess.Bytes]. This function allows for additional bytes to be padded at the | ||
| // end of the provided bytes. | ||
| func ParseTargetDelayExcess(bytes []byte) (TargetDelayExcess, error) { | ||
| if len(bytes) < StateSize { | ||
| return 0, fmt.Errorf("%w: expected at least %d bytes but got %d bytes", | ||
| ErrStateInsufficientLength, | ||
| StateSize, | ||
| len(bytes), | ||
| ) | ||
| } | ||
|
|
||
| return TargetDelayExcess(binary.BigEndian.Uint64(bytes)), nil | ||
| } | ||
|
|
||
| // Bytes returns the binary representation of the target delay excess. | ||
| func (t TargetDelayExcess) Bytes() []byte { | ||
| bytes := make([]byte, StateSize) | ||
| binary.BigEndian.PutUint64(bytes, uint64(t)) | ||
| return bytes | ||
| } | ||
|
|
||
| // TargetDelay returns the target minimum block delay in milliseconds, `T`. | ||
| // | ||
| // TargetDelay = MinTargetDelayMilliseconds * e^(TargetDelayExcess / TargetConversion) | ||
| func (t TargetDelayExcess) TargetDelay() uint64 { | ||
| return uint64(gas.CalculatePrice( | ||
| MinTargetDelayMilliseconds, | ||
| gas.Gas(t), | ||
| TargetConversion, | ||
| )) | ||
StephenButtolph marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // UpdateTargetDelayExcess updates the targetDelayExcess to be as close as possible to the | ||
| // desiredTargetDelayExcess without exceeding the maximum targetDelayExcess change. | ||
| func (t *TargetDelayExcess) UpdateTargetDelayExcess(desiredTargetDelayExcess uint64) { | ||
| *t = TargetDelayExcess(targetDelayExcess(uint64(*t), desiredTargetDelayExcess)) | ||
| } | ||
|
|
||
| // DesiredTargetDelayExcess calculates the optimal desiredTargetDelayExcess given the | ||
| // desired target delay. | ||
| func DesiredTargetDelayExcess(desiredTargetDelayExcess uint64) uint64 { | ||
| // This could be solved directly by calculating D * ln(desiredTarget / M) | ||
| // using floating point math. However, it introduces inaccuracies. So, we | ||
| // use a binary search to find the closest integer solution. | ||
| return uint64(sort.Search(maxTargetDelayExcess, func(targetDelayExcessGuess int) bool { | ||
| excess := TargetDelayExcess(targetDelayExcessGuess) | ||
| return excess.TargetDelay() >= desiredTargetDelayExcess | ||
| })) | ||
| } | ||
StephenButtolph marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // targetDelayExcess calculates the optimal new targetDelayExcess for a block proposer to | ||
| // include given the current and desired excess values. | ||
| func targetDelayExcess(excess, desired uint64) uint64 { | ||
| change := safemath.AbsDiff(excess, desired) | ||
| change = min(change, MaxTargetDelayExcessDiff) | ||
| if excess < desired { | ||
| return excess + change | ||
| } | ||
| return excess - change | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.