Browse Source

txfilter: Add an outpoint filter

This can be used both for our own outputs as well as the utxos we are
tracking.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
parent
commit
5fd19483a9
  1. 73
      lightningd/txfilter.c
  2. 27
      lightningd/txfilter.h

73
lightningd/txfilter.c

@ -1,14 +1,50 @@
#include "txfilter.h"
#include <bitcoin/script.h>
#include <ccan/build_assert/build_assert.h>
#include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/structeq/structeq.h>
#include <ccan/mem/mem.h>
#include <common/memleak.h>
#include <common/pseudorand.h>
#include <common/utils.h>
#include <wallet/wallet.h>
struct txfilter {
u8 **scriptpubkeys;
};
struct outpointfilter_entry {
struct bitcoin_txid txid;
u32 outnum;
};
static size_t outpoint_hash(const struct outpointfilter_entry *out)
{
struct siphash24_ctx ctx;
siphash24_init(&ctx, siphash_seed());
siphash24_update(&ctx, &out->txid, sizeof(out->txid));
siphash24_u32(&ctx, out->outnum);
return siphash24_done(&ctx);
}
static bool outpoint_eq(const struct outpointfilter_entry *o1,
const struct outpointfilter_entry *o2)
{
return structeq(&o1->txid, &o2->txid) && o1->outnum == o2->outnum;
}
static const struct outpointfilter_entry *outpoint_keyof(const struct outpointfilter_entry *out)
{
return out;
}
HTABLE_DEFINE_TYPE(struct outpointfilter_entry, outpoint_keyof, outpoint_hash, outpoint_eq,
outpointset);
struct outpointfilter {
struct outpointset *set;
};
struct txfilter *txfilter_new(const tal_t *ctx)
{
@ -52,3 +88,40 @@ bool txfilter_match(const struct txfilter *filter, const struct bitcoin_tx *tx)
}
return false;
}
void outpointfilter_add(struct outpointfilter *of, const struct bitcoin_txid *txid, const u32 outnum)
{
struct outpointfilter_entry *op;
if (outpointfilter_matches(of, txid, outnum))
return;
/* Have to mark the entries as notleak since they'll not be
* pointed to by anything other than the htable */
op = notleak(tal(of->set, struct outpointfilter_entry));
op->txid = *txid;
op->outnum = outnum;
outpointset_add(of->set, op);
}
bool outpointfilter_matches(struct outpointfilter *of, const struct bitcoin_txid *txid, const u32 outnum)
{
struct outpointfilter_entry op;
op.txid = *txid;
op.outnum = outnum;
return outpointset_get(of->set, &op) != NULL;
}
void outpointfilter_remove(struct outpointfilter *of, const struct bitcoin_txid *txid, const u32 outnum)
{
struct outpointfilter_entry op;
op.txid = *txid;
op.outnum = outnum;
outpointset_del(of->set, &op);
}
struct outpointfilter *outpointfilter_new(tal_t *ctx)
{
struct outpointfilter *opf = tal(ctx, struct outpointfilter);
opf->set = tal(opf, struct outpointset);
outpointset_init(opf->set);
return opf;
}

27
lightningd/txfilter.h

@ -8,6 +8,11 @@
struct txfilter;
/**
* outpointfilter -- Simple filter that keeps track of outpoints
*/
struct outpointfilter;
/**
* txfilter_new -- Construct and initialize a new txfilter
*/
@ -33,4 +38,26 @@ bool txfilter_match(const struct txfilter *filter, const struct bitcoin_tx *tx);
*/
void txfilter_add_scriptpubkey(struct txfilter *filter, u8 *script);
/**
* outpointfilter_new -- Create a new outpointfilter
*/
struct outpointfilter *outpointfilter_new(tal_t *ctx);
/**
* outpointfilter_add -- Add an outpoint to the filter
*/
void outpointfilter_add(struct outpointfilter *of,
const struct bitcoin_txid *txid, const u32 outnum);
/**
* outpointfilter_matches -- Are we tracking this outpoint?
*/
bool outpointfilter_matches(struct outpointfilter *of,
const struct bitcoin_txid *txid, const u32 outnum);
/**
* outpointfilter_remove -- Do not match this outpoint in the future
*/
void outpointfilter_remove(struct outpointfilter *of,
const struct bitcoin_txid *txid, const u32 outnum);
#endif /* LIGHTNING_LIGHTNINGD_TXFILTER_H */

Loading…
Cancel
Save