Skip to content

Commit

Permalink
Consolidation spectests from electra
Browse files Browse the repository at this point in the history
WIP: epoch_processing pending_consolidations, currently failing

WIP: Tests
  • Loading branch information
prestonvanloon committed May 10, 2024
1 parent 4bbd799 commit de6f522
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 2 deletions.
1 change: 1 addition & 0 deletions beacon-chain/core/electra/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ go_test(
name = "go_default_test",
srcs = [
"churn_test.go",
"consolidations_test.go",
"upgrade_test.go",
"validator_test.go",
],
Expand Down
10 changes: 8 additions & 2 deletions beacon-chain/core/electra/consolidations.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@ var ErrNilConsolidations = errors.New("nil consolidations")
// next_pending_consolidation += 1
//
// state.pending_consolidations = state.pending_consolidations[next_pending_consolidation:]
func ProcessPendingConsolidations(ctx context.Context, st state.BeaconState, activeBalance uint64) (state.BeaconState, error) {
func ProcessPendingConsolidations(ctx context.Context, st state.BeaconState) (state.BeaconState, error) {
ctx, span := trace.StartSpan(ctx, "electra.ProcessPendingConsolidations")
defer span.End()

if st == nil || st.IsNil() {
return nil, errors.New("nil state")
}

currentEpoch := slots.ToEpoch(st.Slot())

var nextPendingConsolidation uint64
pendingConsolidations, err := st.PendingConsolidations()
if err != nil {
Expand All @@ -62,14 +64,18 @@ func ProcessPendingConsolidations(ctx context.Context, st state.BeaconState, act
nextPendingConsolidation++
continue
}
if sourceValidator.WithdrawableEpoch > slots.ToEpoch(st.Slot()) {
if sourceValidator.WithdrawableEpoch > currentEpoch {
break
}

if err := SwitchToCompoundingValidator(ctx, st, pc.TargetIndex); err != nil {
return nil, err
}

activeBalance, err := st.ActiveBalanceAtIndex(pc.SourceIndex)
if err != nil {
return nil, err
}
if err := helpers.DecreaseBalance(st, pc.SourceIndex, activeBalance); err != nil {
return nil, err
}
Expand Down
11 changes: 11 additions & 0 deletions beacon-chain/core/electra/consolidations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package electra_test

import "testing"

func TestProcessPendingConsolidations(t *testing.T) {
t.Fail()
}

func TestProcessConsolidations(t *testing.T) {
t.Fail()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_test(
"inactivity_updates_test.go",
"justification_and_finalization_test.go",
"participation_flag_updates_test.go",
"pending_consolidations_test.go",
"randao_mixes_reset_test.go",
"rewards_and_penalties_test.go",
"slashings_reset_test.go",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package epoch_processing

import (
"testing"

"github.com/prysmaticlabs/prysm/v5/testing/spectest/shared/electra/epoch_processing"
)

func TestMainnet_Electra_EpochProcessing_PendingConsolidations(t *testing.T) {
epoch_processing.RunPendingConsolidationsTests(t, "mainnet")
}
1 change: 1 addition & 0 deletions testing/spectest/mainnet/electra/operations/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_test(
"attester_slashing_test.go",
"block_header_test.go",
"bls_to_execution_change_test.go",
"consolidation_test.go",
"execution_payload_test.go",
"proposer_slashing_test.go",
"sync_committee_test.go",
Expand Down
11 changes: 11 additions & 0 deletions testing/spectest/mainnet/electra/operations/consolidation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package operations

import (
"testing"

"github.com/prysmaticlabs/prysm/v5/testing/spectest/shared/electra/operations"
)

func TestMainnet_Electra_Operations_Consolidation(t *testing.T) {
operations.RunConsolidationTest(t, "mainnet")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_test(
"inactivity_updates_test.go",
"justification_and_finalization_test.go",
"participation_flag_updates_test.go",
"pending_consolidations_test.go",
"randao_mixes_reset_test.go",
"rewards_and_penalties_test.go",
"slashings_reset_test.go",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package epoch_processing

import (
"testing"

"github.com/prysmaticlabs/prysm/v5/testing/spectest/shared/electra/epoch_processing"
)

func TestMinimal_Electra_EpochProcessing_PendingConsolidations(t *testing.T) {
epoch_processing.RunPendingConsolidationsTests(t, "minimal")
}
1 change: 1 addition & 0 deletions testing/spectest/minimal/electra/operations/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_test(
"attester_slashing_test.go",
"block_header_test.go",
"bls_to_execution_change_test.go",
"consolidation_test.go",
"execution_payload_test.go",
"proposer_slashing_test.go",
"sync_committee_test.go",
Expand Down
11 changes: 11 additions & 0 deletions testing/spectest/minimal/electra/operations/consolidation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package operations

import (
"testing"

"github.com/prysmaticlabs/prysm/v5/testing/spectest/shared/electra/operations"
)

func TestMinimal_Electra_Operations_Consolidation(t *testing.T) {
operations.RunConsolidationTest(t, "minimal")
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
"inactivity_updates.go",
"justification_and_finalization.go",
"participation_flag_updates.go",
"pending_consolidations.go",
"randao_mixes_reset.go",
"rewards_and_penalties.go",
"slashings.go",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package epoch_processing

import (
"context"
"path"
"testing"

"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/electra"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v5/testing/require"
"github.com/prysmaticlabs/prysm/v5/testing/spectest/utils"
)

func RunPendingConsolidationsTests(t *testing.T, config string) {
require.NoError(t, utils.SetConfig(t, config))

testFolders, testsFolderPath := utils.TestFolders(t, config, "electra", "epoch_processing/pending_consolidations/pyspec_tests")
for _, folder := range testFolders {
t.Run(folder.Name(), func(t *testing.T) {
folderPath := path.Join(testsFolderPath, folder.Name())
RunEpochOperationTest(t, folderPath, processPendingConsolidations)
})
}
}

func processPendingConsolidations(t *testing.T, st state.BeaconState) (state.BeaconState, error) {
return electra.ProcessPendingConsolidations(context.TODO(), st)
}
1 change: 1 addition & 0 deletions testing/spectest/shared/electra/operations/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"attester_slashing.go",
"block_header.go",
"bls_to_execution_changes.go",
"consolidations.go",
"deposit_receipt.go",
"execution_layer_withdrawal_request.go",
"execution_payload.go",
Expand Down
48 changes: 48 additions & 0 deletions testing/spectest/shared/electra/operations/consolidations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package operations

import (
"context"
"path"
"testing"

"github.com/golang/snappy"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/electra"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/testing/require"
"github.com/prysmaticlabs/prysm/v5/testing/spectest/utils"
"github.com/prysmaticlabs/prysm/v5/testing/util"
)

func RunConsolidationTest(t *testing.T, config string) {
t.Skip("These tests were temporarily deleted in v1.5.0-alpha.2. See https://github.com/ethereum/consensus-specs/pull/3736")
require.NoError(t, utils.SetConfig(t, config))
testFolders, testsFolderPath := utils.TestFolders(t, config, "electra", "operations/consolidation/pyspec_tests")
require.NotEqual(t, 0, len(testFolders), "missing tests for consolidation operation in folder")
for _, folder := range testFolders {
t.Run(folder.Name(), func(t *testing.T) {
folderPath := path.Join(testsFolderPath, folder.Name())
consolidationFile, err := util.BazelFileBytes(folderPath, "consolidation.ssz_snappy")
require.NoError(t, err)
consolidationSSZ, err := snappy.Decode(nil /* dst */, consolidationFile)
require.NoError(t, err, "Failed to decompress")
consolidation := &ethpb.SignedConsolidation{}
require.NoError(t, consolidation.UnmarshalSSZ(consolidationSSZ), "Failed to unmarshal")

body := &ethpb.BeaconBlockBodyElectra{Consolidations: []*ethpb.SignedConsolidation{consolidation}}
processConsolidationFunc := func(ctx context.Context, s state.BeaconState, b interfaces.SignedBeaconBlock) (state.BeaconState, error) {
body, ok := b.Block().Body().(interfaces.ROBlockBodyElectra)
if !ok {
t.Error("block body is not electra")
}
cs := body.Consolidations()
if len(cs) == 0 {
t.Error("no consolidations to test")
}
return electra.ProcessConsolidations(ctx, s, cs)
}
RunBlockOperationTest(t, folderPath, body, processConsolidationFunc)
})
}
}

0 comments on commit de6f522

Please sign in to comment.