66
77from decimal import Decimal
88from enum import Enum
9+ import http .client
910from io import BytesIO
1011import json
1112from struct import pack , unpack
12-
13- import http .client
1413import urllib .parse
1514
15+
16+ from test_framework .messages import (
17+ BLOCK_HEADER_SIZE ,
18+ COIN ,
19+ )
1620from test_framework .test_framework import BitcoinTestFramework
1721from test_framework .util import (
1822 assert_equal ,
1923 assert_greater_than ,
2024 assert_greater_than_or_equal ,
2125)
26+ from test_framework .wallet import (
27+ MiniWallet ,
28+ getnewdestination ,
29+ )
2230
23- from test_framework .messages import BLOCK_HEADER_SIZE
2431
2532INVALID_PARAM = "abc"
2633UNKNOWN_PARAM = "0000000000000000000000000000000000000000000000000000000000000000"
@@ -43,14 +50,10 @@ def filter_output_indices_by_value(vouts, value):
4350
4451class RESTTest (BitcoinTestFramework ):
4552 def set_test_params (self ):
46- self .setup_clean_chain = True
4753 self .num_nodes = 2
4854 self .extra_args = [["-rest" , "-blockfilterindex=1" ], []]
4955 self .supports_cli = False
5056
51- def skip_test_if_missing_module (self ):
52- self .skip_if_no_wallet ()
53-
5457 def test_rest_request (self , uri , http_method = 'GET' , req_type = ReqType .JSON , body = '' , status = 200 , ret_type = RetType .JSON ):
5558 rest_uri = '/rest' + uri
5659 if req_type == ReqType .JSON :
@@ -79,17 +82,11 @@ def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body=
7982
8083 def run_test (self ):
8184 self .url = urllib .parse .urlparse (self .nodes [0 ].url )
82- self .log .info ("Mine blocks and send Bitcoin to node 1" )
85+ self .wallet = MiniWallet (self .nodes [0 ])
86+ self .wallet .rescan_utxos ()
8387
84- # Random address so node1's balance doesn't increase
85- not_related_address = "2MxqoHEdNQTyYeX1mHcbrrpzgojbosTpCvJ"
86-
87- self .generate (self .nodes [0 ], 1 )
88- self .generatetoaddress (self .nodes [1 ], 100 , not_related_address )
89-
90- assert_equal (self .nodes [0 ].getbalance (), 50 )
91-
92- txid = self .nodes [0 ].sendtoaddress (self .nodes [1 ].getnewaddress (), 0.1 )
88+ self .log .info ("Broadcast test transaction and sync nodes" )
89+ txid , _ = self .wallet .send_to (from_node = self .nodes [0 ], scriptPubKey = getnewdestination ()[1 ], amount = int (0.1 * COIN ))
9390 self .sync_all ()
9491
9592 self .log .info ("Test the /tx URI" )
@@ -115,11 +112,9 @@ def run_test(self):
115112
116113 self .log .info ("Query an unspent TXO using the /getutxos URI" )
117114
118- self .generatetoaddress (self .nodes [ 1 ] , 1 , not_related_address )
115+ self .generate (self .wallet , 1 )
119116 bb_hash = self .nodes [0 ].getbestblockhash ()
120117
121- assert_equal (self .nodes [1 ].getbalance (), Decimal ("0.1" ))
122-
123118 # Check chainTip response
124119 json_obj = self .test_rest_request (f"/getutxos/{ spending [0 ]} -{ spending [1 ]} " )
125120 assert_equal (json_obj ['chaintipHash' ], bb_hash )
@@ -161,15 +156,15 @@ def run_test(self):
161156 response_hash = output .read (32 )[::- 1 ].hex ()
162157
163158 assert_equal (bb_hash , response_hash ) # check if getutxo's chaintip during calculation was fine
164- assert_equal (chain_height , 102 ) # chain height must be 102
159+ assert_equal (chain_height , 201 ) # chain height must be 201 (pre-mined chain [200] + generated block [1])
165160
166161 self .log .info ("Test the /getutxos URI with and without /checkmempool" )
167162 # Create a transaction, check that it's found with /checkmempool, but
168163 # not found without. Then confirm the transaction and check that it's
169164 # found with or without /checkmempool.
170165
171166 # do a tx and don't sync
172- txid = self .nodes [ 0 ]. sendtoaddress ( self .nodes [1 ]. getnewaddress (), 0.1 )
167+ txid , _ = self .wallet . send_to ( from_node = self .nodes [0 ], scriptPubKey = getnewdestination ()[ 1 ], amount = int ( 0.1 * COIN ) )
173168 json_obj = self .test_rest_request (f"/tx/{ txid } " )
174169 # get the spent output to later check for utxo (should be spent by then)
175170 spent = (json_obj ['vin' ][0 ]['txid' ], json_obj ['vin' ][0 ]['vout' ])
@@ -301,11 +296,13 @@ def run_test(self):
301296
302297 self .log .info ("Test tx inclusion in the /mempool and /block URIs" )
303298
304- # Make 3 tx and mine them on node 1
299+ # Make 3 chained txs and mine them on node 1
305300 txs = []
306- txs .append (self .nodes [0 ].sendtoaddress (not_related_address , 11 ))
307- txs .append (self .nodes [0 ].sendtoaddress (not_related_address , 11 ))
308- txs .append (self .nodes [0 ].sendtoaddress (not_related_address , 11 ))
301+ input_txid = txid
302+ for _ in range (3 ):
303+ utxo_to_spend = self .wallet .get_utxo (txid = input_txid )
304+ txs .append (self .wallet .send_self_transfer (from_node = self .nodes [0 ], utxo_to_spend = utxo_to_spend )['txid' ])
305+ input_txid = txs [- 1 ]
309306 self .sync_all ()
310307
311308 # Check that there are exactly 3 transactions in the TX memory pool before generating the block
@@ -351,5 +348,6 @@ def run_test(self):
351348 json_obj = self .test_rest_request ("/chaininfo" )
352349 assert_equal (json_obj ['bestblockhash' ], bb_hash )
353350
351+
354352if __name__ == '__main__' :
355353 RESTTest ().main ()
0 commit comments