(node_factory, bitcoind, chainparams)
| 462 | |
| 463 | |
| 464 | def test_withdraw_misc(node_factory, bitcoind, chainparams): |
| 465 | def dont_spend_outputs(n, txid): |
| 466 | """Reserve both outputs (we assume there are two!) in case any our ours, so we don't spend change: wrecks accounting checks""" |
| 467 | n.rpc.reserveinputs(bitcoind.rpc.createpsbt([{'txid': txid, |
| 468 | 'vout': 0}, |
| 469 | {'txid': txid, |
| 470 | 'vout': 1}], [])) |
| 471 | |
| 472 | # We track channel balances, to verify that accounting is ok. |
| 473 | coin_mvt_plugin = os.path.join(os.getcwd(), 'tests/plugins/coin_movements.py') |
| 474 | |
| 475 | amount = 2000000 |
| 476 | # Don't get any funds from previous runs. |
| 477 | l1 = node_factory.get_node(random_hsm=True, |
| 478 | options={'plugin': coin_mvt_plugin}, |
| 479 | feerates=(7500, 7500, 7500, 7500)) |
| 480 | l2 = node_factory.get_node(random_hsm=True) |
| 481 | addr = l1.rpc.newaddr()['bech32'] |
| 482 | |
| 483 | # Add some funds to withdraw later |
| 484 | for i in range(10): |
| 485 | l1.bitcoin.rpc.sendtoaddress(addr, amount / 10**8) |
| 486 | |
| 487 | bitcoind.generate_block(1) |
| 488 | wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 10) |
| 489 | |
| 490 | # Reach around into the db to check that outputs were added |
| 491 | assert l1.db_query('SELECT COUNT(*) as c FROM outputs WHERE status=0')[0]['c'] == 10 |
| 492 | |
| 493 | waddr = l1.bitcoin.getnewaddress() |
| 494 | # Now attempt to withdraw some (making sure we collect multiple inputs) |
| 495 | l1.rpc.check_request_schemas = False |
| 496 | with pytest.raises(RpcError): |
| 497 | l1.rpc.withdraw('not an address', amount) |
| 498 | with pytest.raises(RpcError): |
| 499 | l1.rpc.withdraw(waddr, 'not an amount') |
| 500 | with pytest.raises(RpcError): |
| 501 | l1.rpc.withdraw(waddr, -amount) |
| 502 | with pytest.raises(RpcError, match=r'Could not afford'): |
| 503 | l1.rpc.withdraw(waddr, amount * 100) |
| 504 | l1.rpc.check_request_schemas = True |
| 505 | |
| 506 | out = l1.rpc.withdraw(waddr, amount) |
| 507 | |
| 508 | # Make sure bitcoind received the withdrawal |
| 509 | unspent = l1.bitcoin.rpc.listunspent(0) |
| 510 | withdrawal = [u for u in unspent if u['txid'] == out['txid']] |
| 511 | |
| 512 | assert(withdrawal[0]['amount'] == Decimal('0.02')) |
| 513 | |
| 514 | bitcoind.generate_block(1, wait_for_mempool=1) |
| 515 | sync_blockheight(bitcoind, [l1]) |
| 516 | |
| 517 | # Now make sure two of them were marked as spent |
| 518 | assert l1.db_query('SELECT COUNT(*) as c FROM outputs WHERE status=2')[0]['c'] == 2 |
| 519 | |
| 520 | dont_spend_outputs(l1, out['txid']) |
| 521 |
nothing calls this directly
no test coverage detected