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
unnecessary conversions
  • Loading branch information
ceyonur committed Oct 6, 2025
commit 3e179816ae3ed4783bac92e58c10e28b47160aaa
20 changes: 10 additions & 10 deletions vms/evm/acp176/acp176.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,25 @@ func (s *State) Target() gas.Gas {
// MaxCapacity returns the maximum possible accrued gas capacity, `C`.
func (s *State) MaxCapacity() gas.Gas {
targetPerSecond := s.Target()
return gas.Gas(excess.MulWithUpperBound(uint64(targetPerSecond), TargetToMaxCapacity))
return mulWithUpperBound(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 := excess.MulWithUpperBound(uint64(targetPerSecond), TargetToPriceUpdateConversion) // K
return gas.CalculatePrice(MinGasPrice, s.Gas.Excess, gas.Gas(priceUpdateConversion))
priceUpdateConversion := mulWithUpperBound(targetPerSecond, TargetToPriceUpdateConversion) // K
return gas.CalculatePrice(MinGasPrice, s.Gas.Excess, priceUpdateConversion)
}

// AdvanceSeconds increases the gas capacity and decreases the gas excess based on
// the elapsed seconds.
// This is used in Fortuna.
func (s *State) AdvanceSeconds(seconds uint64) {
targetPerSecond := s.Target()
maxPerSecond := gas.Gas(excess.MulWithUpperBound(uint64(targetPerSecond), TargetToMax)) // R
maxCapacity := gas.Gas(excess.MulWithUpperBound(uint64(maxPerSecond), TimeToFillCapacity)) // C
maxPerSecond := mulWithUpperBound(targetPerSecond, TargetToMax) // R
maxCapacity := mulWithUpperBound(maxPerSecond, TimeToFillCapacity) // C
s.Gas = s.Gas.AdvanceTime(
maxCapacity,
maxPerSecond,
Expand All @@ -122,9 +122,9 @@ func (s *State) AdvanceSeconds(seconds uint64) {
func (s *State) AdvanceMilliseconds(milliseconds uint64) {
targetPerSecond := s.Target()
targetPerMS := targetPerSecond / 1000
maxPerMS := targetPerMS * TargetToMax // R - this can't overflow since 1000 > TargetToMax.
maxPerSecond := excess.MulWithUpperBound(uint64(targetPerSecond), TargetToMax) // rate used for calculating maxCapacity
maxCapacity := gas.Gas(excess.MulWithUpperBound(maxPerSecond, TimeToFillCapacity)) // C
maxPerMS := targetPerMS * TargetToMax // R - this can't overflow since 1000 > TargetToMax.
maxPerSecond := mulWithUpperBound(targetPerSecond, TargetToMax) // rate used for calculating maxCapacity
maxCapacity := mulWithUpperBound(maxPerSecond, TimeToFillCapacity) // C
s.Gas = s.Gas.AdvanceTime(
maxCapacity,
maxPerMS,
Expand Down Expand Up @@ -177,7 +177,7 @@ func (s *State) UpdateTargetExcess(desiredTargetExcess gas.Gas) {
)

// Ensure the gas capacity does not exceed the maximum capacity.
newMaxCapacity := gas.Gas(excess.MulWithUpperBound(uint64(newTargetPerSecond), TargetToMaxCapacity)) // C
newMaxCapacity := mulWithUpperBound(newTargetPerSecond, TargetToMaxCapacity) // C
s.Gas.Capacity = min(s.Gas.Capacity, newMaxCapacity)
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

}

Expand Down Expand Up @@ -220,7 +220,7 @@ func scaleExcess(

// mulWithUpperBound multiplies two numbers and returns the result. If the
// result overflows, it returns [math.MaxUint64].
func mulWithUpperBound(a, b uint64) uint64 {
func mulWithUpperBound(a, b gas.Gas) gas.Gas {
product, err := safemath.Mul(a, b)
if err != nil {
return math.MaxUint64
Expand Down
4 changes: 2 additions & 2 deletions vms/evm/acp226/acp226.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ func (t *DelayExcess) UpdateDelayExcess(desiredDelayExcess DelayExcess) {

// DesiredDelayExcess calculates the optimal delay excess given the desired
// delay.
func DesiredDelayExcess(desiredDelay uint64) uint64 {
return acp226Params.DesiredExcess(desiredDelay)
func DesiredDelayExcess(desiredDelay uint64) DelayExcess {
return DelayExcess(acp226Params.DesiredExcess(desiredDelay))
}
11 changes: 0 additions & 11 deletions vms/evm/excess/params.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 new code?

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package excess

import (
"math"
"sort"

safemath "github.com/ava-labs/avalanchego/utils/math"
Expand Down Expand Up @@ -50,13 +49,3 @@ func (p Params) CalculateValue(excess uint64) uint64 {
p.ConversionRate,
)
}

// MulWithUpperBound multiplies two numbers and returns the result. If the
// result overflows, it returns [math.MaxUint64].
func MulWithUpperBound(a, b uint64) uint64 {
product, err := safemath.Mul(a, b)
if err != nil {
return math.MaxUint64
}
return product
}