From 7afb8c3c8671fd47f0d12d2675d3984a7dfd0fc0 Mon Sep 17 00:00:00 2001 From: Bastin <43618253+Inspector-Butters@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:51:29 +0100 Subject: [PATCH] move light client rpc helpers tests to core (#14695) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * move rpc helpers tests to core * remove helpers tests * fix linter * deleted extra files * fix conflicts --------- Co-authored-by: Radosław Kapka Co-authored-by: Inspector-Butters --- beacon-chain/core/light-client/BUILD.bazel | 1 + .../core/light-client/lightclient_test.go | 671 ++++++++++++++++- beacon-chain/rpc/eth/light-client/BUILD.bazel | 1 - .../rpc/eth/light-client/helpers_test.go | 679 ------------------ 4 files changed, 670 insertions(+), 682 deletions(-) diff --git a/beacon-chain/core/light-client/BUILD.bazel b/beacon-chain/core/light-client/BUILD.bazel index 7459468fe27a..2757a1325b23 100644 --- a/beacon-chain/core/light-client/BUILD.bazel +++ b/beacon-chain/core/light-client/BUILD.bazel @@ -33,6 +33,7 @@ go_test( "//config/params:go_default_library", "//consensus-types:go_default_library", "//consensus-types/blocks:go_default_library", + "//consensus-types/light-client:go_default_library", "//consensus-types/primitives:go_default_library", "//encoding/ssz:go_default_library", "//proto/engine/v1:go_default_library", diff --git a/beacon-chain/core/light-client/lightclient_test.go b/beacon-chain/core/light-client/lightclient_test.go index c9660e5d25c1..7585f597dfd1 100644 --- a/beacon-chain/core/light-client/lightclient_test.go +++ b/beacon-chain/core/light-client/lightclient_test.go @@ -2,15 +2,18 @@ package light_client_test import ( "reflect" + "strings" "testing" + "github.com/prysmaticlabs/prysm/v5/config/params" + light_client "github.com/prysmaticlabs/prysm/v5/consensus-types/light-client" + "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" + "github.com/pkg/errors" lightClient "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/light-client" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" - "github.com/prysmaticlabs/prysm/v5/config/params" consensustypes "github.com/prysmaticlabs/prysm/v5/consensus-types" "github.com/prysmaticlabs/prysm/v5/consensus-types/blocks" - "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" "github.com/prysmaticlabs/prysm/v5/encoding/ssz" v11 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1" pb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" @@ -972,3 +975,667 @@ func convertArrayToSlice(arr [4][32]uint8) [][]uint8 { } return slice } + +// When the update has relevant sync committee +func createNonEmptySyncCommitteeBranch() [][]byte { + res := make([][]byte, fieldparams.SyncCommitteeBranchDepth) + res[0] = []byte(strings.Repeat("x", 32)) + for i := 1; i < len(res); i++ { + res[i] = make([]byte, fieldparams.RootLength) + } + return res +} + +// When the update has finality +func createNonEmptyFinalityBranch() [][]byte { + res := make([][]byte, fieldparams.FinalityBranchDepth) + res[0] = []byte(strings.Repeat("x", 32)) + for i := 1; i < fieldparams.FinalityBranchDepth; i++ { + res[i] = make([]byte, 32) + } + return res +} + +func TestIsBetterUpdate(t *testing.T) { + config := params.BeaconConfig() + st, err := util.NewBeaconStateAltair() + require.NoError(t, err) + + t.Run("new has supermajority but old doesn't", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,0,1,1,1,1,1,0] + }) + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b11111100, 0b1}, // [0,0,1,1,1,1,1,1] + }) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, true, result) + }) + + t.Run("old has supermajority but new doesn't", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b11111100, 0b1}, // [0,0,1,1,1,1,1,1] + }) + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,0,1,1,1,1,1,0] + }) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, false, result) + }) + + t.Run("new doesn't have supermajority and newNumActiveParticipants is greater than oldNumActiveParticipants", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,0,1,1,1,1,1,0] + }) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, true, result) + }) + + t.Run("new doesn't have supermajority and newNumActiveParticipants is lesser than oldNumActiveParticipants", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,0,1,1,1,1,1,0] + }) + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, false, result) + }) + + t.Run("new has relevant sync committee but old doesn't", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetAttestedHeader(oldAttestedHeader) + require.NoError(t, err) + oldUpdate.SetSignatureSlot(9999) + + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000001, + }, + }) + require.NoError(t, err) + err = newUpdate.SetAttestedHeader(newAttestedHeader) + require.NoError(t, err) + err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + newUpdate.SetSignatureSlot(1000000) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, true, result) + }) + + t.Run("old has relevant sync committee but new doesn't", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000001, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetAttestedHeader(oldAttestedHeader) + require.NoError(t, err) + err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + oldUpdate.SetSignatureSlot(1000000) + + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = newUpdate.SetAttestedHeader(newAttestedHeader) + require.NoError(t, err) + newUpdate.SetSignatureSlot(9999) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, false, result) + }) + + t.Run("new has finality but old doesn't", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetAttestedHeader(oldAttestedHeader) + require.NoError(t, err) + err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + oldUpdate.SetSignatureSlot(9999) + + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = newUpdate.SetAttestedHeader(newAttestedHeader) + require.NoError(t, err) + err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + newUpdate.SetSignatureSlot(9999) + err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, true, result) + }) + + t.Run("old has finality but new doesn't", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetAttestedHeader(oldAttestedHeader) + require.NoError(t, err) + err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + oldUpdate.SetSignatureSlot(9999) + err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = newUpdate.SetAttestedHeader(newAttestedHeader) + require.NoError(t, err) + err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + newUpdate.SetSignatureSlot(9999) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, false, result) + }) + + t.Run("new has finality and sync committee finality both but old doesn't have sync committee finality", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetAttestedHeader(oldAttestedHeader) + require.NoError(t, err) + err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + oldUpdate.SetSignatureSlot(9999) + oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 9999, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) + require.NoError(t, err) + + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,0,1,1,1,1,1,0] + }) + newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = newUpdate.SetAttestedHeader(newAttestedHeader) + require.NoError(t, err) + err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + newUpdate.SetSignatureSlot(999999) + err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 999999, + }, + }) + require.NoError(t, err) + err = newUpdate.SetFinalizedHeader(newFinalizedHeader) + require.NoError(t, err) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, true, result) + }) + + t.Run("new has finality but doesn't have sync committee finality and old has sync committee finality", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetAttestedHeader(oldAttestedHeader) + require.NoError(t, err) + err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + oldUpdate.SetSignatureSlot(999999) + oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 999999, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) + require.NoError(t, err) + + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = newUpdate.SetAttestedHeader(newAttestedHeader) + require.NoError(t, err) + err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + newUpdate.SetSignatureSlot(9999) + err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 9999, + }, + }) + require.NoError(t, err) + err = newUpdate.SetFinalizedHeader(newFinalizedHeader) + require.NoError(t, err) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, false, result) + }) + + t.Run("new has more active participants than old", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,1,1,1,1,1,0,0] + }) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, true, result) + }) + + t.Run("new has less active participants than old", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,1,1,1,1,1,0,0] + }) + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, false, result) + }) + + t.Run("new's attested header's slot is lesser than old's attested header's slot", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetAttestedHeader(oldAttestedHeader) + require.NoError(t, err) + err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + oldUpdate.SetSignatureSlot(9999) + oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 9999, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) + require.NoError(t, err) + + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 999999, + }, + }) + require.NoError(t, err) + err = newUpdate.SetAttestedHeader(newAttestedHeader) + require.NoError(t, err) + err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + newUpdate.SetSignatureSlot(9999) + err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 9999, + }, + }) + require.NoError(t, err) + err = newUpdate.SetFinalizedHeader(newFinalizedHeader) + require.NoError(t, err) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, true, result) + }) + + t.Run("new's attested header's slot is greater than old's attested header's slot", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 999999, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetAttestedHeader(oldAttestedHeader) + require.NoError(t, err) + err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + oldUpdate.SetSignatureSlot(9999) + oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 9999, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) + require.NoError(t, err) + + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = newUpdate.SetAttestedHeader(newAttestedHeader) + require.NoError(t, err) + err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + newUpdate.SetSignatureSlot(9999) + err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 9999, + }, + }) + require.NoError(t, err) + err = newUpdate.SetFinalizedHeader(newFinalizedHeader) + require.NoError(t, err) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, false, result) + }) + + t.Run("none of the above conditions are met and new signature's slot is less than old signature's slot", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetAttestedHeader(oldAttestedHeader) + require.NoError(t, err) + err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + oldUpdate.SetSignatureSlot(9999) + oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 9999, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) + require.NoError(t, err) + + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = newUpdate.SetAttestedHeader(newAttestedHeader) + require.NoError(t, err) + err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + newUpdate.SetSignatureSlot(9998) + err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 9999, + }, + }) + require.NoError(t, err) + err = newUpdate.SetFinalizedHeader(newFinalizedHeader) + require.NoError(t, err) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, true, result) + }) + + t.Run("none of the above conditions are met and new signature's slot is greater than old signature's slot", func(t *testing.T) { + oldUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) + require.NoError(t, err) + newUpdate, err := lightClient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) + require.NoError(t, err) + + oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetAttestedHeader(oldAttestedHeader) + require.NoError(t, err) + err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + oldUpdate.SetSignatureSlot(9998) + oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 9999, + }, + }) + require.NoError(t, err) + err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) + require.NoError(t, err) + + newUpdate.SetSyncAggregate(&pb.SyncAggregate{ + SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] + }) + newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 1000000, + }, + }) + require.NoError(t, err) + err = newUpdate.SetAttestedHeader(newAttestedHeader) + require.NoError(t, err) + err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) + require.NoError(t, err) + newUpdate.SetSignatureSlot(9999) + err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) + require.NoError(t, err) + newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ + Beacon: &pb.BeaconBlockHeader{ + Slot: 9999, + }, + }) + require.NoError(t, err) + err = newUpdate.SetFinalizedHeader(newFinalizedHeader) + require.NoError(t, err) + + result, err := lightClient.IsBetterUpdate(newUpdate, oldUpdate) + require.NoError(t, err) + require.Equal(t, false, result) + }) +} diff --git a/beacon-chain/rpc/eth/light-client/BUILD.bazel b/beacon-chain/rpc/eth/light-client/BUILD.bazel index 4b668ff2ee2a..7cc250da9953 100644 --- a/beacon-chain/rpc/eth/light-client/BUILD.bazel +++ b/beacon-chain/rpc/eth/light-client/BUILD.bazel @@ -54,7 +54,6 @@ go_test( "//proto/engine/v1:go_default_library", "//proto/prysm/v1alpha1:go_default_library", "//runtime/version:go_default_library", - "//testing/assert:go_default_library", "//testing/require:go_default_library", "//testing/util:go_default_library", "@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", diff --git a/beacon-chain/rpc/eth/light-client/helpers_test.go b/beacon-chain/rpc/eth/light-client/helpers_test.go index 6d60fca8185a..51b23585a906 100644 --- a/beacon-chain/rpc/eth/light-client/helpers_test.go +++ b/beacon-chain/rpc/eth/light-client/helpers_test.go @@ -1,680 +1 @@ package lightclient - -import ( - "strings" - "testing" - - lightclient "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/light-client" - fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" - "github.com/prysmaticlabs/prysm/v5/config/params" - light_client "github.com/prysmaticlabs/prysm/v5/consensus-types/light-client" - "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" - pb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/v5/testing/assert" - "github.com/prysmaticlabs/prysm/v5/testing/require" - "github.com/prysmaticlabs/prysm/v5/testing/util" -) - -// When the update has relevant sync committee -func createNonEmptySyncCommitteeBranch() [][]byte { - res := make([][]byte, fieldparams.SyncCommitteeBranchDepth) - res[0] = []byte(strings.Repeat("x", 32)) - for i := 1; i < len(res); i++ { - res[i] = make([]byte, fieldparams.RootLength) - } - return res -} - -// When the update has finality -func createNonEmptyFinalityBranch() [][]byte { - res := make([][]byte, fieldparams.FinalityBranchDepth) - res[0] = []byte(strings.Repeat("x", 32)) - for i := 1; i < fieldparams.FinalityBranchDepth; i++ { - res[i] = make([]byte, 32) - } - return res -} - -func TestIsBetterUpdate(t *testing.T) { - config := params.BeaconConfig() - st, err := util.NewBeaconStateAltair() - require.NoError(t, err) - - t.Run("new has supermajority but old doesn't", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,0,1,1,1,1,1,0] - }) - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b11111100, 0b1}, // [0,0,1,1,1,1,1,1] - }) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, true, result) - }) - - t.Run("old has supermajority but new doesn't", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b11111100, 0b1}, // [0,0,1,1,1,1,1,1] - }) - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,0,1,1,1,1,1,0] - }) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, false, result) - }) - - t.Run("new doesn't have supermajority and newNumActiveParticipants is greater than oldNumActiveParticipants", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,0,1,1,1,1,1,0] - }) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, true, result) - }) - - t.Run("new doesn't have supermajority and newNumActiveParticipants is lesser than oldNumActiveParticipants", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,0,1,1,1,1,1,0] - }) - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, false, result) - }) - - t.Run("new has relevant sync committee but old doesn't", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetAttestedHeader(oldAttestedHeader) - assert.NoError(t, err) - oldUpdate.SetSignatureSlot(9999) - - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000001, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetAttestedHeader(newAttestedHeader) - assert.NoError(t, err) - err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - newUpdate.SetSignatureSlot(1000000) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, true, result) - }) - - t.Run("old has relevant sync committee but new doesn't", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000001, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetAttestedHeader(oldAttestedHeader) - assert.NoError(t, err) - err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - oldUpdate.SetSignatureSlot(1000000) - - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetAttestedHeader(newAttestedHeader) - assert.NoError(t, err) - newUpdate.SetSignatureSlot(9999) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, false, result) - }) - - t.Run("new has finality but old doesn't", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetAttestedHeader(oldAttestedHeader) - assert.NoError(t, err) - err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - oldUpdate.SetSignatureSlot(9999) - - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetAttestedHeader(newAttestedHeader) - assert.NoError(t, err) - err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - newUpdate.SetSignatureSlot(9999) - err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, true, result) - }) - - t.Run("old has finality but new doesn't", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetAttestedHeader(oldAttestedHeader) - assert.NoError(t, err) - err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - oldUpdate.SetSignatureSlot(9999) - err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetAttestedHeader(newAttestedHeader) - assert.NoError(t, err) - err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - newUpdate.SetSignatureSlot(9999) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, false, result) - }) - - t.Run("new has finality and sync committee finality both but old doesn't have sync committee finality", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetAttestedHeader(oldAttestedHeader) - assert.NoError(t, err) - err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - oldUpdate.SetSignatureSlot(9999) - oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 9999, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) - assert.NoError(t, err) - - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,0,1,1,1,1,1,0] - }) - newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetAttestedHeader(newAttestedHeader) - assert.NoError(t, err) - err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - newUpdate.SetSignatureSlot(999999) - err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 999999, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetFinalizedHeader(newFinalizedHeader) - assert.NoError(t, err) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, true, result) - }) - - t.Run("new has finality but doesn't have sync committee finality and old has sync committee finality", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetAttestedHeader(oldAttestedHeader) - assert.NoError(t, err) - err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - oldUpdate.SetSignatureSlot(999999) - oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 999999, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) - assert.NoError(t, err) - - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetAttestedHeader(newAttestedHeader) - assert.NoError(t, err) - err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - newUpdate.SetSignatureSlot(9999) - err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 9999, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetFinalizedHeader(newFinalizedHeader) - assert.NoError(t, err) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, false, result) - }) - - t.Run("new has more active participants than old", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,1,1,1,1,1,0,0] - }) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, true, result) - }) - - t.Run("new has less active participants than old", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b01111100, 0b1}, // [0,1,1,1,1,1,0,0] - }) - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, false, result) - }) - - t.Run("new's attested header's slot is lesser than old's attested header's slot", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetAttestedHeader(oldAttestedHeader) - assert.NoError(t, err) - err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - oldUpdate.SetSignatureSlot(9999) - oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 9999, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) - assert.NoError(t, err) - - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 999999, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetAttestedHeader(newAttestedHeader) - assert.NoError(t, err) - err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - newUpdate.SetSignatureSlot(9999) - err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 9999, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetFinalizedHeader(newFinalizedHeader) - assert.NoError(t, err) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, true, result) - }) - - t.Run("new's attested header's slot is greater than old's attested header's slot", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 999999, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetAttestedHeader(oldAttestedHeader) - assert.NoError(t, err) - err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - oldUpdate.SetSignatureSlot(9999) - oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 9999, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) - assert.NoError(t, err) - - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetAttestedHeader(newAttestedHeader) - assert.NoError(t, err) - err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - newUpdate.SetSignatureSlot(9999) - err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 9999, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetFinalizedHeader(newFinalizedHeader) - assert.NoError(t, err) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, false, result) - }) - - t.Run("none of the above conditions are met and new signature's slot is less than old signature's slot", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetAttestedHeader(oldAttestedHeader) - assert.NoError(t, err) - err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - oldUpdate.SetSignatureSlot(9999) - oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 9999, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) - assert.NoError(t, err) - - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetAttestedHeader(newAttestedHeader) - assert.NoError(t, err) - err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - newUpdate.SetSignatureSlot(9998) - err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 9999, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetFinalizedHeader(newFinalizedHeader) - assert.NoError(t, err) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, true, result) - }) - - t.Run("none of the above conditions are met and new signature's slot is greater than old signature's slot", func(t *testing.T) { - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1), st) - assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2), st) - assert.NoError(t, err) - - oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - oldAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetAttestedHeader(oldAttestedHeader) - assert.NoError(t, err) - err = oldUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - err = oldUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - oldUpdate.SetSignatureSlot(9998) - oldFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 9999, - }, - }) - assert.NoError(t, err) - err = oldUpdate.SetFinalizedHeader(oldFinalizedHeader) - assert.NoError(t, err) - - newUpdate.SetSyncAggregate(&pb.SyncAggregate{ - SyncCommitteeBits: []byte{0b00111100, 0b1}, // [0,0,1,1,1,1,0,0] - }) - newAttestedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 1000000, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetAttestedHeader(newAttestedHeader) - assert.NoError(t, err) - err = newUpdate.SetNextSyncCommitteeBranch(createNonEmptySyncCommitteeBranch()) - assert.NoError(t, err) - newUpdate.SetSignatureSlot(9999) - err = newUpdate.SetFinalityBranch(createNonEmptyFinalityBranch()) - assert.NoError(t, err) - newFinalizedHeader, err := light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ - Beacon: &pb.BeaconBlockHeader{ - Slot: 9999, - }, - }) - assert.NoError(t, err) - err = newUpdate.SetFinalizedHeader(newFinalizedHeader) - assert.NoError(t, err) - - result, err := lightclient.IsBetterUpdate(newUpdate, oldUpdate) - assert.NoError(t, err) - assert.Equal(t, false, result) - }) -}