Commit Graph

399 Commits

Author SHA1 Message Date
ZmnSCPxj 993d5dc75a wallet: Implement wallet_invoice_nextpaid.
wallet_invoice_nextpaid is used to iterate over paid invoices,
and will be used to fix waitanyinvoice command.
2018-01-01 23:13:31 +00:00
ZmnSCPxj 3dd50d6219 wallet: Add pay_index column to database for paid invoice ordering. 2018-01-01 23:13:31 +00:00
Rusty Russell 887e9dcc44 travis: reenable check-source (without BOLT text).
We've been slipping, so fix up minor issues too so it compiles.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-21 14:33:27 +01:00
Rusty Russell 0237e0b28c bitcoin: create new wrapper type bitcoin_txid, log backward endianness.
It's just a sha256_double, but importantly when we convert it to a
string (in type_to_string, which is used in logging) we use
bitcoin_txid_to_hex() so it's reversed as people expect.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-21 11:05:38 +00:00
Rusty Russell 553ebc936b json: add json_add_txid.
I prefer the typesafety of specific functions, rather than having the
caller know that txids are traditionally reversed in bitcoin.

And we already have a bitcoin_txid_to_hex() function for this.

Closes: #411
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-21 11:05:38 +00:00
Andrew Chow f792e236eb Encode reversed hex for txid in listfunds 2017-12-21 11:05:38 +00:00
William Casarin bcd49b063b fixup! wallet: insert address into peers table 2017-12-21 09:56:20 +00:00
William Casarin d89eb32c85 wallet: insert address into peers table
It looks like we were missing the address on insert into the peers table. This
will insert the address formatted by fmt_wireaddr. This happens to include the
ip and port.

This is fine, since parse_wireaddr has been updated to parse ports in ip address
strings.

Signed-off-by: William Casarin <jb55@jb55.com>
2017-12-21 09:56:20 +00:00
William Casarin 7ecccd50b9 wireaddr: add ip[:port] parsing
* Add port parsing support to parse_wireaddr. This is in preparation for storing
addresses in the peers table. This also makes parse_wireaddr a proper inverse of
fmt_wireaddr.

* Move parse_wireaddr to common/wireaddr.c this seems like a better place for
it. I bring along parse_ip_port with it for convenience. This also fixes some
issues with the upcoming ip/port parsing tests.

Signed-off-by: William Casarin <jb55@jb55.com>
2017-12-21 09:56:20 +00:00
Rusty Russell 3543530172 build_utxos: fix weight calculation, and make more accurate.
Accuracy improvements:

1. We assumed the output was a p2wpkh, but it can be user-supplied now.
2. We assumed we always had change; remove this for wallet_select_all.

Calculation out-by-one fixes:

1. We need to add 1 byte (4 sipa) for the input count.
2. We need to add 1 byte (4 sipa) for the output count.
3. We need to add 1 byte (4 sipa) for the output script length for each output.
4. We need to add 1 byte (4 sipa) for the input script length for each input.
5. We need to add 1 byte (4 sipa) for the PUSH optcode for each P2SH input.

The results are now a slight overestimate (due to guessing 73 bytes
for signature, whereas they're 71 or 72 in practice).

Fixes: #458
Reported-by: Jonas Nick @jonasnick
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-20 12:56:15 +01:00
Rusty Russell bbac67f108 withdraw_tx: don't create empty output if no change.
We were using changekey as the flag to produce change, not changesat,
but the caller was using changesat as the flag.

Also, don't allocate changekey at all if we don't need it; this means
valgrind will complain if we use it at all, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-20 12:56:15 +01:00
Rusty Russell 41a5d19198 wallet: don't leak channel struct when peer freed.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-20 12:43:10 +01:00
Christian Decker ee25547576 Re-enable builds with DEVELOPER=0
Two changes:
 - Fixed the function signature of noleak_ to match in both
   configurations
 - Added memleak.o to linker for tests

Generating the stubs for the unit tests doesn't really work since the
stubs are checked in an differ between the two configurations, so
adding memleak to the linker fixes that, by not requiring stubs to be
generated in the first place.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-12-20 12:43:10 +01:00
Rusty Russell ed8baf5f88 funding_spent: fix leaks.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-20 12:43:10 +01:00
Rusty Russell 4957921e02 log: mark ltmp as notleak.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-20 12:43:10 +01:00
Rusty Russell c956d9f5eb lightningd: tal memleak detection, dev-memleak command.
This is a primitive mark-and-sweep-style garbage detector.  The core is
in common/ for later use by subdaemons, but for now it's just lightningd.
We initialize it before most other allocations.

We walk the tal tree to get all the pointers, then search the `ld`
object for those pointers, recursing down.  Some specific helpers are
required for hashtables (which stash bits in the unused pointer bits,
so won't be found).

There's `notleak()` for annotating things that aren't leaks: things
like globals and timers, and other semi-transients.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-20 12:43:10 +01:00
Rusty Russell 8c665835fa jsonrpc: make explicit call to indicate cmd is still pending.
jsonrpc handlers usually directly call command_success or
command_fail; not doing that implies they're waiting for something
async.

Put an explicit call (currently a noop) there, and add debugging
checks to make sure it's used.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-20 12:43:10 +01:00
Rusty Russell 28f02e0c0e wallet: use real feerates for withdraw rpc.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-20 01:41:22 +00:00
Rusty Russell 3da93319fb withdraw: make rpc call accept 'all' as a value.
Closes: #409
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-20 01:41:22 +00:00
Rusty Russell 8cd7ebb924 wallet: wallet_select_all to select everything.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-20 01:41:22 +00:00
ZmnSCPxj 4022fc0034 wallet.c: Minor leak in wallet_invoices_load. 2017-12-15 10:41:54 +00:00
ZmnSCPxj e26d294b60 Add support for withdrawing to BECH32 addresses.
Fixes: #428
2017-12-14 03:36:40 +00:00
Rusty Russell bbbbb60863 wallet: don't leak string on update.
We actually don't need tal_fmt() here at all, anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-13 22:46:10 +01:00
Rusty Russell 5a319c781c wallet: don't leak UTXO array on extracting outputs.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-13 22:46:10 +01:00
Rusty Russell 9747d1a62c database: don't leak query string.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-13 22:46:10 +01:00
Rusty Russell 005f3826c9 wallet/tests: add mocks for futureproofing.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-13 22:46:10 +01:00
Rusty Russell 2931760b5b wallet: move tests into test/ dir like other unit tests.
Trivial move, but makes it easy to add new ones.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-13 22:46:10 +01:00
ZmnSCPxj 9842898ca4 Add support for withdrawing to P2SH addresses. 2017-12-13 03:10:04 +00:00
ZmnSCPxj d43fb87871 Factor out scriptpubkey_from_address function from json_withdraw. 2017-12-13 03:10:04 +00:00
ZmnSCPxj 9f481226fa Rearrange checking of withdrawal address parsing success in json_withdraw. 2017-12-13 03:10:04 +00:00
ZmnSCPxj b56fea8b2a Modify hsm_sign_withdrawal to transmit scriptpubkey to withdraw to, rather than pkh. 2017-12-13 03:10:04 +00:00
ZmnSCPxj 5fd74f9933 Change withdraw_tx to accept scriptpubkey rather than plain address. 2017-12-13 03:10:04 +00:00
Christian Decker 61852b4603 cleanup: Use check_act* in handshake and remove unused static inline
This was tripping up `clang`.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-12-12 02:31:03 +00:00
practicalswift 486d321094 Avoid NULL pointer dereference in channelseq(...) 2017-12-11 16:07:57 +01:00
ZmnSCPxj 38fc280fad Make json_withdraw check testnet flag.
bitcoin_from_base58 returns a testnet flag, but json_withdraw
did not actually check it. Add a basic check that the given
withdraw address belongs to the same net lightningd is using.
2017-12-09 16:18:59 +01:00
practicalswift 61c47c09d0 Fix typos 2017-12-08 13:07:20 +01:00
Christian Decker 9ba99d2b2d hsm: Cleanup after merging control and client libraries
Change all calls to use the correct serialization and deserialization
functions, include the correct headers and remove the control
messages.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-12-03 17:06:13 +01:00
Rusty Russell be35895847 lightningd: instead of malformed code, keep generic error code.
This is in preparation for generating the actual error messages inside
channeld.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-12-02 12:54:12 +01:00
Christian Decker c29923a623 topology: Add transaction filtering to connect_block
The filter is being populated while initializing the daemon and by
adding new keys as they are being generated. The filter is then used
in connect_block to identify transactions of interest.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-11-29 14:39:12 +01:00
Christian Decker d912d45af8 wallet: Expose wallet_shachain_get_hash to remove warning
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-11-29 14:39:12 +01:00
Christian Decker f4a71f9806 tests: Add stmt2wallet test for remote_shutdown_scriptpubkey
We didn't test that this is done correctly. Now we are :-)

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-11-26 22:45:48 +00:00
Christian Decker 6beb4eb171 wallet: Fix a column mismatch and use absolute column indices
We weren't incrementing the `col` for the `local_shutdown_idx` field,
which meant that all following fields were incorrect. I removed the
`col` computation and opted for absolute indices instead, since they
are way less brittle. Just remember to add new fields to the query at
the end so we don't have to shift too often :-)

Reported-by: William Casarin @jb55
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-11-26 22:45:48 +00:00
Rusty Russell b836b452dc feerate: keep feerates separately for each side.
When we support changing them, they can be different during the transition.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-23 12:40:32 +01:00
Rusty Russell f1e4cad9d4 feerate: use u32 everywhere.
The wire protocol uses this, in the assumption that we'll never see feerates
in excess of 4294967 satoshi per kiloweight.

So let's use that consistently internally as well.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-23 12:40:32 +01:00
Rusty Russell e302d6193c invoice: store expiry time in db.
This is backwards-compat: sets existing expiry for invoices to ~infinity.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-23 00:11:04 +01:00
Christian Decker be97673259 wallet: Add function to retrieve a list of payments
Used by the JSON-RPC for the listtransfers call. Currently does not
support any form of paging.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-11-20 06:28:03 +00:00
Christian Decker d1cc5f4552 wallet: Add unit tests for payment persistence
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-11-20 06:28:03 +00:00
Christian Decker 967dd6c776 wallet: Add functions to store and manipulate payments
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-11-20 06:28:03 +00:00
Christian Decker 6601c43edc db: Add payments table
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-11-20 06:28:03 +00:00
Christian Decker 490ee76162 wallet: Add payment struct to track status of transfers
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-11-20 06:28:03 +00:00
Rusty Russell f71a18241a wallet: save and restore htlc_basepoint for remote side.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-16 17:40:57 +01:00
Rusty Russell 9fd81ab06d db: make db_exec() an internal function.
Every caller is using prepared statements now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-06 10:24:34 +01:00
Rusty Russell 4fb472b7a7 db: we must always be in a transaction, remove nested, call fatal()
We save location where transaction was started, in case we try to nest.
There's now no error case; db_exec_mayfail() is the only one.

This means the tests need to override fatal() if they want to intercept
these errors.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-06 10:24:34 +01:00
Rusty Russell f4d27eefa1 wallet: remove internal transactions.
We're going to be always in a transaction soon.

Note the rollback we used to do was an optimization: the utxo destructors
would already clean up the new UTXOs in the database.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-06 10:24:34 +01:00
Rusty Russell 7133a2f9b3 wallet: assume db errors will be fatal, don't check.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-06 10:24:34 +01:00
Rusty Russell 3282768302 wallet: don't fail, assume db ops will call fatal.
And override fatal() in wallet_tests to be sure.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-06 10:24:34 +01:00
Rusty Russell 1eb7e92a30 db_migrate: get version inside transaction.
we should never be doing two startups at once, but why take chances?  Plus,
we can then assert that all db calls are in transactions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-06 10:24:34 +01:00
Rusty Russell bbe7a03300 wallet: use db_exec_mayfail() for wallet_add_utxo.
This is the only case where we actually rely on the db to ensure we don't
do something twice: don't error out if it fails.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-06 10:24:34 +01:00
Rusty Russell b148b89baf db_exec_mayfail: variant of db_exec where we actually expect an error.
There's one caller where db_exec can actually fail due to constraints,
and we rely on it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-06 10:24:34 +01:00
Rusty Russell 1f7e370fda db: rollback transaction if we had an error.
This is temporary; we'll eventually fail on error.  However, since
db_exec() is a NOOP if we have an error, we need to do something.
2017-11-06 10:24:34 +01:00
Rusty Russell 360aa15e4d db_query: don't remove transaction or set error if query fails.
We return NULL in this case.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-06 10:24:34 +01:00
Rusty Russell 5e46af64fc db: keep in_transaction as a counter, so we can nest commits.
Otherwise we find ourselves outside a commitment.  This is a bandaid
until we remove nested commitments again at the end of this series.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-06 10:24:34 +01:00
Christian Decker 77789bb705 db: Implemented poor mans nested transactions
Nesting is provided by only actually performing the outermost
transaction and simulating the nested ones. This still allows us to
ensure on lower levels that we are in the context of a transaction
without having to resort to keeping explicitly track of it in the
calling code.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-11-06 10:24:34 +01:00
Rusty Russell 9ec5cb7ba2 script: enhance is_p2sh/is_p2pkh/is_p2wsh/is_p2wpkh to extract addr.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-11-01 01:09:23 +00:00
Christian Decker da183c22a0 db: Cleanup all remaining traces of hex-encoded values
In addition we also set some of the test values to a pattern instead
of just `memset`ting it to 0, which may hide some crossed lines.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-31 22:43:47 +00:00
Christian Decker c1d364c5fb wallet: Migrate channel persistence to native sqlite3 binding
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-31 22:43:47 +00:00
Christian Decker 9c12c807d1 wallet: Migrate shachain persistence to native sqlite3 binding
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-31 22:43:47 +00:00
Christian Decker 8f198f3746 wallet: Migrate output tracking to native sqlite3 binding
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-31 22:43:47 +00:00
Christian Decker abad23b339 wallet: Removing hex fields from HTLCS 2017-10-31 22:43:47 +00:00
Christian Decker e9cfa65a12 wallet: Migrate HTLC persistence to native sqlite3 binding
This is a preparatory step before we get rid of the hex encoding of
blob values.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-31 22:43:47 +00:00
Christian Decker 33da7f50c7 db: Added short_channel_id, tx, pubkey and signature primitives
We use these quite often and it is cumbersome having to do these
simple conversions inline, so just expose pseudo-sqlite3 methods to
bind and extract from/to a stmt.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-31 22:43:47 +00:00
Christian Decker b267b24c08 db: db_exec_prepared takes ownership of the statement
Technically it's the caller that'll own the statement, but it is nice
to have db_exec_prepared dispose of it.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-31 22:43:47 +00:00
Rusty Russell 4a06da8f78 wallet: fix wallet_update_output_status where oldstatus == output_state_any
"near \"AND\": syntax error"

This was caught by the "always keep errors for db_commit_transaction".

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-31 04:14:33 +00:00
Rusty Russell 48cedef756 peer_control: remove unique_id field.
It's now completely useless.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-24 16:12:22 +02:00
Rusty Russell 8f057f7fc7 Revert "gossip: send the *other* node's cltv_expiry_delta in channel_announce."
This reverts commit 297e278132.
2017-10-11 11:54:50 +02:00
Christian Decker 49ed7c9ab0 wallet: Add primitives to retrieve htlc_stubs for channel
We'd like to not keep them in memory and retrieve them on-demand when
`onchaind` is launched. This uses the `channel_htlcs` table as backing
but only fetches the minimal necessary information.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-10 23:59:34 +00:00
Rusty Russell 297e278132 gossip: send the *other* node's cltv_expiry_delta in channel_announce.
Include tests from example doc.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-10 20:17:37 +02:00
Christian Decker 9a106bf799 wallet: Add invoice removal support
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker a005bce155 wallet: Adding primitives to save/load invoices to wallet
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker 048680d0f2 db: Add db_prepare and db_exec_prepared to expose native binding
This makes executing a query/command a two step process, but allows us
to use the native binding and avoid having to build queries as SQL
strings. Two major advantages are that we are no longer vulnerable to
SQL injections and that we do not have to hex-encode binary fields
like private keys, hashes, and routing onions, halving the storage
requirements for those.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker 067d2d7435 db: Added table for invoices
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker b4732f6091 wallet_tests: Testing the rewiring of loaded htlcs
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker 4114f6f79b db: Clear errors before executing new query
This was causing me some trouble by making it look like the last query
failed, when it really was an old one. No need to drag failures around
for longer than needed.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker e162a53e86 wallet_tests: Enable logging in the unit tests
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker df9b8e22b4 wallet: Added helper to rewire HTLCs after loading from DB
This is a necessary evil since at the time we load `struct htlc_out`
associated with a channel we might not have loaded the `struct
htlc_in` that it depends on, so we defer the rewiring until we have
loaded all HTLCs for all channels. At that point rewiring MUST work,
otherwise we report a failure.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker c6f210a46c htlcs: Needed to add origin_htlc_id to outgoing HTLCs
While loading HTLCs from the database we might not yet have all the
incoming HTLCs loaded when loading a dependent htlc_out. So we defer
the wiring of the HTLCs until we are sure we have them loaded.

This is also the first step towards keeping that association only in
the database, since otherwise we cannot selectively load channels from
DB.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker eb5ed961ed wallet: Added loading of HTLCs from the database
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker 53763ba6a4 wallet: Store HTLCs to the wallet
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker b8fa765c3f wallet: Add primitives to store htlcs in the database
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Christian Decker 3dda72c885 db: Create table for HTLCs
Also added a small warning to one of the used enums not to reorder or
insert values. They'd break the update path.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-10-09 11:51:13 +10:30
Rusty Russell f219955dc6 wallet: fix spending of change.
We recorded our own change as P2SH; it's not.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-12 22:59:24 +02:00
Christian Decker d14c9d30cd moveonly: Move make wallet_extract_owned available publicly
This was so far only used in the walletrpc, but we'll need it in a few
places.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-09-10 10:48:53 +09:30
Christian Decker 71618d07e6 jsonrpc: Add listfunds RPC command
Addresses #207 by adding a method to retrieve available funds from the
wallet.

Reported-by: @jl777
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-09-06 05:14:38 +09:30
Christian Decker 4b59c4f857 fix: Do not assume size_t == u64
This was causing some compilation trouble on 32bit systems, see #256.

Reported-by: @shsmith
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-09-05 18:12:28 +02:00
Rusty Russell bbed5e3411 Rename subdaemons, move them into top level.
We leave the *build* results in lightningd/ for ease of in-place testing though.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 739b163f8b Makefiles: simplify dependencies.
Gather all binaries and objects and make the depend on external
requirements and common headers.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell c6976cd947 shachain: always build 48 bit version.
No more special Makefile hacks required.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 153c622157 lightningd: remove lightningd_state.
Some fields were redundant, some are simply moved into 'struct lightningd'.
All routines updated to hand 'struct lightningd *ld' now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell a37c165cb9 common: move some files out of lightningd/
Basically all files shared by different daemons.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 8375857116 common: absorb remaining files from daemon/
Also, we split the more sophisticated json_add helpers to avoid pulling in
everything into lightning-cli, and unify the routines to print struct
short_channel_id (it's ':',  not '/' too).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 85ff95e829 common: new directory for any shared objects.
To avoid everything pulling in HTLCs stuff to the opening daemon, we
split the channel and commit_tx routines into initial_channel and
initial_commit_tx (no HTLC support) and move full HTLC supporting versions
into channeld.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00
Rusty Russell 1396cf6249 lightningd: remove num_revocations_received.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-23 17:38:22 +02:00
Christian Decker def3d77a70 lightningd: Base peer->seed off of the channel ID
The peer->seed needs to be unique for each channel, since bitcoin
pubkeys and the shachain are generated from it. However we also need
to guarantee that the same seed is generated for a given channel every
time, e.g., upon a restart. The DB channel ID is guaranteed to be
unique, and will not change throughout the lifetime of a channel, so
we simply mix it in, instead of a separate increasing counter.

We also needed to make sure to store in the DB before deriving the
seed, in order to get an ID assigned by the DB.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-23 10:23:54 +09:30
Christian Decker e51d261f51 lightningd: Load persisted channels on startup
This is the big one, and it's completely anticlimactic: it loads all
channels that have reached opening and are not marked as
closingd_complete into memory, that's it.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-23 10:23:54 +09:30
Christian Decker 4e6f9787b6 wallet: unique_id is not the same as dbid
They happen to advance at the same pace but mixing them may have
unforeseen consequences, and I have done so a few times already so
this explicitly separates them.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-23 10:23:54 +09:30
Christian Decker 1a94e7282b db: Make peers unique by node_id
We should only ever have a single entry for each node_id, enforcing
this on the DB.
2017-08-23 10:23:54 +09:30
Christian Decker 4bc0750882 wallet: Moving wallet_channel->peer_id into the peer struct
This was supposed to be a temporary solution anyway, and I had a
rather annoying mixup between peer_id and unique_id, the latter of
which is actually a connection identifier.
2017-08-23 10:23:54 +09:30
Christian Decker 50ee067393 wallet: Set shutdown keyindex to -1 if not set
Was using unsigned ints before, which is just plain wrong.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-23 10:23:54 +09:30
Christian Decker 05aaaa1f49 wallet: Actually save the correct min_index when creating shachains
If we kill the daemon without performing any commits we ended up with
a 0 instead of UINT48_MAX which was expected.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-23 10:23:54 +09:30
Rusty Russell 1ffb9f07c1 ccan: update to latest shachain.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell 893335244d openingd: supply initial tx as well as signature.
And store in peer->last_tx/peer->last_sig like all other places,
that way we broadcast it if we need to.

Note: the removal of tmpctx in funder_channel() is needed because we
use txs[0], which was allocated off tmpctx.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell c389215a35 closingd: send the tx to master as we negotiate.
We can derive fees from this, and also broadcast at any time without
having to reconstruct it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Rusty Russell dbfac68c3f lightningd: keep last valid tx, and its signature.
This avoids us having to reconstruct it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-20 13:06:41 +09:30
Christian Decker 3e42485ddb fix: Typo in the error message for adding an output to DB
Fixes #216

Reported-by: @jl777
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-18 14:40:34 +02:00
Christian Decker 1ac10716be wallet: Add closing parameters to channel state
I must have missed these before, so adding them now.
2017-08-10 12:34:58 +09:30
Christian Decker 1070bbccde wallet: Add channel_config persistence to channel persistence
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-10 12:34:58 +09:30
Christian Decker f2382884b6 wallet: Add roundtrip test for channel_config persistence
Signed-off-by: Christian Decker <decker.christian@gmail.com>
2017-08-10 12:34:58 +09:30
Christian Decker cc5af13e05 wallet: Implement channel_config persistence 2017-08-10 12:34:58 +09:30
Christian Decker 6f441d3cd2 wallet: Add a CRU(D) test for wallet_channels
Simple roundtrip testing which writes to DB, reads it back in and
checks for equality.
2017-08-10 12:34:58 +09:30
Christian Decker af62c9ca97 wallet: Insert/Update channels into database
Definitely not as nice as it could be, but it works for now. This is
primarily intended as a simple dump method that just saves everything
to the database. We will later use smaller incremental updates to
update specific things. wallet_channel_save serves both to insert as
well as update.
2017-08-10 12:34:58 +09:30
Christian Decker cfe87b16c3 db: Do not exit transaction on failure
Automatically exiting the DB transaction upon any failure is strange
since it'll kill any later attempt to commit. The commit itself should
be used to verify that everything was ok.
2017-08-10 12:34:58 +09:30
Christian Decker a5b425faad wallet: Implement loading of stored channels from DB
Ugliest code I've written in a while, but it seems to do the trick.
2017-08-10 12:34:58 +09:30
Christian Decker 5912c68185 db: Add a hexval helper to decode hex values from db into fields
This is going to be handy pretty soon. Channels are almost fully
blobs...
2017-08-10 12:34:58 +09:30
Christian Decker 33ffb53d9e db: Added schema for peers and channels 2017-08-10 12:34:58 +09:30
Christian Decker 1ff64ef07c wallet: Implement delete for wallet_shachain 2017-08-10 12:34:58 +09:30
Christian Decker 416d6bf860 db: Enable foreign keys and cascade on shachain delete
Should make maintenance a bit easier.
2017-08-10 12:34:58 +09:30
Christian Decker 8b7efd01d7 wallet: Created a simple persisted shachain unit-test
This exercises the create, read and update functionality of the
persisted shachain.
2017-08-10 12:34:58 +09:30
Christian Decker 34baf05973 wallet: Implement shachain persistence
This needed a rather annoying hack since sqlite3 can only store
integers up to 2^63, so I just squash it down/invert it, and hope that
we never ever have more than 2^63 updates.
2017-08-10 12:34:58 +09:30
Christian Decker b0c64909e7 wallet: Added database schema for shachain persistence 2017-08-10 12:34:58 +09:30
Rusty Russell a0800e352a wallet_get_newindex: encapsulate routine to get a new keyindex.
We'll want this for shutdown.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 2ff50107ac lightningd/hsm_control: use a simple fd for HSM.
Now we're always sync, just use an fd.  Put the hsm_sync_read() helper
here, too, and do HSM init sync which makes things much simpler.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Rusty Russell 8631539822 wallet: talk to HSM synchronously.
We temporarily play games with the hsm fd; those will go away once
we're done.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-06-27 10:25:53 +09:30
Christian Decker 8c6fab0ec4 wallet: Do not create dust from withdrawal transactions 2017-06-23 16:02:20 +09:30
Christian Decker a1faccb9dc walletrpc: Split owned output detection out and use it for change
Splitting the detection for outputs that we own into a separate
`wallet_extract_owned_outputs` function and use it when the broadcast
succeeds to re-add the change output back to the database.
2017-06-23 16:02:20 +09:30
Christian Decker 938ab67a01 wallet: move can_spend to wallet.c and json_addfunds to walletrpc.c 2017-06-23 16:02:20 +09:30
Christian Decker d6656358b5 wallet: Add bip32_base to the wallet struct
Wallet should really be the container for anything bip32 related, so
I'd like to slowly wean off of `ld->bip32_base` in favor of
`ld->wallet->bip32_base`
2017-06-23 16:02:20 +09:30
Christian Decker 8cdab9da1e wallet: Move json_newaddr to wallet.c 2017-06-23 16:02:20 +09:30
Christian Decker b4e4a744aa walletrpc: Implemented the withdrawal flow 2017-06-23 16:02:20 +09:30
Christian Decker a775b52941 wallet: Return change satoshis when selecting coins
We'd be computing them later most of the time anyway.
2017-06-23 16:02:20 +09:30
Christian Decker 7738bccf42 wallet: Move coin-selection primitives to wallet
We'll re-use them a few times so having them at a central location is
nice. We also fix a bug that was unreserving UTXO entries upon free,
instead of promoting them to being spent.
2017-06-23 16:02:20 +09:30
Christian Decker 1522eee528 unittests: Test some more of the db primitives. 2017-06-23 16:02:20 +09:30
Christian Decker 3404509928 wallet: Move UTXO tracking to DB
Since we have a simple way to query the database for UTXOs we can
simplify some of the coin selection logic. That gets rid of the
in-memory list of UTXOs.
2017-06-06 09:16:10 +09:30
Christian Decker 257ecf6222 wallet: Helper to store/retrieve persistent vars to/from DB
Not the nicest code, but it allows us to store the bip32_max_index so
that we don't forget our addresses upon restart. We could have done
the same by retrieving the max index from our index, but then we'd
forget addresses that don't have an associated output. Conversion
to/from string is so that we can store arbitrary one off values in the
DB in the future, independent of type.
2017-06-06 09:16:10 +09:30
Christian Decker bdb6b1002f wallet: Reserve UTXOs used in build_utxos 2017-06-06 09:16:10 +09:30
Christian Decker 19a4e7f542 wallet: Expose transactional interface for db 2017-06-06 09:16:10 +09:30
Christian Decker 5396335363 wallet: Add wallet_tests and simplify db_tests
I'll eventually move the utils into a different location (maybe CCAN?)
but for now I'm keeping it close to where it is used.
2017-06-06 09:16:10 +09:30
Christian Decker e91aff210e wallet: Add compile dependency on CCAN 2017-06-06 09:16:10 +09:30
Christian Decker 9882a9fb29 wallet: Start the wallet interface and link it into lightningd
The database is hidden behind the wallet interface, which has all the
wallet specific functionality. First up is the tracking of outputs.
2017-06-06 09:16:10 +09:30
Christian Decker 80436d06cb wallet: Create and manage a sqlite3 database for the wallet 2017-06-06 09:16:10 +09:30