Skip to content

Commit 2937c18

Browse files
committed
stellar#4538: use LedgerBackend interface for NewCaptive, fix unit tests that use catchup
1 parent 3b706e4 commit 2937c18

10 files changed

+364
-71
lines changed

ingest/ledger_change_reader_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323

2424
func TestNewLedgerChangeReaderFails(t *testing.T) {
2525
ctx := context.Background()
26-
mock := &ledgerbackend.MockDatabaseBackend{}
26+
mock := &ledgerbackend.MockLedgerBackend{}
2727
seq := uint32(123)
2828
mock.On("GetLedger", ctx, seq).Return(
2929
xdr.LedgerCloseMeta{},
@@ -39,7 +39,7 @@ func TestNewLedgerChangeReaderFails(t *testing.T) {
3939

4040
func TestNewLedgerChangeReaderSucceeds(t *testing.T) {
4141
ctx := context.Background()
42-
mock := &ledgerbackend.MockDatabaseBackend{}
42+
mock := &ledgerbackend.MockLedgerBackend{}
4343
seq := uint32(123)
4444

4545
header := xdr.LedgerHeaderHistoryEntry{
@@ -146,7 +146,7 @@ func assertChangesEqual(
146146

147147
func TestLedgerChangeReaderOrder(t *testing.T) {
148148
ctx := context.Background()
149-
mock := &ledgerbackend.MockDatabaseBackend{}
149+
mock := &ledgerbackend.MockLedgerBackend{}
150150
seq := uint32(123)
151151

152152
src := xdr.MustAddress("GBXGQJWVLWOYHFLVTKWV5FGHA3LNYY2JQKM7OAJAUEQFU6LPCSEFVXON")
@@ -353,7 +353,7 @@ func TestLedgerChangeReaderOrder(t *testing.T) {
353353

354354
func TestLedgerChangeLedgerCloseMetaV2(t *testing.T) {
355355
ctx := context.Background()
356-
mock := &ledgerbackend.MockDatabaseBackend{}
356+
mock := &ledgerbackend.MockLedgerBackend{}
357357
seq := uint32(123)
358358

359359
src := xdr.MustAddress("GBXGQJWVLWOYHFLVTKWV5FGHA3LNYY2JQKM7OAJAUEQFU6LPCSEFVXON")
@@ -600,7 +600,7 @@ func TestLedgerChangeLedgerCloseMetaV2(t *testing.T) {
600600

601601
func TestLedgerChangeLedgerCloseMetaV2Empty(t *testing.T) {
602602
ctx := context.Background()
603-
mock := &ledgerbackend.MockDatabaseBackend{}
603+
mock := &ledgerbackend.MockLedgerBackend{}
604604
seq := uint32(123)
605605

606606
baseFee := xdr.Int64(100)

ingest/ledgerbackend/captive_core_backend.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ type CaptiveStellarCore struct {
9595
stellarCoreLock sync.RWMutex
9696

9797
// For testing
98-
stellarCoreRunnerFactory func() stellarCoreRunnerInterface
98+
stellarCoreRunnerFactory func() stellarCoreRunnerInterface
99+
trustedHashBackendFactory func(config CaptiveCoreConfig) (LedgerBackend, error)
99100

100101
// cachedMeta keeps that ledger data of the last fetched ledger. Updated in GetLedger().
101102
cachedMeta *xdr.LedgerCloseMeta
@@ -163,7 +164,7 @@ type CaptiveCoreConfig struct {
163164
}
164165

165166
// NewCaptive returns a new CaptiveStellarCore instance.
166-
func NewCaptive(config CaptiveCoreConfig) (*CaptiveStellarCore, error) {
167+
func NewCaptive(config CaptiveCoreConfig) (LedgerBackend, error) {
167168
// Here we set defaults in the config. Because config is not a pointer this code should
168169
// not mutate the original CaptiveCoreConfig instance which was passed into NewCaptive()
169170

@@ -233,6 +234,8 @@ func NewCaptive(config CaptiveCoreConfig) (*CaptiveStellarCore, error) {
233234
return newStellarCoreRunner(config)
234235
}
235236

237+
c.trustedHashBackendFactory = NewCaptive
238+
236239
if config.Toml != nil && config.Toml.HTTPPort != 0 {
237240
c.stellarCoreClient = &stellarcore.Client{
238241
HTTP: &http.Client{
@@ -338,8 +341,8 @@ func (c *CaptiveStellarCore) getTrustedHashForLedger(sequence uint32) (uint32, s
338341
// first, direct from network which is considered trusted, after which the captive core instance is closed.
339342
captiveConfigTrustedHash := c.config
340343
captiveConfigTrustedHash.LedgerHashStore = nil
341-
captiveConfigTrustedHash.Context = context.Background()
342-
captiveTrustedHash, err := NewCaptive(captiveConfigTrustedHash)
344+
captiveConfigTrustedHash.Context, _ = context.WithCancel(c.config.Context)
345+
captiveTrustedHash, err := c.trustedHashBackendFactory(captiveConfigTrustedHash)
343346

344347
if sequence <= 2 {
345348
// The line below is to support minimum edge case for streaming ledgers from core in run 'from'
@@ -353,16 +356,16 @@ func (c *CaptiveStellarCore) getTrustedHashForLedger(sequence uint32) (uint32, s
353356

354357
defer func() {
355358
if closeErr := captiveTrustedHash.Close(); closeErr != nil {
356-
captiveTrustedHash.config.Log.Error("error when closing captive core for network hash", closeErr)
359+
captiveConfigTrustedHash.Log.Error("error when closing captive core for network hash", closeErr)
357360
}
358361
}()
359362

360-
err = captiveTrustedHash.PrepareRange(captiveTrustedHash.config.Context, UnboundedRange(sequence))
363+
err = captiveTrustedHash.PrepareRange(captiveConfigTrustedHash.Context, UnboundedRange(sequence))
361364
if err != nil {
362365
return 0, "", errors.Wrapf(err, "error preparing to get network hash for Ledger %v", sequence)
363366
}
364367

365-
networkLCM, err := captiveTrustedHash.GetLedger(captiveTrustedHash.config.Context, sequence)
368+
networkLCM, err := captiveTrustedHash.GetLedger(captiveConfigTrustedHash.Context, sequence)
366369
if err != nil {
367370
return 0, "", errors.Wrapf(err, "error getting network hash for Ledger %v", sequence)
368371
}

0 commit comments

Comments
 (0)