From 769f9f1cc5aee885d2ead5bc9ec1db4533552bee Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 7 Aug 2015 12:45:30 +0930 Subject: [PATCH] gather_updates: extract some update logic into update_rhash. Signed-off-by: Rusty Russell --- test-cli/gather_updates.c | 69 +++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/test-cli/gather_updates.c b/test-cli/gather_updates.c index 2a9f337e8..9bbd2e2de 100644 --- a/test-cli/gather_updates.c +++ b/test-cli/gather_updates.c @@ -23,13 +23,25 @@ static void check_preimage(const Sha256Hash *preimage, errx(1, "Invalid preimage in %s!", file); } -static void get_rhash(const Sha256Hash *rhash, struct sha256 *old, - struct sha256 *new) +static void update_rhash(const Sha256Hash *rhash, + 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) { - *old = *new; - proto_to_sha256(rhash, new); + /* Update rhash (and save old one for checking) */ + if (received) { + *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. */ @@ -43,7 +55,7 @@ struct channel_state *gather_updates(const tal_t *ctx, struct signature *their_commit_sig) { 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; /* Start sanity check. */ @@ -51,11 +63,15 @@ struct channel_state *gather_updates(const tal_t *ctx, if (!cstate) errx(1, "Invalid open combination (need 1 anchor offer)"); - if (our_rhash) - proto_to_sha256(o1->revocation_hash, our_rhash); + /* If they don't want these, use dummy ones. */ + if (!our_rhash) + our_rhash = &rhash1; + + if (!their_rhash) + their_rhash = &rhash2; - if (their_rhash) - proto_to_sha256(o2->revocation_hash, their_rhash); + proto_to_sha256(o1->revocation_hash, our_rhash); + proto_to_sha256(o2->revocation_hash, their_rhash); /* If o2 sent anchor, it contains their commit sig. */ 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) sig = pkt->open_commit_sig->sig; break; - case PKT__PKT_UPDATE: { - if (received) { + case PKT__PKT_UPDATE: + if (received) delta = -pkt->update->delta; - get_rhash(pkt->update->revocation_hash, - &old_their_rhash, their_rhash); - } else { + else delta = pkt->update->delta; - get_rhash(pkt->update->revocation_hash, - &old_our_rhash, our_rhash); - } if (!funding_delta(o1, o2, oa, delta, 0, &cstate->a, &cstate->b)) errx(1, "Impossible funding update %lli %s", (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; - } case PKT__PKT_UPDATE_ACCEPT: - if (received) { + if (received) sig = pkt->update_accept->sig; - get_rhash(pkt->update_accept->revocation_hash, - &old_their_rhash, their_rhash); - } else { - get_rhash(pkt->update_accept->revocation_hash, - &old_our_rhash, our_rhash); - } + + /* Does not increase num_updates */ + update_rhash(pkt->update_accept->revocation_hash, + received, NULL, + &old_our_rhash, &old_their_rhash, + our_rhash, their_rhash); break; case PKT__PKT_UPDATE_SIGNATURE: if (received) {