bitcoind: importmulti fails (bitcoin master), use importdescriptors

But this requires a watch-only wallet, and python-bitcoinlib doesn't support
multiple wallets, so we need to unload the original one, but then we need
to generate a block, so that can't generate a new address, so we need
an address arg to generate_block.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2022-02-18 10:14:11 +10:30
parent c4ec1d576e
commit d0c7e18995
2 changed files with 30 additions and 12 deletions

View File

@@ -422,7 +422,7 @@ class BitcoinD(TailableProc):
# int > 0 := wait for at least N transactions # int > 0 := wait for at least N transactions
# 'tx_id' := wait for one transaction id given as a string # 'tx_id' := wait for one transaction id given as a string
# ['tx_id1', 'tx_id2'] := wait until all of the specified transaction IDs # ['tx_id1', 'tx_id2'] := wait until all of the specified transaction IDs
def generate_block(self, numblocks=1, wait_for_mempool=0): def generate_block(self, numblocks=1, wait_for_mempool=0, to_addr=None):
if wait_for_mempool: if wait_for_mempool:
if isinstance(wait_for_mempool, str): if isinstance(wait_for_mempool, str):
wait_for_mempool = [wait_for_mempool] wait_for_mempool = [wait_for_mempool]
@@ -439,7 +439,9 @@ class BitcoinD(TailableProc):
)) ))
# As of 0.16, generate() is removed; use generatetoaddress. # As of 0.16, generate() is removed; use generatetoaddress.
return self.rpc.generatetoaddress(numblocks, self.rpc.getnewaddress()) if to_addr is None:
to_addr = self.rpc.getnewaddress()
return self.rpc.generatetoaddress(numblocks, to_addr)
def simple_reorg(self, height, shift=0): def simple_reorg(self, height, shift=0):
""" """

View File

@@ -1189,21 +1189,37 @@ def test_hsmtool_dump_descriptors(node_factory, bitcoind):
out = subprocess.check_output(cmd_line).decode("utf8").split("\n") out = subprocess.check_output(cmd_line).decode("utf8").split("\n")
descriptor = [l for l in out if l.startswith("wpkh(tpub")][0] descriptor = [l for l in out if l.startswith("wpkh(tpub")][0]
# If we switch wallet, we can't generate address: do so now.
mine_to_addr = bitcoind.rpc.getnewaddress()
# Import the descriptor to bitcoind # Import the descriptor to bitcoind
# FIXME: if we update the testsuite to use the upcoming 0.21 we could use try:
# importdescriptors instead. bitcoind.rpc.importmulti([{
bitcoind.rpc.importmulti([{ "desc": descriptor,
"desc": descriptor, # No need to rescan, we'll transact afterward
# No need to rescan, we'll transact afterward "timestamp": "now",
"timestamp": "now", # The default
# The default "range": [0, 99]
"range": [0, 99] }])
}]) except JSONRPCError:
# Oh look, a new API!
# Need watch-only wallet, since descriptor has no privkeys.
bitcoind.rpc.createwallet("lightningd-ro", True)
# FIXME: No way to access non-default wallet in python-bitcoinlib
bitcoind.rpc.unloadwallet("lightningd-tests", True)
bitcoind.rpc.importdescriptors([{
"desc": descriptor,
# No need to rescan, we'll transact afterward
"timestamp": "now",
# The default
"range": [0, 99]
}])
# Funds sent to lightningd can be retrieved by bitcoind # Funds sent to lightningd can be retrieved by bitcoind
addr = l1.rpc.newaddr()["bech32"] addr = l1.rpc.newaddr()["bech32"]
txid = l1.rpc.withdraw(addr, 10**3)["txid"] txid = l1.rpc.withdraw(addr, 10**3)["txid"]
bitcoind.generate_block(1, txid) bitcoind.generate_block(1, txid, mine_to_addr)
assert len(bitcoind.rpc.listunspent(1, 1, [addr])) == 1 assert len(bitcoind.rpc.listunspent(1, 1, [addr])) == 1