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

Function test_txprepare

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

Source from the content-addressed store, hash-verified

341
342@unittest.skipIf(TEST_NETWORK == 'liquid-regtest', "BIP86 random_hsm not compatible with liquid-regtest bech32")
343def test_txprepare(node_factory, bitcoind, chainparams):
344 amount = 1000000
345 l1 = node_factory.get_node(random_hsm=True, options={'dev-warn-on-overgrind': None},
346 broken_log='overgrind: short signature length')
347 addr = chainparams['example_addr']
348
349 # Add some funds to withdraw later
350 for i in range(10):
351 bitcoind.rpc.sendtoaddress(l1.rpc.newaddr(good_addrtype())[good_addrtype()],
352 amount / 10**8)
353
354 bitcoind.generate_block(1)
355 wait_for(lambda: len(l1.rpc.listfunds()['outputs']) == 10)
356 for est in l1.rpc.feerates('perkw')['perkw']['estimates']:
357 if est['blockcount'] == 12:
358 normal_feerate_perkw = est['feerate']
359
360 prep = l1.rpc.txprepare(outputs=[{addr: Millisatoshi(amount * 3 * 1000)}])
361 decode = bitcoind.rpc.decoderawtransaction(prep['unsigned_tx'])
362 assert decode['txid'] == prep['txid']
363 # 4 inputs, 2 outputs (3 if we have a fee output).
364 assert len(decode['vin']) == 4
365 assert len(decode['vout']) == 2 if not chainparams['feeoutput'] else 3
366 if not chainparams['elements']: # FIXME
367 check_feerate([l1], feerate_from_psbt(chainparams, bitcoind, l1, prep['psbt']), normal_feerate_perkw)
368
369 # One output will be correct.
370 outnum = [i for i, o in enumerate(decode['vout']) if o['value'] == Decimal(amount * 3) / 10**8][0]
371
372 for i, o in enumerate(decode['vout']):
373 if i == outnum:
374 assert o['scriptPubKey']['type'] == 'witness_v0_keyhash'
375 assert scriptpubkey_addr(o['scriptPubKey']) == addr
376 else:
377 if chainparams['elements']:
378 o['scriptPubKey']['type'] in ['witness_v0_keyhash', 'fee']
379 else:
380 assert o['scriptPubKey']['type'] in ['witness_v1_taproot', 'fee']
381
382 # Now prepare one with no change.
383 prep2 = l1.rpc.txprepare([{addr: 'all'}])
384 decode = bitcoind.rpc.decoderawtransaction(prep2['unsigned_tx'])
385 assert decode['txid'] == prep2['txid']
386 # 6 inputs, 1 outputs.
387 assert len(decode['vin']) == 6
388 assert len(decode['vout']) == 1 if not chainparams['feeoutput'] else 2
389
390 # Some fees will be paid.
391 assert decode['vout'][0]['value'] < Decimal(amount * 6) / 10**8
392 assert decode['vout'][0]['value'] > Decimal(amount * 6) / 10**8 - Decimal(0.0002)
393 assert decode['vout'][0]['scriptPubKey']['type'] == 'witness_v0_keyhash'
394 assert scriptpubkey_addr(decode['vout'][0]['scriptPubKey']) == addr
395 if not chainparams['elements']: # FIXME
396 check_feerate([l1], feerate_from_psbt(chainparams, bitcoind, l1, prep2['psbt']), normal_feerate_perkw)
397
398 # If I cancel the first one, I can get those first 4 outputs.
399 discard = l1.rpc.txdiscard(prep['txid'])
400 assert discard['txid'] == prep['txid']

Callers

nothing calls this directly

Calls 14

wait_forFunction · 0.90
MillisatoshiClass · 0.90
check_feerateFunction · 0.90
scriptpubkey_addrFunction · 0.90
good_addrtypeFunction · 0.85
feerate_from_psbtFunction · 0.85
generate_blockMethod · 0.80
txprepareMethod · 0.80
txdiscardMethod · 0.80
get_nodeMethod · 0.45
newaddrMethod · 0.45
listfundsMethod · 0.45

Tested by

no test coverage detected