Commit Graph

292 Commits

Author SHA1 Message Date
Olaoluwa Osuntokun
b491488c6f
contractcourt: update to new ChanSyncMsg API, reflow comments where needed 2019-03-28 17:53:57 -07:00
Jim Posen
d107627145
contractcourt: remove broadcastRedeemMultiplier
The multiplier doesn't make sense because funds may be equally at risk
by failing to broadcast to chain regardless of whether the HTLC is a
redeem or a timeout.
2019-03-26 14:31:51 +01:00
Olaoluwa Osuntokun
c7ca387a9d
Merge pull request #2744 from cfromknecht/disable-before-close
cnct+chancloser: disable channel before closing
2019-03-21 21:08:23 -07:00
Olaoluwa Osuntokun
db411c244e
contractcourt: add new set of tests in htlcTimeoutResolver to exercise refactorings 2019-03-19 16:50:28 -07:00
Olaoluwa Osuntokun
6b24b6dabd
contractcourt: simplify htlcTimeoutResolver, unify with HTLC contest logic
In this commit, we simplify the existing `htlcTImeoutResolver` with some
newly refactored out methods from the `htlcTimeoutContestResolver`. The
resulting logic is easier to follow as it's more linear, and only deals
with spend notifications rather than both spend _and_ confirmation
notifications.
2019-03-19 16:50:27 -07:00
Olaoluwa Osuntokun
e1a07b68e8
contractcourt: extract which timeout HTLC output to watch into new method 2019-03-19 16:50:09 -07:00
Olaoluwa Osuntokun
b078cea83f contractcourt: move claimCleanUp from contest resolver to timeout 2019-03-19 16:49:56 -07:00
Olaoluwa Osuntokun
aa1cd04dbf
Merge pull request #2022 from joostjager/holdinvoice
htlcswitch: hodl invoice
2019-03-15 13:26:53 -07:00
Olaoluwa Osuntokun
5ef95a51dc
Merge pull request #2766 from Roasbeef/chain-watcher-fix-off-by-one
contractcourt: fix off-by-one error in closeObserver
2019-03-15 13:26:08 -07:00
Joost Jager
32f2b047e8
htlcswitch: hodl invoice
This commit modifies the invoice registry to handle invoices for which
the preimage is not known yet (hodl invoices). In that case, the
resolution channel passed in from links and resolvers is stored until we
either learn the preimage or want to cancel the htlc.
2019-03-15 10:09:17 +01:00
Joost Jager
1f41a2abce
htlcswitch: abtract invoice from link
This commit detaches signaling the invoice registry that an htlc was
locked in from the actually settling of the htlc.

It is a preparation for hodl invoices.
2019-03-15 10:09:15 +01:00
Joost Jager
449c3d533e
contractcourt: add access to full invoice registry from resolvers
Previously a function pointer was passed to chain arbitrator to avoid a
circular dependency. Now that the routetypes package exists, we can pass
the full invoice registry to chain arbitrator.

This is a preparation to be able to use other invoice registry methods
in contract resolvers.
2019-03-15 10:08:35 +01:00
Wilmer Paulino
7946d0a256
multi: remove ErrDoubleSpend check for PublishTransaction
In this commit, we address a lingering issue within some subsystems that
are responsible for broadcasting transactions. Previously,
ErrDoubleSpend indicated that a transaction was already included in the
mempool/chain. This error was then modified to actually be returned for
conflicting transactions, but its callers were not modified accordingly.
This would lead to conflicting transactions to be interpreted as valid,
when they shouldn't be.
2019-03-13 17:57:19 -07:00
Olaoluwa Osuntokun
6983a9e146
contractcourt: fix off-by-one error in closeObserver case
In this commit, we fix an off-by-one error when handling force closes
from the remote party. Before this commit, if the remote party
broadcasts state 2, and we were on state 1, then we wouldn't act at all.
This is due to an extraneous +1 in the comparison, causing us to only
detect this DLP case if the remote party's state is two beyond what we
know atm. Before this commit, the test added in the prior commit failed.
2019-03-13 17:31:23 -07:00
Olaoluwa Osuntokun
795c9f1550
contractcourt: add new TestChainWatcherDataLossProtect test case
In this commit, we add a new test case to exercise the way we handle the
DLP detection and dispatch within the chain watcher. Briefly, we use
the `testing/quick` package to ensure that the following invariant is
always held: "if we do N state updates, then state M is broadcast, iff M
> N, we'll execute the DLP protocol". We limit the number of iterations
to 10 for now, as the tests can take a bit of time to execute, since it
actually does proper state transitions.
2019-03-13 17:31:21 -07:00
Olaoluwa Osuntokun
48532dc9f6
contractcourt: abstract call to GetStateNumHint within the closeObserver
In this commit, we abstract the call to `GetStateNumHint` within the
`closeObserver` method to a function closure in the primary config. This
allows us to feed in an arbitrary broadcast state number within unit
tests.
2019-03-13 17:31:20 -07:00
Conner Fromknecht
325d77c431
chancloser+cnct: disable channel before closing 2019-03-12 17:34:28 -07:00
Olaoluwa Osuntokun
bc72691806
contractcourt: mark channel as borked before removing the link
In this commit, we ensure that we mark the channel as borked before we
remove the link during the force close process. This ensures that if the
peer reconnects right after we remove the link, then it won't be loaded
into memory in `loadActiveChannels`. We'll now:

  * mark the channel as borked
  * remove the link
  * read the channel state from disk
  * force close

This ensures that the link (if it's active) is able to commit any
pending changes to disk before we read out the channel to force close.
2019-03-08 19:15:08 -08:00
Joost Jager
f23fdf95e7
cnct: fix error returning bug
The wrong error variable was returned, causing a resolution failure to
be interpreted by the channel arbitrator as a success.
2019-02-26 12:56:21 -03:00
Conner Fromknecht
29f07a58cb
cnct+lnwl+hswc: use lntypes.Preimage for witness beacon 2019-02-19 17:06:00 -08:00
Conner Fromknecht
30f61b7630
multi: make AddPreimage variadic, optimistically compute key
In this commit, we modify the WitnessCache's
AddPreimage method to accept a variadic number
of preimages. This enables callers to batch
preimage writes in performance critical areas
of the codebase, e.g. the htlcswitch.

Additionally, we lift the computation of the
witnesses' keys outside of the db transaction.
This saves us from having to do hashing inside
and blocking other callers, and limits extraneous
blocking at the call site.
2019-02-19 17:05:04 -08:00
Valentine Wallace
10090a978d contractcourt/chain_arbitrator: notify the ChannelNotifier on newly closed channels. 2019-02-05 18:17:54 -08:00
Olaoluwa Osuntokun
8aecccf266
Merge pull request #2407 from joostjager/commit-resolver-sweeper
cnct: use sweeper in commit resolver
2019-02-01 17:34:51 -08:00
Olaoluwa Osuntokun
4af857f0c6
Merge pull request #2356 from joostjager/invoices-subserver
invoices: add subscribesingleinvoice
2019-02-01 17:19:58 -08:00
Joost Jager
bacd92418a
invoices: use lntypes.Hash and lntypes.Preimage
Previously chainhash.Hash was used, which converts to/from string in
reversed format. Payment hashes and preimages are supposed to be
non-reversed.
2019-02-01 09:42:29 +01:00
Joost Jager
55aee9c703
cnct: expose non-incubating htlcs after channel force close
In this commit we fix a reporting gap that previously existed for htlcs
that were still contested.
2019-02-01 09:36:47 +01:00
Joost Jager
e486340106
cnct: use sweeper in commit resolver
Now that the sweeper is available, it isn't necessary anymore for the
commit resolver to craft its own sweep tx. Instead it can offer its
input to the sweeper and wait for the outcome.
2019-02-01 09:20:45 +01:00
Joost Jager
9abe06f3c8
cnct: finish channel arbitrator in StateFullyResolved
Previously the arbitrator wasn't advanced to the final stage after
the last contract resolved.

Also channel arbitrator now does not ignore a log error anymore
unresolved contracts cannot be retrieved.
2019-02-01 09:10:04 +01:00
Joost Jager
9e012ecc93
multi: move Input interface and related code
This commit is a step to split the lnwallet package. It puts the Input
interface and implementations in a separate package along with all their
dependencies from lnwallet.
2019-01-31 13:25:33 +01:00
Olaoluwa Osuntokun
4fd1f832d7
contractcourt+rpc: use new FetchChannel method instead of scanning
In this commit, we modify areas where we need to force close a channel
to use the new FetchChannel method instead of manually scanning. This
dramatically reduces the CPU usage when doing things like closing a
large number of channels within lnd.
2019-01-28 20:24:45 -08:00
Wilmer Paulino
41f638c7cf
contractcourt+server: settle invoice for on-chain HTLC sweep
In this commit, we extend the htlcSuccessResolver to settle the invoice,
if any, of the corresponding on-chain HTLC sweep. This ensures that the
invoice state is consistent as when claiming the HTLC "off-chain".
2019-01-22 20:46:14 -08:00
Wilmer Paulino
974e0f2df5
cnct: make original htlc amt available for contract resolvers
Previously, contract resolvers that needed to publish a second level tx,
did not have access to the original htlc amount.

This commit reconstructs this amount from data that is already persisted
in arbitrator log.

Co-authored-by: Joost Jager <joost.jager@gmail.com>
2019-01-22 20:47:17 -08:00
Wilmer Paulino
93754f8d37
lnwallet: add HtlcPoint() resolution helper methods
Co-authored-by: Joost Jager <joost.jager@gmail.com>
2019-01-22 20:47:13 -08:00
Wilmer Paulino
865f7568d6
cnct: extract relaunching of resolvers to method
Co-authored-by: Joost Jager <joost.jager@gmail.com>
2019-01-22 20:46:14 -08:00
Joost Jager
4ec3fc3b1a
cnct: split resolvers in separate files 2019-01-16 20:03:59 +01:00
Olaoluwa Osuntokun
509bed614c
Merge pull request #2198 from Roasbeef/sendall-rpc
multi: add ability to sweep all coins in the the wallet to an addr to sendcoins
2019-01-15 14:49:17 -08:00
Olaoluwa Osuntokun
400a1d3282
Merge pull request #2431 from cfromknecht/keyring-remote-delay-retribution
lnwallet: Add keyring and remote CSV delay to BreachRetribution
2019-01-11 15:33:16 -08:00
Conner Fromknecht
eb2f5cecf6
multi: remove breach tx arg from NewBreachRetribution args
This commit removes the breach transaction from the
arguments passed to NewBreachRetribution. We already
keep all prior remote commitments on disk in the
commitment log, and load that transaction from disk
inside the method. In practice, the one loaded from
disk will be the same one that is passed in by the
caller, so there should be no change in behavior
as we've already derived the appropriate state number.

This changes makes integration with the watchtower
client simpler, since we no longer need to acquire
the breaching commitment transaction to be able to
construct the BreachRetribution. This simplifies
not only the logic surrounding transient backsups,
but also on startup (and later, retroactively
backing up historic updates).
2019-01-10 13:13:38 -08:00
Olaoluwa Osuntokun
73c9c2ee15
sweep+cnct+nursery+rpc: extract DetermineFeePerKw to func, add FeePreference
In this commit, we extract the existing determineFeePerKw method on the
RPC server into a new file in the sweep package. Along the way, we
consolidate code by introducing a new FeePreference struct, which allows
the caller to express their fee preference either in blocks to
confirmation, or a direct fee rate. This move takes a small step to
father decoupling calls in the main RPC server.
2019-01-09 15:55:17 -08:00
Conner Fromknecht
088fb33718
contractcourt: nil keyring curves on breach info 2019-01-07 17:15:45 -08:00
Joost Jager
687d4e7725
cnct: add todo comment for sweeper 2018-12-18 10:50:22 +01:00
Joost Jager
e43e89514f
sweep+utxonursery+cnct+breacharbiter: add height hint to input
This commit is a preparation for the implementation of remote spend
detection. Remote spends may happen before we broadcast our own sweep
tx. This calls for accurate height hints.
2018-12-18 10:50:11 +01:00
Olaoluwa Osuntokun
febe6cd47f
Merge pull request #2131 from wpaulino/force-close-same-channel
contractcourt/chain_arbitrator: prevent force closing same channel twice
2018-12-17 21:23:57 -08:00
Olaoluwa Osuntokun
fa160f559c
multi: replace per channel sigPool with global daemon level sigPool
In this commit, we remove the per channel `sigPool` within the
`lnwallet.LightningChannel` struct. With this change, we ensure that as
the number of channels grows, the number of gouroutines idling in the
sigPool stays constant. It's the case that currently on the daemon, most
channels are likely inactive, with only a hand full actually
consistently carrying out channel updates. As a result, this change
should reduce the amount of idle CPU usage, as we have less active
goroutines in select loops.

In order to make this change, the `SigPool` itself has been publicly
exported such that outside callers can make a `SigPool` and pass it into
newly created channels. Since the sig pool now lives outside the
channel, we were also able to do away with the Stop() method on the
channel all together.

Finally, the server is the sub-system that is currently responsible for
managing the `SigPool` within lnd.
2018-12-16 15:40:14 -08:00
Wilmer Paulino
60036aca1d
contractcourt/channel_arbitrator: prevent force closing same channel twice
In this commit, we prevent the ChainArbitrator from sending a force
close request for a channel if it has previously already sent one. We do
this to prevent blocking the caller of ForceCloseContract.
2018-12-10 20:37:58 -08:00
Olaoluwa Osuntokun
1fd3aac925
multi: switch from bolt packge to bbolt package for all imports 2018-11-29 20:33:49 -08:00
Johan T. Halseth
9e81b1fe53
chain_watcher: poll for commit point in case of failure
We pool the database for the channel commit point with an exponential
backoff. This is meant to handle the case where we are in process of
handling a channel sync, and the case where we detect a channel close
and must wait for the peer to come online to start channel sync before
we can proceed.
2018-11-21 10:28:57 +01:00
Johan T. Halseth
a9f93b2988
contractcourt/chain_watcher: add channel sync message to CloseChannelSummary 2018-11-21 10:28:56 +01:00
Joost Jager
839752857f
cnct: add comment on expiry calculation 2018-11-07 15:29:57 +01:00
Joost Jager
c3560932b6
cnct: fix log line 2018-11-07 15:29:56 +01:00
Conner Fromknecht
f9cec4a67b
cnct/contract_resolvers: propagate checkpoint failures 2018-10-24 13:57:10 -07:00
Conner Fromknecht
f957b78c0b
cnct/contract_resolvers: ignore duplicate publication error 2018-10-24 13:57:10 -07:00
Conner Fromknecht
682c4c96c7
cnct/contract_resolvers: reliably publish htlc success sweep 2018-10-24 13:57:10 -07:00
Conner Fromknecht
b7aebd92cf
cnct/contract_resolvers: reliably publish commit sweep 2018-10-24 13:57:10 -07:00
Olaoluwa Osuntokun
fc21bf091a
multi: modify sweeper.CreateSweepTx to accept conf target, style changes
In this commit, we modify the newly introduced UtxoSweeper.CreateSweepTx
to accept the confirmation target as a param of the method rather than a
struct level variable. We do this as this allows each caller to decide
at sweep time, what the fee rate should be, rather than using a global
value that is meant to work in all scenarios. For example, anytime
we're sweeping an output with a CLTV lock that's has a dependant
transaction we need to sweep/cancel, we may require a higher fee rate
than a regular force close with a CSV output.
2018-10-18 18:08:27 -07:00
Joost Jager
6977d59e35
cnct: reuse sweep tx logic for success resolver 2018-10-17 12:44:34 +02:00
Joost Jager
c1d845aa0d
cnct: reuse sweep tx logic for commit resolver
Removes duplicate sweep tx code from commit resolver
and delegates generation to UtxoSweeper in the sweep
package.
2018-10-17 12:44:34 +02:00
Conner Fromknecht
59b459674d
multi: init subsystem loggers via build pkg 2018-10-05 13:04:45 +09:00
Olaoluwa Osuntokun
6afee3d099
Merge pull request #1856 from maurycy/typos
multi: fix various typos in comments
2018-09-27 20:38:10 -07:00
Olaoluwa Osuntokun
16ec80e163
contractcourt: update tests to use new IncubateOutputs API 2018-09-25 21:49:10 -07:00
Olaoluwa Osuntokun
1494c9e67f
Merge pull request #1952 from joostjager/chanarb-test
cnct: add channel_arbitor htlcOutgoingContestResolver test
2018-09-25 21:05:43 -07:00
Joost Jager
f34ffab9ba
cnct: add channel_arbitor test that exercises the htlcOutgoingContestResolver 2018-09-20 18:17:23 -07:00
Johan T. Halseth
3a67fe2552
contractcourt+server: make IncubateOutputs take broadcastHeight 2018-09-20 12:54:48 +02:00
Conner Fromknecht
0c74575c44
contractcourt/chain_arbitrator: parallel chain watcher start
This commit restructures the initialization procedure
for chain watchers such that they can proceed in parallel.
This is primarily to help nodes running with the neutrino
backend, which otherwise forces a serial rescan for each
active channel to check for spentness.

Doing so allows the rescans to take advantage of batch
scheduling in registering for the spend notifications,
ensuring that only one or two passes are made, as opposed
to one for each channel.

Lastly, this commit ensures that the chain arb is properly
shutdown if any of it's chain watchers or channel arbs
fails to start, so as to cancel their goroutines before
exiting.
2018-09-11 16:10:29 -07:00
maurycy
ac24b12bf2
multi: fix various typos in comments 2018-09-07 06:51:49 +02:00
Conner Fromknecht
d706e40ff7
contractcourt/channel_arbitrator: handle onchain close race on restart 2018-09-03 23:12:57 -07:00
Johan T. Halseth
8344b05c38
contractcourt/chanarb test: expand TestChannelArbitratorCommitFailure to coop and local force close 2018-08-23 10:04:43 +02:00
Johan T. Halseth
4933120d38
contractcourt/channel arbitrator: add coopCloseTrigger on startup for pendingClose channel 2018-08-23 10:04:43 +02:00
Johan T. Halseth
bb71856204
contractcourt/channel_arbitrator test: add TestChannelArbitratorEmptyResolutions 2018-08-23 00:03:22 +02:00
Johan T. Halseth
ed5db96e05
contractcourt/channel_arbittrator: ignore noResolutions error on startup 2018-08-23 00:03:22 +02:00
Johan T. Halseth
b031d87daf
contractcourt/briefcase: test FetchContractResolutions error when no resolutions logged 2018-08-23 00:03:22 +02:00
Johan T. Halseth
53286b8cee
contractcourt test: add TestChannelArbitratorCommitFailure 2018-08-23 00:03:22 +02:00
Johan T. Halseth
f2a033e965
contractcourt/channel_arbitrator: override trigger on startup if IsClosing
At ChannelArbitrator startup we now check the database close status of
the channel. If we detect that the channel is closed, but our state
machine hasn't advanced to reflect that (possibly because of a shutdown
before the state transition was finished), we manually trigger the state
transition to recover.
2018-08-23 00:03:22 +02:00
Johan T. Halseth
1758ad829f
contractcourt/[chain|channel]_arbitrator: define IsPendingClose 2018-08-23 00:03:22 +02:00
Johan T. Halseth
6b24ca0387
contractcourt test: add TestChannelArbitratorPeristence 2018-08-23 00:03:21 +02:00
Johan T. Halseth
75df58c68b
contractcourt: move responsibility for closing force closes to chanArb
This commit moves the responsibility for closing local and remote force
closes in the database from the chain watcher to the channel arbitrator.

We do this because we previously would close the channel in the
database, before sending the event to the channel arbitrator. This could
lead to a situation where the channel was marked closed, but the channel
arbitrator didn't receive the event before shutdown. As we don't listen
for chain events for channels that are closed, those channels would be
stuck in the pending close state forever, as the channel arbitrator
state machine wouldn't progress.

We fix this by letting the ChannelArbitrator close the channel in the
database. After the contract resolutions are logged (in the state
callback before transitioning to StateContractClosed) we mark the
channel closed in the database. This way we make sure that it is marked
closed only if the resolutions have been successfully persisted.
2018-08-23 00:03:21 +02:00
Johan T. Halseth
c11e0940c2
contractcourt/channel_arbitrator test: test new coop close actions 2018-08-23 00:03:21 +02:00
Johan T. Halseth
ad1d78884a
contractcourt: move responsibility for closing coop close channel to chanArb 2018-08-23 00:03:21 +02:00
Johan T. Halseth
3549f2bc17
contractcourt/[channel|chain]_arbitrator: define MarkChannelClosed 2018-08-22 23:51:04 +02:00
Johan T. Halseth
740db4716f
contractcourt/channel_arbitrator test: test state transitions 2018-08-22 23:51:04 +02:00
Johan T. Halseth
862c7ae223
contractcourt/channel_arbitrator test: check both respChan and errChan
// Both are sent on, so in some cases we would read from errChan first,
making the test fail.
2018-08-22 23:51:04 +02:00
Johan T. Halseth
06c683d48d
contractcourt/channel_arbitrator: move next state commit out of stateStep 2018-08-22 23:51:04 +02:00
Johan T. Halseth
6df302d7f5
contractcourt/channel_arbitrator: remove state callback
This commit removes the state callback, and instead logs the contract
resolutions directly after receiving the unilateral close event. The
resolutions won't change so there's not really necessary to wait to log
them, and this greatly simplifies the code.
2018-08-22 23:51:04 +02:00
Johan T. Halseth
245dae0071
contractcourt/channel_arbitrator: properly commit to next state 2018-08-22 23:51:04 +02:00
Olaoluwa Osuntokun
86e2e3fe2e
Merge pull request #1608 from halseth/htlc-expiry-off-by-one
contractcourt/contract_resolvers: correct off-by-one HTLC expiry
2018-08-17 16:41:42 -07:00
Olaoluwa Osuntokun
e97e4c1f5d
contractcourt: set auxiliary chan recovery info for all created chan close summaries 2018-08-14 19:11:40 -07:00
Johan T. Halseth
5476f6d310
contractcourt/contract_resolvers: correct off-by-one HTLC expiry
This commit attempts to fix an inconsistency in when we consider an HTLC
to expire. When we first launched the resolver we would compare the
current block height against the expiry, while for new incoming blocks
we would compare against expiry-1.

This lead to a flake during integration tests, during a call to
RestartNode after _exactly_ enough blocks for the HTLC to expire. In
some cases the resolver would see the new blocks and consider the HTLC
to be expired (because of the -1), while in some cases resolver would
shut down before seeing the new blocks, and upon restart wouldn't act on
the new height because we did not compare against -1.

This commit fixes this by doing the same comparison in both cases.
2018-08-14 09:15:48 +02:00
Valentine Wallace
1ffc3bb82e
multi: update to latest RegisterBlockEpochNtfn interface 2018-08-10 01:08:57 -07:00
Wilmer Paulino
a63677a381
multi: switch to transaction weight to calculate transaction fees
Due to a recent change within the codebase to return estimated fee rates
in sat/kw, this commit ensures that we use this fee rate properly by
calculing a transaction's fees using its weight. This includes all of
the different transactions that are created within lnd (funding, sweeps,
etc.). On-chain transactions still rely on a sat/vbyte fee rate since it's
required by btcwallet.
2018-08-09 17:29:50 -07:00
Conner Fromknecht
8758671552
contractcourt/chain_arbitrator: fix potential shutdown race
This commit fixes a potential race condition during
shutdown, that could allow the chain arb's
activeWatchers or activeChannels map to be modified
while ranging over their contents. We fix this by
copying the contents into new maps with the mutex
held, before releasing the mutex and shutting down
each watcher or channel arbitrator.
2018-08-03 16:46:41 -07:00
Conner Fromknecht
1ded697e8d
multi: sort import paths with gofmt 2018-08-02 18:20:49 -07:00
Olaoluwa Osuntokun
d9969f5dc2
contractcourt: update contract resolvers to use pkScripts for spend ntfns 2018-07-31 21:29:01 -07:00
Olaoluwa Osuntokun
c6950fcc2f
contractcourt: update channel arbitrator unit tests due to recent API changes 2018-07-31 21:29:01 -07:00
Olaoluwa Osuntokun
08e11d197d
contractcourt: update chain watcher to make funding pkScript for spend ntfn 2018-07-31 21:29:00 -07:00
Olaoluwa Osuntokun
ac9f9bd061
contractcourt: remove now obsolete code from the chain arbitrator
We no longer need this code fragment as the chain watcher is now able to
handle confirmed cooperative channel closures itself.
2018-07-31 21:29:00 -07:00
Olaoluwa Osuntokun
c707577e99
contractcourt: update conf ntfn registartions to use pkScript not txid 2018-07-31 21:28:49 -07:00
Johan T. Halseth
00154bda24
contractcourt/chain_watcher: attempt dispatchRemoteClose using data loss commitPoint
This commit makes the chainwatcher attempt to dispatch a remote close
when it detects a remote state with a state number higher than our
known remote state. This can mean that we lost some state, and we check
the database for (hopefully) a data loss commit point retrieved during
channel sync with the remote peer. If this commit point is found in the
database we use it to try to recover our funds from the commitment.
2018-07-31 15:16:24 +02:00
Johan T. Halseth
2626bba105
contractcourt/chain_watcher: use commitPoint directly instead of isPendingCommit 2018-07-31 08:27:03 +02:00
Conner Fromknecht
aa6e5bdd2a
contractcourt/contract_resolvers: fix subscribe preimage race
This commit fixes a potential race condition within the
IncomingContestResolver, that could cause us to miss a
preimage that was delivered in time.

Currently we query the db for the preimage, and then
subscribe for notifications. This permits the following
ordering of events:
 - query for preimage, returns nothing
 - preimage is added and delivered to subscribers
 - subscribe to preimages
 - preimage never comes through!!

We fix this by reordering to subscribe for preimages and
then query just in case it already exists. The effect is
that the query will always return a valid read of the
preimages that are currently queued for delivery.
2018-07-25 03:15:51 -07:00
Olaoluwa Osuntokun
e0baa49690
Merge pull request #1387 from wpaulino/send-disable-chan-update
multi: send a channel update with disabled flag set on channel close
2018-07-23 17:46:37 -07:00
Johan T. Halseth
d7b2977e8c
multi test: remove mempool bool from RegisterSpendNtfn 2018-07-22 23:09:08 +02:00
Johan T. Halseth
57e829f47e
multi: remove mempool bool from RegisterSpendNtfn 2018-07-22 23:09:08 +02:00
Johan T. Halseth
a16bb662a4
contractcourt/chain_arbitrator: make watchForChannelClose use confirmed spend 2018-07-22 23:09:07 +02:00
Johan T. Halseth
5bfa2f5912
contractcourt/contract_resolvers: make commitSweepResolver use confirmed spend 2018-07-22 23:09:07 +02:00
Johan T. Halseth
9fffe23696
contractcourt/contract_resolvers: make htlcOutgoingContestResolver act on conf 2018-07-22 23:07:53 +02:00
Johan T. Halseth
b3e17aa67b
contractcourt/contract_resolvers: make second level htlcSuccessResolver wait for conf 2018-07-22 11:20:23 +02:00
Johan T. Halseth
bf06dc24c1
contractcourt/contract_resolvers: make waitForOutputResolution use confirmed spend 2018-07-22 11:20:23 +02:00
Wilmer Paulino
d26050f711
contractcourt: use MarkChannelResolved for coop channel closures
In this commit, we alter cooperative channel closures to also use
MarkChannelResolved in order to unify the logic for the different types
of channel closures.
2018-07-17 17:35:28 -07:00
Olaoluwa Osuntokun
6f60f139f4 multi: switch over import paths from roasbeef/* to btcsuite/* 2018-07-13 17:05:39 -07:00
Wilmer Paulino
873cccc7a6
multi: send channel update with disabled active on force and coop chan closes 2018-07-11 17:07:03 -07:00
Johan T. Halseth
d787e74125
contractcourt/channel_arbitrator: stop block epoch on channel attendant exit 2018-06-21 11:13:46 +01:00
Johan T. Halseth
1c43a0cb36
contractcourt/chain_arbitrator: delete watcher from activeWatchers 2018-06-21 11:13:46 +01:00
Johan T. Halseth
327420d854
contractcourt/channel_arbitrator: Cancel block epoch in case of error in Start() 2018-06-19 12:20:30 +01:00
maurycy
3be08e69cf multi: 64bit aligment of atomic vars on arm/x86-32 2018-06-04 20:02:34 -07:00
Johan T. Halseth
1f746c3fda
contractcourt/chan_arb: specify that ForceCloseChan removes link from
switch
2018-05-25 06:54:05 +02:00
Johan T. Halseth
8afc7bf66e
contractcourt/chain_arbitrator: add TODO for removing watchForChannelClose 2018-05-24 10:24:31 +02:00
Johan T. Halseth
921f02fe22
contractcourt/chain_arbitrator: markChanClosed->notifyChanClosed
We no longer have to mark the channel as fully closed in the database,
as it is done directly in the chainWatcher. Instead, we stop the watcher
and delete it from the set of active watchers.
2018-05-22 14:30:19 +02:00
Johan T. Halseth
0f077fcb54
contractcourt/chain_watcher: mark fully closed in dispatchCoopClose
This commit makes the dispatchCooperativeClose method mark the channel
fully closed directly, without registering for confirmation
notifications first. We can do this as recent changes to the
contractcourt changed the definition of a closed channel in the database
to have had its closing tx confirmed, and we only dispatch the
cooperative close once the transaction has 1 confirmation.

We also rename the markChanClosed method to notifyChanClosed, to more
clearly indicate that the ChainArbitrator no longer has to mark the
channel fully closed in the database.
2018-05-22 14:26:02 +02:00
Johan T. Halseth
af14a2fc57
contractcourt/chain_watcher: remove CooperativeCloseCtx
Removes CooperativeCloseCtx and methods.
2018-05-22 14:21:43 +02:00
Johan T. Halseth
21d1cc3fe8
contractcourt/chain_arbitrator: remove BeginCoopChanClose 2018-05-22 12:06:33 +02:00
Olaoluwa Osuntokun
9dcd98ad9c
contractcourt: prevent scoping bug by re-binding channel pointer
In this commit, we attempt to fix a bug that's possible within the
Start() method of the ChainArbiter. We pass the channel pointer directly
into the newActiveChannelArbitrator function causing it to close over
the loop variable. We later use the channel point directly to send
messages to other sub-systems. It's possible that we actually have the
shadowed loop variable and will send an incorrect message. Defensively,
we now re-bind the loop variable in order to ensure we point to the
proper channel.
2018-05-18 16:21:06 -07:00
Johan T. Halseth
ec93bf581d
contractcourt test: add TestChannelArbitratorLocalForceDoubleSpend
This commit adds a test for the case where the ChannelArbitrator fails
to broadcast its commitment during a force close because of
ErrDoubleSpend. We test that in this case it will still wait for a
commitment getting confirmed in-chain, then resolve.
2018-05-14 14:21:06 +02:00
Conner Fromknecht
f963f91a3c
multi: use mutex-guarded ShortChanID() on OpenChannel 2018-05-09 16:06:49 -07:00
Olaoluwa Osuntokun
7af699f3a7
contractcourt: update chain watcher tests to latest API 2018-05-03 21:29:30 -07:00
Olaoluwa Osuntokun
a3227ba147
contractcourt: add two new tests to ensure the chainWatcher is able to play all remote commitments 2018-05-03 21:29:29 -07:00
Olaoluwa Osuntokun
c8b15719f2
contractcourt: ensure the chainWatcher is able to play all remote commitments 2018-05-03 21:29:29 -07:00
Johan T. Halseth
f06797ab00
contractcourt/chain_arbitrator: make breachArbiter handoff a function closure
This commit adds the new function closure option ContractBreach to the
ChainArbitrator config, a closure that is again used by the ChainWatcher
to reliably handoff a breach event to the breachArbiter.
2018-05-02 08:43:31 +02:00
Johan T. Halseth
3fdc04dff0
contractcourt/chain_watcher: make breachArbiter handoff a function closure
This commit changes how the ChainWatcher notifies the breachArbiter
about a channel breach. Instead of assuming the breachArbiter is among
the clients subscibing to channel events, it will call a new method
contractBreach(), and assume the breachArbiter has reliably gotten the
breach info when this method returns with a non-nil error.

Since the breachArbiter was the only sybsystem having a sync chain
subsciption, we also remove the (now) unused syncDispatch option.
2018-05-02 08:43:31 +02:00
Johan T. Halseth
a6f0dd72ac
contractcourt/chain_arbitrator: use chainWatcherConfig 2018-05-02 08:43:31 +02:00
Johan T. Halseth
0697510884
contractcourt/chain_watcher: create chainWatcherConfig 2018-05-02 08:43:31 +02:00
Johan T. Halseth
79341fc63e
contractcourt/chain_watcher: move log message
This move the log message "channel marked pending-closed" to the point
where the channel actually has been marked pending closed, instead of
before the database transaction has been done.
2018-05-02 08:43:30 +02:00
Johan T. Halseth
7e397067f6
contractcourt/chainwatcher: remove close observer for loop
This commit removes the for loop in the closeObserver, as it wasn't
serving any purpose. After receiving a spend notification we would
return, breaking out of the loop. When getting a quit signal we would
also return, making the loop only do one iteration in any case.
2018-05-02 08:43:30 +02:00
Johan T. Halseth
4320421110
contractcourt/chain_watcher: don't delete syncDispatch clients on Cancel()
This commit makes clients subscribing to channel events that are marked
"sync dispatch" _not_ being deleted from the list of clients when they
call Cancel(). Instead a go routine will be launched that will send an
error on every read of the ProcessACK channel.

This fixes a race in handing off the breach info while lnd was shutting
down. The breach arbiter could end up being shut down (and calling
Cancel()) before while the ChainWatcher was in the process of
dispatching a breach. Since the breach arbiter no longer was among the
registered clients at this point, the ChainWatcher would assume the
breach was handed off successfully, and mark the channel as pending
closed. When lnd now was restarted, the breach arbiter would not know
about the breach, and the ChainWatcher wouldn't attempt to re-dispatch,
as it was already marked as pending closed.
2018-04-25 09:37:26 +02:00
Johan T. Halseth
ddf62bbeea
contractcourt/channel_arbitrator: remove unused transationTrigger 2018-04-25 09:37:25 +02:00
Johan T. Halseth
b2949bd728
contractcourt/channel_arbitrator test: add unit tests
This commit adds MVP unit tests for the following scenarios in the
ChannelArbitrator:
1) A cooperative close is confirmed.
2) A remote force close is confirmed.
3) A local force close is requested and confirmed.
4) A local force close is requested, but a remote force close gets
confirmed.
2018-04-25 09:37:25 +02:00
Johan T. Halseth
84f06959f3
contractcourt/chain_arbitrator: remove CloseChannel method from ChannelArbitrator config 2018-04-25 09:37:25 +02:00
Johan T. Halseth
5f1da5b5f4
contractcourt/channel_arbitrator: remove unused CloseChannel method from config 2018-04-25 09:37:24 +02:00
Johan T. Halseth
70e8087731
contractcourt/channel_arbitrator: mark channel commitment broadcasted instead of closed after broadcast 2018-04-25 09:37:24 +02:00
Johan T. Halseth
854e73b8e0
contractcourt/chain_watcher: register for confirmed spend, mark closed when detected
This commit changes the ChainWatcher to only send a chain event in case
the various spends are _confirmed_ on-chain, not only seen on the
network.

A consequence of this is that we now give the ChainWatcher the
responsibility of marking the channel closed when the closing tx is
confirmed, instead of the ChannelArbitrator.
2018-04-25 09:37:24 +02:00
Johan T. Halseth
1265c9cc99
contractcourt/chain_arbitrator: add MarkCommitmentBroadcasted to ChanArb cfg 2018-04-25 09:37:24 +02:00
Johan T. Halseth
d2d87758f7
contractcourt/channel_arbitrator: only act on close signal from chain_watcher
This commit changes the channel arbitrator state machine to only care
about commitment transactions that are being confirmed on-chain
according to the chain_watcher. This is meant to handles the cases where
we would broadcast our commitment, expecting it to get confirmed, but
instead a competing transaction was confirmed.

This commit readies the ChannelArbitrator state machine for the change
that will make the ChainWatcher only notify on confirmed commitments.

The state machine has gotten a new state, StateCommitmentBroadcasted,
which we'll transition to after we have broadcasted our own commitment.
From this state we'll go to the StateContractClosed state regardless of
which commitment the ChainWatcher notifies about, unifying the contract
resolution betweee the local and remote force close.
2018-04-25 09:37:23 +02:00
Johan T. Halseth
a60e621b5f
contractcourt/channel_arbitrator: specify that height is triggerHeight, advance from height of event not current height 2018-04-25 09:37:23 +02:00
Johan T. Halseth
40b8cf0adb
contractcourt/channel_arbitrator: rename UnilateralClose -> RemoteUnilateralClose 2018-04-25 09:37:22 +02:00
Johan T. Halseth
49f4a70e5a
contractcourt/chain_watcher: add dispatchLocalForceClose
This commit adds a new method dispatchLocalClose, which will be called
in case our commitment is detected to spend the funding transaction. In
this case LocalUnilateralCloseInfo will be sent on the
LocalUnilateralClosure channel to all subscribers.

The UnilateralClosure channel is renamed to RemoteUnilateralClosure for
consistency.
2018-04-25 09:37:22 +02:00
Johan T. Halseth
7bacdd65dc
contractcourt: define StateCommitmentBroadcasted 2018-04-25 09:37:22 +02:00
Johan T. Halseth
24e8bace9c
contractcourt/channel_arbitrator: add missing return after forceClose request 2018-04-25 09:37:21 +02:00
Johan T. Halseth
0b2e6d1776
contractcourt/channel_arbitrator: removed unused bestHash parameter 2018-04-25 09:37:21 +02:00
Johan T. Halseth
9b9fada675
contractcourt: remove unecessary contractRes check
This commit removes a short circuit checking if the contract resolver
after a unilateral close is empty. After removing this, the state
machine will advance the state from StateDefault->ContractClosed, in
which the stateCallback will be called, logging the state needed to
advance. Since this logged state is empty, the state machine will go
directly to StateFullyResolved, which will trigger the
MarkChannelResolved call. This means the behaviour is kept.
2018-04-25 09:37:21 +02:00
Dimitris Tsapakidis
4009f7f874 multi: fix typos in comments 2018-04-17 19:03:27 -07:00
practicalswift
663c396235 multi: fix a-vs-an typos 2018-04-17 19:02:04 -07:00