Build the WeCross cross-chain platform and write cross-chain smart contracts

WeCross is a blockchain cross-chain collaboration platform independently developed by WeBank and fully open source. It supports multi-dimensional cross-chain interactions such as application and multi-chain interoperability, and interoperability between homogeneous/heterogeneous chains. ——From "WeCross Technical Documentation "

This tutorial is based on wecross-demo, taking the implementation of multi-group cross-chain as an example:

Cross-chain example: Use group1 as the source chain and group2 as the target chain. A smart contract is deployed on group2, which includes the global variable data and the function setData(_data) that modifies the global variable data. During the cross-chain process, group1 initiates a cross-chain request to pass the function parameter data. The specified target function is setData corresponding to the smart contract on group2, and attempts to modify the global variable data on group2 to implement cross-chain contract calls.

0. Environment configuration

Free Linux 18.04

MySQL

Java 11.0.18

1. Install wecross-demo

Download script:

  • cd ~
  • bash <(curl -sL https://gitee.com/WeBank/WeCross/raw/master/scripts/download_demo.sh)

 Execute the script:

  • cd ~/wecross-demo
  • bash clear.sh
  • bash build_cross_groups.sh

During execution, you need to enter the IP, port, username and password of the MySQL database: 

 After the download is complete, enter y to open the console:

 Enter login to enter the administrator console:

 2. Write cross-chain contracts

Cross-chain contracts need to contain three functions:

  1. Function to initiate cross-chain transactions in the source chain
  2. Functions in the target chain that handle cross-chain transactions
  3. The callback function in the source chain that receives the cross-chain transaction execution results

Contract source code path: ~/wecross-demo/WeCross-Console/conf/contracts/solidity/SetDataInterchain.sol

As follows:

pragma solidity ^0.5.0;

pragma experimental ABIEncoderV2;
import "./WeCrossHub.sol";

contract SetDataInterchain {
    // 声明WeCrossHub
    WeCrossHub hub;
    function init(address _hub) public {
        hub = WeCrossHub(_hub);
    }

    // 全局变量data
    string data;
    constructor() public {}
    function setData(string memory _data) public {
        data = _data;
    }
    function getData() public returns (string memory) {
        return data;
    }

    // 1.源链中发起跨链交易的函数
    function setDataInterchainInvoke(
        string memory _path,
        string memory _method,
        string memory _data,
        string memory _callbackPath,
        string memory _callbackMethod
    ) public returns (string memory) {
        // 将待跨链传递的参数data存储在args中
        string[] memory args = new string[](1);
        args[0] = _data;

        return
            hub.interchainInvoke(
                _path,
                _method,
                args,
                _callbackPath,
                _callbackMethod
            ); // WeCrossHub将args进行跨链传递
    }

    // 2.目标链中处理跨链交易的函数
    function setDataFromInterchain(string[] memory args)
        public
        returns (string[] memory)
    {
        // 目标链收到args,然后执行setData
        setData(args[0]);
        return args;
    }

    // 3.源链中接收跨链交易执行结果的回调函数
    function setDataInterchainCallback(bool state, string[] memory _result)
        public
        returns (string[] memory)
    {
        // 源链根据回调的结果_result,可选的进行后续操作
        if (state) {}
        return _result;
    }
}

3. Deploy cross-chain contracts

3.1-3.4 shows the four steps of deploying a cross-chain contract on group1, and 3.5 shows the full process of the four steps on group2.

3.1 Deploy single-chain contract

Excuting an order: 

  • bcosDeploy payment.group1.SetDataInterchain conf/contracts/solidity/SetDataInterchain.sol SetDataInterchain 1.0

Save contract address: 0xdd46661c0f32003a644f079395010c632da626e2

 Test the functionality of the function:

  • sendTransaction payment.group1.SetDataInterchain setData HelloWorld
  • call payment.group1.SetDataInterchain getData

3.2 Register single-chain contract as a cross-chain resource

  • bcosRegister payment.group1.SetDataInterchain conf/contracts/solidity/SetDataInterchain.sol 0xdd46661c0f32003a644f079395010c632da626e2 SetDataInterchain 2.0

 Note: 0xdd46661c0f32003a644f079395010c632da626e2 is the contract address on the single chain. The version number 2.0 must be distinguished from the 1.0 version number just deployed on the single chain.

3.3 Deploy WeCrossHub

WeCrossHub is deployed on each chain to communicate with other chains.

  • bcosDeploy payment.group1.WeCrossHub conf/contracts/solidity/WeCrossHub.sol WeCrossHub 2.0

Save the address of WeCrossHub: 0x3a5b4196646c1b7ec6cba22258986f4097903196

 3.4 Binding cross-chain resources to WeCrossHub

Execute the init initialization function in the SetDataInterchain contract and enter the address of WeCrossHub:

  • sendTransaction payment.group1.SetDataInterchain init "0x3a5b4196646c1b7ec6cba22258986f4097903196"

Realize the binding between SetDataInterchain and WeCrossHub, so the contract SetDataInterchain has the ability to communicate with the outside world across the chain.

 3.5 All processes deployed on group2

  • bcosDeploy payment.group2.SetDataInterchain conf/contracts/solidity/SetDataInterchain.sol SetDataInterchain 1.0
  • bcosRegister payment.group2.SetDataInterchain conf/contracts/solidity/SetDataInterchain.sol 0x87f3bb38533280a278d7a3e8558bc787d10addf5 SetDataInterchain 2.0

  • bcosDeploy payment.group1.WeCrossHub conf/contracts/solidity/WeCrossHub.sol WeCrossHub 2.0
  • sendTransaction payment.group2.SetDataInterchain init "0x150d4d7cacf6a36b860c5777819eb58279e633f3" 

4. Cross-chain contract call

 4.1 The source chain initiates a cross-chain call

  • sendTransaction payment.group1.SetDataInterchain setDataInterchainInvoke payment.group2.SetDataInterchain setDataFromInterchain NewHelloWorld payment.group1.SetDataInterchain setDataInterchainCallback

According to sendTransaction, this command is essentially just a contract call transaction on the source chain. The second parameter payment.group1.SetDataInterchain is the contract name, the third parameter setDataInterchainInvoke is the contract function name, and the following parameters are the function list of setDataInterchainInvoke, which respectively correspond to the contract name on the target chain, the function name on the contract, and the function Parameters, the on-chain contract where the callback function is located, the callback function method name, and then call WeCrossHub to make a cross-chain call:

function setDataInterchainInvoke(
        string memory _path,
        string memory _method,
        string memory _data,
        string memory _callbackPath,
        string memory _callbackMethod
    ) public returns (string memory) {
        string[] memory args = new string[](1);
        args[0] = _data;

        return
            hub.interchainInvoke(
                _path,
                _method,
                args,
                _callbackPath,
                _callbackMethod
            );
    }

4.2 View the cross-chain call results on the target chain

The target chain group2 does not actively call the setData function. Only the cross-chain request from the source chain group1 calls the setData function on the target chain group2 and passes the NewHelloWorld parameter. You can view the cross-chain call results:

  • call payment.group2.SetDataInterchain getData

This means that the cross-chain contract call was successful!

Guess you like

Origin blog.csdn.net/yilongyoung/article/details/129992489