You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rest.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014-2015 The Bitcoin Core developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #
  6. # Test REST interface
  7. #
  8. from test_framework.test_framework import BitcoinTestFramework
  9. from test_framework.util import *
  10. from struct import *
  11. import binascii
  12. import json
  13. import StringIO
  14. import decimal
  15. try:
  16. import http.client as httplib
  17. except ImportError:
  18. import httplib
  19. try:
  20. import urllib.parse as urlparse
  21. except ImportError:
  22. import urlparse
  23. def deser_uint256(f):
  24. r = 0
  25. for i in range(8):
  26. t = unpack(b"<I", f.read(4))[0]
  27. r += t << (i * 32)
  28. return r
  29. #allows simple http get calls
  30. def http_get_call(host, port, path, response_object = 0):
  31. conn = httplib.HTTPConnection(host, port)
  32. conn.request('GET', path)
  33. if response_object:
  34. return conn.getresponse()
  35. return conn.getresponse().read()
  36. #allows simple http post calls with a request body
  37. def http_post_call(host, port, path, requestdata = '', response_object = 0):
  38. conn = httplib.HTTPConnection(host, port)
  39. conn.request('POST', path, requestdata)
  40. if response_object:
  41. return conn.getresponse()
  42. return conn.getresponse().read()
  43. class RESTTest (BitcoinTestFramework):
  44. FORMAT_SEPARATOR = "."
  45. def setup_chain(self):
  46. print("Initializing test directory "+self.options.tmpdir)
  47. initialize_chain_clean(self.options.tmpdir, 3)
  48. def setup_network(self, split=False):
  49. self.nodes = start_nodes(3, self.options.tmpdir)
  50. connect_nodes_bi(self.nodes,0,1)
  51. connect_nodes_bi(self.nodes,1,2)
  52. connect_nodes_bi(self.nodes,0,2)
  53. self.is_network_split=False
  54. self.sync_all()
  55. def run_test(self):
  56. url = urlparse.urlparse(self.nodes[0].url)
  57. print "Mining blocks..."
  58. self.nodes[0].generate(1)
  59. self.sync_all()
  60. self.nodes[2].generate(100)
  61. self.sync_all()
  62. assert_equal(self.nodes[0].getbalance(), 50)
  63. txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
  64. self.sync_all()
  65. self.nodes[2].generate(1)
  66. self.sync_all()
  67. bb_hash = self.nodes[0].getbestblockhash()
  68. assert_equal(self.nodes[1].getbalance(), Decimal("0.1")) #balance now should be 0.1 on node 1
  69. # load the latest 0.1 tx over the REST API
  70. json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+txid+self.FORMAT_SEPARATOR+"json")
  71. json_obj = json.loads(json_string)
  72. vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then)
  73. # get n of 0.1 outpoint
  74. n = 0
  75. for vout in json_obj['vout']:
  76. if vout['value'] == 0.1:
  77. n = vout['n']
  78. ######################################
  79. # GETUTXOS: query a unspent outpoint #
  80. ######################################
  81. json_request = '/checkmempool/'+txid+'-'+str(n)
  82. json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
  83. json_obj = json.loads(json_string)
  84. #check chainTip response
  85. assert_equal(json_obj['chaintipHash'], bb_hash)
  86. #make sure there is one utxo
  87. assert_equal(len(json_obj['utxos']), 1)
  88. assert_equal(json_obj['utxos'][0]['value'], 0.1)
  89. ################################################
  90. # GETUTXOS: now query a already spent outpoint #
  91. ################################################
  92. json_request = '/checkmempool/'+vintx+'-0'
  93. json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
  94. json_obj = json.loads(json_string)
  95. #check chainTip response
  96. assert_equal(json_obj['chaintipHash'], bb_hash)
  97. #make sure there is no utox in the response because this oupoint has been spent
  98. assert_equal(len(json_obj['utxos']), 0)
  99. #check bitmap
  100. assert_equal(json_obj['bitmap'], "0")
  101. ##################################################
  102. # GETUTXOS: now check both with the same request #
  103. ##################################################
  104. json_request = '/checkmempool/'+txid+'-'+str(n)+'/'+vintx+'-0'
  105. json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
  106. json_obj = json.loads(json_string)
  107. assert_equal(len(json_obj['utxos']), 1)
  108. assert_equal(json_obj['bitmap'], "10")
  109. #test binary response
  110. bb_hash = self.nodes[0].getbestblockhash()
  111. binaryRequest = b'\x01\x02'
  112. binaryRequest += binascii.unhexlify(txid)
  113. binaryRequest += pack("i", n);
  114. binaryRequest += binascii.unhexlify(vintx);
  115. binaryRequest += pack("i", 0);
  116. bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', binaryRequest)
  117. output = StringIO.StringIO()
  118. output.write(bin_response)
  119. output.seek(0)
  120. chainHeight = unpack("i", output.read(4))[0]
  121. hashFromBinResponse = hex(deser_uint256(output))[2:].zfill(65).rstrip("L")
  122. assert_equal(bb_hash, hashFromBinResponse) #check if getutxo's chaintip during calculation was fine
  123. assert_equal(chainHeight, 102) #chain height must be 102
  124. ############################
  125. # GETUTXOS: mempool checks #
  126. ############################
  127. # do a tx and don't sync
  128. txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1)
  129. json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+txid+self.FORMAT_SEPARATOR+"json")
  130. json_obj = json.loads(json_string)
  131. vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then)
  132. # get n of 0.1 outpoint
  133. n = 0
  134. for vout in json_obj['vout']:
  135. if vout['value'] == 0.1:
  136. n = vout['n']
  137. json_request = '/'+txid+'-'+str(n)
  138. json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
  139. json_obj = json.loads(json_string)
  140. assert_equal(len(json_obj['utxos']), 0) #there should be a outpoint because it has just added to the mempool
  141. json_request = '/checkmempool/'+txid+'-'+str(n)
  142. json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json')
  143. json_obj = json.loads(json_string)
  144. assert_equal(len(json_obj['utxos']), 1) #there should be a outpoint because it has just added to the mempool
  145. #do some invalid requests
  146. json_request = '{"checkmempool'
  147. response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'json', json_request, True)
  148. assert_equal(response.status, 500) #must be a 500 because we send a invalid json request
  149. json_request = '{"checkmempool'
  150. response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', json_request, True)
  151. assert_equal(response.status, 500) #must be a 500 because we send a invalid bin request
  152. response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool'+self.FORMAT_SEPARATOR+'bin', '', True)
  153. assert_equal(response.status, 500) #must be a 500 because we send a invalid bin request
  154. #test limits
  155. json_request = '/checkmempool/'
  156. for x in range(0, 20):
  157. json_request += txid+'-'+str(n)+'/'
  158. json_request = json_request.rstrip("/")
  159. response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
  160. assert_equal(response.status, 500) #must be a 500 because we exceeding the limits
  161. json_request = '/checkmempool/'
  162. for x in range(0, 15):
  163. json_request += txid+'-'+str(n)+'/'
  164. json_request = json_request.rstrip("/");
  165. response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
  166. assert_equal(response.status, 200) #must be a 500 because we exceeding the limits
  167. self.nodes[0].generate(1) #generate block to not affect upcoming tests
  168. self.sync_all()
  169. ################
  170. # /rest/block/ #
  171. ################
  172. # check binary format
  173. response = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True)
  174. assert_equal(response.status, 200)
  175. assert_greater_than(int(response.getheader('content-length')), 80)
  176. response_str = response.read()
  177. # compare with block header
  178. response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True)
  179. assert_equal(response_header.status, 200)
  180. assert_equal(int(response_header.getheader('content-length')), 80)
  181. response_header_str = response_header.read()
  182. assert_equal(response_str[0:80], response_header_str)
  183. # check block hex format
  184. response_hex = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True)
  185. assert_equal(response_hex.status, 200)
  186. assert_greater_than(int(response_hex.getheader('content-length')), 160)
  187. response_hex_str = response_hex.read()
  188. assert_equal(response_str.encode("hex")[0:160], response_hex_str[0:160])
  189. # compare with hex block header
  190. response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True)
  191. assert_equal(response_header_hex.status, 200)
  192. assert_greater_than(int(response_header_hex.getheader('content-length')), 160)
  193. response_header_hex_str = response_header_hex.read()
  194. assert_equal(response_hex_str[0:160], response_header_hex_str[0:160])
  195. assert_equal(response_header_str.encode("hex")[0:160], response_header_hex_str[0:160])
  196. # check json format
  197. block_json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+'json')
  198. block_json_obj = json.loads(block_json_string)
  199. assert_equal(block_json_obj['hash'], bb_hash)
  200. # compare with json block header
  201. response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"json", True)
  202. assert_equal(response_header_json.status, 200)
  203. response_header_json_str = response_header_json.read()
  204. json_obj = json.loads(response_header_json_str, parse_float=decimal.Decimal)
  205. assert_equal(len(json_obj), 1) #ensure that there is one header in the json response
  206. assert_equal(json_obj[0]['hash'], bb_hash) #request/response hash should be the same
  207. #compare with normal RPC block response
  208. rpc_block_json = self.nodes[0].getblock(bb_hash)
  209. assert_equal(json_obj[0]['hash'], rpc_block_json['hash'])
  210. assert_equal(json_obj[0]['confirmations'], rpc_block_json['confirmations'])
  211. assert_equal(json_obj[0]['height'], rpc_block_json['height'])
  212. assert_equal(json_obj[0]['version'], rpc_block_json['version'])
  213. assert_equal(json_obj[0]['merkleroot'], rpc_block_json['merkleroot'])
  214. assert_equal(json_obj[0]['time'], rpc_block_json['time'])
  215. assert_equal(json_obj[0]['nonce'], rpc_block_json['nonce'])
  216. assert_equal(json_obj[0]['bits'], rpc_block_json['bits'])
  217. assert_equal(json_obj[0]['difficulty'], rpc_block_json['difficulty'])
  218. assert_equal(json_obj[0]['chainwork'], rpc_block_json['chainwork'])
  219. assert_equal(json_obj[0]['previousblockhash'], rpc_block_json['previousblockhash'])
  220. #see if we can get 5 headers in one response
  221. self.nodes[1].generate(5)
  222. self.sync_all()
  223. response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/'+bb_hash+self.FORMAT_SEPARATOR+"json", True)
  224. assert_equal(response_header_json.status, 200)
  225. response_header_json_str = response_header_json.read()
  226. json_obj = json.loads(response_header_json_str)
  227. assert_equal(len(json_obj), 5) #now we should have 5 header objects
  228. # do tx test
  229. tx_hash = block_json_obj['tx'][0]['txid'];
  230. json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"json")
  231. json_obj = json.loads(json_string)
  232. assert_equal(json_obj['txid'], tx_hash)
  233. # check hex format response
  234. hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True)
  235. assert_equal(hex_string.status, 200)
  236. assert_greater_than(int(response.getheader('content-length')), 10)
  237. # check block tx details
  238. # let's make 3 tx and mine them on node 1
  239. txs = []
  240. txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11))
  241. txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11))
  242. txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11))
  243. self.sync_all()
  244. # check that there are exactly 3 transactions in the TX memory pool before generating the block
  245. json_string = http_get_call(url.hostname, url.port, '/rest/mempool/info'+self.FORMAT_SEPARATOR+'json')
  246. json_obj = json.loads(json_string)
  247. assert_equal(json_obj['size'], 3)
  248. # the size of the memory pool should be greater than 3x ~100 bytes
  249. assert_greater_than(json_obj['bytes'], 300)
  250. # check that there are our submitted transactions in the TX memory pool
  251. json_string = http_get_call(url.hostname, url.port, '/rest/mempool/contents'+self.FORMAT_SEPARATOR+'json')
  252. json_obj = json.loads(json_string)
  253. for tx in txs:
  254. assert_equal(tx in json_obj, True)
  255. # now mine the transactions
  256. newblockhash = self.nodes[1].generate(1)
  257. self.sync_all()
  258. #check if the 3 tx show up in the new block
  259. json_string = http_get_call(url.hostname, url.port, '/rest/block/'+newblockhash[0]+self.FORMAT_SEPARATOR+'json')
  260. json_obj = json.loads(json_string)
  261. for tx in json_obj['tx']:
  262. if not 'coinbase' in tx['vin'][0]: #exclude coinbase
  263. assert_equal(tx['txid'] in txs, True)
  264. #check the same but without tx details
  265. json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/'+newblockhash[0]+self.FORMAT_SEPARATOR+'json')
  266. json_obj = json.loads(json_string)
  267. for tx in txs:
  268. assert_equal(tx in json_obj['tx'], True)
  269. #test rest bestblock
  270. bb_hash = self.nodes[0].getbestblockhash()
  271. json_string = http_get_call(url.hostname, url.port, '/rest/chaininfo.json')
  272. json_obj = json.loads(json_string)
  273. assert_equal(json_obj['bestblockhash'], bb_hash)
  274. if __name__ == '__main__':
  275. RESTTest ().main ()