Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove target prefixes
  • Loading branch information
ceyonur committed Sep 24, 2025
commit 4b2b0bd844e90642dcc4a13b58af06b35ed819dd
4 changes: 2 additions & 2 deletions utils/math/exponential.go
Copy link
Contributor

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?

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/holiman/uint256"
)

var max256Uint64 = new(uint256.Int).SetUint64(math.MaxUint64)
var maxUint64 = new(uint256.Int).SetUint64(math.MaxUint64)

// CalculateExponential returns the approximate exponential result given the factor, the
// numerator, and the denominator.
Expand Down Expand Up @@ -60,7 +60,7 @@ func CalculateExponential(
numeratorAccum.SetUint64(factor) // range is [0, MaxUint64]
numeratorAccum.Mul(&numeratorAccum, &denom) // range is [0, MaxUint128]

maxOutput.Mul(&denom, max256Uint64) // range is [0, MaxUint128]
maxOutput.Mul(&denom, maxUint64) // range is [0, MaxUint128]
for numeratorAccum.Sign() > 0 {
output.Add(&output, &numeratorAccum) // range is [0, MaxUint192+MaxUint128]
if output.Cmp(&maxOutput) >= 0 {
Expand Down
16 changes: 8 additions & 8 deletions vms/evm/upgrades/acp176/acp176.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ const (
var (
ErrStateInsufficientLength = errors.New("insufficient length for fee state")

acp176Params = common.TargetExcessParams{
MinTarget: MinTargetPerSecond,
TargetConversion: TargetConversion,
MaxExcessDiff: MaxTargetExcessDiff,
MaxExcess: maxTargetExcess,
acp176Params = common.ExcessParams{
MinValue: MinTargetPerSecond,
ConversionRate: TargetConversion,
MaxExcessDiff: MaxTargetExcessDiff,
MaxExcess: maxTargetExcess,
}
)

Expand Down Expand Up @@ -81,7 +81,7 @@ func ParseState(bytes []byte) (State, error) {
//
// Target = MinTargetPerSecond * e^(TargetExcess / TargetConversion)
func (s *State) Target() gas.Gas {
return gas.Gas(acp176Params.CalculateTarget(uint64(s.TargetExcess)))
return gas.Gas(acp176Params.CalculateValue(uint64(s.TargetExcess)))
}

// MaxCapacity returns the maximum possible accrued gas capacity, `C`.
Expand Down Expand Up @@ -148,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 = gas.Gas(acp176Params.TargetExcess(uint64(s.TargetExcess), uint64(desiredTargetExcess)))
s.TargetExcess = gas.Gas(acp176Params.AdjustExcess(uint64(s.TargetExcess), uint64(desiredTargetExcess)))
newTargetPerSecond := s.Target()
s.Gas.Excess = scaleExcess(
s.Gas.Excess,
Expand All @@ -173,7 +173,7 @@ func (s *State) Bytes() []byte {
// DesiredTargetExcess calculates the optimal desiredTargetExcess given the
// desired target.
func DesiredTargetExcess(desiredTarget gas.Gas) gas.Gas {
return gas.Gas(acp176Params.DesiredTargetExcess(uint64(desiredTarget)))
return gas.Gas(acp176Params.DesiredExcess(uint64(desiredTarget)))
}

// scaleExcess scales the excess during gas target modifications to keep the
Expand Down
16 changes: 8 additions & 8 deletions vms/evm/upgrades/acp226/acp226.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const (
)

// acp226Params is the params used for the acp226 upgrade.
var acp226Params = common.TargetExcessParams{
MinTarget: MinDelayMilliseconds,
TargetConversion: ConversionRate,
MaxExcessDiff: MaxDelayExcessDiff,
MaxExcess: maxDelayExcess,
var acp226Params = common.ExcessParams{
MinValue: MinDelayMilliseconds,
ConversionRate: ConversionRate,
MaxExcessDiff: MaxDelayExcessDiff,
MaxExcess: maxDelayExcess,
}

// DelayExcess represents the excess for delay calculation in the dynamic minimum block delay mechanism.
Expand All @@ -33,13 +33,13 @@ type DelayExcess uint64
//
// Delay = MinDelayMilliseconds * e^(DelayExcess / ConversionRate)
func (t DelayExcess) Delay() uint64 {
return acp226Params.CalculateTarget(uint64(t))
return acp226Params.CalculateValue(uint64(t))
}

// UpdateDelayExcess updates the DelayExcess to be as close as possible to the
// desiredDelayExcess without exceeding the maximum DelayExcess change.
func (t *DelayExcess) UpdateDelayExcess(desiredDelayExcess uint64) {
*t = DelayExcess(acp226Params.TargetExcess(uint64(*t), desiredDelayExcess))
*t = DelayExcess(acp226Params.AdjustExcess(uint64(*t), desiredDelayExcess))
}

// DesiredDelayExcess calculates the optimal delay excess given the desired
Expand All @@ -48,5 +48,5 @@ func DesiredDelayExcess(desiredDelay uint64) uint64 {
// This could be solved directly by calculating D * ln(desired / M)
// using floating point math. However, it introduces inaccuracies. So, we
// use a binary search to find the closest integer solution.
return acp226Params.DesiredTargetExcess(desiredDelay)
return acp226Params.DesiredExcess(desiredDelay)
}
40 changes: 20 additions & 20 deletions vms/evm/upgrades/common/target_excess.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
safemath "github.com/ava-labs/avalanchego/utils/math"
)

// TargetExcessParams contains the parameters needed for target excess calculations.
type TargetExcessParams struct {
MinTarget uint64 // Minimum target value (P or M)
TargetConversion uint64 // Conversion factor for exponential calculations (D)
MaxExcessDiff uint64 // Maximum change in excess per update (Q)
MaxExcess uint64 // Maximum possible excess value
// ExcessParams contains the parameters needed for excess calculations.
type ExcessParams struct {
MinValue uint64 // Minimum value (P or M)
ConversionRate uint64 // Conversion factor for exponential calculations (D)
MaxExcessDiff uint64 // Maximum change in excess per update (Q)
MaxExcess uint64 // Maximum possible excess value
}

// TargetExcess calculates the optimal new targetExcess for a block proposer to
// include given the current and desired excess values.
func (p TargetExcessParams) TargetExcess(excess, desired uint64) uint64 {
// 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 ExcessParams) AdjustExcess(excess, desired uint64) uint64 {
change := safemath.AbsDiff(excess, desired)
change = min(change, p.MaxExcessDiff)
if excess < desired {
Expand All @@ -30,25 +30,25 @@ func (p TargetExcessParams) TargetExcess(excess, desired uint64) uint64 {
return excess - change
}

// DesiredTargetExcess calculates the optimal desiredTargetExcess given the
// desired target using binary search.
func (p TargetExcessParams) DesiredTargetExcess(desiredTarget uint64) uint64 {
// This could be solved directly by calculating D * ln(desiredTarget / P)
// DesiredExcess calculates the optimal desiredExcess given the
// desired value using binary search.
func (p ExcessParams) DesiredExcess(desiredValue uint64) uint64 {
// This could be solved directly by calculating D * ln(desiredValue / P)
// 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 {
calculatedTarget := p.CalculateTarget(uint64(targetExcessGuess))
return calculatedTarget >= desiredTarget
calculatedValue := p.CalculateValue(uint64(targetExcessGuess))
return calculatedValue >= desiredValue
}))
}

// CalculateTarget calculates the target value using exponential formula:
// Target = MinTarget * e^(Excess / TargetConversion)
func (p TargetExcessParams) CalculateTarget(excess uint64) uint64 {
// CalculateValue calculates the value using exponential formula:
// Value = MinValue * e^(Excess / ConversionRate)
func (p ExcessParams) CalculateValue(excess uint64) uint64 {
return safemath.CalculateExponential(
p.MinTarget,
p.MinValue,
excess,
p.TargetConversion,
p.ConversionRate,
)
}

Expand Down