-
Notifications
You must be signed in to change notification settings - Fork 840
acp-176/226 refactor to common package #4292
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
ceyonur
wants to merge
21
commits into
master
Choose a base branch
from
ceyonur/acp-176-226-refactor
base: master
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.
+142
−120
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b20dfd9
refactor common parts from acp-176 and acp-226
ceyonur 70d12c3
move fake expo calculation to safemath
ceyonur f1609fa
fix linter
ceyonur 4b1dd83
remove extra import
ceyonur 62000d4
fix predeclared identifier
ceyonur 1a03e68
use builtin min
ceyonur 4b2b0bd
remove target prefixes
ceyonur e43a12f
remove comment
ceyonur 9efd27a
revert var name change
ceyonur 5fbcbef
Merge branch 'master' into ceyonur/acp-176-226-refactor
ceyonur d5b7f34
fix import path
ceyonur 7f3cfaf
Rename target_excess.go to excess_params.go
ceyonur 7b1b98b
Merge branch 'master' into ceyonur/acp-176-226-refactor
ceyonur bc65754
resolve conflict
ceyonur e5af5e0
Merge branch 'ceyonur/acp-176-226-refactor' of github.com:ava-labs/av…
ceyonur 795236e
Merge branch 'master' into ceyonur/acp-176-226-refactor
ceyonur 0c58d10
reviews
ceyonur 34a58f9
fix import paths
ceyonur 3e17981
unnecessary conversions
ceyonur 43c2413
Merge branch 'master' into ceyonur/acp-176-226-refactor
ceyonur 37a85e0
Merge branch 'master' into ceyonur/acp-176-226-refactor
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package math | ||
|
|
||
| import ( | ||
| "math" | ||
|
|
||
| "github.com/holiman/uint256" | ||
| ) | ||
|
|
||
| var max256Uint64 = new(uint256.Int).SetUint64(math.MaxUint64) | ||
|
|
||
| // ApproximateExponential implements the EIP-4844 fake exponential formula and | ||
| // returns the approximate exponential result given the factor, the numerator, and the denominator. | ||
| // | ||
| // It is defined as an approximation of: | ||
| // | ||
| // factor * e^(numerator / denominator) | ||
| // | ||
| // This implementation is optimized with the knowledge that any value greater | ||
| // than MaxUint64 gets returned as MaxUint64. This means that every intermediate | ||
| // value is guaranteed to be at most MaxUint193. So, we can safely use | ||
| // uint256.Int. | ||
| // | ||
| // This function does not perform any memory allocations. | ||
| func ApproximateExponential( | ||
| factor uint64, | ||
| numerator uint64, | ||
| denominator uint64, | ||
| ) uint64 { | ||
| var ( | ||
| num uint256.Int | ||
| denom uint256.Int | ||
|
|
||
| i uint256.Int | ||
| output uint256.Int | ||
| numeratorAccum uint256.Int | ||
|
|
||
| maxOutput uint256.Int | ||
| ) | ||
| num.SetUint64(numerator) | ||
| denom.SetUint64(denominator) | ||
|
|
||
| i.SetOne() | ||
| numeratorAccum.SetUint64(factor) | ||
| numeratorAccum.Mul(&numeratorAccum, &denom) // range is [0, MaxUint128] | ||
|
|
||
| maxOutput.Mul(&denom, max256Uint64) // range is [0, MaxUint128] | ||
| for numeratorAccum.Sign() > 0 { | ||
| output.Add(&output, &numeratorAccum) // range is [0, MaxUint192+MaxUint128] | ||
| if output.Cmp(&maxOutput) >= 0 { | ||
| return math.MaxUint64 | ||
| } | ||
| // maxOutput < MaxUint128 so numeratorAccum < MaxUint128. | ||
| numeratorAccum.Mul(&numeratorAccum, &num) // range is [0, MaxUint192] | ||
| numeratorAccum.Div(&numeratorAccum, &denom) | ||
| numeratorAccum.Div(&numeratorAccum, &i) | ||
|
|
||
| i.AddUint64(&i, 1) | ||
| } | ||
| return output.Div(&output, &denom).Uint64() | ||
| } |
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
michaelkaplan13 marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
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. Can we add tests for new code? |
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,51 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package excess | ||
|
|
||
| import ( | ||
| "sort" | ||
|
|
||
| safemath "github.com/ava-labs/avalanchego/utils/math" | ||
| ) | ||
|
|
||
| // Params contains the parameters needed for excess calculations. | ||
| type Params struct { | ||
| MinValue uint64 // Minimum value | ||
| ConversionRate uint64 // Conversion factor for exponential calculations | ||
| MaxExcessDiff uint64 // Maximum change in excess per update | ||
| MaxExcess uint64 // Maximum possible excess value | ||
| } | ||
|
|
||
| // AdjustExcess calculates the optimal new excess given the current and desired excess values, | ||
| // ensuring the change does not exceed the maximum excess difference. | ||
| func (p Params) AdjustExcess(excess, desired uint64) uint64 { | ||
| change := safemath.AbsDiff(excess, desired) | ||
| change = min(change, p.MaxExcessDiff) | ||
| if excess < desired { | ||
| return excess + change | ||
| } | ||
| return excess - change | ||
| } | ||
|
|
||
| // DesiredExcess calculates the optimal desiredExcess given the | ||
| // desired value using binary search. | ||
| func (p Params) DesiredExcess(desiredValue uint64) uint64 { | ||
| // This could be solved directly by calculating ConversionRate * ln(desiredValue / MinValue) | ||
| // using floating point math. However, it introduces inaccuracies. So, we | ||
| // use a binary search to find the closest integer solution. | ||
| return uint64(sort.Search(int(p.MaxExcess), func(targetExcessGuess int) bool { | ||
| calculatedValue := p.CalculateValue(uint64(targetExcessGuess)) | ||
| return calculatedValue >= desiredValue | ||
| })) | ||
| } | ||
|
|
||
| // CalculateValue calculates the value using exponential formula: | ||
| // Value = MinValue * e^(Excess / ConversionRate) | ||
| func (p Params) CalculateValue(excess uint64) uint64 { | ||
| return safemath.ApproximateExponential( | ||
| p.MinValue, | ||
| excess, | ||
| p.ConversionRate, | ||
| ) | ||
| } |
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.
Can we add tests for this new code?