Commit Graph

313 Commits

Author SHA1 Message Date
Johan T. Halseth
71228a6b06
lnwallet/channel: don't use commit height to determine FullySynced
This commit removes a faulty check we did to determine if the channel
commitments were fully synced. We assumed that if out local commitment
chain had a height higher than the remote, then we would have state
updates present in our chain but not in theirs, and owed a commitment.
However, there were cases where this wasn't true, and we would send a
new commitment even though we had no new updates to sign. This is a
protocol violation.

Now we don't longer check the heights to determine if we are fully
synced. A consequence of this is that we also need to check if we have
any pending fee updates that are nopt yet signed, as those are
considered non-empty updates.
2018-05-09 08:47:21 +02:00
Johan T. Halseth
9324bf7b1d
lnwallet/channel: return error in case of htlc index mismatch
This commit make us return an error in case a restored HTLC from a
pending remote commit has an index that is different from our local
update log index. It is appended with the assumption that these indexes
are the same, and if they are not we cannot really continue.
2018-05-08 13:41:52 +02:00
Johan T. Halseth
7e1f2a7dc3
lnwallet/channel: remove unused arguments 2018-05-08 13:41:52 +02:00
Johan T. Halseth
aa1c2cdf81
lnwallet/channel: add more info in case of crash
This commit adds a call to panic in case the HTLC we are looking for is
not found in the update log. It _should_ always be there, but we have
seen crashes resulting from it not being found. Since it will crash with
a nil pointer dereference below, we instead call panic giving us a bit
more information to work with.
2018-05-08 13:41:52 +02:00
Johan T. Halseth
1d676b77e1
lnwallet/channel: log pending remote commit during restore 2018-05-08 13:41:51 +02:00
Johan T. Halseth
1d172428c4
lnwallet/channel: return missing errors
This commit corrects a few cases where we would encounter errors, but
return nil by mistake.
2018-05-08 13:41:51 +02:00
Olaoluwa Osuntokun
8b06817482
lnwallet: modify NewUnilateralCloseSummary to be aware of pending remote commits
In this commit, we modify the NewUnilateralCloseSummary to be able to
distinguish between a unilateral closure using the lowest+highest
commitment the remote party possesses. Before this commit, if the remote
party broadcast their highest commitment, when they have a lower
unrevoked commitment, then this function would fail to find the proper
output, leaving funds on the chain.

To fix this, it's now the duty of the caller to pass remotePendingCommit
with the proper value. The caller should use the lowest unrevoked
commitment, and the height hint of the broadcast commitment to discern
if this is a pending commitment or not.
2018-05-03 21:29:28 -07:00
Conner Fromknecht
cae1d468e7
lnwallet/channel: expose NextLocalHtlcIndex 2018-05-02 01:12:17 -07:00
Olaoluwa Osuntokun
c7c25445eb
lnwallet: add new test to ensure channel is able to update fees in both directions
Closes #1145.
2018-05-01 17:35:42 -07:00
Vegard Engen
d7a254328e Take into account that the new fee will not come in addition to old fee when updating commit fee 2018-04-29 22:42:20 +00:00
Johan T. Halseth
b7bb53a8b8
channeldb/channel: add property ChanStatus in place of IsBorked to OpenChannel
This commit changes the bool `IsBorked` in OpenChannel to a `ChanStatus`
struct, of type ChannelStatus. This is used to indicated that a channel
that is technically still open, is either borked, or has had a
commitment broadcasted, but is not confirmed on-chain yet.

The ChannelStatus type has the value 1 for the status Borked, meaning it
is backwards compatible with the old database format.
2018-04-25 09:37:23 +02:00
Johan T. Halseth
3d0cb16677
lnwallet/channel: clearify that the spendDetails is spending funding outpoint 2018-04-25 09:37:22 +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
Johan T. Halseth
8e77e1e6eb
lnwallet/channel: add NewLocalForceCloseSummary
This commit renames ForceCloseSummary to LocalForceCloseSummary, and
adds a new method NewLocalForceCloseSummary that can be used to derive a
LocalForceCloseSummary if our commitment transaction gets confirmed
in-chain. It is meant to accompany the NewUnilateralCloseSummary method,
which is used for the same purpose in the event of a remote commitment
being seen in-chain.
2018-04-06 09:46:30 +02:00
Olaoluwa Osuntokun
065b39bc0b
Merge pull request #1025 from Roasbeef/incoming-htlc-breach-retribution
lnwallet+test: properly generate the sender HTLC script in a contract breach scenario
2018-04-05 17:58:31 -07:00
Olaoluwa Osuntokun
f79af461d3
lnwallet: in NewBreachRetribution create the htlcRetributions slice to capacity, not length
In this commit, we fix an existing bug in the NewBreachRetribution
method. Rather than creating the slice to the proper length, we instead
now create it to the proper _capacity_. As we'll now properly filter out
any dust HTLCs, before this commit, even if no HTLCs were added, then
the slice would still have a full length, meaning callers could actually
interact with _blank_ HtlcRetribution structs.

The fix is simple: create the slice with the proper capacity, and append
to the end of it.
2018-04-05 16:19:49 -07:00
Olaoluwa Osuntokun
c393475d39
lnwallet: ensure that we skip dust HTLC's in NewBreachRetribution 2018-04-04 18:41:38 -07:00
Olaoluwa Osuntokun
4e44528bff
lnwallet: fix ordering of keys when we generate the sender script during a breach
In this commit, we fix an existing within lnd. Before this commit,
within NewBreachRetribution the order of the keys when generating the
sender HTLC script was incorrect. As in this case, the remote party is
the sender, their key should be first. However, the order was swapped,
meaning that at breach time, our transaction would be rejected as it had
the incorrect witness script.

The fix is simple: swap the ordering of the keys. After this commit, the
test extension added in the prior commit now passes.
2018-04-04 18:41:37 -07:00
Olaoluwa Osuntokun
1c5d62a804
lnwallet: add new concrete error InvalidHtlcSigError for failed htlc sig validation
In this commit we add a new error: InvalidHtlcSigError. This error will
be returned when we're unable to validate an HTLC signature sent by the
remote party. This will allow other nodes to more easily debug _why_ the
signature was rejected.
2018-04-04 17:41:10 -07:00
Olaoluwa Osuntokun
6103ccb081
lnwallet: ensure we only accept/produce sane commitments
In this commit, we add an additional check within CreateCommitTx to
ensure that we will never create or accept a commitment transaction that
wasn't valid by consensus. To enforce this check, we use the
blockchain.CheckTransactionSanity method.
2018-03-27 15:37:15 -07:00
Johan T. Halseth
a6e7dce7b7
lnwallet/channel: reject received commitment with too many htlc sigs
This commit adds a check that will make LightningChannel reject a
received commitment if it is accompanied with too many HTLC signatures.
This enforces the requirement in BOLT-2, saying:

if num_htlcs is not equal to the number of HTLC outputs in the local commitment transaction:
    * MUST fail the channel.

A test exercising the behaviour is added.
2018-03-22 14:58:22 +01:00
Johan T. Halseth
70b86e596e
lnwallet/channel: use remote dustlimit when generating HTLC sigs
This commit fixes an issue which would arise in some cases when the
local and remote dust limits would differ, resulting in lnd not
producing the expected number of HTLC signatures. This was a result of
checking dust against the local instead of the remote dust limit.

A test exercising the scenario is added.
2018-03-22 14:58:22 +01:00
Johan T. Halseth
263d6b9c1f
lnwallet/channel: don't accept immediately on empty htlc sigs
This commit fixes an issue where we would blindly accept a commitment
which came without any accompanying HTLC signatures. A test exercising
the scenario is added.
2018-03-22 14:58:22 +01:00
Johan T. Halseth
241c79397f
lnwallet/channel: fix crash on receiving too few HTLC sigs
This commit fixes an out of bounds error that would occur in the case
where we received a new commitment where the accompanying HTLC sigs were
too few. Now we'll just reject such an commitment.

A test exercising the behavior is also added.
2018-03-22 14:58:22 +01:00
Conner Fromknecht
d420266911
lnwallet/channel: expose API changes to channel state transitions 2018-03-09 17:35:53 -08:00
Conner Fromknecht
53e4422a2e
lnwallet/channel: integrate fwdpkgs w/ in-mem buffering 2018-03-09 14:45:30 -08:00
Olaoluwa Osuntokun
b8d0df998a
lnwallet: when validating fee updates, ensure newFee < balance 2018-03-08 12:50:49 -05:00
Olaoluwa Osuntokun
ac90a8288e
lnwallet: precalculate fees in mSAT to avoid multiple conversions 2018-03-08 12:50:49 -05:00
Olaoluwa Osuntokun
217166fb10
lnwallet: within validateCommitmentSanity check for balance underflow
In this commit, we add an additional check within
validateCommitmentSanity due to the recent change to unsigned integers
for peer balances in the channel state machine. If after evaluation
(just applying HTLC updates), the balances are negative, then we’ll
return ErrBelowChanReserve.
2018-03-08 12:50:48 -05:00
Olaoluwa Osuntokun
5f5e4554cb
lnwallet: if the initiator is unable to pay fees, then consume their entire output
In this commit, we add logic to account for an edge case in the
protocol. If they initiator if unable to pay the fees for a commitment,
then their *entire* output is meant to go to fees. The recent change to
properly interpret balances as unsigned integers (within the protocol)
let to the discovery of this missed edge case.
2018-03-08 12:50:48 -05:00
Olaoluwa Osuntokun
22ee0a7576
lnwallet: update state machine due to channeldb and SignDescriptor changes 2018-03-06 16:04:02 -05:00
Johan T. Halseth
1f839d2526
lnwallet/channel: denominate in fee rate types 2018-02-26 22:42:25 +01:00
Johan T. Halseth
7b9f098fe6
lnwallet: fully validate all channel constraints in validateCommitmentSanity
This commit introduces changes to the validateCommitmentSanity
function to fully validate all channel constraints.
validateCommitmentSanity now validates that the
MaxPendingAmount, ChanReserve, MinHTLC, & MaxAcceptedHTLCs
limits are all adhered to during the lifetime of a channel.

When applying a set of updates, the channel constraints are
validated from the point-of-view of either the local or the
remote node, to make sure the updates will be accepted.

Co-authored-by: nsa <elzeigel@gmail.com>
2018-02-08 18:35:25 -05:00
Johan T. Halseth
1873fe1381
lnwallet/channel: move common calculation of balance into computeView
This commit moves common logic used to calculate the state
of a commitment after applying a set of HTLC updates, into
the new method computeView. This method can be used when
calculating the available balance, validating the sanity
of a commitment after applying a set of updates, and also
when creating a new commitment, reducing the duplication
of this logic.
2018-02-08 18:35:25 -05:00
Johan T. Halseth
98d28611e4
lnwallet/channel: let evaluateHTLCView take mutate boolean
This commit adds a new boolean parameter mutateState to
evalueteHTLCView, that let us call it without neccessarily
mutating the addHeight/removeHeight of the HTLCs, which is
useful when evaluating the commitment validity without
mutating the state.
2018-02-08 18:35:25 -05:00
Olaoluwa Osuntokun
9c483c38b1
lnwallet: update state machine to use new lnwire.Sig everywhere 2018-02-06 20:14:31 -08:00
Daniel McNally
8543497dcc multi: fixing it's/its typos in comments 2018-02-06 19:13:07 -08:00
practicalswift
a93736d21e multi: comprehensive typo fixes across all packages 2018-02-06 19:11:11 -08:00
Johan T. Halseth
83b368d20e
lnwallet/channel: use remoteACKedIndex instead of logIndex in
availableBalance
2018-02-02 21:16:37 -05:00
Jim Posen
916b83a6ee lnwallet: Implement test vectors from BOLT 03, Appendix C.
Appendix C of BOLT 03 contains a series of test vectors asserting that
commitment, HTLC success, and HTLC timeout transactions are created
correctly. Here the test cases are transcribed to Go structs and
verified.

We also break out some logic need to tests that bypass the constructor
and remove some redundant fields.
2018-01-31 14:30:56 -08:00
Olaoluwa Osuntokun
f85f1f97ca
lnwallet: add the second level witness script to the HtlcRetribution
In this commit, we add the second level witness script to the
HtlcRetribution struct. We do this as it’s possible that we when
attempt to sweep funds after a channel breach, then the remote party
has already gone to the second layer. In this case, we’ll then need to
update our SignDesc and also the witness, in order to do that we need
this script that’ll get us pass the second layer P2WSH check.
2018-01-22 19:19:58 -08:00
Olaoluwa Osuntokun
1604c75a9c
lnwallet: modify CreateCloseProposal to also return the close txid and local balance 2018-01-22 19:19:53 -08:00
Olaoluwa Osuntokun
5bbe126c34
lnwallet: add new NewUnilateralCloseSummary function
In this commit, we add a new function that allows a caller to create a
UnilateralCloseSummary with the proper materials. This will be used
within a new sub-system to be added in a later commit to properly
dispatch notifications when on-chain events happen for a channel.
2018-01-22 19:19:47 -08:00
Olaoluwa Osuntokun
341c1678fc
lnwallet: publicly export NewBreachRetribution 2018-01-22 19:19:47 -08:00
Olaoluwa Osuntokun
30c4196f91
lnwallet: remove the closeObserver from the channel state machine
In this PR, we entirely remove the closeObserver from the channel state
machine. It was added very early on before most of the other aspects of
the daemon were built out. This goroutine was responsible for
dispatching notifications to outside parties if the commitment
transaction was spent at all. This had several issues, since it was
linked to the *lifetime* of the channel state machine itself. As a
result of this linkage, we had to do weird stuff like hand off in
memory pointers to the state machine in order to ensure notifications
were properly dispatched.
2018-01-22 19:19:47 -08:00
Olaoluwa Osuntokun
b5496c52a2
lnwallet: newHtlcResolution now returns both incoming and outgoing HTLC resolutions
In this commit, we’ve added a new HtlcResolutions struct to house both
the incoming and outgoing HTLC resolutions. This struct will now be
coupled with the object that returns when we detect that a commitment
transaction was closed on chain. For incoming HTLC’s, we’ll check the
preimage cache to see if we can claim the HTLC on-chain. If we can,
then we’ll copy of the preimage, and make a proper incoming HTLC
resolution.
2018-01-22 19:19:32 -08:00
Olaoluwa Osuntokun
d98026f579
lnwallet: modify OutgoingHtlcResolution to also account for remote force closes
In this commit, we modify the OutgoingHtlcResolution struct to detect
if this is the remote party’s commitment transaction or not. With this
change, we’ll now be able to properly time out an HTLC that was
detected on the commitment transaction of the remote peer.
Additionally, we now populate the CsvDelay (if local commitment) and
the ClaimOutpoint (as we may be sweeping directly from the commitment
transaction now.
2018-01-22 19:19:32 -08:00
Olaoluwa Osuntokun
bcfe7192d7
lnwallet: add new IncomingHtlcResolution for resolving incoming HTLC's on-chain
In this commit, we add a new IncomingHtlcResolution struct. This is the
opposite of the existing OutgoingHtlcResolution struct. The items in
this new struct allow callers to sweep an incoming HTLC that we know
the preimage to. These will always be created when a commitment goes
on-chain. However, if we know the preimage, then that will be populated
in place of all zeroes in the Preimage field.
2018-01-22 19:19:31 -08:00
Olaoluwa Osuntokun
ecf6d758a2
lnwallet: add distinct CommitOutputResolution for resolving commit outputs on-chain
In this commit, we modify both the ForceCloseSummary, and the
UnilateralClosureSummary to return the items needed to sweep the
commitment output distinctly. By doing this, it’s now possible to pass
a dedicated struct to a sub-system in order to allow it to sweep a
commitment output. As the maturity delay is a part of this new struct,
this tells the caller if this was on the local commitment (CSV
required) or on the remote commitment (no CSV required).
2018-01-22 19:19:31 -08:00