Skip to content

Commit e51996d

Browse files
committed
Add beacon client interface to rewards for dependency injection
1 parent a4483c7 commit e51996d

10 files changed

Lines changed: 65 additions & 14 deletions

shared/services/rewards/generator-impl-v8-rolling.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type treeGeneratorImpl_v8_rolling struct {
3030
logPrefix string
3131
rp RewardsExecutionClient
3232
previousRewardsPoolAddresses []common.Address
33-
bc beacon.Client
33+
bc RewardsBeaconClient
3434
opts *bind.CallOpts
3535
smoothingPoolBalance *big.Int
3636
intervalDutiesInfo *IntervalDutiesInfo
@@ -95,7 +95,7 @@ func (r *treeGeneratorImpl_v8_rolling) getRulesetVersion() uint64 {
9595
return r.rewardsFile.RulesetVersion
9696
}
9797

98-
func (r *treeGeneratorImpl_v8_rolling) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {
98+
func (r *treeGeneratorImpl_v8_rolling) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc RewardsBeaconClient) (*GenerateTreeResult, error) {
9999

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

@@ -171,7 +171,7 @@ func (r *treeGeneratorImpl_v8_rolling) generateTree(rp RewardsExecutionClient, n
171171

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

177177
r.rp = rp

shared/services/rewards/generator-impl-v8.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type treeGeneratorImpl_v8 struct {
3333
logPrefix string
3434
rp RewardsExecutionClient
3535
previousRewardsPoolAddresses []common.Address
36-
bc beacon.Client
36+
bc RewardsBeaconClient
3737
opts *bind.CallOpts
3838
nodeDetails []*NodeSmoothingDetails
3939
smoothingPoolBalance *big.Int
@@ -102,7 +102,7 @@ func (r *treeGeneratorImpl_v8) getRulesetVersion() uint64 {
102102
return r.rewardsFile.RulesetVersion
103103
}
104104

105-
func (r *treeGeneratorImpl_v8) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {
105+
func (r *treeGeneratorImpl_v8) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc RewardsBeaconClient) (*GenerateTreeResult, error) {
106106

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

@@ -179,7 +179,7 @@ func (r *treeGeneratorImpl_v8) generateTree(rp RewardsExecutionClient, networkNa
179179

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

185185
r.rp = rp

shared/services/rewards/generator-impl-v9-rolling.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type treeGeneratorImpl_v9_rolling struct {
3030
logPrefix string
3131
rp RewardsExecutionClient
3232
previousRewardsPoolAddresses []common.Address
33-
bc beacon.Client
33+
bc RewardsBeaconClient
3434
opts *bind.CallOpts
3535
smoothingPoolBalance *big.Int
3636
intervalDutiesInfo *IntervalDutiesInfo
@@ -92,7 +92,7 @@ func (r *treeGeneratorImpl_v9_rolling) getRulesetVersion() uint64 {
9292
return r.rewardsFile.RulesetVersion
9393
}
9494

95-
func (r *treeGeneratorImpl_v9_rolling) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {
95+
func (r *treeGeneratorImpl_v9_rolling) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc RewardsBeaconClient) (*GenerateTreeResult, error) {
9696

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

@@ -176,7 +176,7 @@ func (r *treeGeneratorImpl_v9_rolling) generateTree(rp RewardsExecutionClient, n
176176

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

182182
r.rp = rp

shared/services/rewards/generator-impl-v9.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type treeGeneratorImpl_v9 struct {
3737
logPrefix string
3838
rp RewardsExecutionClient
3939
previousRewardsPoolAddresses []common.Address
40-
bc beacon.Client
40+
bc RewardsBeaconClient
4141
opts *bind.CallOpts
4242
nodeDetails []*NodeSmoothingDetails
4343
smoothingPoolBalance *big.Int
@@ -103,7 +103,7 @@ func (r *treeGeneratorImpl_v9) getRulesetVersion() uint64 {
103103
return r.rewardsFile.RulesetVersion
104104
}
105105

106-
func (r *treeGeneratorImpl_v9) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error) {
106+
func (r *treeGeneratorImpl_v9) generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc RewardsBeaconClient) (*GenerateTreeResult, error) {
107107

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

@@ -188,7 +188,7 @@ func (r *treeGeneratorImpl_v9) generateTree(rp RewardsExecutionClient, networkNa
188188

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

194194
r.rp = rp

shared/services/rewards/generator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ type SnapshotEnd struct {
8484
}
8585

8686
type treeGeneratorImpl interface {
87-
generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc beacon.Client) (*GenerateTreeResult, error)
88-
approximateStakerShareOfSmoothingPool(rp RewardsExecutionClient, networkName string, bc beacon.Client) (*big.Int, error)
87+
generateTree(rp RewardsExecutionClient, networkName string, previousRewardsPoolAddresses []common.Address, bc RewardsBeaconClient) (*GenerateTreeResult, error)
88+
approximateStakerShareOfSmoothingPool(rp RewardsExecutionClient, networkName string, bc RewardsBeaconClient) (*big.Int, error)
8989
getRulesetVersion() uint64
9090
// Returns the primary artifact cid for consensus, all cids of all files in a map, and any potential errors
9191
saveFiles(smartnode *config.SmartnodeConfig, treeResult *GenerateTreeResult, nodeTrusted bool) (cid.Cid, map[string]cid.Cid, error)

shared/services/rewards/rewards-file-v1.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ func (f *RewardsFile_v1) GetTotalNodeOperatorSmoothingPoolEth() *big.Int {
191191
return &f.RewardsFileHeader.TotalRewards.NodeOperatorSmoothingPoolEth.Int
192192
}
193193

194+
// Get the the execution start block
195+
func (f *RewardsFile_v1) GetExecutionStartBlock() uint64 {
196+
return f.RewardsFileHeader.ExecutionStartBlock
197+
}
198+
199+
// Get the the consensus start block
200+
func (f *RewardsFile_v1) GetConsensusStartBlock() uint64 {
201+
return f.RewardsFileHeader.ConsensusStartBlock
202+
}
203+
194204
// Get the the execution end block
195205
func (f *RewardsFile_v1) GetExecutionEndBlock() uint64 {
196206
return f.RewardsFileHeader.ExecutionEndBlock

shared/services/rewards/rewards-file-v2.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ func (f *RewardsFile_v2) GetTotalNodeOperatorSmoothingPoolEth() *big.Int {
188188
return &f.RewardsFileHeader.TotalRewards.NodeOperatorSmoothingPoolEth.Int
189189
}
190190

191+
// Get the the execution start block
192+
func (f *RewardsFile_v2) GetExecutionStartBlock() uint64 {
193+
return f.RewardsFileHeader.ExecutionStartBlock
194+
}
195+
196+
// Get the the consensus start block
197+
func (f *RewardsFile_v2) GetConsensusStartBlock() uint64 {
198+
return f.RewardsFileHeader.ConsensusStartBlock
199+
}
200+
191201
// Get the the execution end block
192202
func (f *RewardsFile_v2) GetExecutionEndBlock() uint64 {
193203
return f.RewardsFileHeader.ExecutionEndBlock

shared/services/rewards/rewards-file-v3.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ func (f *RewardsFile_v3) GetConsensusEndBlock() uint64 {
113113
return f.RewardsFileHeader.ConsensusEndBlock
114114
}
115115

116+
// Get the execution start block
117+
func (f *RewardsFile_v3) GetExecutionStartBlock() uint64 {
118+
return f.RewardsFileHeader.ExecutionStartBlock
119+
}
120+
121+
// Get the consensus start block
122+
func (f *RewardsFile_v3) GetConsensusStartBlock() uint64 {
123+
return f.RewardsFileHeader.ConsensusStartBlock
124+
}
125+
116126
// Get the start time
117127
func (f *RewardsFile_v3) GetStartTime() time.Time {
118128
return f.RewardsFileHeader.StartTime

shared/services/rewards/ssz_types/rewards-file-v4.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,14 @@ func (f *SSZFile_v1) GetNodeAddresses() []common.Address {
438438
return out
439439
}
440440

441+
func (f *SSZFile_v1) GetConsensusStartBlock() uint64 {
442+
return f.ConsensusStartBlock
443+
}
444+
445+
func (f *SSZFile_v1) GetExecutionStartBlock() uint64 {
446+
return f.ExecutionStartBlock
447+
}
448+
441449
func (f *SSZFile_v1) GetConsensusEndBlock() uint64 {
442450
return f.ConsensusEndBlock
443451
}

shared/services/rewards/types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
ethtypes "github.com/ethereum/go-ethereum/core/types"
1313
"github.com/rocket-pool/rocketpool-go/rewards"
1414
"github.com/rocket-pool/rocketpool-go/types"
15+
"github.com/rocket-pool/smartnode/shared/services/beacon"
1516
"github.com/wealdtech/go-merkletree"
1617
)
1718

@@ -40,6 +41,16 @@ type RewardsExecutionClient interface {
4041
GetRewardSnapshotEvent(previousRewardsPoolAddresses []common.Address, interval uint64, opts *bind.CallOpts) (rewards.RewardsEvent, error)
4142
}
4243

44+
// RewardsBeaconClient defines and interface
45+
// that contains only the functions from beacon.Client
46+
// required for rewards generation.
47+
// This facade makes it easier to perform dependency injection in tests.
48+
type RewardsBeaconClient interface {
49+
GetBeaconBlock(slot string) (beacon.BeaconBlock, bool, error)
50+
GetCommitteesForEpoch(epoch *uint64) (beacon.Committees, error)
51+
GetAttestations(slot string) ([]beacon.AttestationInfo, bool, error)
52+
}
53+
4354
// Interface for version-agnostic minipool performance
4455
type IMinipoolPerformanceFile interface {
4556
// Serialize a minipool performance file into bytes
@@ -80,6 +91,8 @@ type IRewardsFile interface {
8091
GetTotalCollateralRpl() *big.Int
8192
GetTotalNodeOperatorSmoothingPoolEth() *big.Int
8293
GetTotalPoolStakerSmoothingPoolEth() *big.Int
94+
GetExecutionStartBlock() uint64
95+
GetConsensusStartBlock() uint64
8396
GetExecutionEndBlock() uint64
8497
GetConsensusEndBlock() uint64
8598
GetStartTime() time.Time

0 commit comments

Comments
 (0)