Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add information about how many blocks to go until funding is confirmed #2405

Merged
merged 1 commit into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- JSON API: `newaddr` outputs `bech32` or `p2sh-segwit`, or both with new `all` parameter (#2390)
- JSON API: `listpeers` status now shows how many confirmations until channel is open (#2405)

### Changed

Expand Down
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
trueptolemy marked this conversation as resolved.
Show resolved Hide resolved
channel_funding_depth,,depth,u32

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

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

/* Additional confirmations need for local lockin. */
u32 depth_togo;
};

static u8 *create_channel_announcement(const tal_t *ctx, struct peer *peer);
Expand All @@ -165,8 +168,9 @@ 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.";
funding_status = tal_fmt(tmpctx,
"Funding needs %d confirmations to reach lockin.",
peer->depth_togo);
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,36 +2385,52 @@ 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->depth_togo). */
static void handle_funding_depth(struct peer *peer, const u8 *msg)
{
unsigned int depth;
u32 depth;
struct short_channel_id *scid;

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

/* Too late, we're shutting down! */
if (peer->shutdown_sent[LOCAL])
return;

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;
}
if (depth < peer->channel->minimum_depth) {
trueptolemy marked this conversation as resolved.
Show resolved Hide resolved
peer->depth_togo = peer->channel->minimum_depth - depth;

peer->announce_depth_reached = (depth >= ANNOUNCE_MIN_DEPTH);
} else {
peer->depth_togo = 0;

/* Send temporary or final announcements */
channel_announcement_negotiate(peer);
assert(scid);
trueptolemy marked this conversation as resolved.
Show resolved Hide resolved
peer->short_channel_ids[LOCAL] = *scid;

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;
}

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

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

billboard_update(peer);
}
Expand Down Expand Up @@ -2633,8 +2653,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 +2740,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 +2752,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 @@ -2773,8 +2795,9 @@ static void init_channel(struct peer *peer)
&funding_signed,
&peer->announce_depth_reached,
&last_remote_per_commit_secret,
&peer->localfeatures))
master_badmsg(WIRE_CHANNEL_INIT, msg);
&peer->localfeatures)) {
master_badmsg(WIRE_CHANNEL_INIT, msg);
}

status_trace("init %s: remote_per_commit = %s, old_remote_per_commit = %s"
" next_idx_local = %"PRIu64
Expand Down Expand Up @@ -2804,7 +2827,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 @@ -2841,6 +2866,9 @@ static void init_channel(struct peer *peer)
if (peer->channel->funder == LOCAL)
peer->desired_feerate = feerate_per_kw[REMOTE];

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

/* OK, now we can process peer messages. */
if (reconnected)
peer_reconnect(peer, &last_remote_per_commit_secret);
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
21 changes: 14 additions & 7 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,32 +393,38 @@ 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)
{
const char *txidstr;

txidstr = type_to_string(tmpctx, struct bitcoin_txid, txid);

/* If not awaiting lockin/announce, it doesn't care any more */
if (channel->state != CHANNELD_AWAITING_LOCKIN
&& channel->state != CHANNELD_NORMAL) {
log_debug(channel->log,
"Funding tx confirmed, but peer in state %s",
channel_state_name(channel));
"Funding tx %s confirmed, but peer in state %s",
txidstr, channel_state_name(channel));
return true;
}

if (!channel->owner) {
log_debug(channel->log,
"Funding tx confirmed, but peer disconnected");
"Funding tx %s confirmed, but peer disconnected",
txidstr);
return false;
}

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
Loading