Updates os.urandom to python3

All string generated with urandom(x) were using binascii + os.urandom + encode. In python3 os.urandom has a hex method.
This commit is contained in:
Sergi Delgado Segura
2019-10-07 11:39:10 +01:00
parent beac88a2c5
commit 5ae210d893
5 changed files with 13 additions and 15 deletions

View File

@@ -28,9 +28,8 @@ def generate_dummy_appointment():
current_height = r.json().get("block_count") current_height = r.json().get("block_count")
dummy_appointment_data = {"tx": hexlify(os.urandom(192)).decode('utf-8'), dummy_appointment_data = {"tx": os.urandom(192).hex(), "tx_id": os.urandom(32).hex(),
"tx_id": hexlify(os.urandom(32)).decode('utf-8'), "start_time": current_height + 5, "start_time": current_height + 5, "end_time": current_height + 10, "dispute_delta": 20}
"end_time": current_height + 10, "dispute_delta": 20}
print('Generating dummy appointment data:''\n\n' + json.dumps(dummy_appointment_data, indent=4, sort_keys=True)) print('Generating dummy appointment data:''\n\n' + json.dumps(dummy_appointment_data, indent=4, sort_keys=True))

View File

@@ -18,8 +18,7 @@ def generate_dummy_appointment(dispute_txid):
current_height = r.json().get("block_count") current_height = r.json().get("block_count")
dummy_appointment_data = {"tx": hexlify(os.urandom(32)).decode('utf-8'), dummy_appointment_data = {"tx": os.urandom(32).hex(), "tx_id": dispute_txid, "start_time": current_height + 5,
"tx_id": dispute_txid, "start_time": current_height + 5,
"end_time": current_height + 10, "dispute_delta": 20} "end_time": current_height + 10, "dispute_delta": 20}
cipher = "AES-GCM-128" cipher = "AES-GCM-128"
@@ -40,7 +39,7 @@ def generate_dummy_appointment(dispute_txid):
def test_add_appointment(appointment=None): def test_add_appointment(appointment=None):
if not appointment: if not appointment:
dispute_txid = hexlify(os.urandom(32)).decode('utf-8') dispute_txid = os.urandom(32).hex()
appointment = generate_dummy_appointment(dispute_txid) appointment = generate_dummy_appointment(dispute_txid)
print("Sending appointment (locator: {}) to PISA".format(appointment.get("locator"))) print("Sending appointment (locator: {}) to PISA".format(appointment.get("locator")))
@@ -67,7 +66,7 @@ def test_add_appointment(appointment=None):
def test_same_locator_multiple_appointments(): def test_same_locator_multiple_appointments():
dispute_txid = hexlify(os.urandom(32)).decode('utf-8') dispute_txid = os.urandom(32).hex()
appointment = generate_dummy_appointment(dispute_txid) appointment = generate_dummy_appointment(dispute_txid)
# Send it once # Send it once

View File

@@ -32,7 +32,7 @@ assert(len(block.get('tx')) != 0)
assert(isinstance(block.get('height'), int)) assert(isinstance(block.get('height'), int))
# Some fails # Some fails
values += ["a"*64, binascii.hexlify(os.urandom(32)).decode()] values += ["a"*64, os.urandom(32).hex()]
print("\ngetblock fails ({}):".format(len(values))) print("\ngetblock fails ({}):".format(len(values)))
for v in values: for v in values:
@@ -50,14 +50,14 @@ assert(isinstance(tx.get('txid'), str))
assert(check_txid_format(tx.get('txid'))) assert(check_txid_format(tx.get('txid')))
# Therefore should also work for a random formatted 32-byte hex in our simulation # Therefore should also work for a random formatted 32-byte hex in our simulation
random_tx = binascii.hexlify(os.urandom(32)).decode() random_tx = os.urandom(32).hex()
tx = bitcoin_cli.decoderawtransaction(random_tx) tx = bitcoin_cli.decoderawtransaction(random_tx)
assert(isinstance(tx, dict)) assert(isinstance(tx, dict))
assert(isinstance(tx.get('txid'), str)) assert(isinstance(tx.get('txid'), str))
assert(check_txid_format(tx.get('txid'))) assert(check_txid_format(tx.get('txid')))
# But it should fail for not proper formatted one # But it should fail for not proper formatted one
values = [1, None, '', "a"*63, "b"*65, [], binascii.hexlify(os.urandom(31)).hex()] values = [1, None, '', "a"*63, "b"*65, [], os.urandom(31).hex()]
print("\ndecoderawtransaction fails ({}):".format(len(values))) print("\ndecoderawtransaction fails ({}):".format(len(values)))
for v in values: for v in values:
@@ -68,7 +68,7 @@ for v in values:
print('\t{}'.format(e)) print('\t{}'.format(e))
# sendrawtransaction should only allow txids that the simulator has not mined yet # sendrawtransaction should only allow txids that the simulator has not mined yet
bitcoin_cli.sendrawtransaction(binascii.hexlify(os.urandom(32)).decode()) bitcoin_cli.sendrawtransaction(os.urandom(32).hex())
# Any data not matching the txid format or that matches with an already mined transaction should fail # Any data not matching the txid format or that matches with an already mined transaction should fail
values += [coinbase_tx] values += [coinbase_tx]

View File

@@ -172,8 +172,8 @@ def simulate_mining():
prev_block_hash = None prev_block_hash = None
while True: while True:
block_hash = binascii.hexlify(os.urandom(32)).decode('utf-8') block_hash = os.urandom(32).hex()
coinbase_tx_hash = binascii.hexlify(os.urandom(32)).decode('utf-8') coinbase_tx_hash = os.urandom(32).hex()
txs_to_mine = [coinbase_tx_hash] txs_to_mine = [coinbase_tx_hash]
if len(mempool) != 0: if len(mempool) != 0:

View File

@@ -20,7 +20,7 @@ def set_up_appointments(total_appointments):
for _ in range(total_appointments): for _ in range(total_appointments):
uuid = uuid4().hex uuid = uuid4().hex
locator = hexlify(urandom(64)) locator = urandom(32).hex()
appointments[uuid] = Appointment(locator, None, None, None, None, None, None) appointments[uuid] = Appointment(locator, None, None, None, None, None, None)
locator_uuid_map[locator] = [uuid] locator_uuid_map[locator] = [uuid]
@@ -41,7 +41,7 @@ def set_up_jobs(total_jobs):
for _ in range(total_jobs): for _ in range(total_jobs):
uuid = uuid4().hex uuid = uuid4().hex
txid = hexlify(urandom(64)) txid = urandom(32).hex()
# Assign both justice_txid and dispute_txid the same id (it shouldn't matter) # Assign both justice_txid and dispute_txid the same id (it shouldn't matter)
jobs[uuid] = Job(txid, txid, None, None, None) jobs[uuid] = Job(txid, txid, None, None, None)