This commit is contained in:
Justin Drake 2019-06-09 11:29:22 +01:00
parent 4ee00c9cbd
commit c293b9dcef
3 changed files with 36 additions and 62 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,59 +1,54 @@
MIN_DEPOSIT_AMOUNT: constant(uint256) = 1000000000 # Gwei MIN_DEPOSIT_AMOUNT: constant(uint256) = 1000000000 # Gwei
DEPOSIT_CONTRACT_TREE_DEPTH: constant(uint256) = 32 DEPOSIT_CONTRACT_TREE_DEPTH: constant(uint256) = 32
MAX_64_BIT_VALUE: constant(uint256) = 18446744073709551615 # 2**64 - 1
PUBKEY_LENGTH: constant(uint256) = 48 # bytes PUBKEY_LENGTH: constant(uint256) = 48 # bytes
WITHDRAWAL_CREDENTIALS_LENGTH: constant(uint256) = 32 # bytes WITHDRAWAL_CREDENTIALS_LENGTH: constant(uint256) = 32 # bytes
AMOUNT_LENGTH: constant(uint256) = 8 # bytes
SIGNATURE_LENGTH: constant(uint256) = 96 # bytes SIGNATURE_LENGTH: constant(uint256) = 96 # bytes
MAX_DEPOSIT_COUNT: constant(uint256) = 4294967295 # 2**DEPOSIT_CONTRACT_TREE_DEPTH - 1
Deposit: event({ Deposit: event({
pubkey: bytes[48], pubkey: bytes[48],
withdrawal_credentials: bytes[32], withdrawal_credentials: bytes[32],
amount: bytes[8], amount: bytes[8],
signature: bytes[96], signature: bytes[96],
merkle_tree_index: bytes[8],
}) })
zerohashes: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH]
branch: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH] branch: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH]
deposit_count: uint256 deposit_count: uint256
# Compute hashes in empty sparse Merkle tree
zero_hashes: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH]
@public @public
def __init__(): def __init__():
for i in range(DEPOSIT_CONTRACT_TREE_DEPTH - 1): for i in range(DEPOSIT_CONTRACT_TREE_DEPTH - 1):
self.zerohashes[i+1] = sha256(concat(self.zerohashes[i], self.zerohashes[i])) self.zero_hashes[i + 1] = sha256(concat(self.zero_hashes[i], self.zero_hashes[i]))
@public @public
@constant @constant
def to_little_endian_64(value: uint256) -> bytes[8]: def to_little_endian_64(value: uint256) -> bytes[8]:
assert value <= MAX_64_BIT_VALUE # Reversing bytes using bitwise uint256 manipulations
# (array accesses of bytes[] are not currently supported in Vyper)
# array access for bytes[] not currently supported in vyper so
# reversing bytes using bitwise uint256 manipulations
y: uint256 = 0 y: uint256 = 0
x: uint256 = value x: uint256 = value
for i in range(8): for _ in range(8):
y = shift(y, 8) y = shift(y, 8)
y = y + bitwise_and(x, 255) y = y + bitwise_and(x, 255)
x = shift(x, -8) x = shift(x, -8)
return slice(convert(y, bytes32), start=24, len=8) return slice(convert(y, bytes32), start=24, len=8)
@public @public
@constant @constant
def get_deposit_root() -> bytes32: def get_deposit_root() -> bytes32:
root: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000 node: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000
size: uint256 = self.deposit_count size: uint256 = self.deposit_count
for h in range(DEPOSIT_CONTRACT_TREE_DEPTH): for height in range(DEPOSIT_CONTRACT_TREE_DEPTH):
if bitwise_and(size, 1) == 1: if bitwise_and(size, 1) == 1: # More gas efficient than `size % 2 == 1`
root = sha256(concat(self.branch[h], root)) node = sha256(concat(self.branch[height], node))
else: else:
root = sha256(concat(root, self.zerohashes[h])) node = sha256(concat(node, self.zero_hashes[height]))
size /= 2 size /= 2
return root return node
@public @public
@ -67,56 +62,38 @@ def get_deposit_count() -> bytes[8]:
def deposit(pubkey: bytes[PUBKEY_LENGTH], def deposit(pubkey: bytes[PUBKEY_LENGTH],
withdrawal_credentials: bytes[WITHDRAWAL_CREDENTIALS_LENGTH], withdrawal_credentials: bytes[WITHDRAWAL_CREDENTIALS_LENGTH],
signature: bytes[SIGNATURE_LENGTH]): signature: bytes[SIGNATURE_LENGTH]):
# Prevent edge case in computing `self.branch` when `self.deposit_count == MAX_DEPOSIT_COUNT` # Avoid overflowing the Merkle tree
# NOTE: reaching this point with the constants as currently defined is impossible due to the assert self.deposit_count < 2**DEPOSIT_CONTRACT_TREE_DEPTH - 1
# uni-directional nature of transfers from eth1 to eth2 and the total ether supply (< 130M).
assert self.deposit_count < MAX_DEPOSIT_COUNT
# Validate deposit data
deposit_amount: uint256 = msg.value / as_wei_value(1, "gwei")
assert deposit_amount >= MIN_DEPOSIT_AMOUNT
assert len(pubkey) == PUBKEY_LENGTH assert len(pubkey) == PUBKEY_LENGTH
assert len(withdrawal_credentials) == WITHDRAWAL_CREDENTIALS_LENGTH assert len(withdrawal_credentials) == WITHDRAWAL_CREDENTIALS_LENGTH
assert len(signature) == SIGNATURE_LENGTH assert len(signature) == SIGNATURE_LENGTH
deposit_amount: uint256 = msg.value / as_wei_value(1, "gwei") # Emit `Deposit` log
assert deposit_amount >= MIN_DEPOSIT_AMOUNT
amount: bytes[8] = self.to_little_endian_64(deposit_amount) amount: bytes[8] = self.to_little_endian_64(deposit_amount)
log.Deposit(pubkey, withdrawal_credentials, amount, signature)
index: uint256 = self.deposit_count # Compute `DepositData` root
zero_bytes32: bytes32
# add deposit to merkle tree pubkey_root: bytes32 = sha256(concat(pubkey, slice(zero_bytes32, start=0, len=64 - PUBKEY_LENGTH)))
i: int128 = 0
size: uint256 = index + 1
for _ in range(DEPOSIT_CONTRACT_TREE_DEPTH):
if bitwise_and(size, 1) == 1:
break
i += 1
size /= 2
zero_bytes_32: bytes32
pubkey_root: bytes32 = sha256(concat(pubkey, slice(zero_bytes_32, start=0, len=16)))
signature_root: bytes32 = sha256(concat( signature_root: bytes32 = sha256(concat(
sha256(slice(signature, start=0, len=64)), sha256(slice(signature, start=0, len=64)),
sha256(concat(slice(signature, start=64, len=32), zero_bytes_32)) sha256(concat(slice(signature, start=64, len=SIGNATURE_LENGTH - 64), zero_bytes32)),
)) ))
value: bytes32 = sha256(concat( node: bytes32 = sha256(concat(
sha256(concat(pubkey_root, withdrawal_credentials)), sha256(concat(pubkey_root, withdrawal_credentials)),
sha256(concat( sha256(concat(amount, slice(zero_bytes32, start=0, len=32 - AMOUNT_LENGTH), signature_root)),
amount,
slice(zero_bytes_32, start=0, len=24),
signature_root,
))
)) ))
for j in range(DEPOSIT_CONTRACT_TREE_DEPTH):
if j < i:
value = sha256(concat(self.branch[j], value))
else:
break
self.branch[i] = value
# Add `DepositData` root to Merkle tree (update a single `branch` node)
self.deposit_count += 1 self.deposit_count += 1
log.Deposit( size: uint256 = self.deposit_count
pubkey, for height in range(DEPOSIT_CONTRACT_TREE_DEPTH):
withdrawal_credentials, if bitwise_and(size, 1) == 1: # More gas efficient than `size % 2 == 1`
amount, self.branch[height] = node
signature, break
self.to_little_endian_64(index), node = sha256(concat(self.branch[height], node))
) size /= 2

View File

@ -56,7 +56,7 @@ def deposit_input():
(10, True), (10, True),
(55555, True), (55555, True),
(2**64 - 1, True), (2**64 - 1, True),
(2**64, False), (2**64, True), # Note that all calls to `to_little_endian_64` have an input less than 2**64
] ]
) )
def test_to_little_endian_64(registration_contract, value, success, assert_tx_failed): def test_to_little_endian_64(registration_contract, value, success, assert_tx_failed):
@ -151,7 +151,6 @@ def test_deposit_log(registration_contract, a0, w3, deposit_input):
assert log['withdrawal_credentials'] == deposit_input[1] assert log['withdrawal_credentials'] == deposit_input[1]
assert log['amount'] == deposit_amount_list[i].to_bytes(8, 'little') assert log['amount'] == deposit_amount_list[i].to_bytes(8, 'little')
assert log['signature'] == deposit_input[2] assert log['signature'] == deposit_input[2]
assert log['merkle_tree_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):
@ -172,8 +171,6 @@ def test_deposit_tree(registration_contract, w3, assert_tx_failed, deposit_input
assert len(logs) == 1 assert len(logs) == 1
log = logs[0]['args'] log = logs[0]['args']
assert log["merkle_tree_index"] == i.to_bytes(8, 'little')
deposit_data = DepositData( deposit_data = DepositData(
pubkey=deposit_input[0], pubkey=deposit_input[0],
withdrawal_credentials=deposit_input[1], withdrawal_credentials=deposit_input[1],