Commit Graph

104 Commits

Author SHA1 Message Date
Jim Posen
0becaddcd5 lnwallet: Move mocks to separate file and augment mockSigner.
To implement the BOLT 03 test vectors, a more powerful mockSigner is
required. The new version of mockSigner stores multiple keys and signs
the transaction outputs with the appropriate one.
2018-01-22 19:00:28 -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
ae1731da27
lnwallet: add test case for InvalidCommitSigError 2018-01-08 19:50:23 -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
20f4c61c8b
lnwallet/channel_test: adds TestBreachClose 2018-01-05 13:47:17 -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
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
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
c27e87f168
lnwallet: update UpdateFee msg usage to latest API changes 2017-11-30 22:10:42 -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
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
807784a1c4
lnwallet: fix ChanReserve calculation in createTestChannels to be non-random 2017-11-26 14:08:58 -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
661f27a404
lnwallet: update test due to recent API changes 2017-11-23 23:10:08 -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
6cd210041d
lnwallet: assert that no chan sync is needed before updates in TestChanSyncFullySynced 2017-11-16 20:00:05 -08:00
Olaoluwa Osuntokun
94ffb07b90
lnwallet: ensure created channels are stopped in ChanSync tests 2017-11-16 20:00:04 -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
b6f64932c2
htlcswitch: face race condition in unit tests by returning invoice
In this commit we modify the primary InvoiceRegistry interface within
the package to instead return a direct value for LookupInvoice rather
than a pointer. This fixes an existing race condition wherein a caller
could modify or read the value of the returned invoice.
2017-11-11 16:09:29 -08:00
Olaoluwa Osuntokun
1734f96544
lnwallet: add a set of unit tests to exercise the various ChanSync cases
In this commit, we’ve added a set of unit tests to cover all enumerated
channel sync scenarios, including the case where both nodes deem that
they’re unable to synchronize properly.
2017-11-10 19:51:01 -08:00
Olaoluwa Osuntokun
08c7fd9b4d
lnwallet: extend TestStateUpdatePersistence to assert proper htlc counters 2017-11-10 19:51:00 -08:00
Olaoluwa Osuntokun
32651e9311
lnwallet: revert commit 5240953de02d281be694b2c87d151d6c7dce2cb5
In this commit we revert a prior commit
(5240953de02d281be694b2c87d151d6c7dce2cb5) which was added as a stop
gap before we added the proper state needed to recover from cases where
the commitment transactions of both chains had diverged slightly due to
asymmetric dust limits.
2017-11-10 19:51:00 -08:00
Olaoluwa Osuntokun
e9d0e40a17
lnwallet: update existing channel unit tests to adhere to new channels API's 2017-11-10 19:51:00 -08:00
Olaoluwa Osuntokun
097e1c0f5c
lnwallet: update createTestChannels to adhere to new channeldb API's 2017-11-10 19:50:59 -08:00
Andrey Samokhvalov
e29193d550 htlcswitch+channel: remove cancel reasons from channel link 2017-11-09 16:38:58 -08:00
Jim Posen
a13ad0a339 multi: Fix various typos. 2017-10-25 13:20:54 -07:00
Olaoluwa Osuntokun
3b94e5df4d
lnwallet: introduce distinct HTLC counter+index on top of updateLog
In this commit, we fix an existing derivation from the commitment state
machine as defined within the specification. Before this commit, we
only kept a single counter which both HTLC adds and fails/settles would
share. This was valid in the prior pre-spec iteration of the state
machine. However in the current draft of the spec, only a distinct
counter for HTLCs are used throughout.

This would cause an incompatibility, as if we mixed adds and settles
during an exchange, then our counter values would differ with other
implementations. To remedy this, we now introduce a distinct HTLC
counter and index within the updateLog.

Each Add will increment both the log counter, and the HTLC counter.
Each Settle/Fail will only increment the log counter. Inbound
Settle/Fails will index into the HTLC index as to target the proper
HTLC. The PaymentDescriptor type has been extended with an additional
field (HltcIndex) which itself tracks the index of an incoming/outgoing
HTLC.
2017-10-22 18:36:57 -07:00
nsa
aa6395874d wallet: channel test fee fix for added HTLC
This commit fixes the TestChannelBalanceDustLimit unit test in
channel_test.go. The unit test does not account for the fees
required by adding an HTLC. As a result, Alice's balance according
to her local and remote commitment chains drops below 0 at certain
points. By using the correct fee, this is avoided.
2017-10-19 20:04:04 -07:00
Olaoluwa Osuntokun
d79c0ff95b
lnwallet: expand TestForceClose to ensure outgoing HTLC's can be swept
This commit expands the existing TestForceClose test case to add an
HTLC (outgoing) to Alice’s commitment transaction before force closing.
We then ensure that both the pre-signed timeout transaction _and_ the
sign descriptor to sweep the second-level output are fully valid.
2017-09-29 14:38:42 -07:00
Olaoluwa Osuntokun
927fb2ea0a
lnwallet: split off new TestForceCloseDustOutput case from TestForceClose
This commit extracts the ending dust adherence test case from the
existing TestForceClose test case into a distinct test case. With this
modification, we now ensure that the two new tests are focused and test
a single scenario at at time.
2017-09-29 14:13:28 -07:00
Olaoluwa Osuntokun
21d151d2ce
lnwallet: ensure the goroutines of all channels are stopped after test execution 2017-09-24 20:20:15 -07:00
Olaoluwa Osuntokun
9164cc218a
lnwallet: modify ReceiveFailHTLC to return value of HTLC failed 2017-09-24 20:17:53 -07:00
Olaoluwa Osuntokun
f576a74bf6
lnwallet: modify SettleHTLC to return value of HTLC settled
This commit adds an additional return value to SettleHTLC in order to
make way for an upcoming change to modify the way bandwidth status from
the link to the switch is reported.
2017-09-24 20:17:14 -07:00
Olaoluwa Osuntokun
2bf8d1b3fb
lnwallet: ensure all channel tests run in parallel 2017-09-24 20:14:30 -07:00
Olaoluwa Osuntokun
c85fcf2885
lnwallet: add test case for ErrInsufficientBalance error in channel state machine 2017-09-24 20:13:46 -07:00
Olaoluwa Osuntokun
a52d405998
lnwallet: ensure HTLC values are properly converted to SAT in commit tx
This commit fixes a bug within the HTLC construction and commitment
transaction construction that would result in HTLC _values_ within the
commitment transaction being off by a factor of 1000. This was due to
the fact that we failed to convert the amount of an HTLC, in mSAT, to
SAT before placing it as an output within the commitment transaction.
When attempt to locate the output index of a particular half, we use
the unconverted amount, meaning it was unnoticed.

This commit adds a new assertion within the TestSimpleAddSettleWorkflow
test to ensure that the HTLC is found within the commitment transaction
with the proper value in satoshi.
2017-08-30 15:34:18 -07:00
Olaoluwa Osuntokun
7f575b688e
multi: txscript.WitnessScript -> txscript.WitnessSignature 2017-08-24 18:55:32 -07:00
Olaoluwa Osuntokun
6e54aba7ac
lnwallet: convert channel state machine accounting to milli-satoshi 2017-08-22 00:52:50 -07:00
Johan T. Halseth
b9516b7cdd
lnwallet: make CreateCloseProposal take absolute fee instead of fee rate 2017-08-11 12:09:39 +02:00
Olaoluwa Osuntokun
5240953de0
lnwallet: temporarily ensure TestStateUpdatePersistence doesn't make dust
Note that this commit is temporary, and should be reverted once #231 is
merged. The reason we need to do this for now, is that we don’t
properly track the exact state of the remote party’s commitment. In
this test case, the resulting HTLC’s added are dust to one party, but
non-dust to another. So upon restart, the states (balance wise) has
diverged.
2017-07-31 20:53:08 -07:00
Olaoluwa Osuntokun
8c6a83a67d
lnwallet: account for second-level HTLC fee in TestChannelBalanceDustLimit 2017-07-31 20:50:54 -07:00
Olaoluwa Osuntokun
4aacf2b51d
lnwallet: ensure TestHTLCDustLimit also asserts for bob's commit chain 2017-07-31 20:50:12 -07:00
Olaoluwa Osuntokun
350e6d5c9f
lnwallet: extend TestDustHTLCFees to ensure dust HTLC's go to miners 2017-07-31 20:49:13 -07:00
Olaoluwa Osuntokun
00ca97c789
lnwallet: in TestForceClose properly assign Bob's balance 2017-07-31 20:44:22 -07:00
Olaoluwa Osuntokun
1b61341410
lnwallet: when creating channels in tests, properly account for commitment fee 2017-07-31 20:43:38 -07:00
Olaoluwa Osuntokun
747e0f57d1
multi: fix linter errors 2017-07-30 18:22:45 -07:00