feat: add SyncCallback for progress events
Chain now supports SetSyncCallback(localHeight, remoteHeight, bps). Wired into BlockchainService to emit EventSyncProgress events. Enables live sync progress in explorer, status dashboard, and stream. Per RFC.pubsub.md: blockchain.sync.progress event. Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
9ce6cc16b9
commit
af32d58159
2 changed files with 20 additions and 0 deletions
|
|
@ -18,6 +18,7 @@ import (
|
|||
type Chain struct {
|
||||
store *store.Store
|
||||
blockCallback BlockCallback
|
||||
syncCallback SyncCallback
|
||||
}
|
||||
|
||||
// New creates a Chain backed by the given store.
|
||||
|
|
@ -69,9 +70,19 @@ func (c *Chain) Snapshot() (uint64, *types.Block, *BlockMeta) {
|
|||
// BlockCallback is called after a block is successfully stored.
|
||||
type BlockCallback func(height uint64, hash string, aliasName string)
|
||||
|
||||
// SyncCallback is called periodically during chain sync with progress info.
|
||||
type SyncCallback func(localHeight, remoteHeight uint64, blocksPerSecond float64)
|
||||
|
||||
// SetBlockCallback sets a function called after each block is stored.
|
||||
//
|
||||
// c.SetBlockCallback(func(height uint64, hash string, alias string) { ... })
|
||||
func (c *Chain) SetBlockCallback(cb BlockCallback) {
|
||||
c.blockCallback = cb
|
||||
}
|
||||
|
||||
// SetSyncCallback sets a function called during sync progress.
|
||||
//
|
||||
// c.SetSyncCallback(func(local, remote uint64, bps float64) { ... })
|
||||
func (c *Chain) SetSyncCallback(cb SyncCallback) {
|
||||
c.syncCallback = cb
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,6 +91,15 @@ func (s *BlockchainService) start() core.Result {
|
|||
}
|
||||
})
|
||||
|
||||
// Wire sync progress events
|
||||
s.chain.SetSyncCallback(func(localHeight, remoteHeight uint64, blocksPerSecond float64) {
|
||||
s.events.Emit(Event{Type: EventSyncProgress, Height: localHeight, Data: map[string]interface{}{
|
||||
"local_height": localHeight,
|
||||
"remote_height": remoteHeight,
|
||||
"blocks_per_sec": blocksPerSecond,
|
||||
}})
|
||||
})
|
||||
|
||||
cfg, forks := resolveConfig(s.opts.Testnet, &s.opts.Seed)
|
||||
|
||||
// Start background sync.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue