@@ -198,7 +198,7 @@ def run_test(self):
198198 self .nodes [0 ].generate (1 ) # generate block to not affect upcoming tests
199199 self .sync_all ()
200200
201- self .log .info ("Test the /block and /headers URIs" )
201+ self .log .info ("Test the /block, /blockhashbyheight and /headers URIs" )
202202 bb_hash = self .nodes [0 ].getbestblockhash ()
203203
204204 # Check binary format
@@ -227,6 +227,23 @@ def run_test(self):
227227 # Check json format
228228 block_json_obj = self .test_rest_request ("/block/{}" .format (bb_hash ))
229229 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 )
230247
231248 # Compare with json block header
232249 json_obj = self .test_rest_request ("/headers/1/{}" .format (bb_hash ))
0 commit comments