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,13 @@ 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" ], []]
55
+ # whitelist peers to speed up tx relay / mempool sync
56
+ for args in self .extra_args :
57
+
49
58
self .supports_cli = False
50
59
51
- def skip_test_if_missing_module (self ):
52
- self .skip_if_no_wallet ()
53
-
54
60
def test_rest_request (self , uri , http_method = 'GET' , req_type = ReqType .JSON , body = '' , status = 200 , ret_type = RetType .JSON ):
55
61
rest_uri = '/rest' + uri
56
62
if req_type == ReqType .JSON :
@@ -79,17 +85,11 @@ def test_rest_request(self, uri, http_method='GET', req_type=ReqType.JSON, body=
79
85
80
86
def run_test (self ):
81
87
self .url = urllib .parse .urlparse (self .nodes [0 ].url )
82
- self .log .info ("Mine blocks and send Bitcoin to node 1" )
88
+ self .wallet = MiniWallet (self .nodes [0 ])
89
+ self .wallet .rescan_utxos ()
83
90
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 )
91
+ self .log .info ("Broadcast test transaction and sync nodes" )
92
+ txid , _ = self .wallet .send_to (from_node = self .nodes [0 ], scriptPubKey = getnewdestination ()[1 ], amount = int (0.1 * COIN ))
93
93
self .sync_all ()
94
94
95
95
self .log .info ("Test the /tx URI" )
@@ -115,11 +115,9 @@ def run_test(self):
115
115
116
116
self .log .info ("Query an unspent TXO using the /getutxos URI" )
117
117
118
- self .generatetoaddress (self .nodes [ 1 ] , 1 , not_related_address )
118
+ self .generate (self .wallet , 1 )
119
119
bb_hash = self .nodes [0 ].getbestblockhash ()
120
120
121
- assert_equal (self .nodes [1 ].getbalance (), Decimal ("0.1" ))
122
-
123
121
# Check chainTip response
124
122
json_obj = self .test_rest_request (f"/getutxos/{ spending [0 ]} -{ spending [1 ]} " )
125
123
assert_equal (json_obj ['chaintipHash' ], bb_hash )
@@ -161,15 +159,15 @@ def run_test(self):
161
159
response_hash = output .read (32 )[::- 1 ].hex ()
162
160
163
161
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
162
+ assert_equal (chain_height , 201 ) # chain height must be 201 (pre-mined chain [200] + generated block [1])
165
163
166
164
self .log .info ("Test the /getutxos URI with and without /checkmempool" )
167
165
# Create a transaction, check that it's found with /checkmempool, but
168
166
# not found without. Then confirm the transaction and check that it's
169
167
# found with or without /checkmempool.
170
168
171
169
# do a tx and don't sync
172
- txid = self .nodes [ 0 ]. sendtoaddress ( self .nodes [1 ]. getnewaddress (), 0.1 )
170
+ txid , _ = self .wallet . send_to ( from_node = self .nodes [0 ], scriptPubKey = getnewdestination ()[ 1 ], amount = int ( 0.1 * COIN ) )
173
171
json_obj = self .test_rest_request (f"/tx/{ txid } " )
174
172
# get the spent output to later check for utxo (should be spent by then)
175
173
spent = (json_obj ['vin' ][0 ]['txid' ], json_obj ['vin' ][0 ]['vout' ])
@@ -301,11 +299,13 @@ def run_test(self):
301
299
302
300
self .log .info ("Test tx inclusion in the /mempool and /block URIs" )
303
301
304
- # Make 3 tx and mine them on node 1
302
+ # Make 3 chained txs and mine them on node 1
305
303
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 ))
304
+ input_txid = txid
305
+ for _ in range (3 ):
306
+ utxo_to_spend = self .wallet .get_utxo (txid = input_txid )
307
+ txs .append (self .wallet .send_self_transfer (from_node = self .nodes [0 ], utxo_to_spend = utxo_to_spend )['txid' ])
308
+ input_txid = txs [- 1 ]
309
309
self .sync_all ()
310
310
311
311
# Check that there are exactly 3 transactions in the TX memory pool before generating the block
@@ -351,5 +351,6 @@ def run_test(self):
351
351
json_obj = self .test_rest_request ("/chaininfo" )
352
352
assert_equal (json_obj ['bestblockhash' ], bb_hash )
353
353
354
+
354
355
if __name__ == '__main__' :
355
356
RESTTest ().main ()
0 commit comments