forked from decred/dcrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.go
122 lines (103 loc) · 3.91 KB
/
notifications.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockchain
import (
"fmt"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil"
)
// 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 (
// 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.
NTBlockAccepted 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
// NTReorganization indicates that a blockchain reorganization is in
// progress.
NTReorganization
// NTSpentAndMissedTickets indicates spent or missed tickets from a newly
// accepted block.
NTSpentAndMissedTickets
// NTSpentAndMissedTickets indicates newly maturing tickets from a newly
// accepted block.
NTNewTickets
)
// 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",
NTReorganization: "NTReorganization",
NTSpentAndMissedTickets: "NTSpentAndMissedTickets",
NTNewTickets: "NTNewTickets",
}
// 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))
}
// BlockAcceptedNtfnsData is the structure for data indicating information
// about a block being accepted.
type BlockAcceptedNtfnsData struct {
OnMainChain bool
Block *dcrutil.Block
}
// ReorganizationNtfnsData is the structure for data indicating information
// about a reorganization.
type ReorganizationNtfnsData struct {
OldHash chainhash.Hash
OldHeight int64
NewHash chainhash.Hash
NewHeight int64
}
// TicketNotificationsData is the structure for new/spent/missed ticket
// notifications at blockchain HEAD that are outgoing from chain.
type TicketNotificationsData struct {
Hash chainhash.Hash
Height int64
StakeDifficulty int64
TicketsSpent []chainhash.Hash
TicketsMissed []chainhash.Hash
TicketsNew []chainhash.Hash
}
// 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:
// - NTBlockAccepted: *BlockAcceptedNtfnsData
// - NTBlockConnected: []*dcrutil.Block of len 2
// - NTBlockDisconnected: []*dcrutil.Block of len 2
// - NTReorganization: *ReorganizationNtfnsData
// - NTSpentAndMissedTickets: *TicketNotificationsData
// - NTNewTickets: *TicketNotificationsData
type Notification struct {
Type NotificationType
Data interface{}
}
// 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 (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
// Ignore it if the caller didn't request notifications.
if b.notifications == nil {
return
}
// Generate and send the notification.
n := Notification{Type: typ, Data: data}
b.notifications(&n)
}