Skip to content

Commit

Permalink
adds test
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Oct 9, 2024
1 parent ba114cf commit e0ea753
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions relayer/relays/beacon/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ func (h *Header) findLatestCheckPoint(slot uint64) (state.FinalizedHeader, error
// https://github.com/paritytech/polkadot-sdk/blob/master/bridges/snowbridge/pallets/ethereum-client/src/lib.rs#L75
for {
index = (index - 1 + totalStates) % totalStates
// We have searched the whole ringbuffer and could not find the header.
if index == startIndex {
log.WithError(err).Debug("unable to find a relevant on-chain header, max iterations reached")
break
Expand All @@ -584,6 +585,7 @@ func (h *Header) findLatestCheckPoint(slot uint64) (state.FinalizedHeader, error
if beaconState.BeaconSlot > slot && beaconState.BeaconSlot < slot+syncCommitteePeriod {
return beaconState, nil
}

}

return beaconState, fmt.Errorf("no checkpoint on chain for slot %d", slot)
Expand Down
6 changes: 6 additions & 0 deletions relayer/relays/beacon/header/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,12 @@ func TestFindLatestCheckPoint(t *testing.T) {
assert.Equal(t, headerIndex4, header.BeaconBlockRoot)
assert.Equal(t, uint64(46), header.BeaconSlot)

// Check that the first item in the ringbuffer can be found
header, err = h.findLatestCheckPoint(43)
assert.NoError(t, err)
assert.Equal(t, headerIndex4, header.BeaconBlockRoot)
assert.Equal(t, uint64(46), header.BeaconSlot)

// Check last finalized state index outside of the circular array does not cause an infinite loop
mockWriter.LastFinalizedStateIndex = types.U32((maxRedundancy * settings.EpochsPerSyncCommitteePeriod) + 50)
header, err = h.findLatestCheckPoint(40)
Expand Down

0 comments on commit e0ea753

Please sign in to comment.