From 9eeb76e1853daa6c93a0e5017675eeeb2b0ea7ad Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 7 Mar 2017 11:33:55 +1030 Subject: [PATCH] lightningd: broadcast transaction once we're told to. Signed-off-by: Rusty Russell --- lightningd/peer_control.c | 82 ++++++++++++++++++++++++++++++++++++-- lightningd/test/test-basic | 7 ++++ 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index da8e68dc5..67976c29c 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1,11 +1,13 @@ #include "lightningd.h" #include "peer_control.h" #include "subdaemon.h" +#include #include #include #include #include #include +#include #include #include #include @@ -515,22 +517,70 @@ static void fail_fundchannel_command(struct funding_channel *fc) command_fail(fc->cmd, "Peer died"); } +static void funding_broadcast_failed(struct peer *peer, + int exitstatus, const char *err) +{ + log_unusual(peer->log, "Funding broadcast exited with %i: %s", + exitstatus, err); + /* FIXME: send PKT_ERR to peer if this happens. */ + tal_free(peer); +} + +static enum watch_result funding_depth_cb(struct peer *peer, + unsigned int depth, + const struct sha256_double *txid, + void *unused) +{ + if (depth >= peer->ld->dstate.config.anchor_confirms) { + peer_set_condition(peer, "Funding tx reached depth %u", depth); + /* FIXME! Start channel proper... */ + return DELETE_WATCH; + } + log_debug(peer->log, "Funding tx depth %u of %u", depth, + peer->ld->dstate.config.anchor_confirms); + return KEEP_WATCHING; +} + static void opening_got_hsm_funding_sig(struct subdaemon *hsm, const u8 *resp, struct funding_channel *fc) { secp256k1_ecdsa_signature *sigs; + struct bitcoin_tx *tx = fc->funding_tx; + size_t i; if (!fromwire_hsmctl_sign_funding_response(fc, resp, NULL, &sigs)) fatal("HSM gave bad sign_funding_response %s", tal_hex(fc, resp)); + if (tal_count(sigs) != tal_count(tx->input)) + fatal("HSM gave %zu sigs, needed %zu", + tal_count(sigs), tal_count(tx->input)); + peer_set_condition(fc->peer, "Waiting for our funding tx"); /* FIXME: Defer until after funding locked. */ tal_del_destructor(fc, fail_fundchannel_command); command_success(fc->cmd, null_response(fc->cmd)); fc->cmd = NULL; - /* FIXME: broadcast tx... */ + /* Create input parts from signatures. */ + for (i = 0; i < tal_count(tx->input); i++) { + struct pubkey key; + + if (!bip32_pubkey(fc->peer->ld->bip32_base, + &key, fc->utxomap[i]->keyindex)) + fatal("Cannot generate BIP32 key for UTXO %u", + fc->utxomap[i]->keyindex); + + /* P2SH inputs have same witness. */ + tx->input[i].witness + = bitcoin_witness_p2wpkh(tx, &sigs[i], &key); + } + + /* Send it out and watch for confirms. */ + broadcast_tx(hsm->ld->topology, fc->peer, tx, funding_broadcast_failed); + watch_tx(fc->peer, fc->peer->ld->topology, fc->peer, tx, + funding_depth_cb, NULL); + tal_free(fc); } static void opening_release_tx(struct subdaemon *opening, const u8 *resp, @@ -595,8 +645,32 @@ static void opening_gen_funding(struct subdaemon *opening, const u8 *resp, static void opening_accept_response(struct subdaemon *opening, const u8 *resp, struct peer *peer) { - peer_set_condition(peer, "Waiting for their commitment tx"); - /* FIXME... */ + struct sha256_double funding_txid; + u16 funding_txout; + struct channel_config their_config; + secp256k1_ecdsa_signature first_commit_sig; + struct crypto_state crypto_state; + struct pubkey remote_fundingkey, revocation_basepoint, + payment_basepoint, delayed_payment_basepoint, + their_per_commit_point; + + log_debug(peer->log, "Got opening_accept_response"); + if (!fromwire_opening_accept_resp(resp, NULL, + &funding_txid, &funding_txout, + &their_config, &first_commit_sig, + &crypto_state, &remote_fundingkey, + &revocation_basepoint, + &payment_basepoint, + &delayed_payment_basepoint, + &their_per_commit_point)) { + log_broken(peer->log, "bad OPENING_ACCEPT_RESP %s", + tal_hex(resp, resp)); + tal_free(peer); + } + + /* FIXME: Start normal channel daemon... */ + watch_txid(peer, peer->ld->topology, peer, &funding_txid, + funding_depth_cb, NULL); } static void channel_config(struct lightningd *ld, @@ -704,7 +778,7 @@ void peer_accept_open(struct peer *peer, tal_free(peer); return; } - subdaemon_req(peer->owner, take(msg), -1, &peer->fd, + subdaemon_req(peer->owner, take(msg), -1, NULL, opening_accept_response, peer); } diff --git a/lightningd/test/test-basic b/lightningd/test/test-basic index 6920c21ca..071361327 100755 --- a/lightningd/test/test-basic +++ b/lightningd/test/test-basic @@ -40,6 +40,13 @@ lcli1 addfunds $FUND_INPUT_TX | $FGREP '"satoshis" : 10000002' # Now fund a channel. lcli1 fundchannel $ID2 100000 +# Now wait for it to reach depth +lcli1 getpeers info | $FGREP "Waiting for our funding tx" + +$CLI generate 10 +check "lcli1 getpeers info | $FGREP 'Funding tx reached depth'" +check "lcli2 getpeers info | $FGREP 'Funding tx reached depth'" + lcli1 stop lcli2 stop