Commit Graph

813 Commits

Author SHA1 Message Date
Olaoluwa Osuntokun
1d7a1ac0ee
lnwallet: update channel state machine tests to use distinct keys
In this commit, we update the channel state machine tests to use a new
key for each purpose. Before this commit, the same key would be used
the entire time. As a result, a few bugs slipped by that would’ve been
detected if we used fresh keys for each purpose. Additionally, this
reflect the real world case as we always use distinct keys for each
purpose to avoid key re-use.
2018-01-22 19:19:32 -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
b1503adfad
lnwallet: add new PreimageCache interface 2018-01-22 19:19:29 -08:00
Olaoluwa Osuntokun
a9d451e46d
lnwallet: add AcceptedHtlcTimeoutWitnessSize to size.go 2018-01-22 19:19:29 -08:00
Olaoluwa Osuntokun
774ec5b528
lnwallet: rename OfferedHtlcWitnessSize to OfferedHtlcSuccessWitnessSize 2018-01-22 19:19:28 -08:00
Olaoluwa Osuntokun
65619b11aa
lnwallet: modify TxWeightEstimator methods to allow chaining
By returning a *TxWeightEstimator from each method, we now all callers
to chain the methods. This adds a bit of nice sugar when interacting
with the struct.
2018-01-22 19:19:28 -08:00
Olaoluwa Osuntokun
ff872b798c
lnwallet: update WitnessType names to be more descriptive
In this commit, we rename several of the existing WitnessType
definitions to be more descriptive than they were previously. We also
add a number of additional types which we need to handle scripts for,
but weren’t yet added before. Finally, we modify the
receiverHtlcSpendTimeout to optionally take an additional parameter to
set the locktime of the spending transaction accordingly. This final
modification allows the caller to specify that the lock time has
already been set on the main transaction.
2018-01-22 19:19:27 -08:00
Olaoluwa Osuntokun
3fa0cde631
lnwallet: export senderHtlcSpendRedeem and make rename HtlcSpendSuccess to HtlcSecondLevelSpend 2018-01-22 19:19:21 -08:00
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
Alex
187f59556a multi: add bitcoind drivers and tests 2018-01-15 13:59:34 -08:00
Conner Fromknecht
c17b695128
lnwallet/btcwallet/blockchain: properly handle nil spend report
This commit adds an additional check in GetUtxo that
tests for the nil-ness of the spend report returned by
the neutrino backend. Previously, a nil error and
spend report could be returned if the rescan did not
find the output at or above the start height. This
was observed to have cause a nil pointer dereference
when the returning line attempted to access the output.
This case is now handled by returning a distinct error
signaling that the output was not found.
2018-01-09 18:14:25 -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
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
20f4c61c8b
lnwallet/channel_test: adds TestBreachClose 2018-01-05 13:47:17 -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
Johan T. Halseth
8e120d1e62 lnwallet test: add FFAnnounceChan flag to reservation init 2017-12-17 18:35:34 -08:00
Johan T. Halseth
fffe15f0fd lnwallet: add FundingFlag parameter to InitChannelReservation 2017-12-17 18:35:34 -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
bead1ba31d
lnwallet: properly check error when creating commit tx in funding flow 2017-12-14 14:55:03 -08:00
Olaoluwa Osuntokun
5e5cc9b209
lnwallet: add new method to register our preferred minHTLC value
Before this commit, during a reservation, we wouldn’t ever specify our
minHTL value. We don’t yet fully validate all channel constrains, but
doing this now serves to ensure that once those features are merged,
we’ll actually be setting a valid value for minHTLC.
2017-12-13 17:15:43 -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
Micah Lerner
231ed5bb32 lnwallet: Update tests to check destination addresses 2017-12-08 15:11:23 -08:00
Micah Lerner
6ae7d275ab lnwallet: Add destination addresses to listchaintxns 2017-12-08 15:11:23 -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
b8bed9a677
lnwallet: in BtcdFeeEstimator is sat/byte is too low, fallback to default rate
In this commit, we fix an existing bug within the EstimateFeePerWeight
method for the BtcdFeeEstimator. If the sat/byte value returned was too
low, then it was possible for us to end up with a zero valued
sat/weight. We correct this issue by detecting, and falling back to the
default fee rate if so.
2017-11-30 22:13:00 -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
Micah Lerner
b98e993d76 lnallet: remove a TODO from signer.go
The TODO indicated that the fmt.Errorf call should be changed
to use the errors package, which allows for wrapping of errors
as necessary.
2017-11-27 20:09:05 -08:00
Liu-Cheng Xu
2fb7f5d4f3 lnwallet: fix typos 2017-11-27 17:24:45 -06: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
52e6cb1a06
lnwallet: correct BTC -> SAT conversion in BtcdFeeEstimator
In this commit, we correct the BTC -> SAT conversion in
BtcdFeeEstimator. Previously, we use 10e8 instead of 1e8, causing us to
be off by an order of magnitude.
2017-11-26 14:08:57 -06:00
Olaoluwa Osuntokun
91bb95991e
lnwallet: make 1% reservation computation clearer
Adding 99 here didn’t really do anything but obfuscate when we were
trying to compute. We’re just using internet division to calculate 1%
of the channel capacity amount. This is the amount that the remote
party must remain above at all times.
2017-11-26 14:08:57 -06:00
Olaoluwa Osuntokun
24ad3e17de
lnwallet: reject funding flows if local amount is insufficient w.r.t fees 2017-11-26 14:08:56 -06:00
Olaoluwa Osuntokun
5a1a3c7277
lnwallet: add IsInitiator and CommitFeeRate methods to LightningChannel 2017-11-23 23:10:18 -06:00
Olaoluwa Osuntokun
1b716e6c87
lnwallet: update testListTransactionDetails to account for tx fee 2017-11-23 23:10:17 -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
661f27a404
lnwallet: update test due to recent API changes 2017-11-23 23:10:08 -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
6d10677ef5
lnwallet: allow callers to specify fee for funding transaction in funding flow
In this commit, we modify the funding reservation workflow slightly to
allow callers to specify their own custom fee when initialization a
funding workflow. This gives power-users the ability to control exactly
how much in fees are paid for each new funding transaction.
2017-11-23 23:10:06 -06:00
Olaoluwa Osuntokun
36256f3f39
lnwallet/btcwallet: update SendOutputs implementation due to recent API changes 2017-11-23 23:10:05 -06:00
Olaoluwa Osuntokun
8b6842c760
lnwallet: update interface level tests to adhere to recent API changes 2017-11-23 23:10:04 -06:00
Olaoluwa Osuntokun
4a277276fd
lnwallet: update SendOutputs method on WalletController to accept fee
In this commit, we extend the existing SendOutputs method on the
WalletController interface to be able to accept a custom fee. With
this, users are now able to specify their exact fee, allowing the
wallet to be fee estimation agnostic.
2017-11-23 23:10:04 -06:00
Olaoluwa Osuntokun
78ccfb4989
lnwallet: add BtcdFeeEstimator implementation of FeeEstimator interface
In this commit, we add a new implementation of the FeeEstimator
interface: the BtcdFeeEstimator. This implementation of the
FeeEstimator is backed by an active bcd instance. Any requests to query
for the current fee for a given confirmation target are proxied to this
active bcd instance.
2017-11-23 23:10:02 -06:00
Olaoluwa Osuntokun
e20448ebc6
lnwallet: update StaticFeeEstimator to adhere to new FeeEstimator interface 2017-11-23 23:10:02 -06:00
Olaoluwa Osuntokun
b4855b687e
lnwallet: add Start() and Stop() method to the FeeEstimator interface 2017-11-23 23:10:01 -06:00
Olaoluwa Osuntokun
0713c8c7ce
lnwallet: extend FeeEstimator methods to be able to return an error
In this commit, we extend the FeeEstimator methods to allow them to
return an error. This is required as most implementations aside from
the static fee estimator will want to be able to return errors to users
to indicate the inability to properly estimate fees.
2017-11-23 23:10:01 -06:00
Olaoluwa Osuntokun
6ef2770e57 lnwallet: move FeeEstimator into distinct file
In this commit, we move the FeeEstimator interface into a distinct file
as follow up commits will begin to flesh out the interface with
additional implementations.
2017-11-23 22:58:54 -06:00
andrew.shvv
4168c97a27 lnwallet: fix infinite loop in coin selection
Fix wrong calculation of overshot amount which causes coin select
function to go into infinite loop. If overshoot amount is calculated
by subtraction of totalSatoshis and amtNeeded than on the second
iteration of loop amtNeeded already include required fee inside, which
causes continuation of the coin selection loop.
2017-11-17 13:15:50 -08: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
a812974808
lnwallet: update sender/receiver HTLC scripts to use distinct HTLC keys
In this commit, we modify the naming and comments of the routines that
create the sender/receiver HTLC scripts to denote that a set of
distinct keys are used. The scripts materially haven’t changed, but the
set of keys expected has.
2017-11-16 20:00:02 -08:00
Olaoluwa Osuntokun
8b0de51003
lnwallet: populate our HtlcBasePoint when reserving a funding reservation 2017-11-16 20:00:01 -08:00
Conner Fromknecht
35e3201097
lnwallet/script_utils: adds public HtlcSpendSuccess witness func 2017-11-15 18:08:33 -08:00
Conner Fromknecht
305acb86d0
lnwallet/size: adds htlc success and timeout witness sizes 2017-11-15 18:04:44 -08:00
Conner Fromknecht
7888c6e040
lnwallet/witnessgen: add HtlcOfferedTimeLock witness type 2017-11-15 18:04:44 -08:00
Olaoluwa Osuntokun
e7e426a1cc
Merge pull request #398 from halseth/sighash-signdesc-hashtype
Use dynamic SigHash type when signing transactions.
2017-11-11 17:30:54 -08:00
Alex
9bc25df906 lnwallet: fix linter complaints re. capitalization in interface test 2017-11-11 17:24:48 -08:00
Alex
7b43c9a750 lnwallet: add timeout and more accurate check to reorg interface test 2017-11-11 17:24:48 -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
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
03ba13fcf8
lnwallet: ensure sigPool goroutines can always exit 2017-11-11 14:59:56 -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
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
Olaoluwa Osuntokun
7f667e2dbc
lnwallet: update funding flow to use new channeldb API's 2017-11-10 19:50:59 -08:00
Olaoluwa Osuntokun
367c6b10fc
lnwallet: add missing OP_CHECKSIG op in senderHTLCScript comment 2017-11-10 19:50:58 -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
Johan T. Halseth
dd4996b4d5
lnwallet: use signDesc.HashType for sweepsig in script_utils
This commit changes the use of SigHash flags in the spend
transactions created in scrit_utils. Instead of always
using SigHashAll for the sweep signature, we instead use
the sighash flag specified by the passed sign descriptor.
2017-11-06 14:31:21 +01:00
Johan T. Halseth
f12dfe2c45
lnwallet/btcwallet: Use signDesc.HashType when signing
Tis commit makes the btcwallet signer implementation use
signDesc.HashType instead of SigHashAll when signing
transactions. This will allow the creator of the transaction
to specify the sighash policy when creating the accompanying
sign descriptior.
2017-11-06 14:31:03 +01:00
Jim Posen
a13ad0a339 multi: Fix various typos. 2017-10-25 13:20:54 -07:00
Jim Posen
2b58a39d30 lnwallet: Simplify updateLog struct by removing redundant pointers.
The updateLog struct contains pointers that refer to the state of the
commitment chains. Instead, query the commitments directly.
2017-10-25 13:11:46 -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
Jim Posen
7c1ae8bda3 lnwallet: Split tx generation code out of fetchCommitmentView.
This moves the commitment transaction generation code out of
fetchCommitmentView into createCommitmentTx. Aside from being a pretty
clean logical split, this allows the transaction generation code to be
unit tested more effectively.
2017-10-19 22:23:14 -07:00
Jim Posen
bd497438af lnwallet: Pass around key ring instead of individual keys.
The signatures of some functions/methods in lnwallet are simplified by
passing in a commitmentKeyRing argument instead of multiple keys.
2017-10-19 22:23:14 -07:00
Jim Posen
3151a3a596 lnwallet: Refactor commitment key generation code.
Create struct holding all commitment keys to clean up code and avoid
deriving keys multiple times.
2017-10-19 22:23:14 -07:00
Alex
0994852396 lnwallet: add reorg test 2017-10-19 21:26:31 -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
Johan T. Halseth
e6cc46d84e lnwallet: export btcwallet.NetworkDir 2017-10-19 19:17:35 -07:00
Jim Posen
b29af869c7 lnwallet: Support funding tx fee estimation for more input types.
The fee estimation for funding transactions now properly accounts for
different types of UTXOs spent, whereas previously it assumed all
inputs were spending native P2WKH outputs.
2017-10-19 17:37:53 -07:00
Jim Posen
3232fd71c2 lnwallet: Add TxWeightEstimator support for nested pay-to-witness. 2017-10-19 17:37:53 -07:00
Jim Posen
c94130328a lnwallet: Extend Utxo struct with AddressType.
The Utxo struct now includes the address type and redeem/witness
scripts. This is necessary for accurate fee estimation.
2017-10-19 17:37:53 -07:00
Jim Posen
10a336db46 lnwallet: Use TxWeightEstimator in breacharbiter. 2017-10-19 17:37:53 -07:00
Jim Posen
ced530f98e lnwallet: Use TxWeightEstimator for funding transaction. 2017-10-19 17:37:53 -07:00
Jim Posen
fb32c3f73d lnwallet: Create type to estimate transaction weights.
The new TxWeightEstimator type can be used to replace the fee
estimation code for funding transactions and transactions in
breacharbiter.
2017-10-19 17:37:53 -07:00
Jim Posen
7807150e73 lnwallet: Implement BOLT-03 key derivation test vectors. 2017-10-05 16:54:38 -07:00
Laura Cressman
156772d04a channeldb: use binary.Read/Write with bools in channel.go
Use binary.Read/Write in functions to serialize and deserialize
channel close summary and HTLC boolean data, as well as in
methods to put and fetch channel funding info. Remove lnd
implementations of readBool and writeBool as they are no
longer needed. Also fix a few minor typos.
2017-10-02 23:13:47 -07:00
flaurida
e106cfdef1 lnwallet: replace sort.Sort with sort.Slice in channel.go
Use sort.Slice in SignNextCommitment function in lnwallet/channel.go,
as part of the move to use new language features. Remove
sortableSignBatch type wrapper for slice of signJobs since it is
no longer needed to sort jobs according to their output indices.
Also fix a few minor typos in channel.go and sigpool.go.
2017-10-02 23:13:47 -07:00
Jim Posen
40ce817235 lnwallet: Add missing opcode in senderHTLCScript.
As specified in BOLT 03.
2017-10-02 16:33:06 -07:00
Johan T. Halseth
6b3844ea66
lnwallet: add getter for channelState.RemoteNextCommitment 2017-10-02 13:29:32 +02:00
Olaoluwa Osuntokun
30d5b90626
lnwallet: properly derive key tweaks in newHtlcResolution
This commit fixes an existing bug where we attempted to re-use the same
commitTweak value when creating an HTLC resolution. Instead, we now
create the commit tweak value factoring the key that is to be used for
signing.
2017-09-29 20:27:19 -07:00
Braydon Fuller
53181f3ec3 cmd/lncli+lnwallet: specify need for witness outputs for funding channels
In this commit, we extend the help message for `newaddress` 
to indicate which address types can be used when directly 
funding channels. Additionally, we add some additional text 
to the insufficient funding error to detail that we don't have 
enough witness outputs.
2017-09-29 15:38:26 -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
dc764c992c
lnwallet: fix bug in extractHtlcResolutions, ensure slice is contiguous
This commit fixes an existing bug within extractHtlcResolutions. The
prior code would use an index to assign the returned
OutgoingHtlcResolutions into a single slice. However, this is invalid
as there are two cases where an HTLC might be skipped: if it’s an
incoming HTLC, or if the HLTC itself is dust from the PoV of the
commitment chain.

To fix this, we now instead use append to add items to the slice. This
ensure that we don’t have any “empty” items in between fully populated
items.
2017-09-29 14:32:22 -07:00
Olaoluwa Osuntokun
2141b481ef
lnwallet: properly set the pkScript within an HTLC resolution 2017-09-29 14:28:19 -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
0086e6e427
lnwallet: in extractHtlcResolutions use csvDelay gated on commitment ownership
This commit fixes a bug wherein we would use the incorrect csvDelay
when crafting HTLC resolutions after a unilateral channel closure.
Previously, we would always use the csvDelay of the local party, as in
the force close case that’s the correct value. However, a unilateral
channel closure instead requires the _remote_ delay.
2017-09-26 19:06:55 -07:00
Olaoluwa Osuntokun
21782374c9
lnwallet: properly use delayKey for htlc-timeout tx in newHtlcResolution
This commit fixes an existing bug when crafting the HTLC resolution in
the face of a commitment broadcast. Previously, we we’re using the
localKey which is incorrect, as directly below we properly use the
delayKey when crafting the secondLevelHtlcScript to sign.
2017-09-26 18:31:22 -07:00
Olaoluwa Osuntokun
8a682d9ec3
lnwallet: add a MaturityDelay field to UnilateralCloseSummary
This commit adds a new field: MaturityDelay, to the
UnilateralCloseSummary struct. This new field will be required, in an
upcoming update as it’s needed in order to properly sweep the
second-level HTLC outputs after MaturityDelay blocks has passed since
confirmation.
2017-09-26 18:00:24 -07:00
Olaoluwa Osuntokun
34604f6214
lnwallet: properly use the p2wkh script in SignDesc for retributions
This commit fixes a minor bug (that doesn’t affect anything atm) when
crafting the SignDesc for sweeping breached outputs. Previously, we
would take the p2wkh script and then p2wsh-ify that, placing that into
the SignDesc. This is incorrect as the p2wkh script is “injected” into
the sighash when signing, and thus doesn’t need another encoding layer.
2017-09-26 17:57:29 -07:00
Olaoluwa Osuntokun
1d487ea78b Merge pull request #341 from cfromknecht/breach-filter-commit-dust
Breach Arbiter Ignore Dust Commitment Outputs
2017-09-26 17:10:12 -07:00
Conner Fromknecht
223f77d135
lnwallet/channel: filter commitment outputs if dust 2017-09-25 18:57:03 -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
815826caac
lnwallet: remove public LocalAvailableBalance method from channel
This commit removes the current active LocalAvailableBalance method
from the channel state machine itself. We still maintain the internal
availableLocalBalance method locally as this is used to ensure that we
don’t add an HTLC which puts our available balance below zero.
2017-09-24 20:13:04 -07:00
Conner Fromknecht
47356493f3
lnwallet/signdesc: increases max witness parsing length 2017-09-19 19:18:21 -07:00
Conner Fromknecht
de1063200a
lnwallet/script_utils: adds public receiver+sender htlc revoke w/key derivation 2017-09-19 19:18:21 -07:00
Conner Fromknecht
84abc962c4
lnwallet/witnessgen: adds htlc offered+accepted revoke witness type 2017-09-19 19:18:21 -07:00
Conner Fromknecht
faf1d40bd0
lnwallet/channel: adds directionality htlc retributions
This commit also adds an incoming flag to
        HtlcRetribution struct to allow the breach arbiter to
        generate the appropriate witness based on the htlc's
        directionality.

        It also ensures that the size of the htlc retribution
        slice is now determined by the size of the number of
        htlcs present in the revoked snapshot, which fixes a
        minor bug that could lead to nil pointer deferences.
2017-09-19 19:18:20 -07:00
Conner Fromknecht
b3a80f7628
lnwallet/size: updated with recent BOLT 5 constants 2017-09-19 19:18:19 -07:00
Conner Fromknecht
da250cd79b
lnwallet/witnessgen: remove use of pointer to Signer iface 2017-09-19 19:18:19 -07:00
Conner Fromknecht
8ef2263e46 lnwallet: move txout serialization out of lnwire 2017-09-14 13:56:25 +02:00
Olaoluwa Osuntokun
67aa519480
lnwallet: reverse order of signatures when spending the funding output
This commit is a follow up to the prior commit, as since we reversed
the order of the pubkeys in the multi-sig scripts, then we also need to
reverse the order of the signatures that we use when attempting to
spend the funding output directly.
2017-09-12 17:54:21 +02:00
Olaoluwa Osuntokun
eb2c8ba653
lnwallet: reverse the order of the pubkeys in the funding output
When creating the script for the funding output, we were reversing the
order of the public keys due to an incorrect assertion of the return
value of the bytes.Compare function. To fix this, we now flip the
order, allowing us to properly create channels as specified within the
specification.
2017-09-12 17:49:37 +02:00
Olaoluwa Osuntokun
5359476936
lnwallet: add new methods to ChannelReservation for dealing w/ chan constraints
This commit adds to methods to the ChannelReservation struct: one for
generating the channel constraints we require for the remote party, and
one for validating their desired constraints, and committing them to
our ChannelConfig.

With these two new methods, we can now begin to properly store and
adhere to the current set of channel flow control constraints.
2017-09-12 17:41:55 +02:00
Olaoluwa Osuntokun
5bb3efba4c
lnwallet: properly use the lsat 6-bytes of the sha hash for state hints
This commit fixes an existing w.r.t the way that we constructed all
commitment transactions. We were computing the hash that the obfsucator
was derived form correctly, but we were using the first 6-bytes, rather
than the last 6 bytes.
2017-09-12 17:38:31 +02:00
Olaoluwa Osuntokun
73421caecc
lnwallet: don't attach the rpcclient to the lnwallet logging instance
We no longer attach the RPC client to the lnwallet logging instance as
it can generate a ton of spam in trace mode as it’ll dump the entire
hex encoded blocks, transactions, etc.
2017-08-30 20:32:37 -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
Conner Fromknecht
438c6b9621 lnwallet: move latest SignDescriptor + serialization 2017-08-25 17:56:50 -07:00
Philip Hayes
ce411f8e22 lint: fix linter complaints 2017-08-25 17:56:50 -07:00
Philip Hayes
d8c56433e3 lnwallet: extract SignDescriptor serialization and witness generator 2017-08-25 17:56:50 -07:00
Olaoluwa Osuntokun
7f575b688e
multi: txscript.WitnessScript -> txscript.WitnessSignature 2017-08-24 18:55:32 -07:00
Olaoluwa Osuntokun
9f0efddc20
multi: switch from btcrpcclient to rpcclient 2017-08-24 18:54:24 -07:00
Olaoluwa Osuntokun
ad00266451
lnwallet: update channel reservation flow to use milli-satoshis 2017-08-22 00:52:53 -07:00
Olaoluwa Osuntokun
6e54aba7ac
lnwallet: convert channel state machine accounting to milli-satoshi 2017-08-22 00:52:50 -07:00
Olaoluwa Osuntokun
063525c6e0
channeldb: update the OutgoingPayment struct to use lnwire.MilliSatoshi 2017-08-22 00:52:12 -07:00
Olaoluwa Osuntokun
6e17c34229
lnwallet: update commitScriptToSelf to match BOLT-0003
This commit updates the script we use to match the current
specification. The change is minor: we can say an extra byte by moving
the OP_CHECKSIG to the end of the script, and swapping the checks and
seqverify operations in the second clause. However, the witness remains
the same!
2017-08-22 00:52:09 -07:00
Olaoluwa Osuntokun
f458dc26ed
lnwallet: correct godoc comments on script util helper functions
The godoc comments for htlcSpendRevoke and htlcSpendSuccess were
incorrect.
2017-08-15 17:53:45 -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
43b736225b
multi: add new method to generate fresh node announcments 2017-08-04 18:32:33 -07:00
Olaoluwa Osuntokun
812ebe6fe6
lnwallet: type htlcWeight and commitWeight as int64's 2017-07-31 21:02:30 -07: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
db793991ac
lnwallet: don't add trimmed HTLC value to initiator commitment fees
This commit fixes a lingering bug in the way the internal channel state
machine handled fee calculation. Previously, we would count the dust
HTLC’s that were trimmed towards the fee that the initiator paid. This
is invalid as otherwise, the initiator would always benefit from dust
HTLC’s. Instead, we now simply “donate” the dust HTLC’s to the miner in
the commitment transaction. This change puts us in compliance with
BOLT-0003.
2017-07-31 20:41:23 -07:00
Olaoluwa Osuntokun
5ece7fec2d
lnwallet: add a String() method to updateType 2017-07-31 20:37:51 -07:00
Olaoluwa Osuntokun
d4a5eaa6ad
lnwallet: modify CommitSpendNoDelay to directly craft witness
This commit modifies the CommitSpendNoDelay script witness generation
function. We must modify this function as all non-delayed outputs now
also require a key derivation. The current default
signer.ComputeInputScript implementation is unable to directly look up
the public key required as it attempt to target the pub key using the
pkScript.
2017-07-30 20:23:31 -07:00
Olaoluwa Osuntokun
747e0f57d1
multi: fix linter errors 2017-07-30 18:22:45 -07:00
Olaoluwa Osuntokun
07b0604458
lnwallet: add TweakPubKeyWithTweak helper function
This commit adds a new helper function which is identical to
TweakPubkey, but lets the caller specify their own hash tweak.
2017-07-30 17:52:22 -07:00
Olaoluwa Osuntokun
899fa1ea3e
lnwallet: properly populate the signDesc within UnilateralCloseSummary
This commit modifies the closeObserver code to populate the signDesc in
the case we have a non-trimmed balance. Additionally, we now also add a
*wire.OutPoint field to the struct in order to allow receivers of the
message to construct a witness that can spend the newly created output
to their wallet.
2017-07-30 17:52:18 -07:00
Olaoluwa Osuntokun
8a23de5303
lnwallet: consult current state instead of revocation log during unilateral closure 2017-07-30 17:52:14 -07:00
Olaoluwa Osuntokun
6ad9d218b0
lnwallet: update all channel unittests due to new commitment design 2017-07-30 17:51:09 -07:00
Olaoluwa Osuntokun
8121bc77ce
lnwallet: add delivery scripts as a param to CreateCloseProposal+ CompleteCooperativeClose
This commit modifies the methods that transition the state of the
channel into an active closing state. With the new commitment design,
the delivery scripts are no longer pre-committed to the initial funding
messages. Instead, the scripts are sent at the instant that either side
decides to shutdown within the Shutdown message.
2017-07-30 17:51:05 -07:00
Olaoluwa Osuntokun
40e9120c04
lnwallet: populate HTLC resolutions after observing a unilateral channel close 2017-07-30 17:51:02 -07:00
Olaoluwa Osuntokun
01aa4eb2ae
lnwallet: populate HTLC resolutions within the ForceCloseSummary 2017-07-30 17:50:59 -07:00
Olaoluwa Osuntokun
83e0116d7e
lnwallet: extend the UnilateralCloseSummary to include HTLC resolutions
This commit adds a new companion struct: OutgoingHtlcResolution to the
commitment state machine. The purpose of this struct is the provide the
caller with the information necessary to sweep all outgoing HTLC’s in
the case of a broadcast up-to-date commitment transaction.

The HTLC resolutions allow a caller to sweep an outgoing HTLC into
their wallet after the absolute timeout of the HTLc has passed. This is
a two step process, with the first portion consisting of broadcasting
the HTLC timeout transaction itself, and the second portion consisting
of claiming the HTLC itself after a CSV delay.
2017-07-30 17:50:55 -07:00
Olaoluwa Osuntokun
a4c07d061e
lnwallet: the BreachRetribution struct is now aware of HTLC outputs
This commit adds awareness of active HTLC outputs to the
BreachRetribution struct. Previously, in the case of a breach, the
struct was only populated with enough information to sweep the two
commitment outputs. With this commit, the struct now has enough
information to sweep _all_ outputs within the commitment transaction.
2017-07-30 17:50:51 -07:00
Olaoluwa Osuntokun
5a78f80ffe
lnwallet: update restoreStateLogs to be aware of new commitment scheme 2017-07-30 17:50:47 -07:00
Olaoluwa Osuntokun
b9da43a2b8
lnwallet: update fetchCommitmentView due to new commitment design
This commit updates the central fetchCommitmentView method to manage
and derive the necessary easy required to create new commitments due to
the new state machine design within the specification. Each state now
requires us to derive a number of keys for each commitment state:
localDelay, remoteDelay, localKey, remoteKey, the commitment point, and
finally the revocation key itself.
2017-07-30 17:50:43 -07:00
Olaoluwa Osuntokun
3a052d2874
lnwallet: when creating new states, dispatch HTLC signing to sigPool 2017-07-30 17:50:40 -07:00
Olaoluwa Osuntokun
f569b80a80
lnwallet: when verifying new states, dispatch HTLC sig validation to sigPool 2017-07-30 17:50:37 -07:00
Olaoluwa Osuntokun
cd8672f824
lnwallet: update CreateCommitTx due to new key derivation 2017-07-30 17:50:34 -07:00
Olaoluwa Osuntokun
e314b83559
lnwallet: update HTLC script generation to account for new covenant HTLC's
This commit updates the set of functions tasked with generating HTLC’s
scripts for new commitments to now adhere to the new commitment
transaction design. With this change, the process of claiming an HTLC
now requires a second-level HTLC transaction, which solves a prior
issues due to the tight coupling of the timeout and delay clauses when
claiming an HTLC.
2017-07-30 17:50:31 -07:00
Olaoluwa Osuntokun
8b9a58cb15
lnwallet: add a sigPool instances to the state machine struct 2017-07-30 17:50:27 -07:00
Olaoluwa Osuntokun
4c9bd9dc54
lnwallet: when handling possible channel breach, only log if contract breach 2017-07-30 17:50:24 -07:00
Olaoluwa Osuntokun
e56258917e
lnwallet: introduce populateHtlcIndexes for tracking HTLC indexes in commitments
This commit adds a new method to the commitment struct:
populateHtlcIndexes.  populateHtlcIndexes modifies the set of HTLC's
locked-into the target view to have full indexing information
populated. This information is required as we need to keep track of the
indexes of each HTLC in order to properly write the current state to
disk, and also to locate the PaymentDescriptor corresponding to HTLC
outputs in the commitment transaction.

We also modify toChannelDelta to take not of these new changes, and
access the appropriate index directly.
2017-07-30 17:50:21 -07:00
Olaoluwa Osuntokun
549688d793
lnwallet: remove isDustLocal and isDustRemote, replace w/ htlcIsDust
This commit modifies the way we account for dust HTLC’s within the
commitment state machine when creating and validating new states.
Previously, an HTLC was dust if the amount of the HTLC was below the
dustLimit of the commitment chain. Now, with the HTLC covenant
transaction, the value of the HTLC also needs to cover the required fee
of the HTLC covenant transaction at the specified fee rate of the
commitment chain.

As a result, we now determine if an HTLC is dust or not, only at the
commitment site, using the new htlcIsDust function.
2017-07-30 17:50:16 -07:00
Olaoluwa Osuntokun
91165d98bb
lnwallet: eliminate the InitialRevocationWindow within the state machine
This commit modifies the current core channel state machine in order to
may a step towards BOLT-0002 and BOLT-0003 compliance. In this change,
we abandon the prior revocation window, in favor of a fixed revocation
window of size two. The revocation window will be filled at the start
of the lifetime of the channel, and never extended from there until the
channel has been fully closed.

We now maintain two variables, the current un-revoked commitment point,
and the next commitment point to use when creating a new state. The
next commitment point must initially be inserted into the channel state
with the InitNextRevocation method.

A major difference between the prior revocation key handling is that
the remote party now instead sends us the _commitment point_ in
isolation, which we then use locally (with our revocation base point)
to create the next full revocation key for _their_ commitment
transaction.
2017-07-30 17:50:13 -07:00
Olaoluwa Osuntokun
c7df82ab68
lnwallet: update the LightningChannel API usage for recent channels changes
This commit updates much of the state interaction within the
LightningChannel structure to account for the recent changes within the
chanenldb involving the OpenChannel struct, namely the introduction of
ChannelConfig and ChannelConstraints.
2017-07-30 17:50:10 -07:00
Olaoluwa Osuntokun
b5044e9217
lnwallet: introduce the sigPool struct to optimize state creation+verification
This commit introduce a new struct which acts as a companion struct to
the channel state machine struct. With the new commitment state
machine, we require a signature to be signed and validated for each
outstanding non-trimmed HTLC within the commitment state. As it’s
possible to have hundreds of HTLC’s on a given commitment transaction,
rather than perform all ECDSA operations in serial, we instead aim to
parallelize the computation with a worker pool of sign+verify workers.

The two primary methods of the sigPool are SubmitVerifyBatch and
SubmitSignBatch which allow a caller to trigger an asynchronous job
execution when creating or validating a new commitment state.
2017-07-30 17:50:06 -07:00
Olaoluwa Osuntokun
30b1cbc1fd
lnwallet: update interface-level wallet tests for new funding flow
This commit updates the prior set of interface-level wallet tests to
exercise the new funding workflow, and also to switch to utilizing two
wallet instances throughout the tests. This allows us to abandon the
dependency on the bobNode struct.
2017-07-30 17:50:03 -07:00
Olaoluwa Osuntokun
5dc0b407f3
lnwallet: update the btcwallet Signer interface due to new key derivation 2017-07-30 17:50:00 -07:00
Olaoluwa Osuntokun
c872d94606
lnwallet: convert interface-level tests to spin up two wallet instances
This commit modifies the interface-level wallet integration tests to
spin up two distinct wallet instances, wiping them after each
successful test. This change paves the way for an upcoming change which
uses two live wallet instances, rather than mocking out most of the
other interaction.
2017-07-30 17:49:57 -07:00
Olaoluwa Osuntokun
4f02a2af80
lnwallet: remove bobNode as sadly he's no longer needed
This commit removes bobNode from the wallet’s funding interaction
tests. bobNode was originally created at a very early point in lnd’s
life time before any sort of back end chain access was hooked in. At
that time the integration tests were unable to run, but bobNode allowed
us to test a portion of the funding workflow given idealized inputs.

The tests will be modified in a later commit to eschew bobNode in favor
of just having two LightningWallet instances communicate with one
another.
2017-07-30 17:49:54 -07:00
Olaoluwa Osuntokun
eca8dd1076
lnwallet: update testSignOutputUsingTweaks to exercise new key derivation 2017-07-30 17:49:50 -07:00
Olaoluwa Osuntokun
9edc335049
lnwallet: update internal funding workflow to account for BOLT-0002
This commit updates the engine that drives the reservation workflow to
utilize the new ChannelConfig and ChannelConstraint structs added in
prior commits.  With this change, the internal reservation engine has
been modified to retain the prior dual funder workflow, but also be
able to properly manage and execute the new single funder workflow
defined in BOLT-0002.
2017-07-30 17:49:47 -07:00
Olaoluwa Osuntokun
441e5b2d7c
lnwallet: add new helper function to generate both comitment pairs 2017-07-30 17:49:44 -07:00
Olaoluwa Osuntokun
62110b33e7
lnwallet: flesh out the Config struct, wallet constructor now takes 2017-07-30 17:49:41 -07:00
Olaoluwa Osuntokun
c63c7cd22f
lnwallet: update Channel[Reservation+Contribution] for new funding flow
This commit updates the channel reservation workflow in order to
properly implement the new funding workflow defined in BOLT-0002.

The workflow itself hasn’t changed significantly, but the contents of
the contributions of both sides have. The bulk of the fields within the
contribution of both sides has been boiled down into a pointer to the
ChannelConfig which houses all the data required to handle all states
of the channel, and commitment state machine.

For the two portions which are dictated by the other party, we now add
builder-like modifiers to allow specifying the constraints after the
initial portion of the workflow.
2017-07-30 17:49:37 -07:00
Olaoluwa Osuntokun
e930af4b43
lnwallet: convert interface-level tests to be run as sub-tests 2017-07-30 17:49:34 -07:00
Olaoluwa Osuntokun
9c0261206d
lnwallet: modify the Signer interface to reflect new key derivation scheme
This commit modifies the Signer interface to reflect the new key
derivation defined within BOLT-0003. This entails removing the prior
PrivateTweak field all together in favor of two new fields: SingleTweak
and DoubleTweak.

These two fields are mutually exclusive. The SingleTweak field is now
required in order to sign all regular non-delay, and also must be used
to generate signatures for the localKey, or delayKey. The DoubleTweak
field is used to generate the private key that corresponds to the
revoked revocation key.
2017-07-30 17:49:31 -07:00
Olaoluwa Osuntokun
7b002175f4
lnwallet: update state hint derivation to match BOLT-0003 2017-07-30 17:49:28 -07:00
Olaoluwa Osuntokun
ca45ae7524
lnwallet: update TestCommitmentSpendValidation due to new commitment scripts 2017-07-30 17:49:25 -07:00
Olaoluwa Osuntokun
016f56e8e3
lnwallet: update TestRevocationKeyDerivation to use new derivation 2017-07-30 17:49:22 -07:00
Olaoluwa Osuntokun
44ffcf2156
lnwallet: add new test case to exercise singly tweaked key derivation 2017-07-30 17:49:19 -07:00
Olaoluwa Osuntokun
5b6cc5e92d
lnwallet: TestHTLCSenderSpendValidation for new HTLC scripts 2017-07-30 17:49:16 -07:00
Olaoluwa Osuntokun
7c035a4729
lnwallet: update TestHTLCSenderSpendValidation for new scripts 2017-07-30 17:49:13 -07:00
Olaoluwa Osuntokun
c4866723d1
lnwallet: add redemption+spend tests for the second-level HTLC txns 2017-07-30 17:49:09 -07:00
Olaoluwa Osuntokun
1a9ada4897
lnwallet: move stateHintTests global var into TestCommitTxStateHint 2017-07-30 17:49:06 -07:00
Olaoluwa Osuntokun
4e4375513d
lnwallet: update MaxHTLCNumber to precisely match the spec 2017-07-30 17:49:03 -07:00
Olaoluwa Osuntokun
71e47283cf
lnwallet: update size.go to add the weight of the success+timeout txns
This commit updates the constants in size.go to include the weight of
the HTLC success and timeout transactions. These values are required in
order to properly compute the fee required for a particular HTLC
transaction. The fee will change depending on if the HTLC is incoming
or outgoing.
2017-07-30 17:49:00 -07:00
Olaoluwa Osuntokun
360876944e
lnwallet: add new functions to generate + claim second-level HTLC txns
This commit adds a series of new functions that can be used to generate
the second level HTLC transactions, and also to claim the output
created by the transaction after a delay. The details of the scripts
and transaction format can be found in BOLT #3.
2017-07-30 17:48:56 -07:00
Olaoluwa Osuntokun
f70697bf71
lnwallet: update all witness gen funcs for receiver's HTLC scripts 2017-07-30 17:48:53 -07:00
Olaoluwa Osuntokun
4b1a7091e6
lnwallet: update public key scripts for the HTLC receiver's outputs
This commit updates all the pkScripts used within the commitment
transaction of the party that is on the reign side of an HTLC.

The major difference in these scripts as the prior generation’s is that
the claim action is now spared into a distinct transition. This clause
is guarded by a 2-of-2 multi-sig op code, which effecting creates an
off-chain covenant forcing the party claiming the transaction to incur
a delay before the funds can be swept.
2017-07-30 17:48:50 -07:00
Olaoluwa Osuntokun
d697c6ca9a
lnwallet: update the witness generation funcs for sender's HTLC output
This commit updates all the spendHtlcSpend* functions which are used to
spend each of the possible clauses within the HTLC contract placed on
the sender’s commitment transaction.
2017-07-30 17:48:47 -07:00
Olaoluwa Osuntokun
8f5129e08f
lnwallet: update public key scripts for the HTLC sender's commitment tx 2017-07-30 17:48:44 -07:00
Olaoluwa Osuntokun
0b9c117bbd
lnwallet: update key derivation for the commitment txn scheme
This commit updates the key derivation to match the derivation required
in order to construct and validate the commitment scheme that is used
within the draft specification of the Lightning Network. The new scheme
is very similar to the prior scheme aside from the following major
differences:
  * Each key (not just the revocation key) now changes with each state.
  * A commitment point (a component of the revocation key) is used to
randomize each key, and also generate new tweaked versions of the key.
   * Base points are used along with the commitment point to generate
the keys for the commitment transaction.
   * Before the remote party would send over the fully valid revocation
key. Now the remote party sends us a commitment point, which we then
use our local revocation base point to generate their revocation key.
2017-07-30 17:48:41 -07:00
Olaoluwa Osuntokun
13404243cf
lnwallet: revocation root generation is now based on a block hash + node key
This commit modifies the prior revocation root generation to a newer
version which is intended to allow for easy recovery of revocation
state. Rather than using the node’s keys (which we can’t count on NOT
to change), we instead now use the block hash as a salt. With this,
given the block hash prior to the one that funded the channel, and the
node’s identity key, we can reconstruct our revocation state.
2017-07-30 17:48:38 -07:00
Olaoluwa Osuntokun
14832d8c09
lnwallet+lnwire: minor grammatical fixes after UpdateFee merge 2017-07-14 17:10:42 -07:00
Johan T. Halseth
ebe05f6568 lnwallet: add update_fee message support.
This commit adds the possibility for the initiator of a
channel to send the update_fee message, as specified
in BOLT#2. After the message is sent and both parties
have committed to the updated fee, all new commitment
messages in the channel will use the specified fee.
2017-07-14 16:39:15 -07:00
bryanvu
7fbb1e5a37 lnwallet: refactor dust limit tests
Split up and simplified dust limit tests.
2017-07-06 18:05:43 -07:00
bryanvu
73767f8c78 lnwallet: factor createHTLC out of unit tests 2017-07-06 18:05:43 -07:00
bryanvu
5068541044 lnwallet: apply dust HTLC amounts to fees
If an HTLC’s value is below a node’s dust limit, the amount for that
HTLC should be applied to to the fee used for the channel’s commitment
transaction.
2017-07-06 18:05:43 -07:00
Andrey Samokhvalov
8fa2b95c12 lnd: remove seelog logger
The btclog package has been changed to defining its own logging
interface (rather than seelog's) and provides a default implementation
for callers to use.

There are two primary advantages to the new logger implementation.

First, all log messages are created before the call returns.  Compared
to seelog, this prevents data races when mutable variables are logged.

Second, the new logger does not implement any kind of artifical rate
limiting (what seelog refers to as "adaptive logging").  Log messages
are outputted as soon as possible and the application will appear to
perform much better when watching standard output.

Because log rotation is not a feature of the btclog logging
implementation, it is handled by the main package by importing a file
rotation package that provides an io.Reader interface for creating
output to a rotating file output.  The rotator has been configured
with the same defaults that btcd previously used in the seelog config
(10MB file limits with maximum of 3 rolls) but now compresses newly
created roll files.  Due to the high compressibility of log text, the
compressed files typically reduce to around 15-30% of the original
10MB file.
2017-06-25 14:19:56 +01:00
Olaoluwa Osuntokun
5dd7b157a0
lnwallet: fix race condition when forwarding by creating new pubkey
This commit fixes a race condition that would at times occur in the
htlcswitch.TestChannelLinkBidirectionalOneHopPayments test case. A race
condition would occur in the goroutine running ReceiveNewCommitment
compared with the grouting that would obtain the snapshot in order to
make a forwarding decision.

We fix this by creating a new public key for each new commitment
transaction such that we complete avoid the read/write race condition.
2017-06-19 17:43:57 +02:00