Browse Source

gather_updates: extract some update logic into update_rhash.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 10 years ago
parent
commit
769f9f1cc5
  1. 69
      test-cli/gather_updates.c

69
test-cli/gather_updates.c

@ -23,13 +23,25 @@ static void check_preimage(const Sha256Hash *preimage,
errx(1, "Invalid preimage in %s!", file); errx(1, "Invalid preimage in %s!", file);
} }
static void get_rhash(const Sha256Hash *rhash, struct sha256 *old, static void update_rhash(const Sha256Hash *rhash,
struct sha256 *new) bool received,
size_t *num_updates,
struct sha256 *old_our_rhash,
struct sha256 *old_their_rhash,
struct sha256 *our_rhash,
struct sha256 *their_rhash)
{ {
if (new) { /* Update rhash (and save old one for checking) */
*old = *new; if (received) {
proto_to_sha256(rhash, new); *old_their_rhash = *their_rhash;
proto_to_sha256(rhash, their_rhash);
} else {
*old_our_rhash = *our_rhash;
proto_to_sha256(rhash, our_rhash);
} }
/* If they care, we count number of updates. */
if (num_updates)
(*num_updates)++;
} }
/* Takes complete update history, gets summary of last state. */ /* Takes complete update history, gets summary of last state. */
@ -43,7 +55,7 @@ struct channel_state *gather_updates(const tal_t *ctx,
struct signature *their_commit_sig) struct signature *their_commit_sig)
{ {
Signature *sig = NULL; Signature *sig = NULL;
struct sha256 old_our_rhash, old_their_rhash; struct sha256 old_our_rhash, old_their_rhash, rhash1, rhash2;
struct channel_state *cstate; struct channel_state *cstate;
/* Start sanity check. */ /* Start sanity check. */
@ -51,11 +63,15 @@ struct channel_state *gather_updates(const tal_t *ctx,
if (!cstate) if (!cstate)
errx(1, "Invalid open combination (need 1 anchor offer)"); errx(1, "Invalid open combination (need 1 anchor offer)");
if (our_rhash) /* If they don't want these, use dummy ones. */
proto_to_sha256(o1->revocation_hash, our_rhash); if (!our_rhash)
our_rhash = &rhash1;
if (!their_rhash)
their_rhash = &rhash2;
if (their_rhash) proto_to_sha256(o1->revocation_hash, our_rhash);
proto_to_sha256(o2->revocation_hash, their_rhash); proto_to_sha256(o2->revocation_hash, their_rhash);
/* If o2 sent anchor, it contains their commit sig. */ /* If o2 sent anchor, it contains their commit sig. */
if (o2->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR) if (o2->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
@ -82,33 +98,30 @@ struct channel_state *gather_updates(const tal_t *ctx,
if (received) if (received)
sig = pkt->open_commit_sig->sig; sig = pkt->open_commit_sig->sig;
break; break;
case PKT__PKT_UPDATE: { case PKT__PKT_UPDATE:
if (received) { if (received)
delta = -pkt->update->delta; delta = -pkt->update->delta;
get_rhash(pkt->update->revocation_hash, else
&old_their_rhash, their_rhash);
} else {
delta = pkt->update->delta; delta = pkt->update->delta;
get_rhash(pkt->update->revocation_hash,
&old_our_rhash, our_rhash);
}
if (!funding_delta(o1, o2, oa, delta, 0, if (!funding_delta(o1, o2, oa, delta, 0,
&cstate->a, &cstate->b)) &cstate->a, &cstate->b))
errx(1, "Impossible funding update %lli %s", errx(1, "Impossible funding update %lli %s",
(long long)delta, *argv); (long long)delta, *argv);
if (num_updates)
(*num_updates)++; update_rhash(pkt->update->revocation_hash,
received, num_updates,
&old_our_rhash, &old_their_rhash,
our_rhash, their_rhash);
break; break;
}
case PKT__PKT_UPDATE_ACCEPT: case PKT__PKT_UPDATE_ACCEPT:
if (received) { if (received)
sig = pkt->update_accept->sig; sig = pkt->update_accept->sig;
get_rhash(pkt->update_accept->revocation_hash,
&old_their_rhash, their_rhash); /* Does not increase num_updates */
} else { update_rhash(pkt->update_accept->revocation_hash,
get_rhash(pkt->update_accept->revocation_hash, received, NULL,
&old_our_rhash, our_rhash); &old_our_rhash, &old_their_rhash,
} our_rhash, their_rhash);
break; break;
case PKT__PKT_UPDATE_SIGNATURE: case PKT__PKT_UPDATE_SIGNATURE:
if (received) { if (received) {

Loading…
Cancel
Save