Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Fix first execution header with incorrect sync aggregate (paritytech#690
Browse files Browse the repository at this point in the history
)

* Fix first execution header with incorrect sync aggregate

* Fix interface.

* Changes max tries to slots in epoch

* Adds missing increment.
  • Loading branch information
claravanstaden authored Sep 21, 2022
1 parent 0276a3f commit afe3560
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
5 changes: 4 additions & 1 deletion relayer/relays/beacon/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ func (h *Header) SyncHeaders(ctx context.Context) error {
}).Info("starting to back-fill headers")

blockRoot := common.HexToHash(finalizedHeader.FinalizedHeader.ParentRoot.Hex())
prevSyncAggregate := finalizedHeader.SyncAggregate
prevSyncAggregate, err := h.syncer.GetSyncAggregateForSlot(uint64(finalizedHeader.FinalizedHeader.Slot + 1))
if err != nil {
return err
}

headerUpdate, err := h.SyncHeader(ctx, finalizedHeaderBlockRoot, prevSyncAggregate)
if err != nil {
Expand Down
38 changes: 37 additions & 1 deletion relayer/relays/beacon/header/syncer/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type BeaconClientTracker interface {
GetHeader(id string) (BeaconHeader, error)
GetSyncCommitteePeriodUpdate(from uint64) (SyncCommitteePeriodUpdateResponse, error)
GetHeadCheckpoint() (FinalizedCheckpointResponse, error)
GetBeaconBlock(slot uint64) (BeaconBlockResponse, error)
GetBeaconBlock(blockID common.Hash) (BeaconBlockResponse, error)
GetBeaconBlockBySlot(slot uint64) (BeaconBlockResponse, error)
GetCurrentForkVersion(slot uint64) (string, error)
GetLatestFinalizedUpdate() (LatestFinalisedUpdateResponse, error)
}
Expand Down Expand Up @@ -286,6 +287,41 @@ func (b *BeaconClient) GetBeaconBlock(blockID common.Hash) (BeaconBlockResponse,
return response, nil
}

func (b *BeaconClient) GetBeaconBlockBySlot(slot uint64) (BeaconBlockResponse, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/eth/v2/beacon/blocks/%d", b.endpoint, slot), nil)
if err != nil {
return BeaconBlockResponse{}, fmt.Errorf("%s: %w", ConstructRequestErrorMessage, err)
}

req.Header.Set("accept", "application/json")
res, err := b.httpClient.Do(req)
if err != nil {
return BeaconBlockResponse{}, fmt.Errorf("%s: %w", DoHTTPRequestErrorMessage, err)
}

if res.StatusCode != http.StatusOK {
if res.StatusCode == 404 {
return BeaconBlockResponse{}, ErrNotFound
}

return BeaconBlockResponse{}, fmt.Errorf("%s: %d", HTTPStatusNotOKErrorMessage, res.StatusCode)
}

bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return BeaconBlockResponse{}, fmt.Errorf("%s: %w", ReadResponseBodyErrorMessage, err)
}

var response BeaconBlockResponse

err = json.Unmarshal(bodyBytes, &response)
if err != nil {
return BeaconBlockResponse{}, fmt.Errorf("%s: %w", UnmarshalBodyErrorMessage, err)
}

return response, nil
}

func (b *BeaconClient) GetBeaconBlockRoot(slot uint64) (common.Hash, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/eth/v1/beacon/blocks/%d/root", b.endpoint, slot), nil)
if err != nil {
Expand Down
29 changes: 28 additions & 1 deletion relayer/relays/beacon/header/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ethereum/go-ethereum/common"
ssz "github.com/ferranbt/fastssz"
log "github.com/sirupsen/logrus"
"github.com/snowfork/go-substrate-rpc-client/v4/types"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/scale"
)
Expand Down Expand Up @@ -106,7 +107,7 @@ func (s *Syncer) GetSyncPeriodsToFetch(checkpointSyncPeriod uint64) ([]uint64, e

currentSyncPeriod := s.ComputeSyncPeriodAtSlot(slot)

//The current sync period's next sync committee should be synced too. So even
//The current sync period's next sync committee should be synced too. So even
// if the syncing is up to date with the current period, we still need to sync the current
// period's next sync committee.
if checkpointSyncPeriod == currentSyncPeriod {
Expand Down Expand Up @@ -289,6 +290,32 @@ func (s *Syncer) GetSyncAggregate(blockRoot common.Hash) (scale.SyncAggregate, e
return blockScale.Body.SyncAggregate, nil
}

func (s *Syncer) GetSyncAggregateForSlot(slot uint64) (scale.SyncAggregate, error) {
err := ErrNotFound
var block BeaconBlockResponse
tries := 0
maxSlotsMissed := int(s.SlotsInEpoch)
for errors.Is(err, ErrNotFound) && tries < maxSlotsMissed {
log.WithFields(log.Fields{
"try_number": tries,
"slot": slot,
}).Info("fetching sync aggregate for slot")
block, err = s.Client.GetBeaconBlockBySlot(slot)
if err != nil && !errors.Is(err, ErrNotFound) {
return scale.SyncAggregate{}, fmt.Errorf("fetch block: %w", err)
}

tries = tries + 1
slot = slot + 1
}

blockScale, err := block.ToScale()
if err != nil {
return scale.SyncAggregate{}, fmt.Errorf("convert block to scale: %w", err)
}
return blockScale.Body.SyncAggregate, nil
}

func (s *Syncer) ComputeSyncPeriodAtSlot(slot uint64) uint64 {
return slot / (s.SlotsInEpoch * s.EpochsPerSyncCommitteePeriod)
}
Expand Down

0 comments on commit afe3560

Please sign in to comment.