-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement db backed ledger store for the captive core backend
- Loading branch information
Showing
10 changed files
with
243 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ledgerbackend | ||
|
||
import ( | ||
sq "github.com/Masterminds/squirrel" | ||
"github.com/stellar/go/support/db" | ||
) | ||
|
||
// Ledger contains information about a ledger (sequence number and hash) | ||
type Ledger struct { | ||
Sequence uint32 `db:"sequence"` | ||
Hash string `db:"ledger_hash"` | ||
} | ||
|
||
// LedgerStore is used to query ledger data from the Horizon DB | ||
type LedgerStore interface { | ||
// LastLedger returns the highest ledger which is less than `seq` if there exists such a ledger | ||
LastLedger(seq uint32) (Ledger, bool, error) | ||
} | ||
|
||
// EmptyLedgerStore is a ledger store which is empty | ||
type EmptyLedgerStore struct{} | ||
|
||
// LastLedger always returns false indicating there is no ledger | ||
func (e EmptyLedgerStore) LastLedger(seq uint32) (Ledger, bool, error) { | ||
return Ledger{}, false, nil | ||
} | ||
|
||
// DBLedgerStore is a ledger store backed by the Horizon database | ||
type DBLedgerStore struct { | ||
session *db.Session | ||
} | ||
|
||
// NewDBLedgerStore constructs a new DBLedgerStore | ||
func NewDBLedgerStore(session *db.Session) LedgerStore { | ||
return DBLedgerStore{session: session} | ||
} | ||
|
||
// LastLedger returns the highest ledger which is less than `seq` if there exists such a ledger | ||
func (l DBLedgerStore) LastLedger(seq uint32) (Ledger, bool, error) { | ||
sql := sq.Select( | ||
"hl.sequence", | ||
"hl.ledger_hash", | ||
).From("history_ledgers hl"). | ||
Limit(1). | ||
Where("sequence < ?", seq). | ||
OrderBy("sequence desc") | ||
|
||
var dest Ledger | ||
err := l.session.Get(&dest, sql) | ||
if l.session.NoRows(err) { | ||
return dest, false, nil | ||
} | ||
return dest, true, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ledgerbackend | ||
|
||
import "github.com/stretchr/testify/mock" | ||
|
||
var _ LedgerStore = (*MockLedgerStore)(nil) | ||
|
||
type MockLedgerStore struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockLedgerStore) LastLedger(seq uint32) (Ledger, bool, error) { | ||
args := m.Called(seq) | ||
return args.Get(0).(Ledger), args.Get(1).(bool), args.Error(2) | ||
} |
Oops, something went wrong.