NEAR Rainbow Bridge代码解析

Posted mutourend

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NEAR Rainbow Bridge代码解析相关的知识,希望对你有一定的参考价值。

1. 引言

前序博客有:

NEAR系列博客有:

NEAR团队开发的Rainbow Bridge,无需信任bridge本身,仅需要信任所连接的NEAR链和以太坊链。除了以太坊链的矿工和NEAR链的validators,无任何authority。

相关代码实现见:


链上light client合约:

  • 以太坊的每个区块头,均需要由Eth2NearRelay通过add_block_header提交到部署在NEAR链上的EthOnNearClient合约中。【采用 以太坊PoW light client Ethash proof
  • NEAR链上每隔4小时的区块头,均需要由Near2EthRelay通过addLightClientBlock提交到部署在以太坊上的NearOnEthClient合约中。

由于以太坊目前为上线EIP-665,验证NEAR Ed25519签名的gas费过高,故在以太坊合约中采用optimisitic challenge模式,在4小时的挑战窗口期内,任何人都可挑战Ed25519,若挑战成功,将获得lastSubmitter lockEthAmount一半的金额。【当前以太坊NearBridge合约中,lockEthAmount设置为 5 ETH。】
因此,任何人可运行Watchdog服务,调用相应的challenge函数:【Watchdog仅需在以太坊端运行】

	function challenge(address payable receiver, uint signatureIndex) public override pausable(PAUSED_CHALLENGE) 
        require(block.timestamp < lastValidAt, "No block can be challenged at this time");
        require(!checkBlockProducerSignatureInHead(signatureIndex), "Can't challenge valid signature");

        balanceOf[lastSubmitter] = balanceOf[lastSubmitter] - lockEthAmount;
        receiver.transfer(lockEthAmount / 2);
        lastValidAt = 0;
    

向NEAR链EthProver合约中提交的proof,主要为以太坊区块头中Receipts Merkle Patricia Tree的proof:【具体为调用相应connector合约中的finalise_eth_to_near_transfer函数。】

#[result_serializer(borsh)]
    pub fn verify_log_entry(
        &self,
        #[serializer(borsh)] log_index: u64,
        #[serializer(borsh)] log_entry_data: Vec<u8>,
        #[serializer(borsh)] receipt_index: u64,
        #[serializer(borsh)] receipt_data: Vec<u8>,
        #[serializer(borsh)] header_data: Vec<u8>,
        #[serializer(borsh)] proof: Vec<Vec<u8>>,
        #[serializer(borsh)] skip_bridge_call: bool,
    ) -> PromiseOrValue<bool> 
        self.check_not_paused(PAUSE_VERIFY);
        let log_entry: LogEntry = rlp::decode(log_entry_data.as_slice()).unwrap();
        let receipt: Receipt = rlp::decode(receipt_data.as_slice()).unwrap();
        let header: BlockHeader = rlp::decode(header_data.as_slice()).unwrap();

        // Verify log_entry included in receipt
        assert_eq!(receipt.logs[log_index as usize], log_entry);

        // Verify receipt included into header
        let data =
            Self::verify_trie_proof(header.receipts_root, rlp::encode(&receipt_index), proof);//使用Receipts root.
        let verification_result = receipt_data == data;
        if verification_result && skip_bridge_call 
            return PromiseOrValue::Value(true);
         else if !verification_result 
            return PromiseOrValue::Value(false);
        

        // Verify block header was in the bridge
        eth_client::block_hash_safe(
            header.number,
            &self.bridge_smart_contract,
            0,
            BLOCK_HASH_SAFE_GAS,
        )
        .then(remote_self::on_block_hash(
            header.hash.unwrap(),
            &env::current_account_id(),
            0,
            ON_BLOCK_HASH_GAS,
        ))
        .into()
    

以太坊端:

NEAR端:

根据NEAR主链finalise_eth_to_near_transfer 交易可知,由Ethereum->NEAR端的交易,由专门的event-relayer负责向NEAR端提交。
根据https://etherscan.io/tx/0x13efcf3d0f84cf0027081e516a628d50833aebcb0a002f6f87def6f2e09bd924 可知,对于NEAR->以太坊的转移,向以太坊提交proof event的为用户本人,而不是relayers。

总体交互流程为:

以上是关于NEAR Rainbow Bridge代码解析的主要内容,如果未能解决你的问题,请参考以下文章

以太坊PoW light client Ethash proof

Aztec connect bridge代码解析

NEAR 智能合约开发

Celo Optics Bridge 代码解析

NEAR像素挖矿游戏BerryClub日交易量突破17万笔

Dapper学习 - Dapper.Rainbow - Read