Skip to content

Commit

Permalink
Precompute EligibleBorrowedEth and cache RplStake
Browse files Browse the repository at this point in the history
  • Loading branch information
jshufro committed Nov 26, 2024
1 parent b47a29b commit 6503067
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
4 changes: 3 additions & 1 deletion shared/services/rewards/generator-impl-v8-rolling.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,12 @@ func (r *treeGeneratorImpl_v8_rolling) calculateNodeRewards() (*big.Int, *big.In
// Get the node amount
nodeInfo, exists := r.nodeDetails[minipool.NodeAddress]
if !exists {
nodeDetails := r.networkState.NodeDetailsByAddress[minipool.NodeAddress]
nodeInfo = &NodeSmoothingDetails{
Minipools: []*MinipoolInfo{},
SmoothingPoolEth: big.NewInt(0),
RewardsNetwork: r.networkState.NodeDetailsByAddress[minipool.NodeAddress].RewardNetwork.Uint64(),
RewardsNetwork: nodeDetails.RewardNetwork.Uint64(),
RplStake: nodeDetails.RplStake,
}
r.nodeDetails[minipool.NodeAddress] = nodeInfo
}
Expand Down
7 changes: 7 additions & 0 deletions shared/services/rewards/generator-impl-v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ func (r *treeGeneratorImpl_v8) getSmoothingPoolNodeDetails() error {
Minipools: []*MinipoolInfo{},
SmoothingPoolEth: big.NewInt(0),
RewardsNetwork: nativeNodeDetails.RewardNetwork.Uint64(),
RplStake: nativeNodeDetails.RplStake,
}

nodeDetails.IsOptedIn = nativeNodeDetails.SmoothingPoolRegistrationState
Expand Down Expand Up @@ -1105,6 +1106,12 @@ func (r *treeGeneratorImpl_v8) getSmoothingPoolNodeDetails() error {
}
}

// Populate the eligible borrowed ETH field for all nodes
for _, nodeDetails := range r.nodeDetails {
nnd := r.networkState.NodeDetailsByAddress[nodeDetails.Address]
nodeDetails.EligibleBorrowedEth = r.networkState.GetEligibleBorrowedEth(nnd)
}

return nil

}
Expand Down
1 change: 1 addition & 0 deletions shared/services/rewards/generator-impl-v9-v10-rolling.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ func (r *treeGeneratorImpl_v9_v10_rolling) calculateNodeRewards() (*big.Int, *bi
SmoothingPoolEth: big.NewInt(0),
BonusEth: big.NewInt(0),
RewardsNetwork: nnd.RewardNetwork.Uint64(),
RplStake: nnd.RplStake,
}
nodeInfo.IsOptedIn = nnd.SmoothingPoolRegistrationState
statusChangeTimeBig := nnd.SmoothingPoolRegistrationChanged
Expand Down
26 changes: 15 additions & 11 deletions shared/services/rewards/generator-impl-v9-v10.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,8 @@ func (r *treeGeneratorImpl_v9_v10) calculateEthRewards(checkBeaconPerformance bo
for _, nodeInfo := range r.nodeDetails {
// Check if the node is currently opted in for simplicity
if nodeInfo.IsEligible && nodeInfo.IsOptedIn && r.elEndTime.After(nodeInfo.OptInTime) {
nnd := r.networkState.NodeDetailsByAddress[nodeInfo.Address]
eligibleBorrowedEth := r.networkState.GetEligibleBorrowedEth(nnd)
_, percentOfBorrowedEth := r.networkState.GetStakedRplValueInEthAndPercentOfBorrowedEth(eligibleBorrowedEth, nnd.RplStake)
eligibleBorrowedEth := nodeInfo.EligibleBorrowedEth
_, percentOfBorrowedEth := r.networkState.GetStakedRplValueInEthAndPercentOfBorrowedEth(eligibleBorrowedEth, nodeInfo.RplStake)
for _, minipool := range nodeInfo.Minipools {
minipool.CompletedAttestations = map[uint64]bool{0: true}

Expand Down Expand Up @@ -622,16 +621,15 @@ func (r *treeGeneratorImpl_v9_v10) calculateNodeBonuses() (*big.Int, error) {
continue
}

nnd := r.networkState.NodeDetailsByAddress[nsd.Address]
eligible, _, eligibleEnd := nnd.IsEligibleForBonuses(r.elStartTime, r.elEndTime)
nodeDetails := r.networkState.NodeDetailsByAddress[nsd.Address]
eligible, _, eligibleEnd := nodeDetails.IsEligibleForBonuses(r.elStartTime, r.elEndTime)
if !eligible {
continue
}

// Get the nodeDetails from the network state
nodeDetails := r.networkState.NodeDetailsByAddress[nsd.Address]
eligibleBorrowedEth := r.networkState.GetEligibleBorrowedEth(nodeDetails)
_, percentOfBorrowedEth := r.networkState.GetStakedRplValueInEthAndPercentOfBorrowedEth(eligibleBorrowedEth, nodeDetails.RplStake)
eligibleBorrowedEth := nsd.EligibleBorrowedEth
_, percentOfBorrowedEth := r.networkState.GetStakedRplValueInEthAndPercentOfBorrowedEth(eligibleBorrowedEth, nsd.RplStake)
for _, mpd := range nsd.Minipools {
mpi := r.networkState.MinipoolDetailsByAddress[mpd.Address]
if !mpi.IsEligibleForBonuses(eligibleEnd) {
Expand Down Expand Up @@ -970,9 +968,8 @@ func (r *treeGeneratorImpl_v9_v10) checkAttestations(attestations []beacon.Attes
continue
}

nnd := r.networkState.NodeDetailsByAddress[validator.NodeAddress]
eligibleBorrowedEth := r.networkState.GetEligibleBorrowedEth(nnd)
_, percentOfBorrowedEth := r.networkState.GetStakedRplValueInEthAndPercentOfBorrowedEth(eligibleBorrowedEth, nnd.RplStake)
eligibleBorrowedEth := nodeDetails.EligibleBorrowedEth
_, percentOfBorrowedEth := r.networkState.GetStakedRplValueInEthAndPercentOfBorrowedEth(eligibleBorrowedEth, nodeDetails.RplStake)

// Mark this duty as completed
validator.CompletedAttestations[attestation.SlotIndex] = true
Expand Down Expand Up @@ -1150,6 +1147,7 @@ func (r *treeGeneratorImpl_v9_v10) getSmoothingPoolNodeDetails() error {
SmoothingPoolEth: big.NewInt(0),
BonusEth: big.NewInt(0),
RewardsNetwork: nativeNodeDetails.RewardNetwork.Uint64(),
RplStake: nativeNodeDetails.RplStake,
}

nodeDetails.IsOptedIn = nativeNodeDetails.SmoothingPoolRegistrationState
Expand Down Expand Up @@ -1205,6 +1203,12 @@ func (r *treeGeneratorImpl_v9_v10) getSmoothingPoolNodeDetails() error {
}
}

// Populate the eligible borrowed ETH field for all nodes
for _, nodeDetails := range r.nodeDetails {
nnd := r.networkState.NodeDetailsByAddress[nodeDetails.Address]
nodeDetails.EligibleBorrowedEth = r.networkState.GetEligibleBorrowedEth(nnd)
}

return nil

}
Expand Down
4 changes: 3 additions & 1 deletion shared/services/rewards/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ type NodeSmoothingDetails struct {
OptOutTime time.Time

// v10 Fields
BonusEth *big.Int
BonusEth *big.Int
EligibleBorrowedEth *big.Int
RplStake *big.Int
}

type QuotedBigInt struct {
Expand Down

0 comments on commit 6503067

Please sign in to comment.