Skip to content

Commit

Permalink
Avoid waiting for the first checkpoint in captive-core online mode (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Nov 11, 2020
1 parent 507901a commit a25756d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
32 changes: 17 additions & 15 deletions ingest/ledgerbackend/captive_core_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,28 +269,30 @@ func (c *CaptiveStellarCore) runFromParams(from uint32) (runFrom uint32, ledgerH
}

if from <= 63 {
// For ledgers before (and including) first checkpoint, get/wait the first
// checkpoint to get the ledger header. It will always start streaming
// from ledger 2.
// 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.
nextLedger = 2
runFrom = 2
// The line below is to support a special case for streaming ledger 2
// that works for all other ledgers <= 63 (fast-forward).
// We can't set from=2 because Stellar-Core will not allow starting from 1.
// To solve this we start from 3 and exploit the fast that Stellar-Core
// will stream data from 2 for the first checkpoint.
from = 3
} else {
// For ledgers after the first checkpoint, start at the previous checkpoint
// and fast-forward from there.
if !historyarchive.IsCheckpoint(from) {
from = historyarchive.PrevCheckpoint(from)
}
// Streaming will start from the previous checkpoint + 1
nextLedger = from - 63
if nextLedger < 2 {
// Stellar-Core always streams from ledger 2 at min.
nextLedger = 2
}
return
}

// For ledgers after the first checkpoint, start at the previous checkpoint
// and fast-forward from there.
if !historyarchive.IsCheckpoint(from) {
from = historyarchive.PrevCheckpoint(from)
}
// Streaming will start from the previous checkpoint + 1
nextLedger = from - 63
if nextLedger < 2 {
// Stellar-Core always streams from ledger 2 at min.
nextLedger = 2
}

runFrom = from - 1
Expand Down
6 changes: 5 additions & 1 deletion ingest/ledgerbackend/captive_core_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,11 @@ func TestCaptiveRunFromParams(t *testing.T) {
runFrom, ledgerHash, nextLedger, err := captiveBackend.runFromParams(tc.from)
tt.NoError(err)
tt.Equal(tc.runFrom, runFrom, "runFrom")
tt.Equal("0101010100000000000000000000000000000000000000000000000000000000", ledgerHash)
if tc.from <= 63 {
tt.Empty(ledgerHash)
} else {
tt.Equal("0101010100000000000000000000000000000000000000000000000000000000", ledgerHash)
}
tt.Equal(tc.nextLedger, nextLedger, "nextLedger")
})
}
Expand Down
10 changes: 7 additions & 3 deletions ingest/ledgerbackend/stellar_core_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,17 @@ func (r *stellarCoreRunner) runFrom(from uint32, hash string) error {
return errors.New("runner already started")
}
var err error
r.cmd, err = r.createCmd(
args := []string{
"run",
"--in-memory",
"--start-at-ledger", fmt.Sprintf("%d", from),
"--start-at-hash", hash,
"--metadata-output-stream", r.getPipeName(),
)
}
if hash != "" {
args = append(args, "--start-at-hash", hash)
}

r.cmd, err = r.createCmd(args...)
if err != nil {
return errors.Wrap(err, "error creating `stellar-core run` subprocess")
}
Expand Down

0 comments on commit a25756d

Please sign in to comment.