Skip to content

Commit 54742c9

Browse files
committed
fix: prevent block range start from being after end
1 parent 1a2ae72 commit 54742c9

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

protocol/chainsync/client.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,29 +156,29 @@ func (c *Client) GetAvailableBlockRange(
156156
) (common.Point, common.Point, error) {
157157
c.busyMutex.Lock()
158158
defer c.busyMutex.Unlock()
159-
c.wantCurrentTip = true
160-
c.wantFirstBlock = true
161159
var start, end common.Point
162160
msgFindIntersect := NewMsgFindIntersect(intersectPoints)
163161
if err := c.SendMessage(msgFindIntersect); err != nil {
164162
return start, end, err
165163
}
166164
select {
167-
case tip := <-c.currentTipChan:
168-
end = tip.Point
169-
// Clear out intersect result channel to prevent blocking
170-
<-c.intersectResultChan
171165
case err := <-c.intersectResultChan:
172-
return start, end, err
166+
if err != nil {
167+
return start, end, err
168+
}
173169
}
174-
c.wantCurrentTip = false
170+
c.wantCurrentTip = true
171+
c.wantFirstBlock = true
175172
// Request the next block. This should result in a rollback
176173
msgRequestNext := NewMsgRequestNext()
177174
if err := c.SendMessage(msgRequestNext); err != nil {
178175
return start, end, err
179176
}
180177
for {
181178
select {
179+
case tip := <-c.currentTipChan:
180+
end = tip.Point
181+
c.wantCurrentTip = false
182182
case point := <-c.firstBlockChan:
183183
start = point
184184
c.wantFirstBlock = false
@@ -189,7 +189,7 @@ func (c *Client) GetAvailableBlockRange(
189189
return start, end, err
190190
}
191191
}
192-
if !c.wantFirstBlock {
192+
if !c.wantFirstBlock && !c.wantCurrentTip {
193193
break
194194
}
195195
}
@@ -329,16 +329,19 @@ func (c *Client) handleRollForward(msgGeneric protocol.Message) error {
329329
return nil
330330
}
331331

332-
func (c *Client) handleRollBackward(msgGeneric protocol.Message) error {
332+
func (c *Client) handleRollBackward(msg protocol.Message) error {
333+
msgRollBackward := msg.(*MsgRollBackward)
334+
if c.wantCurrentTip {
335+
c.currentTipChan <- msgRollBackward.Tip
336+
}
333337
if !c.wantFirstBlock {
334338
if c.config.RollBackwardFunc == nil {
335339
return fmt.Errorf(
336340
"received chain-sync RollBackward message but no callback function is defined",
337341
)
338342
}
339-
msg := msgGeneric.(*MsgRollBackward)
340343
// Call the user callback function
341-
if callbackErr := c.config.RollBackwardFunc(msg.Point, msg.Tip); callbackErr != nil {
344+
if callbackErr := c.config.RollBackwardFunc(msgRollBackward.Point, msgRollBackward.Tip); callbackErr != nil {
342345
if callbackErr == StopSyncProcessError {
343346
// Signal that we're cancelling the sync
344347
c.readyForNextBlockChan <- false

0 commit comments

Comments
 (0)