Skip to content

Commit c329756

Browse files
committed
core: Add a function check if a script is P2WSH without allocating
Processing blocks is rather slow at the moment, but one thing we can do, is to prevent copying all output scripts, when really all we are interested in are the couple of outputs that are P2WSH. This builds the foundation of that by adding a method to introspect the script without having to clone it first, saving us some allocations, and deallocations. Changelog-Changed: core: Processing blocks should now be faster
1 parent 052542e commit c329756

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

bitcoin/script.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#include <common/utils.h>
1111
#include <sodium/randombytes.h>
1212

13-
/* To push 0-75 bytes onto stack. */
14-
#define OP_PUSHBYTES(val) (val)
15-
1613
/* Bitcoin's OP_HASH160 is RIPEMD(SHA256()) */
1714
static void hash160(struct ripemd160 *redeemhash, const void *mem, size_t len)
1815
{

bitcoin/script.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ struct ripemd160;
1313
struct rel_locktime;
1414
struct abs_locktime;
1515

16+
/* To push 0-75 bytes onto stack. */
17+
#define OP_PUSHBYTES(val) (val)
18+
1619
/* tal_count() gives the length of the script. */
1720
u8 *bitcoin_redeem_2of2(const tal_t *ctx,
1821
const struct pubkey *key1,

bitcoin/tx.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,16 @@ const u8 *bitcoin_tx_output_get_script(const tal_t *ctx,
324324
return cln_wally_tx_output_get_script(ctx, output);
325325
}
326326

327+
bool bitcoin_tx_output_script_is_p2wsh(const struct bitcoin_tx *tx, int outnum)
328+
{ const struct wally_tx_output *output;
329+
assert(outnum < tx->wtx->num_outputs);
330+
output = &tx->wtx->outputs[outnum];
331+
332+
return output->script_len == BITCOIN_SCRIPTPUBKEY_P2WSH_LEN &&
333+
output->script[0] == OP_0 &&
334+
output->script[1] == OP_PUSHBYTES(sizeof(struct sha256));
335+
}
336+
327337
u8 *bitcoin_tx_output_get_witscript(const tal_t *ctx, const struct bitcoin_tx *tx,
328338
int outnum)
329339
{

bitcoin/tx.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ void bitcoin_tx_output_set_amount(struct bitcoin_tx *tx, int outnum,
154154
*/
155155
const u8 *bitcoin_tx_output_get_script(const tal_t *ctx, const struct bitcoin_tx *tx, int outnum);
156156

157+
/**
158+
* Return `true` if the given output is a P2WSH output.
159+
*
160+
* This is useful if you want to peek at the script, without having to
161+
* extract it first.
162+
*/
163+
bool bitcoin_tx_output_script_is_p2wsh(const struct bitcoin_tx *tx, int outnum);
164+
157165
/**
158166
* Helper to get the script of a script's output as a tal_arr
159167
*

0 commit comments

Comments
 (0)