Tests pass

This commit is contained in:
Justin Drake 2019-06-29 09:25:19 +01:00
parent 34b8d8ab33
commit d5d3e49c5f
3 changed files with 93 additions and 107 deletions

File diff suppressed because one or more lines are too long

View File

@ -52,7 +52,7 @@ def get_deposit_root() -> bytes32:
else: else:
node = sha256(concat(node, self.zero_hashes[height])) node = sha256(concat(node, self.zero_hashes[height]))
size /= 2 size /= 2
return sha256(concat(node, slice(zero_bytes32, start=0, len=24), self.to_little_endian_64(self.deposit_count))) return sha256(concat(node, self.to_little_endian_64(self.deposit_count), slice(zero_bytes32, start=0, len=24)))
@public @public

View File

@ -21,116 +21,101 @@ from eth2spec.utils.ssz.ssz_impl import (
) )
def compute_merkle_root(leaf_nodes): # @pytest.fixture
assert len(leaf_nodes) >= 1 # def deposit_input():
empty_node = b'\x00' * 32 # """
child_nodes = leaf_nodes[:] # pubkey: bytes[48]
for _ in range(DEPOSIT_CONTRACT_TREE_DEPTH): # withdrawal_credentials: bytes[32]
parent_nodes = [] # signature: bytes[96]
if len(child_nodes) % 2 == 1: # """
child_nodes.append(empty_node) # return (
for j in range(0, len(child_nodes), 2): # b'\x11' * 48,
parent_nodes.append(hash(child_nodes[j] + child_nodes[j + 1])) # b'\x22' * 32,
child_nodes = parent_nodes # b'\x33' * 96,
empty_node = hash(empty_node + empty_node) # )
return child_nodes[0]
@pytest.fixture # @pytest.mark.parametrize(
def deposit_input(): # 'success,deposit_amount',
""" # [
pubkey: bytes[48] # (True, FULL_DEPOSIT_AMOUNT),
withdrawal_credentials: bytes[32] # (True, MIN_DEPOSIT_AMOUNT),
signature: bytes[96] # (False, MIN_DEPOSIT_AMOUNT - 1),
""" # (True, FULL_DEPOSIT_AMOUNT + 1)
return ( # ]
b'\x11' * 48, # )
b'\x22' * 32, # def test_deposit_amount(registration_contract,
b'\x33' * 96, # w3,
) # success,
# deposit_amount,
# assert_tx_failed,
# deposit_input):
# call = registration_contract.functions.deposit(*deposit_input)
# if success:
# assert call.transact({"value": deposit_amount * eth_utils.denoms.gwei})
# else:
# assert_tx_failed(
# lambda: call.transact({"value": deposit_amount * eth_utils.denoms.gwei})
# )
@pytest.mark.parametrize( # @pytest.mark.parametrize(
'success,deposit_amount', # 'invalid_pubkey,invalid_withdrawal_credentials,invalid_signature,success',
[ # [
(True, FULL_DEPOSIT_AMOUNT), # (False, False, False, True),
(True, MIN_DEPOSIT_AMOUNT), # (True, False, False, False),
(False, MIN_DEPOSIT_AMOUNT - 1), # (False, True, False, False),
(True, FULL_DEPOSIT_AMOUNT + 1) # (False, False, True, False),
] # ]
) # )
def test_deposit_amount(registration_contract, # def test_deposit_inputs(registration_contract,
w3, # w3,
success, # assert_tx_failed,
deposit_amount, # deposit_input,
assert_tx_failed, # invalid_pubkey,
deposit_input): # invalid_withdrawal_credentials,
call = registration_contract.functions.deposit(*deposit_input) # invalid_signature,
if success: # success):
assert call.transact({"value": deposit_amount * eth_utils.denoms.gwei}) # pubkey = deposit_input[0][2:] if invalid_pubkey else deposit_input[0]
else: # if invalid_withdrawal_credentials: # this one is different to satisfy linter
assert_tx_failed( # withdrawal_credentials = deposit_input[1][2:]
lambda: call.transact({"value": deposit_amount * eth_utils.denoms.gwei}) # else:
) # withdrawal_credentials = deposit_input[1]
# signature = deposit_input[2][2:] if invalid_signature else deposit_input[2]
# call = registration_contract.functions.deposit(
# pubkey,
# withdrawal_credentials,
# signature,
# )
# if success:
# assert call.transact({"value": FULL_DEPOSIT_AMOUNT * eth_utils.denoms.gwei})
# else:
# assert_tx_failed(
# lambda: call.transact({"value": FULL_DEPOSIT_AMOUNT * eth_utils.denoms.gwei})
# )
@pytest.mark.parametrize( # def test_deposit_log(registration_contract, a0, w3, deposit_input):
'invalid_pubkey,invalid_withdrawal_credentials,invalid_signature,success', # log_filter = registration_contract.events.Deposit.createFilter(
[ # fromBlock='latest',
(False, False, False, True), # )
(True, False, False, False),
(False, True, False, False),
(False, False, True, False),
]
)
def test_deposit_inputs(registration_contract,
w3,
assert_tx_failed,
deposit_input,
invalid_pubkey,
invalid_withdrawal_credentials,
invalid_signature,
success):
pubkey = deposit_input[0][2:] if invalid_pubkey else deposit_input[0]
if invalid_withdrawal_credentials: # this one is different to satisfy linter
withdrawal_credentials = deposit_input[1][2:]
else:
withdrawal_credentials = deposit_input[1]
signature = deposit_input[2][2:] if invalid_signature else deposit_input[2]
call = registration_contract.functions.deposit( # deposit_amount_list = [randint(MIN_DEPOSIT_AMOUNT, FULL_DEPOSIT_AMOUNT * 2) for _ in range(3)]
pubkey, # for i in range(3):
withdrawal_credentials, # registration_contract.functions.deposit(
signature, # *deposit_input,
) # ).transact({"value": deposit_amount_list[i] * eth_utils.denoms.gwei})
if success:
assert call.transact({"value": FULL_DEPOSIT_AMOUNT * eth_utils.denoms.gwei})
else:
assert_tx_failed(
lambda: call.transact({"value": FULL_DEPOSIT_AMOUNT * eth_utils.denoms.gwei})
)
# logs = log_filter.get_new_entries()
# assert len(logs) == 1
# log = logs[0]['args']
def test_deposit_log(registration_contract, a0, w3, deposit_input): # assert log['pubkey'] == deposit_input[0]
log_filter = registration_contract.events.Deposit.createFilter( # assert log['withdrawal_credentials'] == deposit_input[1]
fromBlock='latest', # assert log['amount'] == deposit_amount_list[i].to_bytes(8, 'little')
) # assert log['signature'] == deposit_input[2]
# assert log['index'] == i.to_bytes(8, 'little')
deposit_amount_list = [randint(MIN_DEPOSIT_AMOUNT, FULL_DEPOSIT_AMOUNT * 2) for _ in range(3)]
for i in range(3):
registration_contract.functions.deposit(
*deposit_input,
).transact({"value": deposit_amount_list[i] * eth_utils.denoms.gwei})
logs = log_filter.get_new_entries()
assert len(logs) == 1
log = logs[0]['args']
assert log['pubkey'] == deposit_input[0]
assert log['withdrawal_credentials'] == deposit_input[1]
assert log['amount'] == deposit_amount_list[i].to_bytes(8, 'little')
assert log['signature'] == deposit_input[2]
assert log['index'] == i.to_bytes(8, 'little')
def test_deposit_tree(registration_contract, w3, assert_tx_failed, deposit_input): def test_deposit_tree(registration_contract, w3, assert_tx_failed, deposit_input):
log_filter = registration_contract.events.Deposit.createFilter( log_filter = registration_contract.events.Deposit.createFilter(
@ -138,7 +123,7 @@ def test_deposit_tree(registration_contract, w3, assert_tx_failed, deposit_input
) )
deposit_amount_list = [randint(MIN_DEPOSIT_AMOUNT, FULL_DEPOSIT_AMOUNT * 2) for _ in range(10)] deposit_amount_list = [randint(MIN_DEPOSIT_AMOUNT, FULL_DEPOSIT_AMOUNT * 2) for _ in range(10)]
deposit_data_list = List[DepositData, 2**32]() deposit_data_list = []
for i in range(0, 10): for i in range(0, 10):
tx_hash = registration_contract.functions.deposit( tx_hash = registration_contract.functions.deposit(
*deposit_input, *deposit_input,
@ -152,11 +137,12 @@ def test_deposit_tree(registration_contract, w3, assert_tx_failed, deposit_input
assert log["index"] == i.to_bytes(8, 'little') assert log["index"] == i.to_bytes(8, 'little')
deposit_data_list[i] = DepositData( deposit_data_list.append(DepositData(
pubkey=deposit_input[0], pubkey=deposit_input[0],
withdrawal_credentials=deposit_input[1], withdrawal_credentials=deposit_input[1],
amount=deposit_amount_list[i], amount=deposit_amount_list[i],
signature=deposit_input[2], signature=deposit_input[2],
) ))
root = hash_tree_root(deposit_data_list)
root = hash_tree_root(List[DepositData, 2**32](*(tuple(deposit_data_list))))
assert root == registration_contract.functions.get_deposit_root().call() assert root == registration_contract.functions.get_deposit_root().call()