2015-12-16 21:38:21 +01:00
|
|
|
package chainntnfs
|
|
|
|
|
2016-08-04 07:13:10 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
)
|
2016-01-06 22:01:50 +01:00
|
|
|
|
2016-02-27 02:01:08 +01:00
|
|
|
// ChainNotifier represents a trusted source to receive notifications concerning
|
|
|
|
// targeted events on the Bitcoin blockchain. The interface specification is
|
|
|
|
// intentionally general in order to support a wide array of chain notification
|
|
|
|
// implementations such as: btcd's websockets notifications, Bitcoin Core's
|
|
|
|
// ZeroMQ notifications, various Bitcoin API services, Electrum servers, etc.
|
|
|
|
//
|
|
|
|
// Concrete implementations of ChainNotifier should be able to support multiple
|
|
|
|
// concurrent client requests, as well as multiple concurrent notification events.
|
2015-12-16 21:38:21 +01:00
|
|
|
type ChainNotifier interface {
|
2016-02-27 02:01:08 +01:00
|
|
|
// RegisterConfirmationsNtfn registers an intent to be notified once
|
|
|
|
// txid reaches numConfs confirmations. The returned ConfirmationEvent
|
|
|
|
// should properly notify the client once the specified number of
|
|
|
|
// confirmations has been reached for the txid, as well as if the
|
|
|
|
// original tx gets re-org'd out of the mainchain.
|
2016-02-16 23:44:49 +01:00
|
|
|
RegisterConfirmationsNtfn(txid *wire.ShaHash, numConfs uint32) (*ConfirmationEvent, error)
|
2016-02-27 02:01:08 +01:00
|
|
|
|
|
|
|
// RegisterSpendNtfn registers an intent to be notified once the target
|
|
|
|
// outpoint is succesfully spent within a confirmed transaction. The
|
|
|
|
// returned SpendEvent will receive a send on the 'Spend' transaction
|
|
|
|
// once a transaction spending the input is detected on the blockchain.
|
2016-06-21 06:27:38 +02:00
|
|
|
//
|
|
|
|
// NOTE: This notifications should be triggered once the transaction is
|
|
|
|
// *seen* on the network, not when it has received a single confirmation.
|
2016-02-16 23:44:49 +01:00
|
|
|
RegisterSpendNtfn(outpoint *wire.OutPoint) (*SpendEvent, error)
|
2016-01-06 22:01:50 +01:00
|
|
|
|
2016-06-21 06:31:05 +02:00
|
|
|
// RegisterBlockEpochNtfn registers an intent to be notified of each
|
|
|
|
// new block connected to the tip of the main chain. The returned
|
|
|
|
// BlockEpochEvent struct contains a channel which will be sent upon
|
|
|
|
// for each new block discovered.
|
|
|
|
RegisterBlockEpochNtfn(targetHeight int32) (*BlockEpochEvent, error)
|
|
|
|
|
2016-02-27 02:01:08 +01:00
|
|
|
// Start the ChainNotifier. Once started, the implementation should be
|
|
|
|
// ready, and able to receive notification registrations from clients.
|
2016-01-06 22:01:50 +01:00
|
|
|
Start() error
|
2016-02-27 02:01:08 +01:00
|
|
|
|
|
|
|
// Stops the concrete ChainNotifier. Once stopped, the ChainNotifier
|
|
|
|
// should disallow any future requests from potential clients.
|
|
|
|
// Additionally, all pending client notifications will be cancelled
|
|
|
|
// by closing the related channels on the *Event's.
|
2016-01-06 22:01:50 +01:00
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
2016-02-16 23:44:49 +01:00
|
|
|
// TODO(roasbeef): ln channels should request spend ntfns for counterparty's
|
|
|
|
// inputs to funding tx also, consider channel closed if funding tx re-org'd
|
|
|
|
// out and inputs double spent.
|
|
|
|
|
2016-06-21 06:31:05 +02:00
|
|
|
// TODO(roasbeef): all chans should be receive only.
|
|
|
|
|
2016-02-27 02:01:08 +01:00
|
|
|
// ConfirmationEvent encapsulates a confirmation notification. With this struct,
|
|
|
|
// callers can be notified of: the instance the target txid reaches the targeted
|
|
|
|
// number of confirmations, and also in the event that the original txid becomes
|
|
|
|
// disconnected from the blockchain as a result of a re-org.
|
|
|
|
//
|
|
|
|
// Once the txid reaches the specified number of confirmations, the 'Confirmed'
|
|
|
|
// channel will be sent upon fufulling the notification.
|
|
|
|
//
|
|
|
|
// If the event that the original transaction becomes re-org'd out of the main
|
|
|
|
// chain, the 'NegativeConf' will be sent upon with a value representing the
|
|
|
|
// depth of the re-org.
|
2016-02-16 23:44:49 +01:00
|
|
|
type ConfirmationEvent struct {
|
2016-06-21 06:33:41 +02:00
|
|
|
Confirmed chan int32 // MUST be buffered.
|
2016-02-16 23:44:49 +01:00
|
|
|
// TODO(roasbeef): all goroutines on ln channel updates should also
|
|
|
|
// have a struct chan that's closed if funding gets re-org out. Need
|
|
|
|
// to sync, to request another confirmation event ntfn, then re-open
|
|
|
|
// channel after confs.
|
|
|
|
|
2016-02-27 01:30:27 +01:00
|
|
|
NegativeConf chan int32 // MUST be buffered.
|
2016-02-16 23:44:49 +01:00
|
|
|
}
|
|
|
|
|
2016-02-27 02:01:08 +01:00
|
|
|
// SpendDetail contains details pertaining to a spent output. This struct itself
|
|
|
|
// is the spentness notification. It includes the original outpoint which triggered
|
|
|
|
// the notification, the hash of the transaction spending the output, the
|
|
|
|
// spending transaction itself, and finally the input index which spent the
|
|
|
|
// target output.
|
2016-02-16 23:44:49 +01:00
|
|
|
type SpendDetail struct {
|
2016-02-27 01:30:27 +01:00
|
|
|
SpentOutPoint *wire.OutPoint
|
2016-02-16 23:44:49 +01:00
|
|
|
SpenderTxHash *wire.ShaHash
|
2016-02-27 01:30:27 +01:00
|
|
|
SpendingTx *wire.MsgTx
|
2016-02-16 23:44:49 +01:00
|
|
|
SpenderInputIndex uint32
|
2016-06-21 06:33:41 +02:00
|
|
|
SpendingHeight int32
|
2016-02-16 23:44:49 +01:00
|
|
|
}
|
|
|
|
|
2016-02-27 02:01:08 +01:00
|
|
|
// SpendEvent encapsulates a spentness notification. Its only field 'Spend' will
|
|
|
|
// be sent upon once the target output passed into RegisterSpendNtfn has been
|
|
|
|
// spent on the blockchain.
|
2016-02-16 23:44:49 +01:00
|
|
|
type SpendEvent struct {
|
|
|
|
Spend chan *SpendDetail // MUST be buffered.
|
2015-12-16 21:38:21 +01:00
|
|
|
}
|
2016-06-21 06:31:05 +02:00
|
|
|
|
|
|
|
// BlockEpoch represents meta-data concerning each new block connected to the
|
|
|
|
// main chain.
|
|
|
|
type BlockEpoch struct {
|
|
|
|
Height int32
|
|
|
|
Hash *wire.ShaHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockEpochEvent encapsulates an on-going stream of block epoch
|
|
|
|
// notifications. Its only field 'Epoochs' will be sent upon for each new block
|
|
|
|
// connected to the main-chain.
|
|
|
|
type BlockEpochEvent struct {
|
|
|
|
Epochs chan *BlockEpoch // MUST be buffered.
|
|
|
|
}
|
2016-08-04 07:13:10 +02:00
|
|
|
|
|
|
|
// NotifierDriver represents a "driver" for a particular interface. A driver is
|
|
|
|
// indentified by a globally unique string identifier along with a 'New()'
|
|
|
|
// method which is responsible for initializing a particular ChainNotifier
|
|
|
|
// concrete implementation.
|
|
|
|
type NotifierDriver struct {
|
|
|
|
// NotifierType is a string which uniquely identifes the ChainNotifier
|
|
|
|
// that this driver, drives.
|
|
|
|
NotifierType string
|
|
|
|
|
|
|
|
// New creates a new instance of a concrete ChainNotifier
|
|
|
|
// implementation given a variadic set up arguments. The function takes
|
|
|
|
// a varidaic number of interface paramters in order to provide
|
|
|
|
// initialization flexibility, thereby accomodating several potential
|
|
|
|
// ChainNotifier implementations.
|
|
|
|
New func(args ...interface{}) (ChainNotifier, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
notifiers = make(map[string]*NotifierDriver)
|
|
|
|
registerMtx sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegisteredNotifiers returns a slice of all currently registered notifiers.
|
|
|
|
//
|
|
|
|
// NOTE: This function is safe for concurrent access.
|
|
|
|
func RegisteredNotifiers() []*NotifierDriver {
|
|
|
|
registerMtx.Lock()
|
|
|
|
defer registerMtx.Unlock()
|
|
|
|
|
|
|
|
drivers := make([]*NotifierDriver, 0, len(notifiers))
|
|
|
|
for _, driver := range notifiers {
|
|
|
|
drivers = append(drivers, driver)
|
|
|
|
}
|
|
|
|
|
|
|
|
return drivers
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterNotifier registers a NotifierDriver which is capable of driving a
|
|
|
|
// concrete ChainNotifier interface. In the case that this driver has already
|
|
|
|
// been registered, an error is returned.
|
|
|
|
//
|
|
|
|
// NOTE: This function is safe for concurrent access.
|
|
|
|
func RegisterNotifier(driver *NotifierDriver) error {
|
|
|
|
registerMtx.Lock()
|
|
|
|
defer registerMtx.Unlock()
|
|
|
|
|
|
|
|
if _, ok := notifiers[driver.NotifierType]; ok {
|
|
|
|
return fmt.Errorf("notifier already registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
notifiers[driver.NotifierType] = driver
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SupportedNotifiers returns a slice of strings that represent the database
|
|
|
|
// drivers that have been registered and are therefore supported.
|
|
|
|
//
|
|
|
|
// NOTE: This function is safe for concurrent access.
|
|
|
|
func SupportedNotifiers() []string {
|
|
|
|
registerMtx.Lock()
|
|
|
|
defer registerMtx.Unlock()
|
|
|
|
|
|
|
|
supportedNotifiers := make([]string, 0, len(notifiers))
|
|
|
|
for driverName := range notifiers {
|
|
|
|
supportedNotifiers = append(supportedNotifiers, driverName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return supportedNotifiers
|
|
|
|
}
|