Skip to content

Commit 4e5cd0d

Browse files
committed
Add mock interval tests
1 parent 4042c8c commit 4e5cd0d

3 files changed

Lines changed: 601 additions & 5 deletions

File tree

shared/services/rewards/generator-v8_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ type v8Test struct {
2323
bc *test.MockBeaconClient
2424
}
2525

26-
func (t *v8Test) saveArtifacts(result *GenerateTreeResult) {
26+
func (t *v8Test) saveArtifacts(prefix string, result *GenerateTreeResult) {
2727
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("artifacts-%s", t.Name()))
2828
t.failIf(err)
2929
rewardsLocalFile := LocalFile[IRewardsFile]{
30-
fullPath: filepath.Join(tmpDir, "rewards.json"),
30+
fullPath: filepath.Join(tmpDir, fmt.Sprintf("%s-ewards.json", prefix)),
3131
f: result.RewardsFile,
3232
}
3333
performanceLocalFile := LocalFile[IMinipoolPerformanceFile]{
34-
fullPath: filepath.Join(tmpDir, "minipool-performance.json"),
34+
fullPath: filepath.Join(tmpDir, fmt.Sprintf("%s-minipool-performance.json", prefix)),
3535
f: result.MinipoolPerformanceFile,
3636
}
3737
_, err = rewardsLocalFile.Write()
@@ -106,7 +106,7 @@ func TestV8Mainnet(tt *testing.T) {
106106
generator := newTreeGeneratorImpl_v8(
107107
&logger,
108108
t.Name(),
109-
canonical.GetIndex(),
109+
state.NetworkDetails.RewardIndex,
110110
canonical.GetStartTime(),
111111
canonical.GetEndTime(),
112112
consensusEndBlock,
@@ -140,7 +140,7 @@ func TestV8Mainnet(tt *testing.T) {
140140

141141
// Save the artifacts if verbose mode is enabled
142142
if testing.Verbose() {
143-
t.saveArtifacts(artifacts)
143+
t.saveArtifacts("", artifacts)
144144
}
145145

146146
t.Logf("merkle root: %s\n", artifacts.RewardsFile.GetMerkleRoot())
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package rewards
2+
3+
// This file contains treegen tests which use mock history.
4+
// These mocks are faster to process than real history, and are useful for
5+
// testing new features and refactoring.
6+
7+
import (
8+
"fmt"
9+
"math/big"
10+
"strings"
11+
"testing"
12+
13+
"github.com/ethereum/go-ethereum/common"
14+
"github.com/ethereum/go-ethereum/core/types"
15+
"github.com/fatih/color"
16+
"github.com/rocket-pool/smartnode/shared/services/beacon"
17+
"github.com/rocket-pool/smartnode/shared/services/rewards/test"
18+
"github.com/rocket-pool/smartnode/shared/services/rewards/test/assets"
19+
"github.com/rocket-pool/smartnode/shared/utils/log"
20+
)
21+
22+
func TestMockIntervalDefaultsTreegenv8v9(tt *testing.T) {
23+
t := newV8Test(tt)
24+
25+
history := test.NewDefaultMockHistory()
26+
state := history.GetEndNetworkState()
27+
28+
t.bc.SetState(state)
29+
30+
consensusStartBlock := history.GetConsensusStartBlock()
31+
executionStartBlock := history.GetExecutionStartBlock()
32+
consensusEndBlock := history.GetConsensusEndBlock()
33+
executionEndBlock := history.GetExecutionEndBlock()
34+
35+
logger := log.NewColorLogger(color.Faint)
36+
generator := newTreeGeneratorImpl_v8(
37+
&logger,
38+
t.Name(),
39+
state.NetworkDetails.RewardIndex,
40+
history.GetStartTime(),
41+
history.GetEndTime(),
42+
consensusEndBlock,
43+
&types.Header{
44+
Number: big.NewInt(int64(history.GetExecutionEndBlock())),
45+
Time: assets.Mainnet20ELHeaderTime,
46+
},
47+
/* intervalsPassed= */ 1,
48+
state,
49+
)
50+
51+
t.rp.SetRewardSnapshotEvent(history.GetPreviousRewardSnapshotEvent())
52+
t.bc.SetBeaconBlock(fmt.Sprint(consensusStartBlock-1), beacon.BeaconBlock{ExecutionBlockNumber: executionStartBlock - 1})
53+
t.bc.SetBeaconBlock(fmt.Sprint(consensusStartBlock), beacon.BeaconBlock{ExecutionBlockNumber: executionStartBlock})
54+
t.rp.SetHeaderByNumber(big.NewInt(int64(executionStartBlock)), &types.Header{Time: uint64(history.GetStartTime().Unix())})
55+
56+
for _, validator := range state.ValidatorDetails {
57+
t.bc.SetMinipoolPerformance(validator.Index, make([]uint64, 0))
58+
}
59+
60+
v8Artifacts, err := generator.generateTree(
61+
t.rp,
62+
"mainnet",
63+
make([]common.Address, 0),
64+
t.bc,
65+
)
66+
t.failIf(err)
67+
68+
if testing.Verbose() {
69+
t.saveArtifacts("v8", v8Artifacts)
70+
}
71+
generatorv9 := newTreeGeneratorImpl_v9(
72+
&logger,
73+
t.Name(),
74+
state.NetworkDetails.RewardIndex,
75+
&SnapshotEnd{
76+
Slot: consensusEndBlock,
77+
ConsensusBlock: consensusEndBlock,
78+
ExecutionBlock: executionEndBlock,
79+
},
80+
&types.Header{
81+
Number: big.NewInt(int64(history.GetExecutionEndBlock())),
82+
Time: assets.Mainnet20ELHeaderTime,
83+
},
84+
/* intervalsPassed= */ 1,
85+
state,
86+
)
87+
88+
v9Artifacts, err := generatorv9.generateTree(
89+
t.rp,
90+
"mainnet",
91+
make([]common.Address, 0),
92+
t.bc,
93+
)
94+
t.failIf(err)
95+
96+
if testing.Verbose() {
97+
t.saveArtifacts("v9", v9Artifacts)
98+
}
99+
100+
v8MerkleRoot := v8Artifacts.RewardsFile.GetMerkleRoot()
101+
v9MerkleRoot := v9Artifacts.RewardsFile.GetMerkleRoot()
102+
103+
if !strings.EqualFold(v8MerkleRoot, v9MerkleRoot) {
104+
t.Fatalf("Merkle root does not match %s != %s", v8MerkleRoot, v9MerkleRoot)
105+
} else {
106+
t.Logf("v8/v9 Merkle root matches %s", v8MerkleRoot)
107+
}
108+
}

0 commit comments

Comments
 (0)