Browse Source

wallet: Hook into the hsm_funding_sig to extract change outputs

This is the step where we broadcast the transaction to the network and
a nice place to extract the change from the transaction.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
committed by Rusty Russell
parent
commit
fa13190595
  1. 4
      lightningd/peer_control.c
  2. 19
      tests/test_lightningd.py

4
lightningd/peer_control.c

@ -1378,6 +1378,7 @@ static void opening_got_hsm_funding_sig(struct funding_channel *fc,
{
secp256k1_ecdsa_signature *sigs;
struct bitcoin_tx *tx = fc->funding_tx;
u64 change_satoshi;
size_t i;
if (!fromwire_hsmctl_sign_funding_reply(fc, resp, NULL, &sigs))
@ -1407,6 +1408,9 @@ static void opening_got_hsm_funding_sig(struct funding_channel *fc,
watch_tx(fc->peer, fc->peer->ld->topology, fc->peer, tx,
funding_lockin_cb, NULL);
/* Extract the change output and add it to the DB */
wallet_extract_owned_outputs(fc->peer->ld->wallet, tx, &change_satoshi);
/* FIXME: Remove arg from cb? */
watch_txo(fc->peer, fc->peer->ld->topology, fc->peer,
fc->peer->funding_txid, fc->peer->funding_outnum,

19
tests/test_lightningd.py

@ -1056,6 +1056,25 @@ class LightningDTests(BaseLightningDTests):
c.execute('SELECT COUNT(*) FROM outputs WHERE status=2')
assert(c.fetchone()[0] == 2)
def test_funding_change(self):
"""Add some funds, fund a channel, and make sure we remember the change
"""
l1, l2 = self.connect()
addr = l1.rpc.newaddr()['address']
txid = l1.bitcoin.rpc.sendtoaddress(addr, 0.1)
tx = l1.bitcoin.rpc.getrawtransaction(txid)
l1.rpc.addfunds(tx)
outputs = l1.db_query('SELECT value FROM outputs WHERE status=0;')
assert len(outputs) == 1 and outputs[0]['value'] == 10000000
l1.rpc.fundchannel(l2.info['id'], 1000000)
outputs = {r['status']: r['value'] for r in l1.db_query(
'SELECT status, SUM(value) AS value FROM outputs GROUP BY status;')}
# The 10m out is spent and we have a change output of 9m-fee
assert outputs[0] > 8990000
assert outputs[2] == 10000000
def test_channel_persistence(self):
# Start two nodes and open a channel (to remember)
l1, l2 = self.connect()

Loading…
Cancel
Save