33# Distributed under the MIT software license, see the accompanying
44# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55"""Test the scantxoutset rpc call."""
6+ from test_framework .messages import COIN
67from test_framework .test_framework import BitcoinTestFramework
78from test_framework .util import assert_equal , assert_raises_rpc_error
9+ from test_framework .wallet import (
10+ MiniWallet ,
11+ address_to_scriptpubkey ,
12+ getnewdestination ,
13+ )
814
915from decimal import Decimal
10- import shutil
11- import os
1216
1317
1418def descriptors (out ):
@@ -18,66 +22,55 @@ def descriptors(out):
1822class ScantxoutsetTest (BitcoinTestFramework ):
1923 def set_test_params (self ):
2024 self .num_nodes = 1
21- self .setup_clean_chain = True
2225
23- def skip_test_if_missing_module (self ):
24- self .skip_if_no_wallet ()
26+ def sendtodestination (self , destination , amount ):
27+ # interpret strings as addresses, assume scriptPubKey otherwise
28+ if isinstance (destination , str ):
29+ destination = address_to_scriptpubkey (destination )
30+ self .wallet .send_to (from_node = self .nodes [0 ], scriptPubKey = destination , amount = int (COIN * amount ))
2531
2632 def run_test (self ):
27- self .log .info ("Mining blocks..." )
28- self .generate (self .nodes [0 ], 110 )
29-
30- addr_P2SH_SEGWIT = self .nodes [0 ].getnewaddress ("" , "p2sh-segwit" )
31- pubk1 = self .nodes [0 ].getaddressinfo (addr_P2SH_SEGWIT )['pubkey' ]
32- addr_LEGACY = self .nodes [0 ].getnewaddress ("" , "legacy" )
33- pubk2 = self .nodes [0 ].getaddressinfo (addr_LEGACY )['pubkey' ]
34- addr_BECH32 = self .nodes [0 ].getnewaddress ("" , "bech32" )
35- pubk3 = self .nodes [0 ].getaddressinfo (addr_BECH32 )['pubkey' ]
36- txid = self .nodes [0 ].sendtoaddress (addr_P2SH_SEGWIT , 0.001 )
37- self .nodes [0 ].lockunspent (unlock = False , transactions = [{"txid" : txid , "vout" : 0 }, {"txid" : txid , "vout" : 1 }])
38- txid = self .nodes [0 ].sendtoaddress (addr_LEGACY , 0.002 )
39- self .nodes [0 ].lockunspent (unlock = False , transactions = [{"txid" : txid , "vout" : 0 }, {"txid" : txid , "vout" : 1 }])
40- txid = self .nodes [0 ].sendtoaddress (addr_BECH32 , 0.004 )
41- self .nodes [0 ].lockunspent (unlock = False , transactions = [{"txid" : txid , "vout" : 0 }, {"txid" : txid , "vout" : 1 }])
33+ self .wallet = MiniWallet (self .nodes [0 ])
34+ self .wallet .rescan_utxos ()
35+
36+ self .log .info ("Create UTXOs..." )
37+ pubk1 , spk_P2SH_SEGWIT , addr_P2SH_SEGWIT = getnewdestination ("p2sh-segwit" )
38+ pubk2 , spk_LEGACY , addr_LEGACY = getnewdestination ("legacy" )
39+ pubk3 , spk_BECH32 , addr_BECH32 = getnewdestination ("bech32" )
40+ self .sendtodestination (spk_P2SH_SEGWIT , 0.001 )
41+ self .sendtodestination (spk_LEGACY , 0.002 )
42+ self .sendtodestination (spk_BECH32 , 0.004 )
4243
4344 #send to child keys of tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK
44- self .nodes [ 0 ]. sendtoaddress ("mkHV1C6JLheLoUSSZYk7x3FH5tnx9bu7yc" , 0.008 ) # (m/0'/0'/0')
45- self .nodes [ 0 ]. sendtoaddress ("mipUSRmJAj2KrjSvsPQtnP8ynUon7FhpCR" , 0.016 ) # (m/0'/0'/1')
46- self .nodes [ 0 ]. sendtoaddress ("n37dAGe6Mq1HGM9t4b6rFEEsDGq7Fcgfqg" , 0.032 ) # (m/0'/0'/1500')
47- self .nodes [ 0 ]. sendtoaddress ("mqS9Rpg8nNLAzxFExsgFLCnzHBsoQ3PRM6" , 0.064 ) # (m/0'/0'/0)
48- self .nodes [ 0 ]. sendtoaddress ("mnTg5gVWr3rbhHaKjJv7EEEc76ZqHgSj4S" , 0.128 ) # (m/0'/0'/1)
49- self .nodes [ 0 ]. sendtoaddress ("mketCd6B9U9Uee1iCsppDJJBHfvi6U6ukC" , 0.256 ) # (m/0'/0'/1500)
50- self .nodes [ 0 ]. sendtoaddress ("mj8zFzrbBcdaWXowCQ1oPZ4qioBVzLzAp7" , 0.512 ) # (m/1/1/0')
51- self .nodes [ 0 ]. sendtoaddress ("mfnKpKQEftniaoE1iXuMMePQU3PUpcNisA" , 1.024 ) # (m/1/1/1')
52- self .nodes [ 0 ]. sendtoaddress ("mou6cB1kaP1nNJM1sryW6YRwnd4shTbXYQ" , 2.048 ) # (m/1/1/1500')
53- self .nodes [ 0 ]. sendtoaddress ("mtfUoUax9L4tzXARpw1oTGxWyoogp52KhJ" , 4.096 ) # (m/1/1/0)
54- self .nodes [ 0 ]. sendtoaddress ("mxp7w7j8S1Aq6L8StS2PqVvtt4HGxXEvdy" , 8.192 ) # (m/1/1/1)
55- self .nodes [ 0 ]. sendtoaddress ("mpQ8rokAhp1TAtJQR6F6TaUmjAWkAWYYBq" , 16.384 ) # (m/1/1/1500)
45+ self .sendtodestination ("mkHV1C6JLheLoUSSZYk7x3FH5tnx9bu7yc" , 0.008 ) # (m/0'/0'/0')
46+ self .sendtodestination ("mipUSRmJAj2KrjSvsPQtnP8ynUon7FhpCR" , 0.016 ) # (m/0'/0'/1')
47+ self .sendtodestination ("n37dAGe6Mq1HGM9t4b6rFEEsDGq7Fcgfqg" , 0.032 ) # (m/0'/0'/1500')
48+ self .sendtodestination ("mqS9Rpg8nNLAzxFExsgFLCnzHBsoQ3PRM6" , 0.064 ) # (m/0'/0'/0)
49+ self .sendtodestination ("mnTg5gVWr3rbhHaKjJv7EEEc76ZqHgSj4S" , 0.128 ) # (m/0'/0'/1)
50+ self .sendtodestination ("mketCd6B9U9Uee1iCsppDJJBHfvi6U6ukC" , 0.256 ) # (m/0'/0'/1500)
51+ self .sendtodestination ("mj8zFzrbBcdaWXowCQ1oPZ4qioBVzLzAp7" , 0.512 ) # (m/1/1/0')
52+ self .sendtodestination ("mfnKpKQEftniaoE1iXuMMePQU3PUpcNisA" , 1.024 ) # (m/1/1/1')
53+ self .sendtodestination ("mou6cB1kaP1nNJM1sryW6YRwnd4shTbXYQ" , 2.048 ) # (m/1/1/1500')
54+ self .sendtodestination ("mtfUoUax9L4tzXARpw1oTGxWyoogp52KhJ" , 4.096 ) # (m/1/1/0)
55+ self .sendtodestination ("mxp7w7j8S1Aq6L8StS2PqVvtt4HGxXEvdy" , 8.192 ) # (m/1/1/1)
56+ self .sendtodestination ("mpQ8rokAhp1TAtJQR6F6TaUmjAWkAWYYBq" , 16.384 ) # (m/1/1/1500)
5657
5758 self .generate (self .nodes [0 ], 1 )
5859
59- self .log .info ("Stop node, remove wallet, mine again some blocks..." )
60- self .stop_node (0 )
61- shutil .rmtree (os .path .join (self .nodes [0 ].datadir , self .chain , 'wallets' ))
62- self .start_node (0 , ['-nowallet' ])
63- self .import_deterministic_coinbase_privkeys ()
64- self .generate (self .nodes [0 ], 110 )
65-
6660 scan = self .nodes [0 ].scantxoutset ("start" , [])
6761 info = self .nodes [0 ].gettxoutsetinfo ()
6862 assert_equal (scan ['success' ], True )
6963 assert_equal (scan ['height' ], info ['height' ])
7064 assert_equal (scan ['txouts' ], info ['txouts' ])
7165 assert_equal (scan ['bestblock' ], info ['bestblock' ])
7266
73- self .restart_node (0 , ['-nowallet' ])
7467 self .log .info ("Test if we have found the non HD unspent outputs." )
75- assert_equal (self .nodes [0 ].scantxoutset ("start" , ["pkh(" + pubk1 + ")" , "pkh(" + pubk2 + ")" , "pkh(" + pubk3 + ")" ])['total_amount' ], Decimal ("0.002" ))
76- assert_equal (self .nodes [0 ].scantxoutset ("start" , ["wpkh(" + pubk1 + ")" , "wpkh(" + pubk2 + ")" , "wpkh(" + pubk3 + ")" ])['total_amount' ], Decimal ("0.004" ))
77- assert_equal (self .nodes [0 ].scantxoutset ("start" , ["sh(wpkh(" + pubk1 + "))" , "sh(wpkh(" + pubk2 + "))" , "sh(wpkh(" + pubk3 + "))" ])['total_amount' ], Decimal ("0.001" ))
78- assert_equal (self .nodes [0 ].scantxoutset ("start" , ["combo(" + pubk1 + ")" , "combo(" + pubk2 + ")" , "combo(" + pubk3 + ")" ])['total_amount' ], Decimal ("0.007" ))
68+ assert_equal (self .nodes [0 ].scantxoutset ("start" , ["pkh(" + pubk1 . hex () + ")" , "pkh(" + pubk2 . hex () + ")" , "pkh(" + pubk3 . hex () + ")" ])['total_amount' ], Decimal ("0.002" ))
69+ assert_equal (self .nodes [0 ].scantxoutset ("start" , ["wpkh(" + pubk1 . hex () + ")" , "wpkh(" + pubk2 . hex () + ")" , "wpkh(" + pubk3 . hex () + ")" ])['total_amount' ], Decimal ("0.004" ))
70+ assert_equal (self .nodes [0 ].scantxoutset ("start" , ["sh(wpkh(" + pubk1 . hex () + "))" , "sh(wpkh(" + pubk2 . hex () + "))" , "sh(wpkh(" + pubk3 . hex () + "))" ])['total_amount' ], Decimal ("0.001" ))
71+ assert_equal (self .nodes [0 ].scantxoutset ("start" , ["combo(" + pubk1 . hex () + ")" , "combo(" + pubk2 . hex () + ")" , "combo(" + pubk3 . hex () + ")" ])['total_amount' ], Decimal ("0.007" ))
7972 assert_equal (self .nodes [0 ].scantxoutset ("start" , ["addr(" + addr_P2SH_SEGWIT + ")" , "addr(" + addr_LEGACY + ")" , "addr(" + addr_BECH32 + ")" ])['total_amount' ], Decimal ("0.007" ))
80- assert_equal (self .nodes [0 ].scantxoutset ("start" , ["addr(" + addr_P2SH_SEGWIT + ")" , "addr(" + addr_LEGACY + ")" , "combo(" + pubk3 + ")" ])['total_amount' ], Decimal ("0.007" ))
73+ assert_equal (self .nodes [0 ].scantxoutset ("start" , ["addr(" + addr_P2SH_SEGWIT + ")" , "addr(" + addr_LEGACY + ")" , "combo(" + pubk3 . hex () + ")" ])['total_amount' ], Decimal ("0.007" ))
8174
8275 self .log .info ("Test range validation." )
8376 assert_raises_rpc_error (- 8 , "End of range is too high" , self .nodes [0 ].scantxoutset , "start" , [{"desc" : "desc" , "range" : - 1 }])
0 commit comments