Skip to content

Commit

Permalink
fix:Add information about how many blocks to go until funding is conf…
Browse files Browse the repository at this point in the history
…irmed

1. Rename channel_funding_locked to channel_funding_depth in
channeld/channel_wire.csv.
2. Add minimum_depth in struct channel in common/initial_channel.h and
change corresponding init function: new_initial_channel().
3. Add confirmation_needed in struct peer in channeld/channeld.c.
4. Rename channel_tell_funding_locked to channel_tell_depth.
5. Call channel_tell_depth even if depth < minimum, and still call lockin_complete in channel_tell_depth, iff depth > minimum_depth.
6. channeld ignore the channel_funding_depth unless its >
minimum_depth(except to update billboard, and set
peer->confirmation_needed = minimum_depth - depth).
  • Loading branch information
trueptolemy committed Feb 27, 2019
1 parent 7ca0000 commit 5d363dc
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 50 deletions.
9 changes: 5 additions & 4 deletions channeld/channel_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ channel_init,,chain_hash,struct bitcoin_blkid
channel_init,,funding_txid,struct bitcoin_txid
channel_init,,funding_txout,u16
channel_init,,funding_satoshi,struct amount_sat
channel_init,,minimum_depth,u32
channel_init,,our_config,struct channel_config
channel_init,,their_config,struct channel_config
# FIXME: Fix generate-wire.py to allow NUM_SIDES*u32 here.
Expand Down Expand Up @@ -62,10 +63,10 @@ channel_init,,last_remote_secret,struct secret
channel_init,,lflen,u16
channel_init,,localfeatures,lflen*u8

# master->channeld funding hit new depth >= lock depth
channel_funding_locked,1002
channel_funding_locked,,short_channel_id,struct short_channel_id
channel_funding_locked,,depth,u32
# master->channeld funding hit new depth(funding locked if >= lock depth)
channel_funding_depth,1002
channel_funding_depth,,short_channel_id,?struct short_channel_id
channel_funding_depth,,depth,u32

# Tell channel to offer this htlc
channel_offer_htlc,1004
Expand Down
89 changes: 63 additions & 26 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ struct peer {

/* Make sure peer is live. */
struct timeabs last_recv;

/* Must be 0 when funding locked locally. In fact, it keeps
* watch over the changes on local topology.
* Because of network delay, it doesn't correspond to remote
* locking information. */
u32 confirmation_needed;
};

static u8 *create_channel_announcement(const tal_t *ctx, struct peer *peer);
Expand All @@ -164,9 +170,15 @@ static void billboard_update(const struct peer *peer)

if (peer->funding_locked[LOCAL] && peer->funding_locked[REMOTE])
funding_status = "Funding transaction locked.";
else if (!peer->funding_locked[LOCAL] && !peer->funding_locked[REMOTE])
/* FIXME: Say how many blocks to go! */
funding_status = "Funding needs more confirmations.";
else if (!peer->funding_locked[LOCAL] && !peer->funding_locked[REMOTE]) {
if(peer->funding_locked[LOCAL])
funding_status = "Funding transation locked locally,"
" waiting for remote reply";
else
funding_status = tal_fmt(tmpctx,
"Funding needs %d confirmations.",
peer->confirmation_needed);
}
else if (peer->funding_locked[LOCAL] && !peer->funding_locked[REMOTE])
funding_status = "We've confirmed funding, they haven't yet.";
else if (!peer->funding_locked[LOCAL] && peer->funding_locked[REMOTE])
Expand Down Expand Up @@ -2381,38 +2393,56 @@ static void peer_reconnect(struct peer *peer,
}
}

/* Funding has locked in, and reached depth. */
static void handle_funding_locked(struct peer *peer, const u8 *msg)
/* ignores the funding_depth unless depth >= minimum_depth
* (except to update billboard, and set peer->confirmation_needed). */
static void handle_funding_depth(struct peer *peer, const u8 *msg)
{
unsigned int depth;
struct short_channel_id *scid;

if (!fromwire_channel_funding_locked(msg,
&peer->short_channel_ids[LOCAL],
if (!fromwire_channel_funding_depth(msg,
&scid,
&depth))
master_badmsg(WIRE_CHANNEL_FUNDING_LOCKED, msg);
master_badmsg(WIRE_CHANNEL_FUNDING_DEPTH, msg);

/* Too late, we're shutting down! */
if (peer->shutdown_sent[LOCAL])
return;
if (depth < peer->channel->minimum_depth) {
if (peer->funding_locked[LOCAL] || peer->confirmation_needed == 0)
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"funding shouldn't be locked");
peer->confirmation_needed = peer->channel->minimum_depth - depth;

if (!peer->funding_locked[LOCAL]) {
status_trace("funding_locked: sending commit index %"PRIu64": %s",
peer->next_index[LOCAL],
type_to_string(tmpctx, struct pubkey,
&peer->next_local_per_commit));
msg = towire_funding_locked(NULL,
&peer->channel_id,
&peer->next_local_per_commit);
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
peer->funding_locked[LOCAL] = true;
}
billboard_update(peer);
} else {
peer->short_channel_ids[LOCAL] = *scid;
if (!peer->short_channel_ids[LOCAL])
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"funding channel id should be drived");
if (!peer->funding_locked[LOCAL]) {
if (peer->confirmation_needed == 0)
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"funding should be locked");
status_trace("funding_locked: sending commit index %"PRIu64": %s",
peer->next_index[LOCAL],
type_to_string(tmpctx, struct pubkey,
&peer->next_local_per_commit));
msg = towire_funding_locked(NULL,
&peer->channel_id,
&peer->next_local_per_commit);
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
peer->funding_locked[LOCAL] = true;
peer->confirmation_needed = 0;
}

peer->announce_depth_reached = (depth >= ANNOUNCE_MIN_DEPTH);
peer->announce_depth_reached = (depth >= ANNOUNCE_MIN_DEPTH);

/* Send temporary or final announcements */
channel_announcement_negotiate(peer);
/* Send temporary or final announcements */
channel_announcement_negotiate(peer);

billboard_update(peer);
billboard_update(peer);
}
}

static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
Expand Down Expand Up @@ -2633,8 +2663,8 @@ static void req_in(struct peer *peer, const u8 *msg)
enum channel_wire_type t = fromwire_peektype(msg);

switch (t) {
case WIRE_CHANNEL_FUNDING_LOCKED:
handle_funding_locked(peer, msg);
case WIRE_CHANNEL_FUNDING_DEPTH:
handle_funding_depth(peer, msg);
return;
case WIRE_CHANNEL_OFFER_HTLC:
handle_offer_htlc(peer, msg);
Expand Down Expand Up @@ -2720,6 +2750,7 @@ static void init_channel(struct peer *peer)
u8 *funding_signed;
const u8 *msg;
u32 feerate_per_kw[NUM_SIDES];
u32 minimum_depth;
struct secret last_remote_per_commit_secret;

assert(!(fcntl(MASTER_FD, F_GETFL) & O_NONBLOCK));
Expand All @@ -2731,6 +2762,7 @@ static void init_channel(struct peer *peer)
&peer->chain_hash,
&funding_txid, &funding_txout,
&funding,
&minimum_depth,
&conf[LOCAL], &conf[REMOTE],
feerate_per_kw,
&peer->feerate_min, &peer->feerate_max,
Expand Down Expand Up @@ -2804,7 +2836,9 @@ static void init_channel(struct peer *peer)

peer->channel = new_full_channel(peer,
&peer->chain_hash,
&funding_txid, funding_txout,
&funding_txid,
funding_txout,
minimum_depth,
funding,
local_msat,
feerate_per_kw,
Expand Down Expand Up @@ -2849,6 +2883,9 @@ static void init_channel(struct peer *peer)
if (funding_signed)
sync_crypto_write(&peer->cs, PEER_FD, take(funding_signed));

/* from now we need keep watch over WIRE_CHANNEL_FUNDING_DEPTH */
peer->confirmation_needed = minimum_depth;

/* Reenable channel */
channel_announcement_negotiate(peer);

Expand Down
2 changes: 2 additions & 0 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct channel *new_full_channel(const tal_t *ctx,
const struct bitcoin_blkid *chain_hash,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u32 minimum_depth,
struct amount_sat funding,
struct amount_msat local_msat,
const u32 feerate_per_kw[NUM_SIDES],
Expand All @@ -41,6 +42,7 @@ struct channel *new_full_channel(const tal_t *ctx,
chain_hash,
funding_txid,
funding_txout,
minimum_depth,
funding,
local_msat,
feerate_per_kw[LOCAL],
Expand Down
3 changes: 3 additions & 0 deletions channeld/full_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
/**
* new_full_channel: Given initial fees and funding, what is initial state?
* @ctx: tal context to allocate return value from.
* @chain_hash: Which blockchain are we talking about?
* @funding_txid: The commitment transaction id.
* @funding_txout: The commitment transaction output number.
* @minimum_depth: The minimum confirmations needed for funding transaction.
* @funding: The commitment transaction amount.
* @local_msat: The amount for the local side (remainder goes to remote)
* @feerate_per_kw: feerate per kiloweight (satoshis) for the commitment
Expand All @@ -30,6 +32,7 @@ struct channel *new_full_channel(const tal_t *ctx,
const struct bitcoin_blkid *chain_hash,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u32 minimum_depth,
struct amount_sat funding,
struct amount_msat local_msat,
const u32 feerate_per_kw[NUM_SIDES],
Expand Down
4 changes: 2 additions & 2 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ int main(void)
feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 15000;
lchannel = new_full_channel(tmpctx,
&chainparams->genesis_blockhash,
&funding_txid, funding_output_index,
&funding_txid, funding_output_index, 0,
funding_amount, to_local,
feerate_per_kw,
local_config,
Expand All @@ -464,7 +464,7 @@ int main(void)
LOCAL);
rchannel = new_full_channel(tmpctx,
&chainparams->genesis_blockhash,
&funding_txid, funding_output_index,
&funding_txid, funding_output_index, 0,
funding_amount, to_remote,
feerate_per_kw,
remote_config,
Expand Down
2 changes: 2 additions & 0 deletions common/initial_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct channel *new_initial_channel(const tal_t *ctx,
const struct bitcoin_blkid *chain_hash,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u32 minimum_depth,
struct amount_sat funding,
struct amount_msat local_msatoshi,
u32 feerate_per_kw,
Expand All @@ -29,6 +30,7 @@ struct channel *new_initial_channel(const tal_t *ctx,
channel->funding_txid = *funding_txid;
channel->funding_txout = funding_txout;
channel->funding = funding;
channel->minimum_depth = minimum_depth;
if (!amount_sat_sub_msat(&remote_msatoshi,
channel->funding, local_msatoshi))
return tal_free(channel);
Expand Down
5 changes: 5 additions & 0 deletions common/initial_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ struct channel {
/* satoshis in from commitment tx */
struct amount_sat funding;

/* confirmations needed for locking funding */
u32 minimum_depth;

/* Who is paying fees. */
enum side funder;

Expand Down Expand Up @@ -69,6 +72,7 @@ struct channel {
* @chain_hash: Which blockchain are we talking about?
* @funding_txid: The commitment transaction id.
* @funding_txout: The commitment transaction output number.
* @minimum_depth: The minimum confirmations needed for funding transaction.
* @funding_satoshis: The commitment transaction amount.
* @local_msatoshi: The amount for the local side (remainder goes to remote)
* @feerate_per_kw: feerate per kiloweight (satoshis) for the commitment
Expand All @@ -87,6 +91,7 @@ struct channel *new_initial_channel(const tal_t *ctx,
const struct bitcoin_blkid *chain_hash,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u32 minimum_depth,
struct amount_sat funding,
struct amount_msat local_msatoshi,
u32 feerate_per_kw,
Expand Down
10 changes: 6 additions & 4 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)

/* And we never get these from channeld. */
case WIRE_CHANNEL_INIT:
case WIRE_CHANNEL_FUNDING_LOCKED:
case WIRE_CHANNEL_FUNDING_DEPTH:
case WIRE_CHANNEL_OFFER_HTLC:
case WIRE_CHANNEL_FULFILL_HTLC:
case WIRE_CHANNEL_FAIL_HTLC:
Expand Down Expand Up @@ -340,6 +340,7 @@ void peer_start_channeld(struct channel *channel,
&channel->funding_txid,
channel->funding_outnum,
channel->funding,
channel->minimum_depth,
&channel->our_config,
&channel->channel_info.their_config,
channel->channel_info.feerate_per_kw,
Expand Down Expand Up @@ -392,7 +393,7 @@ void peer_start_channeld(struct channel *channel,
try_update_feerates(ld, channel);
}

bool channel_tell_funding_locked(struct lightningd *ld,
bool channel_tell_depth(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *txid,
u32 depth)
Expand All @@ -413,11 +414,12 @@ bool channel_tell_funding_locked(struct lightningd *ld,
}

subd_send_msg(channel->owner,
take(towire_channel_funding_locked(NULL, channel->scid,
take(towire_channel_funding_depth(NULL, channel->scid,
depth)));

if (channel->remote_funding_locked
&& channel->state == CHANNELD_AWAITING_LOCKIN)
&& channel->state == CHANNELD_AWAITING_LOCKIN
&& depth >= channel->minimum_depth)
lockin_complete(channel);

return true;
Expand Down
2 changes: 1 addition & 1 deletion lightningd/channel_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void peer_start_channeld(struct channel *channel,
bool reconnected);

/* Returns true if subd told, otherwise false. */
bool channel_tell_funding_locked(struct lightningd *ld,
bool channel_tell_depth(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *txid,
u32 depth);
Expand Down
13 changes: 7 additions & 6 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
plugin_hook_call_peer_connected(ld, hook_payload, hook_payload);
}

static enum watch_result funding_lockin_cb(struct lightningd *ld,
static enum watch_result funding_depth_cb(struct lightningd *ld,
struct channel *channel,
const struct bitcoin_txid *txid,
unsigned int depth)
Expand All @@ -868,11 +868,10 @@ static enum watch_result funding_lockin_cb(struct lightningd *ld,
txidstr, depth, channel->minimum_depth);
tal_free(txidstr);

if (depth < channel->minimum_depth)
return KEEP_WATCHING;
bool local_locked = depth >= channel->minimum_depth;

/* If we restart, we could already have peer->scid from database */
if (!channel->scid) {
if (local_locked && !channel->scid) {
struct txlocator *loc;

loc = wallet_transaction_locate(tmpctx, ld->wallet, txid);
Expand All @@ -891,9 +890,11 @@ static enum watch_result funding_lockin_cb(struct lightningd *ld,
}

/* Try to tell subdaemon */
if (!channel_tell_funding_locked(ld, channel, txid, depth))
if (!channel_tell_depth(ld, channel, txid, depth))
return KEEP_WATCHING;

if (!local_locked)
return KEEP_WATCHING;
/* BOLT #7:
*
* A node:
Expand Down Expand Up @@ -932,7 +933,7 @@ void channel_watch_funding(struct lightningd *ld, struct channel *channel)
{
/* FIXME: Remove arg from cb? */
watch_txid(channel, ld->topology, channel,
&channel->funding_txid, funding_lockin_cb);
&channel->funding_txid, funding_depth_cb);
watch_txo(channel, ld->topology, channel,
&channel->funding_txid, channel->funding_outnum,
funding_spent);
Expand Down
6 changes: 3 additions & 3 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ void broadcast_tx(struct chain_topology *topo UNNEEDED,
int exitstatus UNNEEDED,
const char *err))
{ fprintf(stderr, "broadcast_tx called!\n"); abort(); }
/* Generated stub for channel_tell_funding_locked */
bool channel_tell_funding_locked(struct lightningd *ld UNNEEDED,
/* Generated stub for channel_tell_depth */
bool channel_tell_depth(struct lightningd *ld UNNEEDED,
struct channel *channel UNNEEDED,
const struct bitcoin_txid *txid UNNEEDED,
u32 depth UNNEEDED)
{ fprintf(stderr, "channel_tell_funding_locked called!\n"); abort(); }
{ fprintf(stderr, "channel_tell_depth called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, int code UNNEEDED,
const char *fmt UNNEEDED, ...)
Expand Down
Loading

0 comments on commit 5d363dc

Please sign in to comment.