Browse Source

[qa] py2: Unfiddle strings into bytes explicitly

0.13
MarcoFalke 9 years ago
parent
commit
faa41ee204
  1. 3
      qa/rpc-tests/bip65-cltv-p2p.py
  2. 5
      qa/rpc-tests/bip68-112-113-p2p.py
  3. 9
      qa/rpc-tests/bip9-softforks.py
  4. 5
      qa/rpc-tests/bipdersig-p2p.py
  5. 22
      qa/rpc-tests/decodescript.py
  6. 3
      qa/rpc-tests/fundrawtransaction.py
  7. 2
      qa/rpc-tests/getblocktemplate_proposals.py
  8. 11
      qa/rpc-tests/httpbasics.py
  9. 10
      qa/rpc-tests/invalidblockrequest.py
  10. 6
      qa/rpc-tests/invalidtxrequest.py
  11. 7
      qa/rpc-tests/listtransactions.py
  12. 13
      qa/rpc-tests/multi_rpc.py
  13. 2
      qa/rpc-tests/p2p-feefilter.py
  14. 32
      qa/rpc-tests/p2p-fullblocktest.py
  15. 8
      qa/rpc-tests/proxy_test.py
  16. 2
      qa/rpc-tests/pruning.py
  17. 2
      qa/rpc-tests/rawtransactions.py
  18. 10
      qa/rpc-tests/replace-by-fee.py
  19. 15
      qa/rpc-tests/rest.py
  20. 2
      qa/rpc-tests/test_framework/blocktools.py
  21. 6
      qa/rpc-tests/test_framework/comptool.py
  22. 186
      qa/rpc-tests/test_framework/mininode.py
  23. 11
      qa/rpc-tests/test_framework/netutil.py
  24. 4
      qa/rpc-tests/test_framework/script.py
  25. 2
      qa/rpc-tests/test_framework/socks5.py
  26. 15
      qa/rpc-tests/test_framework/util.py
  27. 22
      qa/rpc-tests/zmq_test.py
  28. 3
      src/test/bctest.py

3
qa/rpc-tests/bip65-cltv-p2p.py

@ -10,7 +10,6 @@ from test_framework.mininode import CTransaction, NetworkThread
from test_framework.blocktools import create_coinbase, create_block from test_framework.blocktools import create_coinbase, create_block
from test_framework.comptool import TestInstance, TestManager from test_framework.comptool import TestInstance, TestManager
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP
from binascii import unhexlify
from io import BytesIO from io import BytesIO
import time import time
@ -60,7 +59,7 @@ class BIP65Test(ComparisonTestFramework):
rawtx = node.createrawtransaction(inputs, outputs) rawtx = node.createrawtransaction(inputs, outputs)
signresult = node.signrawtransaction(rawtx) signresult = node.signrawtransaction(rawtx)
tx = CTransaction() tx = CTransaction()
f = BytesIO(unhexlify(signresult['hex'])) f = BytesIO(hex_str_to_bytes(signresult['hex']))
tx.deserialize(f) tx.deserialize(f)
return tx return tx

5
qa/rpc-tests/bip68-112-113-p2p.py

@ -10,7 +10,6 @@ from test_framework.mininode import ToHex, CTransaction, NetworkThread
from test_framework.blocktools import create_coinbase, create_block from test_framework.blocktools import create_coinbase, create_block
from test_framework.comptool import TestInstance, TestManager from test_framework.comptool import TestInstance, TestManager
from test_framework.script import * from test_framework.script import *
from binascii import unhexlify
from io import BytesIO from io import BytesIO
import time import time
@ -119,7 +118,7 @@ class BIP68_112_113Test(ComparisonTestFramework):
outputs = { to_address : amount } outputs = { to_address : amount }
rawtx = node.createrawtransaction(inputs, outputs) rawtx = node.createrawtransaction(inputs, outputs)
tx = CTransaction() tx = CTransaction()
f = BytesIO(unhexlify(rawtx)) f = BytesIO(hex_str_to_bytes(rawtx))
tx.deserialize(f) tx.deserialize(f)
return tx return tx
@ -127,7 +126,7 @@ class BIP68_112_113Test(ComparisonTestFramework):
rawtx = ToHex(unsignedtx) rawtx = ToHex(unsignedtx)
signresult = node.signrawtransaction(rawtx) signresult = node.signrawtransaction(rawtx)
tx = CTransaction() tx = CTransaction()
f = BytesIO(unhexlify(signresult['hex'])) f = BytesIO(hex_str_to_bytes(signresult['hex']))
tx.deserialize(f) tx.deserialize(f)
return tx return tx

9
qa/rpc-tests/bip9-softforks.py

@ -10,7 +10,6 @@ from test_framework.mininode import CTransaction, NetworkThread
from test_framework.blocktools import create_coinbase, create_block from test_framework.blocktools import create_coinbase, create_block
from test_framework.comptool import TestInstance, TestManager from test_framework.comptool import TestInstance, TestManager
from test_framework.script import CScript, OP_1NEGATE, OP_NOP3, OP_DROP from test_framework.script import CScript, OP_1NEGATE, OP_NOP3, OP_DROP
from binascii import hexlify, unhexlify
from io import BytesIO from io import BytesIO
import time import time
import itertools import itertools
@ -30,7 +29,6 @@ test that enforcement has triggered
''' '''
class BIP9SoftForksTest(ComparisonTestFramework): class BIP9SoftForksTest(ComparisonTestFramework):
def __init__(self): def __init__(self):
@ -53,15 +51,15 @@ class BIP9SoftForksTest(ComparisonTestFramework):
outputs = { to_address : amount } outputs = { to_address : amount }
rawtx = node.createrawtransaction(inputs, outputs) rawtx = node.createrawtransaction(inputs, outputs)
tx = CTransaction() tx = CTransaction()
f = BytesIO(unhexlify(rawtx)) f = BytesIO(hex_str_to_bytes(rawtx))
tx.deserialize(f) tx.deserialize(f)
tx.nVersion = 2 tx.nVersion = 2
return tx return tx
def sign_transaction(self, node, tx): def sign_transaction(self, node, tx):
signresult = node.signrawtransaction(hexlify(tx.serialize())) signresult = node.signrawtransaction(bytes_to_hex_str(tx.serialize()))
tx = CTransaction() tx = CTransaction()
f = BytesIO(unhexlify(signresult['hex'])) f = BytesIO(hex_str_to_bytes(signresult['hex']))
tx.deserialize(f) tx.deserialize(f)
return tx return tx
@ -184,7 +182,6 @@ class BIP9SoftForksTest(ComparisonTestFramework):
NetworkThread().start() # Start up network handling in another thread NetworkThread().start() # Start up network handling in another thread
def get_tests(self): def get_tests(self):
for test in itertools.chain( for test in itertools.chain(
self.test_BIP('csv', 536870913, self.sequence_lock_invalidate, self.donothing), self.test_BIP('csv', 536870913, self.sequence_lock_invalidate, self.donothing),

5
qa/rpc-tests/bipdersig-p2p.py

@ -10,7 +10,6 @@ from test_framework.mininode import CTransaction, NetworkThread
from test_framework.blocktools import create_coinbase, create_block from test_framework.blocktools import create_coinbase, create_block
from test_framework.comptool import TestInstance, TestManager from test_framework.comptool import TestInstance, TestManager
from test_framework.script import CScript from test_framework.script import CScript
from binascii import unhexlify
from io import BytesIO from io import BytesIO
import time import time
@ -25,7 +24,7 @@ def unDERify(tx):
newscript = [] newscript = []
for i in scriptSig: for i in scriptSig:
if (len(newscript) == 0): if (len(newscript) == 0):
newscript.append(i[0:-1] + '\0' + i[-1]) newscript.append(i[0:-1] + b'\0' + i[-1:])
else: else:
newscript.append(i) newscript.append(i)
tx.vin[0].scriptSig = CScript(newscript) tx.vin[0].scriptSig = CScript(newscript)
@ -68,7 +67,7 @@ class BIP66Test(ComparisonTestFramework):
rawtx = node.createrawtransaction(inputs, outputs) rawtx = node.createrawtransaction(inputs, outputs)
signresult = node.signrawtransaction(rawtx) signresult = node.signrawtransaction(rawtx)
tx = CTransaction() tx = CTransaction()
f = BytesIO(unhexlify(signresult['hex'])) f = BytesIO(hex_str_to_bytes(signresult['hex']))
tx.deserialize(f) tx.deserialize(f)
return tx return tx

22
qa/rpc-tests/decodescript.py

@ -6,7 +6,6 @@
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import *
from test_framework.mininode import * from test_framework.mininode import *
from binascii import hexlify, unhexlify
from io import BytesIO from io import BytesIO
class DecodeScriptTest(BitcoinTestFramework): class DecodeScriptTest(BitcoinTestFramework):
@ -131,7 +130,7 @@ class DecodeScriptTest(BitcoinTestFramework):
assert_equal('OP_DUP OP_HASH160 dc863734a218bfe83ef770ee9d41a27f824a6e56 OP_EQUALVERIFY OP_CHECKSIG', rpc_result['vout'][0]['scriptPubKey']['asm']) assert_equal('OP_DUP OP_HASH160 dc863734a218bfe83ef770ee9d41a27f824a6e56 OP_EQUALVERIFY OP_CHECKSIG', rpc_result['vout'][0]['scriptPubKey']['asm'])
assert_equal('OP_HASH160 2a5edea39971049a540474c6a99edf0aa4074c58 OP_EQUAL', rpc_result['vout'][1]['scriptPubKey']['asm']) assert_equal('OP_HASH160 2a5edea39971049a540474c6a99edf0aa4074c58 OP_EQUAL', rpc_result['vout'][1]['scriptPubKey']['asm'])
txSave = CTransaction() txSave = CTransaction()
txSave.deserialize(BytesIO(unhexlify(tx))) txSave.deserialize(BytesIO(hex_str_to_bytes(tx)))
# make sure that a specifically crafted op_return value will not pass all the IsDERSignature checks and then get decoded as a sighash type # make sure that a specifically crafted op_return value will not pass all the IsDERSignature checks and then get decoded as a sighash type
tx = '01000000015ded05872fdbda629c7d3d02b194763ce3b9b1535ea884e3c8e765d42e316724020000006b48304502204c10d4064885c42638cbff3585915b322de33762598321145ba033fc796971e2022100bb153ad3baa8b757e30a2175bd32852d2e1cb9080f84d7e32fcdfd667934ef1b012103163c0ff73511ea1743fb5b98384a2ff09dd06949488028fd819f4d83f56264efffffffff0200000000000000000b6a0930060201000201000180380100000000001976a9141cabd296e753837c086da7a45a6c2fe0d49d7b7b88ac00000000' tx = '01000000015ded05872fdbda629c7d3d02b194763ce3b9b1535ea884e3c8e765d42e316724020000006b48304502204c10d4064885c42638cbff3585915b322de33762598321145ba033fc796971e2022100bb153ad3baa8b757e30a2175bd32852d2e1cb9080f84d7e32fcdfd667934ef1b012103163c0ff73511ea1743fb5b98384a2ff09dd06949488028fd819f4d83f56264efffffffff0200000000000000000b6a0930060201000201000180380100000000001976a9141cabd296e753837c086da7a45a6c2fe0d49d7b7b88ac00000000'
@ -147,7 +146,7 @@ class DecodeScriptTest(BitcoinTestFramework):
# some more full transaction tests of varying specific scriptSigs. used instead of # some more full transaction tests of varying specific scriptSigs. used instead of
# tests in decodescript_script_sig because the decodescript RPC is specifically # tests in decodescript_script_sig because the decodescript RPC is specifically
# for working on scriptPubKeys (argh!). # for working on scriptPubKeys (argh!).
push_signature = hexlify(txSave.vin[0].scriptSig)[2:(0x48*2+4)] push_signature = bytes_to_hex_str(txSave.vin[0].scriptSig)[2:(0x48*2+4)]
signature = push_signature[2:] signature = push_signature[2:]
der_signature = signature[:-2] der_signature = signature[:-2]
signature_sighash_decoded = der_signature + '[ALL]' signature_sighash_decoded = der_signature + '[ALL]'
@ -156,25 +155,24 @@ class DecodeScriptTest(BitcoinTestFramework):
signature_2_sighash_decoded = der_signature + '[NONE|ANYONECANPAY]' signature_2_sighash_decoded = der_signature + '[NONE|ANYONECANPAY]'
# 1) P2PK scriptSig # 1) P2PK scriptSig
txSave.vin[0].scriptSig = unhexlify(push_signature) txSave.vin[0].scriptSig = hex_str_to_bytes(push_signature)
rpc_result = self.nodes[0].decoderawtransaction(hexlify(txSave.serialize())) rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
assert_equal(signature_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm']) assert_equal(signature_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
# make sure that the sighash decodes come out correctly for a more complex / lesser used case. # make sure that the sighash decodes come out correctly for a more complex / lesser used case.
txSave.vin[0].scriptSig = unhexlify(push_signature_2) txSave.vin[0].scriptSig = hex_str_to_bytes(push_signature_2)
rpc_result = self.nodes[0].decoderawtransaction(hexlify(txSave.serialize())) rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
assert_equal(signature_2_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm']) assert_equal(signature_2_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
# 2) multisig scriptSig # 2) multisig scriptSig
txSave.vin[0].scriptSig = unhexlify('00' + push_signature + push_signature_2) txSave.vin[0].scriptSig = hex_str_to_bytes('00' + push_signature + push_signature_2)
rpc_result = self.nodes[0].decoderawtransaction(hexlify(txSave.serialize())) rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
assert_equal('0 ' + signature_sighash_decoded + ' ' + signature_2_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm']) assert_equal('0 ' + signature_sighash_decoded + ' ' + signature_2_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
# 3) test a scriptSig that contains more than push operations. # 3) test a scriptSig that contains more than push operations.
# in fact, it contains an OP_RETURN with data specially crafted to cause improper decode if the code does not catch it. # in fact, it contains an OP_RETURN with data specially crafted to cause improper decode if the code does not catch it.
txSave.vin[0].scriptSig = unhexlify('6a143011020701010101010101020601010101010101') txSave.vin[0].scriptSig = hex_str_to_bytes('6a143011020701010101010101020601010101010101')
rpc_result = self.nodes[0].decoderawtransaction(hexlify(txSave.serialize())) rpc_result = self.nodes[0].decoderawtransaction(bytes_to_hex_str(txSave.serialize()))
print(hexlify('636174'))
assert_equal('OP_RETURN 3011020701010101010101020601010101010101', rpc_result['vin'][0]['scriptSig']['asm']) assert_equal('OP_RETURN 3011020701010101010101020601010101010101', rpc_result['vin'][0]['scriptSig']['asm'])
def run_test(self): def run_test(self):

3
qa/rpc-tests/fundrawtransaction.py

@ -148,7 +148,6 @@ class RawTransactionsTest(BitcoinTestFramework):
assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
##################################################################### #####################################################################
# test a fundrawtransaction with which will not get a change output # # test a fundrawtransaction with which will not get a change output #
##################################################################### #####################################################################
@ -178,7 +177,6 @@ class RawTransactionsTest(BitcoinTestFramework):
assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
######################################################################### #########################################################################
# test a fundrawtransaction with a VIN smaller than the required amount # # test a fundrawtransaction with a VIN smaller than the required amount #
######################################################################### #########################################################################
@ -484,7 +482,6 @@ class RawTransactionsTest(BitcoinTestFramework):
assert_equal(oldBalance+Decimal('51.10000000'), self.nodes[0].getbalance()) assert_equal(oldBalance+Decimal('51.10000000'), self.nodes[0].getbalance())
############################################### ###############################################
# multiple (~19) inputs tx test | Compare fee # # multiple (~19) inputs tx test | Compare fee #
############################################### ###############################################

2
qa/rpc-tests/getblocktemplate_proposals.py

@ -130,7 +130,7 @@ class GetBlockTemplateProposalTest(BitcoinTestFramework):
# Test 5: Add an invalid tx to the end (non-duplicate) # Test 5: Add an invalid tx to the end (non-duplicate)
txlist.append(bytearray(txlist[0])) txlist.append(bytearray(txlist[0]))
txlist[-1][4+1] = b'\xff' txlist[-1][4+1] = 0xff
assert_template(node, tmpl, txlist, 'bad-txns-inputs-missingorspent') assert_template(node, tmpl, txlist, 'bad-txns-inputs-missingorspent')
txlist.pop() txlist.pop()

11
qa/rpc-tests/httpbasics.py

@ -9,7 +9,6 @@
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import *
import base64
try: try:
import http.client as httplib import http.client as httplib
@ -31,7 +30,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
################################################# #################################################
url = urlparse.urlparse(self.nodes[0].url) url = urlparse.urlparse(self.nodes[0].url)
authpair = url.username + ':' + url.password authpair = url.username + ':' + url.password
headers = {"Authorization": "Basic " + base64.b64encode(authpair)} headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
conn = httplib.HTTPConnection(url.hostname, url.port) conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect() conn.connect()
@ -48,7 +47,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
conn.close() conn.close()
#same should be if we add keep-alive because this should be the std. behaviour #same should be if we add keep-alive because this should be the std. behaviour
headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection": "keep-alive"} headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection": "keep-alive"}
conn = httplib.HTTPConnection(url.hostname, url.port) conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect() conn.connect()
@ -65,7 +64,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
conn.close() conn.close()
#now do the same with "Connection: close" #now do the same with "Connection: close"
headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection":"close"} headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection":"close"}
conn = httplib.HTTPConnection(url.hostname, url.port) conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect() conn.connect()
@ -77,7 +76,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
#node1 (2nd node) is running with disabled keep-alive option #node1 (2nd node) is running with disabled keep-alive option
urlNode1 = urlparse.urlparse(self.nodes[1].url) urlNode1 = urlparse.urlparse(self.nodes[1].url)
authpair = urlNode1.username + ':' + urlNode1.password authpair = urlNode1.username + ':' + urlNode1.password
headers = {"Authorization": "Basic " + base64.b64encode(authpair)} headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
conn = httplib.HTTPConnection(urlNode1.hostname, urlNode1.port) conn = httplib.HTTPConnection(urlNode1.hostname, urlNode1.port)
conn.connect() conn.connect()
@ -88,7 +87,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
#node2 (third node) is running with standard keep-alive parameters which means keep-alive is on #node2 (third node) is running with standard keep-alive parameters which means keep-alive is on
urlNode2 = urlparse.urlparse(self.nodes[2].url) urlNode2 = urlparse.urlparse(self.nodes[2].url)
authpair = urlNode2.username + ':' + urlNode2.password authpair = urlNode2.username + ':' + urlNode2.password
headers = {"Authorization": "Basic " + base64.b64encode(authpair)} headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port) conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
conn.connect() conn.connect()

10
qa/rpc-tests/invalidblockrequest.py

@ -77,9 +77,9 @@ class InvalidBlockRequestTest(ComparisonTestFramework):
block2 = create_block(self.tip, create_coinbase(height), self.block_time) block2 = create_block(self.tip, create_coinbase(height), self.block_time)
self.block_time += 1 self.block_time += 1
# chr(81) is OP_TRUE # b'0x51' is OP_TRUE
tx1 = create_transaction(self.block1.vtx[0], 0, chr(81), 50 * COIN) tx1 = create_transaction(self.block1.vtx[0], 0, b'\x51', 50 * COIN)
tx2 = create_transaction(tx1, 0, chr(81), 50 * COIN) tx2 = create_transaction(tx1, 0, b'\x51', 50 * COIN)
block2.vtx.extend([tx1, tx2]) block2.vtx.extend([tx1, tx2])
block2.hashMerkleRoot = block2.calc_merkle_root() block2.hashMerkleRoot = block2.calc_merkle_root()
@ -95,7 +95,7 @@ class InvalidBlockRequestTest(ComparisonTestFramework):
assert(block2_orig.vtx != block2.vtx) assert(block2_orig.vtx != block2.vtx)
self.tip = block2.sha256 self.tip = block2.sha256
yield TestInstance([[block2, RejectResult(16,'bad-txns-duplicate')], [block2_orig, True]]) yield TestInstance([[block2, RejectResult(16, b'bad-txns-duplicate')], [block2_orig, True]])
height += 1 height += 1
''' '''
@ -110,7 +110,7 @@ class InvalidBlockRequestTest(ComparisonTestFramework):
block3.rehash() block3.rehash()
block3.solve() block3.solve()
yield TestInstance([[block3, RejectResult(16,'bad-cb-amount')]]) yield TestInstance([[block3, RejectResult(16, b'bad-cb-amount')]])
if __name__ == '__main__': if __name__ == '__main__':

6
qa/rpc-tests/invalidtxrequest.py

@ -61,10 +61,10 @@ class InvalidTxRequestTest(ComparisonTestFramework):
height += 1 height += 1
yield test yield test
# chr(100) is OP_NOTIF # b'\x64' is OP_NOTIF
# Transaction will be rejected with code 16 (REJECT_INVALID) # Transaction will be rejected with code 16 (REJECT_INVALID)
tx1 = create_transaction(self.block1.vtx[0], 0, chr(100), 50 * COIN - 12000) tx1 = create_transaction(self.block1.vtx[0], 0, b'\x64', 50 * COIN - 12000)
yield TestInstance([[tx1, RejectResult(16, 'mandatory-script-verify-flag-failed')]]) yield TestInstance([[tx1, RejectResult(16, b'mandatory-script-verify-flag-failed')]])
# TODO: test further transactions... # TODO: test further transactions...

7
qa/rpc-tests/listtransactions.py

@ -9,11 +9,10 @@ from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import *
from test_framework.mininode import CTransaction, COIN from test_framework.mininode import CTransaction, COIN
from io import BytesIO from io import BytesIO
import binascii
def txFromHex(hexstring): def txFromHex(hexstring):
tx = CTransaction() tx = CTransaction()
f = BytesIO(binascii.unhexlify(hexstring)) f = BytesIO(hex_str_to_bytes(hexstring))
tx.deserialize(f) tx.deserialize(f)
return tx return tx
@ -167,7 +166,7 @@ class ListTransactionsTest(BitcoinTestFramework):
tx3 = self.nodes[0].createrawtransaction(inputs, outputs) tx3 = self.nodes[0].createrawtransaction(inputs, outputs)
tx3_modified = txFromHex(tx3) tx3_modified = txFromHex(tx3)
tx3_modified.vin[0].nSequence = 0 tx3_modified.vin[0].nSequence = 0
tx3 = binascii.hexlify(tx3_modified.serialize()).decode('utf-8') tx3 = bytes_to_hex_str(tx3_modified.serialize())
tx3_signed = self.nodes[0].signrawtransaction(tx3)['hex'] tx3_signed = self.nodes[0].signrawtransaction(tx3)['hex']
txid_3 = self.nodes[0].sendrawtransaction(tx3_signed) txid_3 = self.nodes[0].sendrawtransaction(tx3_signed)
@ -193,7 +192,7 @@ class ListTransactionsTest(BitcoinTestFramework):
# Replace tx3, and check that tx4 becomes unknown # Replace tx3, and check that tx4 becomes unknown
tx3_b = tx3_modified tx3_b = tx3_modified
tx3_b.vout[0].nValue -= int(Decimal("0.004") * COIN) # bump the fee tx3_b.vout[0].nValue -= int(Decimal("0.004") * COIN) # bump the fee
tx3_b = binascii.hexlify(tx3_b.serialize()).decode('utf-8') tx3_b = bytes_to_hex_str(tx3_b.serialize())
tx3_b_signed = self.nodes[0].signrawtransaction(tx3_b)['hex'] tx3_b_signed = self.nodes[0].signrawtransaction(tx3_b)['hex']
txid_3b = self.nodes[0].sendrawtransaction(tx3_b_signed, True) txid_3b = self.nodes[0].sendrawtransaction(tx3_b_signed, True)
assert(is_opt_in(self.nodes[0], txid_3b)) assert(is_opt_in(self.nodes[0], txid_3b))

13
qa/rpc-tests/multi_rpc.py

@ -53,7 +53,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI=" password2 = "8/F3uMDw4KSEbw96U3CA1C4X05dkHDN2BPFjTgZW4KI="
authpairnew = "rt:"+password authpairnew = "rt:"+password
headers = {"Authorization": "Basic " + base64.b64encode(authpair)} headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
conn = httplib.HTTPConnection(url.hostname, url.port) conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect() conn.connect()
@ -63,7 +63,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
conn.close() conn.close()
#Use new authpair to confirm both work #Use new authpair to confirm both work
headers = {"Authorization": "Basic " + base64.b64encode(authpairnew)} headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
conn = httplib.HTTPConnection(url.hostname, url.port) conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect() conn.connect()
@ -74,7 +74,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
#Wrong login name with rt's password #Wrong login name with rt's password
authpairnew = "rtwrong:"+password authpairnew = "rtwrong:"+password
headers = {"Authorization": "Basic " + base64.b64encode(authpairnew)} headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
conn = httplib.HTTPConnection(url.hostname, url.port) conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect() conn.connect()
@ -85,7 +85,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
#Wrong password for rt #Wrong password for rt
authpairnew = "rt:"+password+"wrong" authpairnew = "rt:"+password+"wrong"
headers = {"Authorization": "Basic " + base64.b64encode(authpairnew)} headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
conn = httplib.HTTPConnection(url.hostname, url.port) conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect() conn.connect()
@ -96,7 +96,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
#Correct for rt2 #Correct for rt2
authpairnew = "rt2:"+password2 authpairnew = "rt2:"+password2
headers = {"Authorization": "Basic " + base64.b64encode(authpairnew)} headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
conn = httplib.HTTPConnection(url.hostname, url.port) conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect() conn.connect()
@ -107,7 +107,7 @@ class HTTPBasicsTest (BitcoinTestFramework):
#Wrong password for rt2 #Wrong password for rt2
authpairnew = "rt2:"+password2+"wrong" authpairnew = "rt2:"+password2+"wrong"
headers = {"Authorization": "Basic " + base64.b64encode(authpairnew)} headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
conn = httplib.HTTPConnection(url.hostname, url.port) conn = httplib.HTTPConnection(url.hostname, url.port)
conn.connect() conn.connect()
@ -117,6 +117,5 @@ class HTTPBasicsTest (BitcoinTestFramework):
conn.close() conn.close()
if __name__ == '__main__': if __name__ == '__main__':
HTTPBasicsTest ().main () HTTPBasicsTest ().main ()

2
qa/rpc-tests/p2p-feefilter.py

@ -14,7 +14,7 @@ FeeFilterTest -- test processing of feefilter messages
''' '''
def hashToHex(hash): def hashToHex(hash):
return format(hash, '064x').decode('utf-8') return format(hash, '064x')
# Wait up to 60 secs to see if the testnode has received all the expected invs # Wait up to 60 secs to see if the testnode has received all the expected invs
def allInvsMatch(invsExpected, testnode): def allInvsMatch(invsExpected, testnode):

32
qa/rpc-tests/p2p-fullblocktest.py

@ -33,7 +33,7 @@ class FullBlockTest(ComparisonTestFramework):
self.num_nodes = 1 self.num_nodes = 1
self.block_heights = {} self.block_heights = {}
self.coinbase_key = CECKey() self.coinbase_key = CECKey()
self.coinbase_key.set_secretbytes(bytes("horsebattery")) self.coinbase_key.set_secretbytes(b"horsebattery")
self.coinbase_pubkey = self.coinbase_key.get_pubkey() self.coinbase_pubkey = self.coinbase_key.get_pubkey()
self.block_time = int(time.time())+1 self.block_time = int(time.time())+1
self.tip = None self.tip = None
@ -70,7 +70,7 @@ class FullBlockTest(ComparisonTestFramework):
block = create_block(base_block_hash, coinbase, self.block_time) block = create_block(base_block_hash, coinbase, self.block_time)
if (spend != None): if (spend != None):
tx = CTransaction() tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n), "", 0xffffffff)) # no signature yet tx.vin.append(CTxIn(COutPoint(spend.tx.sha256, spend.n), b"", 0xffffffff)) # no signature yet
# This copies the java comparison tool testing behavior: the first # This copies the java comparison tool testing behavior: the first
# txout has a garbage scriptPubKey, "to make sure we're not # txout has a garbage scriptPubKey, "to make sure we're not
# pre-verifying too much" (?) # pre-verifying too much" (?)
@ -80,7 +80,7 @@ class FullBlockTest(ComparisonTestFramework):
else: else:
tx.vout.append(CTxOut(1, script)) tx.vout.append(CTxOut(1, script))
# Now sign it if necessary # Now sign it if necessary
scriptSig = "" scriptSig = b""
scriptPubKey = bytearray(spend.tx.vout[spend.n].scriptPubKey) scriptPubKey = bytearray(spend.tx.vout[spend.n].scriptPubKey)
if (scriptPubKey[0] == OP_TRUE): # looks like an anyone-can-spend if (scriptPubKey[0] == OP_TRUE): # looks like an anyone-can-spend
scriptSig = CScript([OP_TRUE]) scriptSig = CScript([OP_TRUE])
@ -225,7 +225,7 @@ class FullBlockTest(ComparisonTestFramework):
# \-> b3 (1) -> b4 (2) # \-> b3 (1) -> b4 (2)
tip(6) tip(6)
block(9, spend=out4, additional_coinbase_value=1) block(9, spend=out4, additional_coinbase_value=1)
yield rejected(RejectResult(16, 'bad-cb-amount')) yield rejected(RejectResult(16, b'bad-cb-amount'))
# Create a fork that ends in a block with too much fee (the one that causes the reorg) # Create a fork that ends in a block with too much fee (the one that causes the reorg)
@ -237,7 +237,7 @@ class FullBlockTest(ComparisonTestFramework):
yield rejected() yield rejected()
block(11, spend=out4, additional_coinbase_value=1) block(11, spend=out4, additional_coinbase_value=1)
yield rejected(RejectResult(16, 'bad-cb-amount')) yield rejected(RejectResult(16, b'bad-cb-amount'))
# Try again, but with a valid fork first # Try again, but with a valid fork first
@ -279,7 +279,7 @@ class FullBlockTest(ComparisonTestFramework):
out6 = get_spendable_output() out6 = get_spendable_output()
too_many_checksigs = CScript([OP_CHECKSIG] * (1000000 // 50)) too_many_checksigs = CScript([OP_CHECKSIG] * (1000000 // 50))
block(16, spend=out6, script=too_many_checksigs) block(16, spend=out6, script=too_many_checksigs)
yield rejected(RejectResult(16, 'bad-blk-sigops')) yield rejected(RejectResult(16, b'bad-blk-sigops'))
# Attempt to spend a transaction created on a different fork # Attempt to spend a transaction created on a different fork
@ -288,7 +288,7 @@ class FullBlockTest(ComparisonTestFramework):
# \-> b3 (1) -> b4 (2) # \-> b3 (1) -> b4 (2)
tip(15) tip(15)
block(17, spend=txout_b3) block(17, spend=txout_b3)
yield rejected(RejectResult(16, 'bad-txns-inputs-missingorspent')) yield rejected(RejectResult(16, b'bad-txns-inputs-missingorspent'))
# Attempt to spend a transaction created on a different fork (on a fork this time) # Attempt to spend a transaction created on a different fork (on a fork this time)
# genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3) # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
@ -309,7 +309,7 @@ class FullBlockTest(ComparisonTestFramework):
tip(15) tip(15)
out7 = get_spendable_output() out7 = get_spendable_output()
block(20, spend=out7) block(20, spend=out7)
yield rejected(RejectResult(16, 'bad-txns-premature-spend-of-coinbase')) yield rejected(RejectResult(16, b'bad-txns-premature-spend-of-coinbase'))
# Attempt to spend a coinbase at depth too low (on a fork this time) # Attempt to spend a coinbase at depth too low (on a fork this time)
# genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3) # genesis -> b1 (0) -> b2 (1) -> b5 (2) -> b6 (3)
@ -333,7 +333,7 @@ class FullBlockTest(ComparisonTestFramework):
old_hash = b23.sha256 old_hash = b23.sha256
tx = CTransaction() tx = CTransaction()
script_length = MAX_BLOCK_SIZE - len(b23.serialize()) - 69 script_length = MAX_BLOCK_SIZE - len(b23.serialize()) - 69
script_output = CScript([chr(0)*script_length]) script_output = CScript([b'\x00' * script_length])
tx.vout.append(CTxOut(0, script_output)) tx.vout.append(CTxOut(0, script_output))
tx.vin.append(CTxIn(COutPoint(b23.vtx[1].sha256, 1))) tx.vin.append(CTxIn(COutPoint(b23.vtx[1].sha256, 1)))
b23 = update_block(23, [tx]) b23 = update_block(23, [tx])
@ -345,11 +345,11 @@ class FullBlockTest(ComparisonTestFramework):
tip(15) tip(15)
b24 = block(24, spend=out6) b24 = block(24, spend=out6)
script_length = MAX_BLOCK_SIZE - len(b24.serialize()) - 69 script_length = MAX_BLOCK_SIZE - len(b24.serialize()) - 69
script_output = CScript([chr(0)*(script_length+1)]) script_output = CScript([b'\x00' * (script_length+1)])
tx.vout = [CTxOut(0, script_output)] tx.vout = [CTxOut(0, script_output)]
b24 = update_block(24, [tx]) b24 = update_block(24, [tx])
assert_equal(len(b24.serialize()), MAX_BLOCK_SIZE+1) assert_equal(len(b24.serialize()), MAX_BLOCK_SIZE+1)
yield rejected(RejectResult(16, 'bad-blk-length')) yield rejected(RejectResult(16, b'bad-blk-length'))
b25 = block(25, spend=out7) b25 = block(25, spend=out7)
yield rejected() yield rejected()
@ -361,12 +361,12 @@ class FullBlockTest(ComparisonTestFramework):
# \-> b3 (1) -> b4 (2) # \-> b3 (1) -> b4 (2)
tip(15) tip(15)
b26 = block(26, spend=out6) b26 = block(26, spend=out6)
b26.vtx[0].vin[0].scriptSig = chr(0) b26.vtx[0].vin[0].scriptSig = b'\x00'
b26.vtx[0].rehash() b26.vtx[0].rehash()
# update_block causes the merkle root to get updated, even with no new # update_block causes the merkle root to get updated, even with no new
# transactions, and updates the required state. # transactions, and updates the required state.
b26 = update_block(26, []) b26 = update_block(26, [])
yield rejected(RejectResult(16, 'bad-cb-length')) yield rejected(RejectResult(16, b'bad-cb-length'))
# Extend the b26 chain to make sure bitcoind isn't accepting b26 # Extend the b26 chain to make sure bitcoind isn't accepting b26
b27 = block(27, spend=out7) b27 = block(27, spend=out7)
@ -375,10 +375,10 @@ class FullBlockTest(ComparisonTestFramework):
# Now try a too-large-coinbase script # Now try a too-large-coinbase script
tip(15) tip(15)
b28 = block(28, spend=out6) b28 = block(28, spend=out6)
b28.vtx[0].vin[0].scriptSig = chr(0)*101 b28.vtx[0].vin[0].scriptSig = b'\x00' * 101
b28.vtx[0].rehash() b28.vtx[0].rehash()
b28 = update_block(28, []) b28 = update_block(28, [])
yield rejected(RejectResult(16, 'bad-cb-length')) yield rejected(RejectResult(16, b'bad-cb-length'))
# Extend the b28 chain to make sure bitcoind isn't accepted b28 # Extend the b28 chain to make sure bitcoind isn't accepted b28
b29 = block(29, spend=out7) b29 = block(29, spend=out7)
@ -390,7 +390,7 @@ class FullBlockTest(ComparisonTestFramework):
# b30 has a max-sized coinbase scriptSig. # b30 has a max-sized coinbase scriptSig.
tip(23) tip(23)
b30 = block(30) b30 = block(30)
b30.vtx[0].vin[0].scriptSig = chr(0)*100 b30.vtx[0].vin[0].scriptSig = b'\x00' * 100
b30.vtx[0].rehash() b30.vtx[0].rehash()
b30 = update_block(30, []) b30 = update_block(30, [])
yield accepted() yield accepted()

8
qa/rpc-tests/proxy_test.py

@ -86,7 +86,7 @@ class ProxyTest(BitcoinTestFramework):
assert(isinstance(cmd, Socks5Command)) assert(isinstance(cmd, Socks5Command))
# Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6 # Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6
assert_equal(cmd.atyp, AddressType.DOMAINNAME) assert_equal(cmd.atyp, AddressType.DOMAINNAME)
assert_equal(cmd.addr, "15.61.23.23") assert_equal(cmd.addr, b"15.61.23.23")
assert_equal(cmd.port, 1234) assert_equal(cmd.port, 1234)
if not auth: if not auth:
assert_equal(cmd.username, None) assert_equal(cmd.username, None)
@ -100,7 +100,7 @@ class ProxyTest(BitcoinTestFramework):
assert(isinstance(cmd, Socks5Command)) assert(isinstance(cmd, Socks5Command))
# Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6 # Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6
assert_equal(cmd.atyp, AddressType.DOMAINNAME) assert_equal(cmd.atyp, AddressType.DOMAINNAME)
assert_equal(cmd.addr, "1233:3432:2434:2343:3234:2345:6546:4534") assert_equal(cmd.addr, b"1233:3432:2434:2343:3234:2345:6546:4534")
assert_equal(cmd.port, 5443) assert_equal(cmd.port, 5443)
if not auth: if not auth:
assert_equal(cmd.username, None) assert_equal(cmd.username, None)
@ -113,7 +113,7 @@ class ProxyTest(BitcoinTestFramework):
cmd = proxies[2].queue.get() cmd = proxies[2].queue.get()
assert(isinstance(cmd, Socks5Command)) assert(isinstance(cmd, Socks5Command))
assert_equal(cmd.atyp, AddressType.DOMAINNAME) assert_equal(cmd.atyp, AddressType.DOMAINNAME)
assert_equal(cmd.addr, "bitcoinostk4e4re.onion") assert_equal(cmd.addr, b"bitcoinostk4e4re.onion")
assert_equal(cmd.port, 8333) assert_equal(cmd.port, 8333)
if not auth: if not auth:
assert_equal(cmd.username, None) assert_equal(cmd.username, None)
@ -125,7 +125,7 @@ class ProxyTest(BitcoinTestFramework):
cmd = proxies[3].queue.get() cmd = proxies[3].queue.get()
assert(isinstance(cmd, Socks5Command)) assert(isinstance(cmd, Socks5Command))
assert_equal(cmd.atyp, AddressType.DOMAINNAME) assert_equal(cmd.atyp, AddressType.DOMAINNAME)
assert_equal(cmd.addr, "node.noumenon") assert_equal(cmd.addr, b"node.noumenon")
assert_equal(cmd.port, 8333) assert_equal(cmd.port, 8333)
if not auth: if not auth:
assert_equal(cmd.username, None) assert_equal(cmd.username, None)

2
qa/rpc-tests/pruning.py

@ -311,7 +311,7 @@ class PruneTest(BitcoinTestFramework):
# \ \ # \ \
# ++...++(1044) .. # ++...++(1044) ..
# #
# N0 ********************(1032) @@...@@@(1552) # N0 ********************(1032) @@...@@@(1552)
# \ # \
# *...**(1320) # *...**(1320)

2
qa/rpc-tests/rawtransactions.py

@ -88,8 +88,6 @@ class RawTransactionsTest(BitcoinTestFramework):
assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
# 2of3 test from different nodes # 2of3 test from different nodes
bal = self.nodes[2].getbalance() bal = self.nodes[2].getbalance()
addr1 = self.nodes[1].getnewaddress() addr1 = self.nodes[1].getnewaddress()

10
qa/rpc-tests/replace-by-fee.py

@ -11,15 +11,11 @@ from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import * from test_framework.util import *
from test_framework.script import * from test_framework.script import *
from test_framework.mininode import * from test_framework.mininode import *
import binascii
MAX_REPLACEMENT_LIMIT = 100 MAX_REPLACEMENT_LIMIT = 100
def satoshi_round(amount):
return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN)
def txToHex(tx): def txToHex(tx):
return binascii.hexlify(tx.serialize()).decode('utf-8') return bytes_to_hex_str(tx.serialize())
def make_utxo(node, amount, confirmed=True, scriptPubKey=CScript([1])): def make_utxo(node, amount, confirmed=True, scriptPubKey=CScript([1])):
"""Create a txout with a given amount and scriptPubKey """Create a txout with a given amount and scriptPubKey
@ -53,9 +49,7 @@ def make_utxo(node, amount, confirmed=True, scriptPubKey=CScript([1])):
tx2.vout = [CTxOut(amount, scriptPubKey)] tx2.vout = [CTxOut(amount, scriptPubKey)]
tx2.rehash() tx2.rehash()
binascii.hexlify(tx2.serialize()).decode('utf-8') signed_tx = node.signrawtransaction(txToHex(tx2))
signed_tx = node.signrawtransaction(binascii.hexlify(tx2.serialize()).decode('utf-8'))
txid = node.sendrawtransaction(signed_tx['hex'], True) txid = node.sendrawtransaction(signed_tx['hex'], True)

15
qa/rpc-tests/rest.py

@ -39,7 +39,7 @@ def http_get_call(host, port, path, response_object = 0):
if response_object: if response_object:
return conn.getresponse() return conn.getresponse()
return conn.getresponse().read() return conn.getresponse().read().decode('utf-8')
#allows simple http post calls with a request body #allows simple http post calls with a request body
def http_post_call(host, port, path, requestdata = '', response_object = 0): def http_post_call(host, port, path, requestdata = '', response_object = 0):
@ -141,9 +141,9 @@ class RESTTest (BitcoinTestFramework):
bb_hash = self.nodes[0].getbestblockhash() bb_hash = self.nodes[0].getbestblockhash()
binaryRequest = b'\x01\x02' binaryRequest = b'\x01\x02'
binaryRequest += binascii.unhexlify(txid) binaryRequest += hex_str_to_bytes(txid)
binaryRequest += pack("i", n) binaryRequest += pack("i", n)
binaryRequest += binascii.unhexlify(vintx) binaryRequest += hex_str_to_bytes(vintx)
binaryRequest += pack("i", 0) binaryRequest += pack("i", 0)
bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', binaryRequest) bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', binaryRequest)
@ -234,7 +234,7 @@ class RESTTest (BitcoinTestFramework):
assert_equal(response_hex.status, 200) assert_equal(response_hex.status, 200)
assert_greater_than(int(response_hex.getheader('content-length')), 160) assert_greater_than(int(response_hex.getheader('content-length')), 160)
response_hex_str = response_hex.read() response_hex_str = response_hex.read()
assert_equal(encode(response_str, "hex")[0:160], response_hex_str[0:160]) assert_equal(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160])
# compare with hex block header # 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) response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True)
@ -242,7 +242,7 @@ class RESTTest (BitcoinTestFramework):
assert_greater_than(int(response_header_hex.getheader('content-length')), 160) assert_greater_than(int(response_header_hex.getheader('content-length')), 160)
response_header_hex_str = response_header_hex.read() response_header_hex_str = response_header_hex.read()
assert_equal(response_hex_str[0:160], response_header_hex_str[0:160]) assert_equal(response_hex_str[0:160], response_header_hex_str[0:160])
assert_equal(encode(response_header_str, "hex")[0:160], response_header_hex_str[0:160]) assert_equal(encode(response_header_str, "hex_codec")[0:160], response_header_hex_str[0:160])
# check json format # check json format
block_json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+'json') block_json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+'json')
@ -252,7 +252,7 @@ class RESTTest (BitcoinTestFramework):
# compare with json block header # compare with json block header
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"json", True) response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"json", True)
assert_equal(response_header_json.status, 200) assert_equal(response_header_json.status, 200)
response_header_json_str = response_header_json.read() response_header_json_str = response_header_json.read().decode('utf-8')
json_obj = json.loads(response_header_json_str, parse_float=Decimal) json_obj = json.loads(response_header_json_str, parse_float=Decimal)
assert_equal(len(json_obj), 1) #ensure that there is one header in the json response assert_equal(len(json_obj), 1) #ensure that there is one header in the json response
assert_equal(json_obj[0]['hash'], bb_hash) #request/response hash should be the same assert_equal(json_obj[0]['hash'], bb_hash) #request/response hash should be the same
@ -276,7 +276,7 @@ class RESTTest (BitcoinTestFramework):
self.sync_all() self.sync_all()
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/'+bb_hash+self.FORMAT_SEPARATOR+"json", True) response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/'+bb_hash+self.FORMAT_SEPARATOR+"json", True)
assert_equal(response_header_json.status, 200) assert_equal(response_header_json.status, 200)
response_header_json_str = response_header_json.read() response_header_json_str = response_header_json.read().decode('utf-8')
json_obj = json.loads(response_header_json_str) json_obj = json.loads(response_header_json_str)
assert_equal(len(json_obj), 5) #now we should have 5 header objects assert_equal(len(json_obj), 5) #now we should have 5 header objects
@ -292,7 +292,6 @@ class RESTTest (BitcoinTestFramework):
assert_greater_than(int(response.getheader('content-length')), 10) assert_greater_than(int(response.getheader('content-length')), 10)
# check block tx details # check block tx details
# let's make 3 tx and mine them on node 1 # let's make 3 tx and mine them on node 1
txs = [] txs = []

2
qa/rpc-tests/test_framework/blocktools.py

@ -62,6 +62,6 @@ def create_transaction(prevtx, n, sig, value):
tx = CTransaction() tx = CTransaction()
assert(n < len(prevtx.vout)) assert(n < len(prevtx.vout))
tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff)) tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
tx.vout.append(CTxOut(value, "")) tx.vout.append(CTxOut(value, b""))
tx.calc_sha256() tx.calc_sha256()
return tx return tx

6
qa/rpc-tests/test_framework/comptool.py

@ -31,7 +31,7 @@ class RejectResult(object):
''' '''
Outcome that expects rejection of a transaction or block. Outcome that expects rejection of a transaction or block.
''' '''
def __init__(self, code, reason=''): def __init__(self, code, reason=b''):
self.code = code self.code = code
self.reason = reason self.reason = reason
def match(self, other): def match(self, other):
@ -97,9 +97,9 @@ class TestNode(NodeConnCB):
raise AssertionError("Got pong for unknown ping [%s]" % repr(message)) raise AssertionError("Got pong for unknown ping [%s]" % repr(message))
def on_reject(self, conn, message): def on_reject(self, conn, message):
if message.message == 'tx': if message.message == b'tx':
self.tx_reject_map[message.data] = RejectResult(message.code, message.reason) self.tx_reject_map[message.data] = RejectResult(message.code, message.reason)
if message.message == 'block': if message.message == b'block':
self.block_reject_map[message.data] = RejectResult(message.code, message.reason) self.block_reject_map[message.data] = RejectResult(message.code, message.reason)
def send_inv(self, obj): def send_inv(self, obj):

186
qa/rpc-tests/test_framework/mininode.py

@ -20,10 +20,10 @@
import struct import struct
import socket import socket
import asyncore import asyncore
import binascii
import time import time
import sys import sys
import random import random
from binascii import hexlify, unhexlify
from io import BytesIO from io import BytesIO
from codecs import encode from codecs import encode
import hashlib import hashlib
@ -34,7 +34,7 @@ import copy
BIP0031_VERSION = 60000 BIP0031_VERSION = 60000
MY_VERSION = 60001 # past bip-31 for ping/pong MY_VERSION = 60001 # past bip-31 for ping/pong
MY_SUBVERSION = "/python-mininode-tester:0.0.1/" MY_SUBVERSION = b"/python-mininode-tester:0.0.2/"
MAX_INV_SZ = 50000 MAX_INV_SZ = 50000
MAX_BLOCK_SIZE = 1000000 MAX_BLOCK_SIZE = 1000000
@ -131,7 +131,7 @@ def deser_vector(f, c):
def ser_vector(l): def ser_vector(l):
r = "" r = b""
if len(l) < 253: if len(l) < 253:
r = struct.pack("B", len(l)) r = struct.pack("B", len(l))
elif len(l) < 0x10000: elif len(l) < 0x10000:
@ -161,7 +161,7 @@ def deser_uint256_vector(f):
def ser_uint256_vector(l): def ser_uint256_vector(l):
r = "" r = b""
if len(l) < 253: if len(l) < 253:
r = struct.pack("B", len(l)) r = struct.pack("B", len(l))
elif len(l) < 0x10000: elif len(l) < 0x10000:
@ -221,7 +221,7 @@ def deser_int_vector(f):
def ser_int_vector(l): def ser_int_vector(l):
r = "" r = b""
if len(l) < 253: if len(l) < 253:
r = struct.pack("B", len(l)) r = struct.pack("B", len(l))
elif len(l) < 0x10000: elif len(l) < 0x10000:
@ -236,19 +236,19 @@ def ser_int_vector(l):
# Deserialize from a hex string representation (eg from RPC) # Deserialize from a hex string representation (eg from RPC)
def FromHex(obj, hex_string): def FromHex(obj, hex_string):
obj.deserialize(BytesIO(binascii.unhexlify(hex_string))) obj.deserialize(BytesIO(unhexlify(hex_string.encode('ascii'))))
return obj return obj
# Convert a binary-serializable object to hex (eg for submission via RPC) # Convert a binary-serializable object to hex (eg for submission via RPC)
def ToHex(obj): def ToHex(obj):
return binascii.hexlify(obj.serialize()).decode('utf-8') return hexlify(obj.serialize()).decode('ascii')
# Objects that map to bitcoind objects, which can be serialized/deserialized # Objects that map to bitcoind objects, which can be serialized/deserialized
class CAddress(object): class CAddress(object):
def __init__(self): def __init__(self):
self.nServices = 1 self.nServices = 1
self.pchReserved = "\x00" * 10 + "\xff" * 2 self.pchReserved = b"\x00" * 10 + b"\xff" * 2
self.ip = "0.0.0.0" self.ip = "0.0.0.0"
self.port = 0 self.port = 0
@ -259,7 +259,7 @@ class CAddress(object):
self.port = struct.unpack(">H", f.read(2))[0] self.port = struct.unpack(">H", f.read(2))[0]
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<Q", self.nServices) r += struct.pack("<Q", self.nServices)
r += self.pchReserved r += self.pchReserved
r += socket.inet_aton(self.ip) r += socket.inet_aton(self.ip)
@ -286,7 +286,7 @@ class CInv(object):
self.hash = deser_uint256(f) self.hash = deser_uint256(f)
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<i", self.type) r += struct.pack("<i", self.type)
r += ser_uint256(self.hash) r += ser_uint256(self.hash)
return r return r
@ -306,7 +306,7 @@ class CBlockLocator(object):
self.vHave = deser_uint256_vector(f) self.vHave = deser_uint256_vector(f)
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<i", self.nVersion) r += struct.pack("<i", self.nVersion)
r += ser_uint256_vector(self.vHave) r += ser_uint256_vector(self.vHave)
return r return r
@ -326,7 +326,7 @@ class COutPoint(object):
self.n = struct.unpack("<I", f.read(4))[0] self.n = struct.unpack("<I", f.read(4))[0]
def serialize(self): def serialize(self):
r = "" r = b""
r += ser_uint256(self.hash) r += ser_uint256(self.hash)
r += struct.pack("<I", self.n) r += struct.pack("<I", self.n)
return r return r
@ -336,7 +336,7 @@ class COutPoint(object):
class CTxIn(object): class CTxIn(object):
def __init__(self, outpoint=None, scriptSig="", nSequence=0): def __init__(self, outpoint=None, scriptSig=b"", nSequence=0):
if outpoint is None: if outpoint is None:
self.prevout = COutPoint() self.prevout = COutPoint()
else: else:
@ -351,7 +351,7 @@ class CTxIn(object):
self.nSequence = struct.unpack("<I", f.read(4))[0] self.nSequence = struct.unpack("<I", f.read(4))[0]
def serialize(self): def serialize(self):
r = "" r = b""
r += self.prevout.serialize() r += self.prevout.serialize()
r += ser_string(self.scriptSig) r += ser_string(self.scriptSig)
r += struct.pack("<I", self.nSequence) r += struct.pack("<I", self.nSequence)
@ -359,12 +359,12 @@ class CTxIn(object):
def __repr__(self): def __repr__(self):
return "CTxIn(prevout=%s scriptSig=%s nSequence=%i)" \ return "CTxIn(prevout=%s scriptSig=%s nSequence=%i)" \
% (repr(self.prevout), binascii.hexlify(self.scriptSig), % (repr(self.prevout), hexlify(self.scriptSig),
self.nSequence) self.nSequence)
class CTxOut(object): class CTxOut(object):
def __init__(self, nValue=0, scriptPubKey=""): def __init__(self, nValue=0, scriptPubKey=b""):
self.nValue = nValue self.nValue = nValue
self.scriptPubKey = scriptPubKey self.scriptPubKey = scriptPubKey
@ -373,7 +373,7 @@ class CTxOut(object):
self.scriptPubKey = deser_string(f) self.scriptPubKey = deser_string(f)
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<q", self.nValue) r += struct.pack("<q", self.nValue)
r += ser_string(self.scriptPubKey) r += ser_string(self.scriptPubKey)
return r return r
@ -381,7 +381,7 @@ class CTxOut(object):
def __repr__(self): def __repr__(self):
return "CTxOut(nValue=%i.%08i scriptPubKey=%s)" \ return "CTxOut(nValue=%i.%08i scriptPubKey=%s)" \
% (self.nValue // COIN, self.nValue % COIN, % (self.nValue // COIN, self.nValue % COIN,
binascii.hexlify(self.scriptPubKey)) hexlify(self.scriptPubKey))
class CTransaction(object): class CTransaction(object):
@ -410,7 +410,7 @@ class CTransaction(object):
self.hash = None self.hash = None
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<i", self.nVersion) r += struct.pack("<i", self.nVersion)
r += ser_vector(self.vin) r += ser_vector(self.vin)
r += ser_vector(self.vout) r += ser_vector(self.vout)
@ -424,7 +424,7 @@ class CTransaction(object):
def calc_sha256(self): def calc_sha256(self):
if self.sha256 is None: if self.sha256 is None:
self.sha256 = uint256_from_str(hash256(self.serialize())) self.sha256 = uint256_from_str(hash256(self.serialize()))
self.hash = encode(hash256(self.serialize())[::-1], 'hex') self.hash = encode(hash256(self.serialize())[::-1], 'hex_codec').decode('ascii')
def is_valid(self): def is_valid(self):
self.calc_sha256() self.calc_sha256()
@ -474,7 +474,7 @@ class CBlockHeader(object):
self.hash = None self.hash = None
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<i", self.nVersion) r += struct.pack("<i", self.nVersion)
r += ser_uint256(self.hashPrevBlock) r += ser_uint256(self.hashPrevBlock)
r += ser_uint256(self.hashMerkleRoot) r += ser_uint256(self.hashMerkleRoot)
@ -485,7 +485,7 @@ class CBlockHeader(object):
def calc_sha256(self): def calc_sha256(self):
if self.sha256 is None: if self.sha256 is None:
r = "" r = b""
r += struct.pack("<i", self.nVersion) r += struct.pack("<i", self.nVersion)
r += ser_uint256(self.hashPrevBlock) r += ser_uint256(self.hashPrevBlock)
r += ser_uint256(self.hashMerkleRoot) r += ser_uint256(self.hashMerkleRoot)
@ -493,7 +493,7 @@ class CBlockHeader(object):
r += struct.pack("<I", self.nBits) r += struct.pack("<I", self.nBits)
r += struct.pack("<I", self.nNonce) r += struct.pack("<I", self.nNonce)
self.sha256 = uint256_from_str(hash256(r)) self.sha256 = uint256_from_str(hash256(r))
self.hash = encode(hash256(r)[::-1], 'hex') self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii')
def rehash(self): def rehash(self):
self.sha256 = None self.sha256 = None
@ -516,7 +516,7 @@ class CBlock(CBlockHeader):
self.vtx = deser_vector(f, CTransaction) self.vtx = deser_vector(f, CTransaction)
def serialize(self): def serialize(self):
r = "" r = b""
r += super(CBlock, self).serialize() r += super(CBlock, self).serialize()
r += ser_vector(self.vtx) r += ser_vector(self.vtx)
return r return r
@ -571,9 +571,9 @@ class CUnsignedAlert(object):
self.nMaxVer = 0 self.nMaxVer = 0
self.setSubVer = [] self.setSubVer = []
self.nPriority = 0 self.nPriority = 0
self.strComment = "" self.strComment = b""
self.strStatusBar = "" self.strStatusBar = b""
self.strReserved = "" self.strReserved = b""
def deserialize(self, f): def deserialize(self, f):
self.nVersion = struct.unpack("<i", f.read(4))[0] self.nVersion = struct.unpack("<i", f.read(4))[0]
@ -591,7 +591,7 @@ class CUnsignedAlert(object):
self.strReserved = deser_string(f) self.strReserved = deser_string(f)
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<i", self.nVersion) r += struct.pack("<i", self.nVersion)
r += struct.pack("<q", self.nRelayUntil) r += struct.pack("<q", self.nRelayUntil)
r += struct.pack("<q", self.nExpiration) r += struct.pack("<q", self.nExpiration)
@ -616,8 +616,8 @@ class CUnsignedAlert(object):
class CAlert(object): class CAlert(object):
def __init__(self): def __init__(self):
self.vchMsg = "" self.vchMsg = b""
self.vchSig = "" self.vchSig = b""
def deserialize(self, f): def deserialize(self, f):
self.vchMsg = deser_string(f) self.vchMsg = deser_string(f)
@ -636,7 +636,7 @@ class CAlert(object):
# Objects that correspond to messages on the wire # Objects that correspond to messages on the wire
class msg_version(object): class msg_version(object):
command = "version" command = b"version"
def __init__(self): def __init__(self):
self.nVersion = MY_VERSION self.nVersion = MY_VERSION
@ -672,7 +672,7 @@ class msg_version(object):
self.nStartingHeight = None self.nStartingHeight = None
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<i", self.nVersion) r += struct.pack("<i", self.nVersion)
r += struct.pack("<Q", self.nServices) r += struct.pack("<Q", self.nServices)
r += struct.pack("<q", self.nTime) r += struct.pack("<q", self.nTime)
@ -691,7 +691,7 @@ class msg_version(object):
class msg_verack(object): class msg_verack(object):
command = "verack" command = b"verack"
def __init__(self): def __init__(self):
pass pass
@ -700,14 +700,14 @@ class msg_verack(object):
pass pass
def serialize(self): def serialize(self):
return "" return b""
def __repr__(self): def __repr__(self):
return "msg_verack()" return "msg_verack()"
class msg_addr(object): class msg_addr(object):
command = "addr" command = b"addr"
def __init__(self): def __init__(self):
self.addrs = [] self.addrs = []
@ -723,7 +723,7 @@ class msg_addr(object):
class msg_alert(object): class msg_alert(object):
command = "alert" command = b"alert"
def __init__(self): def __init__(self):
self.alert = CAlert() self.alert = CAlert()
@ -733,7 +733,7 @@ class msg_alert(object):
self.alert.deserialize(f) self.alert.deserialize(f)
def serialize(self): def serialize(self):
r = "" r = b""
r += self.alert.serialize() r += self.alert.serialize()
return r return r
@ -742,7 +742,7 @@ class msg_alert(object):
class msg_inv(object): class msg_inv(object):
command = "inv" command = b"inv"
def __init__(self, inv=None): def __init__(self, inv=None):
if inv is None: if inv is None:
@ -761,7 +761,7 @@ class msg_inv(object):
class msg_getdata(object): class msg_getdata(object):
command = "getdata" command = b"getdata"
def __init__(self, inv=None): def __init__(self, inv=None):
self.inv = inv if inv != None else [] self.inv = inv if inv != None else []
@ -777,7 +777,7 @@ class msg_getdata(object):
class msg_getblocks(object): class msg_getblocks(object):
command = "getblocks" command = b"getblocks"
def __init__(self): def __init__(self):
self.locator = CBlockLocator() self.locator = CBlockLocator()
@ -789,7 +789,7 @@ class msg_getblocks(object):
self.hashstop = deser_uint256(f) self.hashstop = deser_uint256(f)
def serialize(self): def serialize(self):
r = "" r = b""
r += self.locator.serialize() r += self.locator.serialize()
r += ser_uint256(self.hashstop) r += ser_uint256(self.hashstop)
return r return r
@ -800,7 +800,7 @@ class msg_getblocks(object):
class msg_tx(object): class msg_tx(object):
command = "tx" command = b"tx"
def __init__(self, tx=CTransaction()): def __init__(self, tx=CTransaction()):
self.tx = tx self.tx = tx
@ -816,7 +816,7 @@ class msg_tx(object):
class msg_block(object): class msg_block(object):
command = "block" command = b"block"
def __init__(self, block=None): def __init__(self, block=None):
if block is None: if block is None:
@ -835,7 +835,7 @@ class msg_block(object):
class msg_getaddr(object): class msg_getaddr(object):
command = "getaddr" command = b"getaddr"
def __init__(self): def __init__(self):
pass pass
@ -844,14 +844,14 @@ class msg_getaddr(object):
pass pass
def serialize(self): def serialize(self):
return "" return b""
def __repr__(self): def __repr__(self):
return "msg_getaddr()" return "msg_getaddr()"
class msg_ping_prebip31(object): class msg_ping_prebip31(object):
command = "ping" command = b"ping"
def __init__(self): def __init__(self):
pass pass
@ -860,14 +860,14 @@ class msg_ping_prebip31(object):
pass pass
def serialize(self): def serialize(self):
return "" return b""
def __repr__(self): def __repr__(self):
return "msg_ping() (pre-bip31)" return "msg_ping() (pre-bip31)"
class msg_ping(object): class msg_ping(object):
command = "ping" command = b"ping"
def __init__(self, nonce=0L): def __init__(self, nonce=0L):
self.nonce = nonce self.nonce = nonce
@ -876,7 +876,7 @@ class msg_ping(object):
self.nonce = struct.unpack("<Q", f.read(8))[0] self.nonce = struct.unpack("<Q", f.read(8))[0]
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<Q", self.nonce) r += struct.pack("<Q", self.nonce)
return r return r
@ -885,16 +885,16 @@ class msg_ping(object):
class msg_pong(object): class msg_pong(object):
command = "pong" command = b"pong"
def __init__(self, nonce=0L): def __init__(self, nonce=0):
self.nonce = nonce self.nonce = nonce
def deserialize(self, f): def deserialize(self, f):
self.nonce = struct.unpack("<Q", f.read(8))[0] self.nonce = struct.unpack("<Q", f.read(8))[0]
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<Q", self.nonce) r += struct.pack("<Q", self.nonce)
return r return r
@ -903,7 +903,7 @@ class msg_pong(object):
class msg_mempool(object): class msg_mempool(object):
command = "mempool" command = b"mempool"
def __init__(self): def __init__(self):
pass pass
@ -912,13 +912,13 @@ class msg_mempool(object):
pass pass
def serialize(self): def serialize(self):
return "" return b""
def __repr__(self): def __repr__(self):
return "msg_mempool()" return "msg_mempool()"
class msg_sendheaders(object): class msg_sendheaders(object):
command = "sendheaders" command = b"sendheaders"
def __init__(self): def __init__(self):
pass pass
@ -927,7 +927,7 @@ class msg_sendheaders(object):
pass pass
def serialize(self): def serialize(self):
return "" return b""
def __repr__(self): def __repr__(self):
return "msg_sendheaders()" return "msg_sendheaders()"
@ -937,7 +937,7 @@ class msg_sendheaders(object):
# vector of hashes # vector of hashes
# hash_stop (hash of last desired block header, 0 to get as many as possible) # hash_stop (hash of last desired block header, 0 to get as many as possible)
class msg_getheaders(object): class msg_getheaders(object):
command = "getheaders" command = b"getheaders"
def __init__(self): def __init__(self):
self.locator = CBlockLocator() self.locator = CBlockLocator()
@ -949,7 +949,7 @@ class msg_getheaders(object):
self.hashstop = deser_uint256(f) self.hashstop = deser_uint256(f)
def serialize(self): def serialize(self):
r = "" r = b""
r += self.locator.serialize() r += self.locator.serialize()
r += ser_uint256(self.hashstop) r += ser_uint256(self.hashstop)
return r return r
@ -962,7 +962,7 @@ class msg_getheaders(object):
# headers message has # headers message has
# <count> <vector of block headers> # <count> <vector of block headers>
class msg_headers(object): class msg_headers(object):
command = "headers" command = b"headers"
def __init__(self): def __init__(self):
self.headers = [] self.headers = []
@ -982,10 +982,10 @@ class msg_headers(object):
class msg_reject(object): class msg_reject(object):
command = "reject" command = b"reject"
def __init__(self): def __init__(self):
self.message = "" self.message = b""
self.code = 0 self.code = 0
self.reason = "" self.reason = ""
self.data = 0L self.data = 0L
@ -1025,7 +1025,7 @@ def wait_until(predicate, attempts=float('inf'), timeout=float('inf')):
return False return False
class msg_feefilter(object): class msg_feefilter(object):
command = "feefilter" command = b"feefilter"
def __init__(self, feerate=0L): def __init__(self, feerate=0L):
self.feerate = feerate self.feerate = feerate
@ -1034,7 +1034,7 @@ class msg_feefilter(object):
self.feerate = struct.unpack("<Q", f.read(8))[0] self.feerate = struct.unpack("<Q", f.read(8))[0]
def serialize(self): def serialize(self):
r = "" r = b""
r += struct.pack("<Q", self.feerate) r += struct.pack("<Q", self.feerate)
return r return r
@ -1149,28 +1149,28 @@ class SingleNodeConnCB(NodeConnCB):
# This class provides an interface for a p2p connection to a specified node # This class provides an interface for a p2p connection to a specified node
class NodeConn(asyncore.dispatcher): class NodeConn(asyncore.dispatcher):
messagemap = { messagemap = {
"version": msg_version, b"version": msg_version,
"verack": msg_verack, b"verack": msg_verack,
"addr": msg_addr, b"addr": msg_addr,
"alert": msg_alert, b"alert": msg_alert,
"inv": msg_inv, b"inv": msg_inv,
"getdata": msg_getdata, b"getdata": msg_getdata,
"getblocks": msg_getblocks, b"getblocks": msg_getblocks,
"tx": msg_tx, b"tx": msg_tx,
"block": msg_block, b"block": msg_block,
"getaddr": msg_getaddr, b"getaddr": msg_getaddr,
"ping": msg_ping, b"ping": msg_ping,
"pong": msg_pong, b"pong": msg_pong,
"headers": msg_headers, b"headers": msg_headers,
"getheaders": msg_getheaders, b"getheaders": msg_getheaders,
"reject": msg_reject, b"reject": msg_reject,
"mempool": msg_mempool, b"mempool": msg_mempool,
"feefilter": msg_feefilter b"feefilter": msg_feefilter
} }
MAGIC_BYTES = { MAGIC_BYTES = {
"mainnet": "\xf9\xbe\xb4\xd9", # mainnet "mainnet": b"\xf9\xbe\xb4\xd9", # mainnet
"testnet3": "\x0b\x11\x09\x07", # testnet3 "testnet3": b"\x0b\x11\x09\x07", # testnet3
"regtest": "\xfa\xbf\xb5\xda" # regtest "regtest": b"\xfa\xbf\xb5\xda" # regtest
} }
def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", services=1): def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", services=1):
@ -1179,8 +1179,8 @@ class NodeConn(asyncore.dispatcher):
self.dstaddr = dstaddr self.dstaddr = dstaddr
self.dstport = dstport self.dstport = dstport
self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.sendbuf = "" self.sendbuf = b""
self.recvbuf = "" self.recvbuf = b""
self.ver_send = 209 self.ver_send = 209
self.ver_recv = 209 self.ver_recv = 209
self.last_sent = 0 self.last_sent = 0
@ -1217,8 +1217,8 @@ class NodeConn(asyncore.dispatcher):
self.show_debug_msg("MiniNode: Closing Connection to %s:%d... " self.show_debug_msg("MiniNode: Closing Connection to %s:%d... "
% (self.dstaddr, self.dstport)) % (self.dstaddr, self.dstport))
self.state = "closed" self.state = "closed"
self.recvbuf = "" self.recvbuf = b""
self.sendbuf = "" self.sendbuf = b""
try: try:
self.close() self.close()
except: except:
@ -1261,7 +1261,7 @@ class NodeConn(asyncore.dispatcher):
if self.ver_recv < 209: if self.ver_recv < 209:
if len(self.recvbuf) < 4 + 12 + 4: if len(self.recvbuf) < 4 + 12 + 4:
return return
command = self.recvbuf[4:4+12].split("\x00", 1)[0] command = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0] msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0]
checksum = None checksum = None
if len(self.recvbuf) < 4 + 12 + 4 + msglen: if len(self.recvbuf) < 4 + 12 + 4 + msglen:
@ -1271,7 +1271,7 @@ class NodeConn(asyncore.dispatcher):
else: else:
if len(self.recvbuf) < 4 + 12 + 4 + 4: if len(self.recvbuf) < 4 + 12 + 4 + 4:
return return
command = self.recvbuf[4:4+12].split("\x00", 1)[0] command = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0] msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0]
checksum = self.recvbuf[4+12+4:4+12+4+4] checksum = self.recvbuf[4+12+4:4+12+4+4]
if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen: if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen:
@ -1301,7 +1301,7 @@ class NodeConn(asyncore.dispatcher):
data = message.serialize() data = message.serialize()
tmsg = self.MAGIC_BYTES[self.network] tmsg = self.MAGIC_BYTES[self.network]
tmsg += command tmsg += command
tmsg += "\x00" * (12 - len(command)) tmsg += b"\x00" * (12 - len(command))
tmsg += struct.pack("<I", len(data)) tmsg += struct.pack("<I", len(data))
if self.ver_send >= 209: if self.ver_send >= 209:
th = sha256(data) th = sha256(data)
@ -1313,11 +1313,11 @@ class NodeConn(asyncore.dispatcher):
self.last_sent = time.time() self.last_sent = time.time()
def got_message(self, message): def got_message(self, message):
if message.command == "version": if message.command == b"version":
if message.nVersion <= BIP0031_VERSION: if message.nVersion <= BIP0031_VERSION:
self.messagemap['ping'] = msg_ping_prebip31 self.messagemap[b'ping'] = msg_ping_prebip31
if self.last_sent + 30 * 60 < time.time(): if self.last_sent + 30 * 60 < time.time():
self.send_message(self.messagemap['ping']()) self.send_message(self.messagemap[b'ping']())
self.show_debug_msg("Recv %s" % repr(message)) self.show_debug_msg("Recv %s" % repr(message))
self.cb.deliver(self, message) self.cb.deliver(self, message)

11
qa/rpc-tests/test_framework/netutil.py

@ -4,13 +4,14 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
# Linux network utilities # Linux network utilities
import sys import sys
import socket import socket
import fcntl import fcntl
import struct import struct
import array import array
import os import os
import binascii from binascii import unhexlify, hexlify
# Roughly based on http://voorloopnul.com/blog/a-python-netstat-in-less-than-100-lines-of-code/ by Ricardo Pascal # Roughly based on http://voorloopnul.com/blog/a-python-netstat-in-less-than-100-lines-of-code/ by Ricardo Pascal
STATE_ESTABLISHED = '01' STATE_ESTABLISHED = '01'
@ -43,7 +44,7 @@ def _remove_empty(array):
def _convert_ip_port(array): def _convert_ip_port(array):
host,port = array.split(':') host,port = array.split(':')
# convert host from mangled-per-four-bytes form as used by kernel # convert host from mangled-per-four-bytes form as used by kernel
host = binascii.unhexlify(host) host = unhexlify(host)
host_out = '' host_out = ''
for x in range(0, len(host) // 4): for x in range(0, len(host) // 4):
(val,) = struct.unpack('=I', host[x*4:(x+1)*4]) (val,) = struct.unpack('=I', host[x*4:(x+1)*4])
@ -94,7 +95,7 @@ def all_interfaces():
max_possible = 8 # initial value max_possible = 8 # initial value
while True: while True:
bytes = max_possible * struct_size bytes = max_possible * struct_size
names = array.array('B', '\0' * bytes) names = array.array('B', b'\0' * bytes)
outbytes = struct.unpack('iL', fcntl.ioctl( outbytes = struct.unpack('iL', fcntl.ioctl(
s.fileno(), s.fileno(),
0x8912, # SIOCGIFCONF 0x8912, # SIOCGIFCONF
@ -105,7 +106,7 @@ def all_interfaces():
else: else:
break break
namestr = names.tostring() namestr = names.tostring()
return [(namestr[i:i+16].split('\0', 1)[0], return [(namestr[i:i+16].split(b'\0', 1)[0],
socket.inet_ntoa(namestr[i+20:i+24])) socket.inet_ntoa(namestr[i+20:i+24]))
for i in range(0, outbytes, struct_size)] for i in range(0, outbytes, struct_size)]
@ -136,7 +137,7 @@ def addr_to_hex(addr):
addr = sub[0] + ([0] * nullbytes) + sub[1] addr = sub[0] + ([0] * nullbytes) + sub[1]
else: else:
raise ValueError('Could not parse address %s' % addr) raise ValueError('Could not parse address %s' % addr)
return binascii.hexlify(bytearray(addr)) return hexlify(bytearray(addr)).decode('ascii')
def test_ipv6_local(): def test_ipv6_local():
''' '''

4
qa/rpc-tests/test_framework/script.py

@ -629,7 +629,7 @@ class CScriptNum(object):
neg = obj.value < 0 neg = obj.value < 0
absvalue = -obj.value if neg else obj.value absvalue = -obj.value if neg else obj.value
while (absvalue): while (absvalue):
r.append(chr(absvalue & 0xff)) r.append(absvalue & 0xff)
absvalue >>= 8 absvalue >>= 8
if r[-1] & 0x80: if r[-1] & 0x80:
r.append(0x80 if neg else 0) r.append(0x80 if neg else 0)
@ -777,7 +777,7 @@ class CScript(bytes):
# need to change # need to change
def _repr(o): def _repr(o):
if isinstance(o, bytes): if isinstance(o, bytes):
return "x('%s')" % hexlify(o).decode('utf8') return b"x('%s')" % hexlify(o).decode('ascii')
else: else:
return repr(o) return repr(o)

2
qa/rpc-tests/test_framework/socks5.py

@ -102,7 +102,7 @@ class Socks5Connection(object):
addr = recvall(self.conn, 4) addr = recvall(self.conn, 4)
elif atyp == AddressType.DOMAINNAME: elif atyp == AddressType.DOMAINNAME:
n = recvall(self.conn, 1)[0] n = recvall(self.conn, 1)[0]
addr = str(recvall(self.conn, n)) addr = recvall(self.conn, n)
elif atyp == AddressType.IPV6: elif atyp == AddressType.IPV6:
addr = recvall(self.conn, 16) addr = recvall(self.conn, 16)
else: else:

15
qa/rpc-tests/test_framework/util.py

@ -1,6 +1,8 @@
# Copyright (c) 2014-2015 The Bitcoin Core developers # Copyright (c) 2014-2015 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
# #
# Helpful routines for regression testing # Helpful routines for regression testing
# #
@ -9,6 +11,8 @@
import os import os
import sys import sys
from binascii import hexlify, unhexlify
from base64 import b64encode
from decimal import Decimal, ROUND_DOWN from decimal import Decimal, ROUND_DOWN
import json import json
import random import random
@ -91,6 +95,15 @@ def check_json_precision():
def count_bytes(hex_string): def count_bytes(hex_string):
return len(bytearray.fromhex(hex_string)) return len(bytearray.fromhex(hex_string))
def bytes_to_hex_str(byte_str):
return hexlify(byte_str).decode('ascii')
def hex_str_to_bytes(hex_str):
return unhexlify(hex_str.encode('ascii'))
def str_to_b64str(string):
return b64encode(string.encode('utf-8')).decode('ascii')
def sync_blocks(rpc_connections, wait=1): def sync_blocks(rpc_connections, wait=1):
""" """
Wait until everybody has the same block count Wait until everybody has the same block count
@ -466,7 +479,7 @@ def assert_is_hash_string(string, length=64):
"String %r contains invalid characters for a hash." % string) "String %r contains invalid characters for a hash." % string)
def satoshi_round(amount): def satoshi_round(amount):
return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN) return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN)
# Helper to create at least "count" utxos # Helper to create at least "count" utxos
# Pass in a fee that is sufficient for relay and mining new transactions. # Pass in a fee that is sufficient for relay and mining new transactions.

22
qa/rpc-tests/zmq_test.py

@ -28,8 +28,8 @@ class ZMQTest (BitcoinTestFramework):
def setup_nodes(self): def setup_nodes(self):
self.zmqContext = zmq.Context() self.zmqContext = zmq.Context()
self.zmqSubSocket = self.zmqContext.socket(zmq.SUB) self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "hashblock") self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "hashtx") self.zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port) self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % self.port)
return start_nodes(4, self.options.tmpdir, extra_args=[ return start_nodes(4, self.options.tmpdir, extra_args=[
['-zmqpubhashtx=tcp://127.0.0.1:'+str(self.port), '-zmqpubhashblock=tcp://127.0.0.1:'+str(self.port)], ['-zmqpubhashtx=tcp://127.0.0.1:'+str(self.port), '-zmqpubhashblock=tcp://127.0.0.1:'+str(self.port)],
@ -46,13 +46,13 @@ class ZMQTest (BitcoinTestFramework):
print "listen..." print "listen..."
msg = self.zmqSubSocket.recv_multipart() msg = self.zmqSubSocket.recv_multipart()
topic = str(msg[0]) topic = msg[0]
body = msg[1] body = msg[1]
msg = self.zmqSubSocket.recv_multipart() msg = self.zmqSubSocket.recv_multipart()
topic = str(msg[0]) topic = msg[0]
body = msg[1] body = msg[1]
blkhash = binascii.hexlify(body) blkhash = bytes_to_hex_str(body)
assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq assert_equal(genhashes[0], blkhash) #blockhash from generate must be equal to the hash received over zmq
@ -63,10 +63,10 @@ class ZMQTest (BitcoinTestFramework):
zmqHashes = [] zmqHashes = []
for x in range(0,n*2): for x in range(0,n*2):
msg = self.zmqSubSocket.recv_multipart() msg = self.zmqSubSocket.recv_multipart()
topic = str(msg[0]) topic = msg[0]
body = msg[1] body = msg[1]
if topic == "hashblock": if topic == b"hashblock":
zmqHashes.append(binascii.hexlify(body)) zmqHashes.append(bytes_to_hex_str(body))
for x in range(0,n): for x in range(0,n):
assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq assert_equal(genhashes[x], zmqHashes[x]) #blockhash from generate must be equal to the hash received over zmq
@ -77,11 +77,11 @@ class ZMQTest (BitcoinTestFramework):
# now we should receive a zmq msg because the tx was broadcast # now we should receive a zmq msg because the tx was broadcast
msg = self.zmqSubSocket.recv_multipart() msg = self.zmqSubSocket.recv_multipart()
topic = str(msg[0]) topic = msg[0]
body = msg[1] body = msg[1]
hashZMQ = "" hashZMQ = ""
if topic == "hashtx": if topic == b"hashtx":
hashZMQ = binascii.hexlify(body) hashZMQ = bytes_to_hex_str(body)
assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq assert_equal(hashRPC, hashZMQ) #blockhash from generate must be equal to the hash received over zmq

3
src/test/bctest.py

@ -2,6 +2,7 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
from __future__ import division,print_function,unicode_literals from __future__ import division,print_function,unicode_literals
from io import open
import subprocess import subprocess
import os import os
import json import json
@ -16,7 +17,7 @@ def bctest(testDir, testObj, exeext):
inputData = None inputData = None
if "input" in testObj: if "input" in testObj:
filename = testDir + "/" + testObj['input'] filename = testDir + "/" + testObj['input']
inputData = open(filename).read() inputData = open(filename, 'rb').read()
stdinCfg = subprocess.PIPE stdinCfg = subprocess.PIPE
outputFn = None outputFn = None

Loading…
Cancel
Save