Basically, `devtools/reduce-includes.sh */*.c`. Build time from make clean (RUST=0) (includes building external libs): Before: real 0m38.944000-40.416000(40.1131+/-0.4)s user 3m6.790000-17.159000(15.0571+/-2.8)s sys 0m35.304000-37.336000(36.8942+/-0.57)s After: real 0m37.872000-39.974000(39.5466+/-0.59)s user 3m1.211000-14.968000(12.4556+/-3.9)s sys 0m35.008000-36.830000(36.4143+/-0.5)s Build time after touch config.vars (RUST=0): Before: real 0m19.831000-21.862000(21.5528+/-0.58)s user 2m15.361000-30.731000(28.4798+/-4.4)s sys 0m21.056000-22.339000(22.0346+/-0.35)s After: real 0m18.384000-21.307000(20.8605+/-0.92)s user 2m5.585000-26.843000(23.6017+/-6.7)s sys 0m19.650000-22.003000(21.4943+/-0.69)s Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
138 lines
3.4 KiB
C
138 lines
3.4 KiB
C
#include "config.h"
|
|
#include <bitcoin/script.h>
|
|
#include <common/memleak.h>
|
|
#include <common/utils.h>
|
|
#include <wallet/txfilter.h>
|
|
#include <wallet/wallet.h>
|
|
|
|
size_t scriptpubkey_hash(const u8 *out)
|
|
{
|
|
struct siphash24_ctx ctx;
|
|
siphash24_init(&ctx, siphash_seed());
|
|
siphash24_update(&ctx, out, tal_bytelen(out));
|
|
return siphash24_done(&ctx);
|
|
}
|
|
|
|
static const u8 *scriptpubkey_keyof(const u8 *out)
|
|
{
|
|
return out;
|
|
}
|
|
|
|
static bool scriptpubkey_eq(const u8 *a, const u8 *b)
|
|
{
|
|
return tal_arr_eq(a, b);
|
|
}
|
|
|
|
/* FIXME: Should we disallow dups here? */
|
|
HTABLE_DEFINE_DUPS_TYPE(u8, scriptpubkey_keyof, scriptpubkey_hash, scriptpubkey_eq, scriptpubkeyset);
|
|
|
|
struct txfilter {
|
|
struct scriptpubkeyset scriptpubkeyset;
|
|
};
|
|
|
|
static size_t outpoint_hash(const struct bitcoin_outpoint *out)
|
|
{
|
|
struct siphash24_ctx ctx;
|
|
siphash24_init(&ctx, siphash_seed());
|
|
siphash24_update(&ctx, &out->txid, sizeof(out->txid));
|
|
siphash24_u32(&ctx, out->n);
|
|
return siphash24_done(&ctx);
|
|
}
|
|
|
|
static const struct bitcoin_outpoint *outpoint_keyof(const struct bitcoin_outpoint *out)
|
|
{
|
|
return out;
|
|
}
|
|
|
|
HTABLE_DEFINE_NODUPS_TYPE(struct bitcoin_outpoint, outpoint_keyof, outpoint_hash, bitcoin_outpoint_eq,
|
|
outpointset);
|
|
|
|
struct outpointfilter {
|
|
struct outpointset *set;
|
|
};
|
|
|
|
struct txfilter *txfilter_new(const tal_t *ctx)
|
|
{
|
|
struct txfilter *filter = tal(ctx, struct txfilter);
|
|
scriptpubkeyset_init(&filter->scriptpubkeyset);
|
|
return filter;
|
|
}
|
|
|
|
void txfilter_add_scriptpubkey(struct txfilter *filter, const u8 *script TAKES)
|
|
{
|
|
scriptpubkeyset_add(
|
|
&filter->scriptpubkeyset,
|
|
notleak(tal_dup_talarr(filter, u8, script)));
|
|
}
|
|
|
|
void txfilter_add_derkey(struct txfilter *filter,
|
|
const u8 derkey[PUBKEY_CMPR_LEN])
|
|
{
|
|
u8 *skp, *p2sh, *p2tr;
|
|
|
|
skp = scriptpubkey_p2wpkh_derkey(tmpctx, derkey);
|
|
p2sh = scriptpubkey_p2sh(tmpctx, skp);
|
|
p2tr = scriptpubkey_p2tr_derkey(tmpctx, derkey);
|
|
|
|
txfilter_add_scriptpubkey(filter, take(skp));
|
|
txfilter_add_scriptpubkey(filter, take(p2sh));
|
|
txfilter_add_scriptpubkey(filter, take(p2tr));
|
|
}
|
|
|
|
|
|
bool txfilter_match(const struct txfilter *filter, const struct bitcoin_tx *tx)
|
|
{
|
|
for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
|
|
const struct wally_tx_output *txout = &tx->wtx->outputs[i];
|
|
if (txfilter_scriptpubkey_matches(filter, txout->script))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool txfilter_scriptpubkey_matches(const struct txfilter *filter, const u8 *scriptPubKey)
|
|
{
|
|
if (!scriptPubKey)
|
|
return false;
|
|
return scriptpubkeyset_exists(&filter->scriptpubkeyset, scriptPubKey);
|
|
}
|
|
|
|
void outpointfilter_add(struct outpointfilter *of,
|
|
const struct bitcoin_outpoint *outpoint)
|
|
{
|
|
if (outpointfilter_matches(of, outpoint))
|
|
return;
|
|
outpointset_add(of->set, tal_dup(of->set,
|
|
struct bitcoin_outpoint,
|
|
outpoint));
|
|
}
|
|
|
|
bool outpointfilter_matches(struct outpointfilter *of,
|
|
const struct bitcoin_outpoint *outpoint)
|
|
{
|
|
return outpointset_get(of->set, outpoint) != NULL;
|
|
}
|
|
|
|
void outpointfilter_remove(struct outpointfilter *of,
|
|
const struct bitcoin_outpoint *outpoint)
|
|
{
|
|
struct bitcoin_outpoint *o = outpointset_get(of->set, outpoint);
|
|
if (o) {
|
|
outpointset_del(of->set, o);
|
|
tal_free(o);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void memleak_scan_outpointfilter(struct htable *memtable, const struct outpointfilter *opf)
|
|
{
|
|
memleak_scan_htable(memtable, &opf->set->raw);
|
|
}
|