跳转至

跨链交易提交

跨链交易提交代码示例

参考跨链合约中对submitCrossProxyTran的介绍,用户(或cross-server跨链服务后台)首先向RepChain提交跨链交易请求

@Test
@Order(11)
void testRegisterFabric2SubmitCross() throws Exception {

    List<String> content = new ArrayList<>();
    content.add("asset1");
    content.add("blue");
    content.add("20");
    content.add("Tomoko");
    content.add("400");
    // 提交交易
    String tranId = IdUtil.randomUUID();
    JSONObject jsonObject = new JSONObject();
    jsonObject
            .fluentPut("chainHash", "74e8089786e54f859112d20666719ac7")
            .fluentPut("routeHash", DigestUtils.sha256Hex("ContractAssetsTPL-to-basic"))
            .fluentPut("accountId", "appUser")
                // fabrci合约方法UpdateAsset
            .fluentPut("methodName", "UpdateAsset")
                // fabric合约方法UpdateAsset的参数
            .fluentPut("methodArgs", content.stream().collect(Collectors.joining(",")));
    Peer.Transaction tran = tranCreator.createInvokeTran(tranId, certId, crossManageId, RepConstant.SUBMIT_CROSS_PROXYTRAN, jsonObject.toJSONString(), 0, "");
    String tranHex = Hex.encodeHexString(tran.toByteArray());
    JSONObject res = tranPostClient.postSignedTran(tranHex);
    System.out.println(res);
    assertThat(res.getString("txid")).isEqualTo(tran.getId());
}

@Test
@Order(12)
void testRegisterFiscoSubmitCross() throws Exception {

    String content = "0x4ed3885e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000028636f6e74656e742d3266613130326336626537383462663838653836613734333961636265313062000000000000000000000000000000000000000000000000";
    // 提交交易
    String tranId = IdUtil.randomUUID();
    JSONObject jsonObject = new JSONObject();
    jsonObject
            .fluentPut("chainHash", "74e8089786e54f859112d20666719ac6")
            .fluentPut("routeHash", DigestUtils.sha256Hex("ContractAssetsTPL-to-HelloWorld"))
            .fluentPut("accountId", "0x71440f40691d8b2e2c6e1cce7685f7e4000cfcd1")
                // fisco-bcos的合约方法set
            .fluentPut("methodName", "set")
                // isco-bcos合约方法set的参数
            .fluentPut("methodArgs", content);
    Peer.Transaction tran = tranCreator.createInvokeTran(tranId, certId, crossManageId, RepConstant.SUBMIT_CROSS_PROXYTRAN, jsonObject.toJSONString(), 0, "");
    String tranHex = Hex.encodeHexString(tran.toByteArray());
    JSONObject res = tranPostClient.postSignedTran(tranHex);
    assertThat(res.getString("txid")).isEqualTo(tran.getId());
}

@Test
@Order(14)
void testRegisterRepChainSubmitCross() throws Exception {

    // 提交交易
    String tranId = IdUtil.randomUUID();
    JSONObject jsonObject = new JSONObject();
    jsonObject
            .fluentPut("chainHash", "79e5d00cc30f40d3a857ec4511c4fa4e")
            .fluentPut("routeHash", DigestUtils.sha256Hex("ContractAssetsTPL-to-HelloWorld"))
            .fluentPut("accountId", "identity-net:121000005l35120456.node1")
            // RepChain合约方法transfer
            .fluentPut("methodName", "transfer")
            // RepChain合约方法transfer的参数
            .fluentPut("methodArgs",
                            "{\n" +
                            "    \"from\" : \"identity-net:121000005l35120456\",\n" +
                            "    \"to\" : \"identity-net:12110107bi45jh675g\",\n" +
                            "    \"amount\" : 5\n" +
                            "}"
            );
    Peer.Transaction tran = tranCreator.createInvokeTran(tranId, certId, crossManageId, RepConstant.SUBMIT_CROSS_PROXYTRAN, jsonObject.toJSONString(), 0, "");
    String tranHex = Hex.encodeHexString(tran.toByteArray());
    JSONObject res = tranPostClient.postSignedTran(tranHex);
    assertThat(res.getString("txid")).isEqualTo(tran.getId());
}

注意

fisco-bcos的参数比较特殊,需要经过编码,因此实际使用过程可参考如下:

1.以fisco/console-1.2.0为例,首先将solity合约转为java合约,命令如下:

# 脚本sol2java.sh在fisco的终端包下fisco/console-1.2.0/sol2java.sh
./sol2java.sh contracts/solidity/HelloWorld.sol 

2.编辑生成的java文件

vim contracts/sdk/java/contracts/solidity/HelloWorld/sol/HelloWorld.java

3.摘取所需要的方法

public void set(String n, TransactionSucCallback callback) {
    final Function function = new Function(
            FUNC_SET,
            Arrays.<Type>asList(new org.fisco.bcos.web3j.abi.datatypes.Utf8String(n)),
            Collections.<TypeReference<?>>emptyList());
    asyncExecuteTransaction(function, callback);
}

4.编码我们要构造的交易的参数

package rep.cross.contract;

import java.util.Arrays;
import java.util.Collections;

import cn.hutool.core.util.IdUtil;
import org.fisco.bcos.web3j.abi.FunctionEncoder;
import org.fisco.bcos.web3j.abi.TypeReference;
import org.fisco.bcos.web3j.abi.datatypes.Function;
import org.fisco.bcos.web3j.abi.datatypes.Type;
import org.fisco.bcos.web3j.abi.datatypes.Utf8String;

/**
 * @author zyf
 */
public class EncodeFunction {

  public static void main(String[] args) {
    // 编码参数
    String content = "content-" + IdUtil.simpleUUID();
    final Function function = new Function(
            "set",
            Arrays.<Type>asList(new Utf8String(content)),
            Collections.<TypeReference<?>>emptyList());

    System.out.println(FunctionEncoder.encode(function));
  }
}

5.输出的参数补充到methodArgs:

@Test
@Order(12)
void testRegisterFiscoSubmitCross() throws Exception {
    // 第4步中编码输出的字符串
    String content = "0x4ed3885e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000028636f6e74656e742d3266613130326336626537383462663838653836613734333961636265313062000000000000000000000000000000000000000000000000";
    // 提交交易
    String tranId = IdUtil.randomUUID();
    JSONObject jsonObject = new JSONObject();
    jsonObject
            .fluentPut("chainHash", "74e8089786e54f859112d20666719ac6")
            .fluentPut("routeHash", DigestUtils.sha256Hex("ContractAssetsTPL-to-HelloWorld"))
            .fluentPut("accountId", "0x71440f40691d8b2e2c6e1cce7685f7e4000cfcd1")
                  // fisco-bcos的合约方法set
            .fluentPut("methodName", "set")
                  // isco-bcos合约方法set的参数
            .fluentPut("methodArgs", content);
    Peer.Transaction tran = tranCreator.createInvokeTran(tranId, certId, crossManageId, RepConstant.SUBMIT_CROSS_PROXYTRAN, jsonObject.toJSONString(), 0, "");
    String tranHex = Hex.encodeHexString(tran.toByteArray());
    JSONObject res = tranPostClient.postSignedTran(tranHex);
    assertThat(res.getString("txid")).isEqualTo(tran.getId());
}
回到页面顶部