Skip to content

Commit

Permalink
Create interface for rp rewards execution functions to support future…
Browse files Browse the repository at this point in the history
… dependency injection
  • Loading branch information
jshufro committed Sep 25, 2024
1 parent 19ca26b commit a37e00a
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 54 deletions.
4 changes: 3 additions & 1 deletion rocketpool/watchtower/generate-rewards-tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ func (t *generateRewardsTree) generateRewardsTree(index uint64) {
// Get previous rewards pool addresses
previousRewardsPoolAddresses := t.cfg.Smartnode.GetPreviousRewardsPoolAddresses()

rewardsClient := rprewards.NewRewardsExecutionClient(t.rp)

// Find the event for this interval
rewardsEvent, err := rprewards.GetRewardSnapshotEvent(t.rp, previousRewardsPoolAddresses, index, nil)
rewardsEvent, err := rewardsClient.GetRewardSnapshotEvent(previousRewardsPoolAddresses, index, nil)
if err != nil {
t.handleError(fmt.Errorf("%s Error getting event for interval %d: %w", generationPrefix, index, err))
return
Expand Down
56 changes: 56 additions & 0 deletions shared/services/rewards/execution-client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package rewards

import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/rocket-pool/rocketpool-go/rewards"
"github.com/rocket-pool/rocketpool-go/rocketpool"
"github.com/rocket-pool/rocketpool-go/settings/trustednode"
)

// An implementation of RewardsExecutionClient that uses
// rocketpool-go to access chain data.
//
// Importantly, this struct instantiates rocketpool.RocketPool and passes it
// to the old fashioned rocketpool-go getters that take it as an argument
// but it also fulfills the requirements of an interface used for dependency injection
// in tests.
type defaultRewardsExecutionClient struct {
*rocketpool.RocketPool
}

func NewRewardsExecutionClient(rp *rocketpool.RocketPool) (out *defaultRewardsExecutionClient) {
out.RocketPool = rp
return
}

func (client *defaultRewardsExecutionClient) GetNetworkEnabled(networkId *big.Int, opts *bind.CallOpts) (bool, error) {
return trustednode.GetNetworkEnabled(client.RocketPool, networkId, opts)
}

func (client *defaultRewardsExecutionClient) HeaderByNumber(ctx context.Context, block *big.Int) (*ethtypes.Header, error) {
return client.RocketPool.Client.HeaderByNumber(ctx, block)
}

func (client *defaultRewardsExecutionClient) GetRewardsEvent(index uint64, rocketRewardsPoolAddresses []common.Address, opts *bind.CallOpts) (bool, rewards.RewardsEvent, error) {
return rewards.GetRewardsEvent(client.RocketPool, index, rocketRewardsPoolAddresses, opts)
}

func (client *defaultRewardsExecutionClient) GetRewardSnapshotEvent(previousRewardsPoolAddresses []common.Address, interval uint64, opts *bind.CallOpts) (rewards.RewardsEvent, error) {

found, event, err := client.GetRewardsEvent(interval, previousRewardsPoolAddresses, opts)
if err != nil {
return rewards.RewardsEvent{}, fmt.Errorf("error getting rewards event for interval %d: %w", interval, err)
}
if !found {
return rewards.RewardsEvent{}, fmt.Errorf("interval %d event not found", interval)
}

return event, nil

}
12 changes: 5 additions & 7 deletions shared/services/rewards/generator-impl-v8-rolling.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ipfs/go-cid"
"github.com/rocket-pool/rocketpool-go/rocketpool"
tnsettings "github.com/rocket-pool/rocketpool-go/settings/trustednode"
"github.com/rocket-pool/rocketpool-go/utils/eth"
"github.com/rocket-pool/smartnode/shared/services/beacon"
"github.com/rocket-pool/smartnode/shared/services/config"
Expand All @@ -30,7 +28,7 @@ type treeGeneratorImpl_v8_rolling struct {
elSnapshotHeader *types.Header
log *log.ColorLogger
logPrefix string
rp *rocketpool.RocketPool
rp RewardsExecutionClient
previousRewardsPoolAddresses []common.Address
bc beacon.Client
opts *bind.CallOpts
Expand Down Expand Up @@ -97,7 +95,7 @@ func (r *treeGeneratorImpl_v8_rolling) getRulesetVersion() uint64 {
return r.rewardsFile.RulesetVersion
}

func (r *treeGeneratorImpl_v8_rolling) generateTree(rp *rocketpool.RocketPool, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {
func (r *treeGeneratorImpl_v8_rolling) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {

r.log.Printlnf("%s Generating tree using Ruleset v%d.", r.logPrefix, r.rewardsFile.RulesetVersion)

Expand Down Expand Up @@ -173,7 +171,7 @@ func (r *treeGeneratorImpl_v8_rolling) generateTree(rp *rocketpool.RocketPool, n

// Quickly calculates an approximate of the staker's share of the smoothing pool balance without processing Beacon performance
// Used for approximate returns in the rETH ratio update
func (r *treeGeneratorImpl_v8_rolling) approximateStakerShareOfSmoothingPool(rp *rocketpool.RocketPool, networkName string, bc beacon.Client) (*big.Int, error) {
func (r *treeGeneratorImpl_v8_rolling) approximateStakerShareOfSmoothingPool(rp RewardsExecutionClient, networkName string, bc beacon.Client) (*big.Int, error) {
r.log.Printlnf("%s Approximating tree using Ruleset v%d.", r.logPrefix, r.rewardsFile.RulesetVersion)

r.rp = rp
Expand Down Expand Up @@ -758,7 +756,7 @@ func (r *treeGeneratorImpl_v8_rolling) validateNetwork(network uint64) (bool, er
valid, exists := r.validNetworkCache[network]
if !exists {
var err error
valid, err = tnsettings.GetNetworkEnabled(r.rp, big.NewInt(int64(network)), r.opts)
valid, err = r.rp.GetNetworkEnabled(big.NewInt(int64(network)), r.opts)
if err != nil {
return false, err
}
Expand All @@ -785,7 +783,7 @@ func (r *treeGeneratorImpl_v8_rolling) getStartBlocksForInterval() (*types.Heade
elBlockNumber := beaconBlock.ExecutionBlockNumber
r.rewardsFile.ExecutionStartBlock = elBlockNumber
r.rewardsFile.MinipoolPerformanceFile.ExecutionStartBlock = r.rewardsFile.ExecutionStartBlock
startElHeader, err := r.rp.Client.HeaderByNumber(context.Background(), big.NewInt(int64(elBlockNumber)))
startElHeader, err := r.rp.HeaderByNumber(context.Background(), big.NewInt(int64(elBlockNumber)))
if err != nil {
return nil, fmt.Errorf("error getting EL header for block %d: %w", elBlockNumber, err)
}
Expand Down
16 changes: 7 additions & 9 deletions shared/services/rewards/generator-impl-v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ipfs/go-cid"
"github.com/rocket-pool/rocketpool-go/rewards"
"github.com/rocket-pool/rocketpool-go/rocketpool"
tnsettings "github.com/rocket-pool/rocketpool-go/settings/trustednode"
rptypes "github.com/rocket-pool/rocketpool-go/types"
"github.com/rocket-pool/rocketpool-go/utils/eth"
rpstate "github.com/rocket-pool/rocketpool-go/utils/state"
Expand All @@ -33,7 +31,7 @@ type treeGeneratorImpl_v8 struct {
elSnapshotHeader *types.Header
log *log.ColorLogger
logPrefix string
rp *rocketpool.RocketPool
rp RewardsExecutionClient
previousRewardsPoolAddresses []common.Address
bc beacon.Client
opts *bind.CallOpts
Expand Down Expand Up @@ -104,7 +102,7 @@ func (r *treeGeneratorImpl_v8) getRulesetVersion() uint64 {
return r.rewardsFile.RulesetVersion
}

func (r *treeGeneratorImpl_v8) generateTree(rp *rocketpool.RocketPool, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {
func (r *treeGeneratorImpl_v8) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {

r.log.Printlnf("%s Generating tree using Ruleset v%d.", r.logPrefix, r.rewardsFile.RulesetVersion)

Expand Down Expand Up @@ -181,7 +179,7 @@ func (r *treeGeneratorImpl_v8) generateTree(rp *rocketpool.RocketPool, networkNa

// Quickly calculates an approximate of the staker's share of the smoothing pool balance without processing Beacon performance
// Used for approximate returns in the rETH ratio update
func (r *treeGeneratorImpl_v8) approximateStakerShareOfSmoothingPool(rp *rocketpool.RocketPool, networkName string, bc beacon.Client) (*big.Int, error) {
func (r *treeGeneratorImpl_v8) approximateStakerShareOfSmoothingPool(rp RewardsExecutionClient, networkName string, bc beacon.Client) (*big.Int, error) {
r.log.Printlnf("%s Approximating tree using Ruleset v%d.", r.logPrefix, r.rewardsFile.RulesetVersion)

r.rp = rp
Expand Down Expand Up @@ -535,7 +533,7 @@ func (r *treeGeneratorImpl_v8) calculateEthRewards(checkBeaconPerformance bool)

// Get the start time of this interval based on the event from the previous one
//previousIntervalEvent, err := GetRewardSnapshotEvent(r.rp, r.cfg, r.rewardsFile.Index-1, r.opts) // This is immutable so querying at the head is fine and mitigates issues around calls for pruned EL state
previousIntervalEvent, err := GetRewardSnapshotEvent(r.rp, r.previousRewardsPoolAddresses, r.rewardsFile.Index-1, nil)
previousIntervalEvent, err := r.rp.GetRewardSnapshotEvent(r.previousRewardsPoolAddresses, r.rewardsFile.Index-1, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -1116,7 +1114,7 @@ func (r *treeGeneratorImpl_v8) validateNetwork(network uint64) (bool, error) {
valid, exists := r.validNetworkCache[network]
if !exists {
var err error
valid, err = tnsettings.GetNetworkEnabled(r.rp, big.NewInt(int64(network)), r.opts)
valid, err = r.rp.GetNetworkEnabled(big.NewInt(int64(network)), r.opts)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -1163,15 +1161,15 @@ func (r *treeGeneratorImpl_v8) getStartBlocksForInterval(previousIntervalEvent r
// We are pre-merge, so get the first block after the one from the previous interval
r.rewardsFile.ExecutionStartBlock = previousIntervalEvent.ExecutionBlock.Uint64() + 1
r.rewardsFile.MinipoolPerformanceFile.ExecutionStartBlock = r.rewardsFile.ExecutionStartBlock
startElHeader, err = r.rp.Client.HeaderByNumber(context.Background(), big.NewInt(int64(r.rewardsFile.ExecutionStartBlock)))
startElHeader, err = r.rp.HeaderByNumber(context.Background(), big.NewInt(int64(r.rewardsFile.ExecutionStartBlock)))
if err != nil {
return nil, fmt.Errorf("error getting EL start block %d: %w", r.rewardsFile.ExecutionStartBlock, err)
}
} else {
// We are post-merge, so get the EL block corresponding to the BC block
r.rewardsFile.ExecutionStartBlock = elBlockNumber
r.rewardsFile.MinipoolPerformanceFile.ExecutionStartBlock = r.rewardsFile.ExecutionStartBlock
startElHeader, err = r.rp.Client.HeaderByNumber(context.Background(), big.NewInt(int64(elBlockNumber)))
startElHeader, err = r.rp.HeaderByNumber(context.Background(), big.NewInt(int64(elBlockNumber)))
if err != nil {
return nil, fmt.Errorf("error getting EL header for block %d: %w", elBlockNumber, err)
}
Expand Down
12 changes: 5 additions & 7 deletions shared/services/rewards/generator-impl-v9-rolling.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ipfs/go-cid"
"github.com/rocket-pool/rocketpool-go/rocketpool"
tnsettings "github.com/rocket-pool/rocketpool-go/settings/trustednode"
"github.com/rocket-pool/rocketpool-go/utils/eth"
"github.com/rocket-pool/smartnode/shared/services/beacon"
"github.com/rocket-pool/smartnode/shared/services/config"
Expand All @@ -30,7 +28,7 @@ type treeGeneratorImpl_v9_rolling struct {
snapshotEnd *SnapshotEnd
log *log.ColorLogger
logPrefix string
rp *rocketpool.RocketPool
rp RewardsExecutionClient
previousRewardsPoolAddresses []common.Address
bc beacon.Client
opts *bind.CallOpts
Expand Down Expand Up @@ -94,7 +92,7 @@ func (r *treeGeneratorImpl_v9_rolling) getRulesetVersion() uint64 {
return r.rewardsFile.RulesetVersion
}

func (r *treeGeneratorImpl_v9_rolling) generateTree(rp *rocketpool.RocketPool, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {
func (r *treeGeneratorImpl_v9_rolling) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {

r.log.Printlnf("%s Generating tree using Ruleset v%d.", r.logPrefix, r.rewardsFile.RulesetVersion)

Expand Down Expand Up @@ -178,7 +176,7 @@ func (r *treeGeneratorImpl_v9_rolling) generateTree(rp *rocketpool.RocketPool, n

// Quickly calculates an approximate of the staker's share of the smoothing pool balance without processing Beacon performance
// Used for approximate returns in the rETH ratio update
func (r *treeGeneratorImpl_v9_rolling) approximateStakerShareOfSmoothingPool(rp *rocketpool.RocketPool, networkName string, bc beacon.Client) (*big.Int, error) {
func (r *treeGeneratorImpl_v9_rolling) approximateStakerShareOfSmoothingPool(rp RewardsExecutionClient, networkName string, bc beacon.Client) (*big.Int, error) {
r.log.Printlnf("%s Approximating tree using Ruleset v%d.", r.logPrefix, r.rewardsFile.RulesetVersion)

r.rp = rp
Expand Down Expand Up @@ -646,7 +644,7 @@ func (r *treeGeneratorImpl_v9_rolling) validateNetwork(network uint64) (bool, er
valid, exists := r.validNetworkCache[network]
if !exists {
var err error
valid, err = tnsettings.GetNetworkEnabled(r.rp, big.NewInt(int64(network)), r.opts)
valid, err = r.rp.GetNetworkEnabled(big.NewInt(int64(network)), r.opts)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -674,7 +672,7 @@ func (r *treeGeneratorImpl_v9_rolling) getBlocksAndTimesForInterval() (*types.He
elBlockNumber := beaconBlock.ExecutionBlockNumber
r.rewardsFile.ExecutionStartBlock = elBlockNumber
r.minipoolPerformanceFile.ExecutionStartBlock = r.rewardsFile.ExecutionStartBlock
startElHeader, err := r.rp.Client.HeaderByNumber(context.Background(), big.NewInt(int64(elBlockNumber)))
startElHeader, err := r.rp.HeaderByNumber(context.Background(), big.NewInt(int64(elBlockNumber)))
if err != nil {
return nil, fmt.Errorf("error getting EL header for block %d: %w", elBlockNumber, err)
}
Expand Down
16 changes: 7 additions & 9 deletions shared/services/rewards/generator-impl-v9.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ipfs/go-cid"
"github.com/rocket-pool/rocketpool-go/rewards"
"github.com/rocket-pool/rocketpool-go/rocketpool"
tnsettings "github.com/rocket-pool/rocketpool-go/settings/trustednode"
rptypes "github.com/rocket-pool/rocketpool-go/types"
"github.com/rocket-pool/rocketpool-go/utils/eth"
rpstate "github.com/rocket-pool/rocketpool-go/utils/state"
Expand All @@ -37,7 +35,7 @@ type treeGeneratorImpl_v9 struct {
snapshotEnd *SnapshotEnd
log *log.ColorLogger
logPrefix string
rp *rocketpool.RocketPool
rp RewardsExecutionClient
previousRewardsPoolAddresses []common.Address
bc beacon.Client
opts *bind.CallOpts
Expand Down Expand Up @@ -105,7 +103,7 @@ func (r *treeGeneratorImpl_v9) getRulesetVersion() uint64 {
return r.rewardsFile.RulesetVersion
}

func (r *treeGeneratorImpl_v9) generateTree(rp *rocketpool.RocketPool, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {
func (r *treeGeneratorImpl_v9) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {

r.log.Printlnf("%s Generating tree using Ruleset v%d.", r.logPrefix, r.rewardsFile.RulesetVersion)

Expand Down Expand Up @@ -190,7 +188,7 @@ func (r *treeGeneratorImpl_v9) generateTree(rp *rocketpool.RocketPool, networkNa

// Quickly calculates an approximate of the staker's share of the smoothing pool balance without processing Beacon performance
// Used for approximate returns in the rETH ratio update
func (r *treeGeneratorImpl_v9) approximateStakerShareOfSmoothingPool(rp *rocketpool.RocketPool, networkName string, bc beacon.Client) (*big.Int, error) {
func (r *treeGeneratorImpl_v9) approximateStakerShareOfSmoothingPool(rp RewardsExecutionClient, networkName string, bc beacon.Client) (*big.Int, error) {
r.log.Printlnf("%s Approximating tree using Ruleset v%d.", r.logPrefix, r.rewardsFile.RulesetVersion)

r.rp = rp
Expand Down Expand Up @@ -504,7 +502,7 @@ func (r *treeGeneratorImpl_v9) calculateEthRewards(checkBeaconPerformance bool)

// Get the start time of this interval based on the event from the previous one
//previousIntervalEvent, err := GetRewardSnapshotEvent(r.rp, r.cfg, r.rewardsFile.Index-1, r.opts) // This is immutable so querying at the head is fine and mitigates issues around calls for pruned EL state
previousIntervalEvent, err := GetRewardSnapshotEvent(r.rp, r.previousRewardsPoolAddresses, r.rewardsFile.Index-1, r.opts)
previousIntervalEvent, err := r.rp.GetRewardSnapshotEvent(r.previousRewardsPoolAddresses, r.rewardsFile.Index-1, r.opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -1075,7 +1073,7 @@ func (r *treeGeneratorImpl_v9) validateNetwork(network uint64) (bool, error) {
valid, exists := r.validNetworkCache[network]
if !exists {
var err error
valid, err = tnsettings.GetNetworkEnabled(r.rp, big.NewInt(int64(network)), r.opts)
valid, err = r.rp.GetNetworkEnabled(big.NewInt(int64(network)), r.opts)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -1139,15 +1137,15 @@ func (r *treeGeneratorImpl_v9) getBlocksAndTimesForInterval(previousIntervalEven
// We are pre-merge, so get the first block after the one from the previous interval
r.rewardsFile.ExecutionStartBlock = previousIntervalEvent.ExecutionBlock.Uint64() + 1
r.minipoolPerformanceFile.ExecutionStartBlock = r.rewardsFile.ExecutionStartBlock
startElHeader, err = r.rp.Client.HeaderByNumber(context.Background(), big.NewInt(int64(r.rewardsFile.ExecutionStartBlock)))
startElHeader, err = r.rp.HeaderByNumber(context.Background(), big.NewInt(int64(r.rewardsFile.ExecutionStartBlock)))
if err != nil {
return nil, fmt.Errorf("error getting EL start block %d: %w", r.rewardsFile.ExecutionStartBlock, err)
}
} else {
// We are post-merge, so get the EL block corresponding to the BC block
r.rewardsFile.ExecutionStartBlock = elBlockNumber
r.minipoolPerformanceFile.ExecutionStartBlock = r.rewardsFile.ExecutionStartBlock
startElHeader, err = r.rp.Client.HeaderByNumber(context.Background(), big.NewInt(int64(elBlockNumber)))
startElHeader, err = r.rp.HeaderByNumber(context.Background(), big.NewInt(int64(elBlockNumber)))
if err != nil {
return nil, fmt.Errorf("error getting EL header for block %d: %w", elBlockNumber, err)
}
Expand Down
8 changes: 4 additions & 4 deletions shared/services/rewards/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type TreeGenerator struct {
rewardsIntervalInfos map[uint64]rewardsIntervalInfo
logger *log.ColorLogger
logPrefix string
rp *rocketpool.RocketPool
rp *defaultRewardsExecutionClient
cfg *config.RocketPoolConfig
bc beacon.Client
index uint64
Expand All @@ -84,8 +84,8 @@ type SnapshotEnd struct {
}

type treeGeneratorImpl interface {
generateTree(rp *rocketpool.RocketPool, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error)
approximateStakerShareOfSmoothingPool(rp *rocketpool.RocketPool, networkName string, bc beacon.Client) (*big.Int, error)
generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error)
approximateStakerShareOfSmoothingPool(rp RewardsExecutionClient, networkName string, bc beacon.Client) (*big.Int, error)
getRulesetVersion() uint64
// Returns the primary artifact cid for consensus, all cids of all files in a map, and any potential errors
saveFiles(smartnode *config.SmartnodeConfig, treeResult *GenerateTreeResult, nodeTrusted bool) (cid.Cid, map[string]cid.Cid, error)
Expand All @@ -95,7 +95,7 @@ func NewTreeGenerator(logger *log.ColorLogger, logPrefix string, rp *rocketpool.
t := &TreeGenerator{
logger: logger,
logPrefix: logPrefix,
rp: rp,
rp: &defaultRewardsExecutionClient{rp},
cfg: cfg,
bc: bc,
index: index,
Expand Down
Loading

0 comments on commit a37e00a

Please sign in to comment.