NEAR Rainbow Bridge代码解析
Posted mutourend
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NEAR Rainbow Bridge代码解析相关的知识,希望对你有一定的参考价值。
1. 引言
前序博客有:
NEAR系列博客有:
NEAR团队开发的Rainbow Bridge,无需信任bridge本身,仅需要信任所连接的NEAR链和以太坊链。除了以太坊链的矿工和NEAR链的validators,无任何authority。
相关代码实现见:
- https://github.com/aurora-is-near/rainbow-bridge(Solidity & Go & Java & Rust)【Rainbow Bridge核心层】
- https://github.com/aurora-is-near/aurora-relayer【Relayer】
- https://github.com/Shard-Labs/rainbow-non-fungible-token-connector【目前未被NEAR官方认可的NFT Connector】
- https://github.com/aurora-is-near/rainbow-token-connector【ERC-20/NEP-141 Token Connector】
- https://github.com/aurora-is-near/near-erc20-connector【$NEAR Connector】
- https://github.com/aurora-is-near/eth-connector【$ETH Connector】
- https://github.com/near/rainbow-bridge-sol【早期原型】
- https://github.com/aurora-is-near/rainbow-bridge-frontend【前端界面】
链上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()
以太坊端:
- NearBridge合约:作为以太坊端的NEAR light client,relayers每4小时向其提交NEAR区块头。
- NearProver合约
- EthCustodian合约【$ETH Connector】
- Erc20Locker合约【ERC-20 Connector】
- eNear合约【$NEAR Connector】
NEAR端:
- relayer.bridge.near 账号【主链relayer.bridge.near账号
add_block_header
交易】 - client.bridge.near合约【主链EthOnNear light client合约】
- event-relayer.near 账号【主链event-relayer.near账号
deposit
交易】 - factory.bridge.near合约
- prover.bridge.near合约
- e-near.near合约【主链ERC20 Connector合约】
根据NEAR主链finalise_eth_to_near_transfer 交易可知,由Ethereum->NEAR端的交易,由专门的event-relayer负责向NEAR端提交。
根据https://etherscan.io/tx/0x13efcf3d0f84cf0027081e516a628d50833aebcb0a002f6f87def6f2e09bd924 可知,对于NEAR->以太坊的转移,向以太坊提交proof event的为用户本人,而不是relayers。
总体交互流程为:
以上是关于NEAR Rainbow Bridge代码解析的主要内容,如果未能解决你的问题,请参考以下文章