@@ -198,7 +198,7 @@ def run_test(self):
198
198
self .nodes [0 ].generate (1 ) # generate block to not affect upcoming tests
199
199
self .sync_all ()
200
200
201
- self .log .info ("Test the /block and /headers URIs" )
201
+ self .log .info ("Test the /block, /blockhashbyheight and /headers URIs" )
202
202
bb_hash = self .nodes [0 ].getbestblockhash ()
203
203
204
204
# Check binary format
@@ -227,6 +227,23 @@ def run_test(self):
227
227
# Check json format
228
228
block_json_obj = self .test_rest_request ("/block/{}" .format (bb_hash ))
229
229
assert_equal (block_json_obj ['hash' ], bb_hash )
230
+ assert_equal (self .test_rest_request ("/blockhashbyheight/{}" .format (block_json_obj ['height' ]))['blockhash' ], bb_hash )
231
+
232
+ # Check hex/bin format
233
+ resp_hex = self .test_rest_request ("/blockhashbyheight/{}" .format (block_json_obj ['height' ]), req_type = ReqType .HEX , ret_type = RetType .OBJ )
234
+ assert_equal (resp_hex .read ().decode ('utf-8' ).rstrip (), bb_hash )
235
+ resp_bytes = self .test_rest_request ("/blockhashbyheight/{}" .format (block_json_obj ['height' ]), req_type = ReqType .BIN , ret_type = RetType .BYTES )
236
+ blockhash = binascii .hexlify (resp_bytes [::- 1 ]).decode ('utf-8' )
237
+ assert_equal (blockhash , bb_hash )
238
+
239
+ # Check invalid blockhashbyheight requests
240
+ resp = self .test_rest_request ("/blockhashbyheight/abc" , ret_type = RetType .OBJ , status = 400 )
241
+ assert_equal (resp .read ().decode ('utf-8' ).rstrip (), "Invalid height: abc" )
242
+ resp = self .test_rest_request ("/blockhashbyheight/1000000" , ret_type = RetType .OBJ , status = 404 )
243
+ assert_equal (resp .read ().decode ('utf-8' ).rstrip (), "Block height out of range" )
244
+ resp = self .test_rest_request ("/blockhashbyheight/-1" , ret_type = RetType .OBJ , status = 400 )
245
+ assert_equal (resp .read ().decode ('utf-8' ).rstrip (), "Invalid height: -1" )
246
+ self .test_rest_request ("/blockhashbyheight/" , ret_type = RetType .OBJ , status = 400 )
230
247
231
248
# Compare with json block header
232
249
json_obj = self .test_rest_request ("/headers/1/{}" .format (bb_hash ))
0 commit comments