Commit Graph

206 Commits

Author SHA1 Message Date
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
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
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
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
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
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
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
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
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
43b736225b
multi: add new method to generate fresh node announcments 2017-08-04 18:32:33 -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
747e0f57d1
multi: fix linter errors 2017-07-30 18:22:45 -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
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
14832d8c09
lnwallet+lnwire: minor grammatical fixes after UpdateFee merge 2017-07-14 17:10:42 -07:00