3
3
# Distributed under the MIT software license, see the accompanying
4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Test the scantxoutset rpc call."""
6
+ from test_framework .messages import COIN
6
7
from test_framework .test_framework import BitcoinTestFramework
7
8
from 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
+ )
8
14
9
15
from decimal import Decimal
10
- import shutil
11
- import os
12
16
13
17
14
18
def descriptors (out ):
@@ -18,66 +22,55 @@ def descriptors(out):
18
22
class ScantxoutsetTest (BitcoinTestFramework ):
19
23
def set_test_params (self ):
20
24
self .num_nodes = 1
21
- self .setup_clean_chain = True
22
25
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 ))
25
31
26
32
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 )
42
43
43
44
#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)
56
57
57
58
self .generate (self .nodes [0 ], 1 )
58
59
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
-
66
60
scan = self .nodes [0 ].scantxoutset ("start" , [])
67
61
info = self .nodes [0 ].gettxoutsetinfo ()
68
62
assert_equal (scan ['success' ], True )
69
63
assert_equal (scan ['height' ], info ['height' ])
70
64
assert_equal (scan ['txouts' ], info ['txouts' ])
71
65
assert_equal (scan ['bestblock' ], info ['bestblock' ])
72
66
73
- self .restart_node (0 , ['-nowallet' ])
74
67
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" ))
79
72
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" ))
81
74
82
75
self .log .info ("Test range validation." )
83
76
assert_raises_rpc_error (- 8 , "End of range is too high" , self .nodes [0 ].scantxoutset , "start" , [{"desc" : "desc" , "range" : - 1 }])
0 commit comments