Move to a callback for notifications.
Rather than using a channel for notifictions, use a callback instead. There are several issues that arise with async notifications via a channel, so use a callback instead. The caller can always make the notifications async by using channels in the provided callback if needed.
This commit is contained in:
parent
e54fb96d80
commit
667eaa1562
2 changed files with 17 additions and 13 deletions
14
chain.go
14
chain.go
|
@ -144,7 +144,7 @@ func removeChildNode(children []*blockNode, node *blockNode) []*blockNode {
|
||||||
type BlockChain struct {
|
type BlockChain struct {
|
||||||
db btcdb.Db
|
db btcdb.Db
|
||||||
btcnet btcwire.BitcoinNet
|
btcnet btcwire.BitcoinNet
|
||||||
notifications chan *Notification
|
notifications NotificationCallback
|
||||||
root *blockNode
|
root *blockNode
|
||||||
bestChain *blockNode
|
bestChain *blockNode
|
||||||
index map[btcwire.ShaHash]*blockNode
|
index map[btcwire.ShaHash]*blockNode
|
||||||
|
@ -960,12 +960,12 @@ func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block) err
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a BlockChain instance for the passed bitcoin network using the
|
// New returns a BlockChain instance for the passed bitcoin network using the
|
||||||
// provided backing database. It accepts a channel on which asynchronous
|
// provided backing database. It accepts a callback on which notifications
|
||||||
// notifications will be sent when various events take place. See the
|
// will be sent when various events take place. See the documentation for
|
||||||
// documentation for Notification and NotificationType for details on the
|
// Notification and NotificationType for details on the types and contents of
|
||||||
// types and contents of notifications. The provided channel can be nil if the
|
// notifications. The provided callback can be nil if the caller is not
|
||||||
// caller is not interested in receiving notifications.
|
// interested in receiving notifications.
|
||||||
func New(db btcdb.Db, btcnet btcwire.BitcoinNet, c chan *Notification) *BlockChain {
|
func New(db btcdb.Db, btcnet btcwire.BitcoinNet, c NotificationCallback) *BlockChain {
|
||||||
b := BlockChain{
|
b := BlockChain{
|
||||||
db: db,
|
db: db,
|
||||||
btcnet: btcnet,
|
btcnet: btcnet,
|
||||||
|
|
|
@ -11,6 +11,10 @@ import (
|
||||||
// NotificationType represents the type of a notification message.
|
// NotificationType represents the type of a notification message.
|
||||||
type NotificationType int
|
type NotificationType int
|
||||||
|
|
||||||
|
// NotificationCallback is used for a caller to provide a callback for
|
||||||
|
// notifications about various chain events.
|
||||||
|
type NotificationCallback func(*Notification)
|
||||||
|
|
||||||
// Constants for the type of a notification message.
|
// Constants for the type of a notification message.
|
||||||
const (
|
const (
|
||||||
// NTOrphanBlock indicates an orphan block was processed and the
|
// NTOrphanBlock indicates an orphan block was processed and the
|
||||||
|
@ -50,10 +54,9 @@ func (n NotificationType) String() string {
|
||||||
return fmt.Sprintf("Unknown Notification Type (%d)", int(n))
|
return fmt.Sprintf("Unknown Notification Type (%d)", int(n))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notification defines an asynchronous notification that is sent to the caller
|
// Notification defines notification that is sent to the caller via the callback
|
||||||
// over the notification channel provided during the call to New and consists
|
// function provided during the call to New and consists of a notification type
|
||||||
// of a notification type as well as associated data that depends on the type as
|
// as well as associated data that depends on the type as follows:
|
||||||
// follows:
|
|
||||||
// - NTOrphanBlock: *btcwire.ShaHash
|
// - NTOrphanBlock: *btcwire.ShaHash
|
||||||
// - NTBlockAccepted: *btcutil.Block
|
// - NTBlockAccepted: *btcutil.Block
|
||||||
// - NTBlockConnected: *btcutil.Block
|
// - NTBlockConnected: *btcutil.Block
|
||||||
|
@ -64,7 +67,8 @@ type Notification struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// sendNotification sends a notification with the passed type and data if the
|
// sendNotification sends a notification with the passed type and data if the
|
||||||
// caller requested notifications by providing a channel in the call to New.
|
// caller requested notifications by providing a callback function in the call
|
||||||
|
// to New.
|
||||||
func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
|
func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
|
||||||
// Ignore it if the caller didn't request notifications.
|
// Ignore it if the caller didn't request notifications.
|
||||||
if b.notifications == nil {
|
if b.notifications == nil {
|
||||||
|
@ -73,5 +77,5 @@ func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
|
||||||
|
|
||||||
// Generate and send the notification.
|
// Generate and send the notification.
|
||||||
n := Notification{Type: typ, Data: data}
|
n := Notification{Type: typ, Data: data}
|
||||||
b.notifications <- &n
|
b.notifications(&n)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue