Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make unit test great again #38

Merged
merged 9 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
124 changes: 62 additions & 62 deletions api/client/beacon/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,27 @@ func TestFname(t *testing.T) {
require.Equal(t, expected, actual)
}

func TestDownloadWeakSubjectivityCheckpoint(t *testing.T) {
// runs computeBackwardsCompatible directly
// and via ComputeWeakSubjectivityCheckpoint with a round tripper that triggers the backwards compatible code path
func TestDownloadBackwardsCompatibleCombined(t *testing.T) {
ctx := context.Background()
params.SetupForkEpochConfigForTest()
cfg := params.BeaconConfig()
epoch := cfg.AltairForkEpoch - 1
cfg.AltairForkEpoch = 299
cfg.BellatrixForkEpoch = 300
cfg.CapellaForkEpoch = 301
cfg.InitializeForkSchedule()
params.OverrideBeaconConfig(cfg)

st, expectedEpoch := defaultTestHeadState(t, cfg)
serialized, err := st.MarshalSSZ()
require.NoError(t, err)

// set up checkpoint state, using the epoch that will be computed as the ws checkpoint state based on the head state
wSlot, err := slots.EpochStart(epoch)
wSlot, err := slots.EpochStart(expectedEpoch)
require.NoError(t, err)
wst, err := util.NewBeaconState()
require.NoError(t, err)
fork, err := forkForEpoch(cfg, epoch)
fork, err := forkForEpoch(cfg, cfg.GenesisEpoch)
require.NoError(t, err)
require.NoError(t, wst.SetFork(fork))

Expand Down Expand Up @@ -160,34 +170,27 @@ func TestDownloadWeakSubjectivityCheckpoint(t *testing.T) {

wsSerialized, err := wst.MarshalSSZ()
require.NoError(t, err)
expectedWSD := WeakSubjectivityData{
BlockRoot: bRoot,
StateRoot: wRoot,
Epoch: epoch,
}

trans := &testRT{rt: func(req *http.Request) (*http.Response, error) {
res := &http.Response{Request: req}
switch req.URL.Path {
case getWeakSubjectivityPath:
case getNodeVersionPath:
res.StatusCode = http.StatusOK
cp := struct {
Epoch string `json:"epoch"`
Root string `json:"root"`
}{
Epoch: fmt.Sprintf("%d", slots.ToEpoch(b.Block().Slot())),
Root: fmt.Sprintf("%#x", bRoot),
}
wsr := struct {
Checkpoint interface{} `json:"ws_checkpoint"`
StateRoot string `json:"state_root"`
b := bytes.NewBuffer(nil)
d := struct {
Version string `json:"version"`
}{
Checkpoint: cp,
StateRoot: fmt.Sprintf("%#x", wRoot),
Version: "Lighthouse/v0.1.5 (Linux x86_64)",
}
rb, err := marshalToEnvelope(wsr)
encoded, err := marshalToEnvelope(d)
require.NoError(t, err)
res.Body = io.NopCloser(bytes.NewBuffer(rb))
b.Write(encoded)
res.Body = io.NopCloser(b)
case getWeakSubjectivityPath:
res.StatusCode = http.StatusNotFound
case renderGetStatePath(IdHead):
res.StatusCode = http.StatusOK
res.Body = io.NopCloser(bytes.NewBuffer(serialized))
case renderGetStatePath(IdFromSlot(wSlot)):
res.StatusCode = http.StatusOK
res.Body = io.NopCloser(bytes.NewBuffer(wsSerialized))
Expand All @@ -202,34 +205,25 @@ func TestDownloadWeakSubjectivityCheckpoint(t *testing.T) {
c, err := NewClient("http://localhost:3500", client.WithRoundTripper(trans))
require.NoError(t, err)

wsd, err := ComputeWeakSubjectivityCheckpoint(ctx, c)
wsPub, err := ComputeWeakSubjectivityCheckpoint(ctx, c)
require.NoError(t, err)
require.Equal(t, expectedWSD.Epoch, wsd.Epoch)
require.Equal(t, expectedWSD.StateRoot, wsd.StateRoot)
require.Equal(t, expectedWSD.BlockRoot, wsd.BlockRoot)

wsPriv, err := computeBackwardsCompatible(ctx, c)
require.NoError(t, err)
require.DeepEqual(t, wsPriv, wsPub)
}

// runs computeBackwardsCompatible directly
// and via ComputeWeakSubjectivityCheckpoint with a round tripper that triggers the backwards compatible code path
func TestDownloadBackwardsCompatibleCombined(t *testing.T) {
func TestDownloadWeakSubjectivityCheckpoint(t *testing.T) {
ctx := context.Background()
params.SetupForkEpochConfigForTest()
cfg := params.BeaconConfig()
cfg.AltairForkEpoch = 299
cfg.BellatrixForkEpoch = 300
cfg.CapellaForkEpoch = 301
cfg.InitializeForkSchedule()
params.OverrideBeaconConfig(cfg)

st, expectedEpoch := defaultTestHeadState(t, cfg)
serialized, err := st.MarshalSSZ()
require.NoError(t, err)

epoch := cfg.AltairForkEpoch - 1
// set up checkpoint state, using the epoch that will be computed as the ws checkpoint state based on the head state
wSlot, err := slots.EpochStart(expectedEpoch)
wSlot, err := slots.EpochStart(epoch)
require.NoError(t, err)
wst, err := util.NewBeaconState()
require.NoError(t, err)
fork, err := forkForEpoch(cfg, cfg.GenesisEpoch)
fork, err := forkForEpoch(cfg, epoch)
require.NoError(t, err)
require.NoError(t, wst.SetFork(fork))

Expand Down Expand Up @@ -264,27 +258,34 @@ func TestDownloadBackwardsCompatibleCombined(t *testing.T) {

wsSerialized, err := wst.MarshalSSZ()
require.NoError(t, err)
expectedWSD := WeakSubjectivityData{
BlockRoot: bRoot,
StateRoot: wRoot,
Epoch: epoch,
}

trans := &testRT{rt: func(req *http.Request) (*http.Response, error) {
res := &http.Response{Request: req}
switch req.URL.Path {
case getNodeVersionPath:
case getWeakSubjectivityPath:
res.StatusCode = http.StatusOK
b := bytes.NewBuffer(nil)
d := struct {
Version string `json:"version"`
cp := struct {
Epoch string `json:"epoch"`
Root string `json:"root"`
}{
Version: "Lighthouse/v0.1.5 (Linux x86_64)",
Epoch: fmt.Sprintf("%d", slots.ToEpoch(b.Block().Slot())),
Root: fmt.Sprintf("%#x", bRoot),
}
encoded, err := marshalToEnvelope(d)
wsr := struct {
Checkpoint interface{} `json:"ws_checkpoint"`
StateRoot string `json:"state_root"`
}{
Checkpoint: cp,
StateRoot: fmt.Sprintf("%#x", wRoot),
}
rb, err := marshalToEnvelope(wsr)
require.NoError(t, err)
b.Write(encoded)
res.Body = io.NopCloser(b)
case getWeakSubjectivityPath:
res.StatusCode = http.StatusNotFound
case renderGetStatePath(IdHead):
res.StatusCode = http.StatusOK
res.Body = io.NopCloser(bytes.NewBuffer(serialized))
res.Body = io.NopCloser(bytes.NewBuffer(rb))
case renderGetStatePath(IdFromSlot(wSlot)):
res.StatusCode = http.StatusOK
res.Body = io.NopCloser(bytes.NewBuffer(wsSerialized))
Expand All @@ -299,12 +300,11 @@ func TestDownloadBackwardsCompatibleCombined(t *testing.T) {
c, err := NewClient("http://localhost:3500", client.WithRoundTripper(trans))
require.NoError(t, err)

wsPub, err := ComputeWeakSubjectivityCheckpoint(ctx, c)
require.NoError(t, err)

wsPriv, err := computeBackwardsCompatible(ctx, c)
wsd, err := ComputeWeakSubjectivityCheckpoint(ctx, c)
require.NoError(t, err)
require.DeepEqual(t, wsPriv, wsPub)
require.Equal(t, expectedWSD.Epoch, wsd.Epoch)
require.Equal(t, expectedWSD.StateRoot, wsd.StateRoot)
require.Equal(t, expectedWSD.BlockRoot, wsd.BlockRoot)
}

func TestGetWeakSubjectivityEpochFromHead(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/core/altair/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func TestSetParticipationAndRewardProposer(t *testing.T) {
headFlagIndex: false,
},
wantedParticipation: []byte{3, 3, 3, 3, 0, 0, 0, 0},
wantedBalance: 256044591892,
wantedBalance: 256044591808,
},
{name: "all participated with some flags",
indices: []uint64{0, 1, 2, 3, 4, 5, 6, 7}, epochParticipation: []byte{0, 0, 0, 0, 0, 0, 0, 0}, participatedFlags: map[uint8]bool{
Expand All @@ -529,7 +529,7 @@ func TestSetParticipationAndRewardProposer(t *testing.T) {
headFlagIndex: false,
},
wantedParticipation: []byte{1, 1, 1, 1, 1, 1, 1, 1},
wantedBalance: 256029727928,
wantedBalance: 256029727872,
},
{name: "all participated with all flags",
indices: []uint64{0, 1, 2, 3, 4, 5, 6, 7}, epochParticipation: []byte{0, 0, 0, 0, 0, 0, 0, 0}, participatedFlags: map[uint8]bool{
Expand All @@ -538,7 +538,7 @@ func TestSetParticipationAndRewardProposer(t *testing.T) {
headFlagIndex: true,
},
wantedParticipation: []byte{7, 7, 7, 7, 7, 7, 7, 7},
wantedBalance: 256118911712,
wantedBalance: 256118911488,
},
}
for _, test := range tests {
Expand Down Expand Up @@ -618,7 +618,7 @@ func TestEpochParticipation(t *testing.T) {
targetFlagIndex: true,
headFlagIndex: false,
},
wantedNumerator: 17123286528,
wantedNumerator: 17123254272,
wantedReserve: 0,
wantedEpochParticipation: []byte{3, 3, 3, 3, 0, 0, 0, 0},
},
Expand All @@ -628,7 +628,7 @@ func TestEpochParticipation(t *testing.T) {
targetFlagIndex: false,
headFlagIndex: false,
},
wantedNumerator: 11415524352,
wantedNumerator: 11415502848,
wantedReserve: 0,
wantedEpochParticipation: []byte{1, 1, 1, 1, 1, 1, 1, 1},
},
Expand All @@ -638,7 +638,7 @@ func TestEpochParticipation(t *testing.T) {
targetFlagIndex: true,
headFlagIndex: true,
},
wantedNumerator: 45662097408,
wantedNumerator: 45662011392,
wantedReserve: 0,
wantedEpochParticipation: []byte{7, 7, 7, 7, 7, 7, 7, 7},
},
Expand Down
22 changes: 11 additions & 11 deletions beacon-chain/core/altair/epoch_precompute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestProcessEpochParticipation_InactiveValidator(t *testing.T) {
require.DeepEqual(t, &precompute.Validator{
IsActiveCurrentEpoch: false,
IsActivePrevEpoch: false,
IsWithdrawableCurrentEpoch: false,
IsWithdrawableCurrentEpoch: true,
CurrentEpochEffectiveBalance: eb,
ActualBalance: eb,
}, validators[0])
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestAttestationsDelta(t *testing.T) {
require.NoError(t, err)
return s
},
wantRewards: []uint64{0, 8561643835, 19977168949, 24733637746},
wantRewards: []uint64{0, 8561643804, 19977168876, 24733637656},
wantPenalties: []uint64{0, 0, 0, 0},
},
{
Expand All @@ -249,7 +249,7 @@ func TestAttestationsDelta(t *testing.T) {
require.NoError(t, err)
return s
},
wantPenalties: []uint64{1625395, 1083597, 0, 0},
wantPenalties: []uint64{3250792, 2167195, 0, 0},
},
{
name: "altair - zero penalty in inactivity leak",
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestAttestationsDelta(t *testing.T) {
require.NoError(t, s.SetBalances([]uint64{balance, balance, balance, balance}))
return s
},
wantPenalties: []uint64{1625395, 1083597, 0, 0},
wantPenalties: []uint64{3250792, 2167195, 0, 0},
},
{
name: "bellatrix - zero penalty",
Expand All @@ -288,7 +288,7 @@ func TestAttestationsDelta(t *testing.T) {
require.NoError(t, err)
return s
},
wantRewards: []uint64{0, 8561643835, 19977168949, 24733637746},
wantRewards: []uint64{0, 8561643804, 19977168876, 24733637656},
wantPenalties: []uint64{0, 0, 0, 0},
},
{
Expand All @@ -300,7 +300,7 @@ func TestAttestationsDelta(t *testing.T) {
require.NoError(t, err)
return s
},
wantPenalties: []uint64{1625395, 1083597, 0, 0},
wantPenalties: []uint64{3250792, 2167195, 0, 0},
},
{
name: "bellatrix - zero penalty in inactivity leak",
Expand Down Expand Up @@ -330,7 +330,7 @@ func TestAttestationsDelta(t *testing.T) {
require.NoError(t, s.SetBalances([]uint64{balance, balance, balance, balance}))
return s
},
wantPenalties: []uint64{1625395, 1083597, 0, 0},
wantPenalties: []uint64{3250792, 2167195, 0, 0},
},
}

Expand Down Expand Up @@ -428,8 +428,8 @@ func TestProcessRewardsAndPenaltiesPrecompute_InactivityLeak(t *testing.T) {
balances := s.Balances()
inactivityBalances := sCopy.Balances()
// Balances decreased to 0 due to inactivity
require.Equal(t, uint64(19977168949), balances[2])
require.Equal(t, uint64(24733637746), balances[3])
require.Equal(t, uint64(19977168876), balances[2])
require.Equal(t, uint64(24733637656), balances[3])
require.Equal(t, uint64(0), inactivityBalances[2])
require.Equal(t, uint64(0), inactivityBalances[3])
}
Expand Down Expand Up @@ -489,7 +489,7 @@ func TestProcessInactivityScores_CanProcessNonInactivityLeak(t *testing.T) {
inactivityScores, err := s.InactivityScores()
require.NoError(t, err)

require.DeepEqual(t, []uint64{7, 7, 4, 4}, inactivityScores, "Incorrect inactivity scores")
require.DeepEqual(t, []uint64{9, 9, 4, 4}, inactivityScores, "Incorrect inactivity scores")
}

func TestProcessRewardsAndPenaltiesPrecompute_GenesisEpoch(t *testing.T) {
Expand Down Expand Up @@ -555,7 +555,7 @@ func TestProcessInactivityScores_NonEligibleValidator(t *testing.T) {
inactivityScores, err := s.InactivityScores()
require.NoError(t, err)

require.Equal(t, uint64(7), inactivityScores[0])
require.Equal(t, uint64(9), inactivityScores[0])
require.Equal(t, defaultScore, inactivityScores[1]) // Should remain unchanged
require.Equal(t, defaultScore, inactivityScores[2]) // Should remain unchanged
require.Equal(t, uint64(4), inactivityScores[3])
Expand Down
Loading
Loading