eth2.0-specs/deposit_contract/contracts/validator_registration.v.py

111 lines
4.1 KiB
Python
Raw Normal View History

2019-12-18 00:58:56 +00:00
# Vyper target 0.1.0b13.hotfix1761
2019-05-27 05:15:10 +00:00
MIN_DEPOSIT_AMOUNT: constant(uint256) = 1000000000 # Gwei
DEPOSIT_CONTRACT_TREE_DEPTH: constant(uint256) = 32
2019-06-10 14:55:08 +00:00
MAX_DEPOSIT_COUNT: constant(uint256) = 4294967295 # 2**DEPOSIT_CONTRACT_TREE_DEPTH - 1
PUBKEY_LENGTH: constant(uint256) = 48 # bytes
WITHDRAWAL_CREDENTIALS_LENGTH: constant(uint256) = 32 # bytes
SIGNATURE_LENGTH: constant(uint256) = 96 # bytes
2019-09-03 17:59:18 +00:00
AMOUNT_LENGTH: constant(uint256) = 8 # bytes
2019-05-27 05:15:10 +00:00
DepositEvent: event({
2019-05-27 05:15:10 +00:00
pubkey: bytes[48],
withdrawal_credentials: bytes[32],
amount: bytes[8],
signature: bytes[96],
2019-06-11 14:25:25 +00:00
index: bytes[8],
2019-05-27 05:15:10 +00:00
})
branch: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH]
deposit_count: uint256
2019-06-09 10:29:22 +00:00
# Compute hashes in empty sparse Merkle tree
zero_hashes: bytes32[DEPOSIT_CONTRACT_TREE_DEPTH]
2019-05-27 05:15:10 +00:00
@public
def __init__():
for i in range(DEPOSIT_CONTRACT_TREE_DEPTH - 1):
2019-06-09 10:29:22 +00:00
self.zero_hashes[i + 1] = sha256(concat(self.zero_hashes[i], self.zero_hashes[i]))
2019-05-27 05:15:10 +00:00
2019-06-09 10:03:38 +00:00
2019-06-11 14:25:25 +00:00
@private
2019-05-27 05:15:10 +00:00
@constant
def to_little_endian_64(value: uint256) -> bytes[8]:
2019-06-09 10:29:22 +00:00
# Reversing bytes using bitwise uint256 manipulations
2019-06-10 14:55:08 +00:00
# Note: array accesses of bytes[] are not currently supported in Vyper
# Note: this function is only called when `value < 2**64`
2019-05-27 05:15:10 +00:00
y: uint256 = 0
x: uint256 = value
2019-06-09 10:29:22 +00:00
for _ in range(8):
2019-05-27 05:15:10 +00:00
y = shift(y, 8)
y = y + bitwise_and(x, 255)
x = shift(x, -8)
return slice(convert(y, bytes32), start=24, len=8)
2019-06-09 10:03:38 +00:00
2019-05-27 05:15:10 +00:00
@public
@constant
2019-09-03 17:59:18 +00:00
def get_deposit_root() -> bytes32:
zero_bytes32: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000
node: bytes32 = zero_bytes32
2019-05-27 05:15:10 +00:00
size: uint256 = self.deposit_count
2019-06-09 10:29:22 +00:00
for height in range(DEPOSIT_CONTRACT_TREE_DEPTH):
if bitwise_and(size, 1) == 1: # More gas efficient than `size % 2 == 1`
node = sha256(concat(self.branch[height], node))
2019-05-27 05:15:10 +00:00
else:
2019-06-09 10:29:22 +00:00
node = sha256(concat(node, self.zero_hashes[height]))
2019-05-27 05:15:10 +00:00
size /= 2
2019-06-29 08:25:19 +00:00
return sha256(concat(node, self.to_little_endian_64(self.deposit_count), slice(zero_bytes32, start=0, len=24)))
2019-05-27 05:15:10 +00:00
2019-06-09 10:03:38 +00:00
2019-05-27 05:15:10 +00:00
@public
@constant
def get_deposit_count() -> bytes[8]:
return self.to_little_endian_64(self.deposit_count)
2019-06-09 10:03:38 +00:00
2019-05-27 05:15:10 +00:00
@payable
@public
def deposit(pubkey: bytes[PUBKEY_LENGTH],
withdrawal_credentials: bytes[WITHDRAWAL_CREDENTIALS_LENGTH],
2019-09-03 17:59:18 +00:00
signature: bytes[SIGNATURE_LENGTH],
deposit_data_root: bytes32):
2019-06-10 14:55:08 +00:00
# Avoid overflowing the Merkle tree (and prevent edge case in computing `self.branch`)
assert self.deposit_count < MAX_DEPOSIT_COUNT
2019-09-03 17:59:18 +00:00
# Check deposit amount
2019-06-09 10:29:22 +00:00
deposit_amount: uint256 = msg.value / as_wei_value(1, "gwei")
assert deposit_amount >= MIN_DEPOSIT_AMOUNT
2019-09-03 17:59:18 +00:00
2019-11-26 19:40:58 +00:00
# Length checks for safety
assert len(pubkey) == PUBKEY_LENGTH
assert len(withdrawal_credentials) == WITHDRAWAL_CREDENTIALS_LENGTH
assert len(signature) == SIGNATURE_LENGTH
# Emit `DepositEvent` log
2019-06-09 16:03:35 +00:00
amount: bytes[8] = self.to_little_endian_64(deposit_amount)
log.DepositEvent(pubkey, withdrawal_credentials, amount, signature, self.to_little_endian_64(self.deposit_count))
2019-06-11 18:03:26 +00:00
2019-09-03 17:59:18 +00:00
# Compute deposit data root (`DepositData` hash tree root)
2019-06-11 18:03:26 +00:00
zero_bytes32: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000
2019-06-09 10:29:22 +00:00
pubkey_root: bytes32 = sha256(concat(pubkey, slice(zero_bytes32, start=0, len=64 - PUBKEY_LENGTH)))
2019-05-27 05:15:10 +00:00
signature_root: bytes32 = sha256(concat(
sha256(slice(signature, start=0, len=64)),
2019-06-09 10:29:22 +00:00
sha256(concat(slice(signature, start=64, len=SIGNATURE_LENGTH - 64), zero_bytes32)),
2019-05-27 05:15:10 +00:00
))
2019-06-09 10:29:22 +00:00
node: bytes32 = sha256(concat(
2019-05-27 05:15:10 +00:00
sha256(concat(pubkey_root, withdrawal_credentials)),
2019-06-09 10:29:22 +00:00
sha256(concat(amount, slice(zero_bytes32, start=0, len=32 - AMOUNT_LENGTH), signature_root)),
2019-05-27 05:15:10 +00:00
))
2019-09-03 17:59:18 +00:00
# Verify computed and expected deposit data roots match
assert node == deposit_data_root
2019-05-27 05:15:10 +00:00
2019-09-03 17:59:18 +00:00
# Add deposit data root to Merkle tree (update a single `branch` node)
2019-05-27 05:15:10 +00:00
self.deposit_count += 1
2019-06-09 10:29:22 +00:00
size: uint256 = self.deposit_count
for height in range(DEPOSIT_CONTRACT_TREE_DEPTH):
if bitwise_and(size, 1) == 1: # More gas efficient than `size % 2 == 1`
self.branch[height] = node
break
node = sha256(concat(self.branch[height], node))
size /= 2
2019-06-09 16:03:35 +00:00