-
Notifications
You must be signed in to change notification settings - Fork 546
Expand file tree
/
Copy pathrpc_backend.go
More file actions
286 lines (239 loc) · 8.42 KB
/
rpc_backend.go
File metadata and controls
286 lines (239 loc) · 8.42 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package ledgerbackend
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/stellar/go/xdr"
rpc "github.com/stellar/stellar-rpc/client"
"github.com/stellar/stellar-rpc/protocol"
)
const rpcBackendDefaultBufferSize uint32 = 10
const rpcBackendDefaultWaitIntervalSeconds uint32 = 2
type RPCLedgerMissingError struct {
Sequence uint32
}
func (e *RPCLedgerMissingError) Error() string {
return fmt.Sprintf("ledger %d was not present on rpc", e.Sequence)
}
type rpcLedgerBeyondLatestError struct{}
func (e rpcLedgerBeyondLatestError) Error() string {
return "ledger is not available on the RPC server yet"
}
// The minimum required RPC client methods used by RPCLedgerBackend.
type RPCLedgerGetter interface {
GetLedgers(ctx context.Context, req protocol.GetLedgersRequest) (protocol.GetLedgersResponse, error)
GetHealth(ctx context.Context) (protocol.GetHealthResponse, error) // <-- Added
}
type RPCLedgerBackend struct {
client RPCLedgerGetter
buffer map[uint32]xdr.LedgerCloseMeta
bufferSize uint32
preparedRange *Range
nextLedger uint32
latestBufferLedger atomic.Uint32
closed chan struct{}
closedOnce sync.Once
bufferLock sync.RWMutex
}
type RPCLedgerBackendOptions struct {
// Required, URL of the Stellar RPC server
RPCServerURL string
// Optional, size of the ledger retrieval buffer to use with RPC server requests.
// if not set, defaults to 10
BufferSize uint32
// Optional, custom HTTP client to use for RPC requests.
// If nil, the default http.Client will be used.
HttpClient *http.Client
}
// NewRPCLedgerBackend creates a new RPCLedgerBackend instance that fetches ledger data
// from a Stellar RPC server.
//
// Parameters:
// - options: RPCLedgerBackendOptions
//
// Returns:
// - *RPCLedgerBackend: A new backend instance ready for use
func NewRPCLedgerBackend(options RPCLedgerBackendOptions) *RPCLedgerBackend {
backend := &RPCLedgerBackend{
closed: make(chan struct{}),
client: rpc.NewClient(options.RPCServerURL, options.HttpClient),
bufferSize: options.BufferSize,
}
if backend.bufferSize == 0 {
backend.bufferSize = rpcBackendDefaultBufferSize
}
backend.initBuffer()
return backend
}
// GetLatestLedgerSequence returns the latest ledger sequence currently loaded by internal buffer.
func (b *RPCLedgerBackend) GetLatestLedgerSequence(ctx context.Context) (sequence uint32, err error) {
if err := b.checkClosed(); err != nil {
return 0, err
}
if b.preparedRange == nil {
return 0, fmt.Errorf("RPCLedgerBackend must be prepared before calling GetLatestLedgerSequence")
}
return b.latestBufferLedger.Load(), nil
}
// GetLedger queries the RPC server for a specific ledger sequence and returns the meta data.
// If the requested ledger is not immediately available but is beyond the latest ledger in the RPC server,
// it will block and retry until either:
// - The ledger becomes available
// - The context is cancelled or times out
// - The backend is closed
// - An error occurs while fetching the ledger
//
// The caller can control the maximum wait time by setting a timeout or deadline on the provided context.
// Or by invoking the Close method from another goroutine.
//
// Parameters:
// - ctx: Context for cancellation and timeout control
// - sequence: The ledger sequence number to retrieve meta data for
//
// Returns:
// - xdr.LedgerCloseMeta: The ledger meta data if found
// - error: if ledger sequence is not avaialble from the RPC server,
// or context is cancelled or times out.
func (b *RPCLedgerBackend) GetLedger(ctx context.Context, sequence uint32) (xdr.LedgerCloseMeta, error) {
b.bufferLock.Lock()
defer b.bufferLock.Unlock()
if err := b.checkClosed(); err != nil {
return xdr.LedgerCloseMeta{}, err
}
if b.preparedRange == nil {
return xdr.LedgerCloseMeta{}, fmt.Errorf("RPCLedgerBackend must be prepared before calling GetLedger")
}
if sequence < b.preparedRange.from || (b.preparedRange.bounded && sequence > b.preparedRange.to) {
return xdr.LedgerCloseMeta{}, fmt.Errorf("requested ledger %d is outside prepared range [%d, %d]",
sequence, b.preparedRange.from, b.preparedRange.to)
}
if sequence != b.nextLedger {
return xdr.LedgerCloseMeta{}, fmt.Errorf("requested ledger %d is not the expected ledger %d", sequence, b.nextLedger)
}
for {
lcm, err := b.getBufferedLedger(ctx, sequence)
if err == nil {
b.nextLedger = sequence + 1
return lcm, nil
}
var beyondErr *rpcLedgerBeyondLatestError
if !(errors.As(err, &beyondErr)) {
return xdr.LedgerCloseMeta{}, err
}
select {
case <-b.closed:
return xdr.LedgerCloseMeta{}, fmt.Errorf("RPCLedgerBackend is closed: %w", err)
case <-ctx.Done():
return xdr.LedgerCloseMeta{}, ctx.Err()
case <-time.After(time.Duration(rpcBackendDefaultWaitIntervalSeconds) * time.Second):
continue
}
}
}
// PrepareRange initiates retrieval of requested ledger range.
// It does minimal validation of data on RPC up front.
// It wil check if starting point of range is withing current historical retention window of the RPC server.
// It cannot gaurantee ledgers within historical ranges will be available when requested later by GetLedger.
// See Also: GetLedger for more details on how the RPCLedgerBackend handles ledger availability.
func (b *RPCLedgerBackend) PrepareRange(ctx context.Context, ledgerRange Range) error {
b.bufferLock.Lock()
defer b.bufferLock.Unlock()
if err := b.checkClosed(); err != nil {
return err
}
if b.preparedRange != nil {
return fmt.Errorf("RPCLedgerBackend is already prepared with range [%d, %d]", b.preparedRange.from, b.preparedRange.to)
}
_, err := b.getBufferedLedger(ctx, ledgerRange.from)
if err != nil {
// beyond latest is handled later in GetLedger
var beyondErr *rpcLedgerBeyondLatestError
if !(errors.As(err, &beyondErr)) {
return err
}
}
b.nextLedger = ledgerRange.from
b.preparedRange = &ledgerRange
return nil
}
func (b *RPCLedgerBackend) IsPrepared(ctx context.Context, ledgerRange Range) (bool, error) {
b.bufferLock.RLock()
defer b.bufferLock.RUnlock()
if err := b.checkClosed(); err != nil {
return false, err
}
if b.preparedRange == nil {
return false, nil
}
rangesMatch := b.preparedRange.from == ledgerRange.from &&
b.preparedRange.bounded == ledgerRange.bounded &&
(!b.preparedRange.bounded || b.preparedRange.to == ledgerRange.to)
return rangesMatch, nil
}
// Close cleans up the RPCLedgerBackend resources and closes the backend.
// It will halt any ongoing GerLedger calls that may be in progress on other goroutines.
func (b *RPCLedgerBackend) Close() error {
b.closedOnce.Do(func() {
close(b.closed)
})
return nil
}
func (b *RPCLedgerBackend) checkClosed() error {
select {
case <-b.closed:
return fmt.Errorf("RPCLedgerBackend is closed")
default:
return nil
}
}
func (b *RPCLedgerBackend) initBuffer() {
b.buffer = make(map[uint32]xdr.LedgerCloseMeta)
}
func (b *RPCLedgerBackend) getBufferedLedger(ctx context.Context, sequence uint32) (xdr.LedgerCloseMeta, error) {
// Check if ledger is in buffer
if lcm, exists := b.buffer[sequence]; exists {
return lcm, nil
}
// Check if requested ledger is beyond the RPC retention window using GetHealth
health, err := b.client.GetHealth(ctx)
if err != nil {
return xdr.LedgerCloseMeta{}, fmt.Errorf("failed to get health from RPC: %w", err)
}
if sequence > health.LatestLedger {
return xdr.LedgerCloseMeta{}, &rpcLedgerBeyondLatestError{}
}
// attempt to fetch a small batch from RPC starting from the requested sequence
req := protocol.GetLedgersRequest{
StartLedger: sequence,
Pagination: &protocol.LedgerPaginationOptions{
Limit: uint(b.bufferSize),
},
}
ledgers, err := b.client.GetLedgers(ctx, req)
if err != nil {
return xdr.LedgerCloseMeta{}, err
}
b.initBuffer()
// Populate buffer with new ledgers
for _, ledger := range ledgers.Ledgers {
var lcm xdr.LedgerCloseMeta
if err := xdr.SafeUnmarshalBase64(ledger.LedgerMetadata, &lcm); err != nil {
return xdr.LedgerCloseMeta{}, fmt.Errorf("failed to unmarshal ledger %d: %w", ledger.Sequence, err)
}
b.buffer[ledger.Sequence] = lcm
}
latestSeq := uint32(0)
if size := len(ledgers.Ledgers); size > 0 {
latestSeq = ledgers.Ledgers[size-1].Sequence
}
b.latestBufferLedger.Store(latestSeq)
// Check if requested ledger is in new buffer
if lcm, exists := b.buffer[sequence]; exists {
return lcm, nil
}
return xdr.LedgerCloseMeta{}, &RPCLedgerMissingError{Sequence: sequence}
}