Skip to content

Commit

Permalink
#4538: use LedgerBackend interface for NewCaptive, fix unit tests tha…
Browse files Browse the repository at this point in the history
…t use catchup
  • Loading branch information
sreuland committed Aug 20, 2024
1 parent 3b706e4 commit 2937c18
Show file tree
Hide file tree
Showing 10 changed files with 364 additions and 71 deletions.
10 changes: 5 additions & 5 deletions ingest/ledger_change_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (

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

func TestNewLedgerChangeReaderSucceeds(t *testing.T) {
ctx := context.Background()
mock := &ledgerbackend.MockDatabaseBackend{}
mock := &ledgerbackend.MockLedgerBackend{}
seq := uint32(123)

header := xdr.LedgerHeaderHistoryEntry{
Expand Down Expand Up @@ -146,7 +146,7 @@ func assertChangesEqual(

func TestLedgerChangeReaderOrder(t *testing.T) {
ctx := context.Background()
mock := &ledgerbackend.MockDatabaseBackend{}
mock := &ledgerbackend.MockLedgerBackend{}
seq := uint32(123)

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

func TestLedgerChangeLedgerCloseMetaV2(t *testing.T) {
ctx := context.Background()
mock := &ledgerbackend.MockDatabaseBackend{}
mock := &ledgerbackend.MockLedgerBackend{}
seq := uint32(123)

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

func TestLedgerChangeLedgerCloseMetaV2Empty(t *testing.T) {
ctx := context.Background()
mock := &ledgerbackend.MockDatabaseBackend{}
mock := &ledgerbackend.MockLedgerBackend{}
seq := uint32(123)

baseFee := xdr.Int64(100)
Expand Down
17 changes: 10 additions & 7 deletions ingest/ledgerbackend/captive_core_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ type CaptiveStellarCore struct {
stellarCoreLock sync.RWMutex

// For testing
stellarCoreRunnerFactory func() stellarCoreRunnerInterface
stellarCoreRunnerFactory func() stellarCoreRunnerInterface
trustedHashBackendFactory func(config CaptiveCoreConfig) (LedgerBackend, error)

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

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

Expand Down Expand Up @@ -233,6 +234,8 @@ func NewCaptive(config CaptiveCoreConfig) (*CaptiveStellarCore, error) {
return newStellarCoreRunner(config)
}

c.trustedHashBackendFactory = NewCaptive

if config.Toml != nil && config.Toml.HTTPPort != 0 {
c.stellarCoreClient = &stellarcore.Client{
HTTP: &http.Client{
Expand Down Expand Up @@ -338,8 +341,8 @@ func (c *CaptiveStellarCore) getTrustedHashForLedger(sequence uint32) (uint32, s
// first, direct from network which is considered trusted, after which the captive core instance is closed.
captiveConfigTrustedHash := c.config
captiveConfigTrustedHash.LedgerHashStore = nil
captiveConfigTrustedHash.Context = context.Background()
captiveTrustedHash, err := NewCaptive(captiveConfigTrustedHash)
captiveConfigTrustedHash.Context, _ = context.WithCancel(c.config.Context)

Check failure on line 344 in ingest/ledgerbackend/captive_core_backend.go

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.22.1)

the cancel function returned by context.WithCancel should be called, not discarded, to avoid a context leak
captiveTrustedHash, err := c.trustedHashBackendFactory(captiveConfigTrustedHash)

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

defer func() {
if closeErr := captiveTrustedHash.Close(); closeErr != nil {
captiveTrustedHash.config.Log.Error("error when closing captive core for network hash", closeErr)
captiveConfigTrustedHash.Log.Error("error when closing captive core for network hash", closeErr)
}
}()

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

networkLCM, err := captiveTrustedHash.GetLedger(captiveTrustedHash.config.Context, sequence)
networkLCM, err := captiveTrustedHash.GetLedger(captiveConfigTrustedHash.Context, sequence)
if err != nil {
return 0, "", errors.Wrapf(err, "error getting network hash for Ledger %v", sequence)
}
Expand Down
Loading

0 comments on commit 2937c18

Please sign in to comment.