From e0ea75321fd50d9a46ced04ef50781addd30553a Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Wed, 9 Oct 2024 11:05:43 +0200 Subject: [PATCH] adds test --- relayer/relays/beacon/header/header.go | 2 ++ relayer/relays/beacon/header/header_test.go | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/relayer/relays/beacon/header/header.go b/relayer/relays/beacon/header/header.go index f95a3a5afe..cf6abfc109 100644 --- a/relayer/relays/beacon/header/header.go +++ b/relayer/relays/beacon/header/header.go @@ -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 @@ -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) diff --git a/relayer/relays/beacon/header/header_test.go b/relayer/relays/beacon/header/header_test.go index 02e2f3764e..6591e32930 100644 --- a/relayer/relays/beacon/header/header_test.go +++ b/relayer/relays/beacon/header/header_test.go @@ -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)