From 1af3428c6c8bc8554fc72832cd4885c5c3213399 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 18 Aug 2016 14:23:45 +0930 Subject: [PATCH] peer: keep a single HTLC map for all htlcs. Not separate "locally-offered" and "remotely-offered" ones; we can distinguish them by htlc->state now. Signed-off-by: Rusty Russell --- daemon/packets.c | 18 +++--------------- daemon/peer.c | 34 +++++----------------------------- daemon/peer.h | 9 ++++----- 3 files changed, 12 insertions(+), 49 deletions(-) diff --git a/daemon/packets.c b/daemon/packets.c index 8c377991f..58e388bb0 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -269,21 +269,9 @@ static bool htlcs_changestate(struct peer *peer, struct htlc *h; bool changed = false; - for (h = htlc_map_first(&peer->local.htlcs, &it); + for (h = htlc_map_first(&peer->htlcs, &it); h; - h = htlc_map_next(&peer->local.htlcs, &it)) { - size_t i; - for (i = 0; i < n; i++) { - if (h->state == table[i].from) { - htlc_changestate(h, table[i].from, table[i].to); - changed = true; - } - } - } - - for (h = htlc_map_first(&peer->remote.htlcs, &it); - h; - h = htlc_map_next(&peer->remote.htlcs, &it)) { + h = htlc_map_next(&peer->htlcs, &it)) { size_t i; for (i = 0; i < n; i++) { if (h->state == table[i].from) { @@ -689,7 +677,7 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt) /* Note that it's not *our* problem if they do this, it's * theirs (future confusion). Nonetheless, we detect and * error for them. */ - if (htlc_map_get(&peer->remote.htlcs, u->id)) + if (htlc_get(&peer->htlcs, u->id, REMOTE)) return pkt_err(peer, "HTLC id %"PRIu64" clashes for you", u->id); /* BOLT #2: diff --git a/daemon/peer.c b/daemon/peer.c index c859584b7..29d5bdcc8 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -165,21 +165,9 @@ static bool committed_to_htlcs(const struct peer *peer) struct htlc_map_iter it; struct htlc *h; - for (h = htlc_map_first(&peer->local.htlcs, &it); + for (h = htlc_map_first(&peer->htlcs, &it); h; - h = htlc_map_next(&peer->local.htlcs, &it)) { - /* FIXME: Move these dead ones to a separate hash (or - * just leave in database only). */ - if (h->state == RCVD_REMOVE_ACK_REVOCATION) - continue; - if (h->state == SENT_REMOVE_ACK_REVOCATION) - continue; - return true; - } - - for (h = htlc_map_first(&peer->remote.htlcs, &it); - h; - h = htlc_map_next(&peer->remote.htlcs, &it)) { + h = htlc_map_next(&peer->htlcs, &it)) { /* FIXME: Move these dead ones to a separate hash (or * just leave in database only). */ if (h->state == RCVD_REMOVE_ACK_REVOCATION) @@ -990,8 +978,7 @@ static struct peer *new_peer(struct lightningd_state *dstate, peer->local.commit = peer->remote.commit = NULL; peer->local.staging_cstate = peer->remote.staging_cstate = NULL; - htlc_map_init(&peer->local.htlcs); - htlc_map_init(&peer->remote.htlcs); + htlc_map_init(&peer->htlcs); /* FIXME: Attach IO logging for this peer. */ tal_add_destructor(peer, destroy_peer); @@ -1014,17 +1001,7 @@ static struct peer *new_peer(struct lightningd_state *dstate, static void htlc_destroy(struct htlc *htlc) { - struct htlc_map *map; - - /* FIXME: make peer->local/remote an array*/ - if (htlc_owner(htlc) == LOCAL) - map = &htlc->peer->local.htlcs; - else { - assert(htlc_owner(htlc) == REMOTE); - map = &htlc->peer->remote.htlcs; - } - - if (!htlc_map_del(map, htlc)) + if (!htlc_map_del(&htlc->peer->htlcs, htlc)) fatal("Could not find htlc to destroy"); } @@ -1058,11 +1035,10 @@ struct htlc *peer_new_htlc(struct peer *peer, /* If we're paying, give it a little longer. */ h->deadline = expiry + peer->dstate->config.min_htlc_expiry; - htlc_map_add(&peer->local.htlcs, h); } else { assert(htlc_owner(h) == REMOTE); - htlc_map_add(&peer->remote.htlcs, h); } + htlc_map_add(&peer->htlcs, h); tal_add_destructor(h, htlc_destroy); return h; diff --git a/daemon/peer.h b/daemon/peer.h index d4a39cab0..c93f880fc 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -96,10 +96,6 @@ struct peer_visible_state { /* cstate to generate next commitment tx. */ struct channel_state *staging_cstate; - - /* FIXME: Use single map in struct peer. */ - /* HTLCs offered by this side */ - struct htlc_map htlcs; }; /* Off peer->outgoing_txs */ @@ -184,7 +180,10 @@ struct peer { const struct commit_info *ci; const struct bitcoin_tx **resolved; } closing_onchain; - + + /* All HTLCs. */ + struct htlc_map htlcs; + /* Current ongoing packetflow */ struct io_data *io_data;