Browse Source

peer: cache txid for commitment_tx.

Minor efficiency and simplification.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
abf4182ef5
  1. 3
      daemon/packets.c
  2. 41
      daemon/peer.c
  3. 3
      daemon/peer.h

3
daemon/packets.c

@ -315,12 +315,14 @@ void queue_pkt_commit(struct peer *peer)
ci->cstate = copy_cstate(ci, peer->remote.staging_cstate); ci->cstate = copy_cstate(ci, peer->remote.staging_cstate);
ci->tx = create_commit_tx(ci, peer, &ci->revocation_hash, ci->tx = create_commit_tx(ci, peer, &ci->revocation_hash,
ci->cstate, REMOTE, &ci->map); ci->cstate, REMOTE, &ci->map);
bitcoin_txid(ci->tx, &ci->txid);
log_debug(peer->log, "Signing tx for %u/%u msatoshis, %zu/%zu htlcs", log_debug(peer->log, "Signing tx for %u/%u msatoshis, %zu/%zu htlcs",
ci->cstate->side[OURS].pay_msat, ci->cstate->side[OURS].pay_msat,
ci->cstate->side[THEIRS].pay_msat, ci->cstate->side[THEIRS].pay_msat,
tal_count(ci->cstate->side[OURS].htlcs), tal_count(ci->cstate->side[OURS].htlcs),
tal_count(ci->cstate->side[THEIRS].htlcs)); tal_count(ci->cstate->side[THEIRS].htlcs));
log_add_struct(peer->log, " (txid %s)", struct sha256_double, &ci->txid);
/* BOLT #2: /* BOLT #2:
* *
@ -819,6 +821,7 @@ Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt)
ci->cstate = copy_cstate(ci, peer->local.staging_cstate); ci->cstate = copy_cstate(ci, peer->local.staging_cstate);
ci->tx = create_commit_tx(ci, peer, &ci->revocation_hash, ci->tx = create_commit_tx(ci, peer, &ci->revocation_hash,
ci->cstate, LOCAL, &ci->map); ci->cstate, LOCAL, &ci->map);
bitcoin_txid(ci->tx, &ci->txid);
/* BOLT #2: /* BOLT #2:
* *

41
daemon/peer.c

@ -585,7 +585,7 @@ static const struct bitcoin_tx *htlc_fulfill_tx(const struct peer *peer,
REMOTE); REMOTE);
tx->input[0].index = ci->map[i]; tx->input[0].index = ci->map[i];
bitcoin_txid(ci->tx, &tx->input[0].txid); tx->input[0].txid = ci->txid;
satoshis = htlc->msatoshis / 1000; satoshis = htlc->msatoshis / 1000;
tx->input[0].amount = tal_dup(tx->input, u64, &satoshis); tx->input[0].amount = tal_dup(tx->input, u64, &satoshis);
tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime); tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime);
@ -1349,22 +1349,13 @@ static enum watch_result anchor_depthchange(struct peer *peer,
return KEEP_WATCHING; return KEEP_WATCHING;
} }
/* Yay, segwit! We can just compare txids, even though we don't have both
* signatures. */
static bool txidmatch(const struct bitcoin_tx *tx,
const struct sha256_double *txid)
{
struct sha256_double tx_txid;
bitcoin_txid(tx, &tx_txid);
return structeq(txid, &tx_txid);
}
static struct commit_info *find_commit(struct commit_info *ci, static struct commit_info *find_commit(struct commit_info *ci,
const struct sha256_double *txid) const struct sha256_double *txid)
{ {
while (ci) { while (ci) {
if (txidmatch(ci->tx, txid)) /* Yay, segwit! We can just compare txids, even
* though we don't have both signatures. */
if (structeq(txid, &ci->txid))
return ci; return ci;
ci = ci->prev; ci = ci->prev;
} }
@ -1426,7 +1417,7 @@ static const struct bitcoin_tx *htlc_timeout_tx(const struct peer *peer,
/* We must set locktime so HTLC expiry can OP_CHECKLOCKTIMEVERIFY */ /* We must set locktime so HTLC expiry can OP_CHECKLOCKTIMEVERIFY */
tx->lock_time = htlc->expiry.locktime; tx->lock_time = htlc->expiry.locktime;
tx->input[0].index = 0; tx->input[0].index = 0;
bitcoin_txid(ci->tx, &tx->input[0].txid); tx->input[0].txid = ci->txid;
satoshis = htlc->msatoshis / 1000; satoshis = htlc->msatoshis / 1000;
tx->input[0].amount = tal_dup(tx->input, u64, &satoshis); tx->input[0].amount = tal_dup(tx->input, u64, &satoshis);
tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime); tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime);
@ -2163,7 +2154,7 @@ static enum watch_result anchor_spent(struct peer *peer,
err = pkt_err(peer, "Unilateral close tx seen"); err = pkt_err(peer, "Unilateral close tx seen");
resolve_their_unilateral(peer); resolve_their_unilateral(peer);
} }
} else if (txidmatch(peer->local.commit->tx, &txid)) { } else if (structeq(&peer->local.commit->txid, &txid)) {
newstate = STATE_CLOSE_ONCHAIN_OUR_UNILATERAL; newstate = STATE_CLOSE_ONCHAIN_OUR_UNILATERAL;
/* We're almost certainly closed to them by now. */ /* We're almost certainly closed to them by now. */
err = pkt_err(peer, "Our own unilateral close tx seen"); err = pkt_err(peer, "Our own unilateral close tx seen");
@ -2425,7 +2416,7 @@ const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
/* Now, create transaction to spend it. */ /* Now, create transaction to spend it. */
tx = bitcoin_tx(peer, 1, 1); tx = bitcoin_tx(peer, 1, 1);
bitcoin_txid(commit, &tx->input[0].txid); tx->input[0].txid = peer->local.commit->txid;
p2wsh_out = find_p2wsh_out(commit, witnessscript); p2wsh_out = find_p2wsh_out(commit, witnessscript);
tx->input[0].index = p2wsh_out; tx->input[0].index = p2wsh_out;
tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime); tx->input[0].sequence_number = bitcoin_nsequence(&peer->remote.locktime);
@ -2802,14 +2793,14 @@ bool setup_first_commit(struct peer *peer)
if (!peer->local.commit->cstate) if (!peer->local.commit->cstate)
return false; return false;
peer->remote.commit->cstate = initial_cstate(peer, peer->remote.commit->cstate = initial_cstate(peer,
peer->anchor.satoshis, peer->anchor.satoshis,
peer->remote.commit_fee_rate, peer->remote.commit_fee_rate,
peer->local.offer_anchor peer->local.offer_anchor
== CMD_OPEN_WITH_ANCHOR ? == CMD_OPEN_WITH_ANCHOR ?
OURS : THEIRS); OURS : THEIRS);
if (!peer->remote.commit->cstate) if (!peer->remote.commit->cstate)
return false; return false;
peer->local.commit->tx = create_commit_tx(peer->local.commit, peer->local.commit->tx = create_commit_tx(peer->local.commit,
peer, peer,
@ -2817,6 +2808,7 @@ bool setup_first_commit(struct peer *peer)
peer->local.commit->cstate, peer->local.commit->cstate,
LOCAL, LOCAL,
&peer->local.commit->map); &peer->local.commit->map);
bitcoin_txid(peer->local.commit->tx, &peer->local.commit->txid);
peer->remote.commit->tx = create_commit_tx(peer->remote.commit, peer->remote.commit->tx = create_commit_tx(peer->remote.commit,
peer, peer,
@ -2824,6 +2816,7 @@ bool setup_first_commit(struct peer *peer)
peer->remote.commit->cstate, peer->remote.commit->cstate,
REMOTE, REMOTE,
&peer->remote.commit->map); &peer->remote.commit->map);
bitcoin_txid(peer->remote.commit->tx, &peer->remote.commit->txid);
peer->local.staging_cstate = copy_cstate(peer, peer->local.commit->cstate); peer->local.staging_cstate = copy_cstate(peer, peer->local.commit->cstate);
peer->remote.staging_cstate = copy_cstate(peer, peer->remote.commit->cstate); peer->remote.staging_cstate = copy_cstate(peer, peer->remote.commit->cstate);

3
daemon/peer.h

@ -62,8 +62,9 @@ struct commit_info {
u64 commit_num; u64 commit_num;
/* Revocation hash. */ /* Revocation hash. */
struct sha256 revocation_hash; struct sha256 revocation_hash;
/* Commit tx. */ /* Commit tx & txid */
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
struct sha256_double txid;
/* Channel state for this tx. */ /* Channel state for this tx. */
struct channel_state *cstate; struct channel_state *cstate;
/* Other side's signature for last commit tx (if known) */ /* Other side's signature for last commit tx (if known) */

Loading…
Cancel
Save