Commit Graph

171 Commits

Author SHA1 Message Date
Olaoluwa Osuntokun
5dc0d669a6
contractcourt: watch proper output within htlcOutgoingContestResolver
In this commit, we fix an existing bug in the implementation of the
resolution of the htlcOutgoingContestResolver. Before this commit, we
would _always_ watch the claim outpoint. However, if this is on the
remote party’s commitment transaction, then we would end up watching
the wrong output. We’ll now properly detect this by modifying which
output we watch, based on if we have a second level transaction or not.
2018-01-23 14:16:22 -08:00
Olaoluwa Osuntokun
f8adab1f1c
test: add comprehensive integration tests for on-chain HTLC handling
In this commit, we add 6 new integration tests to test the various
actions that may need to be performed when either side goes on-chain to
fully resolve HTLC’s. Many of the tests are mirrors of each other as
they test sweeping/resolving HTLC’s from both commitment transactions.
2018-01-22 19:20:02 -08:00
Olaoluwa Osuntokun
73641d222f
contractcourt: properly use the broadcast state number during breach remedy 2018-01-22 19:19:58 -08:00
Olaoluwa Osuntokun
5df6704a9c
contractcourt: make synchronous chain watcher notifications optional
In this commit, we modify the way that notifications are dispatched
within the chainWatcher. Before we would *always* wait for an ack back
before we started to clean up he database state. This would at times
lead to deadlocks. To remedy this, we now allow callers to decide if
they want notifications to be sync or not. The only current caller that
requires this is the breach arbiter.
2018-01-22 19:19:58 -08:00
Olaoluwa Osuntokun
783f01e1c7
multi: fix linter warnings 2018-01-22 19:19:56 -08:00
Olaoluwa Osuntokun
91d45b37ba
contractcourt: on startup, launch a goroutine to finalize co-op chan close if needed 2018-01-22 19:19:54 -08:00
Olaoluwa Osuntokun
db2b9fd9ff
contractcourt: remove the existing ManuallyResolveChannel methiod
We’re removing this method as it’s no longer needed, since the
chainWatcher will properly handle on-chain co-op closes.
2018-01-22 19:19:54 -08:00
Olaoluwa Osuntokun
3ec83cc82f
peer+contractcourt: delegate watching for co-op closes to the chainWatcher
In this commit, we modify the interaction between the chanCloser
sub-system and the chain notifier all together. This fixes a series of
bugs as before this commit, we wouldn’t be able to detect if the remote
party actually broadcasted *any* of the transactions that we signed off
upon. This would be rejected to the user by having a “zombie” channel
close that would never actually be resolved.

Rather than the chanCloser watching for on-chain closes, we’ll now open
up a co-op close context to the chainWatcher (via a layer of
indirection via the ChainArbitrator), and report to it all possible
closes that we’ve signed. The chainWatcher will then be able to launch
a goroutine to properly update the database state once any of the
possible closure transactions confirms.
2018-01-22 19:19:53 -08:00
Olaoluwa Osuntokun
bbca53507f
contractcourt: extend the ChainArbitratorConfig with IsOurAddress closure
In this commit, we add the IsOurAddress field into the config of the
chain arb. With this new function closure, the chain arb is able to
detect co-op on chain closes automatically.
2018-01-22 19:19:53 -08:00
Olaoluwa Osuntokun
bdbb33344a
contractcourt: extend resolveContract to also stop any active chainWatcher 2018-01-22 19:19:52 -08:00
Olaoluwa Osuntokun
723bfb0eac
contractcourt: channel arbitrators now exit on co-op close of the channel 2018-01-22 19:19:52 -08:00
Olaoluwa Osuntokun
62f951a969
contractcourt: extend the chainWatcher to be able to detect co-op closes
In this commit, we extend the chainWatcher to be able to automatically
detect co-op closes of the channel. With this change, it’s now fully
encompassed so able to detect all types of closes on-chain. We detect a
co-op close due to the sequence number being finalized, as well as
paying to us directly in a regular p2wkh-like output.
2018-01-22 19:19:52 -08:00
Olaoluwa Osuntokun
b5ae0855d2
contractcourt: add new SubscribeChannelEvents method to ChainArbitrator
In this commit, we add a new method to allow external sub-systems to
gain an intent to receive notifications once an on-chain event happens.
This will be used in place of the old channel signals directly on the
channel state machine object in a series of follow up commits.
2018-01-22 19:19:49 -08:00
Olaoluwa Osuntokun
754d1c1c38
contractcourt: when handling a remote force close, use their view of the HTLC's 2018-01-22 19:19:49 -08:00
Olaoluwa Osuntokun
63f7bf4e65
contractcourt: integrate notifications of the chainWatcher with each channel arb
In this commit, we modify the construction of the channel arbitrator to
accept a pointer to an event stream from the chain watcher that’s been
assigned to that channel. As a result, we no longer need a fresh
unilateral close signal, as the one we get from the chain watcher will
*always* be up to date.

For each active channel, we’ll now create a chainWatcher instance that
will be around until the channel is fully closed on chain.
2018-01-22 19:19:48 -08:00
Olaoluwa Osuntokun
0e14ac2063
contractcourt: add new chainWatcher struct
In this commit, we add a new struct to the package, the chainWatcher.
The duty of this struct is to replace the functionality that was
previously implemented by the closeObserver of each channel. Rather
than the source of notification being tied to the lifetime of a
particular object, it’s now delegated to a persistent object that will
be around for the entire lifetime of the channel (until it’s closed).
This will serve to greatly simplify the code, and eliminate a large
class of bugs.
2018-01-22 19:19:48 -08:00
Olaoluwa Osuntokun
31aa7265b7
contractcourt: add new ChainArbitrator struct as central coordinator of package
In this commit, we add the ChainArbitrator struct. The ChainArbitrator
is a special sub-system that will oversee the on-chain resolution of
all active channels, and also channels that are in the pending close
state. The ChainArbitrator maintains a set of ChannelArbitrators, one
for each channel that hasn’t yet been fully resolved.

Outside sub-systems should send new channels to the arbitrator once
they’ve opened. Additionally, they can also trigger manual
interventions to close out a channel on chain forcibly, or just to
signal that a channel has been closed cooperatively.

Finally, (for now) the ChainArbitrator should be notified once a fresh
set of signals for a channel becomes available. The ChannelArbitrator
for the channel will use these set of signals to be notified when an
on-chain event happens.
2018-01-22 19:19:36 -08:00
Olaoluwa Osuntokun
09b6bee8d4
contractcourt: add complete ContractResolver implementations
In this commit, we introduce a new interface, the ContractResolver. The
duty of a ContractResolver is to watch a contract on-chain, for all
possible transitions, and exit finally when the contract has been fully
resolved. Resolvers themselves can be recursive: meaning producing
another resolver to hand off the duties require to fully resolve a
contract.

Each resolver also has a ResolverKit which contains all the function
closures and interfaces that the resolver need to properly do its job.

The 5 types of resolvers are:
  * outgoing HTLC timeout
  * outgoing HTLC contested
  * incoming HTLC know presage
  * incoming HTLC contested (don’t yet know)
  * commitment sweep

In the future, more advanced resolver types can be added as required.
2018-01-22 19:19:36 -08:00
Olaoluwa Osuntokun
701eb9d4f4
contractcourt: add new briefcase.go file to house persistent arbitrator state
In this commit, we add a new file: briefcase.go. The contents of this
file are the ArbitratorLog. This log will be used by the internal state
machine of each Channel Arbitrator to ensure that each state transition
is fully reflected on-disk, to ensure that the state machine is durable
and able to survive restarts.

This commit also adds a new implementation of the ArbitratorLog
interface backed by boltdb.
2018-01-22 19:19:35 -08:00
Olaoluwa Osuntokun
d64ffcb6c8
contractcourt: add new ChannelArbitrator struct
In this commit, we add the primary struct of the package with a full
implementation. The duty of the ChannelArbitrator is to watch the set
of active contracts on a comment transaction and act accordingly if any
of their redemption criteria have been met. Potential criteria include:
an HTLC about to time out, and HTLC about to time out that we know the
preiamge to, or the remote party going to chain (forcing us to resolve
all pending contracts on chain).

The primary goroutine of this struct implements a persistent state
machine in order to ensure that mid contract resolution, we’re able to
properly survive restarts without losing our place, or forgetting about
a pending contract.

A ChannelArbitrator will stay alive until all contracts have been fully
resolved. This means that outside sub-systems no longer need to worry
about remembering to mark a channel as fully resolved, as it’s the job
of the ChannelArbitrator to do this task.
2018-01-22 19:19:34 -08:00
Olaoluwa Osuntokun
71009438b6
contractcourt: create new package, add logger 2018-01-22 19:19:34 -08:00