可以看到:
在生成上述的参数后,会按照合约接口编码成一定的格式,然后发送给存款合约完成调用。 合约执行合约的主要方法 deposit,定义了收到一笔存款交易时如何处理,存款交易正是通过调用这一方法来实现存款。 deposit 方法接受 pubkey (验证者公钥)、withdrawal_credentials (提取存款权限信息),signature (签名)和 deposit_data_root (防止篡改标识)作为参数。主要分成参数基础校验、触发存款事件、检验数据完整性、更新数据结构几个部分,如以下代码所示: @public def deposit(pubkey: bytes[PUBKEY_LENGTH], withdrawal_credentials: bytes[WITHDRAWAL_CREDENTIALS_LENGTH], signature: bytes[SIGNATURE_LENGTH], deposit_data_root: bytes32): ############## 1. 参数基础校验 ################ # Avoid overflowing the Merkle tree (and prevent edge case in computing `self.branch`) assert self.deposit_count < MAX_DEPOSIT_COUNT # Check deposit amount deposit_amount: uint256 = msg.value / as_wei_value(1, "gwei") assert deposit_amount >= MIN_DEPOSIT_AMOUNT # Length checks for safety assert len(pubkey) == PUBKEY_LENGTH assert len(withdrawal_credentials) == WITHDRAWAL_CREDENTIALS_LENGTH assert len(signature) == SIGNATURE_LENGTH ######################################### # Emit `DepositEvent` log amount: bytes[8] = self.to_little_endian_64(deposit_amount) ############## 2. 触发存款事件 ################ log.DepositEvent(pubkey, withdrawal_credentials, amount, signature, self.to_little_endian_64(self.deposit_count)) ############## 3. 校验数据完整性 ################ # Compute deposit data root (`DepositData` hash tree root) zero_bytes32: bytes32 = 0x0000000000000000000000000000000000000000000000000000000000000000 pubkey_root: bytes32 = sha256(concat(pubkey, slice(zero_bytes32, start=0, len=64 - PUBKEY_LENGTH))) signature_root: bytes32 = sha256(concat( sha256(slice(signature, start=0, len=64)), sha256(concat(slice(signature, start=64, len=SIGNATURE_LENGTH - 64), zero_bytes32)), )) node: bytes32 = sha256(concat( sha256(concat(pubkey_root, withdrawal_credentials)), sha256(concat(amount, slice(zero_bytes32, start=0, len=32 - AMOUNT_LENGTH), signature_root)), )) # Verify computed and expected deposit data roots match assert node == deposit_data_root ########################################### ############## 4. 更新 Merkle Tree ################ # Add deposit data root to Merkle tree (update a single `branch` node) self.deposit_count += 1 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 (责任编辑:admin) |