diff --git a/ingest/ledgerbackend/captive_core_backend.go b/ingest/ledgerbackend/captive_core_backend.go index 920f9bc215..c1f296d872 100644 --- a/ingest/ledgerbackend/captive_core_backend.go +++ b/ingest/ledgerbackend/captive_core_backend.go @@ -276,29 +276,11 @@ func (c *CaptiveStellarCore) runFromParams(from uint32) (runFrom uint32, ledgerH err = errors.Wrapf(err, "Cannot fetch ledgers preceding %v", from) return } - if exists && - ledger.Sequence > 1 && // stellar-core cannot run from ledger 1 + useIngestedLedgerHash := exists && // if the last ledger is too far behind, let's use the history archive instead - (from-ledger.Sequence) < 2*historyarchive.CheckpointFreq { - runFrom = ledger.Sequence - ledgerHash = ledger.Hash - if ledger.Sequence < 63 { - // if we run from a sequence which occurs before the first checkpoint - // the first ledger we will stream is 2 - nextLedger = 2 - } else if historyarchive.IsCheckpoint(ledger.Sequence) { - // if we run from a checkpoint sequence - // the first ledger we will stream is the next ledger - nextLedger = ledger.Sequence + 1 - } else { - // if we run from a non-checkpoint sequence - // the first ledger we will stream is the first ledger in the checkpoint range which spans ledger.Sequence - nextLedger = historyarchive.PrevCheckpoint(ledger.Sequence) + 1 - } - return - } + (from-ledger.Sequence) < 2*historyarchive.CheckpointFreq - if from <= 63 { + if from <= 63 || (useIngestedLedgerHash && ledger.Sequence <= 63) { // For ledgers before (and including) first checkpoint, we start streaming // without providing a hash, to avoid waiting for the checkpoint. // It will always start streaming from ledger 2. @@ -313,6 +295,21 @@ func (c *CaptiveStellarCore) runFromParams(from uint32) (runFrom uint32, ledgerH return } + if useIngestedLedgerHash { + runFrom = ledger.Sequence + ledgerHash = ledger.Hash + if historyarchive.IsCheckpoint(ledger.Sequence) { + // if we run from a checkpoint sequence + // the first ledger we will stream is the next ledger + nextLedger = ledger.Sequence + 1 + } else { + // if we run from a non-checkpoint sequence + // the first ledger we will stream is the first ledger in the checkpoint range which spans ledger.Sequence + nextLedger = historyarchive.PrevCheckpoint(ledger.Sequence) + 1 + } + return + } + // For ledgers after the first checkpoint, start at the previous checkpoint // and fast-forward from there. if !historyarchive.IsCheckpoint(from) { diff --git a/ingest/ledgerbackend/captive_core_backend_test.go b/ingest/ledgerbackend/captive_core_backend_test.go index 3c682fea54..d11ffa430b 100644 --- a/ingest/ledgerbackend/captive_core_backend_test.go +++ b/ingest/ledgerbackend/captive_core_backend_test.go @@ -865,14 +865,6 @@ func TestCaptiveUseOfLedgerStore(t *testing.T) { }, }, nil) - mockArchive. - On("GetLedgerHeader", uint32(3)). - Return(xdr.LedgerHeaderHistoryEntry{ - Header: xdr.LedgerHeader{ - PreviousLedgerHash: xdr.Hash{2}, - }, - }, nil) - mockLedgerStore := &MockLedgerStore{} mockLedgerStore.On("LastLedger", uint32(300)). Return(Ledger{Sequence: 19, Hash: "abc"}, true, nil).Once() @@ -884,6 +876,8 @@ func TestCaptiveUseOfLedgerStore(t *testing.T) { Return(Ledger{}, false, nil).Once() mockLedgerStore.On("LastLedger", uint32(86)). Return(Ledger{Sequence: 85, Hash: "cde"}, true, nil).Once() + mockLedgerStore.On("LastLedger", uint32(128)). + Return(Ledger{Sequence: 127, Hash: "ghi"}, true, nil).Once() captiveBackend := CaptiveStellarCore{ archive: mockArchive, @@ -894,8 +888,8 @@ func TestCaptiveUseOfLedgerStore(t *testing.T) { runFrom, ledgerHash, nextLedger, err := captiveBackend.runFromParams(24) assert.NoError(t, err) - assert.Equal(t, uint32(19), runFrom) - assert.Equal(t, "abc", ledgerHash) + assert.Equal(t, uint32(2), runFrom) + assert.Equal(t, "", ledgerHash) assert.Equal(t, uint32(2), nextLedger) runFrom, ledgerHash, nextLedger, err = captiveBackend.runFromParams(86) @@ -904,6 +898,12 @@ func TestCaptiveUseOfLedgerStore(t *testing.T) { assert.Equal(t, "cde", ledgerHash) assert.Equal(t, uint32(64), nextLedger) + runFrom, ledgerHash, nextLedger, err = captiveBackend.runFromParams(128) + assert.NoError(t, err) + assert.Equal(t, uint32(127), runFrom) + assert.Equal(t, "ghi", ledgerHash) + assert.Equal(t, uint32(128), nextLedger) + runFrom, ledgerHash, nextLedger, err = captiveBackend.runFromParams(14) assert.EqualError(t, err, "Cannot fetch ledgers preceding 14: transient error") @@ -916,7 +916,7 @@ func TestCaptiveUseOfLedgerStore(t *testing.T) { runFrom, ledgerHash, nextLedger, err = captiveBackend.runFromParams(9) assert.NoError(t, err) assert.Equal(t, uint32(2), runFrom, "runFrom") - assert.Equal(t, "0200000000000000000000000000000000000000000000000000000000000000", ledgerHash) + assert.Equal(t, "", ledgerHash) assert.Equal(t, uint32(2), nextLedger, "nextLedger") mockLedgerStore.AssertExpectations(t)