From 04fa3a71a926917dca29a94b0fe49ad83a85aad7 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 18 Aug 2016 14:23:45 +0930 Subject: [PATCH] peer: use peer->htlcs in JSON getpeers output, instead of cstate array. We're weaning off the cstate arrays; use the htlc map. But for the moment we keep the output basically the same. Signed-off-by: Rusty Russell --- daemon/peer.c | 111 ++++++++++++++++++++++++++++++++++---------- daemon/test/test.sh | 11 ++++- 2 files changed, 96 insertions(+), 26 deletions(-) diff --git a/daemon/peer.c b/daemon/peer.c index a5d7c789b..05f82c3ec 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -2895,34 +2895,97 @@ static bool htlc_fully_committed(const struct commit_info *ci, static void json_add_htlcs(struct json_result *response, const char *id, - const struct peer *peer, - enum channel_side side) + struct peer *peer, + enum htlc_side owner) { - size_t i; - struct htlc **htlcs = peer->local.commit->cstate->side[side].htlcs; + struct htlc_map_iter it; + struct htlc *h; + const struct htlc_map *htlcs = &peer->htlcs; json_array_start(response, id); - for (i = 0; i < tal_count(htlcs); i++) { - const char *committed; + for (h = htlc_map_first(htlcs, &it); h; h = htlc_map_next(htlcs, &it)) { + if (htlc_owner(h) != owner) + continue; + + /* Ignore completed HTLCs. */ + if (h->state == RCVD_REMOVE_ACK_REVOCATION + || h->state == SENT_REMOVE_ACK_REVOCATION) + continue; +#if 1 + /* Ignore uncommitted HTLCs. */ + if (!htlc_has(h, HTLC_LOCAL_F_COMMITTED)) + continue; +#endif + json_object_start(response, NULL); - json_add_u64(response, "msatoshis", htlcs[i]->msatoshis); - json_add_abstime(response, "expiry", &htlcs[i]->expiry); - json_add_hex(response, "rhash", - &htlcs[i]->rhash, sizeof(htlcs[i]->rhash)); - if (htlc_fully_committed(peer->local.commit, htlcs[i])) { - if (htlc_fully_committed(peer->remote.commit, htlcs[i])) - committed = "both"; - else - committed = "us"; - } else { - if (htlc_fully_committed(peer->remote.commit, htlcs[i])) - committed = "them"; - else - /* Weird, shouldn't happen. */ - committed = "none"; + json_add_u64(response, "msatoshis", h->msatoshis); + json_add_abstime(response, "expiry", &h->expiry); + json_add_hex(response, "rhash", &h->rhash, sizeof(h->rhash)); +#if 0 + json_add_string(response, "state", htlc_state_name(h->state)); +#else + switch (h->state) { + case SENT_ADD_HTLC: + json_add_string(response, "committed", "none"); + break; + case SENT_ADD_COMMIT: + json_add_string(response, "committed", "none"); + break; + case RCVD_ADD_REVOCATION: + json_add_string(response, "committed", "them"); + break; + case SENT_ADD_ACK_REVOCATION: + json_add_string(response, "committed", "both"); + break; + case RCVD_REMOVE_HTLC: + json_add_string(response, "committed", "both"); + break; + case SENT_REMOVE_REVOCATION: + json_add_string(response, "committed", "them"); + break; + case SENT_REMOVE_ACK_COMMIT: + json_add_string(response, "committed", "them"); + break; + + case RCVD_ADD_HTLC: + json_add_string(response, "committed", "none"); + break; + case SENT_ADD_REVOCATION: + json_add_string(response, "committed", "us"); + break; + case SENT_ADD_ACK_COMMIT: + json_add_string(response, "committed", "us"); + break; + case RCVD_ADD_ACK_REVOCATION: + json_add_string(response, "committed", "both"); + break; + case SENT_REMOVE_HTLC: + json_add_string(response, "committed", "both"); + break; + case SENT_REMOVE_COMMIT: + json_add_string(response, "committed", "both"); + break; + case RCVD_REMOVE_REVOCATION: + json_add_string(response, "committed", "us"); + break; + + /* These ones are temporary states, since we always + * send revocation immediately. */ + case RCVD_REMOVE_ACK_COMMIT: + case RCVD_REMOVE_COMMIT: + case RCVD_ADD_ACK_COMMIT: + case RCVD_ADD_COMMIT: + /* These are never printed (see continue above) */ + case SENT_REMOVE_ACK_REVOCATION: + case RCVD_REMOVE_ACK_REVOCATION: + log_broken(peer->log, + "Unexpected htlc state %s for %"PRIu64, + htlc_state_name(h->state), h->id); + json_add_string(response, "committed", "unknown"); + break; } - json_add_string(response, "committed", committed); +#endif json_object_end(response); } json_array_end(response); @@ -2964,8 +3027,8 @@ static void json_getpeers(struct command *cmd, json_add_num(response, "our_fee", last->side[OURS].fee_msat); json_add_num(response, "their_amount", last->side[THEIRS].pay_msat); json_add_num(response, "their_fee", last->side[THEIRS].fee_msat); - json_add_htlcs(response, "our_htlcs", p, OURS); - json_add_htlcs(response, "their_htlcs", p, THEIRS); + json_add_htlcs(response, "our_htlcs", p, LOCAL); + json_add_htlcs(response, "their_htlcs", p, REMOTE); /* Any changes since then? */ if (p->local.staging_cstate->changes != last->changes) diff --git a/daemon/test/test.sh b/daemon/test/test.sh index fdb3eea97..1399214bc 100755 --- a/daemon/test/test.sh +++ b/daemon/test/test.sh @@ -166,7 +166,7 @@ check_status_single() check_balance_single "$lcli" $us_pay $us_fee $them_pay $them_fee - if $lcli getpeers | tr -s '\012\011" ' ' ' | $FGREP "our_htlcs : [ $us_htlcs], their_htlcs : [ $them_htlcs]"; then :; else + if check "$lcli getpeers | tr -s '\012\011\" ' ' ' | $FGREP \"our_htlcs : [ $us_htlcs], their_htlcs : [ $them_htlcs]\""; then :; else echo Cannot find $lcli output: "our_htlcs : [ $us_htlcs], their_htlcs : [ $them_htlcs]" >&2 $lcli getpeers | tr -s '\012\011" ' ' ' >&2 return 1 @@ -759,7 +759,14 @@ lcli2 dev-output $ID1 true [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 # Both sides should be committed to htlcs -check_status $(($A_AMOUNT - $HTLC_AMOUNT*2 - $EXTRA_FEE)) $(($A_FEE + $EXTRA_FEE)) "{ msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , committed : both }, { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH2 , committed : both } " $(($B_AMOUNT - $EXTRA_FEE)) $(($B_FEE + $EXTRA_FEE)) "" +# We open-code check_status here: HTLCs could be in either order. +check_balance_single lcli1 $(($A_AMOUNT - $HTLC_AMOUNT*2 - $EXTRA_FEE)) $(($A_FEE + $EXTRA_FEE)) $(($B_AMOUNT - $EXTRA_FEE)) $(($B_FEE + $EXTRA_FEE)) +check_balance_single lcli2 $(($B_AMOUNT - $EXTRA_FEE)) $(($B_FEE + $EXTRA_FEE)) $(($A_AMOUNT - $HTLC_AMOUNT*2 - $EXTRA_FEE)) $(($A_FEE + $EXTRA_FEE)) + +# Once both balances are correct, this should be right. +lcli1 getpeers | tr -s '\012\011" ' ' ' | $FGREP "our_htlcs : [ { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , committed : both }, { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH2 , committed : both } ], their_htlcs : [ ]" || lcli1 getpeers | tr -s '\012\011" ' ' ' | $FGREP "our_htlcs : [ { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH2 , committed : both }, { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , committed : both } ], their_htlcs : [ ]" + +lcli2 getpeers | tr -s '\012\011" ' ' ' | $FGREP "our_htlcs : [ ], their_htlcs : [ { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , committed : both }, { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH2 , committed : both } ]" || lcli2 getpeers | tr -s '\012\011" ' ' ' | $FGREP "our_htlcs : [ ], their_htlcs : [ { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH2 , committed : both }, { msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , committed : both } ]" # Node2 collects the HTLCs. lcli2 fulfillhtlc $ID1 $SECRET