Skip to content

Commit

Permalink
Merge pull request #237 from blinklabs-io/feat/stop-chainsync
Browse files Browse the repository at this point in the history
feat: method to stop chainsync process
  • Loading branch information
agaffney authored Apr 25, 2023
2 parents 57e9c4e + 91d9678 commit 261b268
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
30 changes: 22 additions & 8 deletions protocol/chainsync/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ func (c *Client) Sync(intersectPoints []common.Point) error {
func (c *Client) syncLoop() {
for {
// Wait for a block to be received
if _, ok := <-c.readyForNextBlockChan; !ok {
if ready, ok := <-c.readyForNextBlockChan; !ok {
// Channel is closed, which means we're shutting down
return
} else if !ready {
// Sync was cancelled
return
}
c.busyMutex.Lock()
// Request the next block
Expand All @@ -172,10 +175,7 @@ func (c *Client) handleRollForward(msgGeneric protocol.Message) error {
if c.config.RollForwardFunc == nil {
return fmt.Errorf("received chain-sync RollForward message but no callback function is defined")
}
// Signal that we're ready for the next block after we finish handling this one
defer func() {
c.readyForNextBlockChan <- true
}()
var callbackErr error
if c.Mode() == protocol.ProtocolModeNodeToNode {
msg := msgGeneric.(*MsgRollForwardNtN)
var blockHeader interface{}
Expand Down Expand Up @@ -206,16 +206,23 @@ func (c *Client) handleRollForward(msgGeneric protocol.Message) error {
}
}
// Call the user callback function
return c.config.RollForwardFunc(blockType, blockHeader, msg.Tip)
callbackErr = c.config.RollForwardFunc(blockType, blockHeader, msg.Tip)
} else {
msg := msgGeneric.(*MsgRollForwardNtC)
blk, err := ledger.NewBlockFromCbor(msg.BlockType(), msg.BlockCbor())
if err != nil {
return err
}
// Call the user callback function
return c.config.RollForwardFunc(msg.BlockType(), blk, msg.Tip)
callbackErr = c.config.RollForwardFunc(msg.BlockType(), blk, msg.Tip)
}
if callbackErr == StopSyncProcessError {
// Signal that we're cancelling the sync
c.readyForNextBlockChan <- false
}
// Signal that we're ready for the next block
c.readyForNextBlockChan <- true
return nil
}

func (c *Client) handleRollBackward(msgGeneric protocol.Message) error {
Expand All @@ -228,7 +235,14 @@ func (c *Client) handleRollBackward(msgGeneric protocol.Message) error {
c.readyForNextBlockChan <- true
}()
// Call the user callback function
return c.config.RollBackwardFunc(msg.Point, msg.Tip)
callbackErr := c.config.RollBackwardFunc(msg.Point, msg.Tip)
if callbackErr == StopSyncProcessError {
// Signal that we're cancelling the sync
c.readyForNextBlockChan <- false
}
// Signal that we're ready for the next block
c.readyForNextBlockChan <- true
return nil
}

func (c *Client) handleIntersectFound(msgGeneric protocol.Message) error {
Expand Down
8 changes: 8 additions & 0 deletions protocol/chainsync/error.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package chainsync

import (
"fmt"
)

// IntersectNotFoundError represents a failure to find a chain intersection
type IntersectNotFoundError struct {
}

func (e IntersectNotFoundError) Error() string {
return "chain intersection not found"
}

// StopChainSync is used as a special return value from a RollForward or RollBackward handler function
// to signify that the sync process should be stopped
var StopSyncProcessError = fmt.Errorf("stop sync process")

0 comments on commit 261b268

Please sign in to comment.