Skip to content

Commit

Permalink
Merge pull request #11 from jshufro/jms/v10-opt
Browse files Browse the repository at this point in the history
One small optimization, two small bugfixes for v10
  • Loading branch information
jshufro authored Nov 26, 2024
2 parents 157ca6f + 6503067 commit 06fcc7f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
2 changes: 2 additions & 0 deletions shared/services/beacon/client/std-http-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ func (c *StandardHttpClient) GetBeaconBlock(blockId string) (beacon.BeaconBlock,
}

// Add attestation info
beaconBlock.Attestations = make([]beacon.AttestationInfo, 0, len(block.Data.Message.Body.Attestations))
for i, attestation := range block.Data.Message.Body.Attestations {
bitString := hexutil.RemovePrefix(attestation.AggregationBits)
info := beacon.AttestationInfo{
Expand All @@ -664,6 +665,7 @@ func (c *StandardHttpClient) GetBeaconBlock(blockId string) (beacon.BeaconBlock,
}

// Add withdrawals
beaconBlock.Withdrawals = make([]beacon.WithdrawalInfo, 0, len(block.Data.Message.Body.ExecutionPayload.Withdrawals))
for _, withdrawal := range block.Data.Message.Body.ExecutionPayload.Withdrawals {
amount, ok := new(big.Int).SetString(withdrawal.Amount, 10)
if !ok {
Expand Down
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
4 changes: 4 additions & 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 Expand Up @@ -687,6 +688,9 @@ func (r *treeGeneratorImpl_v9_v10_rolling) calculateNodeRewards() (*big.Int, *bi
// Calculate the reduced bonus for each minipool
// Because of integer division, this will be less than the actual bonus by up to 1 wei
for _, mpd := range nsd.Minipools {
if mpd.MinipoolBonus == nil {
continue
}
mpd.MinipoolBonus.Mul(mpd.MinipoolBonus, remainingBalance)
mpd.MinipoolBonus.Div(mpd.MinipoolBonus, totalConsensusBonus)
}
Expand Down
33 changes: 22 additions & 11 deletions shared/services/rewards/generator-impl-v9-v10.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/big"
"sort"
"sync"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -507,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 @@ -621,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 @@ -739,6 +738,9 @@ func (r *treeGeneratorImpl_v9_v10) calculateNodeRewards() (*big.Int, *big.Int, *
// Calculate the reduced bonus for each minipool
// Because of integer division, this will be less than the actual bonus by up to 1 wei
for _, mpd := range nsd.Minipools {
if mpd.MinipoolBonus == nil {
continue
}
mpd.MinipoolBonus.Mul(mpd.MinipoolBonus, remainingBalance)
mpd.MinipoolBonus.Div(mpd.MinipoolBonus, totalConsensusBonus)
}
Expand Down Expand Up @@ -839,6 +841,7 @@ func (r *treeGeneratorImpl_v9_v10) processEpoch(duringInterval bool, epoch uint6
})
}

withdrawalsLock := &sync.Mutex{}
for i := uint64(0); i < r.slotsPerEpoch; i++ {
// Get the beacon block for this slot
i := i
Expand Down Expand Up @@ -891,11 +894,13 @@ func (r *treeGeneratorImpl_v9_v10) processEpoch(duringInterval bool, epoch uint6
}

// Create the minipool's withdrawal sum big.Int if it doesn't exist
withdrawalsLock.Lock()
if r.minipoolWithdrawals[mpi.Address] == nil {
r.minipoolWithdrawals[mpi.Address] = big.NewInt(0)
}
// Add the withdrawal amount
r.minipoolWithdrawals[mpi.Address].Add(r.minipoolWithdrawals[mpi.Address], withdrawalAmount)
withdrawalsLock.Unlock()
}
return nil
})
Expand Down Expand Up @@ -963,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 @@ -1143,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 @@ -1198,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: 4 additions & 0 deletions shared/services/rewards/rolling-record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rewards
import (
"fmt"
"math/big"
"sync"
"time"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -344,6 +345,7 @@ func (r *RollingRecord) processAttestationsInEpoch(epoch uint64, state *state.Ne
attestationsPerSlot := make([][]beacon.AttestationInfo, r.beaconConfig.SlotsPerEpoch)

// Get the attestation records for this epoch
withdrawalsLock := &sync.Mutex{}
for i := uint64(0); i < slotsPerEpoch; i++ {
i := i
slot := epoch*slotsPerEpoch + i
Expand Down Expand Up @@ -392,12 +394,14 @@ func (r *RollingRecord) processAttestationsInEpoch(epoch uint64, state *state.Ne
}

// Create the minipool's income big.Int if it doesn't exist
withdrawalsLock.Lock()
if mpi.ConsensusIncome == nil {
mpi.ConsensusIncome = NewQuotedBigInt(0)
}

// Add the withdrawal amount as consensus income
mpi.ConsensusIncome.Add(&mpi.ConsensusIncome.Int, withdrawalAmount)
withdrawalsLock.Unlock()
}
}
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 06fcc7f

Please sign in to comment.