Skip to content

Commit

Permalink
merged
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Sep 18, 2024
2 parents 53e621b + 6c0945b commit ed2be76
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
19 changes: 2 additions & 17 deletions snow/consensus/snowman/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,11 @@ func (n *Network) AddNode(t testing.TB, sm Consensus) error {
}

n.shuffleColors()
deps := map[ids.ID]ids.ID{}
for _, blk := range n.colors {
myDep, found := deps[blk.ParentV]
if !found {
myDep = blk.Parent()
}
myBlock := &snowmantest.Block{
Decidable: snowtest.Decidable{
IDV: blk.ID(),
Status: blk.Status,
},
ParentV: myDep,
HeightV: blk.Height(),
VerifyV: blk.Verify(context.Background()),
BytesV: blk.Bytes(),
}
if err := sm.Add(myBlock); err != nil {
copiedBlk := *blk
if err := sm.Add(&copiedBlk); err != nil {
return err
}
deps[myBlock.ID()] = myDep
}
n.nodes = append(n.nodes, sm)
n.running = append(n.running, sm)
Expand Down
16 changes: 8 additions & 8 deletions vms/platformvm/validators/fee/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s State) AdvanceTime(target gas.Gas, seconds uint64) State {
}

// CostOf calculates how much to charge based on the dynamic fee mechanism for
// [seconds].
// seconds.
//
// This implements the ACP-77 cost over time formula:
func (s State) CostOf(c Config, seconds uint64) uint64 {
Expand Down Expand Up @@ -92,9 +92,9 @@ func (s State) CostOf(c Config, seconds uint64) uint64 {
}

// SecondsRemaining calculates the maximum number of seconds that a validator
// can pay fees before their [costLimit] would be exceeded based on the dynamic
// fee mechanism. The result is capped at [maxSeconds].
func (s State) SecondsRemaining(c Config, maxSeconds uint64, costLimit uint64) uint64 {
// can pay fees before their fundsRemaining would be exhausted based on the
// dynamic fee mechanism. The result is capped at maxSeconds.
func (s State) SecondsRemaining(c Config, maxSeconds uint64, fundsRemaining uint64) uint64 {
// Because this function can divide by prices, we need to sanity check the
// parameters to avoid division by 0.
if c.MinPrice == 0 {
Expand All @@ -104,7 +104,7 @@ func (s State) SecondsRemaining(c Config, maxSeconds uint64, costLimit uint64) u
// If the current and target are the same, the price is constant.
if s.Current == c.Target {
price := uint64(gas.CalculatePrice(c.MinPrice, s.Excess, c.ExcessConversionConstant))
seconds := costLimit / price
seconds := fundsRemaining / price
return min(seconds, maxSeconds)
}

Expand All @@ -116,7 +116,7 @@ func (s State) SecondsRemaining(c Config, maxSeconds uint64, costLimit uint64) u
// equal to 0 after performing one of these operations, it is guaranteed
// to always remain 0.
if s.Excess == 0 {
secondsWithZeroExcess := costLimit / uint64(c.MinPrice)
secondsWithZeroExcess := fundsRemaining / uint64(c.MinPrice)
totalSeconds, err := safemath.Add(seconds, secondsWithZeroExcess)
if err != nil {
// This is technically unreachable, but makes the code more
Expand All @@ -127,10 +127,10 @@ func (s State) SecondsRemaining(c Config, maxSeconds uint64, costLimit uint64) u
}

price := uint64(gas.CalculatePrice(c.MinPrice, s.Excess, c.ExcessConversionConstant))
if price > costLimit {
if price > fundsRemaining {
return seconds
}
costLimit -= price
fundsRemaining -= price
}
return maxSeconds
}
12 changes: 6 additions & 6 deletions vms/platformvm/validators/fee/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func FuzzStateCostOf(f *testing.F) {
MinPrice: gas.Price(minPrice),
ExcessConversionConstant: gas.Gas(max(excessConversionConstant, 1)),
}
seconds = min(seconds, week)
seconds = min(seconds, hour)
require.Equal(
t,
s.unoptimizedCostOf(c, seconds),
Expand All @@ -496,7 +496,7 @@ func FuzzStateSecondsRemaining(f *testing.F) {
uint64(test.config.Target),
uint64(test.config.MinPrice),
uint64(test.config.ExcessConversionConstant),
uint64(week),
uint64(hour),
test.expectedCost,
)
}
Expand All @@ -520,7 +520,7 @@ func FuzzStateSecondsRemaining(f *testing.F) {
MinPrice: gas.Price(minPrice),
ExcessConversionConstant: gas.Gas(max(excessConversionConstant, 1)),
}
maxSeconds = min(maxSeconds, week)
maxSeconds = min(maxSeconds, hour)
require.Equal(
t,
s.unoptimizedSecondsRemaining(c, maxSeconds, targetCost),
Expand Down Expand Up @@ -551,15 +551,15 @@ func (s State) unoptimizedCostOf(c Config, seconds uint64) uint64 {

// unoptimizedSecondsRemaining is a naive implementation of SecondsRemaining
// that is used for differential fuzzing.
func (s State) unoptimizedSecondsRemaining(c Config, maxSeconds uint64, costLimit uint64) uint64 {
func (s State) unoptimizedSecondsRemaining(c Config, maxSeconds uint64, fundsRemaining uint64) uint64 {
for seconds := uint64(0); seconds < maxSeconds; seconds++ {
s = s.AdvanceTime(c.Target, 1)

price := uint64(gas.CalculatePrice(c.MinPrice, s.Excess, c.ExcessConversionConstant))
if price > costLimit {
if price > fundsRemaining {
return seconds
}
costLimit -= price
fundsRemaining -= price
}
return maxSeconds
}
Expand Down

0 comments on commit ed2be76

Please sign in to comment.