prysm-pulse/contracts/deposit-contract/depositContract.v.py
Nishant Das b07a3c3bb8 Update Deposit Contract (#1494)
* Update deposit Contarct

* updating deposit trie

* add verifyMerkleBranch

* fix test

* fixed all tests

* fix other tests

* fixing a test

* All tests are fixed

* lint

* fix lint
2019-02-08 11:01:15 -06:00

86 lines
3.0 KiB
Python

## compiled with v0.1.0-beta.7 ##
DEPOSIT_TREE_DEPTH: constant(uint256) = 32
TWO_TO_POWER_OF_TREE_DEPTH: constant(uint256) = 4294967296 # 2**32
SECONDS_PER_DAY: constant(uint256) = 86400
Deposit: event({deposit_root: bytes32, data: bytes[528], merkle_tree_index: bytes[8], branch: bytes32[32]})
ChainStart: event({deposit_root: bytes32, time: bytes[8]})
MIN_DEPOSIT_AMOUNT: uint256
MAX_DEPOSIT_AMOUNT: uint256
CHAIN_START_FULL_DEPOSIT_THRESHOLD: uint256
zerohashes: bytes32[32]
branch: bytes32[32]
deposit_count: uint256
full_deposit_count: uint256
chainStarted: public(bool)
@public
def __init__(depositThreshold: uint256,minDeposit: uint256,maxDeposit: uint256):
self.CHAIN_START_FULL_DEPOSIT_THRESHOLD = depositThreshold
self.MIN_DEPOSIT_AMOUNT = minDeposit
self.MAX_DEPOSIT_AMOUNT = maxDeposit
for i in range(31):
self.zerohashes[i+1] = sha3(concat(self.zerohashes[i], self.zerohashes[i]))
self.branch[i+1] = self.zerohashes[i+1]
@public
@constant
def to_bytes8(value: uint256) -> bytes[8]:
return slice(convert(value, bytes32), start=24, len=8)
@public
@constant
def get_deposit_root() -> bytes32:
root:bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000
size:uint256 = self.deposit_count
for h in range(32):
if size % 2 == 1:
root = sha3(concat(self.branch[h], root))
else:
root = sha3(concat(root, self.zerohashes[h]))
size /= 2
return root
@payable
@public
def deposit(deposit_input: bytes[512]):
deposit_amount: uint256 = msg.value / as_wei_value(1, "gwei")
assert deposit_amount >= self.MIN_DEPOSIT_AMOUNT
assert deposit_amount <= self.MAX_DEPOSIT_AMOUNT
index: uint256 = self.deposit_count
deposit_timestamp: uint256 = as_unitless_number(block.timestamp)
deposit_data: bytes[528] = concat(self.to_bytes8(deposit_amount), self.to_bytes8(deposit_timestamp), deposit_input)
# add deposit to merkle tree
i: int128 = 0
power_of_two: uint256 = 2
for _ in range(32):
if (index+1) % power_of_two != 0:
break
i += 1
power_of_two *= 2
value:bytes32 = sha3(deposit_data)
for j in range(32):
if j < i:
value = sha3(concat(self.branch[j], value))
self.branch[i] = value
self.deposit_count += 1
new_deposit_root:bytes32 = self.get_deposit_root()
log.Deposit(new_deposit_root, deposit_data, self.to_bytes8(index), self.branch)
if deposit_amount == self.MAX_DEPOSIT_AMOUNT:
self.full_deposit_count += 1
if self.full_deposit_count == self.CHAIN_START_FULL_DEPOSIT_THRESHOLD:
# Temporarily commenting out the day boundary.
#timestamp_day_boundary: uint256 = deposit_timestamp - deposit_timestamp % SECONDS_PER_DAY + SECONDS_PER_DAY
#log.ChainStart(self.get_deposit_root(), self.to_bytes(timestamp_day_boundary))
log.ChainStart(self.get_deposit_root(), self.to_bytes8(deposit_timestamp))
self.chainStarted = True