From 11025a6e31d3ed695b121adf088c5a384459778b Mon Sep 17 00:00:00 2001 From: guha-rahul <69rahul16@gmail.com> Date: Fri, 13 Sep 2024 16:45:39 +0530 Subject: [PATCH 1/5] added test to TestLightClientHandler_GetLightClientFinalityUpdateAltair --- .../rpc/eth/light-client/handlers_test.go | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/beacon-chain/rpc/eth/light-client/handlers_test.go b/beacon-chain/rpc/eth/light-client/handlers_test.go index 245def3d2218..b614c2a06d46 100644 --- a/beacon-chain/rpc/eth/light-client/handlers_test.go +++ b/beacon-chain/rpc/eth/light-client/handlers_test.go @@ -7,12 +7,14 @@ import ( "fmt" "net/http" "net/http/httptest" + "strconv" "testing" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/prysmaticlabs/prysm/v5/api/server/structs" mock "github.com/prysmaticlabs/prysm/v5/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers" + lightclient "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/light-client" "github.com/prysmaticlabs/prysm/v5/beacon-chain/rpc/testutil" "github.com/prysmaticlabs/prysm/v5/beacon-chain/state" "github.com/prysmaticlabs/prysm/v5/config/params" @@ -1243,7 +1245,27 @@ func TestLightClientHandler_GetLightClientFinalityUpdateAltair(t *testing.T) { require.NoError(t, err) require.Equal(t, "altair", resp.Version) require.Equal(t, hexutil.Encode(attestedHeader.BodyRoot), respHeader.Beacon.BodyRoot) - require.NotNil(t, resp.Data) + require.Equal(t, hexutil.Encode(attestedHeader.ParentRoot), respHeader.Beacon.ParentRoot) + require.Equal(t, hexutil.Encode(parent.Block.StateRoot), respHeader.Beacon.StateRoot) + require.Equal(t, strconv.FormatUint(uint64(attestedHeader.Slot), 10), respHeader.Beacon.Slot) + require.Equal(t, strconv.FormatUint(uint64(attestedHeader.ProposerIndex), 10), respHeader.Beacon.ProposerIndex) + require.Equal(t, len(resp.Data.FinalityBranch), lightclient.FinalityBranchNumOfLeaves) + // var finalizedHeader structs.LightClientHeader + // err = json.Unmarshal(resp.Data.FinalizedHeader, &finalizedHeader) + // require.NoError(t, err) + // require.Equal(t, finalizedHeader.Beacon, nil) + //require.Equal(t, finalizedHeader, finalizedHeader.Beacon.BodyRoot) + //require.Equal(t, finalizedHeader.Beacon.ParentRoot, hexutil.Encode(parentHeader.Header.ParentRoot)) + //require.Equal(t, finalizedHeader.Beacon.StateRoot, nil) + // require.Equal(t, finalizedHeader.Beacon.ProposerIndex, strconv.FormatUint(uint64(parentHeader.Header.ProposerIndex), 10)) + // require.NoError(t, err) + //require.Equal(t, finalizedHeader.Beacon.BodyRoot, expectedBodyRoot) + //require.Equal(t, finalizedHeader.Beacon.Slot, strconv.FormatUint(uint64(signedParent.Block().Slot()), 10)) + require.Equal(t, resp.Data.SignatureSlot, strconv.FormatUint(uint64(signedBlock.Block().Slot()), 10)) + SyncAggregate, err := signedBlock.Block().Body().SyncAggregate() + require.NoError(t, err) + require.Equal(t, resp.Data.SyncAggregate.SyncCommitteeBits, hexutil.Encode(SyncAggregate.SyncCommitteeBits)) + require.Equal(t, resp.Data.SyncAggregate.SyncCommitteeSignature, hexutil.Encode(SyncAggregate.SyncCommitteeSignature)) } func TestLightClientHandler_GetLightClientFinalityUpdateCapella(t *testing.T) { From e8c40128e4b571530208fc09f2285dd4230af563 Mon Sep 17 00:00:00 2001 From: guha-rahul <69rahul16@gmail.com> Date: Sat, 14 Sep 2024 19:36:42 +0530 Subject: [PATCH 2/5] fix finalised header in GetLightClientFinalityUpdateAltair --- .../rpc/eth/light-client/handlers_test.go | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/beacon-chain/rpc/eth/light-client/handlers_test.go b/beacon-chain/rpc/eth/light-client/handlers_test.go index bf4c03189740..5e613af68679 100644 --- a/beacon-chain/rpc/eth/light-client/handlers_test.go +++ b/beacon-chain/rpc/eth/light-client/handlers_test.go @@ -1148,10 +1148,17 @@ func TestLightClientHandler_GetLightClientFinalityUpdateAltair(t *testing.T) { require.NoError(t, err) err = attestedState.SetSlot(slot.Sub(1)) require.NoError(t, err) + finalizedBlock, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlockAltair()) + require.NoError(t, err) + finalizedBlock.SetSlot(1) + finalizedHeader, err := finalizedBlock.Header() + require.NoError(t, err) + finalizedRoot, err := finalizedHeader.Header.HashTreeRoot() + require.NoError(t, err) require.NoError(t, attestedState.SetFinalizedCheckpoint(ðpb.Checkpoint{ Epoch: config.AltairForkEpoch - 10, - Root: make([]byte, 32), + Root: finalizedRoot[:], })) parent := util.NewBeaconBlockAltair() @@ -1211,8 +1218,9 @@ func TestLightClientHandler_GetLightClientFinalityUpdateAltair(t *testing.T) { mockBlocker := &testutil.MockBlocker{ RootBlockMap: map[[32]byte]interfaces.ReadOnlySignedBeaconBlock{ - parentRoot: signedParent, - root: signedBlock, + parentRoot: signedParent, + root: signedBlock, + finalizedRoot: finalizedBlock, }, SlotBlockMap: map[primitives.Slot]interfaces.ReadOnlySignedBeaconBlock{ slot.Sub(1): signedParent, @@ -1244,24 +1252,28 @@ func TestLightClientHandler_GetLightClientFinalityUpdateAltair(t *testing.T) { err = json.Unmarshal(resp.Data.AttestedHeader, &respHeader) require.NoError(t, err) require.Equal(t, "altair", resp.Version) - require.Equal(t, hexutil.Encode(attestedHeader.BodyRoot), respHeader.Beacon.BodyRoot) - require.Equal(t, hexutil.Encode(attestedHeader.ParentRoot), respHeader.Beacon.ParentRoot) - require.Equal(t, hexutil.Encode(parent.Block.StateRoot), respHeader.Beacon.StateRoot) require.Equal(t, strconv.FormatUint(uint64(attestedHeader.Slot), 10), respHeader.Beacon.Slot) require.Equal(t, strconv.FormatUint(uint64(attestedHeader.ProposerIndex), 10), respHeader.Beacon.ProposerIndex) + require.Equal(t, hexutil.Encode(attestedHeader.ParentRoot), respHeader.Beacon.ParentRoot) + require.Equal(t, hexutil.Encode(parent.Block.StateRoot), respHeader.Beacon.StateRoot) + require.Equal(t, hexutil.Encode(attestedHeader.BodyRoot), respHeader.Beacon.BodyRoot) require.Equal(t, len(resp.Data.FinalityBranch), lightclient.FinalityBranchNumOfLeaves) - // var finalizedHeader structs.LightClientHeader - // err = json.Unmarshal(resp.Data.FinalizedHeader, &finalizedHeader) - // require.NoError(t, err) - // require.Equal(t, finalizedHeader.Beacon, nil) - //require.Equal(t, finalizedHeader, finalizedHeader.Beacon.BodyRoot) - //require.Equal(t, finalizedHeader.Beacon.ParentRoot, hexutil.Encode(parentHeader.Header.ParentRoot)) - //require.Equal(t, finalizedHeader.Beacon.StateRoot, nil) - // require.Equal(t, finalizedHeader.Beacon.ProposerIndex, strconv.FormatUint(uint64(parentHeader.Header.ProposerIndex), 10)) - // require.NoError(t, err) - //require.Equal(t, finalizedHeader.Beacon.BodyRoot, expectedBodyRoot) - //require.Equal(t, finalizedHeader.Beacon.Slot, strconv.FormatUint(uint64(signedParent.Block().Slot()), 10)) + + var finalizedHeaderresp structs.LightClientHeader + err = json.Unmarshal(resp.Data.FinalizedHeader, &finalizedHeaderresp) + require.NoError(t, err) + require.Equal(t, finalizedHeaderresp.Beacon.Slot, strconv.FormatUint(uint64(finalizedBlock.Block().Slot()), 10)) + require.Equal(t, finalizedHeaderresp.Beacon.ProposerIndex, strconv.FormatUint(uint64(finalizedBlock.Block().ProposerIndex()), 10)) + parentRootresp := finalizedBlock.Block().ParentRoot() + require.Equal(t, finalizedHeaderresp.Beacon.ParentRoot, hexutil.Encode(parentRootresp[:])) + StateRootResp := finalizedBlock.Block().StateRoot() + require.Equal(t, finalizedHeaderresp.Beacon.StateRoot, hexutil.Encode(StateRootResp[:])) + BodyRootResp, err := finalizedBlock.Block().Body().HashTreeRoot() + require.NoError(t, err) + require.Equal(t, finalizedHeaderresp.Beacon.BodyRoot, hexutil.Encode(BodyRootResp[:])) + require.Equal(t, resp.Data.SignatureSlot, strconv.FormatUint(uint64(signedBlock.Block().Slot()), 10)) + SyncAggregate, err := signedBlock.Block().Body().SyncAggregate() require.NoError(t, err) require.Equal(t, resp.Data.SyncAggregate.SyncCommitteeBits, hexutil.Encode(SyncAggregate.SyncCommitteeBits)) From ee3a1c833773ef6483149630312569c7877bcc5e Mon Sep 17 00:00:00 2001 From: guha-rahul <69rahul16@gmail.com> Date: Sat, 14 Sep 2024 19:58:09 +0530 Subject: [PATCH 3/5] test for GetLightClientFinalityUpdateCapella --- .../rpc/eth/light-client/handlers_test.go | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/beacon-chain/rpc/eth/light-client/handlers_test.go b/beacon-chain/rpc/eth/light-client/handlers_test.go index 5e613af68679..7452d9791580 100644 --- a/beacon-chain/rpc/eth/light-client/handlers_test.go +++ b/beacon-chain/rpc/eth/light-client/handlers_test.go @@ -1290,10 +1290,17 @@ func TestLightClientHandler_GetLightClientFinalityUpdateCapella(t *testing.T) { require.NoError(t, err) err = attestedState.SetSlot(slot.Sub(1)) require.NoError(t, err) + finalizedBlock, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlockCapella()) + require.NoError(t, err) + finalizedBlock.SetSlot(1) + finalizedHeader, err := finalizedBlock.Header() + require.NoError(t, err) + finalizedRoot, err := finalizedHeader.Header.HashTreeRoot() + require.NoError(t, err) require.NoError(t, attestedState.SetFinalizedCheckpoint(ðpb.Checkpoint{ Epoch: config.AltairForkEpoch - 10, - Root: make([]byte, 32), + Root: finalizedRoot[:], })) parent := util.NewBeaconBlockCapella() @@ -1353,8 +1360,9 @@ func TestLightClientHandler_GetLightClientFinalityUpdateCapella(t *testing.T) { mockBlocker := &testutil.MockBlocker{ RootBlockMap: map[[32]byte]interfaces.ReadOnlySignedBeaconBlock{ - parentRoot: signedParent, - root: signedBlock, + parentRoot: signedParent, + root: signedBlock, + finalizedRoot: finalizedBlock, }, SlotBlockMap: map[primitives.Slot]interfaces.ReadOnlySignedBeaconBlock{ slot.Sub(1): signedParent, @@ -1387,7 +1395,32 @@ func TestLightClientHandler_GetLightClientFinalityUpdateCapella(t *testing.T) { require.NoError(t, err) require.Equal(t, "capella", resp.Version) require.Equal(t, hexutil.Encode(attestedHeader.BodyRoot), respHeader.Beacon.BodyRoot) - require.NotNil(t, resp.Data) + require.Equal(t, strconv.FormatUint(uint64(attestedHeader.Slot), 10), respHeader.Beacon.Slot) + require.Equal(t, strconv.FormatUint(uint64(attestedHeader.ProposerIndex), 10), respHeader.Beacon.ProposerIndex) + require.Equal(t, hexutil.Encode(attestedHeader.ParentRoot), respHeader.Beacon.ParentRoot) + require.Equal(t, hexutil.Encode(parent.Block.StateRoot), respHeader.Beacon.StateRoot) + require.Equal(t, hexutil.Encode(attestedHeader.BodyRoot), respHeader.Beacon.BodyRoot) + require.Equal(t, len(resp.Data.FinalityBranch), lightclient.FinalityBranchNumOfLeaves) + + var finalizedHeaderresp structs.LightClientHeader + err = json.Unmarshal(resp.Data.FinalizedHeader, &finalizedHeaderresp) + require.NoError(t, err) + require.Equal(t, finalizedHeaderresp.Beacon.Slot, strconv.FormatUint(uint64(finalizedBlock.Block().Slot()), 10)) + require.Equal(t, finalizedHeaderresp.Beacon.ProposerIndex, strconv.FormatUint(uint64(finalizedBlock.Block().ProposerIndex()), 10)) + parentRootresp := finalizedBlock.Block().ParentRoot() + require.Equal(t, finalizedHeaderresp.Beacon.ParentRoot, hexutil.Encode(parentRootresp[:])) + StateRootResp := finalizedBlock.Block().StateRoot() + require.Equal(t, finalizedHeaderresp.Beacon.StateRoot, hexutil.Encode(StateRootResp[:])) + BodyRootResp, err := finalizedBlock.Block().Body().HashTreeRoot() + require.NoError(t, err) + require.Equal(t, finalizedHeaderresp.Beacon.BodyRoot, hexutil.Encode(BodyRootResp[:])) + + require.Equal(t, resp.Data.SignatureSlot, strconv.FormatUint(uint64(signedBlock.Block().Slot()), 10)) + + SyncAggregate, err := signedBlock.Block().Body().SyncAggregate() + require.NoError(t, err) + require.Equal(t, resp.Data.SyncAggregate.SyncCommitteeBits, hexutil.Encode(SyncAggregate.SyncCommitteeBits)) + require.Equal(t, resp.Data.SyncAggregate.SyncCommitteeSignature, hexutil.Encode(SyncAggregate.SyncCommitteeSignature)) } func TestLightClientHandler_GetLightClientFinalityUpdateDeneb(t *testing.T) { From 055b8bc051e08f7502feb6ab61af75cabeb9e123 Mon Sep 17 00:00:00 2001 From: guha-rahul <69rahul16@gmail.com> Date: Sat, 14 Sep 2024 19:58:43 +0530 Subject: [PATCH 4/5] test for GetLightClientFinalityUpdateDeneb --- .../rpc/eth/light-client/handlers_test.go | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/beacon-chain/rpc/eth/light-client/handlers_test.go b/beacon-chain/rpc/eth/light-client/handlers_test.go index 7452d9791580..a368ef8230b7 100644 --- a/beacon-chain/rpc/eth/light-client/handlers_test.go +++ b/beacon-chain/rpc/eth/light-client/handlers_test.go @@ -1433,10 +1433,17 @@ func TestLightClientHandler_GetLightClientFinalityUpdateDeneb(t *testing.T) { require.NoError(t, err) err = attestedState.SetSlot(slot.Sub(1)) require.NoError(t, err) + finalizedBlock, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlockDeneb()) + require.NoError(t, err) + finalizedBlock.SetSlot(1) + finalizedHeader, err := finalizedBlock.Header() + require.NoError(t, err) + finalizedRoot, err := finalizedHeader.Header.HashTreeRoot() + require.NoError(t, err) require.NoError(t, attestedState.SetFinalizedCheckpoint(ðpb.Checkpoint{ Epoch: config.AltairForkEpoch - 10, - Root: make([]byte, 32), + Root: finalizedRoot[:], })) parent := util.NewBeaconBlockDeneb() @@ -1496,8 +1503,9 @@ func TestLightClientHandler_GetLightClientFinalityUpdateDeneb(t *testing.T) { mockBlocker := &testutil.MockBlocker{ RootBlockMap: map[[32]byte]interfaces.ReadOnlySignedBeaconBlock{ - parentRoot: signedParent, - root: signedBlock, + parentRoot: signedParent, + root: signedBlock, + finalizedRoot: finalizedBlock, }, SlotBlockMap: map[primitives.Slot]interfaces.ReadOnlySignedBeaconBlock{ slot.Sub(1): signedParent, @@ -1530,7 +1538,32 @@ func TestLightClientHandler_GetLightClientFinalityUpdateDeneb(t *testing.T) { require.NoError(t, err) require.Equal(t, "deneb", resp.Version) require.Equal(t, hexutil.Encode(attestedHeader.BodyRoot), respHeader.Beacon.BodyRoot) - require.NotNil(t, resp.Data) + require.Equal(t, strconv.FormatUint(uint64(attestedHeader.Slot), 10), respHeader.Beacon.Slot) + require.Equal(t, strconv.FormatUint(uint64(attestedHeader.ProposerIndex), 10), respHeader.Beacon.ProposerIndex) + require.Equal(t, hexutil.Encode(attestedHeader.ParentRoot), respHeader.Beacon.ParentRoot) + require.Equal(t, hexutil.Encode(parent.Block.StateRoot), respHeader.Beacon.StateRoot) + require.Equal(t, hexutil.Encode(attestedHeader.BodyRoot), respHeader.Beacon.BodyRoot) + require.Equal(t, len(resp.Data.FinalityBranch), lightclient.FinalityBranchNumOfLeaves) + + var finalizedHeaderresp structs.LightClientHeader + err = json.Unmarshal(resp.Data.FinalizedHeader, &finalizedHeaderresp) + require.NoError(t, err) + require.Equal(t, finalizedHeaderresp.Beacon.Slot, strconv.FormatUint(uint64(finalizedBlock.Block().Slot()), 10)) + require.Equal(t, finalizedHeaderresp.Beacon.ProposerIndex, strconv.FormatUint(uint64(finalizedBlock.Block().ProposerIndex()), 10)) + parentRootresp := finalizedBlock.Block().ParentRoot() + require.Equal(t, finalizedHeaderresp.Beacon.ParentRoot, hexutil.Encode(parentRootresp[:])) + StateRootResp := finalizedBlock.Block().StateRoot() + require.Equal(t, finalizedHeaderresp.Beacon.StateRoot, hexutil.Encode(StateRootResp[:])) + BodyRootResp, err := finalizedBlock.Block().Body().HashTreeRoot() + require.NoError(t, err) + require.Equal(t, finalizedHeaderresp.Beacon.BodyRoot, hexutil.Encode(BodyRootResp[:])) + + require.Equal(t, resp.Data.SignatureSlot, strconv.FormatUint(uint64(signedBlock.Block().Slot()), 10)) + + SyncAggregate, err := signedBlock.Block().Body().SyncAggregate() + require.NoError(t, err) + require.Equal(t, resp.Data.SyncAggregate.SyncCommitteeBits, hexutil.Encode(SyncAggregate.SyncCommitteeBits)) + require.Equal(t, resp.Data.SyncAggregate.SyncCommitteeSignature, hexutil.Encode(SyncAggregate.SyncCommitteeSignature)) } func TestLightClientHandler_GetLightClientOptimisticUpdateAltair(t *testing.T) { From fca8c67359522ef98c658b501b23d93ab269661a Mon Sep 17 00:00:00 2001 From: guha-rahul <69rahul16@gmail.com> Date: Sun, 15 Sep 2024 14:26:00 +0530 Subject: [PATCH 5/5] fix: types in headers in finality --- beacon-chain/rpc/eth/light-client/handlers_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/beacon-chain/rpc/eth/light-client/handlers_test.go b/beacon-chain/rpc/eth/light-client/handlers_test.go index a368ef8230b7..5df7a99c9f16 100644 --- a/beacon-chain/rpc/eth/light-client/handlers_test.go +++ b/beacon-chain/rpc/eth/light-client/handlers_test.go @@ -1245,7 +1245,7 @@ func TestLightClientHandler_GetLightClientFinalityUpdateAltair(t *testing.T) { s.GetLightClientFinalityUpdate(writer, request) require.Equal(t, http.StatusOK, writer.Code) - var resp *structs.LightClientUpdateResponse + var resp *structs.LightClientFinalityUpdateResponse err = json.Unmarshal(writer.Body.Bytes(), &resp) require.NoError(t, err) var respHeader structs.LightClientHeader @@ -1387,10 +1387,10 @@ func TestLightClientHandler_GetLightClientFinalityUpdateCapella(t *testing.T) { s.GetLightClientFinalityUpdate(writer, request) require.Equal(t, http.StatusOK, writer.Code) - var resp *structs.LightClientUpdateResponse + var resp *structs.LightClientFinalityUpdateResponse err = json.Unmarshal(writer.Body.Bytes(), &resp) require.NoError(t, err) - var respHeader structs.LightClientHeader + var respHeader structs.LightClientHeaderCapella err = json.Unmarshal(resp.Data.AttestedHeader, &respHeader) require.NoError(t, err) require.Equal(t, "capella", resp.Version) @@ -1402,7 +1402,7 @@ func TestLightClientHandler_GetLightClientFinalityUpdateCapella(t *testing.T) { require.Equal(t, hexutil.Encode(attestedHeader.BodyRoot), respHeader.Beacon.BodyRoot) require.Equal(t, len(resp.Data.FinalityBranch), lightclient.FinalityBranchNumOfLeaves) - var finalizedHeaderresp structs.LightClientHeader + var finalizedHeaderresp structs.LightClientHeaderCapella err = json.Unmarshal(resp.Data.FinalizedHeader, &finalizedHeaderresp) require.NoError(t, err) require.Equal(t, finalizedHeaderresp.Beacon.Slot, strconv.FormatUint(uint64(finalizedBlock.Block().Slot()), 10)) @@ -1545,7 +1545,7 @@ func TestLightClientHandler_GetLightClientFinalityUpdateDeneb(t *testing.T) { require.Equal(t, hexutil.Encode(attestedHeader.BodyRoot), respHeader.Beacon.BodyRoot) require.Equal(t, len(resp.Data.FinalityBranch), lightclient.FinalityBranchNumOfLeaves) - var finalizedHeaderresp structs.LightClientHeader + var finalizedHeaderresp structs.LightClientHeaderDeneb err = json.Unmarshal(resp.Data.FinalizedHeader, &finalizedHeaderresp) require.NoError(t, err) require.Equal(t, finalizedHeaderresp.Beacon.Slot, strconv.FormatUint(uint64(finalizedBlock.Block().Slot()), 10))