-
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 6 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
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,77 @@ | ||
| // 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) | ||
ceyonur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // CalculateExponential 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 implements the EIP-4844 fake exponential formula: | ||
| // | ||
| // def fake_exponential(factor: int, numerator: int, denominator: int) -> int: | ||
| // i = 1 | ||
| // output = 0 | ||
| // numerator_accum = factor * denominator | ||
| // while numerator_accum > 0: | ||
| // output += numerator_accum | ||
| // numerator_accum = (numerator_accum * numerator) // (denominator * i) | ||
| // i += 1 | ||
| // return output // 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. | ||
joshua-kim marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // | ||
| //nolint:dupword // The python is copied from the EIP-4844 specification | ||
| func CalculateExponential( | ||
joshua-kim marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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) // range is [0, MaxUint64] | ||
joshua-kim marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| denom.SetUint64(denominator) // range is [0, MaxUint64] | ||
|
|
||
| i.SetOne() | ||
| numeratorAccum.SetUint64(factor) // range is [0, MaxUint64] | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,14 +11,12 @@ import ( | |
| "fmt" | ||
| "math" | ||
| "math/big" | ||
| "sort" | ||
|
|
||
| "github.com/holiman/uint256" | ||
|
|
||
| "github.com/ava-labs/avalanchego/utils/wrappers" | ||
| "github.com/ava-labs/avalanchego/vms/components/gas" | ||
|
|
||
| safemath "github.com/ava-labs/avalanchego/utils/math" | ||
| "github.com/ava-labs/avalanchego/vms/evm/upgrades/common" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -41,7 +39,16 @@ const ( | |
| maxTargetExcess = 1_024_950_627 // TargetConversion * ln(MaxUint64 / MinTargetPerSecond) + 1 | ||
| ) | ||
|
|
||
| var ErrStateInsufficientLength = errors.New("insufficient length for fee state") | ||
| var ( | ||
| ErrStateInsufficientLength = errors.New("insufficient length for fee state") | ||
|
|
||
| acp176Params = common.TargetExcessParams{ | ||
| MinTarget: MinTargetPerSecond, | ||
| TargetConversion: TargetConversion, | ||
| MaxExcessDiff: MaxTargetExcessDiff, | ||
| MaxExcess: maxTargetExcess, | ||
| } | ||
| ) | ||
|
|
||
| // State represents the current state of the gas pricing and constraints. | ||
| type State struct { | ||
|
|
@@ -74,34 +81,30 @@ func ParseState(bytes []byte) (State, error) { | |
| // | ||
| // Target = MinTargetPerSecond * e^(TargetExcess / TargetConversion) | ||
| func (s *State) Target() gas.Gas { | ||
| return gas.Gas(gas.CalculatePrice( | ||
| MinTargetPerSecond, | ||
| s.TargetExcess, | ||
| TargetConversion, | ||
| )) | ||
| return gas.Gas(acp176Params.CalculateTarget(uint64(s.TargetExcess))) | ||
| } | ||
|
|
||
| // MaxCapacity returns the maximum possible accrued gas capacity, `C`. | ||
| func (s *State) MaxCapacity() gas.Gas { | ||
| targetPerSecond := s.Target() | ||
| return mulWithUpperBound(targetPerSecond, TargetToMaxCapacity) | ||
| return gas.Gas(common.MulWithUpperBound(uint64(targetPerSecond), TargetToMaxCapacity)) | ||
| } | ||
|
|
||
| // GasPrice returns the current required fee per gas. | ||
| // | ||
| // GasPrice = MinGasPrice * e^(Excess / (Target() * TargetToPriceUpdateConversion)) | ||
| func (s *State) GasPrice() gas.Price { | ||
| targetPerSecond := s.Target() | ||
| priceUpdateConversion := mulWithUpperBound(targetPerSecond, TargetToPriceUpdateConversion) // K | ||
| return gas.CalculatePrice(MinGasPrice, s.Gas.Excess, priceUpdateConversion) | ||
| priceUpdateConversion := common.MulWithUpperBound(uint64(targetPerSecond), TargetToPriceUpdateConversion) // K | ||
| return gas.CalculatePrice(MinGasPrice, s.Gas.Excess, gas.Gas(priceUpdateConversion)) | ||
| } | ||
|
|
||
| // AdvanceTime increases the gas capacity and decreases the gas excess based on | ||
| // the elapsed seconds. | ||
| func (s *State) AdvanceTime(seconds uint64) { | ||
| targetPerSecond := s.Target() | ||
| maxPerSecond := mulWithUpperBound(targetPerSecond, TargetToMax) // R | ||
| maxCapacity := mulWithUpperBound(maxPerSecond, TimeToFillCapacity) // C | ||
| maxPerSecond := gas.Gas(common.MulWithUpperBound(uint64(targetPerSecond), TargetToMax)) // R | ||
| maxCapacity := gas.Gas(common.MulWithUpperBound(uint64(maxPerSecond), TimeToFillCapacity)) // C | ||
| s.Gas = s.Gas.AdvanceTime( | ||
| maxCapacity, | ||
| maxPerSecond, | ||
|
|
@@ -145,7 +148,7 @@ func (s *State) ConsumeGas( | |
| // desiredTargetExcess without exceeding the maximum targetExcess change. | ||
| func (s *State) UpdateTargetExcess(desiredTargetExcess gas.Gas) { | ||
| previousTargetPerSecond := s.Target() | ||
| s.TargetExcess = targetExcess(s.TargetExcess, desiredTargetExcess) | ||
| s.TargetExcess = gas.Gas(acp176Params.TargetExcess(uint64(s.TargetExcess), uint64(desiredTargetExcess))) | ||
| newTargetPerSecond := s.Target() | ||
| s.Gas.Excess = scaleExcess( | ||
| s.Gas.Excess, | ||
|
|
@@ -154,7 +157,7 @@ func (s *State) UpdateTargetExcess(desiredTargetExcess gas.Gas) { | |
| ) | ||
|
|
||
| // Ensure the gas capacity does not exceed the maximum capacity. | ||
| newMaxCapacity := mulWithUpperBound(newTargetPerSecond, TargetToMaxCapacity) // C | ||
| newMaxCapacity := gas.Gas(common.MulWithUpperBound(uint64(newTargetPerSecond), TargetToMaxCapacity)) // C | ||
| s.Gas.Capacity = min(s.Gas.Capacity, newMaxCapacity) | ||
|
Contributor
Author
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. this now uses golang builtin min, would it be a problem? cc @StephenButtolph |
||
| } | ||
|
|
||
|
|
@@ -170,26 +173,7 @@ func (s *State) Bytes() []byte { | |
| // DesiredTargetExcess calculates the optimal desiredTargetExcess given the | ||
| // desired target. | ||
| func DesiredTargetExcess(desiredTarget gas.Gas) gas.Gas { | ||
| // This could be solved directly by calculating D * ln(desiredTarget / P) | ||
| // using floating point math. However, it introduces inaccuracies. So, we | ||
| // use a binary search to find the closest integer solution. | ||
| return gas.Gas(sort.Search(maxTargetExcess, func(targetExcessGuess int) bool { | ||
| state := State{ | ||
| TargetExcess: gas.Gas(targetExcessGuess), | ||
| } | ||
| return state.Target() >= desiredTarget | ||
| })) | ||
| } | ||
|
|
||
| // targetExcess calculates the optimal new targetExcess for a block proposer to | ||
| // include given the current and desired excess values. | ||
| func targetExcess(excess, desired gas.Gas) gas.Gas { | ||
| change := safemath.AbsDiff(excess, desired) | ||
| change = min(change, MaxTargetExcessDiff) | ||
| if excess < desired { | ||
| return excess + change | ||
| } | ||
| return excess - change | ||
| return gas.Gas(acp176Params.DesiredTargetExcess(uint64(desiredTarget))) | ||
| } | ||
|
|
||
| // scaleExcess scales the excess during gas target modifications to keep the | ||
|
|
@@ -213,13 +197,3 @@ func scaleExcess( | |
| } | ||
| return gas.Gas(bigExcess.Uint64()) | ||
| } | ||
|
|
||
| // mulWithUpperBound multiplies two numbers and returns the result. If the | ||
| // result overflows, it returns [math.MaxUint64]. | ||
| func mulWithUpperBound(a, b gas.Gas) gas.Gas { | ||
| product, err := safemath.Mul(a, b) | ||
| if err != nil { | ||
| return math.MaxUint64 | ||
| } | ||
| return product | ||
| } | ||
File renamed without changes.
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
|
File renamed without changes.
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.
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?