MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_withdraw_misc

Function test_withdraw_misc

tests/test_misc.py:594–765  ·  view source on GitHub ↗
(node_factory, bitcoind, chainparams)

Source from the content-addressed store, hash-verified

592
593@unittest.skipIf(TEST_NETWORK == 'liquid-regtest', "P2TR not yet supported on Elements")
594def test_withdraw_misc(node_factory, bitcoind, chainparams):
595 def dont_spend_outputs(n, txid):
596 """Reserve both outputs (we assume there are two!) in case any our ours, so we don't spend change: wrecks accounting checks"""
597 n.rpc.reserveinputs(bitcoind.rpc.createpsbt([{'txid': txid,
598 'vout': 0},
599 {'txid': txid,
600 'vout': 1}], []))
601
602 # We track channel balances, to verify that accounting is ok.
603 coin_mvt_plugin = os.path.join(os.getcwd(), 'tests/plugins/coin_movements.py')
604
605 amount = 2000000
606 # Don't get any funds from previous runs.
607 l1 = node_factory.get_node(random_hsm=True,
608 options={'plugin': coin_mvt_plugin},
609 feerates=(7500, 7500, 7500, 7500))
610 l2 = node_factory.get_node(random_hsm=True)
611 addr = l1.rpc.newaddr('p2tr')['p2tr']
612
613 # Add some funds to withdraw later
614 for i in range(10):
615 l1.bitcoin.rpc.sendtoaddress(addr, amount / 10**8)
616
617 bitcoind.generate_block(1)
618 wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 10)
619
620 # Reach around into the db to check that outputs were added
621 assert l1.db_query('SELECT COUNT(*) as c FROM outputs WHERE status=0')[0]['c'] == 10
622
623 waddr = l1.bitcoin.getnewaddress()
624 # Now attempt to withdraw some (making sure we collect multiple inputs)
625 l1.rpc.check_request_schemas = False
626 with pytest.raises(RpcError):
627 l1.rpc.withdraw('not an address', amount)
628 with pytest.raises(RpcError):
629 l1.rpc.withdraw(waddr, 'not an amount')
630 with pytest.raises(RpcError):
631 l1.rpc.withdraw(waddr, -amount)
632 with pytest.raises(RpcError, match=r'Could not afford'):
633 l1.rpc.withdraw(waddr, amount * 100)
634 l1.rpc.check_request_schemas = True
635
636 out = l1.rpc.withdraw(waddr, amount)
637
638 # Make sure bitcoind received the withdrawal
639 unspent = l1.bitcoin.rpc.listunspent(0)
640 withdrawal = [u for u in unspent if u['txid'] == out['txid']]
641
642 assert(withdrawal[0]['amount'] == Decimal('0.02'))
643
644 bitcoind.generate_block(1, wait_for_mempool=1)
645 sync_blockheight(bitcoind, [l1])
646
647 # Now make sure two of them were marked as spent
648 assert l1.db_query('SELECT COUNT(*) as c FROM outputs WHERE status=2')[0]['c'] == 2
649
650 dont_spend_outputs(l1, out['txid'])
651

Callers

nothing calls this directly

Calls 15

wait_forFunction · 0.90
sync_blockheightFunction · 0.90
only_oneFunction · 0.90
account_balanceFunction · 0.90
check_coin_movesFunction · 0.90
dont_spend_outputsFunction · 0.85
joinMethod · 0.80
generate_blockMethod · 0.80
db_queryMethod · 0.80
unreserveinputsMethod · 0.80
get_nodeMethod · 0.45
newaddrMethod · 0.45

Tested by

no test coverage detected