6
6
7
7
from decimal import Decimal
8
8
from enum import Enum
9
+ import http .client
9
10
from io import BytesIO
10
11
import json
11
12
from struct import pack , unpack
12
-
13
- import http .client
14
13
import urllib .parse
15
14
15
+
16
+ from test_framework .messages import (
17
+ BLOCK_HEADER_SIZE ,
18
+ COIN ,
19
+ )
16
20
from test_framework .test_framework import BitcoinTestFramework
17
21
from test_framework .util import (
18
22
assert_equal ,
19
23
assert_greater_than ,
20
24
assert_greater_than_or_equal ,
21
25
)
26
+ from test_framework .wallet import (
27
+ MiniWallet ,
28
+ getnewdestination ,
29
+ )
22
30
23
- from test_framework .messages import BLOCK_HEADER_SIZE
24
31
25
32
INVALID_PARAM = "abc"
26
33
UNKNOWN_PARAM = "0000000000000000000000000000000000000000000000000000000000000000"
@@ -43,14 +50,10 @@ def filter_output_indices_by_value(vouts, value):
43
50
44
51
class RESTTest (BitcoinTestFramework ):
45
52
def set_test_params (self ):
46
- self .setup_clean_chain = True
47
53
self .num_nodes = 2
48
54
self .extra_args = [["-rest" , "-blockfilterindex=1" ], []]
49
55
self .supports_cli = False
50
56
51
- def skip_test_if_missing_module (self ):
52
- self .skip_if_no_wallet ()
53
-
54
57
def test_rest_request (self , uri , http_method = 'GET' , req_type = ReqType .JSON , body = '' , status = 200 , ret_type = RetType .JSON ):
55
58
rest_uri = '/rest' + uri
56
59
if req_type == ReqType .JSON :
@@ -79,17 +82,11 @@ def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body=
79
82
80
83
def run_test (self ):
81
84
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 ()
83
87
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 ))
93
90
self .sync_all ()
94
91
95
92
self .log .info ("Test the /tx URI" )
@@ -115,11 +112,9 @@ def run_test(self):
115
112
116
113
self .log .info ("Query an unspent TXO using the /getutxos URI" )
117
114
118
- self .generatetoaddress (self .nodes [ 1 ] , 1 , not_related_address )
115
+ self .generate (self .wallet , 1 )
119
116
bb_hash = self .nodes [0 ].getbestblockhash ()
120
117
121
- assert_equal (self .nodes [1 ].getbalance (), Decimal ("0.1" ))
122
-
123
118
# Check chainTip response
124
119
json_obj = self .test_rest_request (f"/getutxos/{ spending [0 ]} -{ spending [1 ]} " )
125
120
assert_equal (json_obj ['chaintipHash' ], bb_hash )
@@ -161,15 +156,15 @@ def run_test(self):
161
156
response_hash = output .read (32 )[::- 1 ].hex ()
162
157
163
158
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])
165
160
166
161
self .log .info ("Test the /getutxos URI with and without /checkmempool" )
167
162
# Create a transaction, check that it's found with /checkmempool, but
168
163
# not found without. Then confirm the transaction and check that it's
169
164
# found with or without /checkmempool.
170
165
171
166
# 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 ) )
173
168
json_obj = self .test_rest_request (f"/tx/{ txid } " )
174
169
# get the spent output to later check for utxo (should be spent by then)
175
170
spent = (json_obj ['vin' ][0 ]['txid' ], json_obj ['vin' ][0 ]['vout' ])
@@ -301,11 +296,13 @@ def run_test(self):
301
296
302
297
self .log .info ("Test tx inclusion in the /mempool and /block URIs" )
303
298
304
- # Make 3 tx and mine them on node 1
299
+ # Make 3 chained txs and mine them on node 1
305
300
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 ]
309
306
self .sync_all ()
310
307
311
308
# Check that there are exactly 3 transactions in the TX memory pool before generating the block
@@ -351,5 +348,6 @@ def run_test(self):
351
348
json_obj = self .test_rest_request ("/chaininfo" )
352
349
assert_equal (json_obj ['bestblockhash' ], bb_hash )
353
350
351
+
354
352
if __name__ == '__main__' :
355
353
RESTTest ().main ()
0 commit comments