@@ -29,6 +29,11 @@ import (
29
29
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
30
30
)
31
31
32
+ const (
33
+ // Size of cache for recent chainsync cursors
34
+ cursorCacheSize = 20
35
+ )
36
+
32
37
type ChainSync struct {
33
38
oConn * ouroboros.Connection
34
39
logger plugin.Logger
@@ -41,12 +46,14 @@ type ChainSync struct {
41
46
intersectTip bool
42
47
intersectPoints []ocommon.Point
43
48
includeCbor bool
49
+ autoReconnect bool
44
50
statusUpdateFunc StatusUpdateFunc
45
51
status * ChainSyncStatus
46
52
errorChan chan error
47
53
eventChan chan event.Event
48
54
bulkRangeStart ocommon.Point
49
55
bulkRangeEnd ocommon.Point
56
+ cursorCache []ocommon.Point
50
57
}
51
58
52
59
type ChainSyncStatus struct {
@@ -79,6 +86,7 @@ func (c *ChainSync) Start() error {
79
86
if err := c .setupConnection (); err != nil {
80
87
return err
81
88
}
89
+ // Start chainsync client
82
90
c .oConn .ChainSync ().Client .Start ()
83
91
if c .oConn .BlockFetch () != nil {
84
92
c .oConn .BlockFetch ().Client .Start ()
@@ -201,11 +209,27 @@ func (c *ChainSync) setupConnection() error {
201
209
go func () {
202
210
err , ok := <- c .oConn .ErrorChan ()
203
211
if ok {
204
- // Pass error through our own error channel
205
- c .errorChan <- err
206
- return
212
+ if c .autoReconnect {
213
+ if c .logger != nil {
214
+ c .logger .Infof ("reconnecting to %s due to error: %s" , dialAddress , err )
215
+ }
216
+ // Shutdown current connection
217
+ if err := c .oConn .Close (); err != nil {
218
+ c .errorChan <- err
219
+ return
220
+ }
221
+ // Set the intersect points from the cursor cache
222
+ c .intersectPoints = c .cursorCache [:]
223
+ // Restart the connection
224
+ if err := c .Start (); err != nil {
225
+ c .errorChan <- err
226
+ return
227
+ }
228
+ } else {
229
+ // Pass error through our own error channel
230
+ c .errorChan <- err
231
+ }
207
232
}
208
- close (c .errorChan )
209
233
}()
210
234
return nil
211
235
}
@@ -297,6 +321,12 @@ func (c *ChainSync) updateStatus(
297
321
tipSlotNumber uint64 ,
298
322
tipBlockHash string ,
299
323
) {
324
+ // Update cursor cache
325
+ blockHashBytes , _ := hex .DecodeString (blockHash )
326
+ c .cursorCache = append (c .cursorCache , ocommon.Point {Slot : slotNumber , Hash : blockHashBytes })
327
+ if len (c .cursorCache ) > cursorCacheSize {
328
+ c .cursorCache = c .cursorCache [len (c .cursorCache )- cursorCacheSize :]
329
+ }
300
330
// Determine if we've reached the chain tip
301
331
if ! c .status .TipReached {
302
332
// Make sure we're past the end slot in any bulk range, since we don't update the tip during bulk sync
0 commit comments