@@ -42,8 +42,6 @@ def http_post_call(host, port, path, requestdata='', response_object=0):
42
42
return conn .getresponse ().read ()
43
43
44
44
class RESTTest (BitcoinTestFramework ):
45
- FORMAT_SEPARATOR = "."
46
-
47
45
def set_test_params (self ):
48
46
self .setup_clean_chain = True
49
47
self .num_nodes = 3
@@ -75,7 +73,8 @@ def run_test(self):
75
73
76
74
self .log .info ("Load the transaction using the /tx URI" )
77
75
78
- json_string = http_get_call (url .hostname , url .port , '/rest/tx/' + txid + self .FORMAT_SEPARATOR + "json" )
76
+ json_request = "/rest/tx/{}.json" .format (txid )
77
+ json_string = http_get_call (url .hostname , url .port , json_request )
79
78
json_obj = json .loads (json_string )
80
79
vintx = json_obj ['vin' ][0 ]['txid' ] # get the vin to later check for utxo (should be spent by then)
81
80
# get n of 0.1 outpoint
@@ -86,8 +85,8 @@ def run_test(self):
86
85
87
86
self .log .info ("Query an unspent TXO using the /getutxos URI" )
88
87
89
- json_request = '/' + txid + '-' + str (n )
90
- json_string = http_get_call (url .hostname , url .port , '/rest/getutxos' + json_request + self . FORMAT_SEPARATOR + 'json' )
88
+ json_request = "/rest/getutxos/{}-{}.json" . format ( txid , str (n ) )
89
+ json_string = http_get_call (url .hostname , url .port , json_request )
91
90
json_obj = json .loads (json_string )
92
91
93
92
# Check chainTip response
@@ -99,8 +98,8 @@ def run_test(self):
99
98
100
99
self .log .info ("Query a spent TXO using the /getutxos URI" )
101
100
102
- json_request = '/' + vintx + '-0'
103
- json_string = http_get_call (url .hostname , url .port , '/rest/getutxos' + json_request + self . FORMAT_SEPARATOR + 'json' )
101
+ json_request = "/rest/getutxos/{}-0.json" . format ( vintx )
102
+ json_string = http_get_call (url .hostname , url .port , json_request )
104
103
json_obj = json .loads (json_string )
105
104
106
105
# Check chainTip response
@@ -114,8 +113,8 @@ def run_test(self):
114
113
115
114
self .log .info ("Query two TXOs using the /getutxos URI" )
116
115
117
- json_request = '/' + txid + '-' + str (n ) + '/' + vintx + '-0'
118
- json_string = http_get_call (url .hostname , url .port , '/rest/getutxos' + json_request + self . FORMAT_SEPARATOR + 'json' )
116
+ json_request = "/rest/getutxos/{}-{}/{}-0.json" . format ( txid , str (n ), vintx )
117
+ json_string = http_get_call (url .hostname , url .port , json_request )
119
118
json_obj = json .loads (json_string )
120
119
assert_equal (len (json_obj ['utxos' ]), 1 )
121
120
assert_equal (json_obj ['bitmap' ], "10" )
@@ -130,7 +129,7 @@ def run_test(self):
130
129
bin_request += hex_str_to_bytes (vintx )
131
130
bin_request += pack ("i" , 0 )
132
131
133
- bin_response = http_post_call (url .hostname , url .port , '/rest/getutxos' + self . FORMAT_SEPARATOR + ' bin' , bin_request )
132
+ bin_response = http_post_call (url .hostname , url .port , '/rest/getutxos. bin' , bin_request )
134
133
output = BytesIO ()
135
134
output .write (bin_response )
136
135
output .seek (0 )
@@ -147,7 +146,8 @@ def run_test(self):
147
146
148
147
# do a tx and don't sync
149
148
txid = self .nodes [0 ].sendtoaddress (self .nodes [1 ].getnewaddress (), 0.1 )
150
- json_string = http_get_call (url .hostname , url .port , '/rest/tx/' + txid + self .FORMAT_SEPARATOR + "json" )
149
+ json_request = "/rest/tx/{}.json" .format (txid )
150
+ json_string = http_get_call (url .hostname , url .port , json_request )
151
151
json_obj = json .loads (json_string )
152
152
# get the spent output to later check for utxo (should be spent by then)
153
153
spent = '{}-{}' .format (json_obj ['vin' ][0 ]['txid' ], json_obj ['vin' ][0 ]['vout' ])
@@ -158,64 +158,58 @@ def run_test(self):
158
158
n = vout ['n' ]
159
159
spending = '{}-{}' .format (txid , n )
160
160
161
- json_request = '/' + spending
162
- json_string = http_get_call (url .hostname , url .port , '/rest/getutxos' + json_request + self . FORMAT_SEPARATOR + 'json' )
161
+ json_request = '/rest/getutxos/{}.json' . format ( spending )
162
+ json_string = http_get_call (url .hostname , url .port , json_request )
163
163
json_obj = json .loads (json_string )
164
164
assert_equal (len (json_obj ['utxos' ]), 0 )
165
165
166
- json_request = '/checkmempool/' + spending
167
- json_string = http_get_call (url .hostname , url .port , '/rest/getutxos' + json_request + self . FORMAT_SEPARATOR + 'json' )
166
+ json_request = '/rest/getutxos/ checkmempool/{}.json' . format ( spending )
167
+ json_string = http_get_call (url .hostname , url .port , json_request )
168
168
json_obj = json .loads (json_string )
169
169
assert_equal (len (json_obj ['utxos' ]), 1 )
170
170
171
- json_request = '/' + spent
172
- json_string = http_get_call (url .hostname , url .port , '/rest/getutxos' + json_request + self . FORMAT_SEPARATOR + 'json' )
171
+ json_request = '/rest/getutxos/{}.json' . format ( spent )
172
+ json_string = http_get_call (url .hostname , url .port , json_request )
173
173
json_obj = json .loads (json_string )
174
174
assert_equal (len (json_obj ['utxos' ]), 1 )
175
175
176
- json_request = '/checkmempool/' + spent
177
- json_string = http_get_call (url .hostname , url .port , '/rest/getutxos' + json_request + self . FORMAT_SEPARATOR + 'json' )
176
+ json_request = '/rest/getutxos/ checkmempool/{}.json' . format ( spent )
177
+ json_string = http_get_call (url .hostname , url .port , json_request )
178
178
json_obj = json .loads (json_string )
179
179
assert_equal (len (json_obj ['utxos' ]), 0 )
180
180
181
181
self .nodes [0 ].generate (1 )
182
182
self .sync_all ()
183
183
184
- json_request = '/' + spending
185
- json_string = http_get_call (url .hostname , url .port , '/rest/getutxos' + json_request + self . FORMAT_SEPARATOR + 'json' )
184
+ json_request = '/rest/getutxos/{}.json' . format ( spending )
185
+ json_string = http_get_call (url .hostname , url .port , json_request )
186
186
json_obj = json .loads (json_string )
187
187
assert_equal (len (json_obj ['utxos' ]), 1 )
188
188
189
- json_request = '/checkmempool/' + spending
190
- json_string = http_get_call (url .hostname , url .port , '/rest/getutxos' + json_request + self . FORMAT_SEPARATOR + 'json' )
189
+ json_request = '/rest/getutxos/ checkmempool/{}.json' . format ( spending )
190
+ json_string = http_get_call (url .hostname , url .port , json_request )
191
191
json_obj = json .loads (json_string )
192
192
assert_equal (len (json_obj ['utxos' ]), 1 )
193
193
194
194
# Do some invalid requests
195
195
json_request = '{"checkmempool'
196
- response = http_post_call (url .hostname , url .port , '/rest/getutxos' + self . FORMAT_SEPARATOR + ' json' , json_request , True )
196
+ response = http_post_call (url .hostname , url .port , '/rest/getutxos. json' , json_request , True )
197
197
assert_equal (response .status , 400 ) # must be a 400 because we send an invalid json request
198
198
199
199
json_request = '{"checkmempool'
200
- response = http_post_call (url .hostname , url .port , '/rest/getutxos' + self . FORMAT_SEPARATOR + ' bin' , json_request , True )
200
+ response = http_post_call (url .hostname , url .port , '/rest/getutxos. bin' , json_request , True )
201
201
assert_equal (response .status , 400 ) # must be a 400 because we send an invalid bin request
202
202
203
- response = http_post_call (url .hostname , url .port , '/rest/getutxos/checkmempool' + self . FORMAT_SEPARATOR + 'bin ' , '' , True )
203
+ response = http_post_call (url .hostname , url .port , '/rest/getutxos/checkmempool.json ' , '' , True )
204
204
assert_equal (response .status , 400 ) # must be a 400 because we send an invalid bin request
205
205
206
206
# Test limits
207
- json_request = '/checkmempool/'
208
- for x in range (0 , 20 ):
209
- json_request += txid + '-' + str (n ) + '/'
210
- json_request = json_request .rstrip ("/" )
211
- response = http_post_call (url .hostname , url .port , '/rest/getutxos' + json_request + self .FORMAT_SEPARATOR + 'json' , '' , True )
207
+ json_request = '/rest/getutxos/checkmempool/' + '/' .join (["{}-{}" .format (txid , n ) for n in range (20 )]) + '.json'
208
+ response = http_post_call (url .hostname , url .port , json_request , '' , True )
212
209
assert_equal (response .status , 400 ) # must be a 400 because we exceeding the limits
213
210
214
- json_request = '/checkmempool/'
215
- for x in range (0 , 15 ):
216
- json_request += txid + '-' + str (n ) + '/'
217
- json_request = json_request .rstrip ("/" )
218
- response = http_post_call (url .hostname , url .port , '/rest/getutxos' + json_request + self .FORMAT_SEPARATOR + 'json' , '' , True )
211
+ json_request = '/rest/getutxos/checkmempool/' + '/' .join (['{}-{}' .format (txid , n ) for n in range (15 )]) + '.json'
212
+ response = http_post_call (url .hostname , url .port , json_request , '' , True )
219
213
assert_equal (response .status , 200 ) # must be a 200 because we are within the limits
220
214
221
215
self .nodes [0 ].generate (1 ) # generate block to not affect upcoming tests
@@ -224,40 +218,40 @@ def run_test(self):
224
218
self .log .info ("Test the /block and /headers URIs" )
225
219
226
220
# Check binary format
227
- response = http_get_call (url .hostname , url .port , '/rest/block/' + bb_hash + self . FORMAT_SEPARATOR + " bin" , True )
221
+ response = http_get_call (url .hostname , url .port , '/rest/block/{}. bin' . format ( bb_hash ) , True )
228
222
assert_equal (response .status , 200 )
229
223
assert_greater_than (int (response .getheader ('content-length' )), 80 )
230
224
response_str = response .read ()
231
225
232
226
# Compare with block header
233
- response_header = http_get_call (url .hostname , url .port , '/rest/headers/1/' + bb_hash + self . FORMAT_SEPARATOR + " bin" , True )
227
+ response_header = http_get_call (url .hostname , url .port , '/rest/headers/1/{}. bin' . format ( bb_hash ) , True )
234
228
assert_equal (response_header .status , 200 )
235
229
assert_equal (int (response_header .getheader ('content-length' )), 80 )
236
230
response_header_str = response_header .read ()
237
231
assert_equal (response_str [0 :80 ], response_header_str )
238
232
239
233
# Check block hex format
240
- response_hex = http_get_call (url .hostname , url .port , '/rest/block/' + bb_hash + self . FORMAT_SEPARATOR + " hex" , True )
234
+ response_hex = http_get_call (url .hostname , url .port , '/rest/block/{}. hex' . format ( bb_hash ) , True )
241
235
assert_equal (response_hex .status , 200 )
242
236
assert_greater_than (int (response_hex .getheader ('content-length' )), 160 )
243
237
response_hex_str = response_hex .read ()
244
238
assert_equal (encode (response_str , "hex_codec" )[0 :160 ], response_hex_str [0 :160 ])
245
239
246
240
# Compare with hex block header
247
- response_header_hex = http_get_call (url .hostname , url .port , '/rest/headers/1/' + bb_hash + self . FORMAT_SEPARATOR + " hex" , True )
241
+ response_header_hex = http_get_call (url .hostname , url .port , '/rest/headers/1/{}. hex' . format ( bb_hash ) , True )
248
242
assert_equal (response_header_hex .status , 200 )
249
243
assert_greater_than (int (response_header_hex .getheader ('content-length' )), 160 )
250
244
response_header_hex_str = response_header_hex .read ()
251
245
assert_equal (response_hex_str [0 :160 ], response_header_hex_str [0 :160 ])
252
246
assert_equal (encode (response_header_str , "hex_codec" )[0 :160 ], response_header_hex_str [0 :160 ])
253
247
254
248
# Check json format
255
- block_json_string = http_get_call (url .hostname , url .port , '/rest/block/' + bb_hash + self . FORMAT_SEPARATOR + ' json' )
249
+ block_json_string = http_get_call (url .hostname , url .port , '/rest/block/{}. json' . format ( bb_hash ) )
256
250
block_json_obj = json .loads (block_json_string )
257
251
assert_equal (block_json_obj ['hash' ], bb_hash )
258
252
259
253
# Compare with json block header
260
- response_header_json = http_get_call (url .hostname , url .port , '/rest/headers/1/' + bb_hash + self . FORMAT_SEPARATOR + " json" , True )
254
+ response_header_json = http_get_call (url .hostname , url .port , '/rest/headers/1/{}. json' . format ( bb_hash ) , True )
261
255
assert_equal (response_header_json .status , 200 )
262
256
response_header_json_str = response_header_json .read ().decode ('utf-8' )
263
257
json_obj = json .loads (response_header_json_str , parse_float = Decimal )
@@ -272,7 +266,7 @@ def run_test(self):
272
266
# See if we can get 5 headers in one response
273
267
self .nodes [1 ].generate (5 )
274
268
self .sync_all ()
275
- response_header_json = http_get_call (url .hostname , url .port , '/rest/headers/5/' + bb_hash + self . FORMAT_SEPARATOR + " json" , True )
269
+ response_header_json = http_get_call (url .hostname , url .port , '/rest/headers/5/{}. json' . format ( bb_hash ) , True )
276
270
assert_equal (response_header_json .status , 200 )
277
271
response_header_json_str = response_header_json .read ().decode ('utf-8' )
278
272
json_obj = json .loads (response_header_json_str )
@@ -281,12 +275,12 @@ def run_test(self):
281
275
self .log .info ("Test the /tx URI" )
282
276
283
277
tx_hash = block_json_obj ['tx' ][0 ]['txid' ]
284
- json_string = http_get_call (url .hostname , url .port , '/rest/tx/' + tx_hash + self . FORMAT_SEPARATOR + " json" )
278
+ json_string = http_get_call (url .hostname , url .port , '/rest/tx/{}. json' . format ( tx_hash ) )
285
279
json_obj = json .loads (json_string )
286
280
assert_equal (json_obj ['txid' ], tx_hash )
287
281
288
282
# Check hex format response
289
- hex_string = http_get_call (url .hostname , url .port , '/rest/tx/' + tx_hash + self . FORMAT_SEPARATOR + " hex" , True )
283
+ hex_string = http_get_call (url .hostname , url .port , '/rest/tx/{}. hex' . format ( tx_hash ) , True )
290
284
assert_equal (hex_string .status , 200 )
291
285
assert_greater_than (int (response .getheader ('content-length' )), 10 )
292
286
@@ -300,14 +294,14 @@ def run_test(self):
300
294
self .sync_all ()
301
295
302
296
# Check that there are exactly 3 transactions in the TX memory pool before generating the block
303
- json_string = http_get_call (url .hostname , url .port , '/rest/mempool/info' + self . FORMAT_SEPARATOR + ' json' )
297
+ json_string = http_get_call (url .hostname , url .port , '/rest/mempool/info. json' )
304
298
json_obj = json .loads (json_string )
305
299
assert_equal (json_obj ['size' ], 3 )
306
300
# the size of the memory pool should be greater than 3x ~100 bytes
307
301
assert_greater_than (json_obj ['bytes' ], 300 )
308
302
309
303
# Check that there are our submitted transactions in the TX memory pool
310
- json_string = http_get_call (url .hostname , url .port , '/rest/mempool/contents' + self . FORMAT_SEPARATOR + ' json' )
304
+ json_string = http_get_call (url .hostname , url .port , '/rest/mempool/contents. json' )
311
305
json_obj = json .loads (json_string )
312
306
for i , tx in enumerate (txs ):
313
307
assert_equal (tx in json_obj , True )
@@ -319,14 +313,14 @@ def run_test(self):
319
313
self .sync_all ()
320
314
321
315
# Check if the 3 tx show up in the new block
322
- json_string = http_get_call (url .hostname , url .port , '/rest/block/' + newblockhash [0 ] + self . FORMAT_SEPARATOR + 'json' )
316
+ json_string = http_get_call (url .hostname , url .port , '/rest/block/{}.json' . format ( newblockhash [0 ]) )
323
317
json_obj = json .loads (json_string )
324
318
for tx in json_obj ['tx' ]:
325
319
if 'coinbase' not in tx ['vin' ][0 ]: # exclude coinbase
326
320
assert_equal (tx ['txid' ] in txs , True )
327
321
328
322
# Check the same but without tx details
329
- json_string = http_get_call (url .hostname , url .port , '/rest/block/notxdetails/' + newblockhash [0 ] + self . FORMAT_SEPARATOR + 'json' )
323
+ json_string = http_get_call (url .hostname , url .port , '/rest/block/notxdetails/{}.json' . format ( newblockhash [0 ]) )
330
324
json_obj = json .loads (json_string )
331
325
for tx in txs :
332
326
assert_equal (tx in json_obj ['tx' ], True )
0 commit comments