Commit Graph

255 Commits

Author SHA1 Message Date
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
Olaoluwa Osuntokun
611412bab0
lnwallet: add new createCommitDiff method
This commit adds a new method: createCommitDiff. The method will, given
a newly constructed commitment, its signature, and HTLC signatures will
create a channeldb.CommitDiff. The CommitDiff created is to be stored
on disk, as it can be used in the case that the remote party didn’t
receive our CommitSig message and also forgot all the updates that we
queued with the update.
2017-11-10 19:50:55 -08:00
Olaoluwa Osuntokun
e50b3e6517
lnwallet: populate htlc indexes within constructed commitment states 2017-11-10 19:50:54 -08:00
Olaoluwa Osuntokun
9c015a5824
lnwallet: revamp restoring channel state from disk after a restart
In this commit we complexly revamp the process of restoring all channel
state back into memory after a restart. We’ll now properly do the
following: restore the pending “dangling” commit of the remote party
into the in-memory commitment chain, re-populate all active HTLC’s back
into their respective update logs with the proper indexes/counters, and
properly restore the current commitment of the remote party back in
memory.
2017-11-10 19:50:54 -08:00
Olaoluwa Osuntokun
4b71e87b77
lnwallet: add new restoreHtlc method to the updateLog
This commit adds a new method to the updateLog which will be used when
restoring the state of a channel from disk after a restart. This new
method will add an entry to the updateLog without incrementing either
of the counters as the HTLC already comes pre populated with its
historical index.
2017-11-10 19:50:53 -08:00
Olaoluwa Osuntokun
51618f2ae3
lnwallet: modify populateHtlcIndexes to use info in commitment
It wasn’t necessary to pass in the dust limit, or the commitment itself
as it is already contained within the commitment struct.
2017-11-10 19:50:53 -08:00
Olaoluwa Osuntokun
60b814b4aa
lnwallet: group commitmentKeyRing and deriveCommitmentKey in file 2017-11-10 19:50:52 -08:00
Olaoluwa Osuntokun
d790eeb375
lnwallet: add [our|their]HtlcIndex to the commitment struct
With these new fields, we’ll be able to properly reconstruct the log
state after a restart, as each commitment will now note both the
current HTLC and log index.
2017-11-10 19:50:52 -08:00
Olaoluwa Osuntokun
145cd0b2b6
lnwallet: export HtlcWeight and CommitWeight publicly 2017-11-10 19:50:51 -08:00
Andrey Samokhvalov
9247168c5d not finished index persistence 2017-11-09 16:38:58 -08:00
Andrey Samokhvalov
28dd6e5d84 channeldb+lnwallet: add remote pending commiment persistance 2017-11-09 16:38:58 -08:00
Andrey Samokhvalov
a10ed36e8f htlcswitch+lnwallet: add malformed payment descriptor 2017-11-09 16:38:58 -08:00
Andrey Samokhvalov
e29193d550 htlcswitch+channel: remove cancel reasons from channel link 2017-11-09 16:38:58 -08:00
Andrey Samokhvalov
1b4e723a5d htlcswicth+channel: switch to store onion blobs in payment descriptor
After addition of the retransmission logic in the channel link, we
should make the onion blobs persistant, the proper way to do this is
include the onion blobs in the payment descriptor rather than storing
them in the distinct struct in the channel link.
2017-11-09 16:38:57 -08:00
Andrey Samokhvalov
d70ffe93e4 htlcswitch+channel: add channel states synchronization
In this commit BOLT№2 retranmission logic for the channel link have
been added. Now if channel link have been initialised with the
'SyncState' field than it will send the lnwire.ChannelReestablish
message and will be waiting for receiving the same message from remote
side. Exchange of this message allow both sides understand which
updates they should exchange with each other in order sync their
states.
2017-11-09 16:38:57 -08:00
Jim Posen
a13ad0a339 multi: Fix various typos. 2017-10-25 13:20:54 -07:00