From d2c626820fa261c0ddd1c1a866a4ef4cc1ef7bb9 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 20 Mar 2017 17:09:12 +0100 Subject: [PATCH] channel: Wait for 6 confirmations before sending announcement sigs The protocol specifies that in order for an announcement to be valid, the channel has to have at least 6 confirmations. --- lightningd/channel/channel.c | 5 +++-- lightningd/channel/channel_wire.csv | 3 +++ lightningd/peer_control.c | 17 +++++++++++++++-- lightningd/peer_control.h | 3 +++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lightningd/channel/channel.c b/lightningd/channel/channel.c index 66250f151..cacd9a3e1 100644 --- a/lightningd/channel/channel.c +++ b/lightningd/channel/channel.c @@ -203,7 +203,6 @@ static struct io_plan *peer_in(struct io_conn *conn, struct peer *peer, u8 *msg) return peer_read_message(conn, &peer->pcs, peer_in); } - static struct io_plan *setup_peer_conn(struct io_conn *conn, struct peer *peer) { return io_duplex(conn, peer_read_message(conn, &peer->pcs, peer_in), @@ -273,13 +272,15 @@ static struct io_plan *req_in(struct io_conn *conn, struct daemon_conn *master) &peer->next_per_commit[LOCAL]); msg_enqueue(&peer->peer_out, take(msg)); peer->funding_locked[LOCAL] = true; - send_announcement_signatures(peer); if (peer->funding_locked[REMOTE]) { announce_channel(peer); daemon_conn_send(master, take(towire_channel_normal_operation(peer))); } + } else if(fromwire_channel_funding_announce_depth(master->msg_in, NULL)) { + status_trace("Exchanging announcement signatures."); + send_announcement_signatures(peer); } else status_failed(WIRE_CHANNEL_BAD_COMMAND, "%s", strerror(errno)); diff --git a/lightningd/channel/channel_wire.csv b/lightningd/channel/channel_wire.csv index e895feddb..1c4f36329 100644 --- a/lightningd/channel/channel_wire.csv +++ b/lightningd/channel/channel_wire.csv @@ -41,3 +41,6 @@ channel_init,562,remote_node_id,struct pubkey # Tx is deep enough, go! channel_funding_locked,2 channel_funding_locked,0,short_channel_id,struct short_channel_id + +# Tell the channel that we may announce the channel's existence +channel_funding_announce_depth,3 \ No newline at end of file diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index cbf721d86..3f12709f8 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -65,6 +65,7 @@ static struct peer *new_peer(struct lightningd *ld, peer->connect_cmd = cmd; peer->funding_txid = NULL; peer->seed = NULL; + peer->locked = false; /* Max 128k per peer. */ peer->log_book = new_log_book(peer, 128*1024, @@ -555,8 +556,19 @@ static enum watch_result funding_depth_cb(struct peer *peer, return KEEP_WATCHING; } - peer_set_condition(peer, "Funding tx reached depth %u", depth); - subd_send_msg(peer->owner, take(towire_channel_funding_locked(peer, &scid))); + /* Make sure we notify `channeld` just once. */ + if (!peer->locked) { + peer_set_condition(peer, "Funding tx reached depth %u", depth); + subd_send_msg(peer->owner, take(towire_channel_funding_locked(peer, &scid))); + peer->locked = true; + } + + /* With the above this is max(funding_depth, 6) before + * announcing the channel */ + if (depth < ANNOUNCE_MIN_DEPTH) { + return KEEP_WATCHING; + } + subd_send_msg(peer->owner, take(towire_channel_funding_announce_depth(peer))); return DELETE_WATCH; } @@ -626,6 +638,7 @@ static size_t update_channel_status(struct subd *sd, /* And we never get these from channeld. */ case WIRE_CHANNEL_INIT: case WIRE_CHANNEL_FUNDING_LOCKED: + case WIRE_CHANNEL_FUNDING_ANNOUNCE_DEPTH: break; } diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index aa6ea79c8..4574dd4e6 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -6,6 +6,8 @@ #include #include +#define ANNOUNCE_MIN_DEPTH 6 + struct crypto_state; struct peer { @@ -52,6 +54,7 @@ struct peer { /* Gossip client fd, forwarded to the respective owner */ int gossip_client_fd; + bool locked; }; struct peer *peer_by_unique_id(struct lightningd *ld, u64 unique_id);