Skip to content

Commit

Permalink
Merge with master
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Nov 12, 2020
1 parent a85196f commit 2e5ee7f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
39 changes: 18 additions & 21 deletions ingest/ledgerbackend/captive_core_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions ingest/ledgerbackend/captive_core_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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")

Expand All @@ -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)
Expand Down

0 comments on commit 2e5ee7f

Please sign in to comment.