From 0e93fb932a3a12581341497bd64ca5428a382e75 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 12 Feb 2018 20:43:04 +1030 Subject: [PATCH] lightningd: bitcoind and topology routines take channel, not peer. Signed-off-by: Rusty Russell --- lightningd/bitcoind.h | 1 - lightningd/chaintopology.c | 38 +++--- lightningd/chaintopology.h | 10 +- lightningd/peer_control.c | 184 +++++++++++++---------------- lightningd/test/run-find_my_path.c | 2 +- lightningd/watch.c | 32 ++--- lightningd/watch.h | 44 +++---- wallet/test/run-wallet.c | 18 +-- 8 files changed, 155 insertions(+), 174 deletions(-) diff --git a/lightningd/bitcoind.h b/lightningd/bitcoind.h index ff8f70421..146c4d55b 100644 --- a/lightningd/bitcoind.h +++ b/lightningd/bitcoind.h @@ -16,7 +16,6 @@ struct block; struct lightningd; struct ripemd160; struct bitcoin_tx; -struct peer; struct bitcoin_block; enum bitcoind_mode { diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index ad945b4c3..6fb7d60f5 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -210,52 +210,52 @@ static void destroy_outgoing_tx(struct outgoing_tx *otx) list_del(&otx->list); } -static void clear_otx_peer(struct peer *peer, struct outgoing_tx *otx) +static void clear_otx_channel(struct channel *channel, struct outgoing_tx *otx) { - if (otx->peer != peer) - fatal("peer %p, otx %p has peer %p", peer, otx, otx->peer); - otx->peer = NULL; + if (otx->channel != channel) + fatal("channel %p, otx %p has channel %p", channel, otx, otx->channel); + otx->channel = NULL; } static void broadcast_done(struct bitcoind *bitcoind, int exitstatus, const char *msg, struct outgoing_tx *otx) { - /* Peer gone? Stop. */ - if (!otx->peer) { + /* Channel gone? Stop. */ + if (!otx->channel) { tal_free(otx); return; } - /* No longer needs to be disconnected if peer dies. */ - tal_del_destructor2(otx->peer, clear_otx_peer, otx); + /* No longer needs to be disconnected if channel dies. */ + tal_del_destructor2(otx->channel, clear_otx_channel, otx); if (otx->failed && exitstatus != 0) { - otx->failed(otx->peer, exitstatus, msg); + otx->failed(otx->channel, exitstatus, msg); tal_free(otx); } else { - /* For continual rebroadcasting, until peer freed. */ - tal_steal(otx->peer, otx); + /* For continual rebroadcasting, until channel freed. */ + tal_steal(otx->channel, otx); list_add_tail(&bitcoind->ld->topology->outgoing_txs, &otx->list); tal_add_destructor(otx, destroy_outgoing_tx); } } void broadcast_tx(struct chain_topology *topo, - struct peer *peer, const struct bitcoin_tx *tx, - void (*failed)(struct peer *peer, + struct channel *channel, const struct bitcoin_tx *tx, + void (*failed)(struct channel *channel, int exitstatus, const char *err)) { - /* Peer might vanish: topo owns it to start with. */ + /* Channel might vanish: topo owns it to start with. */ struct outgoing_tx *otx = tal(topo, struct outgoing_tx); const u8 *rawtx = linearize_tx(otx, tx); - otx->peer = peer; + otx->channel = channel; bitcoin_txid(tx, &otx->txid); otx->hextx = tal_hex(otx, rawtx); otx->failed = failed; tal_free(rawtx); - tal_add_destructor2(peer, clear_otx_peer, otx); + tal_add_destructor2(channel, clear_otx_channel, otx); log_add(topo->log, " (tx %s)", type_to_string(ltmp, struct bitcoin_txid, &otx->txid)); @@ -679,7 +679,7 @@ void chaintopology_mark_pointers_used(struct htable *memtable, } #endif /* DEVELOPER */ -/* On shutdown, peers get deleted last. That frees from our list, so +/* On shutdown, channels get deleted last. That frees from our list, so * do it now instead. */ static void destroy_outgoing_txs(struct chain_topology *topo) { @@ -710,14 +710,14 @@ struct chain_topology *new_topology(struct lightningd *ld, struct log *log) void setup_topology(struct chain_topology *topo, struct timers *timers, - struct timerel poll_time, u32 first_peer_block) + struct timerel poll_time, u32 first_channel_block) { memset(&topo->feerate, 0, sizeof(topo->feerate)); topo->timers = timers; topo->poll_time = poll_time; /* Start one before the block we are interested in (as we won't * get notifications on txs in that block). */ - topo->first_blocknum = first_peer_block - 1; + topo->first_blocknum = first_channel_block - 1; /* Make sure bitcoind is started, and ready */ wait_for_bitcoind(topo->bitcoind); diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index d4871e749..b35776c8e 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -28,10 +28,10 @@ enum feerate { /* Off topology->outgoing_txs */ struct outgoing_tx { struct list_node list; - struct peer *peer; + struct channel *channel; const char *hextx; struct bitcoin_txid txid; - void (*failed)(struct peer *peer, int exitstatus, const char *err); + void (*failed)(struct channel *channel, int exitstatus, const char *err); }; struct block { @@ -145,15 +145,15 @@ u32 get_feerate(const struct chain_topology *topo, enum feerate feerate); /* Broadcast a single tx, and rebroadcast as reqd (copies tx). * If failed is non-NULL, call that and don't rebroadcast. */ void broadcast_tx(struct chain_topology *topo, - struct peer *peer, const struct bitcoin_tx *tx, - void (*failed)(struct peer *peer, + struct channel *channel, const struct bitcoin_tx *tx, + void (*failed)(struct channel *channel, int exitstatus, const char *err)); struct chain_topology *new_topology(struct lightningd *ld, struct log *log); void setup_topology(struct chain_topology *topology, struct timers *timers, - struct timerel poll_time, u32 first_peer_block); + struct timerel poll_time, u32 first_channel_block); void begin_topology(struct chain_topology *topo); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 78c9844fd..4bf952e58 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -69,7 +69,7 @@ static void peer_offer_channel(struct lightningd *ld, u64 gossip_index, const u8 *gfeatures, const u8 *lfeatures, int peer_fd, int gossip_fd); -static bool peer_start_channeld(struct peer *peer, +static bool peer_start_channeld(struct channel *channel, const struct crypto_state *cs, u64 gossip_index, int peer_fd, int gossip_fd, @@ -187,7 +187,7 @@ void drop_to_chain(struct lightningd *ld, struct channel *channel) /* Keep broadcasting until we say stop (can fail due to dup, * if they beat us to the broadcast). */ - broadcast_tx(ld->topology, channel2peer(channel), channel->last_tx, NULL); + broadcast_tx(ld->topology, channel, channel->last_tx, NULL); remove_sig(channel->last_tx); } @@ -379,7 +379,7 @@ void peer_connected(struct lightningd *ld, const u8 *msg, peer_set_owner(peer, NULL); peer->addr = addr; - peer_start_channeld(peer, &cs, gossip_index, + peer_start_channeld(channel, &cs, gossip_index, peer_fd, gossip_fd, NULL, true); goto connected; @@ -816,28 +816,26 @@ struct funding_channel { struct peer *peer; }; -static void funding_broadcast_failed(struct peer *peer, +static void funding_broadcast_failed(struct channel *channel, int exitstatus, const char *err) { - channel_internal_error(peer2channel(peer), + channel_internal_error(channel, "Funding broadcast exited with %i: %s", exitstatus, err); } -static enum watch_result funding_announce_cb(struct peer *peer, +static enum watch_result funding_announce_cb(struct channel *channel, const struct bitcoin_tx *tx, unsigned int depth, void *unused) { - struct channel *channel = peer2channel(peer); - if (depth < ANNOUNCE_MIN_DEPTH) { return KEEP_WATCHING; } if (!channel->owner || !streq(channel->owner->name, "lightning_channeld")) { - log_debug(peer->log, - "Funding tx announce ready, but peer state %s" + log_debug(channel->log, + "Funding tx announce ready, but channel state %s" " owned by %s", channel_state_name(channel), channel->owner ? channel->owner->name : "none"); @@ -845,7 +843,7 @@ static enum watch_result funding_announce_cb(struct peer *peer, } subd_send_msg(channel->owner, - take(towire_channel_funding_announce_depth(peer))); + take(towire_channel_funding_announce_depth(channel))); return DELETE_WATCH; } @@ -907,7 +905,7 @@ static void handle_onchain_init_reply(struct channel *channel, const u8 *msg) onchaind_tell_fulfill(channel); } -static enum watch_result onchain_tx_watched(struct peer *peer, +static enum watch_result onchain_tx_watched(struct channel *channel, const struct bitcoin_tx *tx, unsigned int depth, void *unused) @@ -916,8 +914,8 @@ static enum watch_result onchain_tx_watched(struct peer *peer, struct bitcoin_txid txid; if (depth == 0) { - log_unusual(peer->log, "Chain reorganization!"); - peer_set_owner(peer, NULL); + log_unusual(channel->log, "Chain reorganization!"); + channel_set_owner(channel, NULL); /* FIXME! topology_rescan(peer->ld->topology, peer->funding_txid); @@ -928,15 +926,15 @@ static enum watch_result onchain_tx_watched(struct peer *peer, } bitcoin_txid(tx, &txid); - msg = towire_onchain_depth(peer, &txid, depth); - subd_send_msg(peer2channel(peer)->owner, take(msg)); + msg = towire_onchain_depth(channel, &txid, depth); + subd_send_msg(channel->owner, take(msg)); return KEEP_WATCHING; } -static void watch_tx_and_outputs(struct peer *peer, +static void watch_tx_and_outputs(struct channel *channel, const struct bitcoin_tx *tx); -static enum watch_result onchain_txo_watched(struct peer *peer, +static enum watch_result onchain_txo_watched(struct channel *channel, const struct bitcoin_tx *tx, size_t input_num, const struct block *block, @@ -944,10 +942,10 @@ static enum watch_result onchain_txo_watched(struct peer *peer, { u8 *msg; - watch_tx_and_outputs(peer, tx); + watch_tx_and_outputs(channel, tx); - msg = towire_onchain_spent(peer, tx, input_num, block->height); - subd_send_msg(peer2channel(peer)->owner, take(msg)); + msg = towire_onchain_spent(channel, tx, input_num, block->height); + subd_send_msg(channel->owner, take(msg)); /* We don't need to keep watching: If this output is double-spent * (reorg), we'll get a zero depth cb to onchain_tx_watched, and @@ -956,21 +954,21 @@ static enum watch_result onchain_txo_watched(struct peer *peer, } /* To avoid races, we watch the tx and all outputs. */ -static void watch_tx_and_outputs(struct peer *peer, +static void watch_tx_and_outputs(struct channel *channel, const struct bitcoin_tx *tx) { struct bitcoin_txid txid; struct txwatch *txw; - struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; bitcoin_txid(tx, &txid); /* Make txwatch a parent of txo watches, so we can unwatch together. */ - txw = watch_tx(channel->owner, peer->ld->topology, peer, tx, + txw = watch_tx(channel->owner, ld->topology, channel, tx, onchain_tx_watched, NULL); for (size_t i = 0; i < tal_count(tx->output); i++) - watch_txo(txw, peer->ld->topology, peer, &txid, i, + watch_txo(txw, ld->topology, channel, &txid, i, onchain_txo_watched, NULL); } @@ -984,7 +982,7 @@ static void handle_onchain_broadcast_tx(struct channel *channel, const u8 *msg) } /* We don't really care if it fails, we'll respond via watch. */ - broadcast_tx(channel->peer->ld->topology, channel2peer(channel), tx, NULL); + broadcast_tx(channel->peer->ld->topology, channel, tx, NULL); } static void handle_onchain_unwatch_tx(struct channel *channel, const u8 *msg) @@ -998,7 +996,7 @@ static void handle_onchain_unwatch_tx(struct channel *channel, const u8 *msg) } /* Frees the txo watches, too: see watch_tx_and_outputs() */ - txw = find_txwatch(channel->peer->ld->topology, &txid, channel2peer(channel)); + txw = find_txwatch(channel->peer->ld->topology, &txid, channel); if (!txw) log_unusual(channel->log, "Can't unwatch txid %s", type_to_string(ltmp, struct bitcoin_txid, &txid)); @@ -1219,7 +1217,7 @@ static bool tell_if_missing(const struct channel *channel, /* With a reorg, this can get called multiple times; each time we'll kill * onchaind (like any other owner), and restart */ -static enum watch_result funding_spent(struct peer *peer, +static enum watch_result funding_spent(struct channel *channel, const struct bitcoin_tx *tx, size_t input_num, const struct block *block, @@ -1230,8 +1228,7 @@ static enum watch_result funding_spent(struct peer *peer, s64 keyindex; struct pubkey ourkey; struct htlc_stub *stubs; - const tal_t *tmpctx = tal_tmpctx(peer); - struct channel *channel = peer2channel(peer); + const tal_t *tmpctx = tal_tmpctx(channel); struct lightningd *ld = channel->peer->ld; channel_fail_permanent(channel, "Funding transaction spent"); @@ -1333,14 +1330,14 @@ static enum watch_result funding_spent(struct peer *peer, subd_send_msg(channel->owner, take(msg)); } - watch_tx_and_outputs(peer, tx); + watch_tx_and_outputs(channel, tx); tal_free(tmpctx); /* We keep watching until peer finally deleted, for reorgs. */ return KEEP_WATCHING; } -static enum watch_result funding_lockin_cb(struct peer *peer, +static enum watch_result funding_lockin_cb(struct channel *channel, const struct bitcoin_tx *tx, unsigned int depth, void *unused) @@ -1349,18 +1346,18 @@ static enum watch_result funding_lockin_cb(struct peer *peer, const char *txidstr; struct txlocator *loc; bool channel_ready; - struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; bitcoin_txid(tx, &txid); - txidstr = type_to_string(peer, struct bitcoin_txid, &txid); - log_debug(peer->log, "Funding tx %s depth %u of %u", + txidstr = type_to_string(channel, struct bitcoin_txid, &txid); + log_debug(channel->log, "Funding tx %s depth %u of %u", txidstr, depth, channel->minimum_depth); tal_free(txidstr); if (depth < channel->minimum_depth) return KEEP_WATCHING; - loc = locate_tx(peer, peer->ld->topology, &txid); + loc = locate_tx(channel, ld->topology, &txid); /* If we restart, we could already have peer->scid from database */ if (!channel->scid) { @@ -1376,7 +1373,7 @@ static enum watch_result funding_lockin_cb(struct peer *peer, channel_ready = (channel->owner && channel->state == CHANNELD_AWAITING_LOCKIN); if (!channel_ready) { log_debug(channel->log, - "Funding tx confirmed, but peer state %s %s", + "Funding tx confirmed, but channel state %s %s", channel_state_name(channel), channel->owner ? channel->owner->name : "unowned"); } else { @@ -1400,9 +1397,9 @@ static enum watch_result funding_lockin_cb(struct peer *peer, * before. If we are at the right depth, call the callback * directly, otherwise schedule a callback */ if (depth >= ANNOUNCE_MIN_DEPTH) - funding_announce_cb(peer, tx, depth, NULL); + funding_announce_cb(channel, tx, depth, NULL); else - watch_txid(peer, peer->ld->topology, peer, &txid, + watch_txid(channel, ld->topology, channel, &txid, funding_announce_cb, NULL); return DELETE_WATCH; } @@ -1418,21 +1415,21 @@ static void opening_got_hsm_funding_sig(struct funding_channel *fc, u64 change_satoshi; struct json_result *response = new_json_result(fc->cmd); struct channel *channel = peer2channel(fc->peer); + struct lightningd *ld = channel->peer->ld; if (!fromwire_hsm_sign_funding_reply(fc, resp, NULL, &tx)) fatal("HSM gave bad sign_funding_reply %s", tal_hex(fc, resp)); /* Send it out and watch for confirms. */ - broadcast_tx(fc->peer->ld->topology, fc->peer, tx, funding_broadcast_failed); - watch_tx(fc->peer, fc->peer->ld->topology, fc->peer, tx, - funding_lockin_cb, NULL); + broadcast_tx(ld->topology, channel, tx, funding_broadcast_failed); + watch_tx(channel, ld->topology, channel, 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); + wallet_extract_owned_outputs(ld->wallet, tx, &change_satoshi); /* FIXME: Remove arg from cb? */ - watch_txo(fc->peer, fc->peer->ld->topology, fc->peer, + watch_txo(channel, ld->topology, channel, channel->funding_txid, channel->funding_outnum, funding_spent, NULL); @@ -1445,7 +1442,7 @@ static void opening_got_hsm_funding_sig(struct funding_channel *fc, channel->opening_cmd = NULL; /* Start normal channel daemon. */ - peer_start_channeld(fc->peer, cs, gossip_index, + peer_start_channeld(channel, cs, gossip_index, peer_fd, gossip_fd, NULL, false); peer_set_condition(fc->peer, OPENINGD, CHANNELD_AWAITING_LOCKIN); @@ -1561,12 +1558,6 @@ static void peer_got_shutdown(struct channel *channel, const u8 *msg) wallet_channel_save(ld->wallet, channel); } -static void peer_last_tx(struct peer *peer, struct bitcoin_tx *tx, - const secp256k1_ecdsa_signature *sig) -{ - channel_set_last_tx(peer2channel(peer), tx, sig); -} - /* Is this better than the last tx we were holding? This can happen * even without closingd misbehaving, if we have multiple, * interrupted, rounds of negotiation. */ @@ -1636,7 +1627,7 @@ static void peer_received_closing_signature(struct channel *channel, /* FIXME: Make sure signature is correct! */ if (better_closing_fee(ld, channel, tx)) { - peer_last_tx(channel2peer(channel), tx, &sig); + channel_set_last_tx(channel, tx, &sig); /* TODO(cdecker) Selectively save updated fields to DB */ wallet_channel_save(ld->wallet, channel); } @@ -1891,17 +1882,16 @@ u32 feerate_max(struct lightningd *ld) return get_feerate(ld->topology, FEERATE_IMMEDIATE) * 5; } -static bool peer_start_channeld(struct peer *peer, +static bool peer_start_channeld(struct channel *channel, const struct crypto_state *cs, u64 gossip_index, int peer_fd, int gossip_fd, const u8 *funding_signed, bool reconnected) { - const tal_t *tmpctx = tal_tmpctx(peer); + const tal_t *tmpctx = tal_tmpctx(channel); u8 *msg, *initmsg; int hsmfd; - const struct config *cfg = &peer->ld->config; struct added_htlc *htlcs; enum htlc_state *htlc_states; struct fulfilled_htlc *fulfilled_htlcs; @@ -1911,8 +1901,8 @@ static bool peer_start_channeld(struct peer *peer, struct short_channel_id funding_channel_id; const u8 *shutdown_scriptpubkey; u64 num_revocations; - struct channel *channel = peer2channel(peer); struct lightningd *ld = channel->peer->ld; + const struct config *cfg = &ld->config; /* Now we can consider balance set. */ if (!reconnected) { @@ -1926,15 +1916,15 @@ static bool peer_start_channeld(struct peer *peer, } else assert(channel->our_msatoshi); - msg = towire_hsm_client_hsmfd(tmpctx, &peer->id, HSM_CAP_SIGN_GOSSIP | HSM_CAP_ECDH); - if (!wire_sync_write(peer->ld->hsm_fd, take(msg))) + msg = towire_hsm_client_hsmfd(tmpctx, &channel->peer->id, HSM_CAP_SIGN_GOSSIP | HSM_CAP_ECDH); + if (!wire_sync_write(ld->hsm_fd, take(msg))) fatal("Could not write to HSM: %s", strerror(errno)); - msg = hsm_sync_read(tmpctx, peer->ld); + msg = hsm_sync_read(tmpctx, ld); if (!fromwire_hsm_client_hsmfd_reply(msg, NULL)) fatal("Bad reply from HSM: %s", tal_hex(tmpctx, msg)); - hsmfd = fdpass_recv(peer->ld->hsm_fd); + hsmfd = fdpass_recv(ld->hsm_fd); if (hsmfd < 0) fatal("Could not read fd from HSM: %s", strerror(errno)); @@ -1947,7 +1937,7 @@ static bool peer_start_channeld(struct peer *peer, take(&hsmfd), NULL)); if (!channel->owner) { - log_unusual(peer->log, "Could not subdaemon channel: %s", + log_unusual(channel->log, "Could not subdaemon channel: %s", strerror(errno)); channel_fail_transient(channel, "Failed to subdaemon channel"); tal_free(tmpctx); @@ -1959,15 +1949,15 @@ static bool peer_start_channeld(struct peer *peer, if (channel->scid) { funding_channel_id = *channel->scid; - log_debug(peer->log, "Already have funding locked in"); + log_debug(channel->log, "Already have funding locked in"); } else { - log_debug(peer->log, "Waiting for funding confirmations"); + log_debug(channel->log, "Waiting for funding confirmations"); memset(&funding_channel_id, 0, sizeof(funding_channel_id)); } if (channel->local_shutdown_idx != -1) { shutdown_scriptpubkey - = p2wpkh_for_keyidx(tmpctx, peer->ld, + = p2wpkh_for_keyidx(tmpctx, ld, channel->local_shutdown_idx); } else shutdown_scriptpubkey = NULL; @@ -1975,20 +1965,19 @@ static bool peer_start_channeld(struct peer *peer, num_revocations = revocations_received(&channel->their_shachain.chain); /* Warn once. */ - if (peer->ld->config.ignore_fee_limits) - log_debug(peer->log, "Ignoring fee limits!"); + if (ld->config.ignore_fee_limits) + log_debug(channel->log, "Ignoring fee limits!"); initmsg = towire_channel_init(tmpctx, - &get_chainparams(peer->ld) - ->genesis_blockhash, + &get_chainparams(ld)->genesis_blockhash, channel->funding_txid, channel->funding_outnum, channel->funding_satoshi, &channel->our_config, &channel->channel_info->their_config, channel->channel_info->feerate_per_kw, - feerate_min(peer->ld), - feerate_max(peer->ld), + feerate_min(ld), + feerate_max(ld), channel->last_sig, cs, gossip_index, &channel->channel_info->remote_fundingkey, @@ -2003,8 +1992,8 @@ static bool peer_start_channeld(struct peer *peer, cfg->fee_per_satoshi, *channel->our_msatoshi, &channel->seed, - &peer->ld->id, - &peer->id, + &ld->id, + &channel->peer->id, time_to_msec(cfg->commit_time), cfg->cltv_expiry_delta, channel->last_was_revoke, @@ -2032,11 +2021,9 @@ static bool peer_start_channeld(struct peer *peer, return true; } -static bool peer_commit_initial(struct peer *peer) +static void channel_commit_initial(struct channel *channel) { - struct channel *channel = peer2channel(peer); channel->next_index[LOCAL] = channel->next_index[REMOTE] = 1; - return true; } static void opening_funder_finished(struct subd *opening, const u8 *resp, @@ -2092,7 +2079,7 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp, channel_info->old_remote_per_commit = channel_info->remote_per_commit; /* Now, keep the initial commit as our last-tx-to-broadcast. */ - peer_last_tx(fc->peer, remote_commit, &remote_commit_sig); + channel_set_last_tx(channel, remote_commit, &remote_commit_sig); /* Generate the funding tx. */ if (fc->change @@ -2139,13 +2126,10 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp, return; } - if (!peer_commit_initial(fc->peer)) { - channel_internal_error(channel, "Initial peer to db failed"); - return; - } + channel_commit_initial(channel); /* Get HSM to sign the funding tx. */ - log_debug(fc->peer->log, "Getting HSM to sign funding tx"); + log_debug(channel->log, "Getting HSM to sign funding tx"); msg = towire_hsm_sign_funding(tmpctx, channel->funding_satoshi, fc->change, fc->change_keyindex, @@ -2167,7 +2151,7 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp, static void opening_fundee_finished(struct subd *opening, const u8 *reply, const int *fds, - struct peer *peer) + struct channel *channel) { u8 *funding_signed; struct channel_info *channel_info; @@ -2175,14 +2159,14 @@ static void opening_fundee_finished(struct subd *opening, u64 gossip_index; secp256k1_ecdsa_signature remote_commit_sig; struct bitcoin_tx *remote_commit; - const tal_t *tmpctx = tal_tmpctx(peer); - struct channel *channel = peer2channel(peer); + const tal_t *tmpctx = tal_tmpctx(channel); + struct lightningd *ld = channel->peer->ld; - log_debug(peer->log, "Got opening_fundee_finish_response"); + log_debug(channel->log, "Got opening_fundee_finish_response"); assert(tal_count(fds) == 2); /* At this point, we care about peer */ - channel->channel_info = channel_info = tal(peer, struct channel_info); + channel->channel_info = channel_info = tal(channel, struct channel_info); /* This is a new channel_info->their_config, set its ID to 0 */ channel->channel_info->their_config.id = 0; @@ -2220,30 +2204,27 @@ static void opening_fundee_finished(struct subd *opening, channel_info->old_remote_per_commit = channel_info->remote_per_commit; /* Now, keep the initial commit as our last-tx-to-broadcast. */ - peer_last_tx(peer, remote_commit, &remote_commit_sig); + channel_set_last_tx(channel, remote_commit, &remote_commit_sig); - if (!peer_commit_initial(peer)) { - tal_free(tmpctx); - return; - } + channel_commit_initial(channel); - log_debug(peer->log, "Watching funding tx %s", + log_debug(channel->log, "Watching funding tx %s", type_to_string(reply, struct bitcoin_txid, channel->funding_txid)); - watch_txid(peer, peer->ld->topology, peer, channel->funding_txid, + watch_txid(channel, ld->topology, channel, channel->funding_txid, funding_lockin_cb, NULL); /* FIXME: Remove arg from cb? */ - watch_txo(peer, peer->ld->topology, peer, channel->funding_txid, + watch_txo(channel, ld->topology, channel, channel->funding_txid, channel->funding_outnum, funding_spent, NULL); /* Unowned (will free openingd). */ - peer_set_owner(peer, NULL); + channel_set_owner(channel, NULL); /* On to normal operation! */ - peer_start_channeld(peer, &cs, gossip_index, + peer_start_channeld(channel, &cs, gossip_index, fds[0], fds[1], funding_signed, false); - peer_set_condition(peer, OPENINGD, CHANNELD_AWAITING_LOCKIN); + channel_set_state(channel, OPENINGD, CHANNELD_AWAITING_LOCKIN); tal_free(tmpctx); } @@ -2363,8 +2344,8 @@ static void peer_accept_channel(struct lightningd *ld, * 5, open_msg); - subd_req(peer, channel->owner, take(msg), -1, 2, - opening_fundee_finished, peer); + subd_req(channel, channel->owner, take(msg), -1, 2, + opening_fundee_finished, channel); } static void peer_offer_channel(struct lightningd *ld, @@ -2639,6 +2620,7 @@ static void activate_peer(struct peer *peer) { u8 *msg; struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; /* Pass gossipd any addrhints we currently have */ msg = towire_gossipctl_peer_addrhint(peer, &peer->id, &peer->addr); @@ -2652,10 +2634,10 @@ static void activate_peer(struct peer *peer) } /* This may be unnecessary, but it's harmless. */ - watch_txid(peer, peer->ld->topology, peer, channel->funding_txid, + watch_txid(channel, ld->topology, channel, channel->funding_txid, funding_lockin_cb, NULL); - watch_txo(peer, peer->ld->topology, peer, + watch_txo(channel, ld->topology, channel, channel->funding_txid, channel->funding_outnum, funding_spent, NULL); diff --git a/lightningd/test/run-find_my_path.c b/lightningd/test/run-find_my_path.c index 409f0d2cb..a00fb20f1 100644 --- a/lightningd/test/run-find_my_path.c +++ b/lightningd/test/run-find_my_path.c @@ -70,7 +70,7 @@ void setup_jsonrpc(struct lightningd *ld UNNEEDED, const char *rpc_filename UNNE /* Generated stub for setup_topology */ void setup_topology(struct chain_topology *topology UNNEEDED, struct timers *timers UNNEEDED, - struct timerel poll_time UNNEEDED, u32 first_peer_block UNNEEDED) + struct timerel poll_time UNNEEDED, u32 first_channel_block UNNEEDED) { fprintf(stderr, "setup_topology called!\n"); abort(); } /* Generated stub for subd_shutdown */ void subd_shutdown(struct subd *subd UNNEEDED, unsigned int seconds UNNEEDED) diff --git a/lightningd/watch.c b/lightningd/watch.c index a843d9cae..4d5303e19 100644 --- a/lightningd/watch.c +++ b/lightningd/watch.c @@ -88,9 +88,9 @@ static void destroy_txwatch(struct txwatch *w) struct txwatch *watch_txid_(const tal_t *ctx, struct chain_topology *topo, - struct peer *peer, + struct channel *channel, const struct bitcoin_txid *txid, - enum watch_result (*cb)(struct peer *peer, + enum watch_result (*cb)(struct channel *channel, const struct bitcoin_tx *, unsigned int depth, void *arg), @@ -102,7 +102,7 @@ struct txwatch *watch_txid_(const tal_t *ctx, w->topo = topo; w->depth = 0; w->txid = *txid; - w->peer = peer; + w->channel = channel; w->cb = cb; w->cbdata = cb_arg; @@ -114,17 +114,17 @@ struct txwatch *watch_txid_(const tal_t *ctx, struct txwatch *find_txwatch(struct chain_topology *topo, const struct bitcoin_txid *txid, - const struct peer *peer) + const struct channel *channel) { struct txwatch_hash_iter i; struct txwatch *w; - /* We could have more than one peer watching same txid, though we + /* We could have more than one channel watching same txid, though we * don't for onchaind. */ for (w = txwatch_hash_getfirst(&topo->txwatches, txid, &i); w; w = txwatch_hash_getnext(&topo->txwatches, txid, &i)) { - if (w->peer == peer) + if (w->channel == channel) break; } return w; @@ -138,9 +138,9 @@ bool watching_txid(const struct chain_topology *topo, struct txwatch *watch_tx_(const tal_t *ctx, struct chain_topology *topo, - struct peer *peer, + struct channel *channel, const struct bitcoin_tx *tx, - enum watch_result (*cb)(struct peer *peer, + enum watch_result (*cb)(struct channel *channel, const struct bitcoin_tx *, unsigned int depth, void *arg), @@ -149,15 +149,15 @@ struct txwatch *watch_tx_(const tal_t *ctx, struct bitcoin_txid txid; bitcoin_txid(tx, &txid); - return watch_txid(ctx, topo, peer, &txid, cb, cb_arg); + return watch_txid(ctx, topo, channel, &txid, cb, cb_arg); } struct txowatch *watch_txo_(const tal_t *ctx, struct chain_topology *topo, - struct peer *peer, + struct channel *channel, const struct bitcoin_txid *txid, unsigned int output, - enum watch_result (*cb)(struct peer *peer, + enum watch_result (*cb)(struct channel *channel, const struct bitcoin_tx *tx, size_t input_num, const struct block *block, @@ -169,7 +169,7 @@ struct txowatch *watch_txo_(const tal_t *ctx, w->topo = topo; w->out.txid = *txid; w->out.index = output; - w->peer = peer; + w->channel = channel; w->cb = cb; w->cbdata = cbdata; @@ -189,12 +189,12 @@ static bool txw_fire(struct chain_topology *topo, if (depth == txw->depth) return false; - log_debug(txw->peer->log, + log_debug(txw->channel->log, "Got depth change %u->%u for %s", txw->depth, depth, type_to_string(ltmp, struct bitcoin_txid, &txw->txid)); txw->depth = depth; - r = txw->cb(txw->peer, tx, txw->depth, txw->cbdata); + r = txw->cb(txw->channel, tx, txw->depth, txw->cbdata); switch (r) { case DELETE_WATCH: tal_free(txw); @@ -229,13 +229,13 @@ void txowatch_fire(struct chain_topology *topo, enum watch_result r; bitcoin_txid(tx, &txid); - log_debug(txow->peer->log, + log_debug(txow->channel->log, "Got UTXO spend for %s:%u: %s", type_to_string(ltmp, struct bitcoin_txid, &txow->out.txid), txow->out.index, type_to_string(ltmp, struct bitcoin_txid, &txid)); - r = txow->cb(txow->peer, tx, input_num, block, txow->cbdata); + r = txow->cb(txow->channel, tx, input_num, block, txow->cbdata); switch (r) { case DELETE_WATCH: tal_free(txow); diff --git a/lightningd/watch.h b/lightningd/watch.h index 06f8d76e6..80a1c790b 100644 --- a/lightningd/watch.h +++ b/lightningd/watch.h @@ -25,14 +25,14 @@ struct txwatch_output { struct txowatch { struct chain_topology *topo; - /* Peer who owns us. */ - struct peer *peer; + /* Channel who owns us. */ + struct channel *channel; /* Output to watch. */ struct txwatch_output out; /* A new tx. */ - enum watch_result (*cb)(struct peer *peer, + enum watch_result (*cb)(struct channel *channel, const struct bitcoin_tx *tx, size_t input_num, const struct block *block, @@ -51,15 +51,15 @@ HTABLE_DEFINE_TYPE(struct txowatch, txowatch_keyof, txo_hash, txowatch_eq, struct txwatch { struct chain_topology *topo; - /* Peer who owns us. */ - struct peer *peer; + /* Channel who owns us. */ + struct channel *channel; /* Transaction to watch. */ struct bitcoin_txid txid; unsigned int depth; /* A new depth (0 if kicked out, otherwise 1 = tip, etc.) */ - enum watch_result (*cb)(struct peer *peer, + enum watch_result (*cb)(struct channel *channel, const struct bitcoin_tx *tx, unsigned int depth, void *cbdata); @@ -76,59 +76,59 @@ HTABLE_DEFINE_TYPE(struct txwatch, txwatch_keyof, txid_hash, txwatch_eq, struct txwatch *watch_txid_(const tal_t *ctx, struct chain_topology *topo, - struct peer *peer, + struct channel *channel, const struct bitcoin_txid *txid, - enum watch_result (*cb)(struct peer *peer, + enum watch_result (*cb)(struct channel *channel, const struct bitcoin_tx *, unsigned int depth, void *), void *cbdata); -#define watch_txid(ctx, topo, peer_, txid, cb, cbdata) \ - watch_txid_((ctx), (topo), (peer_), (txid), \ +#define watch_txid(ctx, topo, channel_, txid, cb, cbdata) \ + watch_txid_((ctx), (topo), (channel_), (txid), \ typesafe_cb_preargs(enum watch_result, void *, \ (cb), (cbdata), \ - struct peer *, \ + struct channel *, \ const struct bitcoin_tx *, \ unsigned int depth), \ (cbdata)) struct txwatch *watch_tx_(const tal_t *ctx, struct chain_topology *topo, - struct peer *peer, + struct channel *channel, const struct bitcoin_tx *tx, - enum watch_result (*cb)(struct peer *peer, + enum watch_result (*cb)(struct channel *channel, const struct bitcoin_tx *, unsigned int depth, void *), void *cbdata); -#define watch_tx(ctx, topo, peer_, tx, cb, cbdata) \ - watch_tx_((ctx), (topo), (peer_), (tx), \ +#define watch_tx(ctx, topo, channel_, tx, cb, cbdata) \ + watch_tx_((ctx), (topo), (channel_), (tx), \ typesafe_cb_preargs(enum watch_result, void *, \ (cb), (cbdata), \ - struct peer *, \ + struct channel *, \ const struct bitcoin_tx *, \ unsigned int depth), \ (cbdata)) struct txowatch *watch_txo_(const tal_t *ctx, struct chain_topology *topo, - struct peer *peer, + struct channel *channel, const struct bitcoin_txid *txid, unsigned int output, - enum watch_result (*cb)(struct peer *peer, + enum watch_result (*cb)(struct channel *channel, const struct bitcoin_tx *tx, size_t input_num, const struct block *block, void *), void *cbdata); -#define watch_txo(ctx, topo, peer_, txid, outnum, cb, cbdata) \ - watch_txo_((ctx), (topo), (peer_), (txid), (outnum), \ +#define watch_txo(ctx, topo, channel_, txid, outnum, cb, cbdata) \ + watch_txo_((ctx), (topo), (channel_), (txid), (outnum), \ typesafe_cb_preargs(enum watch_result, void *, \ (cb), (cbdata), \ - struct peer *, \ + struct channel *, \ const struct bitcoin_tx *, \ size_t, \ const struct block *block), \ @@ -136,7 +136,7 @@ struct txowatch *watch_txo_(const tal_t *ctx, struct txwatch *find_txwatch(struct chain_topology *topo, const struct bitcoin_txid *txid, - const struct peer *peer); + const struct channel *channel); void txwatch_fire(struct chain_topology *topo, const struct bitcoin_tx *tx, diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 92d377bbc..d0f2c0def 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -42,8 +42,8 @@ void bitcoind_gettxout(struct bitcoind *bitcoind UNNEEDED, { fprintf(stderr, "bitcoind_gettxout called!\n"); abort(); } /* Generated stub for broadcast_tx */ void broadcast_tx(struct chain_topology *topo UNNEEDED, - struct peer *peer UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, - void (*failed)(struct peer *peer UNNEEDED, + struct channel *channel UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, + void (*failed)(struct channel *channel UNNEEDED, int exitstatus UNNEEDED, const char *err)) { fprintf(stderr, "broadcast_tx called!\n"); abort(); } @@ -86,7 +86,7 @@ struct htlc_out *find_htlc_out_by_ripemd(const struct channel *channel UNNEEDED, /* Generated stub for find_txwatch */ struct txwatch *find_txwatch(struct chain_topology *topo UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, - const struct peer *peer UNNEEDED) + const struct channel *channel UNNEEDED) { fprintf(stderr, "find_txwatch called!\n"); abort(); } /* Generated stub for fromwire_channel_got_funding_locked */ bool fromwire_channel_got_funding_locked(const void *p UNNEEDED, size_t *plen UNNEEDED, struct pubkey *next_per_commit_point UNNEEDED) @@ -481,9 +481,9 @@ void update_per_commit_point(struct channel *channel UNNEEDED, /* Generated stub for watch_tx_ */ struct txwatch *watch_tx_(const tal_t *ctx UNNEEDED, struct chain_topology *topo UNNEEDED, - struct peer *peer UNNEEDED, + struct channel *channel UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, - enum watch_result (*cb)(struct peer *peer UNNEEDED, + enum watch_result (*cb)(struct channel *channel UNNEEDED, const struct bitcoin_tx * UNNEEDED, unsigned int depth UNNEEDED, void *) UNNEEDED, @@ -492,9 +492,9 @@ struct txwatch *watch_tx_(const tal_t *ctx UNNEEDED, /* Generated stub for watch_txid_ */ struct txwatch *watch_txid_(const tal_t *ctx UNNEEDED, struct chain_topology *topo UNNEEDED, - struct peer *peer UNNEEDED, + struct channel *channel UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, - enum watch_result (*cb)(struct peer *peer UNNEEDED, + enum watch_result (*cb)(struct channel *channel UNNEEDED, const struct bitcoin_tx * UNNEEDED, unsigned int depth UNNEEDED, void *) UNNEEDED, @@ -503,10 +503,10 @@ struct txwatch *watch_txid_(const tal_t *ctx UNNEEDED, /* Generated stub for watch_txo_ */ struct txowatch *watch_txo_(const tal_t *ctx UNNEEDED, struct chain_topology *topo UNNEEDED, - struct peer *peer UNNEEDED, + struct channel *channel UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, unsigned int output UNNEEDED, - enum watch_result (*cb)(struct peer *peer UNNEEDED, + enum watch_result (*cb)(struct channel *channel UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, size_t input_num UNNEEDED, const struct block *block UNNEEDED,