-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathchainsync.go
More file actions
599 lines (566 loc) · 16 KB
/
chainsync.go
File metadata and controls
599 lines (566 loc) · 16 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright 2025 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chainsync
import (
"context"
"encoding/hex"
"errors"
"fmt"
"log/slog"
"math"
"net/http"
"slices"
"strings"
"time"
"github.com/SundaeSwap-finance/kugo"
"github.com/SundaeSwap-finance/ogmigo/v6/ouroboros/chainsync"
"github.com/blinklabs-io/adder/event"
"github.com/blinklabs-io/adder/internal/logging"
"github.com/blinklabs-io/adder/plugin"
ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/gouroboros/ledger"
"github.com/blinklabs-io/gouroboros/protocol/blockfetch"
ochainsync "github.com/blinklabs-io/gouroboros/protocol/chainsync"
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
)
const (
// Size of cache for recent chainsync cursors
cursorCacheSize = 20
maxAutoReconnectDelay = 60 * time.Second
)
type ChainSync struct {
oConn *ouroboros.Connection
logger plugin.Logger
network string
networkMagic uint32
address string
socketPath string
ntcTcp bool
bulkMode bool
intersectTip bool
intersectPoints []ocommon.Point
includeCbor bool
autoReconnect bool
autoReconnectDelay time.Duration
statusUpdateFunc StatusUpdateFunc
status *ChainSyncStatus
errorChan chan error
eventChan chan event.Event
bulkRangeStart ocommon.Point
bulkRangeEnd ocommon.Point
cursorCache []ocommon.Point
dialAddress string
dialFamily string
kupoUrl string
kupoClient *kugo.Client
delayConfirmations uint
delayBuffer [][]event.Event
}
type ChainSyncStatus struct {
SlotNumber uint64
BlockNumber uint64
BlockHash string
TipSlotNumber uint64
TipBlockHash string
TipReached bool
}
type StatusUpdateFunc func(ChainSyncStatus)
// New returns a new ChainSync object with the specified options applied
func New(options ...ChainSyncOptionFunc) *ChainSync {
c := &ChainSync{
errorChan: make(chan error),
eventChan: make(chan event.Event, 10),
intersectPoints: []ocommon.Point{},
status: &ChainSyncStatus{},
}
for _, option := range options {
option(c)
}
return c
}
// Start the chain sync input
func (c *ChainSync) Start() error {
if err := c.setupConnection(); err != nil {
return err
}
// Start chainsync client
c.oConn.ChainSync().Client.Start()
if c.oConn.BlockFetch() != nil {
c.oConn.BlockFetch().Client.Start()
}
if c.bulkMode && !c.intersectTip && c.oConn.BlockFetch() != nil {
// Get available block range between our intersect point(s) and the chain tip
var err error
c.bulkRangeStart, c.bulkRangeEnd, err = c.oConn.ChainSync().Client.GetAvailableBlockRange(
c.intersectPoints,
)
if err != nil {
return err
}
if c.bulkRangeStart.Slot == 0 || c.bulkRangeEnd.Slot == 0 {
// We're already at chain tip, so start a normal sync
if err := c.oConn.ChainSync().Client.Sync(c.intersectPoints); err != nil {
return err
}
} else {
// Use BlockFetch to request the entire available block range at once
if err := c.oConn.BlockFetch().Client.GetBlockRange(c.bulkRangeStart, c.bulkRangeEnd); err != nil {
return err
}
}
} else {
if c.intersectTip {
tip, err := c.oConn.ChainSync().Client.GetCurrentTip()
if err != nil {
return err
}
c.intersectPoints = []ocommon.Point{tip.Point}
}
if err := c.oConn.ChainSync().Client.Sync(c.intersectPoints); err != nil {
return err
}
}
return nil
}
// Stop the chain sync input
func (c *ChainSync) Stop() error {
err := c.oConn.Close()
close(c.eventChan)
close(c.errorChan)
return err
}
// ErrorChan returns the input error channel
func (c *ChainSync) ErrorChan() chan error {
return c.errorChan
}
// InputChan always returns nil
func (c *ChainSync) InputChan() chan<- event.Event {
return nil
}
// OutputChan returns the output event channel
func (c *ChainSync) OutputChan() <-chan event.Event {
return c.eventChan
}
func (c *ChainSync) setupConnection() error {
// Determine connection parameters
var useNtn bool
// Lookup network by name, if provided
if c.network != "" {
network, ok := ouroboros.NetworkByName(c.network)
if !ok {
return fmt.Errorf("unknown network: %s", c.network)
}
c.networkMagic = network.NetworkMagic
// If network has well-known public root address/port, use those as our dial default
if len(network.BootstrapPeers) > 0 {
peer := network.BootstrapPeers[0]
c.dialFamily = "tcp"
c.dialAddress = fmt.Sprintf(
"%s:%d",
peer.Address,
peer.Port,
)
useNtn = true
}
}
// Use user-provided address or socket path, if provided
if c.address != "" {
c.dialFamily = "tcp"
c.dialAddress = c.address
if c.ntcTcp {
useNtn = false
} else {
useNtn = true
}
} else if c.socketPath != "" {
c.dialFamily = "unix"
c.dialAddress = c.socketPath
useNtn = false
} else if c.dialFamily == "" || c.dialAddress == "" {
return errors.New("you must specify a host/port, UNIX socket path, or well-known network name")
}
// Create connection
var err error
c.oConn, err = ouroboros.NewConnection(
ouroboros.WithNetworkMagic(c.networkMagic),
ouroboros.WithNodeToNode(useNtn),
ouroboros.WithKeepAlive(true),
ouroboros.WithChainSyncConfig(
ochainsync.NewConfig(
ochainsync.WithRollForwardFunc(c.handleRollForward),
ochainsync.WithRollBackwardFunc(c.handleRollBackward),
),
),
ouroboros.WithBlockFetchConfig(
blockfetch.NewConfig(
blockfetch.WithBlockFunc(c.handleBlockFetchBlock),
),
),
)
if err != nil {
return err
}
if err := c.oConn.Dial(c.dialFamily, c.dialAddress); err != nil {
return err
}
if c.logger != nil {
c.logger.Info("connected to node at " + c.dialAddress)
}
// Start async error handler
go func() {
err, ok := <-c.oConn.ErrorChan()
if ok {
if c.autoReconnect {
c.autoReconnectDelay = 0
if c.logger != nil {
c.logger.Info(fmt.Sprintf(
"reconnecting to %s due to error: %s",
c.dialAddress,
err,
))
}
for {
if c.autoReconnectDelay > 0 {
c.logger.Info(fmt.Sprintf(
"waiting %s to reconnect",
c.autoReconnectDelay,
))
time.Sleep(c.autoReconnectDelay)
// Double current reconnect delay up to maximum
c.autoReconnectDelay = min(
c.autoReconnectDelay*2,
maxAutoReconnectDelay,
)
} else {
// Set initial reconnect delay
c.autoReconnectDelay = 1 * time.Second
}
// Shutdown current connection
if err := c.oConn.Close(); err != nil {
if c.logger != nil {
c.logger.Warn(fmt.Sprintf(
"failed to properly close connection: %s",
err,
))
}
}
// Set the intersect points from the cursor cache
if len(c.cursorCache) > 0 {
c.intersectPoints = c.cursorCache[:]
}
// Restart the connection
if err := c.Start(); err != nil {
if c.logger != nil {
c.logger.Info(fmt.Sprintf(
"reconnecting to %s due to error: %s",
c.dialAddress,
err,
))
}
continue
}
break
}
} else {
// Pass error through our own error channel
c.errorChan <- err
}
}
}()
return nil
}
func (c *ChainSync) handleRollBackward(
ctx ochainsync.CallbackContext,
point ocommon.Point,
tip ochainsync.Tip,
) error {
evt := event.New(
"chainsync.rollback",
time.Now(),
nil,
NewRollbackEvent(point),
)
// Remove rolled-back events from buffer
if len(c.delayBuffer) > 0 {
// We iterate backwards to avoid the issues with deleting from a list while iterating over it
for i := len(c.delayBuffer) - 1; i >= 0; i-- {
for _, evt := range c.delayBuffer[i] {
// Look for block event
if blockEvtCtx, ok := evt.Context.(BlockContext); ok {
// Delete event batch if slot is after rollback point
if blockEvtCtx.SlotNumber > point.Slot {
c.delayBuffer = slices.Delete(c.delayBuffer, i, i+1)
break
}
}
}
}
}
c.eventChan <- evt
// updating status after roll backward
c.updateStatus(
point.Slot, // SlotNumber
0, // BlockNumber (unknown after rollback)
hex.EncodeToString(point.Hash), // BlockHash
tip.Point.Slot, // TipSlotNumber
hex.EncodeToString(tip.Point.Hash), // TipBlockHash
)
return nil
}
func (c *ChainSync) handleRollForward(
ctx ochainsync.CallbackContext,
blockType uint,
blockData any,
tip ochainsync.Tip,
) error {
var block ledger.Block
var err error
tmpEvents := make([]event.Event, 0, 20)
switch v := blockData.(type) {
case ledger.Block:
block = v
case ledger.BlockHeader:
blockSlot := v.SlotNumber()
block, err = c.oConn.BlockFetch().Client.GetBlock(ocommon.Point{Slot: blockSlot, Hash: v.Hash().Bytes()})
if err != nil {
return err
}
if block == nil {
return errors.New("blockfetch returned empty")
}
default:
return errors.New("unknown type")
}
blockEvt := event.New("chainsync.block", time.Now(), NewBlockHeaderContext(block.Header()), NewBlockEvent(block, c.includeCbor))
tmpEvents = append(tmpEvents, blockEvt)
for t, transaction := range block.Transactions() {
resolvedInputs, err := resolveTransactionInputs(transaction, c)
if err != nil {
return err
}
if t < 0 || t > math.MaxUint32 {
return errors.New("invalid number of transactions")
}
txEvt := event.New("chainsync.transaction", time.Now(), NewTransactionContext(block, transaction, uint32(t), c.networkMagic),
NewTransactionEvent(block, transaction, c.includeCbor, resolvedInputs))
tmpEvents = append(tmpEvents, txEvt)
}
updateTip := ochainsync.Tip{
Point: ocommon.Point{
Slot: block.SlotNumber(),
Hash: block.Hash().Bytes(),
},
BlockNumber: block.BlockNumber(),
}
if c.delayConfirmations == 0 {
// Send events immediately if no delay confirmations configured
for _, evt := range tmpEvents {
c.eventChan <- evt
}
} else {
// Add events to delay buffer
c.delayBuffer = append(c.delayBuffer, tmpEvents)
// Send oldest events and remove from buffer if delay buffer is larger than configured delay confirmations
if uint(len(c.delayBuffer)) > c.delayConfirmations {
for _, evt := range c.delayBuffer[0] {
// Look for block event
if blockEvt, ok := evt.Payload.(BlockEvent); ok {
// Populate current point for update status based on most recently sent events
updateTip = ochainsync.Tip{
Point: ocommon.Point{
Slot: blockEvt.Block.SlotNumber(),
Hash: blockEvt.Block.Hash().Bytes(),
},
BlockNumber: blockEvt.Block.BlockNumber(),
}
}
c.eventChan <- evt
}
c.delayBuffer = slices.Delete(c.delayBuffer, 0, 1)
}
}
c.updateStatus(updateTip.Point.Slot, updateTip.BlockNumber, hex.EncodeToString(updateTip.Point.Hash), tip.Point.Slot, hex.EncodeToString(tip.Point.Hash))
return nil
}
func (c *ChainSync) handleBlockFetchBlock(
ctx blockfetch.CallbackContext,
blockType uint,
block ledger.Block,
) error {
blockEvt := event.New(
"chainsync.block",
time.Now(),
NewBlockContext(block, c.networkMagic),
NewBlockEvent(block, c.includeCbor),
)
c.eventChan <- blockEvt
for t, transaction := range block.Transactions() {
resolvedInputs, err := resolveTransactionInputs(transaction, c)
if err != nil {
return err
}
if t < 0 || t > math.MaxUint32 {
return errors.New("invalid number of transactions")
}
txEvt := event.New(
"chainsync.transaction",
time.Now(),
NewTransactionContext(
block,
transaction,
uint32(t),
c.networkMagic,
),
NewTransactionEvent(
block,
transaction,
c.includeCbor,
resolvedInputs,
),
)
c.eventChan <- txEvt
}
c.updateStatus(
block.SlotNumber(),
block.BlockNumber(),
block.Hash().String(),
c.bulkRangeEnd.Slot,
hex.EncodeToString(c.bulkRangeEnd.Hash),
)
// Start normal chain-sync if we've reached the last block of our bulk range
if block.SlotNumber() == c.bulkRangeEnd.Slot {
if err := c.oConn.ChainSync().Client.Sync([]ocommon.Point{c.bulkRangeEnd}); err != nil {
return err
}
}
return nil
}
func (c *ChainSync) updateStatus(
slotNumber uint64,
blockNumber uint64,
blockHash string,
tipSlotNumber uint64,
tipBlockHash string,
) {
// Update cursor cache
blockHashBytes, _ := hex.DecodeString(blockHash)
c.cursorCache = append(
c.cursorCache,
ocommon.Point{Slot: slotNumber, Hash: blockHashBytes},
)
if len(c.cursorCache) > cursorCacheSize {
c.cursorCache = c.cursorCache[len(c.cursorCache)-cursorCacheSize:]
}
// Determine if we've reached the chain tip
if !c.status.TipReached {
// Make sure we're past the end slot in any bulk range, since we don't update the tip during bulk sync
if slotNumber > c.bulkRangeEnd.Slot {
// Make sure our current slot is equal/higher than our last known tip slot
if c.status.SlotNumber > 0 && slotNumber >= c.status.TipSlotNumber {
c.status.TipReached = true
}
}
}
c.status.SlotNumber = slotNumber
c.status.BlockNumber = blockNumber
c.status.BlockHash = blockHash
c.status.TipSlotNumber = tipSlotNumber
c.status.TipBlockHash = tipBlockHash
if c.statusUpdateFunc != nil {
c.statusUpdateFunc(*(c.status))
}
}
func getKupoClient(c *ChainSync) (*kugo.Client, error) {
if c.kupoClient != nil {
return c.kupoClient, nil
}
KugoCustomLogger := logging.NewKugoCustomLogger(logging.LevelInfo)
k := kugo.New(
kugo.WithEndpoint(c.kupoUrl),
kugo.WithLogger(KugoCustomLogger),
)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
healthUrl := strings.TrimRight(c.kupoUrl, "/") + "/health"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, healthUrl, nil)
if err != nil {
return nil, fmt.Errorf("failed to create health check request: %w", err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to perform health check: %w", err)
}
if resp == nil {
return nil, errors.New("health check response empty, aborting")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"health check failed with status code: %d",
resp.StatusCode,
)
}
c.kupoClient = k
return k, nil
}
// resolveTransactionInputs resolves the transaction inputs by using the
// Kupo client and fetching the corresponding transaction outputs.
func resolveTransactionInputs(
transaction ledger.Transaction,
c *ChainSync,
) ([]ledger.TransactionOutput, error) {
var resolvedInputs []ledger.TransactionOutput
// Use Kupo client to resolve inputs if available
if c.kupoUrl != "" {
k, err := getKupoClient(c)
if err != nil {
return nil, fmt.Errorf("failed to get Kupo client: %w", err)
}
for _, input := range transaction.Inputs() {
// Extract transaction ID and index from the input
txId := input.Id().String()
txIndex := int(input.Index())
matches, err := k.Matches(context.Background(),
kugo.TxOut(chainsync.NewTxID(txId, txIndex)),
)
if err != nil {
return nil, fmt.Errorf(
"error fetching matches for input TxId: %s, Index: %d. Error: %w",
txId,
txIndex,
err,
)
}
if len(matches) == 0 {
slog.Info(
"no matches found for input TxId: %s, Index: %d, could be due to Kupo not in sync",
txId,
txIndex,
)
} else {
slog.Debug(fmt.Sprintf("found matches %d for input TxId: %s, Index: %d", len(matches), txId, txIndex))
for _, match := range matches {
slog.Debug(fmt.Sprintf("Match: %#v", match))
transactionOutput, err := NewResolvedTransactionOutput(match)
if err != nil {
return nil, err
}
resolvedInputs = append(resolvedInputs, transactionOutput)
}
}
}
}
return resolvedInputs, nil
}