From 351b8999c997c1add5aee81722501129cdd7e51a Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 15 Nov 2022 14:52:04 +0100 Subject: [PATCH] ld: Add an outgoing_txs_map htable to avoid costly lookups We were using a quadratic lookup to locate the transactions we broadcast, so let's use an `htable` instead, allowing for constant lookups during block processing. --- lightningd/chaintopology.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index 30566d6ff..357d3933d 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -66,6 +66,26 @@ static inline bool block_eq(const struct block *b, const struct bitcoin_blkid *k } HTABLE_DEFINE_TYPE(struct block, keyof_block_map, hash_sha, block_eq, block_map); +/* Hash blocks by sha */ +static inline const struct bitcoin_txid *keyof_outgoing_tx_map(const struct outgoing_tx *t) +{ + return &t->txid; +} + +static inline size_t outgoing_tx_hash_sha(const struct bitcoin_txid *key) +{ + size_t ret; + memcpy(&ret, key, sizeof(ret)); + return ret; +} + +static inline bool outgoing_tx_eq(const struct outgoing_tx *b, const struct bitcoin_txid *key) +{ + return bitcoin_txid_eq(&b->txid, key); +} +HTABLE_DEFINE_TYPE(struct outgoing_tx, keyof_outgoing_tx_map, + outgoing_tx_hash_sha, outgoing_tx_eq, outgoing_tx_map); + struct chain_topology { struct lightningd *ld; struct block *root;