Commit Graph

191 Commits

Author SHA1 Message Date
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 96fbc7da84
htlcswitch: fix deadlock during chainWatcher notifications 2018-01-22 19:19:59 -08:00
Olaoluwa Osuntokun ca613a625f
htlcswitch: handleLocalDispatch can now handle locally sourced resolutions
In this commit, we update the failure case within handleLocalDispatch
to handle locally sourced resolutions. This is the case that we send a
payment out, but before it can even get past the first hop, we need to
go to chain (may have been a cascading failure). Once the HTLC is fully
resolved, we’ll send back a resolution message, however, that message
doesn’t have a failure reason populated. To properly handle this, we’ll
send back a permanent channel failure to the router.
2018-01-22 19:19:56 -08:00
Olaoluwa Osuntokun 239416f242
htlcswitch: update to use new event stream from the chainWatcher 2018-01-22 19:19:51 -08:00
Olaoluwa Osuntokun 703057c821
htlcswitch: with debughtlc+hodlhtlc mode, skip all HTLC level checks 2018-01-22 19:19:41 -08:00
Olaoluwa Osuntokun e34850c7af
htlcswitch: update tests to account for new API changes 2018-01-22 19:19:41 -08:00
Olaoluwa Osuntokun dbe76a1507
htlcswitch: upon restart, examine all active HTLC's, settle those that we can
In this commit, we address a lingering TODO: before this if we had a
set of HTLC’s that we knew the pre-image to on our commitment
transaction after a restart, then we wouldn’t attempt to settle them.
With this new change, we’ll check that we didn’t already retransmit the
settles for them, and check the preimage cache to see if we already
know the preimage. If we do, then we’ll immediately settle them.
2018-01-22 19:19:40 -08:00
Olaoluwa Osuntokun ca4eb970ec
htlcswitch: move channel re-sync into distinct function 2018-01-22 19:19:40 -08:00
Olaoluwa Osuntokun 2d133acaeb
htlcswitch: after each new state update, notify callers to set of new HTLC's 2018-01-22 19:19:39 -08:00
Olaoluwa Osuntokun 1418d672f9
htlcswitch: notify the ChainArbitrator of fresh signals upon link creation 2018-01-22 19:19:39 -08:00
Olaoluwa Osuntokun f2642a70db
htlcswitch: once we discover a pre-image, add it to the witness cache
In this commit, we add some additional logic to the case when we
receive a pre-image from an upstream peer. We’ll immediately add it to
the witness cache, as an incoming HTLC might be waiting on-chain to
fully resolve the HTLC with knowledge of the newly discovered
pre-image.
2018-01-22 19:19:39 -08:00
Olaoluwa Osuntokun 9cb3657314
htlcswitch: add new ProcessContractResolution method
In this commit, we add a new method: ProcessContractResolution. This
will be used by entities of the contract court package to notify us
whenever they discover that we can resolve an incoming contract
off-chain after the outgoing contract was fully resolved on-chain.

We’ll take a contractcourt.ResolutionMsg and map it to the proper
internal package so we can fully resolve an active circuit.
2018-01-22 19:19:38 -08:00
Johan T. Halseth 9bdb483757
htlcswitch/link: make MinHTLC non-changable
This commit specifies that the MinHTLC value for a link is static
over the lifetime of a channel, and don't process the field
during a policyUpdate.
2018-01-12 22:56:29 +01:00
Olaoluwa Osuntokun 912366ada5
htlcswitch: fix notifier goroutine leak by cancelling epoch when htlcManager exits
Before this commit, if the htlcManager unexpectedly exited (due to a
protocol error, etc), the underlying block epoch notification intent
that was created for it would never be cancelled. This would result in
tens, or hundreds of goroutine leaks as the client would never consume
those notifications.

To fix this, we move cancellation of the block epoch intent from the
Stop() method of the channel link, to the defer statement at the top of
the htlcManager.
2018-01-08 19:50:24 -08:00
Olaoluwa Osuntokun 795b5559f0
htlcswitch: print rhash in error messages of mockInvoiceRegistry 2018-01-08 19:50:23 -08:00
Olaoluwa Osuntokun dbf6a511fc
htlcswitch: if we detect an InvalidCommitSigError, send over detailed error
In this commit, we add an additional case when handling a failed
commitment signature. If we detect that it’s a InvalidCommitSigError,
then we’ll send over an lnwire.Error message with the full details. We
don’t yet properly dispatch this error on the reciting side, but that
will be done in a follow up a commit.
2018-01-08 19:50:23 -08:00
Olaoluwa Osuntokun e54e88ee2f
htlcswitch: ensure onion related errors always have the BadOnion bit set
In this commit, we fix a lingering protocol level bug when reporting
errors encountered during onion blob processing. The spec states that
if one sends an UpdateFailMalformedHtlc, then the error reason MUST
have the BadOnion bit set. Before this commit, we would return
CodeTemporaryChannelFailure. This is incorrect as this doesn’t have the
BadOnio bit set.
2018-01-08 19:50:21 -08:00
Olaoluwa Osuntokun e2fe4c2955
htlcswitch: reject duplicate payments to same invoice
In this commit, we modify the way the link handles HTLC’s that it
detects is destined for itself. Before this commit if a payment hash
came across for an invoice we’d already settled, then we’d gladly
accept the payment _again_. As we’d like to enforce the norm that an
invoice is NEVER to be used twice, this commit modifies that behavior
to instead reject an incoming payment that attempts to re-use an
invoice.

Fixes #560.
2018-01-04 14:23:37 -06:00
Olaoluwa Osuntokun b74b9fc16e
test: extend time outs in flaky tests due to Travis CI instance speed variance 2017-12-22 19:43:50 +01:00
Olaoluwa Osuntokun 9777176d7d
lnwallet: don't use persistent pointer to funding tx within channel state machine
This commit fixes a lingering bug that could at times cause
incompatibilities with other implementations when attempting a
cooperative channel close. Before this commit, we would use a pointer
to the funding txin everywhere. As a result, each time we made a new
state, or verified one, we would modify the sequence field of the main
txin of the commitment transaction. Due to this if we updated the
channel, then went to do a cooperative channel closure, the sequence of
the txin would still be set to the value we used as the state hint.

To remedy this, we now copy the txin each time when making the
commitment transaction, and also the cooperative closure transaction.
This avoids accidentally mutating the txin itself.

Fixes #502.
2017-12-22 19:26:35 +01:00
Olaoluwa Osuntokun 151a4325b1
htlcswitch: fix alignment of the packetQueue's fields for 32-bit systems (#507)
In this commit, we fix an existing issue that would cause lnd to panic
on 32-bit systems. Within the packetQueue we utilize atomics heavily.
However, it's the caller's job to ensure 64-bit alignment of 64-bit words
accessed atomically. This is documented within the sync/atomic package
as a set of known bugs.

The old alignment of this struct was:

⛰  structlayout github.com/lightningnetwork/lnd/htlcswitch packetQueue
packetQueue.queueLen int32: 0-4 (size 4, align 4)
padding: 4-8 (size 4, align 0)
packetQueue.totalHtlcAmt int64: 8-16 (size 8, align 8)
packetQueue.queueCond *sync.Cond: 16-24 (size 8, align 8)
packetQueue.queueMtx.state int32: 24-28 (size 4, align 4)
packetQueue.queueMtx.sema uint32: 28-32 (size 4, align 4)
packetQueue.queue []*github.com/lightningnetwork/lnd/htlcswitch.htlcPacket: 32-56 (size 24, align 8)
packetQueue.outgoingPkts chan *github.com/lightningnetwork/lnd/htlcswitch.htlcPacket: 56-64 (size 8, align 8)
packetQueue.freeSlots chan struct{}: 64-72 (size 8, align 8)
packetQueue.wg.noCopy sync.noCopy: 72-72 (size 0, align 1)
packetQueue.wg.state1 [12]byte: 72-84 (size 12, align 1)
packetQueue.wg.sema uint32: 84-88 (size 4, align 4)
packetQueue.quit chan struct{}: 88-96 (size 8, align 8)

After this commit, the new alignment of this sturct is:

⛰  structlayout  -json github.com/lightningnetwork/lnd/htlcswitch packetQueue | structlayout-optimize
packetQueue.queue []*github.com/lightningnetwork/lnd/htlcswitch.htlcPacket: 0-24 (size 24, align 8)
packetQueue.wg struct: 24-40 (size 16, align 8)
packetQueue.freeSlots chan struct{}: 40-48 (size 8, align 8)
packetQueue.queueCond *sync.Cond: 48-56 (size 8, align 8)
packetQueue.queueMtx struct: 56-64 (size 8, align 8)
packetQueue.outgoingPkts chan *github.com/lightningnetwork/lnd/htlcswitch.htlcPacket: 64-72 (size 8, align 8)
packetQueue.totalHtlcAmt int64: 72-80 (size 8, align 8)
packetQueue.quit chan struct{}: 80-88 (size 8, align 8)
packetQueue.queueLen int32: 88-92 (size 4, align 8)
padding: 92-96 (size 4, align 0)

Fixes #505, and #463.
2017-12-22 16:32:11 +01:00
Olaoluwa Osuntokun 3b986b4c14
htlcswitch: extend timeouts within bandwidth consistency tests 2017-12-21 11:47:45 +01:00
Matt Drollette adf0d98194 multi: fix several typos in godoc comments 2017-12-17 18:40:05 -08:00
Jim Posen 88dc73adb0 htlcswitch: Fix failure error handling on outgoing adds. 2017-12-14 17:53:58 -08:00
Jim Posen 813c012ffe htlcswitch: mockChannelLink adds to circuit map to simplify tests. 2017-12-14 17:53:58 -08:00
Jim Posen 6ea533275c htlcswitch: Remove obsolete fields from htlcPacket. 2017-12-14 17:53:58 -08:00
Jim Posen 40fb0ddcfc htlcswitch: Assign each pending payment a unique ID.
This simplifies the pending payment handling code because it allows it
be handled in nearly the same way as forwarded HTLCs by treating an
empty channel ID as local dispatch.
2017-12-14 17:53:58 -08:00
Jim Posen 4a29fbdab2 htlcswitch: Rename htlcPacket fields for clarity.
The src/dest terminology for routing packets is kind of confusing
because the source HTLC may not be the source of the packet for
settles/fails traversing the circuit in the opposite direction. This
changes the nomenclature to incoming/outgoing and always references
the HTLCs themselves.
2017-12-14 17:53:58 -08:00
Jim Posen 66e654bb42 htlcswitch: Unit tests for CircuitMap. 2017-12-14 17:53:58 -08:00
Jim Posen 317b44e220 lnwallet: Change channel update methods to accept HTLC ID.
Previously, some methods on a LightningChannel like SettleHTLC and
FailHTLC would identify HTLCs by payment hash. This would not always
work correctly if there are multiple HTLCs with the same payment hash,
so instead we change these methods to identify HTLCs by their unique
identifiers instead.
2017-12-14 17:53:58 -08:00
Jim Posen 1328e61c00 htlcswitch: Change circuit map keys to (channel ID, HTLC ID).
This changes the circuit map internals and API to reference circuits
by a primary key of (channel ID, HTLC ID) instead of paymnet
hash. This is because each circuit has a unique offered HTLC, but
there may be multiple circuits for a payment hash with different
source or destination channels.
2017-12-14 17:53:58 -08:00
Jim Posen bc8d674958 htlcswitch: Remove constructor functions for htlcPacket.
The constructor functions have no additional logic other than passing
function parameters into struct fields. Given the large function
signatures, it is more clear to directly construct the htlcPacket in
client code than call a function with lots of positional arguments.
2017-12-14 17:53:58 -08:00
Olaoluwa Osuntokun 4cca23264d
htlcswitch: add new default case when handling UpdateFailMalformedHLTC
In this commit, we modify the existing logic to handle
UpdateFailMalformedHLTC message from an incoming peer. Rather than fail
the Chanel if they give us an invalid failure code, we’ll instead treat
it as a temporary channel failure so we can continue to forward the
error.
2017-12-12 11:42:47 -08:00
Olaoluwa Osuntokun 8da46e0b5a
htlcswitch: add tests to ensure we don't forward to inactive links 2017-12-10 16:19:47 -08:00
Olaoluwa Osuntokun c3d345b575
htlcswitch: don't add UpdateFee to channel if not able to forward
This commit is a follow up to a prior commit which skipped sending the
commitment sig message (and sending out the update fee) message if the
channel wasn’t yet able to forward any HTLC’s. We’ll modify the prior
commit to not add the fee update to the channel at all. Otherwise, we
risk a state desynchronization.
2017-12-10 16:19:21 -08:00
Olaoluwa Osuntokun b40c84833e
htlcswitch: add additional error logging during onion process failures 2017-12-10 15:52:37 -08:00
Ben Congdon 720a8c7e96 htlcswitch: Only send UpdateFee message when channel is eligible
This commit adds a check to `updateChannelFee` which skipssending the
`update_fee` message when the channel is not eligable for forwarding
messages (likely due to the channel's `RemoteNextRevocation` not yet
being set).

This addresses #470.
2017-12-10 10:46:30 -08:00
Olaoluwa Osuntokun 8a7085f8b4
htlcswitch: ensure we don't dispatch local HTLC's to link that aren't eligible to forward
This commit fixes an existing bug wherein we would incorrectly attempt
to forward and HTLC to a link that wasn’t yet eligible for forwarding.
This would occur when we’ve added a link to the switch, but haven’t yet
received a FundingLocked message for the channel. As a result, the
channel won’t have the next revocation point available. A logic error
prior to this commit would skip tallying the largest bandwidth rather
than skipping examining the link all together.

Fixes #464.
2017-12-07 19:03:27 -08:00
Olaoluwa Osuntokun 669c2ee1a0
htlcswitch: only re-send FundingLocked if the channel is fully confirmed 2017-12-06 16:43:02 -08:00
Olaoluwa Osuntokun be1a96b78a
htlcswitch: ensure links are eligible to forward when selecting outgoing links
In this commit, when selecting a candidate link to forward a payment,
we’ll ensure that it’s actually able to take on the HTLC. Otherwise,
we’ll skip over the link itself. Currently, a link is only fully
eligible for forwarding, *after* we’ve received and fully processed the
FundingLocked message.
2017-12-06 16:42:58 -08:00
Olaoluwa Osuntokun 36956d390f
htlcswitch: add new method to the ChannelLink interface, EligibleToForward
In this commit, we add a new method to the ChanneLink interface:
EligibleToForward. This method allows a link to be added to the switch,
but in an intermediate state which indicates that it isn’t yet ready to
forward any incoming HTLC’s.
2017-12-06 16:42:57 -08:00
Olaoluwa Osuntokun 260ff8831a
htlcswitch: update UpdateFee usage due to recent API change 2017-11-30 22:17:56 -08:00
Olaoluwa Osuntokun 62473009b5
htlcswitch: within link, with each new block, check to see if commit fee should change
In this commit we add a new case to the main select statement within a
channel link. This select statement will serve as a Sipping Bird which
will check the network fee rate (as returned by the fee estimator) and
compare that to the fee on the commitment transaction. Using the
shouldAdjustCommitFee function, we determine if we should update the
commitment fee. If so, then we’ll send an UpdateFee message and also
trigger a new commitment update.

We also add a new unit test: TestChannelLinkUpdateCommitFee to ensure
that we update the fee accordingly if the fee increases or decreases by
a large portion.
2017-11-23 23:10:19 -06:00
Olaoluwa Osuntokun c560200ba1
htlcswitch: add new shouldAdjustCommitFee helper function
In this commit, we add a new helper function to the link which will be
utilized in a later commit. This helper function will help us determine
if we should update the commitment fee, in response to a change in the
network fee return by our fee estimators.
2017-11-23 23:10:19 -06:00
Olaoluwa Osuntokun 5a51600f95
htlcswitch: update CloseLink to accept ideal fee rate 2017-11-23 23:10:12 -06:00
Olaoluwa Osuntokun 25082f0b5b
htlcswitch: update WipeChannel on Peer interface to simply take the chanPoint
The WipeChannel method doesn’t need to take the channel itself, as any
relevant indexes should be able to be queried based on the channel
point along.
2017-11-23 23:10:10 -06:00
Olaoluwa Osuntokun 3aabbce551
htlcswitch: update tests to adhere to new FeeEstimator changes 2017-11-23 23:10:10 -06:00
Olaoluwa Osuntokun b31e94573b
htlcswitch: properly set HtlcBasePoint in createTestChannel 2017-11-16 20:00:08 -08:00
Olaoluwa Osuntokun 6afebfba5d
htlcswitch: update link to match latest ChanSyncMsg API change 2017-11-16 20:00:06 -08:00
Olaoluwa Osuntokun d7aa769275
htlcswitch: add additional sleep to end of TestChannelLinkBidirectionalOneHopPayments 2017-11-15 18:25:01 -08:00