From 7a4c53ac38fa3da4f852e4863450b180c9193b63 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Sat, 12 Jan 2019 22:31:24 +0800 Subject: [PATCH] Update deposit contract --- contracts/validator_registration.v.py | 31 ++++++++++++++------------- tests/contracts/test_deposit.py | 14 ++++++------ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/contracts/validator_registration.v.py b/contracts/validator_registration.v.py index daa9a2c..acfd5d8 100644 --- a/contracts/validator_registration.v.py +++ b/contracts/validator_registration.v.py @@ -6,10 +6,10 @@ TWO_TO_POWER_OF_TREE_DEPTH: constant(uint256) = 4294967296 # 2**32 SECONDS_PER_DAY: constant(uint256) = 86400 -Eth1Deposit: event({previous_receipt_root: bytes32, data: bytes[2064], deposit_count: uint256}) -ChainStart: event({receipt_root: bytes32, time: bytes[8]}) +Deposit: event({previous_deposit_root: bytes32, data: bytes[2064], merkle_tree_index: bytes[8]}) +ChainStart: event({deposit_root: bytes32, time: bytes[8]}) -receipt_tree: map(uint256, bytes32) +deposit_tree: map(uint256, bytes32) deposit_count: uint256 full_deposit_count: uint256 @@ -23,14 +23,15 @@ def deposit(deposit_input: bytes[2048]): msg_gwei_bytes8: bytes[8] = slice(concat("", convert(msg.value / GWEI_PER_ETH, bytes32)), start=24, len=8) timestamp_bytes8: bytes[8] = slice(concat("", convert(block.timestamp, bytes32)), start=24, len=8) deposit_data: bytes[2064] = concat(msg_gwei_bytes8, timestamp_bytes8, deposit_input) + merkle_tree_index: bytes[8] = slice(concat("", convert(index, bytes32)), start=24, len=8) - log.Eth1Deposit(self.receipt_tree[1], deposit_data, self.deposit_count) + log.Deposit(self.deposit_tree[1], deposit_data, merkle_tree_index) # add deposit to merkle tree - self.receipt_tree[index] = sha3(deposit_data) + self.deposit_tree[index] = sha3(deposit_data) for i in range(DEPOSIT_CONTRACT_TREE_DEPTH): # DEPOSIT_CONTRACT_TREE_DEPTH (range of constant var not yet supported) index /= 2 - self.receipt_tree[index] = sha3(concat(self.receipt_tree[index * 2], self.receipt_tree[index * 2 + 1])) + self.deposit_tree[index] = sha3(concat(self.deposit_tree[index * 2], self.deposit_tree[index * 2 + 1])) self.deposit_count += 1 if msg.value == as_wei_value(MAX_DEPOSIT, "ether"): @@ -38,19 +39,19 @@ def deposit(deposit_input: bytes[2048]): if self.full_deposit_count == CHAIN_START_FULL_DEPOSIT_THRESHOLD: timestamp_day_boundary: uint256 = as_unitless_number(block.timestamp) - as_unitless_number(block.timestamp) % SECONDS_PER_DAY + SECONDS_PER_DAY timestamp_day_boundary_bytes8: bytes[8] = slice(concat("", convert(timestamp_day_boundary, bytes32)), start=24, len=8) - log.ChainStart(self.receipt_tree[1], timestamp_day_boundary_bytes8) + log.ChainStart(self.deposit_tree[1], timestamp_day_boundary_bytes8) @public @constant -def get_receipt_root() -> bytes32: - return self.receipt_tree[1] +def get_deposit_root() -> bytes32: + return self.deposit_tree[1] @public @constant -def get_merkle_branch(index: uint256) -> bytes32[32]: # returned data size is DEPOSIT_CONTRACT_TREE_DEPTH - idx: uint256 = index + TWO_TO_POWER_OF_TREE_DEPTH - ret: bytes32[32] +def get_branch(leaf: uint256) -> bytes32[32]: # size is DEPOSIT_CONTRACT_TREE_DEPTH (symbolic const not supported) + branch: bytes32[32] # size is DEPOSIT_CONTRACT_TREE_DEPTH + index: uint256 = leaf + TWO_TO_POWER_OF_TREE_DEPTH for i in range(DEPOSIT_CONTRACT_TREE_DEPTH): - ret[i] = self.receipt_tree[bitwise_xor(idx,1)] - idx /= 2 - return ret + branch[i] = self.deposit_tree[bitwise_xor(index, 1)] + index /= 2 + return branch diff --git a/tests/contracts/test_deposit.py b/tests/contracts/test_deposit.py index af8b48b..839ef10 100644 --- a/tests/contracts/test_deposit.py +++ b/tests/contracts/test_deposit.py @@ -64,7 +64,7 @@ def test_deposit_amount(registration_contract, w3, success, amount_deposit, asse def test_deposit_log(registration_contract, a0, w3): - log_filter = registration_contract.events.Eth1Deposit.createFilter( + log_filter = registration_contract.events.Deposit.createFilter( fromBlock='latest', ) @@ -82,11 +82,11 @@ def test_deposit_log(registration_contract, a0, w3): amount_bytes8 = deposit_amount[i].to_bytes(8, 'big') timestamp_bytes8 = int(w3.eth.getBlock(w3.eth.blockNumber)['timestamp']).to_bytes(8, 'big') if i == 0: - assert log['previous_receipt_root'] == b'\x00' * 32 + assert log['previous_deposit_root'] == b'\x00' * 32 else: - assert log['previous_receipt_root'] != b'\x00' * 32 + assert log['previous_deposit_root'] != b'\x00' * 32 assert log['data'] == amount_bytes8 + timestamp_bytes8 + deposit_input - assert log['deposit_count'] == i + assert log['merkle_tree_index'] == (i + TWO_TO_POWER_OF_TREE_DEPTH).to_bytes(8, 'big') def test_reciept_tree(registration_contract, w3, assert_tx_failed): @@ -106,9 +106,9 @@ def test_reciept_tree(registration_contract, w3, assert_tx_failed): data = amount_bytes8 + timestamp_bytes8 + deposit_input leaf_nodes.append(w3.sha3(data)) root = compute_merkle_root(w3, leaf_nodes) - assert registration_contract.functions.get_receipt_root().call() == root + assert registration_contract.functions.get_deposit_root().call() == root index = randint(0, i) - branch = registration_contract.functions.get_merkle_branch(index).call() + branch = registration_contract.functions.get_branch(index).call() assert verify_merkle_branch(w3, root, index, leaf_nodes[index], branch) @@ -152,7 +152,7 @@ def test_chain_start(modified_registration_contract, w3, assert_tx_failed): timestamp = int(w3.eth.getBlock(w3.eth.blockNumber)['timestamp']) timestamp_day_boundary = timestamp + (86400 - timestamp % 86400) log = logs[0]['args'] - assert log['receipt_root'] == modified_registration_contract.functions.get_receipt_root().call() + assert log['deposit_root'] == modified_registration_contract.functions.get_deposit_root().call() assert int.from_bytes(log['time'], byteorder='big') == timestamp_day_boundary # Make 1 deposit with value MAX_DEPOSIT and check that ChainStart event is not triggered