Skip to content

Commit

Permalink
Fix the tree version iterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfornax committed Oct 11, 2024
1 parent 75e68ea commit 3e96639
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions rocketpool-daemon/common/rewards/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"math/big"
"slices"
"time"

"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -160,36 +161,38 @@ func NewTreeGenerator(logger *slog.Logger, rp *rocketpool.RocketPool, cfg *confi
// Get the current network
network := t.cfg.Network.Value

// Determine which actual rulesets to use based on the current interval number, checking in descending order from the latest
// to interval 2 since interval 1 is the default
// Determine which actual rulesets to use based on the current interval number, checking in descending order.
foundGenerator := false
foundApproximator := false
for i := uint64(len(t.rewardsIntervalInfos)); i > 1; i-- {
info := t.rewardsIntervalInfos[i]

// Sort by version number, reversed. That way we will pick the highest version number whose startInterval
// is eligible
slices.SortFunc(rewardsIntervalInfos, func(a, b rewardsIntervalInfo) int {
// b - a sorts high to low
return int(b.rewardsRulesetVersion) - int(a.rewardsRulesetVersion)
})

// The first ruleset whose startInterval is at most t.index is the one to use
// for treegen, and for some reason, the first ruleset whose start interval is less than t.index
// is the one to use for approximations
for _, info := range rewardsIntervalInfos {

startInterval, err := info.GetStartInterval(network)
if err != nil {
return nil, fmt.Errorf("error getting start interval for rewards period %d: %w", i, err)
return nil, fmt.Errorf("error getting start interval for rewards period %d: %w", t.index, err)
}
if !foundGenerator && t.index >= startInterval {
if !foundGenerator && startInterval <= t.index {
t.generatorImpl = info.generator
foundGenerator = true
}
if !foundApproximator && t.index > startInterval {
if !foundApproximator && startInterval < t.index {
t.approximatorImpl = info.generator
foundApproximator = true
}

if foundGenerator && foundApproximator {
break
}
}

// Default to interval 1 if nothing could be found
if !foundGenerator {
t.generatorImpl = t.rewardsIntervalInfos[1].generator
}
if !foundApproximator {
t.approximatorImpl = t.rewardsIntervalInfos[1].generator
if !foundGenerator || !foundApproximator {
// Do not default- require intervals to be explicit
return nil, fmt.Errorf("No treegen implementation could be found for interval %d", t.index)
}

return t, nil
Expand Down

0 comments on commit 3e96639

Please sign in to comment.