From 7bff2e07a8330dacc069a26394a248c8030f0d62 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 20 Jun 2016 21:31:05 -0700 Subject: [PATCH] chainntfs: add new RegisterBlockHeightNtfn method This commit adds a new method to the ChainNotifier interface which subscribes the caller to a continuous stream of notifications generated by new blocks added to the tip of the Bitcoin main chain. Concurrently, this method is intended to be used in order to obtain the necessary block height information to properly handle the timeout period on any pending HTLCs. A continuos stream, rather than a one-off notification is chosen in order to discourage a goroutine-per-HTLC model which would be rather wasteful. --- chainntfs/btcdnotify/btcd.go | 8 ++++++++ chainntfs/chainntfs.go | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/chainntfs/btcdnotify/btcd.go b/chainntfs/btcdnotify/btcd.go index f4be5703..018f30fa 100644 --- a/chainntfs/btcdnotify/btcd.go +++ b/chainntfs/btcdnotify/btcd.go @@ -352,3 +352,11 @@ func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *wire.ShaHash, NegativeConf: ntfn.negativeConf, }, nil } + +// RegisterBlockEpochNtfn returns a BlockEpochEvent which subscribes the +// caller to receive notificationsm, of each new block connected to the main +// chain. +func (b *BtcdNotifier) RegisterBlockEpochNtfn(targetHeight int32) (*chainntnfs.BlockEpochEvent, error) { + // TODO(roasbeef): implement + return nil, nil +} diff --git a/chainntfs/chainntfs.go b/chainntfs/chainntfs.go index 2effd001..c34de0e7 100644 --- a/chainntfs/chainntfs.go +++ b/chainntfs/chainntfs.go @@ -27,6 +27,12 @@ type ChainNotifier interface { // *seen* on the network, not when it has received a single confirmation. RegisterSpendNtfn(outpoint *wire.OutPoint) (*SpendEvent, error) + // 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) + // Start the ChainNotifier. Once started, the implementation should be // ready, and able to receive notification registrations from clients. Start() error @@ -42,6 +48,8 @@ type ChainNotifier interface { // inputs to funding tx also, consider channel closed if funding tx re-org'd // out and inputs double spent. +// TODO(roasbeef): all chans should be receive only. + // 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 @@ -82,3 +90,17 @@ type SpendDetail struct { type SpendEvent struct { Spend chan *SpendDetail // MUST be buffered. } + +// 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. +}