fix: replace BeaconBlockHeader in createLightClientBootstrap with LightClientHeader (#14374)

* fix: replace `BeaconBlockHeader` in `createLightClientBootstrap` with `LightClientHeader`

* minor fix in `handlers_test.go`

* check if `beacon` is `nil` instead of `header`

---------

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
This commit is contained in:
Rupam Dey
2024-08-22 22:43:10 +05:30
committed by GitHub
parent 022a53f8f2
commit dd3c9652c3
3 changed files with 13 additions and 6 deletions

View File

@@ -1,12 +1,16 @@
package structs
type LightClientHeader struct {
Beacon *BeaconBlockHeader `json:"beacon"`
}
type LightClientBootstrapResponse struct {
Version string `json:"version"`
Data *LightClientBootstrap `json:"data"`
}
type LightClientBootstrap struct {
Header *BeaconBlockHeader `json:"header"`
Header *LightClientHeader `json:"header"`
CurrentSyncCommittee *SyncCommittee `json:"current_sync_committee"`
CurrentSyncCommitteeBranch []string `json:"current_sync_committee_branch"`
}

View File

@@ -73,7 +73,7 @@ func TestLightClientHandler_GetLightClientBootstrap(t *testing.T) {
resp := &structs.LightClientBootstrapResponse{}
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
require.Equal(t, "capella", resp.Version)
require.Equal(t, hexutil.Encode(header.Header.BodyRoot), resp.Data.Header.BodyRoot)
require.Equal(t, hexutil.Encode(header.Header.BodyRoot), resp.Data.Header.Beacon.BodyRoot)
require.NotNil(t, resp.Data)
}

View File

@@ -67,17 +67,20 @@ func createLightClientBootstrap(ctx context.Context, state state.BeaconState) (*
branch[i] = hexutil.Encode(proof)
}
header := structs.BeaconBlockHeaderFromConsensus(latestBlockHeader)
if header == nil {
beacon := structs.BeaconBlockHeaderFromConsensus(latestBlockHeader)
if beacon == nil {
return nil, fmt.Errorf("could not get beacon block header")
}
header := &structs.LightClientHeader{
Beacon: beacon,
}
// Above shared util function won't calculate state root, so we need to do it manually
stateRoot, err := state.HashTreeRoot(ctx)
if err != nil {
return nil, fmt.Errorf("could not get state root: %s", err.Error())
}
header.StateRoot = hexutil.Encode(stateRoot[:])
header.Beacon.StateRoot = hexutil.Encode(stateRoot[:])
// Return result
result := &structs.LightClientBootstrap{
@@ -237,7 +240,7 @@ func NewLightClientBootstrapFromJSON(bootstrapJSON *structs.LightClientBootstrap
var err error
v1Alpha1Header, err := bootstrapJSON.Header.ToConsensus()
v1Alpha1Header, err := bootstrapJSON.Header.Beacon.ToConsensus()
if err != nil {
return nil, err
}