From 837db3cbaeb502e6824af32e53dcf27feed1b896 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Mon, 28 Oct 2024 23:59:12 +0530 Subject: [PATCH 01/15] use state in `CreateDefaultLightClientUpdate` --- beacon-chain/core/light-client/lightclient.go | 13 ++- beacon-chain/db/kv/lightclient_test.go | 26 +++-- .../rpc/eth/light-client/handlers_test.go | 11 +- .../rpc/eth/light-client/helpers_test.go | 101 +++++++++++------- 4 files changed, 98 insertions(+), 53 deletions(-) diff --git a/beacon-chain/core/light-client/lightclient.go b/beacon-chain/core/light-client/lightclient.go index f8df29ba6b6c..69a9a30bc366 100644 --- a/beacon-chain/core/light-client/lightclient.go +++ b/beacon-chain/core/light-client/lightclient.go @@ -159,7 +159,7 @@ func NewLightClientUpdateFromBeaconState( updateAttestedPeriod := slots.SyncCommitteePeriod(slots.ToEpoch(attestedBlock.Block().Slot())) // update = LightClientUpdate() - result, err := CreateDefaultLightClientUpdate(currentSlot) + result, err := CreateDefaultLightClientUpdate(state) if err != nil { return nil, errors.Wrap(err, "could not create default light client update") } @@ -243,8 +243,7 @@ func NewLightClientUpdateFromBeaconState( return result, nil } -func CreateDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.LightClientUpdate, error) { - currentEpoch := slots.ToEpoch(currentSlot) +func CreateDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightClientUpdate, error) { syncCommitteeSize := params.BeaconConfig().SyncCommitteeSize pubKeys := make([][]byte, syncCommitteeSize) @@ -257,7 +256,7 @@ func CreateDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig } var nextSyncCommitteeBranch [][]byte - if currentEpoch >= params.BeaconConfig().ElectraForkEpoch { + if state.Version() >= version.Electra { nextSyncCommitteeBranch = make([][]byte, fieldparams.SyncCommitteeBranchDepthElectra) } else { nextSyncCommitteeBranch = make([][]byte, fieldparams.SyncCommitteeBranchDepth) @@ -276,7 +275,7 @@ func CreateDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig } var m proto.Message - if currentEpoch < params.BeaconConfig().CapellaForkEpoch { + if state.Version() < version.Capella { m = &pb.LightClientUpdateAltair{ AttestedHeader: &pb.LightClientHeaderAltair{ Beacon: &pb.BeaconBlockHeader{}, @@ -285,7 +284,7 @@ func CreateDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig NextSyncCommitteeBranch: nextSyncCommitteeBranch, FinalityBranch: finalityBranch, } - } else if currentEpoch < params.BeaconConfig().DenebForkEpoch { + } else if state.Version() < version.Deneb { m = &pb.LightClientUpdateCapella{ AttestedHeader: &pb.LightClientHeaderCapella{ Beacon: &pb.BeaconBlockHeader{}, @@ -296,7 +295,7 @@ func CreateDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig NextSyncCommitteeBranch: nextSyncCommitteeBranch, FinalityBranch: finalityBranch, } - } else if currentEpoch < params.BeaconConfig().ElectraForkEpoch { + } else if state.Version() < version.Electra { m = &pb.LightClientUpdateDeneb{ AttestedHeader: &pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{}, diff --git a/beacon-chain/db/kv/lightclient_test.go b/beacon-chain/db/kv/lightclient_test.go index fc20d8c27a01..046efa0217ec 100644 --- a/beacon-chain/db/kv/lightclient_test.go +++ b/beacon-chain/db/kv/lightclient_test.go @@ -6,6 +6,7 @@ import ( "math/rand" "testing" + "github.com/prysmaticlabs/prysm/v5/beacon-chain/state" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" "github.com/prysmaticlabs/prysm/v5/config/params" "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" @@ -15,7 +16,7 @@ import ( pb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/runtime/version" "github.com/prysmaticlabs/prysm/v5/testing/require" - "github.com/prysmaticlabs/prysm/v5/time/slots" + "github.com/prysmaticlabs/prysm/v5/testing/util" "google.golang.org/protobuf/proto" ) @@ -23,6 +24,7 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { config := params.BeaconConfig() var slot primitives.Slot var header interfaces.LightClientHeader + var state state.BeaconState var err error sampleRoot := make([]byte, 32) @@ -41,6 +43,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { switch v { case version.Altair: slot = primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) + state, err = util.NewBeaconStateAltair() + require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -53,6 +57,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Capella: slot = primitives.Slot(config.CapellaForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) + state, err = util.NewBeaconStateCapella() + require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderCapella{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -79,6 +85,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Deneb: slot = primitives.Slot(config.DenebForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) + state, err = util.NewBeaconStateDeneb() + require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -105,6 +113,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Electra: slot = primitives.Slot(config.ElectraForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) + state, err = util.NewBeaconStateElectra() + require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -133,7 +143,7 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { return nil, fmt.Errorf("unsupported version %s", version.String(v)) } - update, err := createDefaultLightClientUpdate(slot) + update, err := createDefaultLightClientUpdate(state) require.NoError(t, err) update.SetSignatureSlot(slot - 1) syncCommitteeBits := make([]byte, 64) @@ -430,9 +440,7 @@ func TestStore_LightClientUpdate_RetrieveMissingPeriodDistributed(t *testing.T) require.DeepEqual(t, updates[4], retrievedUpdates[uint64(5)], "retrieved update does not match saved update") } -func createDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.LightClientUpdate, error) { - currentEpoch := slots.ToEpoch(currentSlot) - +func createDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightClientUpdate, error) { syncCommitteeSize := params.BeaconConfig().SyncCommitteeSize pubKeys := make([][]byte, syncCommitteeSize) for i := uint64(0); i < syncCommitteeSize; i++ { @@ -444,7 +452,7 @@ func createDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig } var nextSyncCommitteeBranch [][]byte - if currentEpoch >= params.BeaconConfig().ElectraForkEpoch { + if state.Version() >= version.Electra { nextSyncCommitteeBranch = make([][]byte, fieldparams.SyncCommitteeBranchDepthElectra) } else { nextSyncCommitteeBranch = make([][]byte, fieldparams.SyncCommitteeBranchDepth) @@ -463,14 +471,14 @@ func createDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig } var m proto.Message - if currentEpoch < params.BeaconConfig().CapellaForkEpoch { + if state.Version() < version.Capella { m = &pb.LightClientUpdateAltair{ AttestedHeader: &pb.LightClientHeaderAltair{}, NextSyncCommittee: nextSyncCommittee, NextSyncCommitteeBranch: nextSyncCommitteeBranch, FinalityBranch: finalityBranch, } - } else if currentEpoch < params.BeaconConfig().DenebForkEpoch { + } else if state.Version() < version.Deneb { m = &pb.LightClientUpdateCapella{ AttestedHeader: &pb.LightClientHeaderCapella{ Beacon: &pb.BeaconBlockHeader{}, @@ -481,7 +489,7 @@ func createDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig NextSyncCommitteeBranch: nextSyncCommitteeBranch, FinalityBranch: finalityBranch, } - } else if currentEpoch < params.BeaconConfig().ElectraForkEpoch { + } else if state.Version() < version.Electra { m = &pb.LightClientUpdateDeneb{ AttestedHeader: &pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{}, diff --git a/beacon-chain/rpc/eth/light-client/handlers_test.go b/beacon-chain/rpc/eth/light-client/handlers_test.go index 449daa20ec96..27f01f2c82f9 100644 --- a/beacon-chain/rpc/eth/light-client/handlers_test.go +++ b/beacon-chain/rpc/eth/light-client/handlers_test.go @@ -1881,6 +1881,7 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { config := params.BeaconConfig() var slot primitives.Slot var header interfaces.LightClientHeader + var state state.BeaconState var err error sampleRoot := make([]byte, 32) @@ -1899,6 +1900,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { switch v { case version.Altair: slot = primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) + state, err = util.NewBeaconStateAltair() + require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -1911,6 +1914,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Capella: slot = primitives.Slot(config.CapellaForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) + state, err = util.NewBeaconStateCapella() + require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderCapella{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -1937,6 +1942,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Deneb: slot = primitives.Slot(config.DenebForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) + state, err = util.NewBeaconStateDeneb() + require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -1963,6 +1970,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Electra: slot = primitives.Slot(config.ElectraForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) + state, err = util.NewBeaconStateElectra() + require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -1991,7 +2000,7 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { return nil, fmt.Errorf("unsupported version %s", version.String(v)) } - update, err := lightclient.CreateDefaultLightClientUpdate(slot) + update, err := lightclient.CreateDefaultLightClientUpdate(state) require.NoError(t, err) update.SetSignatureSlot(slot - 1) syncCommitteeBits := make([]byte, 64) diff --git a/beacon-chain/rpc/eth/light-client/helpers_test.go b/beacon-chain/rpc/eth/light-client/helpers_test.go index 62594af80c3f..deda80c16ab3 100644 --- a/beacon-chain/rpc/eth/light-client/helpers_test.go +++ b/beacon-chain/rpc/eth/light-client/helpers_test.go @@ -6,11 +6,10 @@ import ( 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/util" ) // When the update has relevant sync committee @@ -35,12 +34,12 @@ func createNonEmptyFinalityBranch() [][]byte { func TestIsBetterUpdate(t *testing.T) { - config := params.BeaconConfig() - 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)) + state, err := util.NewBeaconState() + assert.NoError(t, err) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -56,9 +55,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() + assert.NoError(t, err) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -74,9 +75,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + assert.NoError(t, err) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -92,9 +95,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() + assert.NoError(t, err) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -110,9 +115,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + assert.NoError(t, err) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -149,9 +156,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() + assert.NoError(t, err) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -188,9 +197,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + assert.NoError(t, err) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -231,9 +242,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() + assert.NoError(t, err) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -274,9 +287,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + assert.NoError(t, err) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -335,9 +350,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() + assert.NoError(t, err) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -396,9 +413,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + assert.NoError(t, err) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -414,9 +433,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() + assert.NoError(t, err) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -432,9 +453,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + assert.NoError(t, err) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -493,9 +516,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() + assert.NoError(t, err) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -554,9 +579,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + assert.NoError(t, err) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -615,9 +642,11 @@ func TestIsBetterUpdate(t *testing.T) { }) 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)) + state, err := util.NewBeaconState() + assert.NoError(t, err) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(2)) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ From 3751ff1e9da193b340f1e8a9896edfbe3ad3c9b9 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Tue, 29 Oct 2024 02:43:15 +0530 Subject: [PATCH 02/15] lint --- beacon-chain/core/light-client/lightclient.go | 1 - 1 file changed, 1 deletion(-) diff --git a/beacon-chain/core/light-client/lightclient.go b/beacon-chain/core/light-client/lightclient.go index 69a9a30bc366..aae6d0dd7933 100644 --- a/beacon-chain/core/light-client/lightclient.go +++ b/beacon-chain/core/light-client/lightclient.go @@ -244,7 +244,6 @@ func NewLightClientUpdateFromBeaconState( } func CreateDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightClientUpdate, error) { - syncCommitteeSize := params.BeaconConfig().SyncCommitteeSize pubKeys := make([][]byte, syncCommitteeSize) for i := uint64(0); i < syncCommitteeSize; i++ { From 84468ae5e19543f93f48537b328c9fe506715266 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Tue, 29 Oct 2024 03:27:07 +0530 Subject: [PATCH 03/15] add `stateSlot` to `update.go` structs --- consensus-types/light-client/update.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/consensus-types/light-client/update.go b/consensus-types/light-client/update.go index f6fb739d2996..c7e1efff139f 100644 --- a/consensus-types/light-client/update.go +++ b/consensus-types/light-client/update.go @@ -3,12 +3,14 @@ package light_client import ( "fmt" + "github.com/pkg/errors" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" consensustypes "github.com/prysmaticlabs/prysm/v5/consensus-types" "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" pb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/runtime/version" + "github.com/prysmaticlabs/prysm/v5/testing/util" "google.golang.org/protobuf/proto" ) @@ -39,6 +41,7 @@ type updateAltair struct { nextSyncCommitteeBranch interfaces.LightClientSyncCommitteeBranch finalizedHeader interfaces.LightClientHeader finalityBranch interfaces.LightClientFinalityBranch + stateSlot primitives.Slot } var _ interfaces.LightClientUpdate = &updateAltair{} @@ -77,12 +80,19 @@ func NewWrappedUpdateAltair(p *pb.LightClientUpdateAltair) (interfaces.LightClie return nil, err } + state, err := util.NewBeaconStateAltair() + if err != nil { + return nil, errors.Wrap(err, "failed to create beacon state") + } + stateSlot := state.Slot() + return &updateAltair{ p: p, attestedHeader: attestedHeader, nextSyncCommitteeBranch: scBranch, finalizedHeader: finalizedHeader, finalityBranch: finalityBranch, + stateSlot: stateSlot, }, nil } @@ -217,6 +227,7 @@ type updateCapella struct { nextSyncCommitteeBranch interfaces.LightClientSyncCommitteeBranch finalizedHeader interfaces.LightClientHeader finalityBranch interfaces.LightClientFinalityBranch + stateSlot primitives.Slot } var _ interfaces.LightClientUpdate = &updateCapella{} @@ -255,12 +266,19 @@ func NewWrappedUpdateCapella(p *pb.LightClientUpdateCapella) (interfaces.LightCl return nil, err } + state, err := util.NewBeaconStateCapella() + if err != nil { + return nil, errors.Wrap(err, "failed to create beacon state") + } + stateSlot := state.Slot() + return &updateCapella{ p: p, attestedHeader: attestedHeader, nextSyncCommitteeBranch: scBranch, finalizedHeader: finalizedHeader, finalityBranch: finalityBranch, + stateSlot: stateSlot, }, nil } @@ -395,6 +413,7 @@ type updateDeneb struct { nextSyncCommitteeBranch interfaces.LightClientSyncCommitteeBranch finalizedHeader interfaces.LightClientHeader finalityBranch interfaces.LightClientFinalityBranch + stateSlot primitives.Slot } var _ interfaces.LightClientUpdate = &updateDeneb{} @@ -433,12 +452,19 @@ func NewWrappedUpdateDeneb(p *pb.LightClientUpdateDeneb) (interfaces.LightClient return nil, err } + state, err := util.NewBeaconStateDeneb() + if err != nil { + return nil, errors.Wrap(err, "failed to create beacon state") + } + stateSlot := state.Slot() + return &updateDeneb{ p: p, attestedHeader: attestedHeader, nextSyncCommitteeBranch: scBranch, finalizedHeader: finalizedHeader, finalityBranch: finalityBranch, + stateSlot: stateSlot, }, nil } From 86b4b3a872499c9095d0565f2388aafe63cc6e6b Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Wed, 30 Oct 2024 19:36:16 +0530 Subject: [PATCH 04/15] Revert "add `stateSlot` to `update.go` structs" This reverts commit 84468ae5e19543f93f48537b328c9fe506715266. --- consensus-types/light-client/update.go | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/consensus-types/light-client/update.go b/consensus-types/light-client/update.go index c7e1efff139f..f6fb739d2996 100644 --- a/consensus-types/light-client/update.go +++ b/consensus-types/light-client/update.go @@ -3,14 +3,12 @@ package light_client import ( "fmt" - "github.com/pkg/errors" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" consensustypes "github.com/prysmaticlabs/prysm/v5/consensus-types" "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" pb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/runtime/version" - "github.com/prysmaticlabs/prysm/v5/testing/util" "google.golang.org/protobuf/proto" ) @@ -41,7 +39,6 @@ type updateAltair struct { nextSyncCommitteeBranch interfaces.LightClientSyncCommitteeBranch finalizedHeader interfaces.LightClientHeader finalityBranch interfaces.LightClientFinalityBranch - stateSlot primitives.Slot } var _ interfaces.LightClientUpdate = &updateAltair{} @@ -80,19 +77,12 @@ func NewWrappedUpdateAltair(p *pb.LightClientUpdateAltair) (interfaces.LightClie return nil, err } - state, err := util.NewBeaconStateAltair() - if err != nil { - return nil, errors.Wrap(err, "failed to create beacon state") - } - stateSlot := state.Slot() - return &updateAltair{ p: p, attestedHeader: attestedHeader, nextSyncCommitteeBranch: scBranch, finalizedHeader: finalizedHeader, finalityBranch: finalityBranch, - stateSlot: stateSlot, }, nil } @@ -227,7 +217,6 @@ type updateCapella struct { nextSyncCommitteeBranch interfaces.LightClientSyncCommitteeBranch finalizedHeader interfaces.LightClientHeader finalityBranch interfaces.LightClientFinalityBranch - stateSlot primitives.Slot } var _ interfaces.LightClientUpdate = &updateCapella{} @@ -266,19 +255,12 @@ func NewWrappedUpdateCapella(p *pb.LightClientUpdateCapella) (interfaces.LightCl return nil, err } - state, err := util.NewBeaconStateCapella() - if err != nil { - return nil, errors.Wrap(err, "failed to create beacon state") - } - stateSlot := state.Slot() - return &updateCapella{ p: p, attestedHeader: attestedHeader, nextSyncCommitteeBranch: scBranch, finalizedHeader: finalizedHeader, finalityBranch: finalityBranch, - stateSlot: stateSlot, }, nil } @@ -413,7 +395,6 @@ type updateDeneb struct { nextSyncCommitteeBranch interfaces.LightClientSyncCommitteeBranch finalizedHeader interfaces.LightClientHeader finalityBranch interfaces.LightClientFinalityBranch - stateSlot primitives.Slot } var _ interfaces.LightClientUpdate = &updateDeneb{} @@ -452,19 +433,12 @@ func NewWrappedUpdateDeneb(p *pb.LightClientUpdateDeneb) (interfaces.LightClient return nil, err } - state, err := util.NewBeaconStateDeneb() - if err != nil { - return nil, errors.Wrap(err, "failed to create beacon state") - } - stateSlot := state.Slot() - return &updateDeneb{ p: p, attestedHeader: attestedHeader, nextSyncCommitteeBranch: scBranch, finalizedHeader: finalizedHeader, finalityBranch: finalityBranch, - stateSlot: stateSlot, }, nil } From 353a5b74dfe849a6189f3dca92d528167f22ebf1 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Wed, 30 Oct 2024 23:35:26 +0530 Subject: [PATCH 05/15] set sync committee based on attestedHeader in updateElectra --- consensus-types/light-client/update.go | 73 ++++++++++++++++++-------- 1 file changed, 52 insertions(+), 21 deletions(-) diff --git a/consensus-types/light-client/update.go b/consensus-types/light-client/update.go index f6fb739d2996..1b85519d2cf2 100644 --- a/consensus-types/light-client/update.go +++ b/consensus-types/light-client/update.go @@ -4,11 +4,13 @@ import ( "fmt" 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/interfaces" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" pb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/runtime/version" + "github.com/prysmaticlabs/prysm/v5/time/slots" "google.golang.org/protobuf/proto" ) @@ -568,11 +570,12 @@ func (u *updateDeneb) SetSignatureSlot(slot primitives.Slot) { // constructed from the proto, so that we don't have to reconstruct them every time // in getters. type updateElectra struct { - p *pb.LightClientUpdateElectra - attestedHeader interfaces.LightClientHeader - nextSyncCommitteeBranch interfaces.LightClientSyncCommitteeBranchElectra - finalizedHeader interfaces.LightClientHeader - finalityBranch interfaces.LightClientFinalityBranch + p *pb.LightClientUpdateElectra + attestedHeader interfaces.LightClientHeader + nextSyncCommitteeBranch interfaces.LightClientSyncCommitteeBranch + nextSyncCommitteeBranchElectra interfaces.LightClientSyncCommitteeBranchElectra + finalizedHeader interfaces.LightClientHeader + finalityBranch interfaces.LightClientFinalityBranch } var _ interfaces.LightClientUpdate = &updateElectra{} @@ -594,13 +597,26 @@ func NewWrappedUpdateElectra(p *pb.LightClientUpdateElectra) (interfaces.LightCl } } - scBranch, err := createBranch[interfaces.LightClientSyncCommitteeBranchElectra]( - "sync committee", - p.NextSyncCommitteeBranch, - fieldparams.SyncCommitteeBranchDepthElectra, - ) - if err != nil { - return nil, err + var scBranchElectra interfaces.LightClientSyncCommitteeBranchElectra + var scBranch interfaces.LightClientSyncCommitteeBranch + if slots.ToEpoch(attestedHeader.Beacon().Slot) >= params.BeaconConfig().ElectraForkEpoch { + scBranchElectra, err = createBranch[interfaces.LightClientSyncCommitteeBranchElectra]( + "sync committee", + p.NextSyncCommitteeBranch, + fieldparams.SyncCommitteeBranchDepthElectra, + ) + if err != nil { + return nil, err + } + } else { + scBranch, err = createBranch[interfaces.LightClientSyncCommitteeBranch]( + "sync committee", + p.NextSyncCommitteeBranch, + fieldparams.SyncCommitteeBranchDepth, + ) + if err != nil { + return nil, err + } } finalityBranch, err := createBranch[interfaces.LightClientFinalityBranch]( "finality", @@ -612,11 +628,12 @@ func NewWrappedUpdateElectra(p *pb.LightClientUpdateElectra) (interfaces.LightCl } return &updateElectra{ - p: p, - attestedHeader: attestedHeader, - nextSyncCommitteeBranch: scBranch, - finalizedHeader: finalizedHeader, - finalityBranch: finalityBranch, + p: p, + attestedHeader: attestedHeader, + nextSyncCommitteeBranch: scBranch, + nextSyncCommitteeBranchElectra: scBranchElectra, + finalizedHeader: finalizedHeader, + finalityBranch: finalityBranch, }, nil } @@ -668,15 +685,29 @@ func (u *updateElectra) SetNextSyncCommittee(sc *pb.SyncCommittee) { } func (u *updateElectra) NextSyncCommitteeBranch() (interfaces.LightClientSyncCommitteeBranch, error) { - return [5][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranch", version.Electra) + if slots.ToEpoch(u.attestedHeader.Beacon().Slot) < params.BeaconConfig().ElectraForkEpoch { + return u.nextSyncCommitteeBranch, nil + } + return u.nextSyncCommitteeBranch, consensustypes.ErrNotSupported("NextSyncCommitteeBranch", version.Electra) } -func (u *updateElectra) SetNextSyncCommitteeBranch([][]byte) error { +func (u *updateElectra) SetNextSyncCommitteeBranch(branch [][]byte) error { + if slots.ToEpoch(u.attestedHeader.Beacon().Slot) < params.BeaconConfig().ElectraForkEpoch { + b, err := createBranch[interfaces.LightClientSyncCommitteeBranch]("sync committee", branch, fieldparams.SyncCommitteeBranchDepth) + if err != nil { + return err + } + u.nextSyncCommitteeBranch = b + + u.p.NextSyncCommitteeBranch = branch + + return nil + } return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranch", version.Electra) } func (u *updateElectra) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) { - return u.nextSyncCommitteeBranch, nil + return u.nextSyncCommitteeBranchElectra, nil } func (u *updateElectra) SetNextSyncCommitteeBranchElectra(branch [][]byte) error { @@ -684,7 +715,7 @@ func (u *updateElectra) SetNextSyncCommitteeBranchElectra(branch [][]byte) error if err != nil { return err } - u.nextSyncCommitteeBranch = b + u.nextSyncCommitteeBranchElectra = b u.p.NextSyncCommitteeBranch = branch From 39412c30f96d20f85b5cb83fbf2fbb46e865f757 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Wed, 30 Oct 2024 23:35:37 +0530 Subject: [PATCH 06/15] dependencies --- beacon-chain/db/kv/BUILD.bazel | 1 - consensus-types/light-client/BUILD.bazel | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/beacon-chain/db/kv/BUILD.bazel b/beacon-chain/db/kv/BUILD.bazel index 714bfb1f26d4..0a40aa767a15 100644 --- a/beacon-chain/db/kv/BUILD.bazel +++ b/beacon-chain/db/kv/BUILD.bazel @@ -123,7 +123,6 @@ go_test( "//testing/assert:go_default_library", "//testing/require:go_default_library", "//testing/util:go_default_library", - "//time/slots:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", "@com_github_golang_snappy//:go_default_library", "@com_github_pkg_errors//:go_default_library", diff --git a/consensus-types/light-client/BUILD.bazel b/consensus-types/light-client/BUILD.bazel index 151f22b522f9..78432c8eecba 100644 --- a/consensus-types/light-client/BUILD.bazel +++ b/consensus-types/light-client/BUILD.bazel @@ -14,6 +14,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//config/fieldparams:go_default_library", + "//config/params:go_default_library", "//consensus-types:go_default_library", "//consensus-types/blocks:go_default_library", "//consensus-types/interfaces:go_default_library", @@ -21,6 +22,7 @@ go_library( "//encoding/bytesutil:go_default_library", "//proto/prysm/v1alpha1:go_default_library", "//runtime/version:go_default_library", + "//time/slots:go_default_library", "@org_golang_google_protobuf//proto:go_default_library", ], ) From b80c8c876ec1dd06ba4ec5b3848db03f37ca81aa Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Thu, 31 Oct 2024 00:31:28 +0530 Subject: [PATCH 07/15] add check to `SetNextSyncCommitteeBranchElectra` --- consensus-types/light-client/update.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/consensus-types/light-client/update.go b/consensus-types/light-client/update.go index 1b85519d2cf2..f29618a7b500 100644 --- a/consensus-types/light-client/update.go +++ b/consensus-types/light-client/update.go @@ -711,6 +711,10 @@ func (u *updateElectra) NextSyncCommitteeBranchElectra() (interfaces.LightClient } func (u *updateElectra) SetNextSyncCommitteeBranchElectra(branch [][]byte) error { + if slots.ToEpoch(u.attestedHeader.Beacon().Slot) < params.BeaconConfig().ElectraForkEpoch { + return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Electra) + } + b, err := createBranch[interfaces.LightClientSyncCommitteeBranchElectra]("sync committee", branch, fieldparams.SyncCommitteeBranchDepthElectra) if err != nil { return err From 6e4fc9714208c5134d467cb73b1208ef9dca8b92 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Thu, 31 Oct 2024 00:45:49 +0530 Subject: [PATCH 08/15] add detailed error messages to `update.go` --- consensus-types/light-client/update.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/consensus-types/light-client/update.go b/consensus-types/light-client/update.go index f29618a7b500..12fb0bb460d6 100644 --- a/consensus-types/light-client/update.go +++ b/consensus-types/light-client/update.go @@ -3,6 +3,7 @@ package light_client import ( "fmt" + "github.com/pkg/errors" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" "github.com/prysmaticlabs/prysm/v5/config/params" consensustypes "github.com/prysmaticlabs/prysm/v5/consensus-types" @@ -152,11 +153,11 @@ func (u *updateAltair) SetNextSyncCommitteeBranch(branch [][]byte) error { } func (u *updateAltair) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) { - return [6][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Altair) + return [6][32]byte{}, errors.Wrap(consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Altair), "Attested header's version is Altair") } func (u *updateAltair) SetNextSyncCommitteeBranchElectra([][]byte) error { - return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Altair) + return errors.Wrap(consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Altair), "Attested header's version is Altair") } func (u *updateAltair) FinalizedHeader() interfaces.LightClientHeader { @@ -330,11 +331,11 @@ func (u *updateCapella) SetNextSyncCommitteeBranch(branch [][]byte) error { } func (u *updateCapella) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) { - return [6][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Capella) + return [6][32]byte{}, errors.Wrap(consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Capella), "Attested header's version is Capella") } func (u *updateCapella) SetNextSyncCommitteeBranchElectra([][]byte) error { - return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Capella) + return errors.Wrap(consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Capella), "Attested header's version is Capella") } func (u *updateCapella) FinalizedHeader() interfaces.LightClientHeader { @@ -508,11 +509,11 @@ func (u *updateDeneb) SetNextSyncCommitteeBranch(branch [][]byte) error { } func (u *updateDeneb) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) { - return [6][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Deneb) + return [6][32]byte{}, errors.Wrap(consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Deneb), "Attested header's version is Deneb") } func (u *updateDeneb) SetNextSyncCommitteeBranchElectra([][]byte) error { - return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Deneb) + return errors.Wrap(consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Deneb), "Attested header's version is Deneb") } func (u *updateDeneb) FinalizedHeader() interfaces.LightClientHeader { @@ -688,7 +689,7 @@ func (u *updateElectra) NextSyncCommitteeBranch() (interfaces.LightClientSyncCom if slots.ToEpoch(u.attestedHeader.Beacon().Slot) < params.BeaconConfig().ElectraForkEpoch { return u.nextSyncCommitteeBranch, nil } - return u.nextSyncCommitteeBranch, consensustypes.ErrNotSupported("NextSyncCommitteeBranch", version.Electra) + return u.nextSyncCommitteeBranch, errors.Wrap(consensustypes.ErrNotSupported("NextSyncCommitteeBranch", version.Electra), "Attested header's version is Electra") } func (u *updateElectra) SetNextSyncCommitteeBranch(branch [][]byte) error { @@ -703,7 +704,7 @@ func (u *updateElectra) SetNextSyncCommitteeBranch(branch [][]byte) error { return nil } - return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranch", version.Electra) + return errors.Wrap(consensustypes.ErrNotSupported("SetNextSyncCommitteeBranch", version.Electra), "Attested header's version is Electra") } func (u *updateElectra) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) { @@ -712,7 +713,7 @@ func (u *updateElectra) NextSyncCommitteeBranchElectra() (interfaces.LightClient func (u *updateElectra) SetNextSyncCommitteeBranchElectra(branch [][]byte) error { if slots.ToEpoch(u.attestedHeader.Beacon().Slot) < params.BeaconConfig().ElectraForkEpoch { - return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Electra) + return errors.Wrap(consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Electra), "Attested header's version is Deneb") } b, err := createBranch[interfaces.LightClientSyncCommitteeBranchElectra]("sync committee", branch, fieldparams.SyncCommitteeBranchDepthElectra) From 13e235ca2276ebb218a8113739492be59105d6e2 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Thu, 31 Oct 2024 00:46:38 +0530 Subject: [PATCH 09/15] dependencies --- consensus-types/light-client/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/consensus-types/light-client/BUILD.bazel b/consensus-types/light-client/BUILD.bazel index 78432c8eecba..fd01666d01b5 100644 --- a/consensus-types/light-client/BUILD.bazel +++ b/consensus-types/light-client/BUILD.bazel @@ -23,6 +23,7 @@ go_library( "//proto/prysm/v1alpha1:go_default_library", "//runtime/version:go_default_library", "//time/slots:go_default_library", + "@com_github_pkg_errors//:go_default_library", "@org_golang_google_protobuf//proto:go_default_library", ], ) From 058ac0b0d8246dc874ea2e589cebe6d3dd5ffa7c Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Thu, 31 Oct 2024 19:02:02 +0530 Subject: [PATCH 10/15] fix `createDefaultLightClientUpdate` --- beacon-chain/core/light-client/lightclient.go | 14 ++-- beacon-chain/db/kv/lightclient_test.go | 15 ++-- .../rpc/eth/light-client/handlers_test.go | 2 +- .../rpc/eth/light-client/helpers_test.go | 68 ++++++++++--------- 4 files changed, 54 insertions(+), 45 deletions(-) diff --git a/beacon-chain/core/light-client/lightclient.go b/beacon-chain/core/light-client/lightclient.go index aae6d0dd7933..585dc2132f1c 100644 --- a/beacon-chain/core/light-client/lightclient.go +++ b/beacon-chain/core/light-client/lightclient.go @@ -159,7 +159,7 @@ func NewLightClientUpdateFromBeaconState( updateAttestedPeriod := slots.SyncCommitteePeriod(slots.ToEpoch(attestedBlock.Block().Slot())) // update = LightClientUpdate() - result, err := CreateDefaultLightClientUpdate(state) + result, err := CreateDefaultLightClientUpdate(state, currentSlot) if err != nil { return nil, errors.Wrap(err, "could not create default light client update") } @@ -243,7 +243,9 @@ func NewLightClientUpdateFromBeaconState( return result, nil } -func CreateDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightClientUpdate, error) { +func CreateDefaultLightClientUpdate(attestedState state.BeaconState, currentSlot primitives.Slot) (interfaces.LightClientUpdate, error) { + currentEpoch := slots.ToEpoch(currentSlot) + syncCommitteeSize := params.BeaconConfig().SyncCommitteeSize pubKeys := make([][]byte, syncCommitteeSize) for i := uint64(0); i < syncCommitteeSize; i++ { @@ -255,7 +257,7 @@ func CreateDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightCl } var nextSyncCommitteeBranch [][]byte - if state.Version() >= version.Electra { + if attestedState.Version() >= version.Electra { nextSyncCommitteeBranch = make([][]byte, fieldparams.SyncCommitteeBranchDepthElectra) } else { nextSyncCommitteeBranch = make([][]byte, fieldparams.SyncCommitteeBranchDepth) @@ -274,7 +276,7 @@ func CreateDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightCl } var m proto.Message - if state.Version() < version.Capella { + if currentEpoch < params.BeaconConfig().CapellaForkEpoch { m = &pb.LightClientUpdateAltair{ AttestedHeader: &pb.LightClientHeaderAltair{ Beacon: &pb.BeaconBlockHeader{}, @@ -283,7 +285,7 @@ func CreateDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightCl NextSyncCommitteeBranch: nextSyncCommitteeBranch, FinalityBranch: finalityBranch, } - } else if state.Version() < version.Deneb { + } else if currentEpoch < params.BeaconConfig().DenebForkEpoch { m = &pb.LightClientUpdateCapella{ AttestedHeader: &pb.LightClientHeaderCapella{ Beacon: &pb.BeaconBlockHeader{}, @@ -294,7 +296,7 @@ func CreateDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightCl NextSyncCommitteeBranch: nextSyncCommitteeBranch, FinalityBranch: finalityBranch, } - } else if state.Version() < version.Electra { + } else if currentEpoch < params.BeaconConfig().ElectraForkEpoch { m = &pb.LightClientUpdateDeneb{ AttestedHeader: &pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{}, diff --git a/beacon-chain/db/kv/lightclient_test.go b/beacon-chain/db/kv/lightclient_test.go index 046efa0217ec..6694065dad81 100644 --- a/beacon-chain/db/kv/lightclient_test.go +++ b/beacon-chain/db/kv/lightclient_test.go @@ -17,6 +17,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/runtime/version" "github.com/prysmaticlabs/prysm/v5/testing/require" "github.com/prysmaticlabs/prysm/v5/testing/util" + "github.com/prysmaticlabs/prysm/v5/time/slots" "google.golang.org/protobuf/proto" ) @@ -143,7 +144,7 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { return nil, fmt.Errorf("unsupported version %s", version.String(v)) } - update, err := createDefaultLightClientUpdate(state) + update, err := createDefaultLightClientUpdate(state, slot) require.NoError(t, err) update.SetSignatureSlot(slot - 1) syncCommitteeBits := make([]byte, 64) @@ -440,7 +441,9 @@ func TestStore_LightClientUpdate_RetrieveMissingPeriodDistributed(t *testing.T) require.DeepEqual(t, updates[4], retrievedUpdates[uint64(5)], "retrieved update does not match saved update") } -func createDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightClientUpdate, error) { +func createDefaultLightClientUpdate(attestedState state.BeaconState, currentSlot primitives.Slot) (interfaces.LightClientUpdate, error) { + currentEpoch := slots.ToEpoch(currentSlot) + syncCommitteeSize := params.BeaconConfig().SyncCommitteeSize pubKeys := make([][]byte, syncCommitteeSize) for i := uint64(0); i < syncCommitteeSize; i++ { @@ -452,7 +455,7 @@ func createDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightCl } var nextSyncCommitteeBranch [][]byte - if state.Version() >= version.Electra { + if attestedState.Version() >= version.Electra { nextSyncCommitteeBranch = make([][]byte, fieldparams.SyncCommitteeBranchDepthElectra) } else { nextSyncCommitteeBranch = make([][]byte, fieldparams.SyncCommitteeBranchDepth) @@ -471,14 +474,14 @@ func createDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightCl } var m proto.Message - if state.Version() < version.Capella { + if currentEpoch < params.BeaconConfig().CapellaForkEpoch { m = &pb.LightClientUpdateAltair{ AttestedHeader: &pb.LightClientHeaderAltair{}, NextSyncCommittee: nextSyncCommittee, NextSyncCommitteeBranch: nextSyncCommitteeBranch, FinalityBranch: finalityBranch, } - } else if state.Version() < version.Deneb { + } else if currentEpoch < params.BeaconConfig().DenebForkEpoch { m = &pb.LightClientUpdateCapella{ AttestedHeader: &pb.LightClientHeaderCapella{ Beacon: &pb.BeaconBlockHeader{}, @@ -489,7 +492,7 @@ func createDefaultLightClientUpdate(state state.BeaconState) (interfaces.LightCl NextSyncCommitteeBranch: nextSyncCommitteeBranch, FinalityBranch: finalityBranch, } - } else if state.Version() < version.Electra { + } else if currentEpoch < params.BeaconConfig().ElectraForkEpoch { m = &pb.LightClientUpdateDeneb{ AttestedHeader: &pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{}, diff --git a/beacon-chain/rpc/eth/light-client/handlers_test.go b/beacon-chain/rpc/eth/light-client/handlers_test.go index 27f01f2c82f9..4740015034d9 100644 --- a/beacon-chain/rpc/eth/light-client/handlers_test.go +++ b/beacon-chain/rpc/eth/light-client/handlers_test.go @@ -2000,7 +2000,7 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { return nil, fmt.Errorf("unsupported version %s", version.String(v)) } - update, err := lightclient.CreateDefaultLightClientUpdate(state) + update, err := lightclient.CreateDefaultLightClientUpdate(state, slot) require.NoError(t, err) update.SetSignatureSlot(slot - 1) syncCommitteeBits := make([]byte, 64) diff --git a/beacon-chain/rpc/eth/light-client/helpers_test.go b/beacon-chain/rpc/eth/light-client/helpers_test.go index deda80c16ab3..b2086de27cec 100644 --- a/beacon-chain/rpc/eth/light-client/helpers_test.go +++ b/beacon-chain/rpc/eth/light-client/helpers_test.go @@ -6,7 +6,9 @@ import ( 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/util" @@ -34,12 +36,14 @@ func createNonEmptyFinalityBranch() [][]byte { func TestIsBetterUpdate(t *testing.T) { + config := params.BeaconConfig() + t.Run("new has supermajority but old doesn't", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -57,9 +61,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("old has supermajority but new doesn't", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -77,9 +81,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("new doesn't have supermajority and newNumActiveParticipants is greater than oldNumActiveParticipants", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -97,9 +101,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("new doesn't have supermajority and newNumActiveParticipants is lesser than oldNumActiveParticipants", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -117,9 +121,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("new has relevant sync committee but old doesn't", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -158,9 +162,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("old has relevant sync committee but new doesn't", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -199,9 +203,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("new has finality but old doesn't", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -244,9 +248,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("old has finality but new doesn't", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -289,9 +293,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("new has finality and sync committee finality both but old doesn't have sync committee finality", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -352,9 +356,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("new has finality but doesn't have sync committee finality and old has sync committee finality", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -415,9 +419,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("new has more active participants than old", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -435,9 +439,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("new has less active participants than old", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -455,9 +459,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("new's attested header's slot is lesser than old's attested header's slot", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -518,9 +522,9 @@ func TestIsBetterUpdate(t *testing.T) { t.Run("new's attested header's slot is greater than old's attested header's slot", func(t *testing.T) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -581,9 +585,9 @@ func TestIsBetterUpdate(t *testing.T) { 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) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ @@ -644,9 +648,9 @@ func TestIsBetterUpdate(t *testing.T) { 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) { state, err := util.NewBeaconState() assert.NoError(t, err) - oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + oldUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(1)) assert.NoError(t, err) - newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state) + newUpdate, err := lightclient.CreateDefaultLightClientUpdate(state, primitives.Slot(config.AltairForkEpoch*primitives.Epoch(config.SlotsPerEpoch)).Add(2)) assert.NoError(t, err) oldUpdate.SetSyncAggregate(&pb.SyncAggregate{ From 320f8766918a7f877c16e3d12ceb421f97d36ff8 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Thu, 31 Oct 2024 19:14:29 +0530 Subject: [PATCH 11/15] deps --- beacon-chain/db/kv/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon-chain/db/kv/BUILD.bazel b/beacon-chain/db/kv/BUILD.bazel index 0a40aa767a15..714bfb1f26d4 100644 --- a/beacon-chain/db/kv/BUILD.bazel +++ b/beacon-chain/db/kv/BUILD.bazel @@ -123,6 +123,7 @@ go_test( "//testing/assert:go_default_library", "//testing/require:go_default_library", "//testing/util:go_default_library", + "//time/slots:go_default_library", "@com_github_ethereum_go_ethereum//common:go_default_library", "@com_github_golang_snappy//:go_default_library", "@com_github_pkg_errors//:go_default_library", From 80c11b3efd6c3e54903fa94e7318edd6d812d220 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Sat, 2 Nov 2024 23:43:56 +0530 Subject: [PATCH 12/15] fix errors --- beacon-chain/db/kv/lightclient_test.go | 8 ---- .../rpc/eth/light-client/handlers_test.go | 8 ---- consensus-types/light-client/update.go | 41 +++++-------------- 3 files changed, 10 insertions(+), 47 deletions(-) diff --git a/beacon-chain/db/kv/lightclient_test.go b/beacon-chain/db/kv/lightclient_test.go index 364a6b3575cb..84702567a379 100644 --- a/beacon-chain/db/kv/lightclient_test.go +++ b/beacon-chain/db/kv/lightclient_test.go @@ -44,8 +44,6 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { switch v { case version.Altair: slot = primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) - state, err = util.NewBeaconStateAltair() - require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -60,8 +58,6 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Capella: slot = primitives.Slot(config.CapellaForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) - state, err = util.NewBeaconStateCapella() - require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderCapella{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -90,8 +86,6 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Deneb: slot = primitives.Slot(config.DenebForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) - state, err = util.NewBeaconStateDeneb() - require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -120,8 +114,6 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Electra: slot = primitives.Slot(config.ElectraForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) - state, err = util.NewBeaconStateElectra() - require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, diff --git a/beacon-chain/rpc/eth/light-client/handlers_test.go b/beacon-chain/rpc/eth/light-client/handlers_test.go index f73dbdd444d2..0e91f38e4031 100644 --- a/beacon-chain/rpc/eth/light-client/handlers_test.go +++ b/beacon-chain/rpc/eth/light-client/handlers_test.go @@ -1900,8 +1900,6 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { switch v { case version.Altair: slot = primitives.Slot(config.AltairForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) - state, err = util.NewBeaconStateAltair() - require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderAltair{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -1916,8 +1914,6 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Capella: slot = primitives.Slot(config.CapellaForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) - state, err = util.NewBeaconStateCapella() - require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderCapella{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -1946,8 +1942,6 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Deneb: slot = primitives.Slot(config.DenebForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) - state, err = util.NewBeaconStateDeneb() - require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, @@ -1976,8 +1970,6 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) { require.NoError(t, err) case version.Electra: slot = primitives.Slot(config.ElectraForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1) - state, err = util.NewBeaconStateElectra() - require.NoError(t, err) header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderDeneb{ Beacon: &pb.BeaconBlockHeader{ Slot: 1, diff --git a/consensus-types/light-client/update.go b/consensus-types/light-client/update.go index f5edc8b78799..488015ebe670 100644 --- a/consensus-types/light-client/update.go +++ b/consensus-types/light-client/update.go @@ -5,13 +5,11 @@ import ( "github.com/pkg/errors" 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/interfaces" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" pb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/runtime/version" - "github.com/prysmaticlabs/prysm/v5/time/slots" "google.golang.org/protobuf/proto" ) @@ -596,12 +594,11 @@ func NewWrappedUpdateElectra(p *pb.LightClientUpdateElectra) (interfaces.LightCl } return &updateElectra{ - p: p, - attestedHeader: attestedHeader, - nextSyncCommitteeBranch: scBranch, - nextSyncCommitteeBranchElectra: scBranchElectra, - finalizedHeader: finalizedHeader, - finalityBranch: finalityBranch, + p: p, + attestedHeader: attestedHeader, + nextSyncCommitteeBranch: scBranch, + finalizedHeader: finalizedHeader, + finalityBranch: finalityBranch, }, nil } @@ -648,41 +645,23 @@ func (u *updateElectra) SetNextSyncCommittee(sc *pb.SyncCommittee) { } func (u *updateElectra) NextSyncCommitteeBranch() (interfaces.LightClientSyncCommitteeBranch, error) { - if slots.ToEpoch(u.attestedHeader.Beacon().Slot) < params.BeaconConfig().ElectraForkEpoch { - return u.nextSyncCommitteeBranch, nil - } - return u.nextSyncCommitteeBranch, errors.Wrap(consensustypes.ErrNotSupported("NextSyncCommitteeBranch", version.Electra), "Attested header's version is Electra") + return [5][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranch", version.Electra) } -func (u *updateElectra) SetNextSyncCommitteeBranch(branch [][]byte) error { - if slots.ToEpoch(u.attestedHeader.Beacon().Slot) < params.BeaconConfig().ElectraForkEpoch { - b, err := createBranch[interfaces.LightClientSyncCommitteeBranch]("sync committee", branch, fieldparams.SyncCommitteeBranchDepth) - if err != nil { - return err - } - u.nextSyncCommitteeBranch = b - - u.p.NextSyncCommitteeBranch = branch - - return nil - } - return errors.Wrap(consensustypes.ErrNotSupported("SetNextSyncCommitteeBranch", version.Electra), "Attested header's version is Electra") +func (u *updateElectra) SetNextSyncCommitteeBranch([][]byte) error { + return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranch", version.Electra) } func (u *updateElectra) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) { - return u.nextSyncCommitteeBranchElectra, nil + return u.nextSyncCommitteeBranch, nil } func (u *updateElectra) SetNextSyncCommitteeBranchElectra(branch [][]byte) error { - if slots.ToEpoch(u.attestedHeader.Beacon().Slot) < params.BeaconConfig().ElectraForkEpoch { - return errors.Wrap(consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Electra), "Attested header's version is Deneb") - } - b, err := createBranch[interfaces.LightClientSyncCommitteeBranchElectra]("sync committee", branch, fieldparams.SyncCommitteeBranchDepthElectra) if err != nil { return err } - u.nextSyncCommitteeBranchElectra = b + u.nextSyncCommitteeBranch = b u.p.NextSyncCommitteeBranch = branch From 14cf3f8fbce41071f53cfcffe0b4d01d4b3f3127 Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Sun, 3 Nov 2024 16:51:02 +0530 Subject: [PATCH 13/15] deps --- consensus-types/light-client/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/consensus-types/light-client/BUILD.bazel b/consensus-types/light-client/BUILD.bazel index 78432c8eecba..fd01666d01b5 100644 --- a/consensus-types/light-client/BUILD.bazel +++ b/consensus-types/light-client/BUILD.bazel @@ -23,6 +23,7 @@ go_library( "//proto/prysm/v1alpha1:go_default_library", "//runtime/version:go_default_library", "//time/slots:go_default_library", + "@com_github_pkg_errors//:go_default_library", "@org_golang_google_protobuf//proto:go_default_library", ], ) From ddf50950527e527e6a005d982557223f4fa0c47d Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Sun, 3 Nov 2024 22:01:48 +0530 Subject: [PATCH 14/15] revert error messages --- consensus-types/light-client/update.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/consensus-types/light-client/update.go b/consensus-types/light-client/update.go index 488015ebe670..4e0ffe4ea533 100644 --- a/consensus-types/light-client/update.go +++ b/consensus-types/light-client/update.go @@ -3,7 +3,6 @@ package light_client import ( "fmt" - "github.com/pkg/errors" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" consensustypes "github.com/prysmaticlabs/prysm/v5/consensus-types" "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" @@ -147,11 +146,11 @@ func (u *updateAltair) SetNextSyncCommitteeBranch(branch [][]byte) error { } func (u *updateAltair) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) { - return [6][32]byte{}, errors.Wrap(consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Altair), "Attested header's version is Altair") + return [6][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Altair) } func (u *updateAltair) SetNextSyncCommitteeBranchElectra([][]byte) error { - return errors.Wrap(consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Altair), "Attested header's version is Altair") + return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Altair) } func (u *updateAltair) FinalizedHeader() interfaces.LightClientHeader { @@ -318,11 +317,11 @@ func (u *updateCapella) SetNextSyncCommitteeBranch(branch [][]byte) error { } func (u *updateCapella) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) { - return [6][32]byte{}, errors.Wrap(consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Capella), "Attested header's version is Capella") + return [6][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Capella) } func (u *updateCapella) SetNextSyncCommitteeBranchElectra([][]byte) error { - return errors.Wrap(consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Capella), "Attested header's version is Capella") + return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Capella) } func (u *updateCapella) FinalizedHeader() interfaces.LightClientHeader { @@ -489,11 +488,11 @@ func (u *updateDeneb) SetNextSyncCommitteeBranch(branch [][]byte) error { } func (u *updateDeneb) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) { - return [6][32]byte{}, errors.Wrap(consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Deneb), "Attested header's version is Deneb") + return [6][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Deneb) } func (u *updateDeneb) SetNextSyncCommitteeBranchElectra([][]byte) error { - return errors.Wrap(consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Deneb), "Attested header's version is Deneb") + return consensustypes.ErrNotSupported("SetNextSyncCommitteeBranchElectra", version.Deneb) } func (u *updateDeneb) FinalizedHeader() interfaces.LightClientHeader { From 23fb294922c158e681197cee21a615bf8de3b61f Mon Sep 17 00:00:00 2001 From: rupam-04 Date: Sun, 3 Nov 2024 22:02:04 +0530 Subject: [PATCH 15/15] deps --- consensus-types/light-client/BUILD.bazel | 1 - 1 file changed, 1 deletion(-) diff --git a/consensus-types/light-client/BUILD.bazel b/consensus-types/light-client/BUILD.bazel index fd01666d01b5..78432c8eecba 100644 --- a/consensus-types/light-client/BUILD.bazel +++ b/consensus-types/light-client/BUILD.bazel @@ -23,7 +23,6 @@ go_library( "//proto/prysm/v1alpha1:go_default_library", "//runtime/version:go_default_library", "//time/slots:go_default_library", - "@com_github_pkg_errors//:go_default_library", "@org_golang_google_protobuf//proto:go_default_library", ], )