-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathledger_close_meta_view.go
More file actions
117 lines (111 loc) · 3.4 KB
/
Copy pathledger_close_meta_view.go
File metadata and controls
117 lines (111 loc) · 3.4 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package xdr
// Convenience helpers on LedgerCloseMetaView that decode just the header
// fields commonly needed for streaming validation, without the full XDR
// decode of the body.
//
// Byte-sequence accessors (e.g., LedgerHash) return slices into the source
// bytes — zero-copy, but the slice pins the source bytes alive. Callers
// that need to hold the value past the source's lifetime should copy it
// into a fixed-size type themselves.
// LedgerHeader returns a view of this ledger's LedgerHeaderHistoryEntry,
// resolving the LedgerCloseMeta version (V0/V1/V2) internally. It lets
// callers read header fields without decoding the rest of the meta.
func (v LedgerCloseMetaView) LedgerHeader() (LedgerHeaderHistoryEntryView, error) {
value, err := v.V()
if err != nil {
return nil, err
}
switch value {
case 0:
v0, err := v.V0()
if err != nil {
return nil, err
}
return v0.LedgerHeader()
case 1:
v1, err := v.V1()
if err != nil {
return nil, err
}
return v1.LedgerHeader()
case 2:
v2, err := v.V2()
if err != nil {
return nil, err
}
return v2.LedgerHeader()
default:
return nil, viewErrUnknownDiscriminant(0, value)
}
}
// LedgerSequence returns the sequence number of this LedgerCloseMeta.
func (v LedgerCloseMetaView) LedgerSequence() (uint32, error) {
header, err := v.LedgerHeader()
if err != nil {
return 0, err
}
headerInner, err := header.Header()
if err != nil {
return 0, err
}
seqView, err := headerInner.LedgerSeq()
if err != nil {
return 0, err
}
seq, err := seqView.Value()
if err != nil {
return 0, err
}
return uint32(seq), nil
}
// LedgerCloseTime returns the close time (Unix seconds) of this
// LedgerCloseMeta, mirroring LedgerCloseMeta.LedgerCloseTime on the parsed
// type.
func (v LedgerCloseMetaView) LedgerCloseTime() (int64, error) {
header, err := v.LedgerHeader()
if err != nil {
return 0, err
}
// The Must* accessors panic with *ViewError on the first malformed field
// and Try recovers it, so the chain needs only one error check.
ct, err := Try(func() uint64 {
return header.MustHeader().MustScpValue().MustCloseTime().MustValue()
})
return int64(ct), err //nolint:gosec // TimePoint is uint64; real close times fit int64
}
// LedgerHash returns the 32-byte hash of the closed ledger as a slice into
// the source bytes. Zero copy; the slice is valid as long as the source
// LedgerCloseMetaView's bytes are.
func (v LedgerCloseMetaView) LedgerHash() ([]byte, error) {
header, err := v.LedgerHeader()
if err != nil {
return nil, err
}
hashView, err := header.Hash()
if err != nil {
return nil, err
}
// Raw() returns the zero-copy []byte alias of the source; the fixed-opaque
// Value() would return a [32]byte that escapes to the heap as a copy.
return hashView.Raw()
}
// PreviousLedgerHash returns the 32-byte hash of the parent ledger as a
// slice into the source bytes. Zero copy; the slice is valid as long as
// the source LedgerCloseMetaView's bytes are.
func (v LedgerCloseMetaView) PreviousLedgerHash() ([]byte, error) {
header, err := v.LedgerHeader()
if err != nil {
return nil, err
}
headerInner, err := header.Header()
if err != nil {
return nil, err
}
hashView, err := headerInner.PreviousLedgerHash()
if err != nil {
return nil, err
}
// Raw() returns the zero-copy []byte alias of the source; the fixed-opaque
// Value() would return a [32]byte that escapes to the heap as a copy.
return hashView.Raw()
}