mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
test/test_protocol: Keep pointer to previous state.
Since our pre-change state is always the same as the previous step's post-change state, we can simply keep a pointer, with a dummy empty state for the initial one. We could function-wrap it, but this change is even simpler. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
4c1b50baab
commit
a77466aa21
@ -25,8 +25,8 @@ struct commit_info {
|
|||||||
struct commit_info *prev;
|
struct commit_info *prev;
|
||||||
/* How deep we are */
|
/* How deep we are */
|
||||||
unsigned int number;
|
unsigned int number;
|
||||||
/* Channel state. */
|
/* Funding before changes. */
|
||||||
struct funding funding;
|
const struct funding *funding;
|
||||||
/* Pending changes (for next commit_info) */
|
/* Pending changes (for next commit_info) */
|
||||||
int *changes_incoming;
|
int *changes_incoming;
|
||||||
bool changes_outgoing;
|
bool changes_outgoing;
|
||||||
@ -46,6 +46,8 @@ struct signature {
|
|||||||
struct peer {
|
struct peer {
|
||||||
int infd, outfd, cmdfd, cmddonefd;
|
int infd, outfd, cmdfd, cmddonefd;
|
||||||
|
|
||||||
|
struct funding initial_funding;
|
||||||
|
|
||||||
/* Last one is the one we're changing. */
|
/* Last one is the one we're changing. */
|
||||||
struct commit_info *us, *them;
|
struct commit_info *us, *them;
|
||||||
};
|
};
|
||||||
@ -84,9 +86,7 @@ static bool do_change(u32 *add, u32 *remove, u32 *fees, int htlc)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_change_internal(struct commit_info *ci, int **changes,
|
static void add_change_internal(struct commit_info *ci, int **changes, int c)
|
||||||
const u32 *add, const u32 *remove,
|
|
||||||
int c)
|
|
||||||
{
|
{
|
||||||
size_t i, n = tal_count(*changes);
|
size_t i, n = tal_count(*changes);
|
||||||
|
|
||||||
@ -94,12 +94,6 @@ static void add_change_internal(struct commit_info *ci, int **changes,
|
|||||||
if (c == 0)
|
if (c == 0)
|
||||||
goto add;
|
goto add;
|
||||||
|
|
||||||
/* Can't add/remove it already there/absent. */
|
|
||||||
if (c > 0 && have_htlc(*add, c))
|
|
||||||
errx(1, "Already added htlc %u", c);
|
|
||||||
else if (c < 0 && !have_htlc(*remove, -c))
|
|
||||||
errx(1, "Already removed htlc %u", -c);
|
|
||||||
|
|
||||||
/* Can't request add/remove twice. */
|
/* Can't request add/remove twice. */
|
||||||
for (i = 0; i < n; i++)
|
for (i = 0; i < n; i++)
|
||||||
if ((*changes)[i] == c)
|
if ((*changes)[i] == c)
|
||||||
@ -114,7 +108,7 @@ add:
|
|||||||
* Once we're sending/receiving revoke, we queue the changes on the
|
* Once we're sending/receiving revoke, we queue the changes on the
|
||||||
* alternate side.
|
* alternate side.
|
||||||
*/
|
*/
|
||||||
static bool queue_changes_other(struct commit_info *ci, int *changes)
|
static bool apply_changes_other(struct commit_info *ci, int *changes)
|
||||||
{
|
{
|
||||||
size_t i, n = tal_count(changes);
|
size_t i, n = tal_count(changes);
|
||||||
|
|
||||||
@ -137,41 +131,37 @@ static bool add_incoming_change(struct commit_info *ci, int c)
|
|||||||
if (!do_change(&ci->funding_next.inhtlcs, &ci->funding_next.outhtlcs,
|
if (!do_change(&ci->funding_next.inhtlcs, &ci->funding_next.outhtlcs,
|
||||||
NULL, c))
|
NULL, c))
|
||||||
return false;
|
return false;
|
||||||
add_change_internal(ci, &ci->changes_incoming,
|
add_change_internal(ci, &ci->changes_incoming, c);
|
||||||
&ci->funding.inhtlcs, &ci->funding.outhtlcs,
|
|
||||||
c);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct commit_info *new_commit_info(const tal_t *ctx,
|
static struct commit_info *new_commit_info(const struct peer *peer,
|
||||||
struct commit_info *prev)
|
struct commit_info *prev)
|
||||||
{
|
{
|
||||||
struct commit_info *ci = talz(ctx, struct commit_info);
|
struct commit_info *ci = talz(peer, struct commit_info);
|
||||||
ci->prev = prev;
|
ci->prev = prev;
|
||||||
ci->changes_incoming = tal_arr(ci, int, 0);
|
ci->changes_incoming = tal_arr(ci, int, 0);
|
||||||
ci->changes_outgoing = false;
|
ci->changes_outgoing = false;
|
||||||
if (prev) {
|
if (prev) {
|
||||||
ci->number = prev->number + 1;
|
ci->number = prev->number + 1;
|
||||||
ci->funding = prev->funding;
|
ci->funding = &prev->funding_next;
|
||||||
ci->funding_next = prev->funding_next;
|
ci->funding_next = prev->funding_next;
|
||||||
}
|
} else
|
||||||
|
ci->funding = &peer->initial_funding;
|
||||||
return ci;
|
return ci;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We duplicate the commit info, with the changes applied. */
|
/* We duplicate the commit info, with the changes applied. */
|
||||||
static struct commit_info *apply_changes(const tal_t *ctx,
|
static struct commit_info *apply_changes(const struct peer *peer,
|
||||||
struct commit_info *old)
|
struct commit_info *old)
|
||||||
{
|
{
|
||||||
struct commit_info *ci = new_commit_info(ctx, old);
|
return new_commit_info(peer, old);
|
||||||
ci->funding = ci->funding_next;
|
|
||||||
|
|
||||||
return ci;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct signature commit_sig(const struct commit_info *ci)
|
static struct signature commit_sig(const struct commit_info *ci)
|
||||||
{
|
{
|
||||||
struct signature sig;
|
struct signature sig;
|
||||||
sig.f = ci->funding;
|
sig.f = *ci->funding;
|
||||||
return sig;
|
return sig;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,13 +186,13 @@ static void dump_commit_info(const struct commit_info *ci)
|
|||||||
|
|
||||||
printf(" Commit %u:", ci->number);
|
printf(" Commit %u:", ci->number);
|
||||||
printf("\n Offered htlcs:");
|
printf("\n Offered htlcs:");
|
||||||
dump_htlcs(ci->funding.outhtlcs);
|
dump_htlcs(ci->funding->outhtlcs);
|
||||||
printf("\n Received htlcs:");
|
printf("\n Received htlcs:");
|
||||||
dump_htlcs(ci->funding.inhtlcs);
|
dump_htlcs(ci->funding->inhtlcs);
|
||||||
|
|
||||||
/* Don't clutter output if fee level untouched. */
|
/* Don't clutter output if fee level untouched. */
|
||||||
if (ci->funding.fee)
|
if (ci->funding->fee)
|
||||||
printf("\n Fee level %u", ci->funding.fee);
|
printf("\n Fee level %u", ci->funding->fee);
|
||||||
|
|
||||||
n = tal_count(ci->changes_incoming);
|
n = tal_count(ci->changes_incoming);
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
@ -354,7 +344,7 @@ static void receive_revoke(struct peer *peer, u32 number)
|
|||||||
errx(1, "receive_revoke: revoked unsigned commit?");
|
errx(1, "receive_revoke: revoked unsigned commit?");
|
||||||
|
|
||||||
/* The changes we sent with that commit, add them to us. */
|
/* The changes we sent with that commit, add them to us. */
|
||||||
if (!queue_changes_other(peer->us, ci->changes_incoming))
|
if (!apply_changes_other(peer->us, ci->changes_incoming))
|
||||||
errx(1, "receive_revoke: could not add their changes to us");
|
errx(1, "receive_revoke: could not add their changes to us");
|
||||||
|
|
||||||
/* Cleans up dump output now we've consumed them. */
|
/* Cleans up dump output now we've consumed them. */
|
||||||
@ -401,7 +391,7 @@ static void send_revoke(struct peer *peer, struct commit_info *ci)
|
|||||||
ci->revoked = true;
|
ci->revoked = true;
|
||||||
|
|
||||||
/* Queue changes. */
|
/* Queue changes. */
|
||||||
if (!queue_changes_other(peer->them, ci->changes_incoming))
|
if (!apply_changes_other(peer->them, ci->changes_incoming))
|
||||||
errx(1, "Failed queueing changes to them for send_revoke");
|
errx(1, "Failed queueing changes to them for send_revoke");
|
||||||
|
|
||||||
/* Clean up for dump output. */
|
/* Clean up for dump output. */
|
||||||
@ -481,10 +471,10 @@ static void do_cmd(struct peer *peer)
|
|||||||
read_in(peer->infd, &sig, sizeof(sig));
|
read_in(peer->infd, &sig, sizeof(sig));
|
||||||
receive_commit(peer, &sig);
|
receive_commit(peer, &sig);
|
||||||
} else if (streq(cmd, "checksync")) {
|
} else if (streq(cmd, "checksync")) {
|
||||||
write_all(peer->cmddonefd, &peer->us->funding,
|
write_all(peer->cmddonefd, peer->us->funding,
|
||||||
sizeof(peer->us->funding));
|
sizeof(*peer->us->funding));
|
||||||
write_all(peer->cmddonefd, &peer->them->funding,
|
write_all(peer->cmddonefd, peer->them->funding,
|
||||||
sizeof(peer->them->funding));
|
sizeof(*peer->them->funding));
|
||||||
return;
|
return;
|
||||||
} else if (streq(cmd, "dump")) {
|
} else if (streq(cmd, "dump")) {
|
||||||
dump_peer(peer, false);
|
dump_peer(peer, false);
|
||||||
@ -523,6 +513,8 @@ static void new_peer(int infdpair[2], int outfdpair[2], int cmdfdpair[2],
|
|||||||
close(cmddonefdpair[0]);
|
close(cmddonefdpair[0]);
|
||||||
|
|
||||||
peer = tal(NULL, struct peer);
|
peer = tal(NULL, struct peer);
|
||||||
|
memset(&peer->initial_funding, 0, sizeof(peer->initial_funding));
|
||||||
|
|
||||||
/* Create first, signed commit info. */
|
/* Create first, signed commit info. */
|
||||||
peer->us = new_commit_info(peer, NULL);
|
peer->us = new_commit_info(peer, NULL);
|
||||||
peer->us->counterparty_signed = true;
|
peer->us->counterparty_signed = true;
|
||||||
|
Loading…
Reference in New Issue
Block a user