eth2.0-specs/deposit_contract/tests/contracts/test_deposit.py

202 lines
6.2 KiB
Python
Raw Normal View History

2019-05-27 05:15:10 +00:00
from random import (
randint,
)
import pytest
import eth_utils
from tests.contracts.conftest import (
FULL_DEPOSIT_AMOUNT,
MIN_DEPOSIT_AMOUNT,
)
from eth2spec.phase0.spec import (
DepositData,
)
2019-06-29 08:13:42 +00:00
from eth2spec.utils.ssz.ssz_typing import List
2019-06-07 19:35:52 +00:00
from eth2spec.utils.ssz.ssz_impl import (
2019-05-27 05:15:10 +00:00
hash_tree_root,
)
2019-09-03 17:59:18 +00:00
SAMPLE_PUBKEY = b'\x11' * 48
SAMPLE_WITHDRAWAL_CREDENTIALS = b'\x22' * 32
SAMPLE_VALID_SIGNATURE = b'\x33' * 96
2019-06-29 08:28:46 +00:00
@pytest.fixture
2019-09-03 17:59:18 +00:00
def deposit_input(amount):
2019-06-29 08:28:46 +00:00
"""
pubkey: bytes[48]
withdrawal_credentials: bytes[32]
signature: bytes[96]
2019-09-03 17:59:18 +00:00
deposit_data_root: bytes[32]
2019-06-29 08:28:46 +00:00
"""
return (
2019-09-03 17:59:18 +00:00
SAMPLE_PUBKEY,
SAMPLE_WITHDRAWAL_CREDENTIALS,
SAMPLE_VALID_SIGNATURE,
hash_tree_root(
DepositData(
pubkey=SAMPLE_PUBKEY,
withdrawal_credentials=SAMPLE_WITHDRAWAL_CREDENTIALS,
amount=amount,
signature=SAMPLE_VALID_SIGNATURE,
),
)
2019-06-29 08:28:46 +00:00
)
@pytest.mark.parametrize(
2019-09-03 17:59:18 +00:00
('success', 'amount'),
2019-06-29 08:28:46 +00:00
[
(True, FULL_DEPOSIT_AMOUNT),
(True, MIN_DEPOSIT_AMOUNT),
(False, MIN_DEPOSIT_AMOUNT - 1),
(True, FULL_DEPOSIT_AMOUNT + 1)
]
)
def test_deposit_amount(registration_contract,
w3,
success,
2019-09-03 17:59:18 +00:00
amount,
2019-06-29 08:28:46 +00:00
assert_tx_failed,
deposit_input):
call = registration_contract.functions.deposit(*deposit_input)
if success:
2019-09-03 17:59:18 +00:00
assert call.transact({"value": amount * eth_utils.denoms.gwei})
2019-06-29 08:28:46 +00:00
else:
assert_tx_failed(
2019-09-03 17:59:18 +00:00
lambda: call.transact({"value": amount * eth_utils.denoms.gwei})
2019-06-29 08:28:46 +00:00
)
2019-09-03 17:59:18 +00:00
@pytest.mark.parametrize(
'amount',
[
(FULL_DEPOSIT_AMOUNT)
]
)
2019-06-29 08:28:46 +00:00
@pytest.mark.parametrize(
'invalid_pubkey,invalid_withdrawal_credentials,invalid_signature,success',
[
(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,
2019-09-03 17:59:18 +00:00
amount,
2019-06-29 08:28:46 +00:00
invalid_pubkey,
invalid_withdrawal_credentials,
invalid_signature,
success):
2019-09-03 17:59:18 +00:00
pubkey = SAMPLE_PUBKEY[2:] if invalid_pubkey else SAMPLE_PUBKEY
withdrawal_credentials = (
SAMPLE_WITHDRAWAL_CREDENTIALS[2:] if invalid_withdrawal_credentials
else SAMPLE_WITHDRAWAL_CREDENTIALS
)
signature = SAMPLE_VALID_SIGNATURE[2:] if invalid_signature else SAMPLE_VALID_SIGNATURE
2019-06-29 08:28:46 +00:00
call = registration_contract.functions.deposit(
pubkey,
withdrawal_credentials,
signature,
2019-09-03 17:59:18 +00:00
hash_tree_root(
DepositData(
pubkey=SAMPLE_PUBKEY if invalid_pubkey else pubkey,
withdrawal_credentials=(
SAMPLE_WITHDRAWAL_CREDENTIALS if invalid_withdrawal_credentials
else withdrawal_credentials
),
amount=amount,
signature=SAMPLE_VALID_SIGNATURE if invalid_signature else signature,
),
)
2019-06-29 08:28:46 +00:00
)
if success:
2019-09-03 17:59:18 +00:00
assert call.transact({"value": amount * eth_utils.denoms.gwei})
2019-06-29 08:28:46 +00:00
else:
assert_tx_failed(
2019-09-03 17:59:18 +00:00
lambda: call.transact({"value": amount * eth_utils.denoms.gwei})
2019-06-29 08:28:46 +00:00
)
2019-09-03 17:59:18 +00:00
def test_deposit_event_log(registration_contract, a0, w3):
log_filter = registration_contract.events.DepositEvent.createFilter(
2019-06-29 08:28:46 +00:00
fromBlock='latest',
)
deposit_amount_list = [randint(MIN_DEPOSIT_AMOUNT, FULL_DEPOSIT_AMOUNT * 2) for _ in range(3)]
2019-09-03 17:59:18 +00:00
2019-06-29 08:28:46 +00:00
for i in range(3):
2019-09-03 17:59:18 +00:00
deposit_input = (
SAMPLE_PUBKEY,
SAMPLE_WITHDRAWAL_CREDENTIALS,
SAMPLE_VALID_SIGNATURE,
hash_tree_root(
DepositData(
pubkey=SAMPLE_PUBKEY,
withdrawal_credentials=SAMPLE_WITHDRAWAL_CREDENTIALS,
amount=deposit_amount_list[i],
signature=SAMPLE_VALID_SIGNATURE,
),
)
)
2019-06-29 08:28:46 +00:00
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')
2019-05-27 05:15:10 +00:00
2019-09-03 17:59:18 +00:00
def test_deposit_tree(registration_contract, w3, assert_tx_failed):
log_filter = registration_contract.events.DepositEvent.createFilter(
2019-05-27 05:15:10 +00:00
fromBlock='latest',
)
deposit_amount_list = [randint(MIN_DEPOSIT_AMOUNT, FULL_DEPOSIT_AMOUNT * 2) for _ in range(10)]
2019-06-29 08:25:19 +00:00
deposit_data_list = []
2019-05-27 05:15:10 +00:00
for i in range(0, 10):
2019-09-03 17:59:18 +00:00
deposit_data = DepositData(
pubkey=SAMPLE_PUBKEY,
withdrawal_credentials=SAMPLE_WITHDRAWAL_CREDENTIALS,
amount=deposit_amount_list[i],
signature=SAMPLE_VALID_SIGNATURE,
)
deposit_input = (
SAMPLE_PUBKEY,
SAMPLE_WITHDRAWAL_CREDENTIALS,
SAMPLE_VALID_SIGNATURE,
hash_tree_root(deposit_data),
)
deposit_data_list.append(deposit_data)
2019-05-27 05:15:10 +00:00
tx_hash = registration_contract.functions.deposit(
*deposit_input,
).transact({"value": deposit_amount_list[i] * eth_utils.denoms.gwei})
receipt = w3.eth.getTransactionReceipt(tx_hash)
print("deposit transaction consumes %d gas" % receipt['gasUsed'])
logs = log_filter.get_new_entries()
assert len(logs) == 1
log = logs[0]['args']
2019-06-11 14:25:25 +00:00
assert log["index"] == i.to_bytes(8, 'little')
2019-09-03 17:59:18 +00:00
# Check deposit count and root
count = len(deposit_data_list).to_bytes(8, 'little')
assert count == registration_contract.functions.get_deposit_count().call()
2019-06-29 14:48:08 +00:00
root = hash_tree_root(List[DepositData, 2**32](*deposit_data_list))
2019-09-03 17:59:18 +00:00
assert root == registration_contract.functions.get_deposit_root().call()