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

Function test_fundpsbt

tests/test_wallet.py:669–773  ·  view source on GitHub ↗
(node_factory, bitcoind, chainparams)

Source from the content-addressed store, hash-verified

667
668
669def test_fundpsbt(node_factory, bitcoind, chainparams):
670 amount = 1000000
671 total_outs = 4
672 l1 = node_factory.get_node()
673
674 # CLN returns PSBTv0 and PSETv2, for now
675 is_psbt_v2 = chainparams['elements']
676
677 outputs = []
678 # Add a medley of funds to withdraw later
679 for i in range(total_outs):
680 txid = bitcoind.rpc.sendtoaddress(l1.rpc.newaddr('bech32')['bech32'],
681 amount / 10**8)
682 outputs.append((txid, bitcoind.rpc.gettransaction(txid)['details'][0]['vout']))
683
684 bitcoind.generate_block(1)
685 wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == total_outs)
686
687 feerate = '7500perkw'
688
689 # Should get one input, plus some excess
690 funding = l1.rpc.fundpsbt(amount // 2, feerate, 0, reserve=0)
691
692 psbt = bitcoind.rpc.decodepsbt(funding['psbt'])
693 # We can fuzz this up to 99 blocks back.
694 assert funding['excess_msat'] > Millisatoshi(0)
695 assert funding['excess_msat'] < Millisatoshi(amount // 2 * 1000)
696 assert funding['feerate_per_kw'] == 7500
697 assert 'estimated_final_weight' in funding
698 assert 'reservations' not in funding
699
700 if is_psbt_v2:
701 assert psbt['fallback_locktime'] > bitcoind.rpc.getblockcount() - 100
702 assert psbt['fallback_locktime'] <= bitcoind.rpc.getblockcount()
703 assert psbt['input_count'] == 1
704 else:
705 assert psbt['tx']['locktime'] > bitcoind.rpc.getblockcount() - 100
706 assert psbt['tx']['locktime'] <= bitcoind.rpc.getblockcount()
707 assert len(psbt['tx']['vin']) == 1
708
709 # This should add 99 to the weight, but otherwise be identical (might choose different inputs though!) except for locktime.
710 funding2 = l1.rpc.fundpsbt(amount // 2, feerate, 99, reserve=0, locktime=bitcoind.rpc.getblockcount() + 1)
711 psbt2 = bitcoind.rpc.decodepsbt(funding2['psbt'])
712
713 if is_psbt_v2:
714 assert psbt2['fallback_locktime'] == bitcoind.rpc.getblockcount() + 1
715 assert psbt2['input_count'] == 1
716 else:
717 assert psbt2['tx']['locktime'] == bitcoind.rpc.getblockcount() + 1
718 assert len(psbt2['tx']['vin']) == 1
719
720 assert funding2['excess_msat'] < funding['excess_msat']
721 assert funding2['feerate_per_kw'] == 7500
722 # Naively you'd expect this to be +99, but it might have selected a non-p2sh output...
723 assert funding2['estimated_final_weight'] > funding['estimated_final_weight']
724
725 # Cannot afford this one (too much)
726 with pytest.raises(RpcError, match=r"not afford"):

Callers

nothing calls this directly

Calls 9

wait_forFunction · 0.90
MillisatoshiClass · 0.90
generate_blockMethod · 0.80
fundpsbtMethod · 0.80
reserveinputsMethod · 0.80
unreserveinputsMethod · 0.80
get_nodeMethod · 0.45
newaddrMethod · 0.45
listfundsMethod · 0.45

Tested by

no test coverage detected