Browse Source

peer: fix mutual close detection.

Since 43729c6856 (protocol: add output script to close_clearing message.)
the close scripts are not p2sh, but arbitrary.  Fix the close tx matching.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
4fe90ee3f5
  1. 43
      daemon/peer.c

43
daemon/peer.c

@ -399,6 +399,8 @@ static struct peer *new_peer(struct lightningd_state *dstate,
peer->anchor.watches = NULL;
peer->cur_commit.watch = NULL;
peer->closing.their_sig = NULL;
peer->closing.our_script = NULL;
peer->closing.their_script = NULL;
peer->cleared = INPUT_NONE;
/* Make it different from other node (to catch bugs!), but a
* round number for simple eyeballing. */
@ -708,35 +710,34 @@ static bool is_unrevoked_commit(const struct commit_info *ci,
return false;
}
/* A mutual close is a simple 2 output p2sh to the final addresses, but
* without knowing fee we can't determine order, so examine each output. */
static bool is_mutual_close(const struct peer *peer,
const struct bitcoin_tx *tx)
{
const u8 *ctx, *our_p2sh, *their_p2sh;
bool matches;
const u8 *ours, *theirs;
if (tx->output_count != 2)
ours = peer->closing.our_script;
theirs = peer->closing.their_script;
/* If we don't know the closing scripts, can't have signed them. */
if (!ours || !theirs)
return false;
if (!is_p2sh(tx->output[0].script, tx->output[0].script_length)
|| !is_p2sh(tx->output[1].script, tx->output[1].script_length))
if (tx->output_count != 2)
return false;
/* FIXME: Cache these! */
ctx = tal(NULL, u8);
our_p2sh = scriptpubkey_p2sh(ctx,
bitcoin_redeem_single(tx, &peer->us.finalkey));
their_p2sh = scriptpubkey_p2sh(ctx,
bitcoin_redeem_single(tx, &peer->them.finalkey));
matches =
(memcmp(tx->output[0].script, our_p2sh, tal_count(our_p2sh)) == 0
&& memcmp(tx->output[1].script, their_p2sh, tal_count(their_p2sh)) == 0)
|| (memcmp(tx->output[0].script, their_p2sh, tal_count(their_p2sh)) == 0
&& memcmp(tx->output[1].script, our_p2sh, tal_count(our_p2sh)) == 0);
tal_free(ctx);
return matches;
/* Without knowing fee amounts, can't determine order. Check both. */
if (scripteq(tx->output[0].script, tx->output[0].script_length,
ours, tal_count(ours))
&& scripteq(tx->output[1].script, tx->output[1].script_length,
theirs, tal_count(theirs)))
return true;
if (scripteq(tx->output[0].script, tx->output[0].script_length,
theirs, tal_count(theirs))
&& scripteq(tx->output[1].script, tx->output[1].script_length,
ours, tal_count(ours)))
return true;
return false;
}
static void close_depth_cb(struct peer *peer, int depth,

Loading…
Cancel
Save