@@ -95,7 +95,8 @@ type CaptiveStellarCore struct {
95
95
stellarCoreLock sync.RWMutex
96
96
97
97
// For testing
98
- stellarCoreRunnerFactory func () stellarCoreRunnerInterface
98
+ stellarCoreRunnerFactory func () stellarCoreRunnerInterface
99
+ trustedHashBackendFactory func (config CaptiveCoreConfig ) (LedgerBackend , error )
99
100
100
101
// cachedMeta keeps that ledger data of the last fetched ledger. Updated in GetLedger().
101
102
cachedMeta * xdr.LedgerCloseMeta
@@ -163,7 +164,7 @@ type CaptiveCoreConfig struct {
163
164
}
164
165
165
166
// NewCaptive returns a new CaptiveStellarCore instance.
166
- func NewCaptive (config CaptiveCoreConfig ) (* CaptiveStellarCore , error ) {
167
+ func NewCaptive (config CaptiveCoreConfig ) (LedgerBackend , error ) {
167
168
// Here we set defaults in the config. Because config is not a pointer this code should
168
169
// not mutate the original CaptiveCoreConfig instance which was passed into NewCaptive()
169
170
@@ -233,6 +234,8 @@ func NewCaptive(config CaptiveCoreConfig) (*CaptiveStellarCore, error) {
233
234
return newStellarCoreRunner (config )
234
235
}
235
236
237
+ c .trustedHashBackendFactory = NewCaptive
238
+
236
239
if config .Toml != nil && config .Toml .HTTPPort != 0 {
237
240
c .stellarCoreClient = & stellarcore.Client {
238
241
HTTP : & http.Client {
@@ -338,8 +341,8 @@ func (c *CaptiveStellarCore) getTrustedHashForLedger(sequence uint32) (uint32, s
338
341
// first, direct from network which is considered trusted, after which the captive core instance is closed.
339
342
captiveConfigTrustedHash := c .config
340
343
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 )
343
346
344
347
if sequence <= 2 {
345
348
// 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
353
356
354
357
defer func () {
355
358
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 )
357
360
}
358
361
}()
359
362
360
- err = captiveTrustedHash .PrepareRange (captiveTrustedHash . config .Context , UnboundedRange (sequence ))
363
+ err = captiveTrustedHash .PrepareRange (captiveConfigTrustedHash .Context , UnboundedRange (sequence ))
361
364
if err != nil {
362
365
return 0 , "" , errors .Wrapf (err , "error preparing to get network hash for Ledger %v" , sequence )
363
366
}
364
367
365
- networkLCM , err := captiveTrustedHash .GetLedger (captiveTrustedHash . config .Context , sequence )
368
+ networkLCM , err := captiveTrustedHash .GetLedger (captiveConfigTrustedHash .Context , sequence )
366
369
if err != nil {
367
370
return 0 , "" , errors .Wrapf (err , "error getting network hash for Ledger %v" , sequence )
368
371
}
0 commit comments