subd: keep pointer to channel, not peer.

This rolls through many other functions, making them take channel not peer.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-02-12 20:43:04 +10:30
committed by Christian Decker
parent b7680412e3
commit 409fef582d
10 changed files with 291 additions and 277 deletions

View File

@@ -15,7 +15,7 @@ void channel_set_owner(struct channel *channel, struct subd *owner)
channel->owner = owner; channel->owner = owner;
if (old_owner) if (old_owner)
subd_release_peer(old_owner, channel2peer(channel)); subd_release_channel(old_owner, channel);
} }
static void destroy_channel(struct channel *channel) static void destroy_channel(struct channel *channel)
@@ -112,6 +112,25 @@ struct channel *peer_active_channel(struct peer *peer)
return NULL; 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) struct channel *peer2channel(const struct peer *peer)
{ {
return list_top(&peer->channels, struct channel, list); return list_top(&peer->channels, struct channel, list);

View File

@@ -105,6 +105,10 @@ void channel_fail_permanent(struct channel *channel, const char *fmt, ...);
/* Permanent error, but due to internal problems, not peer. */ /* Permanent error, but due to internal problems, not peer. */
void channel_internal_error(struct channel *channel, const char *fmt, ...); 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. */ /* FIXME: Temporary mapping from peer to channel, while we only have one. */
struct channel *peer2channel(const struct peer *peer); struct channel *peer2channel(const struct peer *peer);
struct peer *channel2peer(const struct channel *channel); struct peer *channel2peer(const struct channel *channel);

View File

@@ -75,7 +75,7 @@ static bool peer_start_channeld(struct peer *peer,
int peer_fd, int gossip_fd, int peer_fd, int gossip_fd,
const u8 *funding_signed, const u8 *funding_signed,
bool reconnected); bool reconnected);
static void peer_start_closingd(struct peer *peer, static void peer_start_closingd(struct channel *channel,
struct crypto_state *cs, struct crypto_state *cs,
u64 gossip_index, u64 gossip_index,
int peer_fd, int gossip_fd, int peer_fd, int gossip_fd,
@@ -146,6 +146,7 @@ struct peer *new_peer(struct lightningd *ld, u64 dbid,
/* Max 128k per peer. */ /* Max 128k per peer. */
peer->log_book = new_log_book(peer, 128*1024, peer->log_book = new_log_book(peer, 128*1024,
get_log_level(ld->log_book)); get_log_level(ld->log_book));
/* FIXME: Use minimal unique pubkey prefix for logs! */
idname = type_to_string(peer, struct pubkey, id); idname = type_to_string(peer, struct pubkey, id);
peer->log = new_log(peer, peer->log_book, "peer %s:", idname); peer->log = new_log(peer, peer->log_book, "peer %s:", idname);
tal_free(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); struct channel *channel = peer2channel(peer);
log_info(peer->log, "State changed from %s to %s", channel_set_state(channel, old_state, state);
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);
}
} }
static void destroy_connect(struct connect *c) 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_set_owner(peer, NULL);
peer->addr = addr; peer->addr = addr;
peer_start_closingd(peer, &cs, gossip_index, peer_start_closingd(channel, &cs, gossip_index,
peer_fd, gossip_fd, peer_fd, gossip_fd,
true); true);
goto connected; 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. */ /* 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_map_iter ini;
struct htlc_in *hin; struct htlc_in *hin;
u8 *msg; 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;
hin = htlc_in_map_next(&peer->ld->htlcs_in, &ini)) { hin = htlc_in_map_next(&ld->htlcs_in, &ini)) {
if (hin->key.peer != peer) if (hin->key.peer != channel2peer(channel))
continue; continue;
/* BOLT #5: /* BOLT #5:
@@ -918,15 +906,14 @@ static void onchaind_tell_fulfill(struct peer *peer)
if (!hin->preimage) if (!hin->preimage)
continue; continue;
msg = towire_onchain_known_preimage(peer, hin->preimage); msg = towire_onchain_known_preimage(channel, hin->preimage);
subd_send_msg(channel->owner, take(msg)); 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; u8 state;
struct channel *channel = peer2channel(peer);
if (!fromwire_onchain_init_reply(msg, NULL, &state)) { if (!fromwire_onchain_init_reply(msg, NULL, &state)) {
channel_internal_error(channel, "Invalid onchain_init_reply"); 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; return;
} }
peer_set_condition(peer, FUNDING_SPEND_SEEN, state); channel_set_state(channel, FUNDING_SPEND_SEEN, state);
/* Tell it about any preimages we know. */ /* 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, 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); 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 bitcoin_tx *tx;
struct channel *channel = peer2channel(peer);
if (!fromwire_onchain_broadcast_tx(msg, msg, NULL, &tx)) { if (!fromwire_onchain_broadcast_tx(msg, msg, NULL, &tx)) {
channel_internal_error(channel, "Invalid onchain_broadcast_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. */ /* 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 bitcoin_txid txid;
struct txwatch *txw; struct txwatch *txw;
struct channel *channel = peer2channel(peer);
if (!fromwire_onchain_unwatch_tx(msg, NULL, &txid)) { if (!fromwire_onchain_unwatch_tx(msg, NULL, &txid)) {
channel_internal_error(channel, "Invalid onchain_unwatch_tx"); 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() */ /* 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) 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)); type_to_string(ltmp, struct bitcoin_txid, &txid));
tal_free(txw); 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 preimage preimage;
struct channel *channel = peer2channel(peer);
if (!fromwire_onchain_extracted_preimage(msg, NULL, &preimage)) { if (!fromwire_onchain_extracted_preimage(msg, NULL, &preimage)) {
channel_internal_error(channel, "Invalid extracted_preimage"); channel_internal_error(channel, "Invalid extracted_preimage");
return; 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 htlc_stub htlc;
struct channel *channel = peer2channel(peer);
if (!fromwire_onchain_missing_htlc_output(msg, NULL, &htlc)) { if (!fromwire_onchain_missing_htlc_output(msg, NULL, &htlc)) {
channel_internal_error(channel, "Invalid missing_htlc_output"); 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 * reasonable depth, and MAY fail it sooner if no valid commitment
* transaction contains an output corresponding to the HTLC. * 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 htlc_stub htlc;
struct channel *channel = peer2channel(peer);
if (!fromwire_onchain_htlc_timeout(msg, NULL, &htlc)) { if (!fromwire_onchain_htlc_timeout(msg, NULL, &htlc)) {
channel_internal_error(channel, "Invalid onchain_htlc_timeout"); 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 * HTLC (if any) once the resolving transaction has reached reasonable
* depth. * 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) */ /* If channel is NULL, free them all (for shutdown) */
void free_htlcs(struct lightningd *ld, const struct peer *peer) void free_htlcs(struct lightningd *ld, const struct channel *channel)
{ {
struct htlc_out_map_iter outi; struct htlc_out_map_iter outi;
struct htlc_out *hout; 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); for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
hout; hout;
hout = htlc_out_map_next(&ld->htlcs_out, &outi)) { hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
if (peer && hout->key.peer != peer) if (channel && hout->key.peer != channel2peer(channel))
continue; continue;
tal_free(hout); tal_free(hout);
deleted = true; 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); for (hin = htlc_in_map_first(&ld->htlcs_in, &ini);
hin; hin;
hin = htlc_in_map_next(&ld->htlcs_in, &ini)) { hin = htlc_in_map_next(&ld->htlcs_in, &ini)) {
if (peer && hin->key.peer != peer) if (channel && hin->key.peer != channel2peer(channel))
continue; continue;
tal_free(hin); tal_free(hin);
deleted = true; deleted = true;
@@ -1134,21 +1116,21 @@ void free_htlcs(struct lightningd *ld, const struct peer *peer)
} while (deleted); } 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! */ /* 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. */ /* 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 * 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); struct utxo *u = tal(msg, struct utxo);
u->close_info = tal(u, struct unilateral_close_info); 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->is_p2sh = true;
u->keyindex = 0; u->keyindex = 0;
u->status = output_state_available; u->status = output_state_available;
u->close_info->channel_id = peer2channel(peer)->dbid; u->close_info->channel_id = channel->dbid;
u->close_info->peer_id = peer->id; u->close_info->peer_id = channel->peer->id;
if (!fromwire_onchain_add_utxo(msg, NULL, &u->txid, &u->outnum, if (!fromwire_onchain_add_utxo(msg, NULL, &u->txid, &u->outnum,
&u->close_info->commitment_point, &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) 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) { switch (t) {
case WIRE_ONCHAIN_INIT_REPLY: case WIRE_ONCHAIN_INIT_REPLY:
handle_onchain_init_reply(sd->peer, msg); handle_onchain_init_reply(sd->channel, msg);
break; break;
case WIRE_ONCHAIN_BROADCAST_TX: case WIRE_ONCHAIN_BROADCAST_TX:
handle_onchain_broadcast_tx(sd->peer, msg); handle_onchain_broadcast_tx(sd->channel, msg);
break; break;
case WIRE_ONCHAIN_UNWATCH_TX: case WIRE_ONCHAIN_UNWATCH_TX:
handle_onchain_unwatch_tx(sd->peer, msg); handle_onchain_unwatch_tx(sd->channel, msg);
break; break;
case WIRE_ONCHAIN_EXTRACTED_PREIMAGE: case WIRE_ONCHAIN_EXTRACTED_PREIMAGE:
handle_extracted_preimage(sd->peer, msg); handle_extracted_preimage(sd->channel, msg);
break; break;
case WIRE_ONCHAIN_MISSING_HTLC_OUTPUT: case WIRE_ONCHAIN_MISSING_HTLC_OUTPUT:
handle_missing_htlc_output(sd->peer, msg); handle_missing_htlc_output(sd->channel, msg);
break; break;
case WIRE_ONCHAIN_HTLC_TIMEOUT: case WIRE_ONCHAIN_HTLC_TIMEOUT:
handle_onchain_htlc_timeout(sd->peer, msg); handle_onchain_htlc_timeout(sd->channel, msg);
break; break;
case WIRE_ONCHAIN_ALL_IRREVOCABLY_RESOLVED: case WIRE_ONCHAIN_ALL_IRREVOCABLY_RESOLVED:
handle_irrevocably_resolved(sd->peer, msg); handle_irrevocably_resolved(sd->channel, msg);
break; break;
case WIRE_ONCHAIN_ADD_UTXO: case WIRE_ONCHAIN_ADD_UTXO:
onchain_add_utxo(sd->peer, msg); onchain_add_utxo(sd->channel, msg);
break; break;
/* We send these, not receive them */ /* 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. */ /* 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) bool *tell_immediate)
{ {
struct htlc_out *hout; struct htlc_out *hout;
@@ -1238,7 +1221,7 @@ static bool tell_if_missing(const struct peer *peer, struct htlc_stub *stub,
*tell_immediate = false; *tell_immediate = false;
/* Is it a current HTLC? */ /* 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) if (!hout)
return false; return false;
@@ -1254,7 +1237,8 @@ static bool tell_if_missing(const struct peer *peer, struct htlc_stub *stub,
&& hout->hstate < SENT_REMOVE_REVOCATION) && hout->hstate < SENT_REMOVE_REVOCATION)
*tell_immediate = true; *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"); hout->key.id, *tell_immediate ? "immediate" : "later");
return true; return true;
} }
@@ -1274,28 +1258,30 @@ static enum watch_result funding_spent(struct peer *peer,
struct htlc_stub *stubs; struct htlc_stub *stubs;
const tal_t *tmpctx = tal_tmpctx(peer); const tal_t *tmpctx = tal_tmpctx(peer);
struct channel *channel = peer2channel(peer); struct channel *channel = peer2channel(peer);
struct lightningd *ld = channel->peer->ld;
channel_fail_permanent(channel, "Funding transaction spent"); channel_fail_permanent(channel, "Funding transaction spent");
/* We could come from almost any state. */ /* 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, channel_set_owner(channel, new_channel_subd(ld,
"lightning_onchaind", peer, "lightning_onchaind",
channel,
onchain_wire_type_name, onchain_wire_type_name,
onchain_msg, onchain_msg,
NULL)); NULL));
if (!channel->owner) { if (!channel->owner) {
log_broken(peer->log, "Could not subdaemon onchain: %s", log_broken(channel->log, "Could not subdaemon onchain: %s",
strerror(errno)); strerror(errno));
tal_free(tmpctx); tal_free(tmpctx);
return KEEP_WATCHING; return KEEP_WATCHING;
} }
stubs = wallet_htlc_stubs(tmpctx, peer->ld->wallet, channel); stubs = wallet_htlc_stubs(tmpctx, ld->wallet, channel);
if (!stubs) { if (!stubs) {
log_broken(peer->log, "Could not load htlc_stubs"); log_broken(channel->log, "Could not load htlc_stubs");
tal_free(tmpctx); tal_free(tmpctx);
return KEEP_WATCHING; return KEEP_WATCHING;
} }
@@ -1304,14 +1290,14 @@ static enum watch_result funding_spent(struct peer *peer,
if (channel->local_shutdown_idx >= 0) if (channel->local_shutdown_idx >= 0)
keyindex = channel->local_shutdown_idx; keyindex = channel->local_shutdown_idx;
else { else {
keyindex = wallet_get_newindex(peer->ld); keyindex = wallet_get_newindex(ld);
if (keyindex < 0) { if (keyindex < 0) {
log_broken(peer->log, "Could not get keyindex"); log_broken(channel->log, "Could not get keyindex");
tal_free(tmpctx); tal_free(tmpctx);
return KEEP_WATCHING; return KEEP_WATCHING;
} }
} }
scriptpubkey = p2wpkh_for_keyidx(tmpctx, peer->ld, keyindex); scriptpubkey = p2wpkh_for_keyidx(tmpctx, ld, keyindex);
if (!scriptpubkey) { if (!scriptpubkey) {
channel_internal_error(channel, channel_internal_error(channel,
"Can't get shutdown script %"PRIu64, "Can't get shutdown script %"PRIu64,
@@ -1319,9 +1305,9 @@ static enum watch_result funding_spent(struct peer *peer,
tal_free(tmpctx); tal_free(tmpctx);
return DELETE_WATCH; 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, channel_internal_error(channel,
"Can't get shutdown key %"PRIu64, "Can't get shutdown key %"PRIu64,
keyindex); 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. */ /* This could be a mutual close, but it doesn't matter. */
bitcoin_txid(channel->last_tx, &our_last_txid); 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->seed, &channel->their_shachain.chain,
channel->funding_satoshi, channel->funding_satoshi,
&channel->channel_info->old_remote_per_commit, &channel->channel_info->old_remote_per_commit,
@@ -1345,8 +1331,7 @@ static enum watch_result funding_spent(struct peer *peer,
* we specify theirs. */ * we specify theirs. */
channel->channel_info->their_config.to_self_delay, channel->channel_info->their_config.to_self_delay,
channel->our_config.to_self_delay, channel->our_config.to_self_delay,
get_feerate(peer->ld->topology, get_feerate(ld->topology, FEERATE_NORMAL),
FEERATE_NORMAL),
channel->our_config.dust_limit_satoshis, channel->our_config.dust_limit_satoshis,
&channel->channel_info->theirbase.revocation, &channel->channel_info->theirbase.revocation,
&our_last_txid, &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... */ /* FIXME: Don't queue all at once, use an empty cb... */
for (size_t i = 0; i < tal_count(stubs); i++) { for (size_t i = 0; i < tal_count(stubs); i++) {
bool tell_immediate; bool tell_immediate;
bool tell = tell_if_missing(peer, &stubs[i], &tell_immediate); bool tell = tell_if_missing(channel, &stubs[i], &tell_immediate);
msg = towire_onchain_htlc(peer, &stubs[i], msg = towire_onchain_htlc(channel, &stubs[i],
tell, tell_immediate); tell, tell_immediate);
subd_send_msg(channel->owner, take(msg)); 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 * first step is to build the provisional announcement and ask the HSM
* to sign it. */ * 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 pubkey next_per_commitment_point;
struct channel *channel = peer2channel(peer);
if (!fromwire_channel_got_funding_locked(msg, NULL, if (!fromwire_channel_got_funding_locked(msg, NULL,
&next_per_commitment_point)) { &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"); "channel_got_funding_locked twice");
return; 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; 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; 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", channel_internal_error(channel, "bad channel_got_shutdown %s",
tal_hex(msg, msg)); tal_hex(msg, msg));
return; return;
@@ -1560,14 +1544,15 @@ static void peer_got_shutdown(struct peer *peer, const u8 *msg)
if (channel->local_shutdown_idx == -1) { if (channel->local_shutdown_idx == -1) {
u8 *scriptpubkey; 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) { if (channel->local_shutdown_idx == -1) {
channel_internal_error(channel, channel_internal_error(channel,
"Can't get local shutdown index"); "Can't get local shutdown index");
return; return;
} }
peer_set_condition(peer, CHANNELD_NORMAL, CHANNELD_SHUTTING_DOWN); channel_set_state(channel,
CHANNELD_NORMAL, CHANNELD_SHUTTING_DOWN);
/* BOLT #2: /* 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), * ...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); channel->local_shutdown_idx);
if (!scriptpubkey) { if (!scriptpubkey) {
channel_internal_error(channel, channel_internal_error(channel,
@@ -1585,7 +1570,7 @@ static void peer_got_shutdown(struct peer *peer, const u8 *msg)
return; return;
} }
txfilter_add_scriptpubkey(peer->ld->owned_txfilter, scriptpubkey); txfilter_add_scriptpubkey(ld->owned_txfilter, scriptpubkey);
/* BOLT #2: /* 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`. * peer, unless it has already sent a `shutdown`.
*/ */
subd_send_msg(channel->owner, subd_send_msg(channel->owner,
take(towire_channel_send_shutdown(peer, take(towire_channel_send_shutdown(channel,
scriptpubkey))); scriptpubkey)));
} }
/* TODO(cdecker) Selectively save updated fields to DB */ /* 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, 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; 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; secp256k1_ecdsa_signature sig;
struct bitcoin_tx *tx; 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)) { if (!fromwire_closing_received_signature(msg, msg, NULL, &sig, &tx)) {
channel_internal_error(channel, "Bad closing_received_signature %s", 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! */ /* FIXME: Make sure signature is correct! */
if (better_closing_fee(peer->ld, channel, tx)) { if (better_closing_fee(ld, channel, tx)) {
peer_last_tx(peer, tx, &sig); peer_last_tx(channel2peer(channel), tx, &sig);
/* TODO(cdecker) Selectively save updated fields to DB */ /* 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. */ /* OK, you can continue now. */
subd_send_msg(peer2channel(peer)->owner, subd_send_msg(channel->owner,
take(towire_closing_received_signature_reply(peer))); 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 */ /* FIXME: We should save this, to return to gossipd */
u64 gossip_index; u64 gossip_index;
@@ -1704,11 +1689,11 @@ static void peer_closing_complete(struct peer *peer, const u8 *msg)
} }
/* Retransmission only, ignore closing. */ /* Retransmission only, ignore closing. */
if (peer2channel(peer)->state == CLOSINGD_COMPLETE) if (channel->state == CLOSINGD_COMPLETE)
return; return;
drop_to_chain(peer->ld, channel); drop_to_chain(channel->peer->ld, channel);
peer_set_condition(peer, CLOSINGD_SIGEXCHANGE, CLOSINGD_COMPLETE); channel_set_state(channel, CLOSINGD_SIGEXCHANGE, CLOSINGD_COMPLETE);
} }
static unsigned closing_msg(struct subd *sd, const u8 *msg, const int *fds) 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) { switch (t) {
case WIRE_CLOSING_RECEIVED_SIGNATURE: case WIRE_CLOSING_RECEIVED_SIGNATURE:
peer_received_closing_signature(sd->peer, msg); peer_received_closing_signature(sd->channel, msg);
break; break;
case WIRE_CLOSING_COMPLETE: case WIRE_CLOSING_COMPLETE:
peer_closing_complete(sd->peer, msg); peer_closing_complete(sd->channel, msg);
break; break;
/* We send these, not receive them */ /* 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; return 0;
} }
static void peer_start_closingd(struct peer *peer, static void peer_start_closingd(struct channel *channel,
struct crypto_state *cs, struct crypto_state *cs,
u64 gossip_index, u64 gossip_index,
int peer_fd, int gossip_fd, int peer_fd, int gossip_fd,
bool reconnected) bool reconnected)
{ {
const tal_t *tmpctx = tal_tmpctx(peer); const tal_t *tmpctx = tal_tmpctx(channel);
u8 *initmsg, *local_scriptpubkey; u8 *initmsg, *local_scriptpubkey;
u64 minfee, startfee, feelimit; u64 minfee, startfee, feelimit;
u64 num_revocations; u64 num_revocations;
u64 funding_msatoshi, our_msatoshi, their_msatoshi; u64 funding_msatoshi, our_msatoshi, their_msatoshi;
struct channel *channel = peer2channel(peer); struct lightningd *ld = channel->peer->ld;
if (channel->local_shutdown_idx == -1 if (channel->local_shutdown_idx == -1
|| !channel->remote_shutdown_scriptpubkey) { || !channel->remote_shutdown_scriptpubkey) {
@@ -1758,20 +1743,20 @@ static void peer_start_closingd(struct peer *peer,
return; return;
} }
peer_set_owner(peer, new_peer_subd(peer->ld, channel_set_owner(channel, new_channel_subd(ld,
"lightning_closingd", peer, "lightning_closingd", channel,
closing_wire_type_name, closing_msg, closing_wire_type_name, closing_msg,
take(&peer_fd), take(&gossip_fd), take(&peer_fd), take(&gossip_fd),
NULL)); NULL));
if (!channel->owner) { if (!channel->owner) {
log_unusual(peer->log, "Could not subdaemon closing: %s", log_unusual(channel->log, "Could not subdaemon closing: %s",
strerror(errno)); strerror(errno));
channel_fail_transient(channel, "Failed to subdaemon closing"); channel_fail_transient(channel, "Failed to subdaemon closing");
tal_free(tmpctx); tal_free(tmpctx);
return; return;
} }
local_scriptpubkey = p2wpkh_for_keyidx(tmpctx, peer->ld, local_scriptpubkey = p2wpkh_for_keyidx(tmpctx, ld,
channel->local_shutdown_idx); channel->local_shutdown_idx);
if (!local_scriptpubkey) { if (!local_scriptpubkey) {
channel_internal_error(channel, 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], feelimit = commit_tx_base_fee(channel->channel_info->feerate_per_kw[LOCAL],
0); 0);
minfee = commit_tx_base_fee(get_feerate(peer->ld->topology, minfee = commit_tx_base_fee(get_feerate(ld->topology, FEERATE_SLOW), 0);
FEERATE_SLOW), 0); startfee = commit_tx_base_fee(get_feerate(ld->topology, FEERATE_NORMAL),
startfee = commit_tx_base_fee(get_feerate(peer->ld->topology, 0);
FEERATE_NORMAL), 0);
if (startfee > feelimit) if (startfee > feelimit)
startfee = feelimit; startfee = feelimit;
@@ -1839,10 +1823,10 @@ static void peer_start_closingd(struct peer *peer,
tal_free(tmpctx); 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) const int *fds)
{ {
struct channel *channel = peer2channel(peer);
struct crypto_state cs; struct crypto_state cs;
u64 gossip_index; 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)) { if (!fromwire_channel_shutdown_complete(msg, NULL, &cs, &gossip_index)) {
channel_internal_error(channel, "bad shutdown_complete: %s", channel_internal_error(channel, "bad shutdown_complete: %s",
tal_hex(peer, msg)); tal_hex(msg, msg));
return; return;
} }
/* This sets peer->owner, closes down channeld. */ /* This sets channel->owner, closes down channeld. */
peer_start_closingd(peer, &cs, gossip_index, fds[0], fds[1], false); peer_start_closingd(channel, &cs, gossip_index, fds[0], fds[1], false);
peer_set_condition(peer, CHANNELD_SHUTTING_DOWN, CLOSINGD_SIGEXCHANGE); channel_set_state(channel, CHANNELD_SHUTTING_DOWN, CLOSINGD_SIGEXCHANGE);
} }
static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds) 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) { switch (t) {
case WIRE_CHANNEL_NORMAL_OPERATION: case WIRE_CHANNEL_NORMAL_OPERATION:
peer_set_condition(sd->peer, channel_set_state(sd->channel,
CHANNELD_AWAITING_LOCKIN, CHANNELD_NORMAL); CHANNELD_AWAITING_LOCKIN, CHANNELD_NORMAL);
break; break;
case WIRE_CHANNEL_SENDING_COMMITSIG: case WIRE_CHANNEL_SENDING_COMMITSIG:
peer_sending_commitsig(sd->peer, msg); peer_sending_commitsig(sd->channel, msg);
break; break;
case WIRE_CHANNEL_GOT_COMMITSIG: case WIRE_CHANNEL_GOT_COMMITSIG:
peer_got_commitsig(sd->peer, msg); peer_got_commitsig(sd->channel, msg);
break; break;
case WIRE_CHANNEL_GOT_REVOKE: case WIRE_CHANNEL_GOT_REVOKE:
peer_got_revoke(sd->peer, msg); peer_got_revoke(sd->channel, msg);
break; break;
case WIRE_CHANNEL_GOT_FUNDING_LOCKED: case WIRE_CHANNEL_GOT_FUNDING_LOCKED:
peer_got_funding_locked(sd->peer, msg); peer_got_funding_locked(sd->channel, msg);
break; break;
case WIRE_CHANNEL_GOT_SHUTDOWN: case WIRE_CHANNEL_GOT_SHUTDOWN:
peer_got_shutdown(sd->peer, msg); peer_got_shutdown(sd->channel, msg);
break; break;
case WIRE_CHANNEL_SHUTDOWN_COMPLETE: case WIRE_CHANNEL_SHUTDOWN_COMPLETE:
/* We expect 2 fds. */ /* We expect 2 fds. */
if (!fds) if (!fds)
return 2; return 2;
peer_start_closingd_after_shutdown(sd->peer, msg, fds); peer_start_closingd_after_shutdown(sd->channel, msg, fds);
break; break;
/* And we never get these from channeld. */ /* And we never get these from channeld. */
@@ -1959,6 +1943,7 @@ static bool peer_start_channeld(struct peer *peer,
const u8 *shutdown_scriptpubkey; const u8 *shutdown_scriptpubkey;
u64 num_revocations; u64 num_revocations;
struct channel *channel = peer2channel(peer); struct channel *channel = peer2channel(peer);
struct lightningd *ld = channel->peer->ld;
/* Now we can consider balance set. */ /* Now we can consider balance set. */
if (!reconnected) { if (!reconnected) {
@@ -1984,8 +1969,8 @@ static bool peer_start_channeld(struct peer *peer,
if (hsmfd < 0) if (hsmfd < 0)
fatal("Could not read fd from HSM: %s", strerror(errno)); fatal("Could not read fd from HSM: %s", strerror(errno));
peer_set_owner(peer, new_peer_subd(peer->ld, channel_set_owner(channel, new_channel_subd(ld,
"lightning_channeld", peer, "lightning_channeld", channel,
channel_wire_type_name, channel_wire_type_name,
channel_msg, channel_msg,
take(&peer_fd), take(&peer_fd),
@@ -2300,8 +2285,7 @@ static unsigned int opening_negotiation_failed(struct subd *openingd,
{ {
struct crypto_state cs; struct crypto_state cs;
u64 gossip_index; u64 gossip_index;
struct peer *peer = openingd->peer; struct channel *channel = openingd->channel;
struct channel *channel = peer2channel(peer);
char *why; char *why;
/* We need the peer fd and gossip fd. */ /* We need the peer fd and gossip fd. */
@@ -2316,16 +2300,18 @@ static unsigned int opening_negotiation_failed(struct subd *openingd,
return 0; 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); NULL);
subd_send_msg(openingd->ld->gossip, take(msg)); subd_send_msg(openingd->ld->gossip, take(msg));
subd_send_fd(openingd->ld->gossip, fds[0]); subd_send_fd(openingd->ld->gossip, fds[0]);
subd_send_fd(openingd->ld->gossip, fds[1]); 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 */ /* This will free openingd, since that's peer->owner */
free_channel(peer2channel(peer), why); free_channel(channel, why);
return 0; return 0;
} }
@@ -2356,12 +2342,13 @@ static void peer_accept_channel(struct lightningd *ld,
assert(channel == peer2channel(peer)); assert(channel == peer2channel(peer));
assert(peer == channel2peer(channel)); assert(peer == channel2peer(channel));
peer_set_condition(peer, UNINITIALIZED, OPENINGD); channel_set_state(channel, UNINITIALIZED, OPENINGD);
peer_set_owner(peer, channel_set_owner(channel,
new_peer_subd(ld, "lightning_openingd", peer, new_channel_subd(ld, "lightning_openingd", channel,
opening_wire_type_name, opening_wire_type_name,
opening_negotiation_failed, opening_negotiation_failed,
take(&peer_fd), take(&gossip_fd), NULL)); take(&peer_fd), take(&gossip_fd),
NULL));
if (!channel->owner) { if (!channel->owner) {
channel_fail_transient(channel, channel_fail_transient(channel,
"Failed to subdaemon opening: %s", "Failed to subdaemon opening: %s",
@@ -2436,13 +2423,14 @@ static void peer_offer_channel(struct lightningd *ld,
channel->funding_satoshi = fc->funding_satoshi; channel->funding_satoshi = fc->funding_satoshi;
channel->push_msat = fc->push_msat; channel->push_msat = fc->push_msat;
peer_set_condition(fc->peer, UNINITIALIZED, OPENINGD); channel_set_state(channel, UNINITIALIZED, OPENINGD);
peer_set_owner(fc->peer, channel_set_owner(channel,
new_peer_subd(ld, new_channel_subd(ld,
"lightning_openingd", fc->peer, "lightning_openingd", channel,
opening_wire_type_name, opening_wire_type_name,
opening_negotiation_failed, opening_negotiation_failed,
take(&peer_fd), take(&gossip_fd), NULL)); take(&peer_fd), take(&gossip_fd),
NULL));
if (!channel->owner) { if (!channel->owner) {
fc->peer = tal_free(fc->peer); fc->peer = tal_free(fc->peer);
command_fail(fc->cmd, command_fail(fc->cmd,

View File

@@ -103,7 +103,7 @@ void activate_peers(struct lightningd *ld);
void drop_to_chain(struct lightningd *ld, struct channel *channel); 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. */ /* Get range of feerates to insist other side abide by for normal channels. */
u32 feerate_min(struct lightningd *ld); u32 feerate_min(struct lightningd *ld);

View File

@@ -334,7 +334,7 @@ static void rcvd_htlc_reply(struct subd *subd, const u8 *msg, const int *fds,
&hout->key.id, &hout->key.id,
&failure_code, &failure_code,
&failurestr)) { &failurestr)) {
channel_internal_error(peer2channel(subd->peer), channel_internal_error(subd->channel,
"Bad channel_offer_htlc_reply"); "Bad channel_offer_htlc_reply");
tal_free(hout); tal_free(hout);
return; 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) if (find_htlc_out(&subd->ld->htlcs_out, hout->key.peer, hout->key.id)
|| hout->key.id == HTLC_INVALID_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 "Bad offer_htlc_reply HTLC id %"PRIu64
" is a duplicate", " is a duplicate",
hout->key.id); hout->key.id);
@@ -688,19 +688,21 @@ static bool peer_fulfilled_our_htlc(struct channel *channel,
return true; 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_map_iter outi;
struct htlc_out *hout; struct htlc_out *hout;
struct sha256 payment_hash; struct sha256 payment_hash;
struct lightningd *ld = channel->peer->ld;
sha256(&payment_hash, preimage, sizeof(*preimage)); sha256(&payment_hash, preimage, sizeof(*preimage));
/* FIXME: use db to look this up! */ /* 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;
hout = htlc_out_map_next(&peer->ld->htlcs_out, &outi)) { hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
if (hout->key.peer != peer) if (hout->key.peer != channel2peer(channel))
continue; continue;
if (!structeq(&hout->payment_hash, &payment_hash)) 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 may have already fulfilled before going onchain, or
* we can fulfill onchain multiple times. */ * we can fulfill onchain multiple times. */
if (!hout->preimage) 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 keep going: this is something of a leak, but onchain
* we have no real way of distinguishing HTLCs anyway */ * 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! */ /* 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) const struct ripemd160 *ripemd)
{ {
struct htlc_out_map_iter outi; struct htlc_out_map_iter outi;
struct htlc_out *hout; 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;
hout = htlc_out_map_next(&peer->ld->htlcs_out, &outi)) { hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
struct ripemd160 hash; struct ripemd160 hash;
if (hout->key.peer != peer) if (hout->key.peer != channel2peer(channel))
continue; continue;
ripemd160(&hash, ripemd160(&hash,
@@ -770,11 +773,11 @@ struct htlc_out *find_htlc_out_by_ripemd(const struct peer *peer,
return NULL; 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 struct htlc_stub *htlc,
const char *why) 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! */ /* Don't fail twice! */
if (hout->failuremsg || hout->failcode) if (hout->failuremsg || hout->failcode)
@@ -783,7 +786,7 @@ void onchain_failed_our_htlc(const struct peer *peer,
hout->failcode = WIRE_PERMANENT_CHANNEL_FAILURE; hout->failcode = WIRE_PERMANENT_CHANNEL_FAILURE;
if (!hout->in) { if (!hout->in) {
char *localfail = tal_fmt(peer, "%s: %s", char *localfail = tal_fmt(channel, "%s: %s",
onion_type_name(WIRE_PERMANENT_CHANNEL_FAILURE), onion_type_name(WIRE_PERMANENT_CHANNEL_FAILURE),
why); why);
payment_failed(hout->key.peer->ld, hout, localfail); 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; return true;
} }
void peer_sending_commitsig(struct peer *peer, const u8 *msg) void peer_sending_commitsig(struct channel *channel, const u8 *msg)
{ {
u64 commitnum; u64 commitnum;
u32 feerate; 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; size_t i, maxid = 0, num_local_added = 0;
secp256k1_ecdsa_signature commit_sig; secp256k1_ecdsa_signature commit_sig;
secp256k1_ecdsa_signature *htlc_sigs; secp256k1_ecdsa_signature *htlc_sigs;
struct channel *channel = peer2channel(peer); struct lightningd *ld = channel->peer->ld;
if (!fromwire_channel_sending_commitsig(msg, msg, NULL, if (!fromwire_channel_sending_commitsig(msg, msg, NULL,
&commitnum, &commitnum,
@@ -959,12 +962,12 @@ void peer_sending_commitsig(struct peer *peer, const u8 *msg)
&changed_htlcs, &changed_htlcs,
&commit_sig, &htlc_sigs)) { &commit_sig, &htlc_sigs)) {
channel_internal_error(channel, "bad channel_sending_commitsig %s", channel_internal_error(channel, "bad channel_sending_commitsig %s",
tal_hex(peer, msg)); tal_hex(channel, msg));
return; return;
} }
for (i = 0; i < tal_count(changed_htlcs); i++) { 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_internal_error(channel,
"channel_sending_commitsig: update failed"); "channel_sending_commitsig: update failed");
return; return;
@@ -980,31 +983,31 @@ void peer_sending_commitsig(struct peer *peer, const u8 *msg)
} }
if (num_local_added != 0) { 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_internal_error(channel,
"channel_sending_commitsig:" "channel_sending_commitsig:"
" Added %"PRIu64", maxid now %"PRIu64 " Added %"PRIu64", maxid now %"PRIu64
" from %"PRIu64, " from %"PRIu64,
num_local_added, maxid, peer2channel(peer)->next_htlc_id); num_local_added, maxid, channel->next_htlc_id);
return; return;
} }
peer2channel(peer)->next_htlc_id += num_local_added; channel->next_htlc_id += num_local_added;
} }
/* Update their feerate. */ /* 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; return;
/* Last was commit. */ /* Last was commit. */
peer2channel(peer)->last_was_revoke = false; channel->last_was_revoke = false;
tal_free(peer2channel(peer)->last_sent_commit); tal_free(channel->last_sent_commit);
peer2channel(peer)->last_sent_commit = tal_steal(peer, changed_htlcs); channel->last_sent_commit = tal_steal(channel, changed_htlcs);
wallet_channel_save(peer->ld->wallet, peer2channel(peer)); wallet_channel_save(ld->wallet, channel);
/* Tell it we've got it, and to go ahead with commitment_signed. */ /* 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))); 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 */ /* 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; u64 commitnum;
u32 feerate; u32 feerate;
@@ -1097,7 +1100,7 @@ void peer_got_commitsig(struct peer *peer, const u8 *msg)
struct changed_htlc *changed; struct changed_htlc *changed;
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
size_t i; size_t i;
struct channel *channel = peer2channel(peer); struct lightningd *ld = channel->peer->ld;
if (!fromwire_channel_got_commitsig(msg, msg, NULL, if (!fromwire_channel_got_commitsig(msg, msg, NULL,
&commitnum, &commitnum,
@@ -1112,11 +1115,11 @@ void peer_got_commitsig(struct peer *peer, const u8 *msg)
&tx)) { &tx)) {
channel_internal_error(channel, channel_internal_error(channel,
"bad fromwire_channel_got_commitsig %s", "bad fromwire_channel_got_commitsig %s",
tal_hex(peer, msg)); tal_hex(channel, msg));
return; return;
} }
log_debug(peer->log, log_debug(channel->log,
"got commitsig %"PRIu64 "got commitsig %"PRIu64
": feerate %u, %zu added, %zu fulfilled, %zu failed, %zu changed", ": feerate %u, %zu added, %zu fulfilled, %zu failed, %zu changed",
commitnum, feerate, tal_count(added), tal_count(fulfilled), 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++) { 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, channel_internal_error(channel,
"got_commitsig: update failed"); "got_commitsig: update failed");
return; return;
@@ -1157,14 +1160,14 @@ void peer_got_commitsig(struct peer *peer, const u8 *msg)
if (!peer_sending_revocation(channel, added, fulfilled, failed, changed)) if (!peer_sending_revocation(channel, added, fulfilled, failed, changed))
return; return;
if (!peer_save_commitsig_received(peer, commitnum, tx, &commit_sig)) if (!peer_save_commitsig_received(channel2peer(channel), commitnum, tx, &commit_sig))
return; return;
wallet_channel_save(peer->ld->wallet, channel); wallet_channel_save(ld->wallet, channel);
tal_free(channel->last_htlc_sigs); tal_free(channel->last_htlc_sigs);
channel->last_htlc_sigs = tal_steal(channel, 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); channel->last_htlc_sigs);
/* Tell it we've committed, and to go ahead with revoke. */ /* 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; 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; u64 revokenum;
struct sha256 per_commitment_secret; struct sha256 per_commitment_secret;
struct pubkey next_per_commitment_point; struct pubkey next_per_commitment_point;
struct changed_htlc *changed; struct changed_htlc *changed;
enum onion_type *failcodes; enum onion_type *failcodes;
struct channel *channel = peer2channel(peer);
size_t i; size_t i;
struct lightningd *ld = channel->peer->ld;
if (!fromwire_channel_got_revoke(msg, msg, NULL, if (!fromwire_channel_got_revoke(msg, msg, NULL,
&revokenum, &per_commitment_secret, &revokenum, &per_commitment_secret,
&next_per_commitment_point, &next_per_commitment_point,
&changed)) { &changed)) {
channel_internal_error(channel, "bad fromwire_channel_got_revoke %s", channel_internal_error(channel, "bad fromwire_channel_got_revoke %s",
tal_hex(peer, msg)); tal_hex(channel, msg));
return; return;
} }
log_debug(peer->log, log_debug(channel->log,
"got revoke %"PRIu64": %zu changed", "got revoke %"PRIu64": %zu changed",
revokenum, tal_count(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++) { for (i = 0; i < tal_count(changed); i++) {
/* If we're doing final accept, we need to forward */ /* If we're doing final accept, we need to forward */
if (changed[i].newstate == RCVD_ADD_ACK_REVOCATION) { 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])) &failcodes[i]))
return; return;
} else { } else {
if (!changed_htlc(peer, &changed[i])) { if (!changed_htlc(channel2peer(channel), &changed[i])) {
channel_internal_error(channel, channel_internal_error(channel,
"got_revoke: update failed"); "got_revoke: update failed");
return; 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 * A receiving node MAY fail if the `per_commitment_secret` was not
* generated by the protocol in [BOLT #3] * 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, &channel->their_shachain,
shachain_index(revokenum), shachain_index(revokenum),
&per_commitment_secret)) { &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 */ /* 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. */ /* Tell it we've committed, and to go ahead with revoke. */
msg = towire_channel_got_revoke_reply(msg); 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. */ /* These are all errors before finding next hop. */
assert(!(failcodes[i] & UPDATE)); 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); 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) static void *tal_arr_append_(void **p, size_t size)

View File

@@ -28,9 +28,9 @@ void peer_htlcs(const tal_t *ctx,
const struct failed_htlc ***failed_htlcs, const struct failed_htlc ***failed_htlcs,
enum side **failed_sides); enum side **failed_sides);
void peer_sending_commitsig(struct peer *peer, const u8 *msg); void peer_sending_commitsig(struct channel *channel, const u8 *msg);
void peer_got_commitsig(struct peer *peer, const u8 *msg); void peer_got_commitsig(struct channel *channel, const u8 *msg);
void peer_got_revoke(struct peer *peer, const u8 *msg); void peer_got_revoke(struct channel *channel, const u8 *msg);
void update_per_commit_point(struct peer *peer, void update_per_commit_point(struct peer *peer,
const struct pubkey *per_commitment_point); 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_in *in,
struct htlc_out **houtp); 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); 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 struct htlc_stub *htlc,
const char *why); 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 */ #endif /* LIGHTNING_LIGHTNINGD_PEER_HTLCS_H */

View File

@@ -10,6 +10,7 @@
#include <common/gen_status_wire.h> #include <common/gen_status_wire.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <lightningd/channel.h>
#include <lightningd/lightningd.h> #include <lightningd/lightningd.h>
#include <lightningd/log.h> #include <lightningd/log.h>
#include <lightningd/log_status.h> #include <lightningd/log_status.h>
@@ -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) 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; struct channel_id channel_id;
char *desc; 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! */ /* FIXME: if not all channels failed, hand back to gossipd! */
/* Don't free sd; we're may be about to free peer. */ /* Don't free sd; we're may be about to free channel. */
sd->peer = NULL; sd->channel = NULL;
channel_fail_permanent(peer2channel(peer), channel_fail_permanent(channel,
"%s: received ERROR %s", sd->name, desc); "%s: received ERROR %s", sd->name, desc);
return true; return true;
} }
static bool handle_sent_errmsg(struct subd *sd, const u8 *msg) 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; struct channel_id channel_id;
char *desc; char *desc;
u8 *errmsg; u8 *errmsg;
@@ -417,12 +418,12 @@ static bool handle_sent_errmsg(struct subd *sd, const u8 *msg)
return false; return false;
/* FIXME: if not all channels failed, hand back to gossipd! */ /* FIXME: if not all channels failed, hand back to gossipd! */
if (!peer2channel(sd->peer)->error) if (!channel->error)
peer2channel(sd->peer)->error = tal_steal(sd->peer, errmsg); channel->error = tal_steal(channel, errmsg);
/* Don't free sd; we're may be about to free peer. */ /* Don't free sd; we're may be about to free channel. */
sd->peer = NULL; sd->channel = NULL;
channel_fail_permanent(peer2channel(peer), channel_fail_permanent(channel,
"%s: sent ERROR %s", sd->name, desc); "%s: sent ERROR %s", sd->name, desc);
return true; return true;
} }
@@ -470,18 +471,18 @@ static struct io_plan *sd_msg_read(struct io_conn *conn, struct subd *sd)
goto malformed; goto malformed;
goto close; goto close;
case WIRE_STATUS_PEER_CONNECTION_LOST: case WIRE_STATUS_PEER_CONNECTION_LOST:
if (!sd->peer) if (!sd->channel)
goto malformed; goto malformed;
log_info(sd->log, "Peer connection lost"); log_info(sd->log, "Peer connection lost");
goto close; goto close;
case WIRE_STATUS_RECEIVED_ERRMSG: case WIRE_STATUS_RECEIVED_ERRMSG:
if (!sd->peer) if (!sd->channel)
goto malformed; goto malformed;
if (!handle_received_errmsg(sd, sd->msg_in)) if (!handle_received_errmsg(sd, sd->msg_in))
goto malformed; goto malformed;
goto close; goto close;
case WIRE_STATUS_SENT_ERRMSG: case WIRE_STATUS_SENT_ERRMSG:
if (!sd->peer) if (!sd->channel)
goto malformed; goto malformed;
if (!handle_sent_errmsg(sd, sd->msg_in)) if (!handle_sent_errmsg(sd, sd->msg_in))
goto malformed; goto malformed;
@@ -570,19 +571,19 @@ static void destroy_subd(struct subd *sd)
sd->conn = tal_free(sd->conn); sd->conn = tal_free(sd->conn);
/* Peer still attached? */ /* Peer still attached? */
if (sd->peer) { if (sd->channel) {
/* Don't loop back when we fail it. */ /* 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; struct db *db = sd->ld->wallet->db;
bool outer_transaction; bool outer_transaction;
sd->peer = NULL; sd->channel = NULL;
/* We can be freed both inside msg handling, or spontaneously. */ /* We can be freed both inside msg handling, or spontaneously. */
outer_transaction = db->in_transaction; outer_transaction = db->in_transaction;
if (!outer_transaction) if (!outer_transaction)
db_begin_transaction(db); db_begin_transaction(db);
channel_fail_transient(peer2channel(peer), channel_fail_transient(channel,
"Owning subdaemon %s died (%i)", "Owning subdaemon %s died (%i)",
sd->name, status); sd->name, status);
if (!outer_transaction) 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, static struct subd *new_subd(struct lightningd *ld,
const char *name, const char *name,
struct peer *peer, struct channel *channel,
const char *(*msgname)(int msgtype), const char *(*msgname)(int msgtype),
unsigned int (*msgcb)(struct subd *, unsigned int (*msgcb)(struct subd *,
const u8 *, const int *fds), const u8 *, const int *fds),
@@ -648,12 +649,9 @@ static struct subd *new_subd(struct lightningd *ld,
return tal_free(sd); return tal_free(sd);
} }
sd->ld = ld; sd->ld = ld;
if (peer) { if (channel) {
/* FIXME: Use minimal unique pubkey prefix for logs! */ sd->log = new_log(sd, channel->peer->log_book, "%s-%s", name,
const char *idstr = type_to_string(peer, struct pubkey, log_prefix(channel->log));
&peer->id);
sd->log = new_log(sd, peer->log_book, "%s(%s):", name, idstr);
tal_free(idstr);
} else { } else {
sd->log = new_log(sd, ld->log_book, "%s(%u):", name, sd->pid); 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); msg_queue_init(&sd->outq, sd);
tal_add_destructor(sd, destroy_subd); tal_add_destructor(sd, destroy_subd);
list_head_init(&sd->reqs); list_head_init(&sd->reqs);
sd->peer = peer; sd->channel = channel;
/* conn actually owns daemon: we die when it does. */ /* conn actually owns daemon: we die when it does. */
sd->conn = io_new_conn(ld, msg_fd, msg_setup, sd); 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; return sd;
} }
struct subd *new_peer_subd(struct lightningd *ld, struct subd *new_channel_subd(struct lightningd *ld,
const char *name, const char *name,
struct peer *peer, struct channel *channel,
const char *(*msgname)(int msgtype), const char *(*msgname)(int msgtype),
unsigned int (*msgcb)(struct subd *, const u8 *, unsigned int (*msgcb)(struct subd *, const u8 *,
const int *fds), ...) const int *fds), ...)
@@ -706,7 +704,7 @@ struct subd *new_peer_subd(struct lightningd *ld,
struct subd *sd; struct subd *sd;
va_start(ap, msgcb); 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); va_end(ap);
return sd; return sd;
} }
@@ -766,12 +764,12 @@ void subd_shutdown(struct subd *sd, unsigned int seconds)
tal_free(sd); 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 is a per-peer-daemon, and not already freeing itself... */
if (owner->peer) { if (owner->channel) {
assert(owner->peer == peer); assert(owner->channel == channel);
owner->peer = NULL; owner->channel = NULL;
tal_free(owner); tal_free(owner);
} }
} }

View File

@@ -25,8 +25,8 @@ struct subd {
/* Connection. */ /* Connection. */
struct io_conn *conn; struct io_conn *conn;
/* If we are associated with a single peer, this points to it. */ /* If we are associated with a single channel, this points to it. */
struct peer *peer; struct channel *channel;
/* For logging */ /* For logging */
struct log *log; 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 * @ld: global state
* @name: basename of daemon * @name: basename of daemon
* @peer: peer to associate. * @channel: channel to associate.
* @msgname: function to get name from messages * @msgname: function to get name from messages
* @msgcb: function to call (inside db transaction) when non-fatal message received (or NULL) * @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... * @...: NULL-terminated list of pointers to fds to hand as fd 3, 4...
@@ -86,9 +86,9 @@ struct subd *new_global_subd(struct lightningd *ld,
* that many @fds are received before calling again. If it returns -1, the * that many @fds are received before calling again. If it returns -1, the
* subdaemon is shutdown. * subdaemon is shutdown.
*/ */
struct subd *new_peer_subd(struct lightningd *ld, struct subd *new_channel_subd(struct lightningd *ld,
const char *name, const char *name,
struct peer *peer, struct channel *channel,
const char *(*msgname)(int msgtype), const char *(*msgname)(int msgtype),
unsigned int (*msgcb)(struct subd *, const u8 *, unsigned int (*msgcb)(struct subd *, const u8 *,
const int *fds), const int *fds),
@@ -144,14 +144,14 @@ void subd_req_(const tal_t *ctx,
void *replycb_data); void *replycb_data);
/** /**
* subd_release_peer - shut down a subdaemon which no longer owns the peer. * subd_release_channel - shut down a subdaemon which no longer owns the channel.
* @owner: subd which owned peer. * @owner: subd which owned channel.
* @peer: peer to release. * @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. * 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. * subd_shutdown - try to politely shut down a subdaemon.

View File

@@ -29,7 +29,7 @@ int debug_poll(struct pollfd *fds UNNEEDED, nfds_t nfds UNNEEDED, int timeout UN
void fatal(const char *fmt UNNEEDED, ...) void fatal(const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "fatal called!\n"); abort(); } { fprintf(stderr, "fatal called!\n"); abort(); }
/* Generated stub for free_htlcs */ /* 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(); } { fprintf(stderr, "free_htlcs called!\n"); abort(); }
/* Generated stub for gossip_init */ /* Generated stub for gossip_init */
void gossip_init(struct lightningd *ld UNNEEDED) void gossip_init(struct lightningd *ld UNNEEDED)

View File

@@ -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) bool extract_channel_id(const u8 *in_pkt UNNEEDED, struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "extract_channel_id called!\n"); abort(); } { fprintf(stderr, "extract_channel_id called!\n"); abort(); }
/* Generated stub for find_htlc_out_by_ripemd */ /* 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) const struct ripemd160 *ripemd160 UNNEEDED)
{ fprintf(stderr, "find_htlc_out_by_ripemd called!\n"); abort(); } { fprintf(stderr, "find_htlc_out_by_ripemd called!\n"); abort(); }
/* Generated stub for find_txwatch */ /* 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 */ /* Generated stub for logv_add */
void logv_add(struct log *log UNNEEDED, const char *fmt UNNEEDED, va_list ap UNNEEDED) void logv_add(struct log *log UNNEEDED, const char *fmt UNNEEDED, va_list ap UNNEEDED)
{ fprintf(stderr, "logv_add called!\n"); abort(); } { fprintf(stderr, "logv_add called!\n"); abort(); }
/* Generated stub for new_json_result */ /* Generated stub for new_channel_subd */
struct json_result *new_json_result(const tal_t *ctx UNNEEDED) struct subd *new_channel_subd(struct lightningd *ld 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, const char *name UNNEEDED,
struct peer *peer UNNEEDED, struct channel *channel UNNEEDED,
const char *(*msgname)(int msgtype) UNNEEDED, const char *(*msgname)(int msgtype) UNNEEDED,
unsigned int (*msgcb)(struct subd * UNNEEDED, const u8 * UNNEEDED, unsigned int (*msgcb)(struct subd * UNNEEDED, const u8 * UNNEEDED,
const int *fds) UNNEEDED, const int *fds) UNNEEDED,
...) ...)
{ fprintf(stderr, "new_peer_subd called!\n"); abort(); } { 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 null_response */ /* Generated stub for null_response */
struct json_result *null_response(const tal_t *ctx UNNEEDED) struct json_result *null_response(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "null_response called!\n"); abort(); } { fprintf(stderr, "null_response called!\n"); abort(); }
/* Generated stub for onchain_failed_our_htlc */ /* 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 struct htlc_stub *htlc UNNEEDED,
const char *why UNNEEDED) const char *why UNNEEDED)
{ fprintf(stderr, "onchain_failed_our_htlc called!\n"); abort(); } { fprintf(stderr, "onchain_failed_our_htlc called!\n"); abort(); }
/* Generated stub for onchain_fulfilled_htlc */ /* 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(); } { fprintf(stderr, "onchain_fulfilled_htlc called!\n"); abort(); }
/* Generated stub for onchain_wire_type_name */ /* Generated stub for onchain_wire_type_name */
const char *onchain_wire_type_name(int e UNNEEDED) 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) const char *opening_wire_type_name(int e UNNEEDED)
{ fprintf(stderr, "opening_wire_type_name called!\n"); abort(); } { fprintf(stderr, "opening_wire_type_name called!\n"); abort(); }
/* Generated stub for peer_got_commitsig */ /* 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(); } { fprintf(stderr, "peer_got_commitsig called!\n"); abort(); }
/* Generated stub for peer_got_revoke */ /* 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(); } { fprintf(stderr, "peer_got_revoke called!\n"); abort(); }
/* Generated stub for peer_htlcs */ /* Generated stub for peer_htlcs */
void peer_htlcs(const tal_t *ctx UNNEEDED, 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) enum side **failed_sides UNNEEDED)
{ fprintf(stderr, "peer_htlcs called!\n"); abort(); } { fprintf(stderr, "peer_htlcs called!\n"); abort(); }
/* Generated stub for peer_sending_commitsig */ /* 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(); } { fprintf(stderr, "peer_sending_commitsig called!\n"); abort(); }
/* Generated stub for sanitize_error */ /* Generated stub for sanitize_error */
char *sanitize_error(const tal_t *ctx UNNEEDED, const u8 *errmsg UNNEEDED, char *sanitize_error(const tal_t *ctx UNNEEDED, const u8 *errmsg UNNEEDED,
struct channel_id *channel_id UNNEEDED) struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "sanitize_error called!\n"); abort(); } { fprintf(stderr, "sanitize_error called!\n"); abort(); }
/* Generated stub for subd_release_peer */ /* Generated stub for subd_release_channel */
void subd_release_peer(struct subd *owner UNNEEDED, struct peer *peer UNNEEDED) void subd_release_channel(struct subd *owner UNNEEDED, struct channel *channel UNNEEDED)
{ fprintf(stderr, "subd_release_peer called!\n"); abort(); } { fprintf(stderr, "subd_release_channel called!\n"); abort(); }
/* Generated stub for subd_req_ */ /* Generated stub for subd_req_ */
void subd_req_(const tal_t *ctx UNNEEDED, void subd_req_(const tal_t *ctx UNNEEDED,
struct subd *sd UNNEEDED, struct subd *sd UNNEEDED,