/
ledger_hash_store.go
62 lines (50 loc) · 1.89 KB
/
ledger_hash_store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package ledgerbackend
import (
"context"
sq "github.com/Masterminds/squirrel"
"github.com/stretchr/testify/mock"
"github.com/sanjayhashcash/go/support/db"
)
// TrustedLedgerHashStore is used to query ledger data from a trusted source.
// The store should contain ledgers verified by Hcnet-Core, do not use untrusted
// source like history archives.
type TrustedLedgerHashStore interface {
// GetLedgerHash returns the ledger hash for the given sequence number
GetLedgerHash(ctx context.Context, seq uint32) (string, bool, error)
Close() error
}
// AuroraDBLedgerHashStore is a TrustedLedgerHashStore which uses aurora's db to look up ledger hashes
type AuroraDBLedgerHashStore struct {
session db.SessionInterface
}
// NewAuroraDBLedgerHashStore constructs a new TrustedLedgerHashStore backed by the aurora db
func NewAuroraDBLedgerHashStore(session db.SessionInterface) TrustedLedgerHashStore {
return AuroraDBLedgerHashStore{session: session}
}
// GetLedgerHash returns the ledger hash for the given sequence number
func (h AuroraDBLedgerHashStore) GetLedgerHash(ctx context.Context, 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(ctx, &hash, sql)
if h.session.NoRows(err) {
return hash, false, nil
}
return hash, true, err
}
func (h AuroraDBLedgerHashStore) Close() error {
return h.session.Close()
}
// 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(ctx context.Context, seq uint32) (string, bool, error) {
args := m.Called(ctx, seq)
return args.Get(0).(string), args.Get(1).(bool), args.Error(2)
}
func (m *MockLedgerHashStore) Close() error {
args := m.Called()
return args.Error(0)
}