2015-12-16 23:38:21 +03:00
|
|
|
package chainntnfs
|
|
|
|
|
2016-08-04 08:13:10 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2016-08-04 08:13:10 +03:00
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
)
|
2016-01-07 00:01:50 +03:00
|
|
|
|
2016-02-27 04:01:08 +03: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.
|
2016-09-08 21:27:07 +03:00
|
|
|
// TODO(roasbeef): all events should have a Cancel() method to free up the
|
|
|
|
// resource
|
2015-12-16 23:38:21 +03:00
|
|
|
type ChainNotifier interface {
|
2016-02-27 04:01:08 +03: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
|
2017-05-11 03:00:18 +03:00
|
|
|
// original tx gets re-org'd out of the mainchain. The heightHint
|
|
|
|
// parameter is provided as a convenience to light clients. The
|
2017-12-18 05:40:05 +03:00
|
|
|
// heightHint denotes the earliest height in the blockchain in which the
|
2017-05-11 03:00:18 +03:00
|
|
|
// target txid _could_ have been included in the chain. This can be
|
|
|
|
// used to bound the search space when checking to see if a
|
|
|
|
// notification can immediately be dispatched due to historical data.
|
2016-11-28 06:15:48 +03:00
|
|
|
//
|
|
|
|
// NOTE: Dispatching notifications to multiple clients subscribed to
|
|
|
|
// the same (txid, numConfs) tuple MUST be supported.
|
2017-05-11 03:00:18 +03:00
|
|
|
RegisterConfirmationsNtfn(txid *chainhash.Hash, numConfs,
|
|
|
|
heightHint uint32) (*ConfirmationEvent, error)
|
2016-02-27 04:01:08 +03:00
|
|
|
|
|
|
|
// RegisterSpendNtfn registers an intent to be notified once the target
|
2018-03-29 16:26:33 +03:00
|
|
|
// outpoint is successfully spent within a transaction. The returned
|
|
|
|
// SpendEvent will receive a send on the 'Spend' transaction once a
|
|
|
|
// transaction spending the input is detected on the blockchain. The
|
|
|
|
// heightHint parameter is provided as a convenience to light clients.
|
|
|
|
// The heightHint denotes the earliest height in the blockchain in
|
|
|
|
// which the target output could have been created.
|
2016-06-21 07:27:38 +03:00
|
|
|
//
|
2018-03-29 16:26:33 +03:00
|
|
|
// NOTE: If mempool=true is set, then this notification should be
|
|
|
|
// triggered on a best-effort basis once the transaction is *seen* on
|
|
|
|
// the network. If mempool=false, it should only be triggered when the
|
|
|
|
// spending transaction receives a single confirmation.
|
2016-11-28 06:15:48 +03:00
|
|
|
//
|
|
|
|
// NOTE: Dispatching notifications to multiple clients subscribed to a
|
|
|
|
// spend of the same outpoint MUST be supported.
|
2018-03-29 16:26:33 +03:00
|
|
|
RegisterSpendNtfn(outpoint *wire.OutPoint, heightHint uint32,
|
|
|
|
mempool bool) (*SpendEvent, error)
|
2016-01-07 00:01:50 +03:00
|
|
|
|
2016-06-21 07:31:05 +03: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.
|
2016-09-08 21:27:07 +03:00
|
|
|
RegisterBlockEpochNtfn() (*BlockEpochEvent, error)
|
2016-06-21 07:31:05 +03:00
|
|
|
|
2016-02-27 04:01:08 +03:00
|
|
|
// Start the ChainNotifier. Once started, the implementation should be
|
|
|
|
// ready, and able to receive notification registrations from clients.
|
2016-01-07 00:01:50 +03:00
|
|
|
Start() error
|
2016-02-27 04:01:08 +03: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-07 00:01:50 +03:00
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
2016-12-25 03:39:30 +03:00
|
|
|
// TxConfirmation carries some additional block-level details of the exact
|
2017-02-21 03:29:05 +03:00
|
|
|
// block that specified transactions was confirmed within.
|
2016-12-25 03:39:30 +03:00
|
|
|
type TxConfirmation struct {
|
|
|
|
// BlockHash is the hash of the block that confirmed the original
|
|
|
|
// transition.
|
2017-01-06 00:56:27 +03:00
|
|
|
BlockHash *chainhash.Hash
|
2016-12-25 03:39:30 +03:00
|
|
|
|
|
|
|
// BlockHeight is the height of the block in which the transaction was
|
|
|
|
// confirmed within.
|
|
|
|
BlockHeight uint32
|
|
|
|
|
|
|
|
// TxIndex is the index within the block of the ultimate confirmed
|
|
|
|
// transaction.
|
|
|
|
TxIndex uint32
|
|
|
|
}
|
2016-06-21 07:31:05 +03:00
|
|
|
|
2016-02-27 04:01:08 +03:00
|
|
|
// ConfirmationEvent encapsulates a confirmation notification. With this struct,
|
|
|
|
// callers can be notified of: the instance the target txid reaches the targeted
|
2018-03-19 21:48:44 +03:00
|
|
|
// number of confirmations, how many confirmations are left for the target txid
|
|
|
|
// to be fully confirmed at every new block height, and also in the event that
|
|
|
|
// the original txid becomes disconnected from the blockchain as a result of a
|
|
|
|
// re-org.
|
2016-02-27 04:01:08 +03:00
|
|
|
//
|
|
|
|
// Once the txid reaches the specified number of confirmations, the 'Confirmed'
|
2017-05-11 03:43:11 +03:00
|
|
|
// channel will be sent upon fulfilling the notification.
|
2016-02-27 04:01:08 +03:00
|
|
|
//
|
|
|
|
// 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-17 01:44:49 +03:00
|
|
|
type ConfirmationEvent struct {
|
2016-12-25 03:39:30 +03:00
|
|
|
// Confirmed is a channel that will be sent upon once the transaction
|
|
|
|
// has been fully confirmed. The struct sent will contain all the
|
|
|
|
// details of the channel's confirmation.
|
|
|
|
Confirmed chan *TxConfirmation // MUST be buffered.
|
|
|
|
|
2018-03-19 21:48:44 +03:00
|
|
|
// Updates is a channel that will sent upon, at every incremental
|
|
|
|
// confirmation, how many confirmations are left to declare the
|
|
|
|
// transaction as fully confirmed.
|
|
|
|
Updates chan uint32 // MUST be buffered.
|
|
|
|
|
2016-02-17 01:44:49 +03: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 03:30:27 +03:00
|
|
|
NegativeConf chan int32 // MUST be buffered.
|
2016-02-17 01:44:49 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:01:08 +03: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-17 01:44:49 +03:00
|
|
|
type SpendDetail struct {
|
2016-02-27 03:30:27 +03:00
|
|
|
SpentOutPoint *wire.OutPoint
|
2017-01-06 00:56:27 +03:00
|
|
|
SpenderTxHash *chainhash.Hash
|
2016-02-27 03:30:27 +03:00
|
|
|
SpendingTx *wire.MsgTx
|
2016-02-17 01:44:49 +03:00
|
|
|
SpenderInputIndex uint32
|
2016-06-21 07:33:41 +03:00
|
|
|
SpendingHeight int32
|
2016-02-17 01:44:49 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 04:01:08 +03: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.
|
2017-02-21 03:29:05 +03:00
|
|
|
//
|
|
|
|
// NOTE: If the caller wishes to cancel their registered spend notification,
|
|
|
|
// the Cancel closure MUST be called.
|
2016-02-17 01:44:49 +03:00
|
|
|
type SpendEvent struct {
|
2017-02-21 03:29:05 +03:00
|
|
|
// Spend is a receive only channel which will be sent upon once the
|
|
|
|
// target outpoint has been spent.
|
|
|
|
Spend <-chan *SpendDetail // MUST be buffered.
|
|
|
|
|
|
|
|
// Cancel is a closure that should be executed by the caller in the
|
2017-05-11 03:43:11 +03:00
|
|
|
// case that they wish to prematurely abandon their registered spend
|
2017-02-21 03:29:05 +03:00
|
|
|
// notification.
|
|
|
|
Cancel func()
|
2015-12-16 23:38:21 +03:00
|
|
|
}
|
2016-06-21 07:31:05 +03:00
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// BlockEpoch represents metadata concerning each new block connected to the
|
2016-06-21 07:31:05 +03:00
|
|
|
// main chain.
|
|
|
|
type BlockEpoch struct {
|
2017-05-11 03:43:11 +03:00
|
|
|
// Hash is the block hash of the latest block to be added to the tip of
|
2017-02-21 03:29:05 +03:00
|
|
|
// the main chain.
|
|
|
|
Hash *chainhash.Hash
|
|
|
|
|
|
|
|
// Height is the height of the latest block to be added to the tip of
|
|
|
|
// the main chain.
|
2016-06-21 07:31:05 +03:00
|
|
|
Height int32
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockEpochEvent encapsulates an on-going stream of block epoch
|
2017-02-21 03:29:05 +03:00
|
|
|
// notifications. Its only field 'Epochs' will be sent upon for each new block
|
2016-06-21 07:31:05 +03:00
|
|
|
// connected to the main-chain.
|
2017-02-21 03:29:05 +03:00
|
|
|
//
|
|
|
|
// NOTE: If the caller wishes to cancel their registered block epoch
|
|
|
|
// notification, the Cancel closure MUST be called.
|
2016-06-21 07:31:05 +03:00
|
|
|
type BlockEpochEvent struct {
|
2017-02-21 03:29:05 +03:00
|
|
|
// Epochs is a receive only channel that will be sent upon each time a
|
|
|
|
// new block is connected to the end of the main chain.
|
|
|
|
Epochs <-chan *BlockEpoch // MUST be buffered.
|
|
|
|
|
|
|
|
// Cancel is a closure that should be executed by the caller in the
|
|
|
|
// case that they wish to abandon their registered spend notification.
|
|
|
|
Cancel func()
|
2016-06-21 07:31:05 +03:00
|
|
|
}
|
2016-08-04 08:13:10 +03:00
|
|
|
|
|
|
|
// NotifierDriver represents a "driver" for a particular interface. A driver is
|
2016-11-28 06:15:48 +03:00
|
|
|
// identified by a globally unique string identifier along with a 'New()'
|
2016-08-04 08:13:10 +03:00
|
|
|
// method which is responsible for initializing a particular ChainNotifier
|
|
|
|
// concrete implementation.
|
|
|
|
type NotifierDriver struct {
|
2016-11-28 06:15:48 +03:00
|
|
|
// NotifierType is a string which uniquely identifies the ChainNotifier
|
2016-08-04 08:13:10 +03:00
|
|
|
// 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
|
2018-02-07 06:11:11 +03:00
|
|
|
// a variadic number of interface parameters in order to provide
|
2016-11-28 06:15:48 +03:00
|
|
|
// initialization flexibility, thereby accommodating several potential
|
2016-08-04 08:13:10 +03:00
|
|
|
// 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
|
|
|
|
}
|