/
ledger_meta_archive.go
65 lines (55 loc) · 2.06 KB
/
ledger_meta_archive.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
63
64
65
package ledgerexporter
import (
"fmt"
"github.com/stellar/go/xdr"
)
// LedgerMetaArchive represents a file with metadata and binary data.
type LedgerMetaArchive struct {
// file name
objectKey string
// Actual binary data
data xdr.LedgerCloseMetaBatch
}
// NewLedgerMetaArchive creates a new LedgerMetaArchive instance.
func NewLedgerMetaArchive(key string, startSeq uint32, endSeq uint32) *LedgerMetaArchive {
return &LedgerMetaArchive{
objectKey: key,
data: xdr.LedgerCloseMetaBatch{
StartSequence: xdr.Uint32(startSeq),
EndSequence: xdr.Uint32(endSeq),
},
}
}
// AddLedger adds a LedgerCloseMeta to the archive.
func (f *LedgerMetaArchive) AddLedger(ledgerCloseMeta xdr.LedgerCloseMeta) error {
if ledgerCloseMeta.LedgerSequence() < uint32(f.data.StartSequence) ||
ledgerCloseMeta.LedgerSequence() > uint32(f.data.EndSequence) {
return fmt.Errorf("ledger sequence %d is outside valid range [%d, %d]",
ledgerCloseMeta.LedgerSequence(), f.data.StartSequence, f.data.EndSequence)
}
if len(f.data.LedgerCloseMetas) > 0 {
lastSequence := f.data.LedgerCloseMetas[len(f.data.LedgerCloseMetas)-1].LedgerSequence()
if ledgerCloseMeta.LedgerSequence() != lastSequence+1 {
return fmt.Errorf("ledgers must be added sequentially: expected sequence %d, got %d",
lastSequence+1, ledgerCloseMeta.LedgerSequence())
}
}
f.data.LedgerCloseMetas = append(f.data.LedgerCloseMetas, ledgerCloseMeta)
return nil
}
// GetLedgerCount returns the number of ledgers currently in the archive.
func (f *LedgerMetaArchive) GetLedgerCount() uint32 {
return uint32(len(f.data.LedgerCloseMetas))
}
// GetStartLedgerSequence returns the starting ledger sequence of the archive.
func (f *LedgerMetaArchive) GetStartLedgerSequence() uint32 {
return uint32(f.data.StartSequence)
}
// GetEndLedgerSequence returns the ending ledger sequence of the archive.
func (f *LedgerMetaArchive) GetEndLedgerSequence() uint32 {
return uint32(f.data.EndSequence)
}
// GetObjectKey returns the object key of the archive.
func (f *LedgerMetaArchive) GetObjectKey() string {
return f.objectKey
}