2015-08-26 06:03:18 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-07-18 16:49:28 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
package blockchain
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NotificationType represents the type of a notification message.
|
|
|
|
type NotificationType int
|
|
|
|
|
2013-10-03 04:16:38 +02:00
|
|
|
// NotificationCallback is used for a caller to provide a callback for
|
|
|
|
// notifications about various chain events.
|
|
|
|
type NotificationCallback func(*Notification)
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// Constants for the type of a notification message.
|
|
|
|
const (
|
|
|
|
// NTBlockAccepted indicates the associated block was accepted into
|
|
|
|
// the block chain. Note that this does not necessarily mean it was
|
|
|
|
// added to the main chain. For that, use NTBlockConnected.
|
2014-06-25 22:47:24 +02:00
|
|
|
NTBlockAccepted NotificationType = iota
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
// NTBlockConnected indicates the associated block was connected to the
|
|
|
|
// main chain.
|
|
|
|
NTBlockConnected
|
|
|
|
|
|
|
|
// NTBlockDisconnected indicates the associated block was disconnected
|
|
|
|
// from the main chain.
|
|
|
|
NTBlockDisconnected
|
|
|
|
)
|
|
|
|
|
|
|
|
// notificationTypeStrings is a map of notification types back to their constant
|
|
|
|
// names for pretty printing.
|
|
|
|
var notificationTypeStrings = map[NotificationType]string{
|
|
|
|
NTBlockAccepted: "NTBlockAccepted",
|
|
|
|
NTBlockConnected: "NTBlockConnected",
|
|
|
|
NTBlockDisconnected: "NTBlockDisconnected",
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the NotificationType in human-readable form.
|
|
|
|
func (n NotificationType) String() string {
|
|
|
|
if s, ok := notificationTypeStrings[n]; ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Unknown Notification Type (%d)", int(n))
|
|
|
|
}
|
|
|
|
|
2013-10-03 04:16:38 +02:00
|
|
|
// Notification defines notification that is sent to the caller via the callback
|
|
|
|
// function provided during the call to New and consists of a notification type
|
|
|
|
// as well as associated data that depends on the type as follows:
|
2022-08-08 08:40:46 +02:00
|
|
|
// - NTBlockAccepted: *btcutil.Block
|
|
|
|
// - NTBlockConnected: *btcutil.Block
|
|
|
|
// - NTBlockDisconnected: *btcutil.Block
|
2013-07-18 16:49:28 +02:00
|
|
|
type Notification struct {
|
|
|
|
Type NotificationType
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
|
2017-08-11 22:17:54 +02:00
|
|
|
// Subscribe to block chain notifications. Registers a callback to be executed
|
|
|
|
// when various events take place. See the documentation on Notification and
|
|
|
|
// NotificationType for details on the types and contents of notifications.
|
|
|
|
func (b *BlockChain) Subscribe(callback NotificationCallback) {
|
|
|
|
b.notificationsLock.Lock()
|
|
|
|
b.notifications = append(b.notifications, callback)
|
|
|
|
b.notificationsLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// sendNotification sends a notification with the passed type and data if the
|
2013-10-03 04:16:38 +02:00
|
|
|
// caller requested notifications by providing a callback function in the call
|
|
|
|
// to New.
|
2013-07-18 16:49:28 +02:00
|
|
|
func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
|
2013-09-21 16:23:26 +02:00
|
|
|
// Generate and send the notification.
|
|
|
|
n := Notification{Type: typ, Data: data}
|
2017-08-15 04:48:23 +02:00
|
|
|
b.notificationsLock.RLock()
|
2017-08-11 22:17:54 +02:00
|
|
|
for _, callback := range b.notifications {
|
|
|
|
callback(&n)
|
|
|
|
}
|
2017-08-15 04:48:23 +02:00
|
|
|
b.notificationsLock.RUnlock()
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|