-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ingest/ledgerbackend: Implement db backed ledger store for the captiv…
…e core backend (#3203) The history archives cannot be fully trusted because there's always the possibility that someone can compromise the history archives either by compromising the S3 bucket which stores the archives or compromising one of the layers above the s3 bucket. When we provide a corrupt ledger hash to stellar core as --start-at-hash parameter, stellar core will download the ledger chain and validate all the hashes from consensus back to the start hash. If the start hash is not valid, stellar-core will exit with an error before streaming any ledgers. This means that ingestion will be blocked until the history archives are fixed. Since stellar core will always verify the ledger chain before emitting any ledgers, we can assume that any ledger hashes ingested into the horizon database are correct. So, in the scenario where the history archives are corrupt, we can avoid blocking ingestion by using ledger hashes found in the Horizon database.
- Loading branch information
Showing
9 changed files
with
218 additions
and
26 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,50 @@ | ||
package ledgerbackend | ||
|
||
import ( | ||
sq "github.com/Masterminds/squirrel" | ||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/stellar/go/support/db" | ||
) | ||
|
||
// TrustedLedgerHashStore is used to query ledger data from a trusted source. | ||
// The store should contain ledgers verified by Stellar-Core, do not use untrusted | ||
// source like history archives. | ||
type TrustedLedgerHashStore interface { | ||
// GetLedgerHash returns the ledger hash for the given sequence number | ||
GetLedgerHash(seq uint32) (string, bool, error) | ||
} | ||
|
||
// HorizonDBLedgerHashStore is a TrustedLedgerHashStore which uses horizon's db to look up ledger hashes | ||
type HorizonDBLedgerHashStore struct { | ||
session *db.Session | ||
} | ||
|
||
// NewHorizonDBLedgerHashStore constructs a new TrustedLedgerHashStore backed by the horizon db | ||
func NewHorizonDBLedgerHashStore(session *db.Session) TrustedLedgerHashStore { | ||
return HorizonDBLedgerHashStore{session: session} | ||
} | ||
|
||
// GetLedgerHash returns the ledger hash for the given sequence number | ||
func (h HorizonDBLedgerHashStore) GetLedgerHash(seq uint32) (string, bool, error) { | ||
sql := sq.Select("hl.ledger_hash").From("history_ledgers hl"). | ||
Limit(1).Where("sequence = ?", seq) | ||
|
||
var hash string | ||
err := h.session.Get(&hash, sql) | ||
if h.session.NoRows(err) { | ||
return hash, false, nil | ||
} | ||
return hash, true, err | ||
} | ||
|
||
// MockLedgerHashStore is a mock implementation of TrustedLedgerHashStore | ||
type MockLedgerHashStore struct { | ||
mock.Mock | ||
} | ||
|
||
// GetLedgerHash returns the ledger hash for the given sequence number | ||
func (m *MockLedgerHashStore) GetLedgerHash(seq uint32) (string, bool, error) { | ||
args := m.Called(seq) | ||
return args.Get(0).(string), args.Get(1).(bool), args.Error(2) | ||
} |
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