Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: chainsync input plugin status update callback #52

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions input/chainsync/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,36 @@ import (
)

type ChainSync struct {
oConn *ouroboros.Connection
network string
networkMagic uint32
address string
socketPath string
ntcTcp bool
intersectTip bool
intersectPoints []ocommon.Point
includeCbor bool
errorChan chan error
eventChan chan event.Event
oConn *ouroboros.Connection
network string
networkMagic uint32
address string
socketPath string
ntcTcp bool
intersectTip bool
intersectPoints []ocommon.Point
includeCbor bool
statusUpdateFunc StatusUpdateFunc
status *ChainSyncStatus
errorChan chan error
eventChan chan event.Event
}

type ChainSyncStatus struct {
SlotNumber uint64
BlockNumber uint64
BlockHash string
}

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)
Expand Down Expand Up @@ -176,6 +187,7 @@ func (c *ChainSync) handleRollForward(blockType uint, blockData interface{}, tip
case ledger.Block:
evt := event.New("chainsync.block", time.Now(), NewBlockEvent(v, c.includeCbor))
c.eventChan <- evt
c.updateStatus(v.SlotNumber(), v.BlockNumber(), v.Hash())
case ledger.BlockHeader:
blockSlot := v.SlotNumber()
blockHash, _ := hex.DecodeString(v.Hash())
Expand All @@ -189,6 +201,16 @@ func (c *ChainSync) handleRollForward(blockType uint, blockData interface{}, tip
txEvt := event.New("chainsync.transaction", time.Now(), NewTransactionEvent(block, transaction, c.includeCbor))
c.eventChan <- txEvt
}
c.updateStatus(v.SlotNumber(), v.BlockNumber(), v.Hash())
}
return nil
}

func (c *ChainSync) updateStatus(slotNumber uint64, blockNumber uint64, blockHash string) {
c.status.SlotNumber = slotNumber
c.status.BlockNumber = blockNumber
c.status.BlockHash = blockHash
if c.statusUpdateFunc != nil {
c.statusUpdateFunc(*(c.status))
}
}
8 changes: 8 additions & 0 deletions input/chainsync/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ func WithIncludeCbor(includeCbor bool) ChainSyncOptionFunc {
c.includeCbor = includeCbor
}
}

// WithStatusUpdateFunc specifies a callback function for status updates. This is useful for tracking the chain-sync status
// to be able to resume a sync at a later time, especially when any filtering could prevent you from getting all block update events
func WithStatusUpdateFunc(statusUpdateFunc StatusUpdateFunc) ChainSyncOptionFunc {
return func(c *ChainSync) {
c.statusUpdateFunc = statusUpdateFunc
}
}
Loading