Commit Graph

208 Commits

Author SHA1 Message Date
Andrey Samokhvalov
da3028e10c lnwallet: add HTLC count validation 2016-11-23 20:02:29 -06:00
Andrey Samokhvalov
391d5cd401 lnwallet: add commitment transaction estimation 2016-11-23 20:02:29 -06:00
Andrey Samokhvalov
5b9e4ae61e general: fix typos, rename variables, add comments 2016-11-23 20:02:29 -06:00
bryanvu
faf9daddf6 lnwallet: calculate channel's total satoshis sent and received
When HTLCs are settled, the channel’s TotalSatoshisSent and
TotalSatoshisReceived fields are updated.
2016-11-22 14:57:03 -06:00
Olaoluwa Osuntokun
cc63db0aee
lnwallet: add detection+handling of contract breaches in channel
This commit adds detection of contract breaches within the commitment
state-machine for a channel. A contract breach is defined as the event
wherein a channel counter-party broadcasts a previously revoked
commitment transaction. Such an event immediately closes a channel as
the funds are now in a state of dispute.

Once a breach is detected, a snapshot of the breached state is
retrieved from the database’s revocation log. This snapshot is then
used to generate the revocation leaf used within this particular state
along with all the other information ncessary to sweep ALL active funds
within the channel. This information is encapsulated within the
BreachRetribution struct which is sent over a new channel dedicated to
sending/receiving BreachRetributions.
2016-11-21 00:54:45 -06:00
Olaoluwa Osuntokun
188811cf05
lnwallet: populate the output index of an HTLC in ChannelDeltas
This commit modifies the logic within the state machine to properly
populate the new field of `OutputIndex` which the HTLC stored within a
channel delta.

With this change, in the future we’ll be able to quickly locate a
particular HTLC output in the scenario that the commitment transaction
has been broadcast on-chain and we need to sweep it. Allocating a few
extra bytes on-disk saves us from the guess-and-check logic+code
required otherwise.
2016-11-20 23:54:24 -06:00
Olaoluwa Osuntokun
2d884618aa
lnwallet: ensure all channel state machine methods are thread-safe
This commit adds some necessary locking to ensure that all updates to
the internal state of the commitment state machine are fully serial and
thread-safe. This change is required to ensure future actions w.r.t
taking action once a revoked commitment transaction has been broadcast
are not carried out while the channel’s state is being updated.
2016-11-20 23:33:27 -06:00
Olaoluwa Osuntokun
e942e70651
lnwallet: extend the SignDescriptor to include a PrivateTweak
This commit extends the SignDescriptor with a single attribute, the
‘PrivateTweak’. The duties of the Signer interface have also been
augmented to properly derive a private key using the specified tweak,
iff it’s non-nil.

As currently defined in order to generate the proper private key based
off of a PrivateTweak, the signer is to add the tweak value to the
private key for the specified public key. This generated value is to be
used for signing within the specified context.

This change paves the way for automatic revoked output sweeping with
signatures generated directly by the Signer interface, maintaining the
structure of the abstraction.

A test has been added at the interface level in order to excerise each
WalletController’s implementation of the key derivation as currently
defined.
2016-11-18 17:12:58 -08:00
Olaoluwa Osuntokun
c81e0a3ebb
lnwallet: convert CommitSpendRevoke+CommitSpendNoDelay to use Signer
This commit converts the rearming two commitment spend functions to use
the lnwallet.Signer interface directly rather than manually manage
private keys during the signing process. This commit is in preparation
for implementation of fully automated revoked uncooperative closure
detection and retribution.
2016-11-18 15:23:50 -08:00
Olaoluwa Osuntokun
22074eb737
lnwallet: integrate obfuscated state hints into funding workflow
This commit finalizes the implementation of #58 by integrating passing
around the obfuscate state hints into the funding workflow of the
wallet, and also the daemon’s funding manager.

In order to amend the tests, the functions to set and receive the state
hints are now publicly exported.
2016-11-16 12:54:39 -08:00
Olaoluwa Osuntokun
3010412bbc
lnwallet: set initiator and channel type during reservation creation 2016-11-16 12:51:27 -08:00
Olaoluwa Osuntokun
74122a0901
lnwallet: encode obfuscated state hints in the commitment tx
This commit modifies the channel state machine slightly to encode the
current state number using 30-bits of the sequence number within the
commitment transaction’s only input.

Such a modification reduces up the processing time required to punish
the counter party for breaching the contract established within the
channel by broadcasting an older revoked state.

This fixes #58 with a minor modification to what was originally
suggested.
2016-11-16 12:49:44 -08:00
Olaoluwa Osuntokun
fef927e276
lnwallet: correct good comments on state hint helper funcs 2016-11-14 18:37:20 -08:00
Olaoluwa Osuntokun
d43ef24ed3
lnwallet: add utility functions for obfuscated commitment state hints
This commit adds two utility functions along with corresponding tests
for adding obfuscated state number hints to each commitment
transaction.

Such a feature reduces the search time to recover the necessary
material to punish a counterpaty for broadcasting an invalid state from
O(N), to O(1), where N is the number of states in the channel’s
transcript. By encoding the obsfucated state number, either side is
able to quickly obtain the ncessary state to excerise “justice”.
2016-11-14 18:35:10 -08:00
Olaoluwa Osuntokun
6e01bb72b0
lnwallet+peer: minor typo fixes 2016-11-14 15:06:17 -08:00
Olaoluwa Osuntokun
4113260f9f
lnwallet: minor comment correction 2016-11-10 17:38:59 -08:00
Olaoluwa Osuntokun
81f7efe1e0
lnwallet+funding: include node net address in reservation workflow
This commit modifies the existing channel reservation workflow slightly
to thread through the IP address that we were able to reach the node
at, or the one which the node reached us via. Additionally, rather than
using OpenChannel.FullSync() at the end of the reservation workflow, we
now use OpenChannel.FullSyncWithAddr() in order to create the
relationship in the database between the channel, and the p2p node we
created the channel with.

All tests, as well as a portion of the fundingManager have been updated
accordingly,
2016-10-26 14:56:58 -07:00
Olaoluwa Osuntokun
e1d9d9c8d2
lnwallet: update to adhere to new channeldb API change
This commit modifies the lnwallet code and related tests in order to
adhere to the recent field-name change to channeldb.OpenChannel.
Instead of having the field ‘TheirLNID’ which is the sha256 of the
node’s public key, we now instead use the public key directly in all
contexts.
2016-10-25 16:41:22 -07:00
Olaoluwa Osuntokun
5d6b8e49a3
lnwallet: disallow creating a funding reservation with zero total satoshis 2016-10-23 19:27:20 -07:00
Andrey Samokhvalov
19d8abade8 config: add --pendingchannel lnd flag 2016-10-22 02:11:18 +03:00
Andrey Samokhvalov
14c6770b76 general: fix typos 2016-10-22 01:48:05 +03:00
andrew.shvv
e515710a7d multi: use witnessScript everywhere instead of redeemScript
This commit consists of a mass variable renaming to call the pkScript being executed for segwit outputs the `witnessScript` instead of `redeemScript`. The latter naming convention is generally considered to be reserved for the context of BIP 16 execution. With segwit to be deployed soon, we should be using the correct terminology uniformly through the codebase. 

In addition some minor typos throughout the codebase has been fixed.
2016-10-15 16:02:09 -07:00
Olaoluwa Osuntokun
bea555e61d
lnwallet: add tests for SubscribeTransactions and ListTransactionDetails 2016-10-15 14:12:23 -07:00
Olaoluwa Osuntokun
628224a5ee
lnwallet/btcwallet: concretely implement the new TransactionSubscription interface
This commit returns the BtcWallet wrapper struct back to conformance to
the WalletController interface by adding support for
SubscribeTransactions.

The implementation of the lnwallet.TransactionSubscription consists
simply of a proxy goroutine which forwards notifications from the
wallet’s internal NotificaitonServer.
2016-10-15 14:11:25 -07:00
Olaoluwa Osuntokun
2eb1936cce
lnwallet: extend the WalletController with a new txn pub/sub client
This commit adds a new simple interface related to the WalletController
which allows for subscribing to new notifications as transactions
relevant to the wallet are seen on at the network and/or mined. The
TransactionSubscription interface will prove useful for building higher
level UI’s on-top of the daemon which update the presentation layer in
response to received notifications.
2016-10-15 14:07:24 -07:00
Olaoluwa Osuntokun
617716ce9a
lnwallet/btcwallet: implement ListTransactionDetails
This commit implements the new ListTransactionDetails method within
btcwallet’s concrete implementation of the WalletController interface.
Both mined and unmined transactions are currently returned via the same
method. Unmined transactions are indicated by their lack of an
inclusion block hash and lack of confirmations.
2016-10-14 20:16:00 -07:00
Olaoluwa Osuntokun
78cfcc28b7
lnwallet: add method to list relevant txns to WalletController interface 2016-10-14 20:14:15 -07:00
Olaoluwa Osuntokun
85b2b52a5f
lnwallet: add concrete type for coin selection fail during funding workflow 2016-09-26 12:18:03 -07:00
Olaoluwa Osuntokun
0013b804c0
lnwallet: display target+current amount when coin selection fails for funding txns 2016-09-26 11:52:56 -07:00
Olaoluwa Osuntokun
9d5b0885d4
lnwallet/channel: add r-preimages to the PaymentDescriptor for settles 2016-09-21 19:49:07 -07:00
Olaoluwa Osuntokun
7310d0a0f7
lnwallet: remove extraneous logging messages 2016-09-21 19:48:46 -07:00
Olaoluwa Osuntokun
49ce1040d4
lnwallet: adopt simple fee structure for commitment transactions
This commit modifies the prior funding workflow to account for fees
when creating the funding output. As a stop gap, the current fee for
the commitment transaction is now hard-coded at 5k satoshis. Once the
fee models are in place this should instead be some high multiple of
the current “average” fee rate within the network, continuing, the
proper fee should be adjusted from the commitment transaction has
outputs are added/removed.
2016-09-12 19:08:01 -07:00
Olaoluwa Osuntokun
d0353b2864
lnwallet: add ability to trigger a force closure within channel state machine
This commit introduces the concept of a manually initiated “force”
closer within the channel state machine. A force closure is a closure
initiated by a  local subsystem which broadcasts the current commitment
state directly on-chain rather than attempting to cooperatively
negotiate a closure with the remote party.

A force closure returns a ForceCloseSummary which includes all the
details required for claiming all rightfully owned outputs within the
broadcast commitment transaction.

Additionally two new publicly exported channels are introduced, one
which is closed due a locally initiated force closure, and the other
which is closed once we detect that the remote party has executed a
unilateral closure by broadcasting their version of the commitment
transaction.
2016-09-12 19:07:35 -07:00
Olaoluwa Osuntokun
024deb93f3
lnwallet: modify btcwallet's SignOutputRaw method to get proper pubkey
This commit slightly modifies btcwallet’s SignOutputRaw method to work
properly in the case that the pkScript of the output being spent isn’t
one of the template pkScripts (p2pkh, multi-sig, etc). Rather than
examining the address, we now attempt to find the private key which
matches the public key passed within the sign descriptor.
2016-09-12 19:07:24 -07:00
Olaoluwa Osuntokun
f972378140
lnwallet: modify CommitSpendTimeout to expect proper input sequence num and tx version 2016-09-12 19:07:20 -07:00
Olaoluwa Osuntokun
e858bb5ca2
lnwallet: convert CommitSpendTimeout to user the Signer interface 2016-09-12 19:07:12 -07:00
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
6684f6aedf
channeldb: implement commitment state update log
This commit implements a state update log which is intended the record
the relevant information for each state transition on disk. For each
state transition a delta should be written recording the new state. A
new method is also provided which is able to retrieve a previous
channel state based on a state update #.

At the moment no measures has been taken to optimize the space
utilization of each update on disk. There are several low-hanging
fruits which can be addressed at a later point. Ultimately the update
log itself should be implemented with an append-only flat file at the
storage level. In any case, the high level abstraction should be able
to maintained independent of differences in the on-disk format itself.
2016-09-12 19:06:44 -07:00
Olaoluwa Osuntokun
e536e1afb1
lnwallet: eliminate flaky assert within integration tests
This commit removes a flaky assertion within the interaction tests. Due
to differences in final coin selection across tests due to the
pseudo-random nature of map iterations, a single output might be
selected rather than two as we previously expected.

Additionally a duplicate test has been removed, and the locked output tests
simplified a bit.
2016-09-08 13:10:51 -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
99fdb3a3a9
lnwallet: modify elkrem root derivation, derive from root HD seed
This commit modifies the elkrem root derivation for each newly created
channel. First a master elkrem root is derived from the rood HD seed
generated from private wallet data. Next, a HKDF is used with the
secret being the master elkrem root.
2016-09-08 12:25:49 -07:00
Olaoluwa Osuntokun
564316a846
lnwallet: publicly export several functions within script_utils.go 2016-09-08 12:25:44 -07:00
Olaoluwa Osuntokun
044b65317a
lnwallet: remove setup.go
This file is no longer needed as each implementation of the
WalletController is expected to handle its own set up via an instance
of the WalletDriver factory struct.
2016-09-08 12:25:40 -07:00
Olaoluwa Osuntokun
773c831561
lnwallet: replace naive coin selection a size+fee aware version 2016-09-08 12:25:36 -07:00
Olaoluwa Osuntokun
6a1d8d0682
lnwallet: add a BtcWallet implementation of WalletController
This commit adds the first concrete implementation of the
WalletController interface: BtcWallet. This implementation is simply a
series of wrapper functions are the base btcwallet struct.

Additionally, for ease of use both the BlockChain IO and Signer
interface are also implemented by BtcWallet. Finally a new WalletDriver
implementation has been implemented, and will be register by the init()
method within this new package.
2016-09-08 12:25:32 -07:00
Olaoluwa Osuntokun
5449ba2b34
lnwallet: revamp interfaces, add BlockChainIO and Singer
This commit revamps the previous WalletController interface, edging it
closer to a more complete version.

Additionally, this commit also introduces two new interfaces:
BlockchainIO, and Singer along with a new factor driver struct, the
WalletDriver.

This BlockChainIO abstracts read-only access to the blockchain, while
the Singer interface abstracts the signing of inputs from the base
wallet paving the way to hardware wallets, air-gapped signing, etc.

Finally, in order to provide an easy method for selecting a particular
concrete implementation of a WalletController interface, the concept of
registering “WalletDriver”s has been introduced. A wallet driver is
essentially the encapsulation of a factory function capable of create a
new instance of a Wallet Controller.
2016-09-08 12:25:28 -07:00
Olaoluwa Osuntokun
a87ddeabdc
lnwallet: remove interaction with the btcutil.coinset package
This commit removes the wrapper functions used to rely on the coinset
package for coin selection. In a future commit the prior behavior will
be replaced by a custom coin selection implementation which estimates
the size of the funding transaction.
2016-09-08 12:25:24 -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
04e7a88a83
lnwallet: fix bug in lockTimeToSequence function
This commit fixes a bug in the lockTimeToSequence function when mapping
a block-based relative lock time to the proper sequence number.

Applying the mask isn’t necessary since the values are expected to be
blow 65K blocks.
2016-08-25 16:25:32 -07:00
Olaoluwa Osuntokun
4104a9bcde
lnwallet: never create zero-value outputs within the commitment transaction 2016-07-27 12:15:50 -07:00