Browse Source

gossip: Simplify announce_signature exchange

The logic of dispatching the announcement_signatures message was
distributed over several places and daemons. This aims to simplify it
by moving it all into `channeld`, making peer_control only report
announcement depth to `channeld`, which then takes care of the
rest. We also do not reuse the funding_locked tx watcher since it is
easier to just fire off a new watcher with the specific purpose of
waiting for the announcement_depth.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 8 years ago
committed by Rusty Russell
parent
commit
b0c0e28a43
  1. 60
      channeld/channel.c
  2. 29
      lightningd/peer_control.c

60
channeld/channel.c

@ -133,6 +133,8 @@ struct peer {
struct changed_htlc *last_sent_commit; struct changed_htlc *last_sent_commit;
u64 revocations_received; u64 revocations_received;
u8 channel_flags; u8 channel_flags;
bool announce_depth_reached;
}; };
static u8 *create_channel_announcement(const tal_t *ctx, struct peer *peer); static u8 *create_channel_announcement(const tal_t *ctx, struct peer *peer);
@ -168,13 +170,25 @@ static void send_announcement_signatures(struct peer *peer)
{ {
/* First 2 + 256 byte are the signatures and msg type, skip them */ /* First 2 + 256 byte are the signatures and msg type, skip them */
size_t offset = 258; size_t offset = 258;
const tal_t *tmpctx = tal_tmpctx(peer); const tal_t *tmpctx;
struct sha256_double hash; struct sha256_double hash;
u8 *msg; u8 *msg, *ca, *req;
u8 *ca = create_channel_announcement(tmpctx, peer);
u8 *req = towire_hsm_cannouncement_sig_req(tmpctx, /* BOLT #7:
&peer->channel->funding_pubkey[LOCAL], *
ca); * If sent, `announcement_signatures` messages MUST NOT be sent until
* `funding_locked` has been sent, and the funding transaction is has
* at least 6 confirmations.
*/
if (!(peer->announce_depth_reached && peer->funding_locked[LOCAL]))
return;
tmpctx = tal_tmpctx(peer);
status_trace("Exchanging announcement signatures.");
ca = create_channel_announcement(tmpctx, peer);
req = towire_hsm_cannouncement_sig_req(
tmpctx, &peer->channel->funding_pubkey[LOCAL], ca);
if (!wire_sync_write(HSM_FD, req)) if (!wire_sync_write(HSM_FD, req))
status_failed(WIRE_CHANNEL_HSM_FAILED, status_failed(WIRE_CHANNEL_HSM_FAILED,
@ -331,9 +345,20 @@ static struct io_plan *handle_peer_funding_locked(struct io_conn *conn,
take(towire_channel_normal_operation(peer))); take(towire_channel_normal_operation(peer)));
} }
send_announcement_signatures(peer);
return peer_read_message(conn, &peer->pcs, peer_in); return peer_read_message(conn, &peer->pcs, peer_in);
} }
static void announce_channel(struct peer *peer)
{
send_channel_announcement(peer);
send_channel_update(peer, false);
/* Tell the master that we just announced the channel,
* so it may announce the node */
daemon_conn_send(&peer->master, take(towire_channel_announced(peer)));
}
static struct io_plan *handle_peer_announcement_signatures(struct io_conn *conn, static struct io_plan *handle_peer_announcement_signatures(struct io_conn *conn,
struct peer *peer, struct peer *peer,
const u8 *msg) const u8 *msg)
@ -362,13 +387,8 @@ static struct io_plan *handle_peer_announcement_signatures(struct io_conn *conn,
peer->have_sigs[REMOTE] = true; peer->have_sigs[REMOTE] = true;
/* We have the remote sigs, do we have the local ones as well? */ /* We have the remote sigs, do we have the local ones as well? */
if (peer->funding_locked[LOCAL] && peer->have_sigs[LOCAL]) { if (peer->funding_locked[LOCAL] && peer->have_sigs[LOCAL])
send_channel_announcement(peer); announce_channel(peer);
send_channel_update(peer, false);
/* Tell the master that we just announced the channel,
* so it may announce the node */
daemon_conn_send(&peer->master, take(towire_channel_announced(msg)));
}
return peer_read_message(conn, &peer->pcs, peer_in); return peer_read_message(conn, &peer->pcs, peer_in);
} }
@ -1667,20 +1687,13 @@ static void handle_funding_locked(struct peer *peer, const u8 *msg)
static void handle_funding_announce_depth(struct peer *peer, const u8 *msg) static void handle_funding_announce_depth(struct peer *peer, const u8 *msg)
{ {
if (peer->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL) { peer->announce_depth_reached = true;
status_trace("Exchanging announcement signatures.");
send_announcement_signatures(peer); send_announcement_signatures(peer);
}
/* Only send the announcement and update if the other end gave /* Only send the announcement and update if the other end gave
* us its sig */ * us its sig */
if (peer->have_sigs[REMOTE]) { if (peer->have_sigs[REMOTE])
send_channel_announcement(peer); announce_channel(peer);
send_channel_update(peer, false);
/* Tell the master that we just announced the channel,
* so it may announce the node */
daemon_conn_send(&peer->master, take(towire_channel_announced(msg)));
}
} }
static void handle_offer_htlc(struct peer *peer, const u8 *inmsg) static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
@ -2171,6 +2184,7 @@ int main(int argc, char *argv[])
timers_init(&peer->timers, time_mono()); timers_init(&peer->timers, time_mono());
peer->commit_timer = NULL; peer->commit_timer = NULL;
peer->have_sigs[LOCAL] = peer->have_sigs[REMOTE] = false; peer->have_sigs[LOCAL] = peer->have_sigs[REMOTE] = false;
peer->announce_depth_reached = false;
peer->handle_master_reply = NULL; peer->handle_master_reply = NULL;
peer->master_reply_type = 0; peer->master_reply_type = 0;
msg_queue_init(&peer->master_deferred, peer); msg_queue_init(&peer->master_deferred, peer);

29
lightningd/peer_control.c

@ -6,6 +6,7 @@
#include <ccan/fdpass/fdpass.h> #include <ccan/fdpass/fdpass.h>
#include <ccan/io/io.h> #include <ccan/io/io.h>
#include <ccan/noerr/noerr.h> #include <ccan/noerr/noerr.h>
#include <ccan/str/str.h>
#include <ccan/take/take.h> #include <ccan/take/take.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <channeld/gen_channel_wire.h> #include <channeld/gen_channel_wire.h>
@ -1011,13 +1012,13 @@ static enum watch_result funding_announce_cb(struct peer *peer,
if (depth < ANNOUNCE_MIN_DEPTH) { if (depth < ANNOUNCE_MIN_DEPTH) {
return KEEP_WATCHING; return KEEP_WATCHING;
} }
if (peer->state != CHANNELD_NORMAL || !peer->owner) {
if (!peer->owner || !streq(peer->owner->name, "lightning_channeld")) {
log_debug(peer->ld->log, log_debug(peer->ld->log,
"Funding tx announce ready, but peer state %s %s", "Funding tx announce ready, but peer is not owned by channeld");
peer_state_name(peer->state),
peer->owner ? peer->owner->name : "unowned");
return KEEP_WATCHING; return KEEP_WATCHING;
} }
subd_send_msg(peer->owner, subd_send_msg(peer->owner,
take(towire_channel_funding_announce_depth(peer))); take(towire_channel_funding_announce_depth(peer)));
return DELETE_WATCH; return DELETE_WATCH;
@ -1350,20 +1351,16 @@ static enum watch_result funding_lockin_cb(struct peer *peer,
if (!(peer->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL)) if (!(peer->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL))
return DELETE_WATCH; return DELETE_WATCH;
/* BOLT #7: /* Tell channeld that we have reached the announce_depth and
* * that it may send the announcement_signatures upon receiving
* If sent, `announcement_signatures` messages MUST NOT be sent until * funding_locked, or right now if it already received it
* `funding_locked` has been sent, and the funding transaction is has * before. If we are at the right depth, call the callback
* at least 6 confirmations. * directly, otherwise schedule a callback */
*/ if (depth >= ANNOUNCE_MIN_DEPTH)
if (depth >= ANNOUNCE_MIN_DEPTH && peer_ready) { funding_announce_cb(peer, tx, depth, NULL);
subd_send_msg(peer->owner, else
take(towire_channel_funding_announce_depth(peer)));
} else {
/* Worst case, we'll send next block. */
watch_txid(peer, peer->ld->topology, peer, &txid, watch_txid(peer, peer->ld->topology, peer, &txid,
funding_announce_cb, NULL); funding_announce_cb, NULL);
}
return DELETE_WATCH; return DELETE_WATCH;
} }

Loading…
Cancel
Save