Commit Graph

270 Commits

Author SHA1 Message Date
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
Olaoluwa Osuntokun
42cd2fa5be
lnwallet: add new ActiveHtlcs to channel state machine
In this commit, we’ve added a new method to the channel state machine:
ActiveHtlcs. This method will allow callers to poll the state of the
channel to retrieve the set of HTLC’s active on *both* commitment
transactions.
2018-01-22 19:19:31 -08:00
Olaoluwa Osuntokun
5f6c15cfa4
lnwallet: RevokeCurrentCommitment now returns the set of active HTLC's
In this commit, we modify the RevokeCurrentCommitment method to now
return the set of active HTLC’s. This will be used by callers in the
future to update other sub-systems when the set of HTLC’s on the
commitment changes, and can also be used on the RPC level to
synchronize systems level integration tests.
2018-01-22 19:19:30 -08:00
Olaoluwa Osuntokun
75d2f09d4c
lnwallet: add a PreimageCache instead of a FeeEstimator as LightningChannel dep 2018-01-22 19:19:29 -08:00
Olaoluwa Osuntokun
9a76b3ee58
lnwallet: only forward freshly locked in HTLC's in ReceiveRevocation
This commit fixes a nasty bug that has been lingering within lnd, and
has been noticed due to the added retransmission logic. Before this
commit, upon a restart, if we had an active HTLC and received a new
commitment update, then we would re-forward ALL active HTLC’s. This
could at times lead to a nasty cycle:
  * We re-forward an HTLC already processed.
  * We then notice that the time-lock is out of date (retransmitted
HTLC), so we go to fail it.
  * This is detected as a replay attack, so we send an
UpdateMalformedHTLC
  * This second failure ends up creating a nil entry in the log,
leading to a panic.
  * Remote party disconnects.
  * Upon reconnect we send again as we need to retransmit the changes,
this goes on forever.

In order to fix this, we now ensure that we only forward HTLC’s that
have been newly locked in at this next state. With this, we now avoid
the loop described above, and also ensure that we don’t accidentally
attempt an HTLC replay attack on our selves.

Fixes #528.

Fixes #545.
2018-01-08 19:50:25 -08:00
Olaoluwa Osuntokun
b4bdd55081
lnwallet: if we fail to validate a remote commit sig, return detailed error
In this commit, we add a new detailed error that’s to be returned
when/if the remote peer sends us an invalid commit signature. The new
error contains the transaction that we attempted to validate the
signature over, the sighs, and the state number. Returning this
additional information will serve to aide in debugging any
cross-implementation issues.
2018-01-08 19:50:22 -08:00
Olaoluwa Osuntokun
95de109844
lnwallet: return our final local balance from CompleteCooperativeClose
In this commit, add an additional return value to
CompleteCooperativeClose. We’ll now report to the caller our final
balance in the cooperative closure transaction. We report this as
depending on if we’re the initiator or not, our final balance may not
exactly match the balance we had in the last state.
2018-01-08 19:50:20 -08:00
Conner Fromknecht
9703ab9161
lnwallet/channel: exposes channelState via State for testing 2018-01-05 13:47:17 -08:00
Conner Fromknecht
bb8c5f82da
lnwallet/channel: delete state after ack from breach arb 2018-01-05 13:47:16 -08: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
Matt Drollette
adf0d98194 multi: fix several typos in godoc comments 2017-12-17 18:40:05 -08:00
Jim Posen
e66f3a37a3 lnwallet: Remove rHashMap.
rHashMap in LightningChannel is no longer needed since HTLCs are now
identified by ID/index instead of payment hash.
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
Olaoluwa Osuntokun
ef520f49c3
lnwallet: during channel resynchronization detect lack of commitment window
In this commit, we extend the ProcessChanSyncMsg to detect a case where
we don’t have the necessary revocation window to send out a new commit.
This can arise if the remote party sends us a new state, but we haven’t
yet fully processed their FundingLocked message yet, so we would be
unable to create a new commitment state.

We fix this by enumerating each of our actions in the case of an error.
If we get ErrNoWindow, then this indicates that we can’t give the
remote party the commitment we would like to optimistically send over.
This isn’t an issue though, as in the next round, we’ll resynchronize
our state.
2017-12-11 15:42:15 -08:00
Olaoluwa Osuntokun
62da377f78
lnwallet: properly reject new commitment if next revocation point isn't known 2017-12-11 15:32:31 -08:00
Olaoluwa Osuntokun
ff6993bb5d
lnwallet: ensure the onion blob is copied over properly when restore log updates
In this commit, we fix an existing bug that would cause issues within
the switch due to a value not being properly set. Before this commit we
would copy a byte array into a slice without first creating the
necessary capacity for that slice. To fix this, we’ll now ensure that
the blob has the proper capacity before copying over. Several tests
have been updated to always set a fake onion blob.
2017-12-10 16:14:41 -08:00
Olaoluwa Osuntokun
5b4aa82667
lnwallet: don't sign new commitment if next revocation point is unknown
In this commit, we extend the initial check within SignNextCommitment
to bail out early if we don’t yet know the commitment point of the
remote party. This prevents a class of nil pointer panics if we attempt
to create a new state without yet having received the FundingLocked
message.
2017-12-10 16:10:57 -08:00
Olaoluwa Osuntokun
d6dcc4276c
lnwallet: add new IsPending method to the channel state machine
The IsPending method will allow callers to determine if a channel has
been fully confirmed or not.
2017-12-06 16:43:01 -08:00
Conner Fromknecht
e8590b12ba
lnwallet/channel: handle error from sigpool start 2017-12-02 17:16:43 -08:00
Olaoluwa Osuntokun
c27e87f168
lnwallet: update UpdateFee msg usage to latest API changes 2017-11-30 22:10:42 -08:00
Olaoluwa Osuntokun
4bec706b11
lnwallet: fix bug in htlcIsDust, outgoing HTLC on remote commit is htlcSuccess 2017-11-30 22:09:49 -08:00
Olaoluwa Osuntokun
94ba7f964d
channel: properly roll over fee from commit tx during co-op chan close
In this commit, we fix an existing bug within our cooperative channel
closing transaction generation. Before this commit, we wouldn’t account
for the fee already allocated within the commitment transaction. As a
result, we would calculate the evaluated balance considering the fee
incorrectly. In this commit, we fix this by adding the commitment fee
to the balance of the initiator when crafting the closing transaction
2017-11-29 16:14:22 -08:00
Olaoluwa Osuntokun
63c8c299ed
lnwallet: use remote height as reference when gauging channel breaches 2017-11-28 12:26:02 -08:00
Olaoluwa Osuntokun
483dc3f96a
lnwallet: only the initiator needs to validate new fee updates
In this commit, we fix an existing bug, as only the initiator needs to
validate any new fee updates. If the initiator sends an invalid fee,
then it will be rejected by the responder as it may put them below
their required reserve.
2017-11-27 12:24:08 -08:00
Olaoluwa Osuntokun
3e90000e2a
lnwallet: reject UpdateFee messages if it puts initiator below the water
In this commit, we ensure that we reject any UpdateFee messages if
after applying the update, the initiator doesn’t have enough funds to
actually pay for the new commitment state.

A test has been added to exercise this new behavior.
2017-11-26 14:08:59 -06:00
Olaoluwa Osuntokun
7d3e1308e4
lnwallet: add ability to properly retransmit UpdateFee state transitions
In this commit, we update the retransmission logic to ensure that we
properly retransmit any sent UpdateFee messages as part of a state
transition. When creating a CommitDiff, if we have a pending fee
update, then we’ll add that to the set of logs updates. When restoring
the commit diff from disk, if we encounter an UpdateFee entry, then
we’ll apply that as waiting to be ACK’d and skip adding it as a log
entry.

A new test has been added to excessive this new behavior.
2017-11-26 14:08:58 -06:00
Olaoluwa Osuntokun
5a1a3c7277
lnwallet: add IsInitiator and CommitFeeRate methods to LightningChannel 2017-11-23 23:10:18 -06:00
Olaoluwa Osuntokun
39295dc5aa
lnwallet+peer: add new ResetState method to channel state machine
In this commit, we add a new ResetState method to the channel state
machine which will reset the state of the channel to `channelOpen`. We
add this as before this commit, it was possible for a channel to shift
into the closing state, the closing negotiation be cancelled for
whatever reason, resulting the the channel held by the breachArbiter
unable to act to potential on-chain events.
2017-11-23 23:10:16 -06:00
Olaoluwa Osuntokun
98cba87fb1
lnwallet: decouple closeObserver from main quit channel in LightningChannel
In this commit, we fix an existing bug that had ramifications within
the operation of the lnd daemon. Before this commit, if the Stop()
method was called, then the closeObserver would exit as well. This
means that would no longer be watching for channel breaches on-chain,
and could miss either a cooperative channel closure or an actual
contract breach.

To fix this, we now introduce a new method to stop for closeObserver:
CancelObserver(). This should ONLY be called once either: the contract
has been fully settled on-chain, or whom ever is watching the relevant
signals has a newer version of the channel that it will watch instead.
2017-11-23 23:10:07 -06:00
Olaoluwa Osuntokun
98a8951e5b
lnwallet: CompleteCooperativeClose now accepts final fee in btcutil.Amount 2017-11-23 23:10:07 -06:00
Olaoluwa Osuntokun
e96c62db65
lnwallet: no longer return fee from CreateCloseProposal
In this commit, we modify CreateCloseProposal to no longer return the
same fee passed in. In the past, this method accepted a fee rat rather
than an absolute fee, and would return the computed absolute fee. Now
that the method takes the absolute fee directly, this is unnecessary.
2017-11-23 23:10:06 -06:00
Olaoluwa Osuntokun
9f215723a6
lnwallet: verify new optional ChannelReestablish fields in ProcessChanSyncMsg
In this commit, we add fully verification (other than checking the
commitment point matches after the fact) of the new optional fields
added to the lnwire.ChannelReestablish message. Two scenarios can
arise: we realize the remote party is on a prior state (and possibly
lost data), or we realize that *we* are on a prior state with the
remote party verifiably proving that they’re on a newer state.
2017-11-16 20:00:05 -08:00
Olaoluwa Osuntokun
a655396616
lnwallet: add optional data loss support to ChanSyncMsg()
In this commit we extend the set of fields populated within the
returned lnwire.ChannelReestablish to populate the optional data loss
fields. This entails included the commitment secret of the most
recently revoked remote commitment transaction and also our current
unrevoked commitment point.
2017-11-16 20:00:04 -08:00
Olaoluwa Osuntokun
d7cdf822e3
lnwallet: update commitmentKeyRing to account for distinct HTLC keys
In this commit, we update all the key derivation within the state
machine to account for the recent spec change which introduces a
distinct key for usages within all HTLC scripts. This change means that
the commitment payment and delay base points, are only required to be
online in the case that a party is forced to go to chain.

We introduce an additional local tweak to the keyring for the HTLC
tweak. Additionally, two new keys have been added: a local and a remote
HTLC key. Generation of sender/receiver HTLC scripts now use the local
and remote HTLC keys rather than the “payment” key for each party.
Finally, when creating/verifying signatures for second-level HTLC
transactions, we use these the distinct HTLC keys, rather than re-using
the payment keys.
2017-11-16 20:00:03 -08:00
Olaoluwa Osuntokun
010815e280
lnwallet: no need to grab mutex in ProcessChanSyncMsg as is init method
In this method we fix an existing deadlock within the unit tests when
running with the race condition detector on. We don’t need to grab the
mutex within the ProcessChanSyncMsg method as this should be the very
first method called when initializing the channel if a channel state
sync is needed.
2017-11-11 15:50:33 -08:00
Olaoluwa Osuntokun
861412529d
lnwallet: add select on quit when waiting for job from sigPool
In this commit we ensure that the channel is always able to exit by
adding a select statement with a quit case when we’re waiting on the
result of a job that was previously sent into the sigPool.
2017-11-11 15:00:50 -08:00
Olaoluwa Osuntokun
3f4b1592a3
lnwallet: add logging error message is unable to carry HTLC in state machine 2017-11-10 19:51:13 -08:00
Olaoluwa Osuntokun
c8479410d2
lnwallet: remove availableLocalBalance field, add new AvailableBalance method
In this commit we do away with the existing availableLocalBalance
attribute and instead add a new, more accurate AvailableBalance method.
The new method will compute the available balance within the channel ,
assuming a new state was created at the instance the method was called.
This new method will now properly account for HTLC fees.

AvailableBalance is now called within AddHTLC in order to ensure we
don’t add any HTLC’s that are unable to be paid for from the PoV of the
fees on the commitment transaction.
2017-11-10 19:50:58 -08:00
Olaoluwa Osuntokun
ded7c11bfe
lnwallet: update channelState interaction to respect new db commitment format 2017-11-10 19:50:57 -08:00
Olaoluwa Osuntokun
b50a94aeab
lnwallet: update ReceiveRevocation to use new DB API's
We no longer need to manually pass in the channel delta to
AppendToRevocationLog (now called AdvanceCommitChainTail) as the
pointers on-disk will be updated atomically.
2017-11-10 19:50:57 -08:00
Olaoluwa Osuntokun
4fa714afce
lnwallet: update RevokeCurrentCommitment to use new DB API's
In this commit we update the RevokeCurrentCommitment method to properly
use the new database UpdateCommitment method along with properly
converting the in-memory commitment to its corresponding on-disk
format.
2017-11-10 19:50:56 -08:00
Olaoluwa Osuntokun
563c07ffa2
lnwallet: re-write and rename LastCounters to ChanSyncMsg
The new version has additional comments, and is also a bit simpler that
the prior version.
2017-11-10 19:50:56 -08:00
Olaoluwa Osuntokun
b5476b2767
lnwallet: re-write and rename ReceiveReestablish to ProcessChanSyncMsg
In this commit we complete the partially completed ReceiveReestablish
method and rename it to ProcessChanSyncMsg. The new version now
properly implements retransmission as defined within BOLT#2.

Additionally, we’ve added a new case which will optimistically try and
force a resynchronization of the commitment states if we detect we can
deliver a new commitment signature sooner than later after realizing
that we need to retransmit our last revocation message when we recevied
a new state transition.
2017-11-10 19:50:55 -08:00
Olaoluwa Osuntokun
769fe5cc13
lnwallet: when signing a new commitment state, store a CommitDiff 2017-11-10 19:50:55 -08:00