/
notification.go
88 lines (73 loc) · 2.84 KB
/
notification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package chain
import (
"fmt"
"github.com/copernet/copernicus/model/blockindex"
)
// NotificationType represents the type of a notification message.
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.
const (
// NTNewPoWValidBlock 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.
NTNewPoWValidBlock NotificationType = iota
// NTBlockConnected indicates the associated block was connected to the
// main chain.
NTBlockConnected
// NTBlockDisconnected indicates the associated block was disconnected
// from the main chain.
NTBlockDisconnected
// NTChainTipUpdated indicates the associated blocks leads to the new main chain.
NTChainTipUpdated
)
// notificationTypeStrings is a map of notification types back to their constant
// names for pretty printing.
var notificationTypeStrings = map[NotificationType]string{
NTNewPoWValidBlock: "NTNewPoWValidBlock",
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))
}
type TipUpdatedEvent struct {
TipIndex *blockindex.BlockIndex
ForkIndex *blockindex.BlockIndex
IsInitialDownload bool
}
// 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:
// - NTNewPoWValidBlock: *btcutil.Block
// - NTBlockConnected: *btcutil.Block
// - NTBlockDisconnected: *btcutil.Block
type Notification struct {
Type NotificationType
Data interface{}
}
// 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 (c *Chain) Subscribe(callback NotificationCallback) {
c.notificationsLock.Lock()
c.notifications = append(c.notifications, callback)
c.notificationsLock.Unlock()
}
// SendNotification sends a notification with the passed type and data if the
// caller requested notifications by providing a callback function in the call
// to New.
func (c *Chain) SendNotification(typ NotificationType, data interface{}) {
n := Notification{Type: typ, Data: data}
c.notificationsLock.RLock()
defer c.notificationsLock.RUnlock()
for _, callback := range c.notifications {
callback(&n)
}
}