diff --git a/lightningd/channel.c b/lightningd/channel.c index b1024933a..abdf8b388 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -15,7 +15,7 @@ void channel_set_owner(struct channel *channel, struct subd *owner) channel->owner = owner; if (old_owner) - subd_release_peer(old_owner, channel2peer(channel)); + subd_release_channel(old_owner, channel); } static void destroy_channel(struct channel *channel) @@ -112,6 +112,25 @@ struct channel *peer_active_channel(struct peer *peer) return NULL; } +void channel_set_state(struct channel *channel, + enum peer_state old_state, + enum peer_state state) +{ + log_info(channel->log, "State changed from %s to %s", + channel_state_name(channel), peer_state_name(state)); + if (channel->state != old_state) + fatal("channel state %s should be %s", + channel_state_name(channel), peer_state_name(old_state)); + + channel->state = state; + + /* We only persist channels/peers that have reached the opening state */ + if (channel_persists(channel)) { + /* TODO(cdecker) Selectively save updated fields to DB */ + wallet_channel_save(channel->peer->ld->wallet, channel); + } +} + struct channel *peer2channel(const struct peer *peer) { return list_top(&peer->channels, struct channel, list); diff --git a/lightningd/channel.h b/lightningd/channel.h index 9d78e37e6..c03f563bf 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -105,6 +105,10 @@ void channel_fail_permanent(struct channel *channel, const char *fmt, ...); /* Permanent error, but due to internal problems, not peer. */ void channel_internal_error(struct channel *channel, const char *fmt, ...); +void channel_set_state(struct channel *channel, + enum peer_state old_state, + enum peer_state state); + /* FIXME: Temporary mapping from peer to channel, while we only have one. */ struct channel *peer2channel(const struct peer *peer); struct peer *channel2peer(const struct channel *channel); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 2dabb4e1f..951f13aef 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -75,7 +75,7 @@ static bool peer_start_channeld(struct peer *peer, int peer_fd, int gossip_fd, const u8 *funding_signed, bool reconnected); -static void peer_start_closingd(struct peer *peer, +static void peer_start_closingd(struct channel *channel, struct crypto_state *cs, u64 gossip_index, int peer_fd, int gossip_fd, @@ -146,6 +146,7 @@ struct peer *new_peer(struct lightningd *ld, u64 dbid, /* Max 128k per peer. */ peer->log_book = new_log_book(peer, 128*1024, get_log_level(ld->log_book)); + /* FIXME: Use minimal unique pubkey prefix for logs! */ idname = type_to_string(peer, struct pubkey, id); peer->log = new_log(peer, peer->log_book, "peer %s:", idname); tal_free(idname); @@ -221,20 +222,7 @@ void peer_set_condition(struct peer *peer, enum peer_state old_state, { struct channel *channel = peer2channel(peer); - log_info(peer->log, "State changed from %s to %s", - channel_state_name(channel), peer_state_name(state)); - if (channel->state != old_state) - fatal("peer state %s should be %s", - channel_state_name(channel), peer_state_name(old_state)); - - channel->state = state; - - /* We only persist channels/peers that have reached the opening state */ - if (peer_persists(peer)) { - assert(channel != NULL); - /* TODO(cdecker) Selectively save updated fields to DB */ - wallet_channel_save(peer->ld->wallet, channel); - } + channel_set_state(channel, old_state, state); } static void destroy_connect(struct connect *c) @@ -429,7 +417,7 @@ void peer_connected(struct lightningd *ld, const u8 *msg, peer_set_owner(peer, NULL); peer->addr = addr; - peer_start_closingd(peer, &cs, gossip_index, + peer_start_closingd(channel, &cs, gossip_index, peer_fd, gossip_fd, true); goto connected; @@ -889,17 +877,17 @@ static enum watch_result funding_announce_cb(struct peer *peer, /* We dump all the known preimages when onchaind starts up. */ -static void onchaind_tell_fulfill(struct peer *peer) +static void onchaind_tell_fulfill(struct channel *channel) { struct htlc_in_map_iter ini; struct htlc_in *hin; u8 *msg; - struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; - for (hin = htlc_in_map_first(&peer->ld->htlcs_in, &ini); + for (hin = htlc_in_map_first(&ld->htlcs_in, &ini); hin; - hin = htlc_in_map_next(&peer->ld->htlcs_in, &ini)) { - if (hin->key.peer != peer) + hin = htlc_in_map_next(&ld->htlcs_in, &ini)) { + if (hin->key.peer != channel2peer(channel)) continue; /* BOLT #5: @@ -918,15 +906,14 @@ static void onchaind_tell_fulfill(struct peer *peer) if (!hin->preimage) continue; - msg = towire_onchain_known_preimage(peer, hin->preimage); + msg = towire_onchain_known_preimage(channel, hin->preimage); subd_send_msg(channel->owner, take(msg)); } } -static void handle_onchain_init_reply(struct peer *peer, const u8 *msg) +static void handle_onchain_init_reply(struct channel *channel, const u8 *msg) { u8 state; - struct channel *channel = peer2channel(peer); if (!fromwire_onchain_init_reply(msg, NULL, &state)) { channel_internal_error(channel, "Invalid onchain_init_reply"); @@ -940,10 +927,10 @@ static void handle_onchain_init_reply(struct peer *peer, const u8 *msg) return; } - peer_set_condition(peer, FUNDING_SPEND_SEEN, state); + channel_set_state(channel, FUNDING_SPEND_SEEN, state); /* Tell it about any preimages we know. */ - onchaind_tell_fulfill(peer); + onchaind_tell_fulfill(channel); } static enum watch_result onchain_tx_watched(struct peer *peer, @@ -1013,10 +1000,9 @@ static void watch_tx_and_outputs(struct peer *peer, onchain_txo_watched, NULL); } -static void handle_onchain_broadcast_tx(struct peer *peer, const u8 *msg) +static void handle_onchain_broadcast_tx(struct channel *channel, const u8 *msg) { struct bitcoin_tx *tx; - struct channel *channel = peer2channel(peer); if (!fromwire_onchain_broadcast_tx(msg, msg, NULL, &tx)) { channel_internal_error(channel, "Invalid onchain_broadcast_tx"); @@ -1024,14 +1010,13 @@ static void handle_onchain_broadcast_tx(struct peer *peer, const u8 *msg) } /* We don't really care if it fails, we'll respond via watch. */ - broadcast_tx(peer->ld->topology, peer, tx, NULL); + broadcast_tx(channel->peer->ld->topology, channel2peer(channel), tx, NULL); } -static void handle_onchain_unwatch_tx(struct peer *peer, const u8 *msg) +static void handle_onchain_unwatch_tx(struct channel *channel, const u8 *msg) { struct bitcoin_txid txid; struct txwatch *txw; - struct channel *channel = peer2channel(peer); if (!fromwire_onchain_unwatch_tx(msg, NULL, &txid)) { channel_internal_error(channel, "Invalid onchain_unwatch_tx"); @@ -1039,30 +1024,28 @@ static void handle_onchain_unwatch_tx(struct peer *peer, const u8 *msg) } /* Frees the txo watches, too: see watch_tx_and_outputs() */ - txw = find_txwatch(peer->ld->topology, &txid, peer); + txw = find_txwatch(channel->peer->ld->topology, &txid, channel2peer(channel)); if (!txw) - log_unusual(peer->log, "Can't unwatch txid %s", + log_unusual(channel->log, "Can't unwatch txid %s", type_to_string(ltmp, struct bitcoin_txid, &txid)); tal_free(txw); } -static void handle_extracted_preimage(struct peer *peer, const u8 *msg) +static void handle_extracted_preimage(struct channel *channel, const u8 *msg) { struct preimage preimage; - struct channel *channel = peer2channel(peer); if (!fromwire_onchain_extracted_preimage(msg, NULL, &preimage)) { channel_internal_error(channel, "Invalid extracted_preimage"); return; } - onchain_fulfilled_htlc(peer, &preimage); + onchain_fulfilled_htlc(channel, &preimage); } -static void handle_missing_htlc_output(struct peer *peer, const u8 *msg) +static void handle_missing_htlc_output(struct channel *channel, const u8 *msg) { struct htlc_stub htlc; - struct channel *channel = peer2channel(peer); if (!fromwire_onchain_missing_htlc_output(msg, NULL, &htlc)) { channel_internal_error(channel, "Invalid missing_htlc_output"); @@ -1077,13 +1060,12 @@ static void handle_missing_htlc_output(struct peer *peer, const u8 *msg) * reasonable depth, and MAY fail it sooner if no valid commitment * transaction contains an output corresponding to the HTLC. */ - onchain_failed_our_htlc(peer, &htlc, "missing in commitment tx"); + onchain_failed_our_htlc(channel, &htlc, "missing in commitment tx"); } -static void handle_onchain_htlc_timeout(struct peer *peer, const u8 *msg) +static void handle_onchain_htlc_timeout(struct channel *channel, const u8 *msg) { struct htlc_stub htlc; - struct channel *channel = peer2channel(peer); if (!fromwire_onchain_htlc_timeout(msg, NULL, &htlc)) { channel_internal_error(channel, "Invalid onchain_htlc_timeout"); @@ -1097,11 +1079,11 @@ static void handle_onchain_htlc_timeout(struct peer *peer, const u8 *msg) * HTLC (if any) once the resolving transaction has reached reasonable * depth. */ - onchain_failed_our_htlc(peer, &htlc, "timed out"); + onchain_failed_our_htlc(channel, &htlc, "timed out"); } -/* If peer is NULL, free them all (for shutdown) */ -void free_htlcs(struct lightningd *ld, const struct peer *peer) +/* If channel is NULL, free them all (for shutdown) */ +void free_htlcs(struct lightningd *ld, const struct channel *channel) { struct htlc_out_map_iter outi; struct htlc_out *hout; @@ -1116,7 +1098,7 @@ void free_htlcs(struct lightningd *ld, const struct peer *peer) for (hout = htlc_out_map_first(&ld->htlcs_out, &outi); hout; hout = htlc_out_map_next(&ld->htlcs_out, &outi)) { - if (peer && hout->key.peer != peer) + if (channel && hout->key.peer != channel2peer(channel)) continue; tal_free(hout); deleted = true; @@ -1125,7 +1107,7 @@ void free_htlcs(struct lightningd *ld, const struct peer *peer) for (hin = htlc_in_map_first(&ld->htlcs_in, &ini); hin; hin = htlc_in_map_next(&ld->htlcs_in, &ini)) { - if (peer && hin->key.peer != peer) + if (channel && hin->key.peer != channel2peer(channel)) continue; tal_free(hin); deleted = true; @@ -1134,21 +1116,21 @@ void free_htlcs(struct lightningd *ld, const struct peer *peer) } while (deleted); } -static void handle_irrevocably_resolved(struct peer *peer, const u8 *msg) +static void handle_irrevocably_resolved(struct channel *channel, const u8 *msg) { /* FIXME: Implement check_htlcs to ensure no dangling hout->in ptrs! */ - free_htlcs(peer->ld, peer); + free_htlcs(channel->peer->ld, channel); - log_info(peer->log, "onchaind complete, forgetting peer"); + log_info(channel->log, "onchaind complete, forgetting peer"); /* This will also free onchaind. */ - free_channel(peer2channel(peer), "onchaind complete, forgetting peer"); + free_channel(channel, "onchaind complete, forgetting peer"); } /** * onchain_add_utxo -- onchaind is telling us about an UTXO we own */ -static void onchain_add_utxo(struct peer *peer, const u8 *msg) +static void onchain_add_utxo(struct channel *channel, const u8 *msg) { struct utxo *u = tal(msg, struct utxo); u->close_info = tal(u, struct unilateral_close_info); @@ -1156,8 +1138,8 @@ static void onchain_add_utxo(struct peer *peer, const u8 *msg) u->is_p2sh = true; u->keyindex = 0; u->status = output_state_available; - u->close_info->channel_id = peer2channel(peer)->dbid; - u->close_info->peer_id = peer->id; + u->close_info->channel_id = channel->dbid; + u->close_info->peer_id = channel->peer->id; if (!fromwire_onchain_add_utxo(msg, NULL, &u->txid, &u->outnum, &u->close_info->commitment_point, @@ -1166,7 +1148,7 @@ static void onchain_add_utxo(struct peer *peer, const u8 *msg) } - wallet_add_utxo(peer->ld->wallet, u, p2wpkh); + wallet_add_utxo(channel->peer->ld->wallet, u, p2wpkh); } static unsigned int onchain_msg(struct subd *sd, const u8 *msg, const int *fds) @@ -1175,35 +1157,35 @@ static unsigned int onchain_msg(struct subd *sd, const u8 *msg, const int *fds) switch (t) { case WIRE_ONCHAIN_INIT_REPLY: - handle_onchain_init_reply(sd->peer, msg); + handle_onchain_init_reply(sd->channel, msg); break; case WIRE_ONCHAIN_BROADCAST_TX: - handle_onchain_broadcast_tx(sd->peer, msg); + handle_onchain_broadcast_tx(sd->channel, msg); break; case WIRE_ONCHAIN_UNWATCH_TX: - handle_onchain_unwatch_tx(sd->peer, msg); + handle_onchain_unwatch_tx(sd->channel, msg); break; case WIRE_ONCHAIN_EXTRACTED_PREIMAGE: - handle_extracted_preimage(sd->peer, msg); + handle_extracted_preimage(sd->channel, msg); break; case WIRE_ONCHAIN_MISSING_HTLC_OUTPUT: - handle_missing_htlc_output(sd->peer, msg); + handle_missing_htlc_output(sd->channel, msg); break; case WIRE_ONCHAIN_HTLC_TIMEOUT: - handle_onchain_htlc_timeout(sd->peer, msg); + handle_onchain_htlc_timeout(sd->channel, msg); break; case WIRE_ONCHAIN_ALL_IRREVOCABLY_RESOLVED: - handle_irrevocably_resolved(sd->peer, msg); + handle_irrevocably_resolved(sd->channel, msg); break; case WIRE_ONCHAIN_ADD_UTXO: - onchain_add_utxo(sd->peer, msg); + onchain_add_utxo(sd->channel, msg); break; /* We send these, not receive them */ @@ -1229,7 +1211,8 @@ static u8 *p2wpkh_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx } /* If we want to know if this HTLC is missing, return depth. */ -static bool tell_if_missing(const struct peer *peer, struct htlc_stub *stub, +static bool tell_if_missing(const struct channel *channel, + struct htlc_stub *stub, bool *tell_immediate) { struct htlc_out *hout; @@ -1238,7 +1221,7 @@ static bool tell_if_missing(const struct peer *peer, struct htlc_stub *stub, *tell_immediate = false; /* Is it a current HTLC? */ - hout = find_htlc_out_by_ripemd(peer, &stub->ripemd); + hout = find_htlc_out_by_ripemd(channel, &stub->ripemd); if (!hout) return false; @@ -1254,7 +1237,8 @@ static bool tell_if_missing(const struct peer *peer, struct htlc_stub *stub, && hout->hstate < SENT_REMOVE_REVOCATION) *tell_immediate = true; - log_debug(peer->log, "We want to know if htlc %"PRIu64" is missing (%s)", + log_debug(channel->log, + "We want to know if htlc %"PRIu64" is missing (%s)", hout->key.id, *tell_immediate ? "immediate" : "later"); return true; } @@ -1274,28 +1258,30 @@ static enum watch_result funding_spent(struct peer *peer, struct htlc_stub *stubs; const tal_t *tmpctx = tal_tmpctx(peer); struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; channel_fail_permanent(channel, "Funding transaction spent"); /* We could come from almost any state. */ - peer_set_condition(peer, channel->state, FUNDING_SPEND_SEEN); + channel_set_state(channel, channel->state, FUNDING_SPEND_SEEN); - peer_set_owner(peer, new_peer_subd(peer->ld, - "lightning_onchaind", peer, - onchain_wire_type_name, - onchain_msg, - NULL)); + channel_set_owner(channel, new_channel_subd(ld, + "lightning_onchaind", + channel, + onchain_wire_type_name, + onchain_msg, + NULL)); if (!channel->owner) { - log_broken(peer->log, "Could not subdaemon onchain: %s", + log_broken(channel->log, "Could not subdaemon onchain: %s", strerror(errno)); tal_free(tmpctx); return KEEP_WATCHING; } - stubs = wallet_htlc_stubs(tmpctx, peer->ld->wallet, channel); + stubs = wallet_htlc_stubs(tmpctx, ld->wallet, channel); if (!stubs) { - log_broken(peer->log, "Could not load htlc_stubs"); + log_broken(channel->log, "Could not load htlc_stubs"); tal_free(tmpctx); return KEEP_WATCHING; } @@ -1304,14 +1290,14 @@ static enum watch_result funding_spent(struct peer *peer, if (channel->local_shutdown_idx >= 0) keyindex = channel->local_shutdown_idx; else { - keyindex = wallet_get_newindex(peer->ld); + keyindex = wallet_get_newindex(ld); if (keyindex < 0) { - log_broken(peer->log, "Could not get keyindex"); + log_broken(channel->log, "Could not get keyindex"); tal_free(tmpctx); return KEEP_WATCHING; } } - scriptpubkey = p2wpkh_for_keyidx(tmpctx, peer->ld, keyindex); + scriptpubkey = p2wpkh_for_keyidx(tmpctx, ld, keyindex); if (!scriptpubkey) { channel_internal_error(channel, "Can't get shutdown script %"PRIu64, @@ -1319,9 +1305,9 @@ static enum watch_result funding_spent(struct peer *peer, tal_free(tmpctx); return DELETE_WATCH; } - txfilter_add_scriptpubkey(peer->ld->owned_txfilter, scriptpubkey); + txfilter_add_scriptpubkey(ld->owned_txfilter, scriptpubkey); - if (!bip32_pubkey(peer->ld->wallet->bip32_base, &ourkey, keyindex)) { + if (!bip32_pubkey(ld->wallet->bip32_base, &ourkey, keyindex)) { channel_internal_error(channel, "Can't get shutdown key %"PRIu64, keyindex); @@ -1332,7 +1318,7 @@ static enum watch_result funding_spent(struct peer *peer, /* This could be a mutual close, but it doesn't matter. */ bitcoin_txid(channel->last_tx, &our_last_txid); - msg = towire_onchain_init(peer, + msg = towire_onchain_init(channel, &channel->seed, &channel->their_shachain.chain, channel->funding_satoshi, &channel->channel_info->old_remote_per_commit, @@ -1345,8 +1331,7 @@ static enum watch_result funding_spent(struct peer *peer, * we specify theirs. */ channel->channel_info->their_config.to_self_delay, channel->our_config.to_self_delay, - get_feerate(peer->ld->topology, - FEERATE_NORMAL), + get_feerate(ld->topology, FEERATE_NORMAL), channel->our_config.dust_limit_satoshis, &channel->channel_info->theirbase.revocation, &our_last_txid, @@ -1368,8 +1353,8 @@ static enum watch_result funding_spent(struct peer *peer, /* FIXME: Don't queue all at once, use an empty cb... */ for (size_t i = 0; i < tal_count(stubs); i++) { bool tell_immediate; - bool tell = tell_if_missing(peer, &stubs[i], &tell_immediate); - msg = towire_onchain_htlc(peer, &stubs[i], + bool tell = tell_if_missing(channel, &stubs[i], &tell_immediate); + msg = towire_onchain_htlc(channel, &stubs[i], tell, tell_immediate); subd_send_msg(channel->owner, take(msg)); } @@ -1499,10 +1484,9 @@ static void opening_got_hsm_funding_sig(struct funding_channel *fc, * first step is to build the provisional announcement and ask the HSM * to sign it. */ -static void peer_got_funding_locked(struct peer *peer, const u8 *msg) +static void peer_got_funding_locked(struct channel *channel, const u8 *msg) { struct pubkey next_per_commitment_point; - struct channel *channel = peer2channel(peer); if (!fromwire_channel_got_funding_locked(msg, NULL, &next_per_commitment_point)) { @@ -1517,18 +1501,18 @@ static void peer_got_funding_locked(struct peer *peer, const u8 *msg) "channel_got_funding_locked twice"); return; } - update_per_commit_point(peer, &next_per_commitment_point); + update_per_commit_point(channel2peer(channel), &next_per_commitment_point); - log_debug(peer->log, "Got funding_locked"); + log_debug(channel->log, "Got funding_locked"); channel->remote_funding_locked = true; } -static void peer_got_shutdown(struct peer *peer, const u8 *msg) +static void peer_got_shutdown(struct channel *channel, const u8 *msg) { u8 *scriptpubkey; - struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; - if (!fromwire_channel_got_shutdown(peer, msg, NULL, &scriptpubkey)) { + if (!fromwire_channel_got_shutdown(channel, msg, NULL, &scriptpubkey)) { channel_internal_error(channel, "bad channel_got_shutdown %s", tal_hex(msg, msg)); return; @@ -1560,14 +1544,15 @@ static void peer_got_shutdown(struct peer *peer, const u8 *msg) if (channel->local_shutdown_idx == -1) { u8 *scriptpubkey; - channel->local_shutdown_idx = wallet_get_newindex(peer->ld); + channel->local_shutdown_idx = wallet_get_newindex(ld); if (channel->local_shutdown_idx == -1) { channel_internal_error(channel, "Can't get local shutdown index"); return; } - peer_set_condition(peer, CHANNELD_NORMAL, CHANNELD_SHUTTING_DOWN); + channel_set_state(channel, + CHANNELD_NORMAL, CHANNELD_SHUTTING_DOWN); /* BOLT #2: * @@ -1576,7 +1561,7 @@ static void peer_got_shutdown(struct peer *peer, const u8 *msg) * * ...3. `OP_0` `20` 20-bytes (version 0 pay to witness pubkey), */ - scriptpubkey = p2wpkh_for_keyidx(msg, peer->ld, + scriptpubkey = p2wpkh_for_keyidx(msg, ld, channel->local_shutdown_idx); if (!scriptpubkey) { channel_internal_error(channel, @@ -1585,7 +1570,7 @@ static void peer_got_shutdown(struct peer *peer, const u8 *msg) return; } - txfilter_add_scriptpubkey(peer->ld->owned_txfilter, scriptpubkey); + txfilter_add_scriptpubkey(ld->owned_txfilter, scriptpubkey); /* BOLT #2: * @@ -1594,12 +1579,12 @@ static void peer_got_shutdown(struct peer *peer, const u8 *msg) * peer, unless it has already sent a `shutdown`. */ subd_send_msg(channel->owner, - take(towire_channel_send_shutdown(peer, + take(towire_channel_send_shutdown(channel, scriptpubkey))); } /* TODO(cdecker) Selectively save updated fields to DB */ - wallet_channel_save(peer->ld->wallet, channel); + wallet_channel_save(ld->wallet, channel); } void peer_last_tx(struct peer *peer, struct bitcoin_tx *tx, @@ -1667,11 +1652,12 @@ static bool better_closing_fee(struct lightningd *ld, return new_diff <= old_diff; } -static void peer_received_closing_signature(struct peer *peer, const u8 *msg) +static void peer_received_closing_signature(struct channel *channel, + const u8 *msg) { secp256k1_ecdsa_signature sig; struct bitcoin_tx *tx; - struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; if (!fromwire_closing_received_signature(msg, msg, NULL, &sig, &tx)) { channel_internal_error(channel, "Bad closing_received_signature %s", @@ -1680,20 +1666,19 @@ static void peer_received_closing_signature(struct peer *peer, const u8 *msg) } /* FIXME: Make sure signature is correct! */ - if (better_closing_fee(peer->ld, channel, tx)) { - peer_last_tx(peer, tx, &sig); + if (better_closing_fee(ld, channel, tx)) { + peer_last_tx(channel2peer(channel), tx, &sig); /* TODO(cdecker) Selectively save updated fields to DB */ - wallet_channel_save(peer->ld->wallet, channel); + wallet_channel_save(ld->wallet, channel); } /* OK, you can continue now. */ - subd_send_msg(peer2channel(peer)->owner, - take(towire_closing_received_signature_reply(peer))); + subd_send_msg(channel->owner, + take(towire_closing_received_signature_reply(channel))); } -static void peer_closing_complete(struct peer *peer, const u8 *msg) +static void peer_closing_complete(struct channel *channel, const u8 *msg) { - struct channel *channel = peer2channel(peer); /* FIXME: We should save this, to return to gossipd */ u64 gossip_index; @@ -1704,11 +1689,11 @@ static void peer_closing_complete(struct peer *peer, const u8 *msg) } /* Retransmission only, ignore closing. */ - if (peer2channel(peer)->state == CLOSINGD_COMPLETE) + if (channel->state == CLOSINGD_COMPLETE) return; - drop_to_chain(peer->ld, channel); - peer_set_condition(peer, CLOSINGD_SIGEXCHANGE, CLOSINGD_COMPLETE); + drop_to_chain(channel->peer->ld, channel); + channel_set_state(channel, CLOSINGD_SIGEXCHANGE, CLOSINGD_COMPLETE); } static unsigned closing_msg(struct subd *sd, const u8 *msg, const int *fds) @@ -1717,11 +1702,11 @@ static unsigned closing_msg(struct subd *sd, const u8 *msg, const int *fds) switch (t) { case WIRE_CLOSING_RECEIVED_SIGNATURE: - peer_received_closing_signature(sd->peer, msg); + peer_received_closing_signature(sd->channel, msg); break; case WIRE_CLOSING_COMPLETE: - peer_closing_complete(sd->peer, msg); + peer_closing_complete(sd->channel, msg); break; /* We send these, not receive them */ @@ -1733,18 +1718,18 @@ static unsigned closing_msg(struct subd *sd, const u8 *msg, const int *fds) return 0; } -static void peer_start_closingd(struct peer *peer, +static void peer_start_closingd(struct channel *channel, struct crypto_state *cs, u64 gossip_index, int peer_fd, int gossip_fd, bool reconnected) { - const tal_t *tmpctx = tal_tmpctx(peer); + const tal_t *tmpctx = tal_tmpctx(channel); u8 *initmsg, *local_scriptpubkey; u64 minfee, startfee, feelimit; u64 num_revocations; u64 funding_msatoshi, our_msatoshi, their_msatoshi; - struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; if (channel->local_shutdown_idx == -1 || !channel->remote_shutdown_scriptpubkey) { @@ -1758,20 +1743,20 @@ static void peer_start_closingd(struct peer *peer, return; } - peer_set_owner(peer, new_peer_subd(peer->ld, - "lightning_closingd", peer, + channel_set_owner(channel, new_channel_subd(ld, + "lightning_closingd", channel, closing_wire_type_name, closing_msg, take(&peer_fd), take(&gossip_fd), NULL)); if (!channel->owner) { - log_unusual(peer->log, "Could not subdaemon closing: %s", + log_unusual(channel->log, "Could not subdaemon closing: %s", strerror(errno)); channel_fail_transient(channel, "Failed to subdaemon closing"); tal_free(tmpctx); return; } - local_scriptpubkey = p2wpkh_for_keyidx(tmpctx, peer->ld, + local_scriptpubkey = p2wpkh_for_keyidx(tmpctx, ld, channel->local_shutdown_idx); if (!local_scriptpubkey) { channel_internal_error(channel, @@ -1790,10 +1775,9 @@ static void peer_start_closingd(struct peer *peer, feelimit = commit_tx_base_fee(channel->channel_info->feerate_per_kw[LOCAL], 0); - minfee = commit_tx_base_fee(get_feerate(peer->ld->topology, - FEERATE_SLOW), 0); - startfee = commit_tx_base_fee(get_feerate(peer->ld->topology, - FEERATE_NORMAL), 0); + minfee = commit_tx_base_fee(get_feerate(ld->topology, FEERATE_SLOW), 0); + startfee = commit_tx_base_fee(get_feerate(ld->topology, FEERATE_NORMAL), + 0); if (startfee > feelimit) startfee = feelimit; @@ -1839,10 +1823,10 @@ static void peer_start_closingd(struct peer *peer, tal_free(tmpctx); } -static void peer_start_closingd_after_shutdown(struct peer *peer, const u8 *msg, +static void peer_start_closingd_after_shutdown(struct channel *channel, + const u8 *msg, const int *fds) { - struct channel *channel = peer2channel(peer); struct crypto_state cs; u64 gossip_index; @@ -1851,13 +1835,13 @@ static void peer_start_closingd_after_shutdown(struct peer *peer, const u8 *msg, if (!fromwire_channel_shutdown_complete(msg, NULL, &cs, &gossip_index)) { channel_internal_error(channel, "bad shutdown_complete: %s", - tal_hex(peer, msg)); + tal_hex(msg, msg)); return; } - /* This sets peer->owner, closes down channeld. */ - peer_start_closingd(peer, &cs, gossip_index, fds[0], fds[1], false); - peer_set_condition(peer, CHANNELD_SHUTTING_DOWN, CLOSINGD_SIGEXCHANGE); + /* This sets channel->owner, closes down channeld. */ + peer_start_closingd(channel, &cs, gossip_index, fds[0], fds[1], false); + channel_set_state(channel, CHANNELD_SHUTTING_DOWN, CLOSINGD_SIGEXCHANGE); } static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds) @@ -1866,29 +1850,29 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds) switch (t) { case WIRE_CHANNEL_NORMAL_OPERATION: - peer_set_condition(sd->peer, - CHANNELD_AWAITING_LOCKIN, CHANNELD_NORMAL); + channel_set_state(sd->channel, + CHANNELD_AWAITING_LOCKIN, CHANNELD_NORMAL); break; case WIRE_CHANNEL_SENDING_COMMITSIG: - peer_sending_commitsig(sd->peer, msg); + peer_sending_commitsig(sd->channel, msg); break; case WIRE_CHANNEL_GOT_COMMITSIG: - peer_got_commitsig(sd->peer, msg); + peer_got_commitsig(sd->channel, msg); break; case WIRE_CHANNEL_GOT_REVOKE: - peer_got_revoke(sd->peer, msg); + peer_got_revoke(sd->channel, msg); break; case WIRE_CHANNEL_GOT_FUNDING_LOCKED: - peer_got_funding_locked(sd->peer, msg); + peer_got_funding_locked(sd->channel, msg); break; case WIRE_CHANNEL_GOT_SHUTDOWN: - peer_got_shutdown(sd->peer, msg); + peer_got_shutdown(sd->channel, msg); break; case WIRE_CHANNEL_SHUTDOWN_COMPLETE: /* We expect 2 fds. */ if (!fds) return 2; - peer_start_closingd_after_shutdown(sd->peer, msg, fds); + peer_start_closingd_after_shutdown(sd->channel, msg, fds); break; /* And we never get these from channeld. */ @@ -1959,6 +1943,7 @@ static bool peer_start_channeld(struct peer *peer, const u8 *shutdown_scriptpubkey; u64 num_revocations; struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; /* Now we can consider balance set. */ if (!reconnected) { @@ -1984,8 +1969,8 @@ static bool peer_start_channeld(struct peer *peer, if (hsmfd < 0) fatal("Could not read fd from HSM: %s", strerror(errno)); - peer_set_owner(peer, new_peer_subd(peer->ld, - "lightning_channeld", peer, + channel_set_owner(channel, new_channel_subd(ld, + "lightning_channeld", channel, channel_wire_type_name, channel_msg, take(&peer_fd), @@ -2300,8 +2285,7 @@ static unsigned int opening_negotiation_failed(struct subd *openingd, { struct crypto_state cs; u64 gossip_index; - struct peer *peer = openingd->peer; - struct channel *channel = peer2channel(peer); + struct channel *channel = openingd->channel; char *why; /* We need the peer fd and gossip fd. */ @@ -2316,16 +2300,18 @@ static unsigned int opening_negotiation_failed(struct subd *openingd, return 0; } - msg = towire_gossipctl_hand_back_peer(msg, &peer->id, &cs, gossip_index, + msg = towire_gossipctl_hand_back_peer(msg, + &channel->peer->id, &cs, + gossip_index, NULL); subd_send_msg(openingd->ld->gossip, take(msg)); subd_send_fd(openingd->ld->gossip, fds[0]); subd_send_fd(openingd->ld->gossip, fds[1]); - log_unusual(peer->log, "Opening negotiation failed: %s", why); + log_unusual(channel->log, "Opening negotiation failed: %s", why); /* This will free openingd, since that's peer->owner */ - free_channel(peer2channel(peer), why); + free_channel(channel, why); return 0; } @@ -2356,12 +2342,13 @@ static void peer_accept_channel(struct lightningd *ld, assert(channel == peer2channel(peer)); assert(peer == channel2peer(channel)); - peer_set_condition(peer, UNINITIALIZED, OPENINGD); - peer_set_owner(peer, - new_peer_subd(ld, "lightning_openingd", peer, - opening_wire_type_name, - opening_negotiation_failed, - take(&peer_fd), take(&gossip_fd), NULL)); + channel_set_state(channel, UNINITIALIZED, OPENINGD); + channel_set_owner(channel, + new_channel_subd(ld, "lightning_openingd", channel, + opening_wire_type_name, + opening_negotiation_failed, + take(&peer_fd), take(&gossip_fd), + NULL)); if (!channel->owner) { channel_fail_transient(channel, "Failed to subdaemon opening: %s", @@ -2436,13 +2423,14 @@ static void peer_offer_channel(struct lightningd *ld, channel->funding_satoshi = fc->funding_satoshi; channel->push_msat = fc->push_msat; - peer_set_condition(fc->peer, UNINITIALIZED, OPENINGD); - peer_set_owner(fc->peer, - new_peer_subd(ld, - "lightning_openingd", fc->peer, - opening_wire_type_name, - opening_negotiation_failed, - take(&peer_fd), take(&gossip_fd), NULL)); + channel_set_state(channel, UNINITIALIZED, OPENINGD); + channel_set_owner(channel, + new_channel_subd(ld, + "lightning_openingd", channel, + opening_wire_type_name, + opening_negotiation_failed, + take(&peer_fd), take(&gossip_fd), + NULL)); if (!channel->owner) { fc->peer = tal_free(fc->peer); command_fail(fc->cmd, diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index be01dac89..73a495641 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -103,7 +103,7 @@ void activate_peers(struct lightningd *ld); void drop_to_chain(struct lightningd *ld, struct channel *channel); -void free_htlcs(struct lightningd *ld, const struct peer *peer); +void free_htlcs(struct lightningd *ld, const struct channel *channel); /* Get range of feerates to insist other side abide by for normal channels. */ u32 feerate_min(struct lightningd *ld); diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 7d15ea671..d345c2894 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -334,7 +334,7 @@ static void rcvd_htlc_reply(struct subd *subd, const u8 *msg, const int *fds, &hout->key.id, &failure_code, &failurestr)) { - channel_internal_error(peer2channel(subd->peer), + channel_internal_error(subd->channel, "Bad channel_offer_htlc_reply"); tal_free(hout); return; @@ -359,7 +359,7 @@ static void rcvd_htlc_reply(struct subd *subd, const u8 *msg, const int *fds, if (find_htlc_out(&subd->ld->htlcs_out, hout->key.peer, hout->key.id) || hout->key.id == HTLC_INVALID_ID) { - channel_internal_error(peer2channel(subd->peer), + channel_internal_error(subd->channel, "Bad offer_htlc_reply HTLC id %"PRIu64 " is a duplicate", hout->key.id); @@ -688,19 +688,21 @@ static bool peer_fulfilled_our_htlc(struct channel *channel, return true; } -void onchain_fulfilled_htlc(struct peer *peer, const struct preimage *preimage) +void onchain_fulfilled_htlc(struct channel *channel, + const struct preimage *preimage) { struct htlc_out_map_iter outi; struct htlc_out *hout; struct sha256 payment_hash; + struct lightningd *ld = channel->peer->ld; sha256(&payment_hash, preimage, sizeof(*preimage)); /* FIXME: use db to look this up! */ - for (hout = htlc_out_map_first(&peer->ld->htlcs_out, &outi); + for (hout = htlc_out_map_first(&ld->htlcs_out, &outi); hout; - hout = htlc_out_map_next(&peer->ld->htlcs_out, &outi)) { - if (hout->key.peer != peer) + hout = htlc_out_map_next(&ld->htlcs_out, &outi)) { + if (hout->key.peer != channel2peer(channel)) continue; if (!structeq(&hout->payment_hash, &payment_hash)) @@ -709,7 +711,7 @@ void onchain_fulfilled_htlc(struct peer *peer, const struct preimage *preimage) /* We may have already fulfilled before going onchain, or * we can fulfill onchain multiple times. */ if (!hout->preimage) - fulfill_our_htlc_out(peer, hout, preimage); + fulfill_our_htlc_out(channel2peer(channel), hout, preimage); /* We keep going: this is something of a leak, but onchain * we have no real way of distinguishing HTLCs anyway */ @@ -748,18 +750,19 @@ static bool peer_failed_our_htlc(struct channel *channel, } /* FIXME: Crazy slow! */ -struct htlc_out *find_htlc_out_by_ripemd(const struct peer *peer, +struct htlc_out *find_htlc_out_by_ripemd(const struct channel *channel, const struct ripemd160 *ripemd) { struct htlc_out_map_iter outi; struct htlc_out *hout; + struct lightningd *ld = channel->peer->ld; - for (hout = htlc_out_map_first(&peer->ld->htlcs_out, &outi); + for (hout = htlc_out_map_first(&ld->htlcs_out, &outi); hout; - hout = htlc_out_map_next(&peer->ld->htlcs_out, &outi)) { + hout = htlc_out_map_next(&ld->htlcs_out, &outi)) { struct ripemd160 hash; - if (hout->key.peer != peer) + if (hout->key.peer != channel2peer(channel)) continue; ripemd160(&hash, @@ -770,11 +773,11 @@ struct htlc_out *find_htlc_out_by_ripemd(const struct peer *peer, return NULL; } -void onchain_failed_our_htlc(const struct peer *peer, +void onchain_failed_our_htlc(const struct channel *channel, const struct htlc_stub *htlc, const char *why) { - struct htlc_out *hout = find_htlc_out_by_ripemd(peer, &htlc->ripemd); + struct htlc_out *hout = find_htlc_out_by_ripemd(channel, &htlc->ripemd); /* Don't fail twice! */ if (hout->failuremsg || hout->failcode) @@ -783,7 +786,7 @@ void onchain_failed_our_htlc(const struct peer *peer, hout->failcode = WIRE_PERMANENT_CHANNEL_FAILURE; if (!hout->in) { - char *localfail = tal_fmt(peer, "%s: %s", + char *localfail = tal_fmt(channel, "%s: %s", onion_type_name(WIRE_PERMANENT_CHANNEL_FAILURE), why); payment_failed(hout->key.peer->ld, hout, localfail); @@ -943,7 +946,7 @@ static bool peer_save_commitsig_sent(struct peer *peer, u64 commitnum) return true; } -void peer_sending_commitsig(struct peer *peer, const u8 *msg) +void peer_sending_commitsig(struct channel *channel, const u8 *msg) { u64 commitnum; u32 feerate; @@ -951,7 +954,7 @@ void peer_sending_commitsig(struct peer *peer, const u8 *msg) size_t i, maxid = 0, num_local_added = 0; secp256k1_ecdsa_signature commit_sig; secp256k1_ecdsa_signature *htlc_sigs; - struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; if (!fromwire_channel_sending_commitsig(msg, msg, NULL, &commitnum, @@ -959,12 +962,12 @@ void peer_sending_commitsig(struct peer *peer, const u8 *msg) &changed_htlcs, &commit_sig, &htlc_sigs)) { channel_internal_error(channel, "bad channel_sending_commitsig %s", - tal_hex(peer, msg)); + tal_hex(channel, msg)); return; } for (i = 0; i < tal_count(changed_htlcs); i++) { - if (!changed_htlc(peer, changed_htlcs + i)) { + if (!changed_htlc(channel2peer(channel), changed_htlcs + i)) { channel_internal_error(channel, "channel_sending_commitsig: update failed"); return; @@ -980,31 +983,31 @@ void peer_sending_commitsig(struct peer *peer, const u8 *msg) } if (num_local_added != 0) { - if (maxid != peer2channel(peer)->next_htlc_id + num_local_added - 1) { + if (maxid != channel->next_htlc_id + num_local_added - 1) { channel_internal_error(channel, "channel_sending_commitsig:" " Added %"PRIu64", maxid now %"PRIu64 " from %"PRIu64, - num_local_added, maxid, peer2channel(peer)->next_htlc_id); + num_local_added, maxid, channel->next_htlc_id); return; } - peer2channel(peer)->next_htlc_id += num_local_added; + channel->next_htlc_id += num_local_added; } /* Update their feerate. */ - peer2channel(peer)->channel_info->feerate_per_kw[REMOTE] = feerate; + channel->channel_info->feerate_per_kw[REMOTE] = feerate; - if (!peer_save_commitsig_sent(peer, commitnum)) + if (!peer_save_commitsig_sent(channel2peer(channel), commitnum)) return; /* Last was commit. */ - peer2channel(peer)->last_was_revoke = false; - tal_free(peer2channel(peer)->last_sent_commit); - peer2channel(peer)->last_sent_commit = tal_steal(peer, changed_htlcs); - wallet_channel_save(peer->ld->wallet, peer2channel(peer)); + channel->last_was_revoke = false; + tal_free(channel->last_sent_commit); + channel->last_sent_commit = tal_steal(channel, changed_htlcs); + wallet_channel_save(ld->wallet, channel); /* Tell it we've got it, and to go ahead with commitment_signed. */ - subd_send_msg(peer2channel(peer)->owner, + subd_send_msg(channel->owner, take(towire_channel_sending_commitsig_reply(msg))); } @@ -1084,7 +1087,7 @@ static bool peer_sending_revocation(struct channel *channel, } /* This also implies we're sending revocation */ -void peer_got_commitsig(struct peer *peer, const u8 *msg) +void peer_got_commitsig(struct channel *channel, const u8 *msg) { u64 commitnum; u32 feerate; @@ -1097,7 +1100,7 @@ void peer_got_commitsig(struct peer *peer, const u8 *msg) struct changed_htlc *changed; struct bitcoin_tx *tx; size_t i; - struct channel *channel = peer2channel(peer); + struct lightningd *ld = channel->peer->ld; if (!fromwire_channel_got_commitsig(msg, msg, NULL, &commitnum, @@ -1112,11 +1115,11 @@ void peer_got_commitsig(struct peer *peer, const u8 *msg) &tx)) { channel_internal_error(channel, "bad fromwire_channel_got_commitsig %s", - tal_hex(peer, msg)); + tal_hex(channel, msg)); return; } - log_debug(peer->log, + log_debug(channel->log, "got commitsig %"PRIu64 ": feerate %u, %zu added, %zu fulfilled, %zu failed, %zu changed", commitnum, feerate, tal_count(added), tal_count(fulfilled), @@ -1140,7 +1143,7 @@ void peer_got_commitsig(struct peer *peer, const u8 *msg) } for (i = 0; i < tal_count(changed); i++) { - if (!changed_htlc(peer, &changed[i])) { + if (!changed_htlc(channel2peer(channel), &changed[i])) { channel_internal_error(channel, "got_commitsig: update failed"); return; @@ -1157,14 +1160,14 @@ void peer_got_commitsig(struct peer *peer, const u8 *msg) if (!peer_sending_revocation(channel, added, fulfilled, failed, changed)) return; - if (!peer_save_commitsig_received(peer, commitnum, tx, &commit_sig)) + if (!peer_save_commitsig_received(channel2peer(channel), commitnum, tx, &commit_sig)) return; - wallet_channel_save(peer->ld->wallet, channel); + wallet_channel_save(ld->wallet, channel); tal_free(channel->last_htlc_sigs); channel->last_htlc_sigs = tal_steal(channel, htlc_sigs); - wallet_htlc_sigs_save(peer->ld->wallet, channel->dbid, + wallet_htlc_sigs_save(ld->wallet, channel->dbid, channel->last_htlc_sigs); /* Tell it we've committed, and to go ahead with revoke. */ @@ -1181,26 +1184,26 @@ void update_per_commit_point(struct peer *peer, ci->remote_per_commit = *per_commitment_point; } -void peer_got_revoke(struct peer *peer, const u8 *msg) +void peer_got_revoke(struct channel *channel, const u8 *msg) { u64 revokenum; struct sha256 per_commitment_secret; struct pubkey next_per_commitment_point; struct changed_htlc *changed; enum onion_type *failcodes; - struct channel *channel = peer2channel(peer); size_t i; + struct lightningd *ld = channel->peer->ld; if (!fromwire_channel_got_revoke(msg, msg, NULL, &revokenum, &per_commitment_secret, &next_per_commitment_point, &changed)) { channel_internal_error(channel, "bad fromwire_channel_got_revoke %s", - tal_hex(peer, msg)); + tal_hex(channel, msg)); return; } - log_debug(peer->log, + log_debug(channel->log, "got revoke %"PRIu64": %zu changed", revokenum, tal_count(changed)); @@ -1209,11 +1212,11 @@ void peer_got_revoke(struct peer *peer, const u8 *msg) for (i = 0; i < tal_count(changed); i++) { /* If we're doing final accept, we need to forward */ if (changed[i].newstate == RCVD_ADD_ACK_REVOCATION) { - if (!peer_accepted_htlc(peer, changed[i].id, + if (!peer_accepted_htlc(channel2peer(channel), changed[i].id, &failcodes[i])) return; } else { - if (!changed_htlc(peer, &changed[i])) { + if (!changed_htlc(channel2peer(channel), &changed[i])) { channel_internal_error(channel, "got_revoke: update failed"); return; @@ -1239,7 +1242,7 @@ void peer_got_revoke(struct peer *peer, const u8 *msg) * A receiving node MAY fail if the `per_commitment_secret` was not * generated by the protocol in [BOLT #3] */ - if (!wallet_shachain_add_hash(peer->ld->wallet, + if (!wallet_shachain_add_hash(ld->wallet, &channel->their_shachain, shachain_index(revokenum), &per_commitment_secret)) { @@ -1252,7 +1255,7 @@ void peer_got_revoke(struct peer *peer, const u8 *msg) } /* FIXME: Check per_commitment_secret -> per_commit_point */ - update_per_commit_point(peer, &next_per_commitment_point); + update_per_commit_point(channel2peer(channel), &next_per_commitment_point); /* Tell it we've committed, and to go ahead with revoke. */ msg = towire_channel_got_revoke_reply(msg); @@ -1268,10 +1271,10 @@ void peer_got_revoke(struct peer *peer, const u8 *msg) /* These are all errors before finding next hop. */ assert(!(failcodes[i] & UPDATE)); - hin = find_htlc_in(&peer->ld->htlcs_in, peer, changed[i].id); + hin = find_htlc_in(&ld->htlcs_in, channel2peer(channel), changed[i].id); local_fail_htlc(hin, failcodes[i], NULL); } - wallet_channel_save(peer->ld->wallet, channel); + wallet_channel_save(ld->wallet, channel); } static void *tal_arr_append_(void **p, size_t size) diff --git a/lightningd/peer_htlcs.h b/lightningd/peer_htlcs.h index 569202b24..70ebf76d6 100644 --- a/lightningd/peer_htlcs.h +++ b/lightningd/peer_htlcs.h @@ -28,9 +28,9 @@ void peer_htlcs(const tal_t *ctx, const struct failed_htlc ***failed_htlcs, enum side **failed_sides); -void peer_sending_commitsig(struct peer *peer, const u8 *msg); -void peer_got_commitsig(struct peer *peer, const u8 *msg); -void peer_got_revoke(struct peer *peer, const u8 *msg); +void peer_sending_commitsig(struct channel *channel, const u8 *msg); +void peer_got_commitsig(struct channel *channel, const u8 *msg); +void peer_got_revoke(struct channel *channel, const u8 *msg); void update_per_commit_point(struct peer *peer, const struct pubkey *per_commitment_point); @@ -41,10 +41,11 @@ enum onion_type send_htlc_out(struct peer *out, u64 amount, u32 cltv, struct htlc_in *in, struct htlc_out **houtp); -struct htlc_out *find_htlc_out_by_ripemd(const struct peer *peer, +struct htlc_out *find_htlc_out_by_ripemd(const struct channel *channel, const struct ripemd160 *ripemd160); -void onchain_failed_our_htlc(const struct peer *peer, +void onchain_failed_our_htlc(const struct channel *channel, const struct htlc_stub *htlc, const char *why); -void onchain_fulfilled_htlc(struct peer *peer, const struct preimage *preimage); +void onchain_fulfilled_htlc(struct channel *channel, + const struct preimage *preimage); #endif /* LIGHTNING_LIGHTNINGD_PEER_HTLCS_H */ diff --git a/lightningd/subd.c b/lightningd/subd.c index b679091e8..fdc2b3b35 100644 --- a/lightningd/subd.c +++ b/lightningd/subd.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -388,7 +389,7 @@ static bool log_status_fail(struct subd *sd, const u8 *msg) static bool handle_received_errmsg(struct subd *sd, const u8 *msg) { - struct peer *peer = sd->peer; + struct channel *channel = sd->channel; struct channel_id channel_id; char *desc; @@ -398,16 +399,16 @@ static bool handle_received_errmsg(struct subd *sd, const u8 *msg) /* FIXME: if not all channels failed, hand back to gossipd! */ - /* Don't free sd; we're may be about to free peer. */ - sd->peer = NULL; - channel_fail_permanent(peer2channel(peer), + /* Don't free sd; we're may be about to free channel. */ + sd->channel = NULL; + channel_fail_permanent(channel, "%s: received ERROR %s", sd->name, desc); return true; } static bool handle_sent_errmsg(struct subd *sd, const u8 *msg) { - struct peer *peer = sd->peer; + struct channel *channel = sd->channel; struct channel_id channel_id; char *desc; u8 *errmsg; @@ -417,12 +418,12 @@ static bool handle_sent_errmsg(struct subd *sd, const u8 *msg) return false; /* FIXME: if not all channels failed, hand back to gossipd! */ - if (!peer2channel(sd->peer)->error) - peer2channel(sd->peer)->error = tal_steal(sd->peer, errmsg); + if (!channel->error) + channel->error = tal_steal(channel, errmsg); - /* Don't free sd; we're may be about to free peer. */ - sd->peer = NULL; - channel_fail_permanent(peer2channel(peer), + /* Don't free sd; we're may be about to free channel. */ + sd->channel = NULL; + channel_fail_permanent(channel, "%s: sent ERROR %s", sd->name, desc); return true; } @@ -470,18 +471,18 @@ static struct io_plan *sd_msg_read(struct io_conn *conn, struct subd *sd) goto malformed; goto close; case WIRE_STATUS_PEER_CONNECTION_LOST: - if (!sd->peer) + if (!sd->channel) goto malformed; log_info(sd->log, "Peer connection lost"); goto close; case WIRE_STATUS_RECEIVED_ERRMSG: - if (!sd->peer) + if (!sd->channel) goto malformed; if (!handle_received_errmsg(sd, sd->msg_in)) goto malformed; goto close; case WIRE_STATUS_SENT_ERRMSG: - if (!sd->peer) + if (!sd->channel) goto malformed; if (!handle_sent_errmsg(sd, sd->msg_in)) goto malformed; @@ -570,19 +571,19 @@ static void destroy_subd(struct subd *sd) sd->conn = tal_free(sd->conn); /* Peer still attached? */ - if (sd->peer) { + if (sd->channel) { /* Don't loop back when we fail it. */ - struct peer *peer = sd->peer; + struct channel *channel = sd->channel; struct db *db = sd->ld->wallet->db; bool outer_transaction; - sd->peer = NULL; + sd->channel = NULL; /* We can be freed both inside msg handling, or spontaneously. */ outer_transaction = db->in_transaction; if (!outer_transaction) db_begin_transaction(db); - channel_fail_transient(peer2channel(peer), + channel_fail_transient(channel, "Owning subdaemon %s died (%i)", sd->name, status); if (!outer_transaction) @@ -624,7 +625,7 @@ static struct io_plan *msg_setup(struct io_conn *conn, struct subd *sd) static struct subd *new_subd(struct lightningd *ld, const char *name, - struct peer *peer, + struct channel *channel, const char *(*msgname)(int msgtype), unsigned int (*msgcb)(struct subd *, const u8 *, const int *fds), @@ -648,12 +649,9 @@ static struct subd *new_subd(struct lightningd *ld, return tal_free(sd); } sd->ld = ld; - if (peer) { - /* FIXME: Use minimal unique pubkey prefix for logs! */ - const char *idstr = type_to_string(peer, struct pubkey, - &peer->id); - sd->log = new_log(sd, peer->log_book, "%s(%s):", name, idstr); - tal_free(idstr); + if (channel) { + sd->log = new_log(sd, channel->peer->log_book, "%s-%s", name, + log_prefix(channel->log)); } else { sd->log = new_log(sd, ld->log_book, "%s(%u):", name, sd->pid); } @@ -666,7 +664,7 @@ static struct subd *new_subd(struct lightningd *ld, msg_queue_init(&sd->outq, sd); tal_add_destructor(sd, destroy_subd); list_head_init(&sd->reqs); - sd->peer = peer; + sd->channel = channel; /* conn actually owns daemon: we die when it does. */ sd->conn = io_new_conn(ld, msg_fd, msg_setup, sd); @@ -695,9 +693,9 @@ struct subd *new_global_subd(struct lightningd *ld, return sd; } -struct subd *new_peer_subd(struct lightningd *ld, +struct subd *new_channel_subd(struct lightningd *ld, const char *name, - struct peer *peer, + struct channel *channel, const char *(*msgname)(int msgtype), unsigned int (*msgcb)(struct subd *, const u8 *, const int *fds), ...) @@ -706,7 +704,7 @@ struct subd *new_peer_subd(struct lightningd *ld, struct subd *sd; va_start(ap, msgcb); - sd = new_subd(ld, name, peer, msgname, msgcb, &ap); + sd = new_subd(ld, name, channel, msgname, msgcb, &ap); va_end(ap); return sd; } @@ -766,12 +764,12 @@ void subd_shutdown(struct subd *sd, unsigned int seconds) tal_free(sd); } -void subd_release_peer(struct subd *owner, struct peer *peer) +void subd_release_channel(struct subd *owner, struct channel *channel) { /* If owner is a per-peer-daemon, and not already freeing itself... */ - if (owner->peer) { - assert(owner->peer == peer); - owner->peer = NULL; + if (owner->channel) { + assert(owner->channel == channel); + owner->channel = NULL; tal_free(owner); } } diff --git a/lightningd/subd.h b/lightningd/subd.h index 2fe03c7de..6cf338f74 100644 --- a/lightningd/subd.h +++ b/lightningd/subd.h @@ -25,8 +25,8 @@ struct subd { /* Connection. */ struct io_conn *conn; - /* If we are associated with a single peer, this points to it. */ - struct peer *peer; + /* If we are associated with a single channel, this points to it. */ + struct channel *channel; /* For logging */ struct log *log; @@ -73,10 +73,10 @@ struct subd *new_global_subd(struct lightningd *ld, ...); /** - * new_peer_subd - create a new subdaemon for a specific peer. + * new_channel_subd - create a new subdaemon for a specific channel. * @ld: global state * @name: basename of daemon - * @peer: peer to associate. + * @channel: channel to associate. * @msgname: function to get name from messages * @msgcb: function to call (inside db transaction) when non-fatal message received (or NULL) * @...: NULL-terminated list of pointers to fds to hand as fd 3, 4... @@ -86,13 +86,13 @@ struct subd *new_global_subd(struct lightningd *ld, * that many @fds are received before calling again. If it returns -1, the * subdaemon is shutdown. */ -struct subd *new_peer_subd(struct lightningd *ld, - const char *name, - struct peer *peer, - const char *(*msgname)(int msgtype), - unsigned int (*msgcb)(struct subd *, const u8 *, - const int *fds), - ...); +struct subd *new_channel_subd(struct lightningd *ld, + const char *name, + struct channel *channel, + const char *(*msgname)(int msgtype), + unsigned int (*msgcb)(struct subd *, const u8 *, + const int *fds), + ...); /** * subd_raw - raw interface to get a subdaemon on an fd (for HSM) @@ -144,14 +144,14 @@ void subd_req_(const tal_t *ctx, void *replycb_data); /** - * subd_release_peer - shut down a subdaemon which no longer owns the peer. - * @owner: subd which owned peer. - * @peer: peer to release. + * subd_release_channel - shut down a subdaemon which no longer owns the channel. + * @owner: subd which owned channel. + * @channel: channel to release. * - * If the subdaemon is not already shutting down, and it is a per-peer + * If the subdaemon is not already shutting down, and it is a per-channel * subdaemon, this shuts it down. */ -void subd_release_peer(struct subd *owner, struct peer *peer); +void subd_release_channel(struct subd *owner, struct channel *channel); /** * subd_shutdown - try to politely shut down a subdaemon. diff --git a/lightningd/test/run-find_my_path.c b/lightningd/test/run-find_my_path.c index aae4503d4..409f0d2cb 100644 --- a/lightningd/test/run-find_my_path.c +++ b/lightningd/test/run-find_my_path.c @@ -29,7 +29,7 @@ int debug_poll(struct pollfd *fds UNNEEDED, nfds_t nfds UNNEEDED, int timeout UN void fatal(const char *fmt UNNEEDED, ...) { fprintf(stderr, "fatal called!\n"); abort(); } /* Generated stub for free_htlcs */ -void free_htlcs(struct lightningd *ld UNNEEDED, const struct peer *peer UNNEEDED) +void free_htlcs(struct lightningd *ld UNNEEDED, const struct channel *channel UNNEEDED) { fprintf(stderr, "free_htlcs called!\n"); abort(); } /* Generated stub for gossip_init */ void gossip_init(struct lightningd *ld UNNEEDED) diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 9e4aa9eef..6cac1a1ed 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -80,7 +80,7 @@ bool derive_basepoints(const struct privkey *seed UNNEEDED, bool extract_channel_id(const u8 *in_pkt UNNEEDED, struct channel_id *channel_id UNNEEDED) { fprintf(stderr, "extract_channel_id called!\n"); abort(); } /* Generated stub for find_htlc_out_by_ripemd */ -struct htlc_out *find_htlc_out_by_ripemd(const struct peer *peer UNNEEDED, +struct htlc_out *find_htlc_out_by_ripemd(const struct channel *channel UNNEEDED, const struct ripemd160 *ripemd160 UNNEEDED) { fprintf(stderr, "find_htlc_out_by_ripemd called!\n"); abort(); } /* Generated stub for find_txwatch */ @@ -321,28 +321,29 @@ void log_io(struct log *log UNNEEDED, enum log_level dir UNNEEDED, const char *c /* Generated stub for logv_add */ void logv_add(struct log *log UNNEEDED, const char *fmt UNNEEDED, va_list ap UNNEEDED) { fprintf(stderr, "logv_add called!\n"); abort(); } +/* Generated stub for new_channel_subd */ +struct subd *new_channel_subd(struct lightningd *ld UNNEEDED, + const char *name UNNEEDED, + struct channel *channel UNNEEDED, + const char *(*msgname)(int msgtype) UNNEEDED, + unsigned int (*msgcb)(struct subd * UNNEEDED, const u8 * UNNEEDED, + const int *fds) UNNEEDED, + ...) +{ fprintf(stderr, "new_channel_subd called!\n"); abort(); } /* Generated stub for new_json_result */ struct json_result *new_json_result(const tal_t *ctx UNNEEDED) { fprintf(stderr, "new_json_result called!\n"); abort(); } -/* Generated stub for new_peer_subd */ -struct subd *new_peer_subd(struct lightningd *ld UNNEEDED, - const char *name UNNEEDED, - struct peer *peer UNNEEDED, - const char *(*msgname)(int msgtype) UNNEEDED, - unsigned int (*msgcb)(struct subd * UNNEEDED, const u8 * UNNEEDED, - const int *fds) UNNEEDED, - ...) -{ fprintf(stderr, "new_peer_subd called!\n"); abort(); } /* Generated stub for null_response */ struct json_result *null_response(const tal_t *ctx UNNEEDED) { fprintf(stderr, "null_response called!\n"); abort(); } /* Generated stub for onchain_failed_our_htlc */ -void onchain_failed_our_htlc(const struct peer *peer UNNEEDED, +void onchain_failed_our_htlc(const struct channel *channel UNNEEDED, const struct htlc_stub *htlc UNNEEDED, const char *why UNNEEDED) { fprintf(stderr, "onchain_failed_our_htlc called!\n"); abort(); } /* Generated stub for onchain_fulfilled_htlc */ -void onchain_fulfilled_htlc(struct peer *peer UNNEEDED, const struct preimage *preimage UNNEEDED) +void onchain_fulfilled_htlc(struct channel *channel UNNEEDED, + const struct preimage *preimage UNNEEDED) { fprintf(stderr, "onchain_fulfilled_htlc called!\n"); abort(); } /* Generated stub for onchain_wire_type_name */ const char *onchain_wire_type_name(int e UNNEEDED) @@ -351,10 +352,10 @@ const char *onchain_wire_type_name(int e UNNEEDED) const char *opening_wire_type_name(int e UNNEEDED) { fprintf(stderr, "opening_wire_type_name called!\n"); abort(); } /* Generated stub for peer_got_commitsig */ -void peer_got_commitsig(struct peer *peer UNNEEDED, const u8 *msg UNNEEDED) +void peer_got_commitsig(struct channel *channel UNNEEDED, const u8 *msg UNNEEDED) { fprintf(stderr, "peer_got_commitsig called!\n"); abort(); } /* Generated stub for peer_got_revoke */ -void peer_got_revoke(struct peer *peer UNNEEDED, const u8 *msg UNNEEDED) +void peer_got_revoke(struct channel *channel UNNEEDED, const u8 *msg UNNEEDED) { fprintf(stderr, "peer_got_revoke called!\n"); abort(); } /* Generated stub for peer_htlcs */ void peer_htlcs(const tal_t *ctx UNNEEDED, @@ -367,15 +368,15 @@ void peer_htlcs(const tal_t *ctx UNNEEDED, enum side **failed_sides UNNEEDED) { fprintf(stderr, "peer_htlcs called!\n"); abort(); } /* Generated stub for peer_sending_commitsig */ -void peer_sending_commitsig(struct peer *peer UNNEEDED, const u8 *msg UNNEEDED) +void peer_sending_commitsig(struct channel *channel UNNEEDED, const u8 *msg UNNEEDED) { fprintf(stderr, "peer_sending_commitsig called!\n"); abort(); } /* Generated stub for sanitize_error */ char *sanitize_error(const tal_t *ctx UNNEEDED, const u8 *errmsg UNNEEDED, struct channel_id *channel_id UNNEEDED) { fprintf(stderr, "sanitize_error called!\n"); abort(); } -/* Generated stub for subd_release_peer */ -void subd_release_peer(struct subd *owner UNNEEDED, struct peer *peer UNNEEDED) -{ fprintf(stderr, "subd_release_peer called!\n"); abort(); } +/* Generated stub for subd_release_channel */ +void subd_release_channel(struct subd *owner UNNEEDED, struct channel *channel UNNEEDED) +{ fprintf(stderr, "subd_release_channel called!\n"); abort(); } /* Generated stub for subd_req_ */ void subd_req_(const tal_t *ctx UNNEEDED, struct subd *sd UNNEEDED,