|
| 1 | +package rewards |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/big" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/ethereum/go-ethereum/common" |
| 11 | + "github.com/ethereum/go-ethereum/core/types" |
| 12 | + "github.com/fatih/color" |
| 13 | + "github.com/rocket-pool/smartnode/shared/services/beacon" |
| 14 | + "github.com/rocket-pool/smartnode/shared/services/rewards/test" |
| 15 | + "github.com/rocket-pool/smartnode/shared/services/rewards/test/assets" |
| 16 | + "github.com/rocket-pool/smartnode/shared/services/state" |
| 17 | + "github.com/rocket-pool/smartnode/shared/utils/log" |
| 18 | +) |
| 19 | + |
| 20 | +type v8Test struct { |
| 21 | + *testing.T |
| 22 | + rp *test.MockRocketPool |
| 23 | + bc *test.MockBeaconClient |
| 24 | +} |
| 25 | + |
| 26 | +func (t *v8Test) saveArtifacts(result *GenerateTreeResult) { |
| 27 | + tmpDir, err := os.MkdirTemp("", fmt.Sprintf("artifacts-%s", t.Name())) |
| 28 | + t.failIf(err) |
| 29 | + rewardsLocalFile := LocalFile[IRewardsFile]{ |
| 30 | + fullPath: filepath.Join(tmpDir, "rewards.json"), |
| 31 | + f: result.RewardsFile, |
| 32 | + } |
| 33 | + performanceLocalFile := LocalFile[IMinipoolPerformanceFile]{ |
| 34 | + fullPath: filepath.Join(tmpDir, "minipool-performance.json"), |
| 35 | + f: result.MinipoolPerformanceFile, |
| 36 | + } |
| 37 | + _, err = rewardsLocalFile.Write() |
| 38 | + t.failIf(err) |
| 39 | + _, err = performanceLocalFile.Write() |
| 40 | + t.failIf(err) |
| 41 | + |
| 42 | + t.Logf("wrote artifacts to %s\n", tmpDir) |
| 43 | +} |
| 44 | + |
| 45 | +func newV8Test(t *testing.T) *v8Test { |
| 46 | + rp := test.NewMockRocketPool(t) |
| 47 | + out := &v8Test{ |
| 48 | + T: t, |
| 49 | + rp: rp, |
| 50 | + bc: test.NewMockBeaconClient(t), |
| 51 | + } |
| 52 | + return out |
| 53 | +} |
| 54 | + |
| 55 | +func (t *v8Test) failIf(err error) { |
| 56 | + if err != nil { |
| 57 | + t.Fatalf(err.Error()) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func (t *v8Test) SetMinipoolPerformance(canonicalMinipoolPerformance IMinipoolPerformanceFile, networkState *state.NetworkState) { |
| 62 | + addresses := canonicalMinipoolPerformance.GetMinipoolAddresses() |
| 63 | + for _, address := range addresses { |
| 64 | + |
| 65 | + // Get the minipool's performance |
| 66 | + perf, ok := canonicalMinipoolPerformance.GetSmoothingPoolPerformance(address) |
| 67 | + if !ok { |
| 68 | + t.Fatalf("Minipool %s not found in canonical minipool performance, despite being listed as present", address.Hex()) |
| 69 | + } |
| 70 | + missedSlots := perf.GetMissingAttestationSlots() |
| 71 | + pubkey, err := perf.GetPubkey() |
| 72 | + |
| 73 | + // Get the minipool's validator index |
| 74 | + validatorStatus := networkState.ValidatorDetails[pubkey] |
| 75 | + |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("Minipool %s pubkey could not be parsed: %s", address.Hex(), err.Error()) |
| 78 | + } |
| 79 | + t.bc.SetMinipoolPerformance(validatorStatus.Index, missedSlots) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// TestV8Mainnet builds a tree using serialized state for a mainnet interval that used v8 |
| 84 | +// and checks that the resulting artifacts match their canonical values. |
| 85 | +func TestV8Mainnet(tt *testing.T) { |
| 86 | + t := newV8Test(tt) |
| 87 | + |
| 88 | + canonical, err := DeserializeRewardsFile(assets.GetMainnet20RewardsJSON()) |
| 89 | + t.failIf(err) |
| 90 | + |
| 91 | + canonicalPerformance, err := DeserializeMinipoolPerformanceFile(assets.GetMainnet20MinipoolPerformanceJSON()) |
| 92 | + t.failIf(err) |
| 93 | + |
| 94 | + state := assets.GetMainnet20RewardsState() |
| 95 | + t.Logf("pending rpl rewards: %s", state.NetworkDetails.PendingRPLRewards.String()) |
| 96 | + |
| 97 | + t.bc.SetState(state) |
| 98 | + |
| 99 | + // Some interval info needed for mocks |
| 100 | + consensusStartBlock := canonical.GetConsensusStartBlock() |
| 101 | + executionStartBlock := canonical.GetExecutionStartBlock() |
| 102 | + consensusEndBlock := canonical.GetConsensusEndBlock() |
| 103 | + |
| 104 | + // Create a new treeGeneratorImpl_v8 |
| 105 | + logger := log.NewColorLogger(color.Faint) |
| 106 | + generator := newTreeGeneratorImpl_v8( |
| 107 | + &logger, |
| 108 | + t.Name(), |
| 109 | + canonical.GetIndex(), |
| 110 | + canonical.GetStartTime(), |
| 111 | + canonical.GetEndTime(), |
| 112 | + consensusEndBlock, |
| 113 | + &types.Header{ |
| 114 | + Number: big.NewInt(int64(canonical.GetExecutionEndBlock())), |
| 115 | + Time: assets.Mainnet20ELHeaderTime, |
| 116 | + }, |
| 117 | + canonical.GetIntervalsPassed(), |
| 118 | + state, |
| 119 | + ) |
| 120 | + |
| 121 | + // Load the mock up |
| 122 | + t.rp.SetRewardSnapshotEvent(assets.GetRewardSnapshotEventInterval19()) |
| 123 | + t.bc.SetBeaconBlock(fmt.Sprint(consensusStartBlock-1), beacon.BeaconBlock{ExecutionBlockNumber: executionStartBlock - 1}) |
| 124 | + t.bc.SetBeaconBlock(fmt.Sprint(consensusStartBlock), beacon.BeaconBlock{ExecutionBlockNumber: executionStartBlock}) |
| 125 | + t.rp.SetHeaderByNumber(big.NewInt(int64(executionStartBlock)), &types.Header{Time: uint64(canonical.GetStartTime().Unix())}) |
| 126 | + |
| 127 | + // Set the critical duties slots |
| 128 | + t.bc.SetCriticalDutiesSlots(assets.GetMainnet20CriticalDutiesSlots()) |
| 129 | + |
| 130 | + // Set the minipool performance |
| 131 | + t.SetMinipoolPerformance(canonicalPerformance, state) |
| 132 | + |
| 133 | + artifacts, err := generator.generateTree( |
| 134 | + t.rp, |
| 135 | + "mainnet", |
| 136 | + make([]common.Address, 0), |
| 137 | + t.bc, |
| 138 | + ) |
| 139 | + t.failIf(err) |
| 140 | + |
| 141 | + // Save the artifacts if verbose mode is enabled |
| 142 | + if testing.Verbose() { |
| 143 | + t.saveArtifacts(artifacts) |
| 144 | + } |
| 145 | + |
| 146 | + t.Logf("merkle root: %s\n", artifacts.RewardsFile.GetMerkleRoot()) |
| 147 | + if artifacts.RewardsFile.GetMerkleRoot() != canonical.GetMerkleRoot() { |
| 148 | + t.Fatalf("Merkle root does not match %s", canonical.GetMerkleRoot()) |
| 149 | + } else { |
| 150 | + t.Logf("merkle root matches %s", canonical.GetMerkleRoot()) |
| 151 | + } |
| 152 | +} |
0 commit comments