Skip to content

Commit 8d819d3

Browse files
committed
lightningd: return addrtype when asking wallet_can_spend.
Not just the key index. Also, remove FIXME: wallet_can_spend is no longer slow with lots of inputs! Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 8087ab4 commit 8d819d3

File tree

6 files changed

+22
-18
lines changed

6 files changed

+22
-18
lines changed

lightningd/closing_control.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ void peer_start_closingd(struct channel *channel, struct peer_fd *peer_fd)
485485
ld->wallet,
486486
channel->shutdown_scriptpubkey[LOCAL],
487487
tal_bytelen(channel->shutdown_scriptpubkey[LOCAL]),
488-
&index_val)) {
488+
&index_val, NULL)) {
489489
if (bip32_key_from_parent(
490490
ld->bip32_base,
491491
index_val,
@@ -744,7 +744,7 @@ static struct command_result *json_close(struct command *cmd,
744744

745745
/* If they give a local address, adjust final_key_idx. */
746746
if (!wallet_can_spend(cmd->ld->wallet, close_to_script, tal_bytelen(close_to_script),
747-
&final_key_idx)) {
747+
&final_key_idx, NULL)) {
748748
final_key_idx = channel->final_key_idx;
749749
}
750750
} else {

lightningd/dual_open_control.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ openchannel2_hook_cb(struct openchannel2_payload *payload STEALS)
696696
if (wallet_can_spend(dualopend->ld->wallet,
697697
payload->our_shutdown_scriptpubkey,
698698
tal_bytelen(payload->our_shutdown_scriptpubkey),
699-
&found_wallet_index)) {
699+
&found_wallet_index, NULL)) {
700700
our_shutdown_script_wallet_index = tal(tmpctx, u32);
701701
*our_shutdown_script_wallet_index = found_wallet_index;
702702
} else
@@ -3092,7 +3092,7 @@ static struct command_result *openchannel_init(struct command *cmd,
30923092
if (wallet_can_spend(cmd->ld->wallet,
30933093
oa->our_upfront_shutdown_script,
30943094
tal_bytelen(oa->our_upfront_shutdown_script),
3095-
&found_wallet_index)) {
3095+
&found_wallet_index, NULL)) {
30963096
our_upfront_shutdown_script_wallet_index = &found_wallet_index;
30973097
} else
30983098
our_upfront_shutdown_script_wallet_index = NULL;
@@ -3861,7 +3861,7 @@ static struct command_result *json_queryrates(struct command *cmd,
38613861
if (wallet_can_spend(cmd->ld->wallet,
38623862
oa->our_upfront_shutdown_script,
38633863
tal_bytelen(oa->our_upfront_shutdown_script),
3864-
&found_wallet_index)) {
3864+
&found_wallet_index, NULL)) {
38653865
our_upfront_shutdown_script_wallet_index = tal(tmpctx, u32);
38663866
*our_upfront_shutdown_script_wallet_index = found_wallet_index;
38673867
} else
@@ -4209,7 +4209,7 @@ bool peer_restart_dualopend(struct peer *peer,
42094209
if (wallet_can_spend(peer->ld->wallet,
42104210
channel->shutdown_scriptpubkey[LOCAL],
42114211
tal_bytelen(channel->shutdown_scriptpubkey[LOCAL]),
4212-
&found_wallet_index)) {
4212+
&found_wallet_index, NULL)) {
42134213
local_shutdown_script_wallet_index = tal(tmpctx, u32);
42144214
*local_shutdown_script_wallet_index = found_wallet_index;
42154215
} else

lightningd/opening_control.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ openchannel_hook_final(struct openchannel_hook_payload *payload STEALS)
717717
if (wallet_can_spend(payload->openingd->ld->wallet,
718718
our_upfront_shutdown_script,
719719
tal_bytelen(our_upfront_shutdown_script),
720-
&found_wallet_index)) {
720+
&found_wallet_index, NULL)) {
721721
upfront_shutdown_script_wallet_index = tal(tmpctx, u32);
722722
*upfront_shutdown_script_wallet_index = found_wallet_index;
723723
} else
@@ -1408,7 +1408,7 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
14081408
if (wallet_can_spend(fc->cmd->ld->wallet,
14091409
fc->our_upfront_shutdown_script,
14101410
tal_bytelen(fc->our_upfront_shutdown_script),
1411-
&found_wallet_index)) {
1411+
&found_wallet_index, NULL)) {
14121412
upfront_shutdown_script_wallet_index = tal(tmpctx, u32);
14131413
*upfront_shutdown_script_wallet_index = found_wallet_index;
14141414
} else

wallet/wallet.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ bool wallet_add_onchaind_utxo(struct wallet *w,
910910
}
911911

912912
bool wallet_can_spend(struct wallet *w, const u8 *script, size_t script_len,
913-
u32 *index)
913+
u32 *index, enum addrtype *addrtype)
914914
{
915915
u64 bip32_max_index;
916916
const struct wallet_address *waddr;
@@ -931,6 +931,8 @@ bool wallet_can_spend(struct wallet *w, const u8 *script, size_t script_len,
931931
db_set_intvar(w->db, "bip32_max_index", waddr->index);
932932

933933
*index = waddr->index;
934+
if (addrtype)
935+
*addrtype = waddr->addrtype;
934936
return true;
935937
}
936938

@@ -3014,6 +3016,7 @@ void wallet_confirm_tx(struct wallet *w,
30143016

30153017
static void got_utxo(struct wallet *w,
30163018
u64 keyindex,
3019+
enum addrtype addrtype,
30173020
const struct wally_tx *wtx,
30183021
size_t outnum,
30193022
bool is_coinbase,
@@ -3025,7 +3028,7 @@ static void got_utxo(struct wallet *w,
30253028
struct amount_asset asset = wally_tx_output_get_amount(txout);
30263029

30273030
utxo->keyindex = keyindex;
3028-
utxo->is_p2sh = is_p2sh(txout->script, txout->script_len, NULL);
3031+
utxo->is_p2sh = (addrtype == ADDR_P2SH_SEGWIT);
30293032
utxo->amount = amount_asset_to_sat(&asset);
30303033
utxo->status = OUTPUT_STATE_AVAILABLE;
30313034
wally_txid(wtx, &utxo->outpoint.txid);
@@ -3087,14 +3090,15 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct wally_tx *wtx,
30873090
const struct wally_tx_output *txout = &wtx->outputs[i];
30883091
u32 keyindex;
30893092
struct amount_asset asset = wally_tx_output_get_amount(txout);
3093+
enum addrtype addrtype;
30903094

30913095
if (!amount_asset_is_main(&asset))
30923096
continue;
30933097

3094-
if (!wallet_can_spend(w, txout->script, txout->script_len, &keyindex))
3098+
if (!wallet_can_spend(w, txout->script, txout->script_len, &keyindex, &addrtype))
30953099
continue;
30963100

3097-
got_utxo(w, keyindex, wtx, i, is_coinbase, blockheight, NULL);
3101+
got_utxo(w, keyindex, addrtype, wtx, i, is_coinbase, blockheight, NULL);
30983102
num_utxos++;
30993103
}
31003104
return num_utxos;
@@ -6751,7 +6755,7 @@ static void mutual_close_p2pkh_catch(struct bitcoind *bitcoind,
67516755
missing->addrs[n].scriptpubkey,
67526756
tal_bytelen(missing->addrs[n].scriptpubkey)))
67536757
continue;
6754-
got_utxo(w, missing->addrs[n].keyidx,
6758+
got_utxo(w, missing->addrs[n].keyidx, ADDR_BECH32,
67556759
wtx, outnum, i == 0, &height, &outp);
67566760
log_broken(bitcoind->ld->log, "Rescan found %s!",
67576761
fmt_bitcoin_outpoint(tmpctx, &outp));

wallet/wallet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,17 +588,17 @@ struct utxo **wallet_utxo_boost(const tal_t *ctx,
588588
/**
589589
* wallet_can_spend - Do we have the private key matching this scriptpubkey?
590590
*
591-
* FIXME: This is very slow with lots of inputs!
592-
*
593591
* @w: (in) wallet holding the pubkeys to check against (privkeys are on HSM)
594592
* @script: (in) the script to check
595593
* @script_len: (in) the length of @script
596594
* @index: (out) the bip32 derivation index that matched the script
595+
* @addrtype: (out) if non-NULL, set to address type of script.
597596
*/
598597
bool wallet_can_spend(struct wallet *w,
599598
const u8 *script,
600599
size_t script_len,
601-
u32 *index);
600+
u32 *index,
601+
enum addrtype *addrtype);
602602

603603
/**
604604
* wallet_get_newindex - get a new index from the wallet.

wallet/walletrpc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ static void match_psbt_outputs_to_wallet(struct wally_psbt *psbt,
729729
const size_t script_len = psbt->outputs[outndx].script_len;
730730
u32 index;
731731

732-
if (!wallet_can_spend(w, script, script_len, &index))
732+
if (!wallet_can_spend(w, script, script_len, &index, NULL))
733733
continue;
734734

735735
if (bip32_key_from_parent(
@@ -936,7 +936,7 @@ static void maybe_notify_new_external_send(struct lightningd *ld,
936936
/* If it's going to our wallet, ignore */
937937
script = wally_psbt_output_get_script(tmpctx,
938938
&psbt->outputs[outnum]);
939-
if (wallet_can_spend(ld->wallet, script, tal_bytelen(script), &index))
939+
if (wallet_can_spend(ld->wallet, script, tal_bytelen(script), &index, NULL))
940940
return;
941941

942942
outpoint.txid = *txid;

0 commit comments

Comments
 (0)