kaspad/blockdag/notifications.go
stasatdaglabs 349e62fcd5 [DEV-332] Create partial block message in wire and make sure partial nodes receive partial blocks (#170)
* [DEV-332] Added MerkleProof to MsgBlock and rejected full-node/partial-block type misbehaviors.

* [DEV-332] Fixed merge issues.

* [DEV-332] Got rid of MerkleProof. Turns out we no longer need it.

* [DEV-332] Got rid of NTBlockDisconnected, as no one was ever triggering it. (It was part of reorg)

* [DEV-332] Implemented clearing out the payloads of transactions of outgoing blocks for partial nodes.

* [DEV-332] Extracted ConvertToPartial to its own method. Added a test. Added a condition for converting to a partial block.

* [DEV-332] Fixed bad ConvertToPartial condition.
2019-01-23 14:51:05 +02:00

77 lines
2.6 KiB
Go

// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockdag
import (
"fmt"
)
// 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
)
// notificationTypeStrings is a map of notification types back to their constant
// names for pretty printing.
var notificationTypeStrings = map[NotificationType]string{
NTBlockAccepted: "NTBlockAccepted",
NTBlockConnected: "NTBlockConnected",
}
// 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))
}
// 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: *util.Block
// - NTBlockConnected: *util.Block
// - NTBlockDisconnected: *util.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 (dag *BlockDAG) Subscribe(callback NotificationCallback) {
dag.notificationsLock.Lock()
dag.notifications = append(dag.notifications, callback)
dag.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 (dag *BlockDAG) sendNotification(typ NotificationType, data interface{}) {
// Generate and send the notification.
n := Notification{Type: typ, Data: data}
dag.notificationsLock.RLock()
for _, callback := range dag.notifications {
callback(&n)
}
dag.notificationsLock.RUnlock()
}