lightningd/opening: fix theoretical race.

We should start watching for the transaction before we send the
signature; we might miss it otherwise.  In practice, we only see
transactions as they enter a block, so it won't happen, but be
thorough.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-03-07 11:35:03 +10:30
parent 9eeb76e185
commit 5f07e8405a
3 changed files with 80 additions and 36 deletions

View File

@ -656,6 +656,20 @@ static u8 *recv_channel(struct state *state, const struct points *ours,
peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_BAD_PARAM,
"could not create channel with given config");
/* Now, ask master to watch. */
status_trace("asking master to watch funding %s",
type_to_string(trc, struct sha256_double, &state->funding_txid));
msg = towire_opening_accept_resp(state, &state->funding_txid);
wire_sync_write(REQ_FD, msg);
msg = wire_sync_read(state, REQ_FD);
if (!fromwire_opening_accept_finish(msg, NULL))
status_failed(WIRE_OPENING_BAD_PARAM,
"Expected valid opening_accept_finish: %s",
tal_hex(trc, msg));
status_trace("master said to finish");
/* BOLT #2:
*
* The recipient MUST fail the channel if `signature` is incorrect.
@ -703,17 +717,16 @@ static u8 *recv_channel(struct state *state, const struct points *ours,
peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_WRITE_FAILED,
"Writing funding_signed");
return towire_opening_accept_resp(state,
&state->funding_txid,
state->funding_txout,
state->remoteconf,
&theirsig,
&state->cs,
&theirs.funding_pubkey,
&theirs.revocation_basepoint,
&theirs.payment_basepoint,
&theirs.delayed_payment_basepoint,
&state->next_per_commit[REMOTE]);
return towire_opening_accept_finish_resp(state,
state->funding_txout,
state->remoteconf,
&theirsig,
&state->cs,
&theirs.funding_pubkey,
&theirs.revocation_basepoint,
&theirs.payment_basepoint,
&theirs.delayed_payment_basepoint,
&state->next_per_commit[REMOTE]);
}
#ifndef TESTING

View File

@ -44,18 +44,23 @@ opening_accept,4,max_feerate,4
opening_accept,8,len,2
opening_accept,10,msg,len,u8
# This gives the txid of their funding tx: we're done.
# This gives the txid of their funding tx to watch.
opening_accept_resp,103
opening_accept_resp,0,funding_txid,32,struct sha256_double
opening_accept_resp,32,funding_txout,2,u16
opening_accept_resp,33,their_config,36,struct channel_config
opening_accept_resp,69,first_commit_sig,64,secp256k1_ecdsa_signature
opening_accept_resp,133,crypto_state,144,struct crypto_state
opening_accept_resp,277,remote_fundingkey,33
opening_accept_resp,310,revocation_basepoint,33
opening_accept_resp,343,payment_basepoint,33
opening_accept_resp,376,delayed_payment_basepoint,33
opening_accept_resp,409,their_per_commit_point,33
# Acknowledge watch is in place, now can send sig.
opening_accept_finish,4
opening_accept_finish_resp,104
opening_accept_finish_resp,32,funding_txout,2,u16
opening_accept_finish_resp,0,their_config,36,struct channel_config
opening_accept_finish_resp,36,first_commit_sig,64,secp256k1_ecdsa_signature
opening_accept_finish_resp,100,crypto_state,144,struct crypto_state
opening_accept_finish_resp,244,remote_fundingkey,33
opening_accept_finish_resp,277,revocation_basepoint,33
opening_accept_finish_resp,310,payment_basepoint,33
opening_accept_finish_resp,343,delayed_payment_basepoint,33
opening_accept_finish_resp,377,their_per_commit_point,33
# You're OK to exit.
opening_exit_req,99

1 #include <lightningd/cryptomsg.h>
44 opening_accept_resp,32,funding_txout,2,u16 # Acknowledge watch is in place, now can send sig.
45 opening_accept_resp,33,their_config,36,struct channel_config opening_accept_finish,4
46 opening_accept_resp,69,first_commit_sig,64,secp256k1_ecdsa_signature opening_accept_finish_resp,104
47 opening_accept_resp,133,crypto_state,144,struct crypto_state opening_accept_finish_resp,32,funding_txout,2,u16
48 opening_accept_resp,277,remote_fundingkey,33 opening_accept_finish_resp,0,their_config,36,struct channel_config
49 opening_accept_resp,310,revocation_basepoint,33 opening_accept_finish_resp,36,first_commit_sig,64,secp256k1_ecdsa_signature
50 opening_accept_resp,343,payment_basepoint,33 opening_accept_finish_resp,100,crypto_state,144,struct crypto_state
51 opening_accept_resp,376,delayed_payment_basepoint,33 opening_accept_finish_resp,244,remote_fundingkey,33
52 opening_accept_resp,409,their_per_commit_point,33 opening_accept_finish_resp,277,revocation_basepoint,33
53 # You're OK to exit. opening_accept_finish_resp,310,payment_basepoint,33
54 opening_exit_req,99 opening_accept_finish_resp,343,delayed_payment_basepoint,33
55 opening_accept_finish_resp,377,their_per_commit_point,33
56 # You're OK to exit.
57 opening_exit_req,99
58
59
60
61
62
63
64
65
66

View File

@ -531,13 +531,15 @@ static enum watch_result funding_depth_cb(struct peer *peer,
const struct sha256_double *txid,
void *unused)
{
const char *txidstr = type_to_string(peer, struct sha256_double, txid);
log_debug(peer->log, "Funding tx %s depth %u of %u",
txidstr, depth, peer->ld->dstate.config.anchor_confirms);
if (depth >= peer->ld->dstate.config.anchor_confirms) {
peer_set_condition(peer, "Funding tx reached depth %u", depth);
/* FIXME! Start channel proper... */
return DELETE_WATCH;
}
log_debug(peer->log, "Funding tx depth %u of %u", depth,
peer->ld->dstate.config.anchor_confirms);
return KEEP_WATCHING;
}
@ -642,10 +644,10 @@ static void opening_gen_funding(struct subdaemon *opening, const u8 *resp,
opening_release_tx, fc);
}
static void opening_accept_response(struct subdaemon *opening, const u8 *resp,
struct peer *peer)
static void opening_accept_finish_response(struct subdaemon *opening,
const u8 *resp,
struct peer *peer)
{
struct sha256_double funding_txid;
u16 funding_txout;
struct channel_config their_config;
secp256k1_ecdsa_signature first_commit_sig;
@ -654,23 +656,47 @@ static void opening_accept_response(struct subdaemon *opening, const u8 *resp,
payment_basepoint, delayed_payment_basepoint,
their_per_commit_point;
log_debug(peer->log, "Got opening_accept_response");
if (!fromwire_opening_accept_resp(resp, NULL,
&funding_txid, &funding_txout,
&their_config, &first_commit_sig,
&crypto_state, &remote_fundingkey,
&revocation_basepoint,
&payment_basepoint,
&delayed_payment_basepoint,
&their_per_commit_point)) {
log_broken(peer->log, "bad OPENING_ACCEPT_RESP %s",
log_debug(peer->log, "Got opening_accept_finish_response");
if (!fromwire_opening_accept_finish_resp(resp, NULL,
&funding_txout,
&their_config,
&first_commit_sig,
&crypto_state,
&remote_fundingkey,
&revocation_basepoint,
&payment_basepoint,
&delayed_payment_basepoint,
&their_per_commit_point)) {
log_broken(peer->log, "bad OPENING_ACCEPT_FINISH_RESP %s",
tal_hex(resp, resp));
tal_free(peer);
return;
}
/* FIXME: Start normal channel daemon... */
}
static void opening_accept_response(struct subdaemon *opening, const u8 *resp,
struct peer *peer)
{
struct sha256_double funding_txid;
if (!fromwire_opening_accept_resp(resp, NULL, &funding_txid)) {
log_broken(peer->log, "bad OPENING_ACCEPT_RESP %s",
tal_hex(resp, resp));
tal_free(peer);
return;
}
log_debug(peer->log, "Watching funding tx %s",
type_to_string(resp, struct sha256_double, &funding_txid));
watch_txid(peer, peer->ld->topology, peer, &funding_txid,
funding_depth_cb, NULL);
/* Tell it we're watching. */
subdaemon_req(peer->owner, towire_opening_accept_finish(resp),
-1, NULL,
opening_accept_finish_response, peer);
}
static void channel_config(struct lightningd *ld,