Commit Graph

55 Commits

Author SHA1 Message Date
Olaoluwa Osuntokun 9c18c3d9a4
chainntnfs: ensure all block epoch notifications are sent *in order*
In this commit, we fix a lingering bug related to the way that we
deliver block epoch notifications to end users. Before this commit, we
would launch a new goroutine for *each block*. This was done in order
to ensure that the notification dispatch wouldn’t block the main
goroutine that was dispatching the notifications. This method archived
the goal, but had a nasty side effect that the goroutines could be
re-ordered during scheduling, meaning that in the case of fast
successive blocks, then notifications would be delivered out of order.
Receiving out of order notifications is either disallowed, or can cause
sub-systems that rely on these notifications to get into weird states.

In order to fix this issue, we’ll no longer launch a new goroutine to
deliver each notification to an awaiting client. Instead, each client
will now gain a concurrent in-order queue for notification delivery.
Due to the internal design of chainntnfs.ConcurrentQueue, the caller
should never block, yet the receivers will receive notifications in
order. This change solves the re-ordering issue and also minimizes the
number of goroutines that we’ll create in order to deliver block epoch
notifications.
2018-02-09 16:13:28 -08:00
Olaoluwa Osuntokun bf6001074c
chainntnfs: fix spend notification registration race condition
In this commit, we fix a race condition related to the way we attempt
to query to see if an outpoint has already been spent by the time it’s
registered within the ChainNotifier. If the transaction creating the
outpoint hasn’t made it into the mempool by the time we execute the
GetTxOut call, then we’ll attempt to query for the transaction itself.
In this case, if we query for the transaction, then the block hash
field will be empty as it hasn’t yet made it into a block. Under the
previous logic, we’d then attempt to force a rescan. This is an issue
as the forced rescan will fail since it’ll try to fetch the block hash
of all zeroes.

In this commit, we fix this issue by only entering this “fallback to
rescan” logic iff, the transaction has actually been mined.
2018-01-28 14:48:56 -08:00
Jim Posen bc7c834362 chainntnfs: Fix stylistic issues. 2017-12-14 19:16:15 -08:00
Jim Posen 280e264e8c chainntnfs: Implement quit signal in TxConfNotifier. 2017-12-14 19:16:15 -08:00
Jim Posen 4405dac4d0 chainntnfs/btcd: Refactor BtcdNotifier to use TxConfNotifier. 2017-12-14 19:16:15 -08:00
Jim Posen 0cac7e80d4 chainntnfs/btcd: Handle block disconnects with chainUpdate.
This does not implement full handling of block disconnections, but
ensures that all chain updates are processed in order.
2017-12-14 19:16:15 -08:00
Olaoluwa Osuntokun 839ce0689e
chainntnfs/btcdnotify: add additional logic when dispatching ntfns 2017-12-07 19:09:53 -08:00
Jim Posen 0297042a8d chainntnfs: Use the new ConcurrentQueue in btcd notifier. 2017-11-16 15:15:22 -08:00
Sam Lewis 1f95b660b9 chainntfns: Fix off by 1 block height error
In the historical dispatch of btcdnotify, the dispatcher checks if a
transaction has been included in a block. If this check happens before
the notifier has processed the update, it's possible that the
currentHeight of the notifier and the currentHeight of the chain might
be out of sync which causes an off by one error when calculating a
target height for the transaction confirmation. This change uses the
height of the block the transaction was found in, rather than the
currentHeight that's known by the notifier to eliminate this.
2017-11-13 22:36:25 -08:00
Sam Lewis 93981a85c0 chainntnfs: Fix btcdnotify dispatch race condition
This race condition can occur if a transaction is included in a block
right when a notification is being added to the notifier for it AND when
the confirmation requires > 1 confirmations. In this case, the
confirmation gets added to the confirmation heap twice.
2017-11-13 22:36:25 -08:00
Olaoluwa Osuntokun 5044cd6468
chainntnfs/btcdnotify: recognize JSON-RPC error in RegisterSpendNtfn
This commit fixes a prior bug in the logic for registering a new spend
notification. Previously, if the transaction wasn’t found in the
mempool or already confirmed within the chain, then
GetRawTransactionVerbose would return an error which would cause the
function itself to exit with an error.

This issue would then cause the server to be unable to start up as the
breach arbiter would be unable to register for spend notifications for
all the channels that it needed to be watching.

We fix this error simply by recognizing the particular JSON-RPC error
that will be returned in this scenario and treating it as a benign
error.
2017-09-12 17:11:47 +02:00
Olaoluwa Osuntokun ed7eae819a
chainntnfs/btcdnotify: don't error if tx not found for historical conf dispatch
This commit fixes a prior mishandled error when attempting historical
confirmation dispatches. In the prior version of this code fragment, if
the transaction under the spotlight wasn’t found within the mempool, or
already in the chain, then an error would be returned by
b.chainConn.GetRawTransactionVerbose, which would case the function to
exit with an error. This behavior was incorrect, as during transaction
re-broadcasts, it was possible for transaction not yet to be a member
of either set.

We fix this issue by ensuring that we treat the JSON error code as a
benign error and continue with the notification registration.
2017-09-12 17:06:58 +02:00
Philip Hayes f0aa186a56 channeldb+utxonursery+lnwire: use lnwire's OutPoint,TxOut serialization 2017-08-25 17:56:50 -07:00
Olaoluwa Osuntokun 9f0efddc20
multi: switch from btcrpcclient to rpcclient 2017-08-24 18:54:24 -07:00
Conner Fromknecht d1e797451d chainntnfs/btcd+neutrino: close spendChan after send 2017-08-03 17:32:44 -07:00
Olaoluwa Osuntokun 399d9c974f
chainntnfs: ensure ntfn cancellation loop will exit
This commit fixes a slight bug introduced. We now ensure that the
cancel loop always exists if the ChainNotifier has been signaled for a
quit.
2017-08-01 17:14:11 -07:00
Conner Fromknecht 9f85eadde1 chainntnfs/btcd+neutrino: sync epoch cancel 2017-07-31 21:44:23 -07:00
Conner Fromknecht a9b1af4c73 chainntnfs/btcd+neurtino: unify + sync ntfn cancels 2017-07-31 21:44:23 -07:00
Olaoluwa Osuntokun c47047c0b8
chainntnfs: extend interface test to ensure initial conf height is correct 2017-07-11 17:38:43 -07:00
Olaoluwa Osuntokun 54cb8a05cd
chainntnfs: fix vet error, don't pass lock by value 2017-06-07 17:07:33 -07:00
Olaoluwa Osuntokun 625d57aea6
chainntnfs/btcdnotify: ensure block epoch coroutines exit before closing ntfn channel 2017-06-07 17:04:32 -07:00
Olaoluwa Osuntokun 0050108391
chainntnfs/btcdnotify: fix off-by one error when setting spending height 2017-06-05 19:07:48 -07:00
Olaoluwa Osuntokun 49e27c77dd
chainntnfs/btcdnotify: modify logging level from error to warn 2017-05-16 18:46:55 -07:00
Olaoluwa Osuntokun dfd9f6d1ca
chainntnfs/btcdnotify: fix historical confirmation dispatch bugs
This commit fixes to distinct bugs in the way we previously dipatched
notifications for transactions which needed a historical dispatch.

Previously we would compare transactions when scanning the block using
the `tx.Hash` field. This was incorrect has the `Hash` field is
actually the wtxid, not the txid which should be the item being
compared. We fix this within the second bug fix by actually using the
txid to find the proper transaction.

The second fix has to due with a slight race condition which led to an
off-by-one error when dispatching the historical confirmation. If while
we were dispatching the confirmation, a new block was found, then we
could calculate the wrong block height (off by one) as we were using
the ‘currentHeight’ instead the height of the block which included the
transaction.
2017-05-15 17:47:07 -07:00
Olaoluwa Osuntokun 2f0639f1af
chainntnfs: add heightHint to RegisterSpendNtfn+ RegisterConfirmationsNtfn
This commit modifies two of the main methods in the ChainNotifier
interface to be more light client friendly. In order to do so, we now
tack on an extra parameter to the methods: heightHint. This value
represents the earliest known height that the chain should be scanned
when attempting to do a dispatch from historical data.

All tests have also been updated to use these new parameters properly
when excising the expected behavior of each interface implementation.
2017-05-11 15:20:38 -07:00
Olaoluwa Osuntokun 45bbeb8f84
chainntnfs/btcdnotify: properly include SpendingHeight in SpendDetails
This commit modifies the btcdnotify implementation of the ChainNotifier
interface to properly include the height in which the watched output
was spent in the SpendDetail sent as a notification.

The set of tests have also been updated to assert that the proper
spending height is included in received notification.
2017-05-11 15:20:36 -07:00
Olaoluwa Osuntokun 4b15310c08
chainntfns/btcdnotify: eliminate block epoch race condition, use diff cancel channel
This commit fixes a race condition that was uncovered by the race
condition detector surrounding cancelling active block epoch
notifications. Previously we would close the main notification channel
for each client, at tine this would cause a read/write race condition
if an active grouting was attempting to dispatch a notification. We now
fix this use by using a distinct channel for signaling cancellation to
the active grouting, and another to signal cancellation to any
notification observers.
2017-05-05 15:53:20 -07:00
Olaoluwa Osuntokun a9b8da7c3f
chainntfns/btcdnotify: log error when unable to query for transaction 2017-05-04 17:38:52 -07:00
Olaoluwa Osuntokun c575107607
chaintnfs/btcdnotify: make cancellation of block epoch synchronous
This commit is meant to fix an occasional flake in the interrogation
tests cause by the async nature of the cancellation of block epoch
notifications. This commit modifies the cancellation to now be fully
synchronous which should eliminate this flake.
2017-04-26 21:17:35 -07:00
Olaoluwa Osuntokun 419c2ac206
chainntnfs/btcdnotify: fix race condition for block epoch clients
This commit fixes a race condition that was introduced while fixing a
lingering bug in the logic to notify block epoch clients. The race
condition would happen as by removing the default case in the select
statement, it was now possible for the client’s block epoch client to
be closed while the routine was attempting a send on it.

We now eliminate this race condition possibility by adding a wait group
to all goroutines launched to dispatch a block epoch notification. With
this modification, the Stop() goroutine will now wait for all other
goroutine to exit before closing the block epoch channels of all
currently registered clients.
2017-04-04 14:20:16 +02:00
Olaoluwa Osuntokun 608b9d96d1
chainntnfs/btcdnotify: fix dropped block epoch notification bug
This commit a bug introduced in the chain notifier while we were
limiting the usage of mutexes within the package. In a prior commit a
default case was introduced in the select statement in order to avoid
the possibility of the main goroutine blocking when dispatching block
epoch notification.

In order to avoid this potentially disastrous bug, we now instead
launch a new goroutine for each client to ensure that all notifications
are reliably dispatched.
2017-04-01 20:13:58 +02:00
Andrey Samokhvalov ee2379775c lnd: fix golint warning which requires to add additional comments 2017-03-13 16:30:23 -07:00
Andrey Samokhvalov fd97a4bd19 lnd: partially fix golint warnings 2017-03-13 16:30:23 -07:00
Andrey Samokhvalov 143a6e01bb lnd: fix unconvert warnings 2017-03-13 16:30:23 -07:00
Olaoluwa Osuntokun 6c81dfad61
chainntfns/btcdnotifier: fix race condition in notifyBlockEpochs
This commit fixes a race condition in the notifyBlockEpochs detected by
the race condition detector. Previously the notifyBlockEpochs function
could cause a race condition when a new caller was either cancelling an
existing notification intent or creating a new one.

We fix this issue by making the call to notifyBlockEpochs synchronous
rather than asynchronous. An alternative would be to add a mutex
guarding the map state. The channel itself is buffered with a good
margin, so there shouldn’t be a huge impact.
2017-02-22 14:51:48 -08:00
Olaoluwa Osuntokun 73cc28d5fb
chainntnfs/btcdnotify: implement spend+epoch ntfn cancellations
This commit minifies the BtcdNotifier concrete implementation of the
ChainNotifier interface to allow callers to optionally cancel an
outstanding block epoch or spend notificaiton intent.

To do this efficiently, we now give each notification intent a unique
ID based on if it’s an epoch intent or a spend intent. We then use this
ID to reference back to the original un-dispatched notification intent
when the caller wishes to cancel the intent.
2017-02-21 01:42:53 -08:00
Olaoluwa Osuntokun 384fe61e73
multi: fix `go vet` warnings throughout code base 2017-02-16 19:33:19 +08:00
Christopher Jämthagen cc4617ca23 chainntnfs: break out of loop once txIndex is found 2017-01-11 15:51:19 -08:00
bryanvu 35cf21733d chainntnfs: exit notifyBlockEpochs upon shutdown
On restarts, notifyBlockEpochs would intermittently attempt to send new
block epoch notifications to clients that had already been shut down,
causing a “send on closed channel” error. This change exits
notifyBlockEpochs upon shutdown so as to prevent this.
2017-01-07 19:39:01 -08:00
Olaoluwa Osuntokun 5affed38fc
multi: update btcsuite API's to latest upstream changes
This commit makes a large number of minor changes concerning API usage
within the deamon to match the latest version on the upstream btcsuite
libraries.

The major changes are the switch from wire.ShaHash to chainhash.Hash,
and that wire.NewMsgTx() now takes a paramter indicating the version of
the transaction to be created.
2017-01-05 13:56:34 -08:00
bryanvu aa04f82a15 utxonursery: added persistence to transaction output states
Moved transaction states from in-memory maps to persistent BoltDB
buckets. This allows channel force closes to operate reliably if the
daemon is shut down and restarted at any point during the forced
channel closure process.
2017-01-03 16:23:07 -08:00
bryanvu 90ed23e6aa chainntnfs: fixed off-by-one in attemptHistoricalDispatch 2017-01-03 16:23:07 -08:00
bryanvu def39799c5 chainntnfs: handling for potential deadlocks during system shutdown
If the lnd daemon is shut down while multiple subsystems are attempting
to register for notifications, the blocking of those chain notifier
registrations may cause the daemon shutdown to deadlock. The additions
in this commit allow the registration functions to return errors rather
than potentially deadlock when the chain notifier is shut down.
2017-01-03 16:23:07 -08:00
Olaoluwa Osuntokun f8711659e6
chainntnfs/btcdnotify: populate full conf details for historical dispatch
This commit modifies the historical dispatch workflow slightly to also
obtain the full block in which the transaction was confirmed we we can
fully populate the full TxConfirmations struct which was recently added
as part of the confirmation subscription API.

With this change, confirmation triggers that a reached while the demon
is down, will now be deliver exactly as if the trigger was reached
while the daemon was up.
2016-12-27 16:44:08 -08:00
Olaoluwa Osuntokun a09db60a1f
chainntnfs/btcdnotify: update BtcdNotifer to recent ChainNotifier API change
This commit updates the BtcdNotifier implementation of the
ChainNotifier by including the details of a transaction’s confirmation
within the ConfirmationEvent struct sent once a registered txid has
reached a sufficient number of confirmation.
2016-12-27 16:43:58 -08:00
Olaoluwa Osuntokun f29b496b76
chainntfns/btcdnotify: close channels for all epoch clients on shutdown
This commit modifies the Stop method of the default ChainNotifier
client, the BtcdNotifier. We now close the notificaiton channel for all
the currently active block epoch clients in order to give clients a
signal that the entire daemon and possibly the ChainNotifier is
shutting down. This gives clients an extra signal to more thoroughly
implement a graceful shutdown across the daemon.
2016-12-15 13:53:46 -08:00
Olaoluwa Osuntokun ccf71100fd
chainntfns/btcdnotify: update to newest btcrpcclient interface 2016-12-12 15:57:03 -08:00
Olaoluwa Osuntokun 1e3635b5aa
chainntnfs: properly account for partial historical confirmation dispatch
This commit modifies the recently added logic to the ChainNotifier to:
  1. Fix the off-by-one confirmation error that was missed due a flaky
test
  2. Ensure that partial historical confirmations are properly handled.

The partial hostile confirmation case arises when a transaction already
a non-zero number of confirmations when the notification is registered,
but less than what would trigger the confirmation notification. To fix
this, transaction which have a partial number of confirmation are now
properly inserted into the confHeap, skipping first first phase for
notifications.
2016-12-08 16:16:29 -08:00
bryanvu 909c3f8df9 chainntfs: add checks for previously confirmed transactions and spends
Without these checks, “zombie” notification requests that would never
be dispatched could be registered. This would occur if notification
requests were made for events (transaction confirmation and output
spent) that had already been recorded on the blockchain.
2016-12-08 12:28:15 -08:00
Olaoluwa Osuntokun d1f12627d2
chainntfns/btcdnotify: add support for multi-dispatch spend notifications
This commit adds support for dispatching the same spend notification to
multiple clients. This is now required by the ChainNotiifer interface
documentation and will be needed within the daemon in order to support
some upcoming refactors.
2016-11-27 19:17:34 -08:00