@@ -8,6 +8,8 @@ package indexer
88
99import (
1010 "encoding/base32"
11+ "encoding/hex"
12+ "errors"
1113 "fmt"
1214 "log/slog"
1315 "time"
@@ -22,7 +24,6 @@ type handshakeState struct {
2224 peer * handshake.Peer
2325 peerAddress string
2426 peerBackoffDelay time.Duration
25- blockHeight int
2627}
2728
2829func (i * Indexer ) startHandshake () error {
@@ -59,8 +60,30 @@ func (i *Indexer) handshakeConnectPeer() error {
5960 // Stop waiting on connection shutdown
6061 }
6162 }()
63+ var locator [][32 ]byte = nil
64+ cursorBlockHash , err := state .GetState ().GetHandshakeCursor ()
65+ if err != nil {
66+ return err
67+ }
68+ if cursorBlockHash != "" {
69+ slog .Info (
70+ "found previous Handshake cursor: " + cursorBlockHash ,
71+ )
72+ hashBytes , err := hex .DecodeString (cursorBlockHash )
73+ if err != nil {
74+ return err
75+ }
76+ if len (hashBytes ) != 32 {
77+ // This isn't a condition we can really recover from, since it implies database corruption
78+ slog .Error (
79+ fmt .Sprintf ("bad Handshake cursor block hash: %x" , hashBytes ),
80+ )
81+ return errors .New ("bad Handshake locator" )
82+ }
83+ locator = [][32 ]byte {[32 ]byte (hashBytes )}
84+ }
6285 // Start sync
63- if err := i .handshakeState .peer .Sync (nil , i .handshakeHandleSync ); err != nil {
86+ if err := i .handshakeState .peer .Sync (locator , i .handshakeHandleSync ); err != nil {
6487 _ = i .handshakeState .peer .Close ()
6588 return err
6689 }
@@ -102,10 +125,8 @@ func (i *Indexer) handshakeReconnectPeer() {
102125}
103126
104127func (i * Indexer ) handshakeHandleSync (block * handshake.Block ) error {
105- i .handshakeState .blockHeight ++
106128 slog .Debug (
107129 "synced Handshake block" ,
108- "height" , i .handshakeState .blockHeight ,
109130 "hash" , fmt .Sprintf ("%x" , block .Hash ()),
110131 "prevHash" , fmt .Sprintf ("%x" , block .Header .PrevBlock ),
111132 )
@@ -152,6 +173,11 @@ func (i *Indexer) handshakeHandleSync(block *handshake.Block) error {
152173 }
153174 }
154175 }
176+ // Update cursor
177+ blockHash := block .Hash ()
178+ if err := state .GetState ().UpdateHandshakeCursor (hex .EncodeToString (blockHash [:])); err != nil {
179+ return err
180+ }
155181 return nil
156182}
157183
0 commit comments