mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
beacon/light/api: include canonical and finalized flags in header response
This commit is contained in:
parent
2e06fbd409
commit
64a333a070
3 changed files with 18 additions and 9 deletions
|
|
@ -73,8 +73,10 @@ func (s *ApiServer) SendRequest(id request.ID, req request.Request) {
|
||||||
r.Updates, r.Committees, err = s.api.GetBestUpdatesAndCommittees(data.FirstPeriod, data.Count)
|
r.Updates, r.Committees, err = s.api.GetBestUpdatesAndCommittees(data.FirstPeriod, data.Count)
|
||||||
resp = r
|
resp = r
|
||||||
case sync.ReqHeader:
|
case sync.ReqHeader:
|
||||||
|
var r sync.RespHeader
|
||||||
log.Debug("Beacon API: requesting header", "reqid", id, "hash", common.Hash(data))
|
log.Debug("Beacon API: requesting header", "reqid", id, "hash", common.Hash(data))
|
||||||
resp, err = s.api.GetHeader(common.Hash(data))
|
r.Header, r.Canonical, r.Finalized, err = s.api.GetHeader(common.Hash(data))
|
||||||
|
resp = r
|
||||||
case sync.ReqCheckpointData:
|
case sync.ReqCheckpointData:
|
||||||
log.Debug("Beacon API: requesting checkpoint data", "reqid", id, "hash", common.Hash(data))
|
log.Debug("Beacon API: requesting checkpoint data", "reqid", id, "hash", common.Hash(data))
|
||||||
resp, err = s.api.GetCheckpointData(common.Hash(data))
|
resp, err = s.api.GetCheckpointData(common.Hash(data))
|
||||||
|
|
|
||||||
|
|
@ -291,7 +291,9 @@ func decodeFinalityUpdate(enc []byte) (types.FinalityUpdate, error) {
|
||||||
|
|
||||||
// GetHeader fetches and validates the beacon header with the given blockRoot.
|
// GetHeader fetches and validates the beacon header with the given blockRoot.
|
||||||
// If blockRoot is null hash then the latest head header is fetched.
|
// If blockRoot is null hash then the latest head header is fetched.
|
||||||
func (api *BeaconLightApi) GetHeader(blockRoot common.Hash) (types.Header, error) {
|
// The values of the canonical and finalized flags are also returned. Note that
|
||||||
|
// these flags are not validated.
|
||||||
|
func (api *BeaconLightApi) GetHeader(blockRoot common.Hash) (types.Header, bool, bool, error) {
|
||||||
var blockId string
|
var blockId string
|
||||||
if blockRoot == (common.Hash{}) {
|
if blockRoot == (common.Hash{}) {
|
||||||
blockId = "head"
|
blockId = "head"
|
||||||
|
|
@ -300,10 +302,11 @@ func (api *BeaconLightApi) GetHeader(blockRoot common.Hash) (types.Header, error
|
||||||
}
|
}
|
||||||
resp, err := api.httpGetf("/eth/v1/beacon/headers/%s", blockId)
|
resp, err := api.httpGetf("/eth/v1/beacon/headers/%s", blockId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.Header{}, err
|
return types.Header{}, false, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
|
Finalized bool `json:"finalized"`
|
||||||
Data struct {
|
Data struct {
|
||||||
Root common.Hash `json:"root"`
|
Root common.Hash `json:"root"`
|
||||||
Canonical bool `json:"canonical"`
|
Canonical bool `json:"canonical"`
|
||||||
|
|
@ -314,16 +317,16 @@ func (api *BeaconLightApi) GetHeader(blockRoot common.Hash) (types.Header, error
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(resp, &data); err != nil {
|
if err := json.Unmarshal(resp, &data); err != nil {
|
||||||
return types.Header{}, err
|
return types.Header{}, false, false, err
|
||||||
}
|
}
|
||||||
header := data.Data.Header.Message
|
header := data.Data.Header.Message
|
||||||
if blockRoot == (common.Hash{}) {
|
if blockRoot == (common.Hash{}) {
|
||||||
blockRoot = data.Data.Root
|
blockRoot = data.Data.Root
|
||||||
}
|
}
|
||||||
if header.Hash() != blockRoot {
|
if header.Hash() != blockRoot {
|
||||||
return types.Header{}, errors.New("retrieved beacon header root does not match")
|
return types.Header{}, false, false, errors.New("retrieved beacon header root does not match")
|
||||||
}
|
}
|
||||||
return header, nil
|
return header, data.Data.Canonical, data.Finalized, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCheckpointData fetches and validates bootstrap data belonging to the given checkpoint.
|
// GetCheckpointData fetches and validates bootstrap data belonging to the given checkpoint.
|
||||||
|
|
@ -446,7 +449,7 @@ func (api *BeaconLightApi) StartHeadListener(listener HeadEventListener) func()
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
// Request initial data.
|
// Request initial data.
|
||||||
if head, err := api.GetHeader(common.Hash{}); err == nil {
|
if head, _, _, err := api.GetHeader(common.Hash{}); err == nil {
|
||||||
listener.OnNewHead(head.Slot, head.Hash())
|
listener.OnNewHead(head.Slot, head.Hash())
|
||||||
}
|
}
|
||||||
if signedHead, err := api.GetOptimisticHeadUpdate(); err == nil {
|
if signedHead, err := api.GetOptimisticHeadUpdate(); err == nil {
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,10 @@ type (
|
||||||
Committees []*types.SerializedSyncCommittee
|
Committees []*types.SerializedSyncCommittee
|
||||||
}
|
}
|
||||||
ReqHeader common.Hash
|
ReqHeader common.Hash
|
||||||
|
RespHeader struct {
|
||||||
|
Header types.Header
|
||||||
|
Canonical, Finalized bool
|
||||||
|
}
|
||||||
ReqCheckpointData common.Hash
|
ReqCheckpointData common.Hash
|
||||||
ReqBeaconBlock common.Hash
|
ReqBeaconBlock common.Hash
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue