美文网首页
私有化LayerZero Protocol测试示例

私有化LayerZero Protocol测试示例

作者: 张亚伦 | 来源:发表于2024-09-10 18:36 被阅读0次

    实现单向holesky到amoy的跨链功能

    操作流程:

    1. 合约部署 已完成
    2. 参数配置 已完成
    3. 跨链调用 已完成

    一、合约概要图

    image.png

    二、合约部署

    Chain EID Owner
    Holesky 40217 (9d19) 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048
    Amoy 40267 (9d4b) 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    2.1【源链】-Holesky

    Eid: 40217 9d19

    Endpiont

    owner:0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    构造参数

    参数名称 参数值
    _eid 40217
    _owner 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    部署txhash

    https://holesky.etherscan.io/tx/0x9c5ab7f3909d49b387d07e10b4c03dfd7edd797453b65eefc05d618781ac27dc

    部署结果

    endpoint: 0xf37817f975cf3865abd5f7a860ff4968e3ad23c9

    newLib :0x8Ce308aB42361C63F114e59CaBbB9393D7DEa41B

    SendLib302

    owner:0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    构造参数

    参数名称 参数值
    _endpoint 0xf37817f975cf3865abd5f7a860ff4968e3ad23c9
    _treasuryGasLimit 200000 30d40
    _treasuryGasForFeeCap 1000000000000000000 1*10^18 de0b6b3a7640000

    部署txhash

    https://holesky.etherscan.io/tx/0x130f7f59ad1ae5975d596fe87933fc86769d5575b99eb5439a272a9f82fe4e90

    部署结果

    SendLib302:0xe83d86ed3352a4eb4a8098e3f189ebecfb5e0a22

    DVN

    owner: 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    构造参数

    0x00000000000000000000000000000000000000000000000000000000000027e900000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000e83d86ed3352a4eb4a8098e3f189ebecfb5e0a2200000000000000000000000000000000000000000000000000000000000000010000000000000000000000004e0539f3e0a941dcd0617f636381b4c3bc25f04800000000000000000000000000000000000000000000000000000000000000010000000000000000000000004e0539f3e0a941dcd0617f636381b4c3bc25f048

    参数名称 参数值
    vid 10217 40217%30_000
    _messageLibs ["0xe83d86ed3352a4eb4a8098e3f189ebecfb5e0a22"] SendLib302
    _priceFeed 0x
    _signers ["0x4e0539F3e0A941dCD0617F636381b4c3BC25F048"]
    _quorum 1
    _admins ["0x4e0539F3e0A941dCD0617F636381b4c3BC25F048"]

    部署txhash

    https://holesky.etherscan.io/tx/0xddb5b2916293506149cdcba03d8a5c81099474011964a8d9403ed7b471b8c465

    部署结果

    DVN:0xc58dc2e767918335e6879d5019fde8705899d6d8

    Executor

    owner: 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    逻辑合约

    构造参数

    部署txhash

    https://holesky.etherscan.io/tx/0x8672290925baa3a17ffc7ebe4832cec6041e3b76378e3289271faeaf5021f3f7

    部署结果

    executor: 0x4e590bb18e7799da84833d55a851a71b68bffd89

    代理合约

    1967

    构造参数

    initialize()

    calldata:0x009fc681000000000000000000000000f37817f975cf3865abd5f7a860ff4968e3ad23c9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004e0539f3e0a941dcd0617f636381b4c3bc25f04800000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000e83d86ed3352a4eb4a8098e3f189ebecfb5e0a2200000000000000000000000000000000000000000000000000000000000000010000000000000000000000004e0539f3e0a941dcd0617f636381b4c3bc25f048

    参数名称 参数值
    _endpoint 0xf37817f975cf3865abd5f7a860ff4968e3ad23c9
    _receiveUln301 0x0000000000000000000000000000000000000000
    _messageLibs ["0xe83d86ed3352a4eb4a8098e3f189ebecfb5e0a22"] sendlib302
    _priceFeed 0x0000000000000000000000000000000000000000
    _roleAdmin 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048
    _admins ["0x4e0539F3e0A941dCD0617F636381b4c3BC25F048"]

    erc1967:

    参数名称 参数值
    _logic 0x4E590Bb18E7799DA84833D55a851A71B68bFFd89
    _DATA 0x009fc681000000000000000000000000f37817f975cf3865abd5f7a860ff4968e3ad23c9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004e0539f3e0a941dcd0617f636381b4c3bc25f04800000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000e83d86ed3352a4eb4a8098e3f189ebecfb5e0a2200000000000000000000000000000000000000000000000000000000000000010000000000000000000000004e0539f3e0a941dcd0617f636381b4c3bc25f048
    部署txhash

    https://holesky.etherscan.io/tx/0x80cbcbb1aced6c2170ff886f259c063b4f3f86a41c0825d73cdf07a9fa59d281

    部署结果

    executor proxy: 0x8bfcc2dc3c4f9e48e4e1be2c6b3e4cba2a0a8dcb

    ReceiveLib302

    owner:0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    构造参数

    参数名称 参数值
    _endpoint 0xf37817f975cf3865abd5f7a860ff4968e3ad23c9

    部署txhash

    https://holesky.etherscan.io/tx/0x3c27e41b7ff9896578db4b19ffae2aad3aca0f97ba9253a49c63ed2db5855ccf

    部署结果

    ReceiveLib302:0xb70dd8b342ce6e4a3fd2d070c0b1098ae8f05a5e

    2.【目标链】-Amoy

    Eid: 40267 9d4b

    Endpiont

    owner:0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    构造参数

    参数名称 参数值
    _eid 40267
    _owner 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    部署txhash

    https://amoy.polygonscan.com/tx/0xc45654be673b48a52e625e2097b77134d788ddc3b7453639082650ba4c3b195e

    部署结果

    endpoint: 0xf37817f975cf3865abd5f7a860ff4968e3ad23c9

    newLib :0x8Ce308aB42361C63F114e59CaBbB9393D7DEa41B

    ReceiveLib302

    owner:0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    构造参数

    数名称 参数值
    _endpoint 0xf37817f975cf3865abd5f7a860ff4968e3ad23c9

    部署txhash

    https://amoy.polygonscan.com/tx/0x8853e44b3dec57f89bc0fd09753a50ad327c51584e916a803e6423f3ac72528c

    部署结果

    ReceiveLib302:0xe83d86ed3352a4eb4a8098e3f189ebecfb5e0a22

    SendLib302

    owner:0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    构造参数

    参数名称 数值
    _endpoint 0xf37817f975cf3865abd5f7a860ff4968e3ad23c9
    _treasuryGasLimit 200000 30d40
    _treasuryGasForFeeCap 1000000000000000000 1*10^19 de0b6b3a7640000

    部署txhash

    https://amoy.polygonscan.com/tx/0xaacc57c3fa65fc8b5613ef6a31095273099a5fd6d293570fd46e1cee90360af1

    部署结果

    SendLib302:0xc58dc2e767918335e6879d5019fde8705899d6d8

    DVN

    owner: 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    构造参数

    参数名称 参数值
    vid 10267 40267%30_000
    _messageLibs ["0xc58dc2e767918335e6879d5019fde8705899d6d8"] SendLib302
    _priceFeed 0x0000000000000000000000000000000000000000
    _signers ["0x4e0539F3e0A941dCD0617F636381b4c3BC25F048"]
    _quorum 1
    _admins ["0x4e0539F3e0A941dCD0617F636381b4c3BC25F048"]

    部署txhash

    https://amoy.polygonscan.com/tx/0xb713ff4115917c3c84ff6a23f5f6f684994130201a14792df0a3270357fdc109

    部署结果

    DVN:0x4e590bb18e7799da84833d55a851a71b68bffd89

    部署结果

    【源链】-Holesky

    Eid: 40217 9d19

    合约名称 合约地址 Owner
    endpoint 0xf37817f975cf3865abd5f7a860ff4968e3ad23c9 newLib :0x8Ce308aB42361C63F114e59CaBbB9393D7DEa41B 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048
    SendLib302 0xe83d86ed3352a4eb4a8098e3f189ebecfb5e0a22 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048
    DVN 0xc58dc2e767918335e6879d5019fde8705899d6d8 signer & admin &owner 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048
    executor proxy: 0x8bfcc2dc3c4f9e48e4e1be2c6b3e4cba2a0a8dcblogic: 0x4e590bb18e7799da84833d55a851a71b68bffd89 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048
    ReceiveLib302 0xb70dd8b342ce6e4a3fd2d070c0b1098ae8f05a5e 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    【目标链】-Amoy

    Eid: 40267 9d4b

    合约名称 合约地址 Owner
    endpoint 0xf37817f975cf3865abd5f7a860ff4968e3ad23c9newLib :0x8Ce308aB42361C63F114e59CaBbB9393D7DEa41B 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048
    ReceiveLib302 0xe83d86ed3352a4eb4a8098e3f189ebecfb5e0a22 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048
    DVN 0x4e590bb18e7799da84833d55a851a71b68bffd89 signer & admin &owner 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048
    executor proxy:logic: 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048
    SendLib302 0xc58dc2e767918335e6879d5019fde8705899d6d8 0x4e0539F3e0A941dCD0617F636381b4c3BC25F048

    三、参数配置

    【源链】-Holesky

    Eid: 40217 9d19

    SendLib302

    1. setDefaultUlnConfigs

    "setDefaultUlnConfigs((uint32,(uint64,uint8,uint8,uint8,address[],address[]))[])": "29460b0b",

    注意:Amoy eid(40267---9d4b)

    Function: setDefaultUlnConfigs((uint32,tuple)[])
    #        Name               Type        Data
    0        _params.eid        uint32      40267
    0        _params.config     tuple       1,1,0,0,0xc58dc2e767918335e6879d5019fde8705899d6d8,
    
    // the formal properties are documented in the setter functions
    struct UlnConfig {
        uint64 confirmations;
        // we store the length of required DVNs and optional DVNs instead of using DVN.length directly to save gas
        uint8 requiredDVNCount; // 0 indicate DEFAULT, NIL_DVN_COUNT indicate NONE (to override the value of default)
        uint8 optionalDVNCount; // 0 indicate DEFAULT, NIL_DVN_COUNT indicate NONE (to override the value of default)
        uint8 optionalDVNThreshold; // (0, optionalDVNCount]
        address[] requiredDVNs; // no duplicates. sorted an an ascending order. allowed overlap with optionalDVNs
        address[] optionalDVNs; // no duplicates. sorted an an ascending order. allowed overlap with requiredDVNs
    }
    
    struct SetDefaultUlnConfigParam {
        uint32 eid;
        UlnConfig config;
    }
    

    参数如下:[[40267,[1,1,0,0,["0xc58dc2e767918335e6879d5019fde8705899d6d8"],[]]]]

    txhash:https://holesky.etherscan.io/tx/0xbe01fb743fb260263c3d55089bd6a5e1e3fee29b05294ffe336fef3b80775ed3

    1. setDefaultExecutorConfigs

    setDefaultExecutorConfigs((uint32,(uint32,address))[])": "c14c4349"

    Function: setDefaultExecutorConfigs((uint32,tuple)[])
    #        Name              Type        Data
    0        _params.eid        uint32     40267
    0        _params.config     tuple      10000,0x8bfcc2dc3c4f9e48e4e1be2c6b3e4cba2a0a8dcb
    
    struct SetDefaultExecutorConfigParam {
        uint32 eid;
        ExecutorConfig config;
    }
    
    struct ExecutorConfig {
        uint32 maxMessageSize;
        address executor;
    }
    

    参数如下:[[40267,[10000,"0x8bfcc2dc3c4f9e48e4e1be2c6b3e4cba2a0a8dcb"]]]

    txhash:https://holesky.etherscan.io/tx/0xa73f6dd6a05a89374a0bf34f9f930791f42fe5493417bdac6ab7ca3870bcd92f

    Endpoint

    1. registerLibrary

    • sendLib302

    https://holesky.etherscan.io/tx/0x1af6cc3430af120bd9c0ed12c6d542a65ba52da142af6e7c3e48cca10321118a

    Function: registerLibrary(address ) ***
    
    MethodID: 0xe8964e81
    [0]:  000000000000000000000000e83d86ed3352a4eb4a8098e3f189ebecfb5e0a22
    
    • receiveLib302

    https://holesky.etherscan.io/tx/0x966905a47d89ee83691d82ab86406c048e7521b8b2e6a00e4910316d5f02b71a

    Function: registerLibrary(address ) ***
    
    MethodID: 0xe8964e81
    [0]:  000000000000000000000000b70dd8b342ce6e4a3fd2d070c0b1098ae8f05a5e
    
    1. setDefaultSendLibrary

    合约上需要针对每一个eid设置一个lib。

    注:_send方法内部会根据目标链id(_params.dstEid)获取sendlib。

    https://holesky.etherscan.io/tx/0x6660383a9ed88b5e3837dac19fd260514d7c81cb0c09fa8836ceffbf8110f512

    Function: setDefaultSendLibrary(uint32 ,address ) ***
    
    MethodID: 0xaafea312
    [0]:  0000000000000000000000000000000000000000000000000000000000009d4b
    [1]:  000000000000000000000000e83d86ed3352a4eb4a8098e3f189ebecfb5e0a22
    
    #        Name             Type          Data
    0        undefined        uint32        40267
    1        undefined        address       0xe83d86Ed3352A4EB4a8098e3F189eBecFb5e0a22
    

    注:第一次设置失败了,原因是内部会调用指定的SendLib302去验证已设置默认的uln。

    https://holesky.etherscan.io/tx/0x6660383a9ed88b5e3837dac19fd260514d7c81cb0c09fa8836ceffbf8110f512

    /// @dev owner setting the defaultSendLibrary
        /// @dev can set to the blockedLibrary, which is a registered library
        /// @dev the msgLib must enable the support before they can be registered to the endpoint as the default
        /// @dev only owner
        function setDefaultSendLibrary(
            uint32 _eid,
            address _newLib
        ) external onlyOwner onlyRegistered(_newLib) isSendLib(_newLib) onlySupportedEid(_newLib, _eid) {
            // must provide a different value
            if (defaultSendLibrary[_eid] == _newLib) revert Errors.LZ_SameValue();
            defaultSendLibrary[_eid] = _newLib;
            emit DefaultSendLibrarySet(_eid, _newLib);
        }
    /// @dev check if the library supported the eid.
        modifier onlySupportedEid(address _lib, uint32 _eid) {
            /// @dev doesnt need to check for default lib, because when they are initially added they get passed through this modifier
            if (_lib != DEFAULT_LIB) {
                if (!IMessageLib(_lib).isSupportedEid(_eid)) revert Errors.LZ_UnsupportedEid();
            }
            _;
        }
    
        ---------------以下为sendlib302的代码----------------
        /// @dev a supported Eid must have a valid default uln config, which has at least one dvn
        function _isSupportedEid(uint32 _remoteEid) internal view returns (bool) {
            UlnConfig storage defaultConfig = ulnConfigs[DEFAULT_CONFIG][_remoteEid];
            return defaultConfig.requiredDVNCount > 0 || defaultConfig.optionalDVNThreshold > 0;
        }
    

    【目标链】-Amoy

    Eid: 40267 9d4b

    ReceiveLib302

    1. setDefaultUlnConfigs

    "setDefaultUlnConfigs((uint32,(uint64,uint8,uint8,uint8,address[],address[]))[])": "29460b0b",

    注意:Holesky eid(40217---9d19)

    Function: setDefaultUlnConfigs((uint32,tuple)[])
    #        Name               Type        Data
    0        _params.eid        uint32      40217
    0        _params.config     tuple       1,1,0,0,0x4e590bb18e7799da84833d55a851a71b68bffd89,
    
    // the formal properties are documented in the setter functions
    struct UlnConfig {
        uint64 confirmations;
        // we store the length of required DVNs and optional DVNs instead of using DVN.length directly to save gas
        uint8 requiredDVNCount; // 0 indicate DEFAULT, NIL_DVN_COUNT indicate NONE (to override the value of default)
        uint8 optionalDVNCount; // 0 indicate DEFAULT, NIL_DVN_COUNT indicate NONE (to override the value of default)
        uint8 optionalDVNThreshold; // (0, optionalDVNCount]
        address[] requiredDVNs; // no duplicates. sorted an an ascending order. allowed overlap with optionalDVNs
        address[] optionalDVNs; // no duplicates. sorted an an ascending order. allowed overlap with requiredDVNs
    }
    
    struct SetDefaultUlnConfigParam {
        uint32 eid;
        UlnConfig config;
    }
    

    参数如下:[[40217,[1,1,0,0,["0x4e590bb18e7799da84833d55a851a71b68bffd89"],[]]]]

    Txhash:https://amoy.polygonscan.com/tx/0x5d3ba9ca1c2d89ca503d0a15eebff3a6c18d2fe0a10eab63e6e78def0a1362be

    Endpoint

    1. registerLibrary

    • receiveLib302

    https://amoy.polygonscan.com/tx/0x690a33fbb8097c575545c85c09f2f76e7850af76cd2965002f4dabe088e18a56

    Function: registerLibrary(address _lib) ***
    
    MethodID: 0xe8964e81
    [0]:  000000000000000000000000e83d86ed3352a4eb4a8098e3f189ebecfb5e0a22
    
    • sendLib302

    https://amoy.polygonscan.com/tx/0x02cd2163d91cc613f3c4c44c7989cc6140b7b4fe438229ee530d029d397ad71d

    Function: registerLibrary(address _lib) ***
    
    MethodID: 0xe8964e81
    [0]:  000000000000000000000000c58dc2e767918335e6879d5019fde8705899d6d8
    
    1. setDefaultReceiveLibrary

    Commitverification 会用到 endpiont的verfiy方法,里面会验证isValidReceiveLibrary(_receiver, _origin.srcEid, msg.sender)

    https://amoy.polygonscan.com/tx/0xed94d229024e803d5298e4c7184db8356da50b3c80c08c0fd923dfe315491f9a

    Function: setDefaultReceiveLibrary(uint32 _eid,address _newLib,uint256 _gracePeriod) ***
    
    MethodID: 0xa718531b
    [0]:  0000000000000000000000000000000000000000000000000000000000009d19
    [1]:  000000000000000000000000e83d86ed3352a4eb4a8098e3f189ebecfb5e0a22
    [2]:  0000000000000000000000000000000000000000000000000000000000000000
    

    四、跨链调用(Holesky 到 Amoy)

    注:前面的部署配置是单向的,所以测试也是单向的,后续补充双向。

    本次测试Holesky 到 Amoy

    应用合约部署

    合约代码如下:

      // SPDX-License-Identifier: MIT
    
    pragma solidity ^0.8.22;
    
    import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
    import { OApp, MessagingFee, Origin } from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/OApp.sol";
    import { MessagingReceipt } from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/OAppSender.sol";
    
    contract MyOApp is OApp {
        constructor(address _endpoint, address _delegate) OApp(_endpoint, _delegate) Ownable(_delegate) {}
    
        string public data = "Nothing received yet.";
    
        /**
         * @notice Sends a message from the source chain to a destination chain.
         * @param _dstEid The endpoint ID of the destination chain.
         * @param _message The message string to be sent.
         * @param _options Additional options for message execution.
         * @dev Encodes the message as bytes and sends it using the `_lzSend` internal function.
         * @return receipt A `MessagingReceipt` struct containing details of the message sent.
         */
        function send(
            uint32 _dstEid,
            string memory _message,
            bytes calldata _options
        ) external payable returns (MessagingReceipt memory receipt) {
            bytes memory _payload = abi.encode(_message);
            receipt = _lzSend(_dstEid, _payload, _options, MessagingFee(msg.value, 0), payable(msg.sender));
        }
    
        /**
         * @notice Quotes the gas needed to pay for the full omnichain transaction in native gas or ZRO token.
         * @param _dstEid Destination chain's endpoint ID.
         * @param _message The message.
         * @param _options Message execution options (e.g., for sending gas to destination).
         * @param _payInLzToken Whether to return fee in ZRO token.
         * @return fee A `MessagingFee` struct containing the calculated gas fee in either the native token or ZRO token.
         */
        function quote(
            uint32 _dstEid,
            string memory _message,
            bytes memory _options,
            bool _payInLzToken
        ) public view returns (MessagingFee memory fee) {
            bytes memory payload = abi.encode(_message);
            fee = _quote(_dstEid, payload, _options, _payInLzToken);
        }
    
        /**
         * @dev Internal function override to handle incoming messages from another chain.
         * @dev _origin A struct containing information about the message sender.
         * @dev _guid A unique global packet identifier for the message.
         * @param payload The encoded message payload being received.
         *
         * @dev The following params are unused in the current implementation of the OApp.
         * @dev _executor The address of the Executor responsible for processing the message.
         * @dev _extraData Arbitrary data appended by the Executor to the message.
         *
         * Decodes the received payload and processes it as per the business logic defined in the function.
         */
        function _lzReceive(
            Origin calldata /*_origin*/,
            bytes32 /*_guid*/,
            bytes calldata payload,
            address /*_executor*/,
            bytes calldata /*_extraData*/
        ) internal override {
            data = abi.decode(payload, (string));
        }
    }
    

    【源链】Holesky

    https://holesky.etherscan.io/tx/0xa61a3cf8aca80af6870b1852ee5e6d7aa022ba979daa666d8b9eb2d561bbe245

    _ENDPOINT:0xf37817f975cf3865abd5f7a860ff4968e3ad23c9
    _DELEGATE:0xAa1a88aa89F50ee9B7e3F6124f18a31d5E6dB1F9
    

    合约地址0xedeabd37bbbf901750dd31b84d67d571fc303d36

    【目标链】Amoy

    https://amoy.polygonscan.com/tx/0x54af7a08601c3dd198a5a02ddbf230458079b42f85b26bee1761085afca19354

    _ENDPOINT:0xf37817f975cf3865abd5f7a860ff4968e3ad23c9
    _DELEGATE:0xAa1a88aa89F50ee9B7e3F6124f18a31d5E6dB1F9
    

    合约地址0xee037dc8dbec28a70db6bafc9ea33824d61836b1

    应用参数配置

    【源链】Holesky

    setPeers

    设置参数如下:

    目标链Id:40267 目标应用合约地址:0xee037dc8dbec28a70db6bafc9ea33824d61836b1

    _DELEGATE:0xAa1a88aa89F50ee9B7e3F6124f18a31d5E6dB1F9

    注意:_DELEGATE才可以调用

    myOApp eid (目标链) peer(目标应用合约地址) Tx hash
    0xedeabd37bbbf901750dd31b84d67d571fc303d36 40267 0x000000000000000000000000ee037dc8dbec28a70db6bafc9ea33824d61836b1 https://holesky.etherscan.io/tx/0x149100eac1e92195b58ef5e8c3071303defc6e044290ac2c6e937666dd590b33
    Function: setPeer(uint32 eid,bytes32 peer) ***
    
    MethodID: 0x3400288b
    [0]:  0000000000000000000000000000000000000000000000000000000000009d4b
    [1]:  000000000000000000000000ee037dc8dbec28a70db6bafc9ea33824d61836b1
    
    #     Name    Type     Data
    0     eid     uint32   40267
    1     peer    bytes32  0x000000000000000000000000ee037dc8dbec28a70db6bafc9ea33824d61836b1
    

    【目标链】Amoy

    setPeers

    设置参数如下:

    源链Id:40217 源链应用合约地址:0xedeabd37bbbf901750dd31b84d67d571fc303d36

    _DELEGATE:0xAa1a88aa89F50ee9B7e3F6124f18a31d5E6dB1F9

    注意:_DELEGATE才可以调用

    myOApp eid****(源链的) peer(源链的应用合约地址) Tx hash
    0xee037dc8dbec28a70db6bafc9ea33824d61836b1 40217 0x000000000000000000000000EDEABD37BBBF901750DD31B84D67D571FC303D36 https://amoy.polygonscan.com/tx/0x643649fa4570bdac892f6bbb2639c98b1ae0cb05975be11fb1ec4ddcc48892a8

    应用跨链测试

    【源链】发起-myOApp

    Generating Options:https://docs.layerzero.network/v2/developers/evm/gas-settings/options#generating-options

    方法参数

    function send(uint32 _dstEid, string memory _message, bytes calldata _options )

    注意:v2里_options参数必须填写,否则无法发起交易。

    Contract Address _dstEid _message _options
    0xedeabd37bbbf901750dd31b84d67d571fc303d36 40267 zxl20240722 0x0003010011010000000000000000000000000000ea60 _gas: 60000 _value:0

    交易结果

    源链Hash:

    https://holesky.etherscan.io/tx/0x18aff9cd6ccaf7a5ef3299237c6689697a560960610f410a559978050dbdfe4e

    PacketSent 事件内容:

    
    PacketSent (bytes encodedPayload, bytes options, address sendLibrary)
    
    encodedPayload :01000000000000000100009D19000000000000000000000000EDEABD37BBBF901750DD31B84D67D571FC303D3600009D4B000000000000000000000000EE037DC8DBEC28A70DB6BAFC9EA33824D61836B1C5D70FA727CDF89680318706077752F4E380A735D99492746965B5256F5403D70000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000B7A786C3230323430373232000000000000000000000000000000000000000000
    options :0003010011010000000000000000000000000000EA60
    sendLibrary :0xe83d86Ed3352A4EB4a8098e3F189eBecFb5e0a22
    

    encodedPayload 解析如下:

    encodedPacket = abi.encodePacked(packetHeader, payload);

    packetHeader:01000000000000000100009D19000000000000000000000000EDEABD37BBBF901750DD31B84D67D571FC303D3600009D4B000000000000000000000000EE037DC8DBEC28A70DB6BAFC9EA33824D61836B1

    payload:

    C5D70FA727CDF89680318706077752F4E380A735D99492746965B5256F5403D70000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000B7A786C3230323430373232000000000000000000000000000000000000000000

    分类 参数值 参数名
    packetHeader 01 PACKET_VERSION 1
    0000000000000001 nonce 1
    00009D19 srcEid 40217
    000000000000000000000000EDEABD37BBBF901750DD31B84D67D571FC303D36 sender
    00009D4B dstEid 40267
    000000000000000000000000EE037DC8DBEC28A70DB6BAFC9EA33824D61836B1 receiver
    payload C5D70FA727CDF89680318706077752F4E380A735D99492746965B5256F5403D7 guid
    0000000000000000000000000000000000000000000000000000000000000020 000000000000000000000000000000000000000000000000000000000000000B 7A786C3230323430373232000000000000000000000000000000000000000000 message

    【目标链】提交签名-DVN -> Receive302

    DVN的admin调用DVN合约的execute方法进行signer的签名的验证以及将交易的确认提交至receivelib302.

    DVN合约地址:0x4e590bb18e7799da84833d55a851a71b68bffd89

    DVN合约中方法execute的参数如下:

    Name Type Data 备注
    params.vid uint32 10267 部署dvn时设置的
    _params.target address 0xe83d86ed3352a4eb4a8098e3f189ebecfb5e0a22 receiveLib302合约地址
    _params.callData bytes 0x0223536e0000000000000000000000000000000000000000000000000000000000000060a292800fdc6a51b7fc3b31905f7813d865aae060014a6821ce5166bcfd9682880000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000005101000000000000000100009d19000000000000000000000000edeabd37bbbf901750dd31b84d67d571fc303d3600009d4b000000000000000000000000ee037dc8dbec28a70db6bafc9ea33824d61836b1000000000000000000000000000000 receiveLib302的方法verify签名数据
    _params.expiration uint256 1721791521 1天有效期 2024-07-24 11:25:21
    _params.signatures bytes 0x3e40e1728a32748ae1bd2ba5adf64445e426cefba22ec9c0c0d4bf17a1d5b1e96171cb4d0ff731c82f36b21f9352a4fab0db12681d7bb5ae830efd48a3fa94a71b 对dvn合约中execute方法的关键参加的消息摘要的签名。_messageHash = keccak256(abi.encodePacked(vid, target, expiration, callData));_messageDigest= keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", _messageHash));

    方法参数

    function execute(ExecuteParam[] calldata _params) external onlyRole(ADMIN_ROLE)

    [[10267,"0xe83d86ed3352a4eb4a8098e3f189ebecfb5e0a22","0x0223536e0000000000000000000000000000000000000000000000000000000000000060a292800fdc6a51b7fc3b31905f7813d865aae060014a6821ce5166bcfd9682880000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000005101000000000000000100009d19000000000000000000000000edeabd37bbbf901750dd31b84d67d571fc303d3600009d4b000000000000000000000000ee037dc8dbec28a70db6bafc9ea33824d61836b1000000000000000000000000000000",1721791521,"0x3e40e1728a32748ae1bd2ba5adf64445e426cefba22ec9c0c0d4bf17a1d5b1e96171cb4d0ff731c82f36b21f9352a4fab0db12681d7bb5ae830efd48a3fa94a71b"]]

    交易结果

    Txhash:

    https://amoy.polygonscan.com/tx/0xa2c21e65b628df518425ca204b98472c36d1404c27dbb1b90490904a8aead5cc

    Transaction Receipt Event Logs :

    Address        0xe83d86ed3352a4eb4a8098e3f189ebecfb5e0a22   
    Name     PayloadVerified (address dvn, bytes header, uint256 confirmations, bytes32 proofHash)
    
    Topics
    0 0x2cb0eed7538baeae4c6fde038c0fd0384d27de0dd55a228c65847bda6aa1ab56
    
    Data
    
    dvn :0x4E590Bb18E7799DA84833D55a851A71B68bFFd89
    header :01000000000000000100009D19000000000000000000000000EDEABD37BBBF901750DD31B84D67D571FC303D3600009D4B000000000000000000000000EE037DC8DBEC28A70DB6BAFC9EA33824D61836B1
    confirmations :1
    proofHash :A292800FDC6A51B7FC3B31905F7813D865AAE060014A6821CE5166BCFD968288
    

    【目标链】执行跨链-ReceiveLib302&Endpoint

    Executor :

    [1,1,0,0,["0x4E590Bb18E7799DA84833D55a851A71B68bFFd89"],[]]

    【ReceiveLib302】commitVerification

    receivelib302合约地址: 0xe83d86ed3352a4eb4a8098e3f189ebecfb5e0a22

    方法参数
    function commitVerification(bytes calldata _packetHeader, bytes32 _payloadHash) external
    
    _packetHeader: 0x01000000000000000100009D19000000000000000000000000EDEABD37BBBF901750DD31B84D67D571FC303D3600009D4B000000000000000000000000EE037DC8DBEC28A70DB6BAFC9EA33824D61836B1
    
    _payloadHash:0xa292800fdc6a51b7fc3b31905f7813d865aae060014a6821ce5166bcfd968288
    

    _packetHeaderHash: 0xf3fec8a71c20e085141ee36d7af1481931cfb88472aec85d64eb43fcf1f6a415

    交易结果

    txhash:https://amoy.polygonscan.com/tx/0xd491bbfc3e3e1cb469cb3841851d3abd20ee4c4124e70dd3136f6403123d8810

    Address     0xf37817f975cf3865abd5f7a860ff4968e3ad23c9   
    Name        PacketVerified (tuple origin, address receiver, bytes32 payloadHash)View Source 
    
    Topics
    0 0x0d87345f3d1c929caba93e1c3821b54ff3512e12b66aa3cfe54b6bcbc17e59b4
    
    Data
    0x0000000000000000000000000000000000000000000000000000000000009d19000000000000000000000000edeabd37bbbf901750dd31b84d67d571fc303d360000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ee037dc8dbec28a70db6bafc9ea33824d61836b1a292800fdc6a51b7fc3b31905f7813d865aae060014a6821ce5166bcfd968288
    

    【Endpoint】lzReceive

    endpoint合约地址:0xf37817f975cf3865abd5f7a860ff4968e3ad23c9

    方法参数
    function lzReceive(Origin calldata _origin,address _receiver,bytes32 _guid,bytes calldata _message,bytes calldata _extraData
    )
    
    struct Origin {
        uint32 srcEid;
        bytes32 sender;
        uint64 nonce;
    }
    
    ----参数如下
    
    _origin:[40217,"0x000000000000000000000000EDEABD37BBBF901750DD31B84D67D571FC303D36",1]
    
    _receiver: 0xEE037DC8DBEC28A70DB6BAFC9EA33824D61836B1
    _guid: 0xC5D70FA727CDF89680318706077752F4E380A735D99492746965B5256F5403D7
    _message:0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000B7A786C3230323430373232000000000000000000000000000000000000000000
    _extraData:0x
    
    交易结果

    Txhash:

    https://amoy.polygonscan.com/tx/0xde9ee7d617ebd75a0a29c90da189b9fd108ab9ee8619615ba8c36bdd46c3a613

    Address   0xf37817f975cf3865abd5f7a860ff4968e3ad23c9   
    Name    PacketDelivered (tuple origin, address receiver)View Source 
    
    Topics
    0 0x3cd5e48f9730b129dc7550f0fcea9c767b7be37837cd10e55eb35f734f4bca04
    Data
    0x0000000000000000000000000000000000000000000000000000000000009d19000000000000000000000000edeabd37bbbf901750dd31b84d67d571fc303d360000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ee037dc8dbec28a70db6bafc9ea33824d61836b1
    

    【目标链】查询结果-myOApp

    image.png

    顺利结束!!!

    帮助类库

    1. 计算message哈希以及摘要
    // SPDX-License-Identifier: GPL-3.0
    
    pragma solidity >=0.8.2 <0.9.0;
    
    /**
     * @title Storage
     * @dev Store & retrieve value in a variable
     * @custom:dev-run-script ./scripts/deploy_with_ethers.ts
     */
    contract  MyLayerzeroHelper {
    
       // 计算hash以及验证摘要
        function calcHash( uint32 vid, address target, bytes memory callData, uint256 expiration) public pure  returns (bytes32,bytes32){
            bytes32 hash =  keccak256(abi.encodePacked(vid, target, expiration, callData));
    
            bytes32 messageDigest = _getEthSignedMessageHash(hash);
            return (hash, messageDigest);
        }
    
         function _getEthSignedMessageHash(bytes32 _messageHash) internal pure returns (bytes32) {
            return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", _messageHash));
        }
    
        function getMessage(string memory _message) public pure  returns (bytes memory){
            bytes memory _payload = abi.encode(_message);
            return _payload;
        }
    
        function getPayloadHash(bytes32 _guid,bytes calldata _message) public  pure returns(bytes32){
           return keccak256(  abi.encodePacked(_guid,_message));
        }
    
        function calcPacketHeaderHash(bytes calldata _packetHeader) public  pure  returns(bytes32){
            return keccak256(_packetHeader);
        }
    
         function generateGUID(
            uint64 _nonce,
            uint32 _srcEid,
            address _sender,
            uint32 _dstEid,
            bytes32 _receiver
        ) public  pure returns (bytes32) {
            return keccak256(abi.encodePacked(_nonce, _srcEid, toBytes32(_sender), _dstEid, _receiver));
        }
    
         function toBytes32(address _address) internal pure returns (bytes32 result) {
            result = bytes32(uint256(uint160(_address)));
        }
    
    struct ExecuteParam {
        uint32 vid;
        address target;
        bytes callData;
        uint256 expiration;
        bytes signatures;
    }
    }
    

    摘要的签名

    /**
     * 以太坊签名  dvn
     */
    async function dvnSign(){
        var digest= "0x5200d16eaf756f7cff0647dde5a967a7193bdb9a4ef035c046140ac6bc31e0fc"
        const signer = new ethers.utils.SigningKey(privateKey);
        var signature = signer.signDigest(digest);
    
        console.log("signature: ", signature);
    
        console.log("signature hex string: ", signature.r + signature.s.substring(2) + signature.v.toString(16));
    }
    

    主要问题

    1. 合约部署问题

    当前使用的是main分支的代码, remix编译时注意版本号(@openzeppelin/contracts@4.9.5/utils/cryptography/ECDSA.sol")。 注:tag1.0有问题,后续看看。

    1. 合约参数设置问题

    合约之间有依赖关系,所以初始化时一定注意顺序。

    1. dvn合约里的签名 注意解析规则,另外,可以不部署dvn合约,直接调用receivelib302进行跨链交易的验证。

    2. 无法执行commitVerification方法

    目标链需要执行setpeers方法。 业务内部会调用

    function _initializable(
            Origin calldata _origin,
            address _receiver,
            uint64 _lazyInboundNonce
        ) internal view returns (bool) {
            return
                _lazyInboundNonce > 0 || // allowInitializePath already checked
                ILayerZeroReceiver(_receiver).allowInitializePath(_origin);
        }
    

    相关文章

      网友评论

          本文标题:私有化LayerZero Protocol测试示例

          本文链接:https://www.haomeiwen.com/subject/elluljtx.html