(node_factory, bitcoind)
| 34 | |
| 35 | @unittest.skipIf(TEST_NETWORK != 'regtest', "Test relies on a number of example addresses valid only in regtest") |
| 36 | def test_withdraw(node_factory, bitcoind): |
| 37 | amount = 1000000 |
| 38 | # Don't get any funds from previous runs. |
| 39 | l1 = node_factory.get_node(random_hsm=True, options={'log-level': 'io'}) |
| 40 | l2 = node_factory.get_node(random_hsm=True) |
| 41 | addr = l1.rpc.newaddr('p2tr')['p2tr'] |
| 42 | |
| 43 | # Add some funds to withdraw later |
| 44 | for i in range(10): |
| 45 | l1.bitcoin.rpc.sendtoaddress(addr, amount / 10**8 + 0.01) |
| 46 | |
| 47 | bitcoind.generate_block(1) |
| 48 | wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 10) |
| 49 | |
| 50 | # Reach around into the db to check that outputs were added |
| 51 | assert l1.db_query('SELECT COUNT(*) as c FROM outputs WHERE status=0')[0]['c'] == 10 |
| 52 | |
| 53 | waddr = l1.bitcoin.rpc.getnewaddress() |
| 54 | # Now attempt to withdraw some (making sure we collect multiple inputs) |
| 55 | |
| 56 | # These violate schemas! |
| 57 | l1.rpc.check_request_schemas = False |
| 58 | with pytest.raises(RpcError): |
| 59 | l1.rpc.withdraw('not an address', amount) |
| 60 | with pytest.raises(RpcError): |
| 61 | l1.rpc.withdraw(waddr, 'not an amount') |
| 62 | with pytest.raises(RpcError): |
| 63 | l1.rpc.withdraw(waddr, -amount) |
| 64 | with pytest.raises(RpcError, match=r'Could not afford'): |
| 65 | l1.rpc.withdraw(waddr, amount * 100) |
| 66 | l1.rpc.check_request_schemas = True |
| 67 | |
| 68 | out = l1.rpc.withdraw(waddr, 2 * amount) |
| 69 | |
| 70 | # Side note: sendrawtransaction will trace back to withdrawl |
| 71 | myname = os.path.splitext(os.path.basename(sys.argv[0]))[0] |
| 72 | l1.daemon.wait_for_log(r': "{}:withdraw#[0-9]*/cln:withdraw#[0-9]*/txprepare:sendpsbt#[0-9]*/cln:sendrawtransaction#[0-9]*"\[OUT\]'.format(myname)) |
| 73 | |
| 74 | # Make sure bitcoind received the withdrawal |
| 75 | unspent = l1.bitcoin.rpc.listunspent(0) |
| 76 | withdrawal = [u for u in unspent if u['txid'] == out['txid']] |
| 77 | |
| 78 | assert(withdrawal[0]['amount'] == Decimal('0.02')) |
| 79 | |
| 80 | l1.bitcoin.generate_block(1) |
| 81 | sync_blockheight(l1.bitcoin, [l1]) |
| 82 | |
| 83 | # Check that there are no unconfirmed outputs (change should be confirmed) |
| 84 | for o in l1.rpc.listfunds()['outputs']: |
| 85 | assert o['status'] == 'confirmed' |
| 86 | |
| 87 | # Now make sure two of them were marked as spent |
| 88 | assert l1.db_query('SELECT COUNT(*) as c FROM outputs WHERE status=2')[0]['c'] == 2 |
| 89 | |
| 90 | # Now send some money to l2. |
| 91 | # BIP86 wallets use P2TR addresses |
| 92 | waddr = l2.rpc.newaddr('p2tr')['p2tr'] |
| 93 | l1.rpc.withdraw(waddr, 2 * amount) |
nothing calls this directly
no test coverage detected