Commit Graph

65 Commits

Author SHA1 Message Date
Olaoluwa Osuntokun
affd7793b3
lnwallet: add htlc persistence, state log restoration to channel state machine
This commit adds full persistence logic of the current lowest
un-revoked height within each commitment chain. The newly added
channeldb methods for record state transitions within both commitment
chains are now utilized. This un-settled HTLC state is now read upon
initialization, with the proper log entries inserted into the state
update log which reflect the garbage collected log right before the
restart.

A new set of tests have been added to exercise a few edge cases around
HTLC persistence to ensure the in-memory log is properly restored based
on the on-disk snapshot.
2016-09-12 19:07:04 -07:00
Olaoluwa Osuntokun
671098325d
lnwallet: refactor all wallet/channel interaction to use the WalletController.
This commit performs a major refactor of the current wallet,
reservation, and channel code in order to call into a WalletController
implementation rather than directly into btcwallet.

The current set of wallets tests have been modified in order to test
against *all* registered WalletController implementations rather than
only btcwallet. As a result, all future WalletControllers primary need
to ensure that their implementation passes the current set of tests
(which will be expanded into the future), providing an easy path of
integration assurance.

Rather than directly holding the private keys throughout funding and
channel creation, the burden of securing keys has been shifted to the
specified WalletController and Signer interfaces. All signing is done
via the Signer interface rather than directly, increasing flexibility
dramatically.

During channel funding, rather than creating a txscript.Engine to
verify commitment signatures, regular ECDSA sig verification is now
used instead. This is faster and more efficient.

Finally certain fields/methods within ChannelReservation and
LightningChannel have been exposed publicly in order to restrict the
amount of modifications the prior tests needed to undergo in order to
support testing directly agains the WalletController interface.
2016-09-08 12:25:54 -07:00
Olaoluwa Osuntokun
8bbd010f74
lnwallet: use the ChainNotifier interface throughout instead of BtcdNotifier
This commit refactors the code within lnwallet interacting with the
ChainNotifier to accept, and call against the implementation rather
than a single concrete implementation.

LightningWallet no longer creates it’s own BtcdNotifier implementation
doing construction, now instead accepting a pre-started `ChainNotifier`
interface.  All imports have been updated to reflect the new naming
scheme.
2016-09-01 19:13:19 -07:00
Olaoluwa Osuntokun
4104a9bcde
lnwallet: never create zero-value outputs within the commitment transaction 2016-07-27 12:15:50 -07:00
Olaoluwa Osuntokun
c0614c0478
lnwallet: add basic tests for cooperative channel closure
This commit adds a basic test for cooperative channel closure. The
current test ensures correctness of the cooperative closure procedure
initiated by either the channel initiator, or the channel responder.
2016-07-27 11:32:35 -07:00
Olaoluwa Osuntokun
e01dd7f18d
lnwallet: properly make channelState an enum by using iota 2016-07-27 11:29:49 -07:00
Olaoluwa Osuntokun
8a56fbf196
lnwallet: switch name of package logger to avoid module collision 2016-07-27 11:29:07 -07:00
Olaoluwa Osuntokun
2bb65a3fb8
lnwallet: add PendingUpdates method to channel state machine
This commit adds a new method, “PendingUpdates” to the channel state
machine which is intended to be a source to give callers a hint as to
when an additional commitment signature should be sent independent of
any request/response book keeping.
2016-07-21 16:52:18 -07:00
Olaoluwa Osuntokun
4063171918
lnwallet: partition state update logs within channel state machine
This commit patrons the state update logs properly within the channel
state machine. This change fixes a number of bugs caused by treating a
central log as two logically distinct logs. Rather than having a bit
indicating if the entry is incoming/outgoing, an entry is added to a
remote or local log depending on which modification method is used.

As a result the code is much easier to follow due to separation of
concerts.

Finally, when attempting to sign a new update with an exhausted
renovation window a distinct error is returned in order to allow higher
level callers to properly back-off and handle the protocol event.
2016-07-21 16:50:38 -07:00
Olaoluwa Osuntokun
b60270f3f7
lnwallet: include htlcs to settle in returned set of htlc's to forward
This commit fixes a slight bug in the channel state machine’s code
executed when processing a revocation messages. With this commit after
processing a revocation, log entries which we should forward to the
downstream or upstream peer for settling/adding HTLC’s are now properly
returned.

The testa have also been updated to ensure to correct htlc’s are
returned “for forwarding”.
2016-07-16 18:12:49 -07:00
Olaoluwa Osuntokun
2c303a1879
lnwallet: AddHTLC now returns log index of created log entry 2016-07-16 18:02:09 -07:00
Olaoluwa Osuntokun
7dea354711
lnwallet: properly handle HTLC settles in channel state-machine
We no longer track HTLC’s by their r-hash within the log into the
index, as we may have multiple HTLC’s that can be redeemed by the same
pre-image. Instead we now use a separate index which is keyed by a
log-index.

Additionally, the SettleHTLC method now also returns the index of the
HTLC being settled which allows the remote party to quickly locate the
HTLC within their log.

This commit also introduces a few trace/debug log messages which will
likely be pruned in the near future
2016-07-12 17:35:57 -07:00
Olaoluwa Osuntokun
06f062e678
lnwallet: sync revocation state after receiving a revocation 2016-07-12 17:32:42 -07:00
Olaoluwa Osuntokun
14d669dfd1
lnwallet: decrease initial revocation window to 4 2016-07-12 17:31:01 -07:00
Olaoluwa Osuntokun
94c242073a
lnwallet: finish initial draft of LightningChannel state machine
This commit finishes the initial draft of the commitment state machine.
A full re-write of the prior protocol which combines aspects of the
former ‘lnstate’ package has replaced the prior un-finished
stop-and-wait protocol.

This new protocol is designed to operate in an asynchronous environment
and to facilitate non-blocking batched and pipelined updates to the
committed channel states. The protocol is also de-synchronized meaning
that either side can propose new commitment states independent of the
actions of the other party.

The state machine implemented is very similar to that of c-lightning,
however we allow multiple unrevoked commentates in order to minimize
blocking, and also to reduce latency across several hops in a
bi-directional setting.

The current implementation consists of 3 main data structures: a
commitment chain which consist of unrevoked commitment transactions
(one for each side), and a (mostly) append-only log of HTLC updates
shared between both sides. New commitments proposed index into the log
denoting which updates they include, this allows both parties to
progress chains independent of one another. Revoked commitments, reduce
the length of the chain by one, and free up space within the revocation
window.

At this point only basic tests are in place for the state machine,
however more extensive testing infrastructure along with formal proofs
using PlusCall are planned.
2016-07-05 17:02:03 -07:00
Olaoluwa Osuntokun
1b490c52ed
lnwallet: createCommitTx now a revocation key 2016-06-30 12:12:19 -07:00
Olaoluwa Osuntokun
e22734f9cf
lnwallet: update HTLC+commitment scripts
This commit updates the previous HTLC and commitment scripts to their
current latest evolution.

The HTLC scripts have been optimized for space savings, the
functionality itself has remained relatively unchanged. A trade off was
made to add additional bytes into the sigScript in order to avoid
extraneous CHECKSIG’s. The rationale is that an extra 1-2 bytes in the
sigScript to guide execution, are worthwhile since they’re in the
witness, and witness data may be pruned in the near future.

The primary change is within the commitment transaction itself. Instead
of using revocation hashes, we now use signature based revocation. This
saves space in the Script, and optimizes away an extra hashing
operation. Elkrem/shachain is still used but, we now use the pre-images
to homomorphically derive a public key which the other party will be
able to sign with, once we disclose the pre-image itself.

Finally, we have switched to using SHA-256 everywhere uniformly for
both revocation hashes, and payment hashes. The rationale is that the
output of ripemd160 is too small for modern security margins, and that
other coins/chains are more likely to have SHA-256 implemented, than
ripemd160.

A set of tests has also been included which contain (mostly) exhaustive
tests of all possible redemption paths for both commitment and HTLC.
2016-06-27 11:35:32 -07:00
Olaoluwa Osuntokun
ac8736ff99
lnwallet: convert commitment no-delay output to p2wsh 2016-06-27 11:21:13 -07:00
Olaoluwa Osuntokun
05fb9b5a6d
lnwallet: payment and revocation hashes are now 32 bytes
This unifies some inconstancies across the code-base with hashes being
32 vs 20 bytes. All hashes, whether payment or revocation are now
uniformly 32 bytes everywhere. As a result, only OP_SHA256 will be used
within commitment and HTLC scripts. The rationale for using sha256
instead of hash160 for the HTLC payment pre-image is that alternative
chains are more likely to have sha256 implemented, rather than
ripemd160.

A forthcoming commit will update the current commitment, and HTLC
scripts.
2016-06-26 23:04:14 -07:00
Olaoluwa Osuntokun
77a006f03b
lnwallet: ChannelPoint() now returns a pointer to outpoint 2016-06-22 22:15:13 -07:00
Olaoluwa Osuntokun
e17bdf08ea
lnwallet: expose db channel deletion+snapshotting 2016-06-22 22:12:37 -07:00
Olaoluwa Osuntokun
45236fa092
lnwallet: implement cooperative closure for LightningChannel
A cooperative closure of a LightningChannel proceeds in two steps.
First, the party who wishes to close the channel sends a signature for
the closing transaction. Next, the responder reconstructs the closing
transaction identically as the initiator did using a canonical
input/output ordering, and the currently settled balance within the
channel. At this point, the responder then broadcasts the closure
transaction. It is the responsibility of the initiator to watch for
this transaction broadcast within the network to clean up any resources
they committed to the active channel.
2016-06-21 13:13:22 -07:00
Olaoluwa Osuntokun
27e6839060
lnwallet: publicly export constructor for LightningChannel 2016-06-21 13:13:18 -07:00
Olaoluwa Osuntokun
507520cda9
lnwallet: move channelState from channeldb to channel.go 2016-06-21 13:13:14 -07:00
Olaoluwa Osuntokun
fcff17c336
multi: change all imports to roasbeef's forks
This commit will allow the general public to build lnd without jumping
through hoops setting up their local git branches nicely with all of
our forks.
2016-05-15 17:22:37 +03:00
Olaoluwa Osuntokun
eeb2887fe9
lnwallet: segwit-ify all scripts and signing utils 2016-05-03 20:06:39 -07:00
Olaoluwa Osuntokun
1e35018e89 lnwallet: fix compile errors against btcd's segwit branch 2016-04-12 21:37:08 -07:00
Olaoluwa Osuntokun
4b16763f4d lnwallet: update channel.go to new OpenChannel fields
Simply to compile, channel.go will be replaced with lnstate soon
enough.
2016-03-24 00:00:59 -07:00
Tadge Dryja
0d3639435f can sync with segnet in hard mode
lots of changes but they seem to work
2016-02-15 22:13:17 -08:00
Olaoluwa Osuntokun
ab16933575 lnwallet: commit tx should have final sequence num 2016-02-05 20:24:32 -08:00
Olaoluwa Osuntokun
81a4887d11 lnwallet: move lockTimeToSequence to script_utils.go 2016-02-05 20:24:17 -08:00
Olaoluwa Osuntokun
fa05ee9a22 lnwallet: move pubKey comparison for funding output spend into spendMultiSig func
* This change makes the spendMultiSig function testable independent of
the reservation workflow.
2016-02-05 12:33:22 -08:00
Olaoluwa Osuntokun
0ce4fb1294 lnwallet: properly set transaction version on commit txs 2016-02-03 12:01:44 -08:00
Tadge Dryja
2815afebb7 update imports to github 2016-01-16 10:45:54 -08:00
Olaoluwa Osuntokun
07b0d5ca3e lnwallet: once the reservation workflow is complete, wait then open the payment channel
* Hooks into the ChainNotifier infrastructure to receive a notification
once the funding transaction gets enough notifications.
* Still need to set up the notification grouting within a
LightningChannel to watch for uncooperative closures, and broadcasts
and revoked channel states.
2016-01-14 23:58:04 -08:00
Olaoluwa Osuntokun
f52b8370bc lnwallet: populate the update totem during channel initialization 2016-01-14 23:56:11 -08:00
Olaoluwa Osuntokun
27744a3582 lnwallet: add logic for adding/removing HTLCs
* Updates to the channel are made atomic, and consistent via a proxy
object: “ChannelUpdate” which encapsulates an update transaction. Only
one update transaction may be outstanding at any time.
* Update transactions are initiated via AddHTLC or SettleHTLC.
* Once a transaction has been begun, in order to complete the update
the transaction must first be presented with a signature from the
counter-party for our new version of the commitment tx
(VerifyNewCommitmentSigs), and finally to atomically commit the
transaction, the counterparty’s pre-image to their previous revocation
hash must be validate (Commit).
2016-01-14 23:56:10 -08:00
Olaoluwa Osuntokun
895d70e79f lnwallet: add funding tx p2sh output to lnchannel
* Allows for quick access to the pkScript when signing new commitment
transactions.
2016-01-14 23:56:10 -08:00
Olaoluwa Osuntokun
3fe82f475f lnwallet: correctly adjust cleared funds during AddHTLC
* When adding an HTLC, the update to settled balances are to the side
sending the HTLC.
2016-01-14 23:56:10 -08:00
Olaoluwa Osuntokun
1772108544 lnwallet: start of HTLC update logic
* moved sorting of transaction outside of createCommitTx also us to add
HTLC’s before sorting
* On the fence about the proxy object design, will re-visit once we
start to implement the p2p code.
2016-01-14 23:56:09 -08:00
Olaoluwa Osuntokun
5a7b98a9e4 lnwallet: remove CLTV funding tx gen 2016-01-14 23:56:09 -08:00
Olaoluwa Osuntokun
41f4992218 lnwallet: add utils to generate HTLC scripts, split up commit output scripts 2016-01-14 23:56:08 -08:00
Olaoluwa Osuntokun
7648fee903 lnwallet: note to future roasbeef 2015-12-29 00:00:29 -06:00
Olaoluwa Osuntokun
ef7a2aa553 lnwallet: properly set sequence number in tx for CSV usage 2015-12-29 00:00:29 -06:00
Olaoluwa Osuntokun
4fdb2763e6 channeldb: create new channeldb package, update lnwallet to use new API
* Initial draft of brain dump of chandler. Nothing yet set in stone.
* Will most likely move the storage of all structs to a more “column”
oriented approach. Such that, small updates like incrementing the total
satoshi sent don’t result in the entire struct being serialized and
written.
* Some skeleton structs for other possible data we might want to store
are also included.
* Seem valuable to record as much data as possible for record keeping,
visualization, debugging, etc. Will need to set up a time+space+dirty
cache to ensure performance isn’t impacted too much.
2015-12-26 12:35:15 -06:00
Olaoluwa Osuntokun
0307f321ad lnwallet: switch LightningChannel struct to use channeldb instead of raw namespace 2015-12-26 00:08:04 -06:00
Olaoluwa Osuntokun
890ceecc1a lnwallet: csv delay is a uint32, fill forgotten fields in channel state 2015-12-26 00:07:30 -06:00
Olaoluwa Osuntokun
07646d05db move channel state struct to channeldb.go
* All fields are now publicly exported
2015-12-24 12:41:15 -06:00
Olaoluwa Osuntokun
a4762b818f Merge branch 'master' of li.lan:labs/plasma 2015-12-22 22:39:28 -06:00
Olaoluwa Osuntokun
b606804934 lnwallet: add comments to fields of OpenChannelState 2015-12-22 22:27:33 -06:00