Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

added test to TestLightClientHandler_GetLightClientFinalityUpdateAltair #14449

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
130 changes: 115 additions & 15 deletions beacon-chain/rpc/eth/light-client/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -1146,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(&ethpb.Checkpoint{
Epoch: config.AltairForkEpoch - 10,
Root: make([]byte, 32),
Root: finalizedRoot[:],
}))

parent := util.NewBeaconBlockAltair()
Expand Down Expand Up @@ -1209,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,
Expand All @@ -1235,15 +1245,39 @@ 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
err = json.Unmarshal(resp.Data.AttestedHeader, &respHeader)
require.NoError(t, err)
require.Equal(t, "altair", resp.Version)
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.NotNil(t, resp.Data)
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_GetLightClientFinalityUpdateCapella(t *testing.T) {
Expand All @@ -1256,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(&ethpb.Checkpoint{
Epoch: config.AltairForkEpoch - 10,
Root: make([]byte, 32),
Root: finalizedRoot[:],
}))

parent := util.NewBeaconBlockCapella()
Expand Down Expand Up @@ -1319,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,
Expand All @@ -1345,15 +1387,40 @@ 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)
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.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))
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) {
Expand All @@ -1366,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(&ethpb.Checkpoint{
Epoch: config.AltairForkEpoch - 10,
Root: make([]byte, 32),
Root: finalizedRoot[:],
}))

parent := util.NewBeaconBlockDeneb()
Expand Down Expand Up @@ -1429,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,
Expand Down Expand Up @@ -1463,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.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))
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) {
Expand Down