Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 13 additions & 8 deletions bindings/utils/state/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ const (
threadLimit int = 10
)

// Global constants
var zero = big.NewInt(0)
// BigTime is a variable-sized big.Int from an evm 256-bit integer that represents a Unix time in seconds
type bigTime struct {
big.Int
}

// BigDuration is a variable-sized big.Int from an evm 256-bit integer that represents a duration in seconds
type bigDuration struct {
big.Int
}

// Converts a time on the chain (as Unix time in seconds) to a time.Time struct
func convertToTime(value *big.Int) time.Time {
return time.Unix(value.Int64(), 0)
func (b *bigTime) toTime() time.Time {
return time.Unix(b.Int64(), 0)
}

// Converts a duration on the chain (as a number of seconds) to a time.Duration struct
func convertToDuration(value *big.Int) time.Duration {
return time.Duration(value.Uint64()) * time.Second
func (b *bigDuration) toDuration() time.Duration {
return time.Duration(b.Uint64()) * time.Second
}
6 changes: 3 additions & 3 deletions bindings/utils/state/minipool.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func CalculateCompleteMinipoolShares(rp *rocketpool.RocketPool, contracts *Netwo

// Calculate the Beacon shares
beaconBalance := big.NewInt(0).Set(beaconBalances[j])
if beaconBalance.Cmp(zero) > 0 {
if beaconBalance.Sign() > 0 {
mc.AddCall(mpContract, &details.NodeShareOfBeaconBalance, "calculateNodeShare", beaconBalance)
mc.AddCall(mpContract, &details.UserShareOfBeaconBalance, "calculateUserShare", beaconBalance)
} else {
Expand All @@ -214,7 +214,7 @@ func CalculateCompleteMinipoolShares(rp *rocketpool.RocketPool, contracts *Netwo
totalBalance.Sub(totalBalance, details.NodeRefundBalance) // Remove node refund

// Calculate the node and user shares
if totalBalance.Cmp(zero) > 0 {
if totalBalance.Sign() > 0 {
mc.AddCall(mpContract, &details.NodeShareOfBalanceIncludingBeacon, "calculateNodeShare", totalBalance)
mc.AddCall(mpContract, &details.UserShareOfBalanceIncludingBeacon, "calculateUserShare", totalBalance)
} else {
Expand Down Expand Up @@ -581,7 +581,7 @@ func addMinipoolShareCalls(rp *rocketpool.RocketPool, mc *multicall.MultiCaller,
mpContract := mp.GetContract()

details.DistributableBalance = big.NewInt(0).Sub(details.Balance, details.NodeRefundBalance)
if details.DistributableBalance.Cmp(zero) >= 0 {
if details.DistributableBalance.Sign() >= 0 {
mc.AddCall(mpContract, &details.NodeShareOfBalance, "calculateNodeShare", details.DistributableBalance)
mc.AddCall(mpContract, &details.UserShareOfBalance, "calculateUserShare", details.DistributableBalance)
} else {
Expand Down
24 changes: 12 additions & 12 deletions bindings/utils/state/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func NewNetworkDetails(rp *rocketpool.RocketPool, contracts *NetworkContracts) (

// Local vars for things that need to be converted
var rewardIndex *big.Int
var intervalStart *big.Int
var intervalDuration *big.Int
var scrubPeriodSeconds *big.Int
var intervalStart *bigTime
var intervalDuration *bigDuration
var scrubPeriodSeconds *bigDuration
var totalQueueCapacity *big.Int
var effectiveQueueCapacity *big.Int
var totalQueueLength *big.Int
Expand All @@ -90,9 +90,9 @@ func NewNetworkDetails(rp *rocketpool.RocketPool, contracts *NetworkContracts) (
var balancesBlock *big.Int
var balancesSubmissionFrequency *big.Int
var minipoolLaunchTimeout *big.Int
var promotionScrubPeriodSeconds *big.Int
var windowStartRaw *big.Int
var windowLengthRaw *big.Int
var promotionScrubPeriodSeconds *bigDuration
var windowStartRaw *bigDuration
var windowLengthRaw *bigDuration

// Multicall getters
contracts.Multicaller.AddCall(contracts.RocketNetworkPrices, &details.RplPrice, "getRPLPrice")
Expand Down Expand Up @@ -143,9 +143,9 @@ func NewNetworkDetails(rp *rocketpool.RocketPool, contracts *NetworkContracts) (

// Conversion for raw parameters
details.RewardIndex = rewardIndex.Uint64()
details.IntervalStart = convertToTime(intervalStart)
details.IntervalDuration = convertToDuration(intervalDuration)
details.ScrubPeriod = convertToDuration(scrubPeriodSeconds)
details.IntervalStart = intervalStart.toTime()
details.IntervalDuration = intervalDuration.toDuration()
details.ScrubPeriod = scrubPeriodSeconds.toDuration()
details.SmoothingPoolAddress = *contracts.RocketSmoothingPool.Address
details.QueueCapacity = minipool.QueueCapacity{
Total: totalQueueCapacity,
Expand All @@ -161,9 +161,9 @@ func NewNetworkDetails(rp *rocketpool.RocketPool, contracts *NetworkContracts) (
details.NodeFee = eth.WeiToEth(nodeFee)
details.BalancesBlock = balancesBlock.Uint64()
details.MinipoolLaunchTimeout = minipoolLaunchTimeout
details.PromotionScrubPeriod = convertToDuration(promotionScrubPeriodSeconds)
details.BondReductionWindowStart = convertToDuration(windowStartRaw)
details.BondReductionWindowLength = convertToDuration(windowLengthRaw)
details.PromotionScrubPeriod = promotionScrubPeriodSeconds.toDuration()
details.BondReductionWindowStart = windowStartRaw.toDuration()
details.BondReductionWindowLength = windowLengthRaw.toDuration()

// Get various balances
addresses := []common.Address{
Expand Down
8 changes: 4 additions & 4 deletions bindings/utils/state/odao.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type OracleDaoMemberDetails struct {
RPLBondAmount *big.Int `json:"rplBondAmount"`
ReplacementAddress common.Address `json:"replacementAddress"`
IsChallenged bool `json:"isChallenged"`
joinedTimeRaw *big.Int `json:"-"`
lastProposalTimeRaw *big.Int `json:"-"`
joinedTimeRaw *bigTime `json:"-"`
lastProposalTimeRaw *bigTime `json:"-"`
}

// Gets the details for an Oracle DAO member using the efficient multicall contract
Expand Down Expand Up @@ -182,7 +182,7 @@ func addOracleDaoMemberDetailsCalls(contracts *NetworkContracts, mc *multicall.M

// Fixes a member details struct with supplemental logic
func fixupOracleDaoMemberDetails(details *OracleDaoMemberDetails) error {
details.JoinedTime = convertToTime(details.joinedTimeRaw)
details.LastProposalTime = convertToTime(details.lastProposalTimeRaw)
details.JoinedTime = details.joinedTimeRaw.toTime()
details.LastProposalTime = details.lastProposalTimeRaw.toTime()
return nil
}
6 changes: 1 addition & 5 deletions rocketpool/node/collectors/node-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ func (collector *NodeCollector) Collect(channel chan<- prometheus.Metric) {
rewardsInterval := state.NetworkDetails.IntervalDuration
inflationInterval := state.NetworkDetails.RPLInflationIntervalRate
totalRplSupply := state.NetworkDetails.RPLTotalSupply
totalEffectiveStake := collector.stateLocker.GetTotalEffectiveRPLStake()
nodeOperatorRewardsPercent := eth.WeiToEth(state.NetworkDetails.NodeOperatorRewardsPercent)
previousIntervalTotalNodeWeight := big.NewInt(0)
ethBalance := eth.WeiToEth(nd.BalanceETH)
Expand All @@ -280,9 +279,6 @@ func (collector *NodeCollector) Collect(channel chan<- prometheus.Metric) {
var beaconHead beacon.BeaconHead
unclaimedEthRewards := float64(0)
unclaimedRplRewards := float64(0)
if totalEffectiveStake == nil {
return
}

// Get the cumulative claimed and unclaimed RPL rewards
wg.Go(func() error {
Expand Down Expand Up @@ -531,7 +527,7 @@ func (collector *NodeCollector) Collect(channel chan<- prometheus.Metric) {
* period we don't attempt an estimate and simply use 0.
*/
estimatedRewards := float64(0)
if totalEffectiveStake.Cmp(big.NewInt(0)) == 1 && nodeWeight.Cmp(big.NewInt(0)) == 1 && state.NetworkDetails.RewardIndex > 0 {
if nodeWeight.Cmp(big.NewInt(0)) == 1 && state.NetworkDetails.RewardIndex > 0 {

nodeWeightSum := big.NewInt(0).Add(nodeWeight, previousIntervalTotalNodeWeight)

Expand Down
9 changes: 4 additions & 5 deletions rocketpool/node/collectors/rpl-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type RplCollector struct {
totalValueStaked *prometheus.Desc

// The total effective amount of RPL staked on the network
// Obsolete, but still populated so the dashboard can show it.
totalEffectiveStaked *prometheus.Desc

// The date and time of the next RPL rewards checkpoint
Expand Down Expand Up @@ -81,20 +82,18 @@ func (collector *RplCollector) Collect(channel chan<- prometheus.Metric) {

rplPriceFloat := eth.WeiToEth(state.NetworkDetails.RplPrice)
totalValueStakedFloat := eth.WeiToEth(state.NetworkDetails.TotalRPLStake)
totalEffectiveStake := collector.stateLocker.GetTotalEffectiveRPLStake()
lastCheckpoint := state.NetworkDetails.IntervalStart
rewardsInterval := state.NetworkDetails.IntervalDuration
nextRewardsTime := float64(lastCheckpoint.Add(rewardsInterval).Unix()) * 1000
if totalEffectiveStake == nil {
return
}

channel <- prometheus.MustNewConstMetric(
collector.rplPrice, prometheus.GaugeValue, rplPriceFloat)
channel <- prometheus.MustNewConstMetric(
collector.totalValueStaked, prometheus.GaugeValue, totalValueStakedFloat)
// All staked RPL is effective RPL, but this metric is on the dashboard so we
// should keep populating it for now.
channel <- prometheus.MustNewConstMetric(
collector.totalEffectiveStaked, prometheus.GaugeValue, eth.WeiToEth(totalEffectiveStake))
collector.totalEffectiveStaked, prometheus.GaugeValue, totalValueStakedFloat)
channel <- prometheus.MustNewConstMetric(
collector.checkpointTime, prometheus.GaugeValue, nextRewardsTime)
}
Expand Down
23 changes: 6 additions & 17 deletions rocketpool/node/collectors/state-locker.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
package collectors

import (
"math/big"
"sync"

"github.com/rocket-pool/smartnode/shared/services/state"
)

type StateLocker struct {
state *state.NetworkState
totalEffectiveStake *big.Int
state *state.NetworkState

// Internal fields
lock *sync.Mutex
lock *sync.RWMutex
}

func NewStateLocker() *StateLocker {
return &StateLocker{
lock: &sync.Mutex{},
lock: &sync.RWMutex{},
}
}

func (l *StateLocker) UpdateState(state *state.NetworkState, totalEffectiveStake *big.Int) {
func (l *StateLocker) UpdateState(state *state.NetworkState) {
l.lock.Lock()
defer l.lock.Unlock()
l.state = state
if totalEffectiveStake != nil {
l.totalEffectiveStake = totalEffectiveStake
}
}

func (l *StateLocker) GetState() *state.NetworkState {
l.lock.Lock()
defer l.lock.Unlock()
l.lock.RLock()
defer l.lock.RUnlock()
return l.state
}

func (l *StateLocker) GetTotalEffectiveRPLStake() *big.Int {
l.lock.Lock()
defer l.lock.Unlock()
return l.totalEffectiveStake
}
21 changes: 6 additions & 15 deletions rocketpool/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
// Config
var tasksInterval, _ = time.ParseDuration("5m")
var taskCooldown, _ = time.ParseDuration("10s")
var totalEffectiveStakeCooldown, _ = time.ParseDuration("1h")

const (
MaxConcurrentEth1Requests = 200
Expand Down Expand Up @@ -172,9 +171,6 @@ func run(c *cli.Context) error {
wg := new(sync.WaitGroup)
wg.Add(2)

// Timestamp for caching total effective RPL stake
lastTotalEffectiveStakeTime := time.Unix(0, 0)

// Run task loop
go func() {
// we assume clients are synced on startup so that we don't send unnecessary alerts
Expand Down Expand Up @@ -213,18 +209,13 @@ func run(c *cli.Context) error {
}

// Update the network state
updateTotalEffectiveStake := false
if time.Since(lastTotalEffectiveStakeTime) > totalEffectiveStakeCooldown {
updateTotalEffectiveStake = true
lastTotalEffectiveStakeTime = time.Now() // Even if the call below errors out, this will prevent contant errors related to this flag
}
state, totalEffectiveStake, err := updateNetworkState(m, &updateLog, nodeAccount.Address, updateTotalEffectiveStake)
state, err := updateNetworkState(m, &updateLog, nodeAccount.Address)
if err != nil {
errorLog.Println(err)
time.Sleep(taskCooldown)
continue
}
stateLocker.UpdateState(state, totalEffectiveStake)
stateLocker.UpdateState(state)

// Manage the fee recipient for the node
if err := manageFeeRecipient.run(state); err != nil {
Expand Down Expand Up @@ -382,13 +373,13 @@ func removeLegacyFeeRecipientFiles(c *cli.Context) error {
}

// Update the latest network state at each cycle
func updateNetworkState(m *state.NetworkStateManager, log *log.ColorLogger, nodeAddress common.Address, calculateTotalEffectiveStake bool) (*state.NetworkState, *big.Int, error) {
func updateNetworkState(m *state.NetworkStateManager, log *log.ColorLogger, nodeAddress common.Address) (*state.NetworkState, error) {
// Get the state of the network
state, totalEffectiveStake, err := m.GetHeadStateForNode(nodeAddress, calculateTotalEffectiveStake)
state, err := m.GetHeadStateForNode(nodeAddress)
if err != nil {
return nil, nil, fmt.Errorf("error updating network state: %w", err)
return nil, fmt.Errorf("error updating network state: %w", err)
}
return state, totalEffectiveStake, nil
return state, nil
}

// Checks if the user-inputted priorityFee is greater than the oracle based maxFee
Expand Down
Loading
Loading