Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exp/ingest/ledgerbackend: Fix index out of range error in GetLatestLedgerSequence() #1914

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions exp/ingest/ledgerbackend/database_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func (dbb *DatabaseBackend) GetLatestLedgerSequence() (uint32, error) {
if err != nil {
return 0, errors.Wrap(err, "couldn't select ledger sequence")
}
if len(ledger) == 0 {
return 0, errors.New("No ledgers found in ledgerheaders table")
tamirms marked this conversation as resolved.
Show resolved Hide resolved
}

return ledger[0].LedgerSeq, nil
}
Expand Down
34 changes: 34 additions & 0 deletions services/horizon/internal/expingest/database_backend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package expingest

import (
"testing"

"github.com/stellar/go/exp/ingest/ledgerbackend"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/services/horizon/internal/test"
)

func TestGetLatestLedger(t *testing.T) {
tt := test.Start(t).ScenarioWithoutHorizon("base")
defer tt.Finish()

backend, err := ledgerbackend.NewDatabaseBackendFromSession(tt.CoreSession())
tt.Assert.NoError(err)
seq, err := backend.GetLatestLedgerSequence()
tt.Assert.NoError(err)
tt.Assert.Equal(uint32(3), seq)
}

func TestGetLatestLedgerNotFound(t *testing.T) {
tt := test.Start(t).ScenarioWithoutHorizon("base")
defer tt.Finish()
q := &core.Q{tt.CoreSession()}

_, err := tt.CoreDB.Exec(`DELETE FROM ledgerheaders`)
tt.Assert.NoError(err, "failed to remove ledgerheaders")

backend, err := ledgerbackend.NewDatabaseBackendFromSession(q.Session)
tt.Assert.NoError(err)
_, err = backend.GetLatestLedgerSequence()
tt.Assert.EqualError(err, "No ledgers found in ledgerheaders table")
}