Skip to content

Commit

Permalink
trixy's review
Browse files Browse the repository at this point in the history
  • Loading branch information
rkapka committed Nov 1, 2024
1 parent c34d89e commit 3aa75e2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
4 changes: 2 additions & 2 deletions api/server/structs/conversions_lightclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func LightClientUpdateFromConsensus(update interfaces.LightClientUpdate) (*Light
}
finalityBranch = fb[:]
} else {
b, err := update.NextSyncCommitteeBranch()
scb, err := update.NextSyncCommitteeBranch()
if err != nil {
return nil, err
}
scBranch = b[:]
scBranch = scb[:]
fb, err := update.FinalityBranch()
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/light-client/lightclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func NewLightClientUpdateFromBeaconState(
updateAttestedPeriod := slots.SyncCommitteePeriod(slots.ToEpoch(attestedBlock.Block().Slot()))

// update = LightClientUpdate()
result, err := CreateDefaultLightClientUpdate(currentSlot, attestedBlock)
result, err := CreateDefaultLightClientUpdate(currentSlot, attestedState)
if err != nil {
return nil, errors.Wrap(err, "could not create default light client update")
}
Expand Down Expand Up @@ -239,7 +239,7 @@ func NewLightClientUpdateFromBeaconState(
return result, nil
}

func CreateDefaultLightClientUpdate(currentSlot primitives.Slot, attestedBlock interfaces.ReadOnlySignedBeaconBlock) (interfaces.LightClientUpdate, error) {
func CreateDefaultLightClientUpdate(currentSlot primitives.Slot, attestedState state.BeaconState) (interfaces.LightClientUpdate, error) {
currentEpoch := slots.ToEpoch(currentSlot)

syncCommitteeSize := params.BeaconConfig().SyncCommitteeSize
Expand Down Expand Up @@ -268,7 +268,7 @@ func CreateDefaultLightClientUpdate(currentSlot primitives.Slot, attestedBlock i
}

var finalityBranch [][]byte
if attestedBlock.Version() >= version.Electra {
if attestedState.Version() >= version.Electra {
finalityBranch = make([][]byte, fieldparams.FinalityBranchDepthElectra)
} else {
finalityBranch = make([][]byte, fieldparams.FinalityBranchDepth)
Expand Down Expand Up @@ -310,7 +310,7 @@ func CreateDefaultLightClientUpdate(currentSlot primitives.Slot, attestedBlock i
FinalityBranch: finalityBranch,
}
} else {
if attestedBlock.Version() >= version.Electra {
if attestedState.Version() >= version.Electra {
m = &pb.LightClientUpdateElectra{
AttestedHeader: &pb.LightClientHeaderDeneb{
Beacon: &pb.BeaconBlockHeader{},
Expand Down
56 changes: 43 additions & 13 deletions beacon-chain/db/kv/lightclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -15,6 +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/testing/util"
"github.com/prysmaticlabs/prysm/v5/time/slots"
"google.golang.org/protobuf/proto"
)
Expand All @@ -23,6 +25,7 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) {
config := params.BeaconConfig()
var slot primitives.Slot
var header interfaces.LightClientHeader
var st state.BeaconState
var err error

sampleRoot := make([]byte, 32)
Expand Down Expand Up @@ -51,6 +54,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) {
},
})
require.NoError(t, err)
st, err = util.NewBeaconState()
require.NoError(t, err)
case version.Capella:
slot = primitives.Slot(config.CapellaForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1)
header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderCapella{
Expand All @@ -77,6 +82,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) {
ExecutionBranch: sampleExecutionBranch,
})
require.NoError(t, err)
st, err = util.NewBeaconStateCapella()
require.NoError(t, err)
case version.Deneb:
slot = primitives.Slot(config.DenebForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1)
header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderDeneb{
Expand All @@ -103,6 +110,8 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) {
ExecutionBranch: sampleExecutionBranch,
})
require.NoError(t, err)
st, err = util.NewBeaconStateDeneb()
require.NoError(t, err)
case version.Electra:
slot = primitives.Slot(config.ElectraForkEpoch * primitives.Epoch(config.SlotsPerEpoch)).Add(1)
header, err = light_client.NewWrappedHeader(&pb.LightClientHeaderDeneb{
Expand All @@ -129,11 +138,13 @@ func createUpdate(t *testing.T, v int) (interfaces.LightClientUpdate, error) {
ExecutionBranch: sampleExecutionBranch,
})
require.NoError(t, err)
st, err = util.NewBeaconStateElectra()
require.NoError(t, err)
default:
return nil, fmt.Errorf("unsupported version %s", version.String(v))
}

update, err := createDefaultLightClientUpdate(slot)
update, err := createDefaultLightClientUpdate(slot, st)
require.NoError(t, err)
update.SetSignatureSlot(slot - 1)
syncCommitteeBits := make([]byte, 64)
Expand Down Expand Up @@ -430,7 +441,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) {
func createDefaultLightClientUpdate(currentSlot primitives.Slot, attestedState state.BeaconState) (interfaces.LightClientUpdate, error) {
currentEpoch := slots.ToEpoch(currentSlot)

syncCommitteeSize := params.BeaconConfig().SyncCommitteeSize
Expand All @@ -457,8 +468,14 @@ func createDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig
for i := 0; i < fieldparams.ExecutionBranchDepth; i++ {
executionBranch[i] = make([]byte, 32)
}
finalityBranch := make([][]byte, fieldparams.FinalityBranchDepth)
for i := 0; i < fieldparams.FinalityBranchDepth; i++ {

var finalityBranch [][]byte
if attestedState.Version() >= version.Electra {
finalityBranch = make([][]byte, fieldparams.FinalityBranchDepthElectra)
} else {
finalityBranch = make([][]byte, fieldparams.FinalityBranchDepth)
}
for i := 0; i < len(finalityBranch); i++ {
finalityBranch[i] = make([]byte, 32)
}

Expand Down Expand Up @@ -493,15 +510,28 @@ func createDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig
FinalityBranch: finalityBranch,
}
} else {
m = &pb.LightClientUpdateElectra{
AttestedHeader: &pb.LightClientHeaderDeneb{
Beacon: &pb.BeaconBlockHeader{},
Execution: &enginev1.ExecutionPayloadHeaderDeneb{},
ExecutionBranch: executionBranch,
},
NextSyncCommittee: nextSyncCommittee,
NextSyncCommitteeBranch: nextSyncCommitteeBranch,
FinalityBranch: finalityBranch,
if attestedState.Version() >= version.Electra {
m = &pb.LightClientUpdateElectra{
AttestedHeader: &pb.LightClientHeaderDeneb{
Beacon: &pb.BeaconBlockHeader{},
Execution: &enginev1.ExecutionPayloadHeaderDeneb{},
ExecutionBranch: executionBranch,
},
NextSyncCommittee: nextSyncCommittee,
NextSyncCommitteeBranch: nextSyncCommitteeBranch,
FinalityBranch: finalityBranch,
}
} else {
m = &pb.LightClientUpdateDeneb{
AttestedHeader: &pb.LightClientHeaderDeneb{
Beacon: &pb.BeaconBlockHeader{},
Execution: &enginev1.ExecutionPayloadHeaderDeneb{},
ExecutionBranch: executionBranch,
},
NextSyncCommittee: nextSyncCommittee,
NextSyncCommitteeBranch: nextSyncCommitteeBranch,
FinalityBranch: finalityBranch,
}
}
}

Expand Down

0 comments on commit 3aa75e2

Please sign in to comment.