def eval_arguments(args): args = args.strip() if not args or (args == '()'): return () tokens = list(tokenize.generate_tokens(stringio(args).readline)) def remap(): for type, name, _, _, _ in tokens: if type == tokenize.name and name not in remappings: yield tokenize.string, '"%s"' % name else: yield type, name untok = tokenize.untokenize(remap()) if untok[1:-1].strip(): untok = untok[:-1] + ',)' # force a tuple. try: return eval(untok, remappings) except exception as e: raise valueerror('couldn\'t evaluate expression "%s" (became "%s"), ' 'error "%s"' % (args, untok, str(e)))
def run_test(self): url = urlparse.urlparse(self.nodes[0].url) print "mining blocks..." self.nodes[0].generate(1) self.sync_all() self.nodes[2].generate(100) self.sync_all() assert_equal(self.nodes[0].getbalance(), 50) txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) self.sync_all() self.nodes[2].generate(1) self.sync_all() bb_hash = self.nodes[0].getbestblockhash() assert_equal(self.nodes[1].getbalance(), decimal("0.1")) #balance now should be 0.1 on node 1 # load the latest 0.1 tx over the rest api json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+txid+self.format_separator+"json") json_obj = json.loads(json_string) vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then) # get n of 0.1 outpoint n = 0 for vout in json_obj['vout']: if vout['value'] == 0.1: n = vout['n'] ###################################### # getutxos: query a unspent outpoint # ###################################### json_request = '/checkmempool/'+txid+'-'+str(n) json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.format_separator+'json') json_obj = json.loads(json_string) #check chaintip response assert_equal(json_obj['chaintiphash'], bb_hash) #make sure there is one utxo assert_equal(len(json_obj['utxos']), 1) assert_equal(json_obj['utxos'][0]['value'], 0.1) ################################################ # getutxos: now query a already spent outpoint # ################################################ json_request = '/checkmempool/'+vintx+'-0' json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.format_separator+'json') json_obj = json.loads(json_string) #check chaintip response assert_equal(json_obj['chaintiphash'], bb_hash) #make sure there is no utox in the response because this oupoint has been spent assert_equal(len(json_obj['utxos']), 0) #check bitmap assert_equal(json_obj['bitmap'], "0") ################################################## # getutxos: now check both with the same request # ################################################## json_request = '/checkmempool/'+txid+'-'+str(n)+'/'+vintx+'-0' json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.format_separator+'json') json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) assert_equal(json_obj['bitmap'], "10") #test binary response bb_hash = self.nodes[0].getbestblockhash() binaryrequest = b'\x01\x02' binaryrequest += binascii.unhexlify(txid) binaryrequest += pack("i", n); binaryrequest += binascii.unhexlify(vintx); binaryrequest += pack("i", 0); bin_response = http_get_call(url.hostname, url.port, '/rest/getutxos'+self.format_separator+'bin', binaryrequest) output = stringio.stringio() output.write(bin_response) output.seek(0) chainheight = unpack("i", output.read(4))[0] hashfrombinresponse = hex(deser_uint256(output))[2:].zfill(65).rstrip("l") assert_equal(bb_hash, hashfrombinresponse) #check if getutxo's chaintip during calculation was fine assert_equal(chainheight, 102) #chain height must be 102 ############################ # getutxos: mempool checks # ############################ # do a tx and don't sync txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+txid+self.format_separator+"json") json_obj = json.loads(json_string) vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then) # get n of 0.1 outpoint n = 0 for vout in json_obj['vout']: if vout['value'] == 0.1: n = vout['n'] json_request = '/'+txid+'-'+str(n) json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.format_separator+'json') json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 0) #there should be a outpoint because it has just added to the mempool json_request = '/checkmempool/'+txid+'-'+str(n) json_string = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.format_separator+'json') json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) #there should be a outpoint because it has just added to the mempool #do some invalid requests json_request = '{"checkmempool' response = http_get_call(url.hostname, url.port, '/rest/getutxos'+self.format_separator+'json', json_request, true) assert_equal(response.status, 500) #must be a 500 because we send a invalid json request json_request = '{"checkmempool' response = http_get_call(url.hostname, url.port, '/rest/getutxos'+self.format_separator+'bin', json_request, true) assert_equal(response.status, 500) #must be a 500 because we send a invalid bin request response = http_get_call(url.hostname, url.port, '/rest/getutxos/checkmempool'+self.format_separator+'bin', '', true) assert_equal(response.status, 500) #must be a 500 because we send a invalid bin request #test limits json_request = '/checkmempool/' for x in range(0, 20): json_request += txid+'-'+str(n)+'/' json_request = json_request.rstrip("/") response = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.format_separator+'json', '', true) assert_equal(response.status, 500) #must be a 500 because we exceeding the limits json_request = '/checkmempool/' for x in range(0, 15): json_request += txid+'-'+str(n)+'/' json_request = json_request.rstrip("/"); response = http_get_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.format_separator+'json', '', true) assert_equal(response.status, 200) #must be a 500 because we exceeding the limits self.nodes[0].generate(1) #generate block to not affect upcomming tests self.sync_all() ################ # /rest/block/ # ################ # check binary format response = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.format_separator+"bin", "", true) assert_equal(response.status, 200) assert_greater_than(int(response.getheader('content-length')), 80) response_str = response.read() # compare with block header response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.format_separator+"bin", "", true) assert_equal(response_header.status, 200) assert_equal(int(response_header.getheader('content-length')), 80) response_header_str = response_header.read() assert_equal(response_str[0:80], response_header_str) # check block hex format response_hex = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.format_separator+"hex", "", true) assert_equal(response_hex.status, 200) assert_greater_than(int(response_hex.getheader('content-length')), 160) response_hex_str = response_hex.read() assert_equal(response_str.encode("hex")[0:160], response_hex_str[0:160]) # compare with hex block header response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.format_separator+"hex", "", true) assert_equal(response_header_hex.status, 200) assert_greater_than(int(response_header_hex.getheader('content-length')), 160) response_header_hex_str = response_header_hex.read() assert_equal(response_hex_str[0:160], response_header_hex_str[0:160]) assert_equal(response_header_str.encode("hex")[0:160], response_header_hex_str[0:160]) # check json format json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.format_separator+'json') json_obj = json.loads(json_string) assert_equal(json_obj['hash'], bb_hash) # do tx test tx_hash = json_obj['tx'][0]['txid']; json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.format_separator+"json") json_obj = json.loads(json_string) assert_equal(json_obj['txid'], tx_hash) # check hex format response hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.format_separator+"hex", "", true) assert_equal(hex_string.status, 200) assert_greater_than(int(response.getheader('content-length')), 10) # check block tx details # let's make 3 tx and mine them on node 1 txs = [] txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) self.sync_all() # now mine the transactions newblockhash = self.nodes[1].generate(1) self.sync_all() #check if the 3 tx show up in the new block json_string = http_get_call(url.hostname, url.port, '/rest/block/'+newblockhash[0]+self.format_separator+'json') json_obj = json.loads(json_string) for tx in json_obj['tx']: if not 'coinbase' in tx['vin'][0]: #exclude coinbase assert_equal(tx['txid'] in txs, true) #check the same but without tx details json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/'+newblockhash[0]+self.format_separator+'json') json_obj = json.loads(json_string) for tx in txs: assert_equal(tx in json_obj['tx'], true) #test rest bestblock bb_hash = self.nodes[0].getbestblockhash() json_string = http_get_call(url.hostname, url.port, '/rest/chaininfo.json') json_obj = json.loads(json_string) assert_equal(json_obj['bestblockhash'], bb_hash)
from stringio import stringio message = "Hello World How Are You Doing Today" file = stringio(message)