zoukankan      html  css  js  c++  java
  • 第八章 FISCO BCOS 国密版本的部署、控制台搭建、合约的部署、sdk 调用

    鉴于笔者以前各大博客教程都有很多人提问,早期建立一个技术交流群,里面技术体系可能比较杂,想了解相关区块链开发,技术提问,请加QQ群:538327407

    参考资料

    证书说明:https://mp.weixin.qq.com/s/nRwHxt2E1TJiopJqLSvMOQ

    参考资料:https://blog.csdn.net/FISCO_BCOS/article/details/95496272 (国密版本部署,很不错)


    准备工作

    1、机构A ,作为创世区块的服务器

    148.70.170.96 (公)

    172.27.16.14 (内)

    2、机构B,联盟链机构

    148.70.71.251 (公)

    172.27.16.17 (内)


    一、下载安装 企业版部署工具

    1、下载

    cd ~/ && git clone https://github.com/FISCO-BCOS/generator.git

    2、安装

    此操作要求用户具有sudo权限。

    cd generator && bash ./scripts/install.sh

    3、检查是否安装成功,若成功,输出 usage: generator xxx

    ./generator -h

    查看版本

    ./generator -v

    > INFO | v1.0.0

    4、拉取节点二进制

    拉取最新fisco-bcos二进制文件到meta中 这里要修改下载国密版本(源程序)

    ./generator --download_fisco ./meta -g

    -g参数表示国密,去掉-g则下载的是普通版FISCO BCOS,执行完该命令,国密版FISCO BCOS会下载到generator/meta/下。

    5、检查二进制版本

    若成功,输出 FISCO-BCOS Version : x.x.x-x

    ./meta/fisco-bcos -v

    显示国密版本



    正常操作流程

    1、创建链证书

    FISCO BCOS国密版的链证书和机构证书需要两套,分别为国密证书和普通版证书。

    证书支持自建、CA公司申请,企业级部署工具底层使用`openssl`工具自建生成,如果要用CA公司申请的证书,可直接把证书文件的名称改为规定的名称即可。

    链证书普通版`ca.crt`,对应的私钥`ca.key`。链证书国密版`gmca.crt`,对应的私钥`gmca.key`。

    机构证书普通版`agency.crt`,对应的私钥`agency.key`,机构证书国密版`gmagency.crt`,对应的私钥`gmagency.key`

    ./generator --generate_chain_certificate ./dir_chain_ca

    使用FISCO BCOS企业级部署工具时,所有需要证书的操作都需要将链证书和机构证书(.crt文件)放置在`generator/meta/`下。

    cd generator 

    #创建一个专用于放置证书的目录(该目录位置及名称均可自定义,只要保证后续使用企业级部署工具需要提供证书目录时,目录路径正确即可)


    #在generator/dir_agency_ca下生成普通版链证书:

    ./generator --generate_chain_certificate ./dir_chain_ca/chain

     

    #在generator/dir_agency_ca/chain-g下生成普通版链证书:

    ./generator --generate_chain_certificate ./dir_chain_ca/chain-g -g

     


    #-g参数表示国密

    #按照企业级部署工具的使用要求将证书放在generator/meta/目录下

    cp ./dir_chain_ca/chain-g/gmca.crt ./meta/gmca.crt 
    
    cp ./dir_chain_ca/chain/ca.crt ./meta/ca.crt

    2、机构向联盟委员会申请机构证书

    生成国密和普通的版本的机构证书

    机构A

    ./generator --generate_agency_certificate ./dir_agency_ca ./dir_chain_ca/chain agencyA
    
    ./generator --generate_agency_certificate ./dir_g_agency_ca ./dir_chain_ca/chain-g agencyA -g

    机构B(前提:将机构A中生成dir_chain_ca 目录 copy 到机构B的目录中)

    ./generator --generate_agency_certificate ./dir_agency_ca ./dir_chain_ca/chain agencyB
    
    ./generator --generate_agency_certificate ./dir_g_agency_ca ./dir_chain_ca/chain-g agencyB -g

     


    #按照企业级部署工具的使用要求将证书放在generator/meta/目录

    采用手动copy 的模式

    其他机构也同时如此操作

    并且将dir_chain_ca 下面的链证书 copy 给其他机构

    普通版本:ca.crt、agency.crt、agency.key

    国密版本:gmagency.crt、gmagency.key

    如果只是为了测试验证,可以直接将dir_chain_ca 目录copy 给其他机构


    3、创世区块所处的机构,修改配置文件 node_deployment.ini

    生成并发节点证书

    机构A

    ./generator --generate_all_certificates ./agencyA_node_info -g

     

    机构B(机构B也要修改node_deployment.ini)

    ./generator --generate_all_certificates ./agencyB_node_info -g


    备注:这里/dir_chain_ca/chain、/dir_chain_ca/chain-g 、./dir_g_agency_ca/agency-B、/dir_g_agency_ca/agencyB

    内容都要copy 给自己机构的meta文件夹中


    将机构B生成的 gmcert_127.0.0.1_30300.crt、gmcert_127.0.0.1_30301.crt、peers.txt 发送到机构A的meta 目录下
    其中peers.txt 改名为peersB.txt;

    将机构A的peers.txt copy给机构Bmeta文件夹下,如果还有其他机构都要相互copy,这个是p2p节点通信地址的配置。

    4、机构A修改conf文件夹下的group_genesis.ini,并生成群组创世区块

    备注:这一步需要其他参与群组机构的节点证书和p2p连接信息。群组组成之后,
    已有节点的退出和新节点的加入都不要再修改创世区块(创世区块一经创建不可再修改)。

    ./generator --create_group_genesis ./group -g

     

    (同时也在generator/meta下放置了一份)


    同时将group.1.genesis copy 给其他机构meta


    5、机构生成所属节点

    机构A

    ./generator --build_install_package ./meta/peersB.txt ./nodeA -g

    机构B

    ./generator --build_install_package ./meta/peersA.txt ./nodeB -g

    备注:上面的操作,机构A已经把群组1的创世区块文件分发给了机构B,
    并放置在了机构B的generator/meta/下。按照企业级部署工具生成机构下属节点部署程序的条件,
    机构B还需要知道群组1内除机构B外其它所有机构的所有下属节点的p2p连接信息,
    且需要放置在需要放置在generator/meta下。节点连接信息的收集过程,实际部署的时候,
    由负责生成群组创世区块的机构分发给群组的下属机构比较好,因为在生成群组创世区块的时候就收集了这些信息,
    避免了群组的机构之间冗余的交流成本。

    6、启动节点

    机构A

    bash ./nodeA/start_all.sh

    机构B

    bash ./nodeB/start_all.sh

    控制台操作

    1、下载控制台

    在generator 目录下,可以在一个机构中下载控制台,配置多个连接配置,使用switch 2 (表示切换为群组2) 等命令切换

    #下载控制台,该命令意为下载并执行链接中的shell脚本,运行的结果是目录下载了控制台程序的压缩包,并解压为console文件夹

    bash <(curl -s https://raw.githubusercontent.com/FISCO-BCOS/console/master/tools/download_console.sh)


    2、替换国密版本的编译包

    跳转到指定的控制台目录下:

    cd /generator/console


    #将国密版合约编译包下载到/console/目录下

    curl -LO https://github.com/FISCO-BCOS/LargeFiles/raw/master/tools/solcj/solcJ-all-0.4.25-gm.jar


    #将默认的合约编译包替换为国密版编译包

    ./replace_solc_jar.sh solcJ-all-0.4.25-gm.jar

    备注:这里的编译包需要配合适合的版本,如果遇到权限 要进行权限赋予 sudo chmod -R 777 ./


    3、修改console 的配置

    conf/applicationContext.xml(新增)

    可以通过conf/applicationContext-sample.xml 修改名字,再修改配置操作

     

    备注:实际部署控制台时,不一定要将控制台放在节点的部署程序里,控制台程序的目录位置不受限制,
    甚至可以部署在非节点服务器上,只要将控制台的配置文件中的节点channel连接地址配置正确,
    保证控制台能连接到链即可。


    4、为控制台配置sdk证书

    控制台在某种意义上也是一种节点,是可以查看和操作链的节点,控制台本身不参与链的运行,
    但是连接链也需要认证(事实上FISCO BCOS系统中所有连接链的行为都需要认证,如使用web3sdk的业务系统等),
    即sdk证书。sdk证书在证书链中与节点证书是同一级别的证书,都是由机构颁发,


    因此联盟各成员机构经过联盟委员会同意后实际控制着联盟链的准入
    (注意:sdk证书在FISCO BCOS链中标志着身份,同一个证书理论上可以被不同场景使用,如控制台,web3sdk项目等,
    但是FISCO​​​​​​​ BCOS会认为是同一个身份,所以sdk证书最好不要复用)。
    本次测试,各机构模拟颁发sdk证书(均是自建的证书)的操作如下:

    机构A

    普通版本

    ./generator --generate_sdk_certificate ./dir_sdk_ca ./dir_agency_ca/agencyA/

    国密版本

    ./generator --generate_sdk_certificate ./dir_g_sdk_ca ./dir_g_agency_ca/agencyA/ -g

     

    将控制台的国密版本配置打开

    控制台的applicationContext.xml 修改配置,说明:配置文件只会在切换群组时会动态加载一次,如果控制台已经开启,这个时候修改配置文件,需要退出控制台重新加入。

    指定节点证书目录,机构证书及私钥存放目录和节点名称

    ./dir_sdk_ca是要生成sdk 证书的位置

    ./dir_agency_ca 是原有机构证书等位置

    将生成的sdk 证书copy 到控制台的conf目录中

     

    5、启动控制台

    cd ~/generator/console && bash ./start.sh 1

    初始化查看各个机构的控制台 getBlockNumber 都为0

    机构A进行合约部署,机构B的控制台再次查看getBlockNumber 是否变为1,如果是则共识成功

     6 合约部署测试

    使用测试合约部署,并查看共识,确认完成相应的测试


    进行sdk国密版本

    1、将控制台的conf 配置copy 到实际项目中。

    2、 sdk 配置版本为国密

    application.yml 设置为

    encrypt-type: # 0:standard, 1:guomi 
    encrypt-type: 1

    3、合约也必须转化为国密版本

    第一种:在console 控制台 执行 ./sol2java.sh org.fisco.bcos,将生成控制台的console/contracts/sdk 生成对应的文件,将java 的copy 到实际项目中就可以了

    加载私钥使用GenCredential类(适用于国密和非国密),Credential类只适用于加载非国密私钥。

    在java开发中,java合约文件就是由abi和bin生成的,单独的abi和bin文件对于调用和部署合约没啥用了,使用解析交易功能时需要相关abi,可以用下abi文件,或者也可以不用,直接传solidity合约,

    代码会生成abi也可以用于解析。所以,abi和bin文件都可以不用。

     

    第二种: 在sdk 中有SolidityFunctionWrapperGeneratorTest,将合约放入,去生成对应的代码,调用单元测试

     4、进行合约部署测试

    Asset.sol 转化的Asset.java 代码

      1 package org.fisco.bcos.solidity;
      2 
      3 import io.reactivex.Flowable;
      4 import java.math.BigInteger;
      5 import java.util.ArrayList;
      6 import java.util.Arrays;
      7 import java.util.Collections;
      8 import java.util.List;
      9 import java.util.concurrent.Callable;
     10 import org.fisco.bcos.channel.client.TransactionSucCallback;
     11 import org.fisco.bcos.web3j.abi.EventEncoder;
     12 import org.fisco.bcos.web3j.abi.TypeReference;
     13 import org.fisco.bcos.web3j.abi.datatypes.Event;
     14 import org.fisco.bcos.web3j.abi.datatypes.Function;
     15 import org.fisco.bcos.web3j.abi.datatypes.Type;
     16 import org.fisco.bcos.web3j.abi.datatypes.Utf8String;
     17 import org.fisco.bcos.web3j.abi.datatypes.generated.Int256;
     18 import org.fisco.bcos.web3j.abi.datatypes.generated.Uint256;
     19 import org.fisco.bcos.web3j.crypto.Credentials;
     20 import org.fisco.bcos.web3j.protocol.Web3j;
     21 import org.fisco.bcos.web3j.protocol.core.DefaultBlockParameter;
     22 import org.fisco.bcos.web3j.protocol.core.RemoteCall;
     23 import org.fisco.bcos.web3j.protocol.core.methods.request.BcosFilter;
     24 import org.fisco.bcos.web3j.protocol.core.methods.response.Log;
     25 import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt;
     26 import org.fisco.bcos.web3j.tuples.generated.Tuple2;
     27 import org.fisco.bcos.web3j.tx.Contract;
     28 import org.fisco.bcos.web3j.tx.TransactionManager;
     29 import org.fisco.bcos.web3j.tx.gas.ContractGasProvider;
     30 
     31 /**
     32  * <p>Auto generated code.
     33  * <p><strong>Do not modify!</strong>
     34  * <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
     35  * or the org.fisco.bcos.web3j.codegen.SolidityFunctionWrapperGenerator in the 
     36  * <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
     37  *
     38  * <p>Generated with web3j version none.
     39  */
     40 @SuppressWarnings("unchecked")
     41 public class Asset extends Contract {
     42     public static final String BINARY = "608060405234801561001057600080fd5b5061002861002d640100000000026401000000009004565b610185565b600061100190508073ffffffffffffffffffffffffffffffffffffffff1663c92a78016040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180806020018060200180602001848103845260078152602001807f745f617373657400000000000000000000000000000000000000000000000000815250602001848103835260078152602001807f6163636f756e74000000000000000000000000000000000000000000000000008152506020018481038252600b8152602001807f61737365745f76616c75650000000000000000000000000000000000000000008152506020019350505050602060405180830381600087803b15801561014657600080fd5b505af115801561015a573d6000803e3d6000fd5b505050506040513d602081101561017057600080fd5b81019080805190602001909291905050505050565b611e0f80620001956000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635b325d781461005c578063612d2bff146100e0578063b433c7ca146101ad575b600080fd5b34801561006857600080fd5b506100c3600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610234565b604051808381526020018281526020019250505060405180910390f35b3480156100ec57600080fd5b50610197600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192908035906020019092919050505061069e565b6040518082815260200191505060405180910390f35b3480156101b957600080fd5b5061021e600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803590602001909291905050506117d4565b6040518082815260200191505060405180910390f35b600080600080600080610245611cf4565b93508373ffffffffffffffffffffffffffffffffffffffff1663d8ac5957888673ffffffffffffffffffffffffffffffffffffffff1663c74f8caf6040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b1580156102c857600080fd5b505af11580156102dc573d6000803e3d6000fd5b505050506040513d60208110156102f257600080fd5b81019080805190602001909291905050506040518363ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180806020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828103825284818151815260200191508051906020019080838360005b838110156103a0578082015181840152602081019050610385565b50505050905090810190601f1680156103cd5780820380516001836020036101000a031916815260200191505b509350505050602060405180830381600087803b1580156103ed57600080fd5b505af1158015610401573d6000803e3d6000fd5b505050506040513d602081101561041757600080fd5b81019080805190602001909291905050509250600091508273ffffffffffffffffffffffffffffffffffffffff1663d3e9af5a6040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b15801561049257600080fd5b505af11580156104a6573d6000803e3d6000fd5b505050506040513d60208110156104bc57600080fd5b810190808051906020019092919050505060001415610503577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8281915095509550610695565b8273ffffffffffffffffffffffffffffffffffffffff16633dd2b61460006040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050602060405180830381600087803b15801561057357600080fd5b505af1158015610587573d6000803e3d6000fd5b505050506040513d602081101561059d57600080fd5b8101908080519060200190929190505050905060008173ffffffffffffffffffffffffffffffffffffffff16634900862e6040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180806020018281038252600b8152602001807f61737365745f76616c7565000000000000000000000000000000000000000000815250602001915050602060405180830381600087803b15801561065257600080fd5b505af1158015610666573d6000803e3d6000fd5b505050506040513d602081101561067c57600080fd5b8101908080519060200190929190505050819150955095505b50505050915091565b60008060008060008060008060008097506000965060009550600094506106c48c610234565b809750819850505060008714151561081a577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff97507f105af2c562df33af7eaa9de5fb0c18d8d30f281a18f95a8f76b44353a322693c888d8d8d604051808581526020018060200180602001848152602001838103835286818151815260200191508051906020019080838360005b8381101561076e578082015181840152602081019050610753565b50505050905090810190601f16801561079b5780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b838110156107d45780820151818401526020810190506107b9565b50505050905090810190601f1680156108015780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a18798506117c5565b6108238b610234565b8096508198505050600087141515610979577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe97507f105af2c562df33af7eaa9de5fb0c18d8d30f281a18f95a8f76b44353a322693c888d8d8d604051808581526020018060200180602001848152602001838103835286818151815260200191508051906020019080838360005b838110156108cd5780820151818401526020810190506108b2565b50505050905090810190601f1680156108fa5780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b83811015610933578082015181840152602081019050610918565b50505050905090810190601f1680156109605780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a18798506117c5565b89861015610ac5577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd97507f105af2c562df33af7eaa9de5fb0c18d8d30f281a18f95a8f76b44353a322693c888d8d8d604051808581526020018060200180602001848152602001838103835286818151815260200191508051906020019080838360005b83811015610a195780820151818401526020810190506109fe565b50505050905090810190601f168015610a465780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b83811015610a7f578082015181840152602081019050610a64565b50505050905090810190601f168015610aac5780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a18798506117c5565b848a86011015610c13577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc97507f105af2c562df33af7eaa9de5fb0c18d8d30f281a18f95a8f76b44353a322693c888d8d8d604051808581526020018060200180602001848152602001838103835286818151815260200191508051906020019080838360005b83811015610b67578082015181840152602081019050610b4c565b50505050905090810190601f168015610b945780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b83811015610bcd578082015181840152602081019050610bb2565b50505050905090810190601f168015610bfa5780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a18798506117c5565b610c1b611cf4565b93508373ffffffffffffffffffffffffffffffffffffffff16635887ab246040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b158015610c8157600080fd5b505af1158015610c95573d6000803e3d6000fd5b505050506040513d6020811015610cab57600080fd5b810190808051906020019092919050505092508273ffffffffffffffffffffffffffffffffffffffff16631a391cb48d6040518263ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808060200180602001838103835260078152602001807f6163636f756e7400000000000000000000000000000000000000000000000000815250602001838103825284818151815260200191508051906020019080838360005b83811015610d7e578082015181840152602081019050610d63565b50505050905090810190601f168015610dab5780820380516001836020036101000a031916815260200191505b509350505050600060405180830381600087803b158015610dcb57600080fd5b505af1158015610ddf573d6000803e3d6000fd5b505050508273ffffffffffffffffffffffffffffffffffffffff1663def426988b88036040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180806020018381526020018281038252600b8152602001807f61737365745f76616c756500000000000000000000000000000000000000000081525060200192505050600060405180830381600087803b158015610e8d57600080fd5b505af1158015610ea1573d6000803e3d6000fd5b505050508373ffffffffffffffffffffffffffffffffffffffff1663664b37d68d858773ffffffffffffffffffffffffffffffffffffffff1663c74f8caf6040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b158015610f2757600080fd5b505af1158015610f3b573d6000803e3d6000fd5b505050506040513d6020811015610f5157600080fd5b81019080805190602001909291905050506040518463ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180806020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828103825285818151815260200191508051906020019080838360005b83811015611031578082015181840152602081019050611016565b50505050905090810190601f16801561105e5780820380516001836020036101000a031916815260200191505b50945050505050602060405180830381600087803b15801561107f57600080fd5b505af1158015611093573d6000803e3d6000fd5b505050506040513d60208110156110a957600080fd5b8101908080519060200190929190505050915060018214151561120a577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb97507f105af2c562df33af7eaa9de5fb0c18d8d30f281a18f95a8f76b44353a322693c888d8d8d604051808581526020018060200180602001848152602001838103835286818151815260200191508051906020019080838360005b8381101561115e578082015181840152602081019050611143565b50505050905090810190601f16801561118b5780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b838110156111c45780820151818401526020810190506111a9565b50505050905090810190601f1680156111f15780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a18798506117c5565b8373ffffffffffffffffffffffffffffffffffffffff16635887ab246040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b15801561126e57600080fd5b505af1158015611282573d6000803e3d6000fd5b505050506040513d602081101561129857600080fd5b810190808051906020019092919050505090508073ffffffffffffffffffffffffffffffffffffffff16631a391cb48c6040518263ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808060200180602001838103835260078152602001807f6163636f756e7400000000000000000000000000000000000000000000000000815250602001838103825284818151815260200191508051906020019080838360005b8381101561136b578082015181840152602081019050611350565b50505050905090810190601f1680156113985780820380516001836020036101000a031916815260200191505b509350505050600060405180830381600087803b1580156113b857600080fd5b505af11580156113cc573d6000803e3d6000fd5b505050508073ffffffffffffffffffffffffffffffffffffffff1663def426988b87016040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180806020018381526020018281038252600b8152602001807f61737365745f76616c756500000000000000000000000000000000000000000081525060200192505050600060405180830381600087803b15801561147a57600080fd5b505af115801561148e573d6000803e3d6000fd5b505050508373ffffffffffffffffffffffffffffffffffffffff1663664b37d68c838773ffffffffffffffffffffffffffffffffffffffff1663c74f8caf6040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b15801561151457600080fd5b505af1158015611528573d6000803e3d6000fd5b505050506040513d602081101561153e57600080fd5b81019080805190602001909291905050506040518463ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180806020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828103825285818151815260200191508051906020019080838360005b8381101561161e578082015181840152602081019050611603565b50505050905090810190601f16801561164b5780820380516001836020036101000a031916815260200191505b50945050505050602060405180830381600087803b15801561166c57600080fd5b505af1158015611680573d6000803e3d6000fd5b505050506040513d602081101561169657600080fd5b8101908080519060200190929190505050507f105af2c562df33af7eaa9de5fb0c18d8d30f281a18f95a8f76b44353a322693c888d8d8d604051808581526020018060200180602001848152602001838103835286818151815260200191508051906020019080838360005b8381101561171d578082015181840152602081019050611702565b50505050905090810190601f16801561174a5780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b83811015611783578082015181840152602081019050611768565b50505050905090810190601f1680156117b05780820380516001836020036101000a031916815260200191505b50965050505050505060405180910390a18798505b50505050505050509392505050565b600080600080600080600080955060009450600093506117f389610234565b8095508196505050600085141515611c155761180d611cf4565b92508273ffffffffffffffffffffffffffffffffffffffff16635887ab246040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b15801561187357600080fd5b505af1158015611887573d6000803e3d6000fd5b505050506040513d602081101561189d57600080fd5b810190808051906020019092919050505091508173ffffffffffffffffffffffffffffffffffffffff16631a391cb48a6040518263ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808060200180602001838103835260078152602001807f6163636f756e7400000000000000000000000000000000000000000000000000815250602001838103825284818151815260200191508051906020019080838360005b83811015611970578082015181840152602081019050611955565b50505050905090810190601f16801561199d5780820380516001836020036101000a031916815260200191505b509350505050600060405180830381600087803b1580156119bd57600080fd5b505af11580156119d1573d6000803e3d6000fd5b505050508173ffffffffffffffffffffffffffffffffffffffff1663def42698896040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180806020018381526020018281038252600b8152602001807f61737365745f76616c756500000000000000000000000000000000000000000081525060200192505050600060405180830381600087803b158015611a7d57600080fd5b505af1158015611a91573d6000803e3d6000fd5b505050508273ffffffffffffffffffffffffffffffffffffffff16634c6f30c08a846040518363ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180806020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828103825284818151815260200191508051906020019080838360005b83811015611b50578082015181840152602081019050611b35565b50505050905090810190601f168015611b7d5780820380516001836020036101000a031916815260200191505b509350505050602060405180830381600087803b158015611b9d57600080fd5b505af1158015611bb1573d6000803e3d6000fd5b505050506040513d6020811015611bc757600080fd5b810190808051906020019092919050505090506001811415611bec5760009550611c10565b7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe95505b611c39565b7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff95505b7f7ac7a04970319ae8fc5b92fe177d000fee3c00c92f8e78aae13d6571f17c351f868a8a6040518084815260200180602001838152602001828103825284818151815260200191508051906020019080838360005b83811015611ca9578082015181840152602081019050611c8e565b50505050905090810190601f168015611cd65780820380516001836020036101000a031916815260200191505b5094505050505060405180910390a185965050505050505092915050565b600080600061100191508173ffffffffffffffffffffffffffffffffffffffff166359a48b656040518163ffffffff167c01000000000000000000000000000000000000000000000000000000000281526004018080602001828103825260078152602001807f745f617373657400000000000000000000000000000000000000000000000000815250602001915050602060405180830381600087803b158015611d9e57600080fd5b505af1158015611db2573d6000803e3d6000fd5b505050506040513d6020811015611dc857600080fd5b810190808051906020019092919050505090508092505050905600a165627a7a72305820a384a134ae2f23d7fb6ca4a68162ed2ae985de052f8414b5112f21e1b6f016400029";
     43 
     44     public static final String ABI = "[{"constant":true,"inputs":[{"name":"account","type":"string"}],"name":"select","outputs":[{"name":"","type":"int256"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"from_account","type":"string"},{"name":"to_account","type":"string"},{"name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"account","type":"string"},{"name":"asset_value","type":"uint256"}],"name":"register","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"ret","type":"int256"},{"indexed":false,"name":"account","type":"string"},{"indexed":false,"name":"asset_value","type":"uint256"}],"name":"RegisterEvent","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"ret","type":"int256"},{"indexed":false,"name":"from_account","type":"string"},{"indexed":false,"name":"to_account","type":"string"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"TransferEvent","type":"event"}]";
     45 
     46     public static final String FUNC_SELECT = "select";
     47 
     48     public static final String FUNC_TRANSFER = "transfer";
     49 
     50     public static final String FUNC_REGISTER = "register";
     51 
     52     public static final Event REGISTEREVENT_EVENT = new Event("RegisterEvent", 
     53             Arrays.<TypeReference<?>>asList(new TypeReference<Int256>() {}, new TypeReference<Utf8String>() {}, new TypeReference<Uint256>() {}));
     54     ;
     55 
     56     public static final Event TRANSFEREVENT_EVENT = new Event("TransferEvent", 
     57             Arrays.<TypeReference<?>>asList(new TypeReference<Int256>() {}, new TypeReference<Utf8String>() {}, new TypeReference<Utf8String>() {}, new TypeReference<Uint256>() {}));
     58     ;
     59 
     60     @Deprecated
     61     protected Asset(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
     62         super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
     63     }
     64 
     65     protected Asset(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
     66         super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
     67     }
     68 
     69     @Deprecated
     70     protected Asset(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
     71         super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
     72     }
     73 
     74     protected Asset(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
     75         super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);
     76     }
     77 
     78     public RemoteCall<Tuple2<BigInteger, BigInteger>> select(String account) {
     79         final Function function = new Function(FUNC_SELECT, 
     80                 Arrays.<Type>asList(new Utf8String(account)),
     81                 Arrays.<TypeReference<?>>asList(new TypeReference<Int256>() {}, new TypeReference<Uint256>() {}));
     82         return new RemoteCall<Tuple2<BigInteger, BigInteger>>(
     83                 new Callable<Tuple2<BigInteger, BigInteger>>() {
     84                     @Override
     85                     public Tuple2<BigInteger, BigInteger> call() throws Exception {
     86                         List<Type> results = executeCallMultipleValueReturn(function);
     87                         return new Tuple2<BigInteger, BigInteger>(
     88                                 (BigInteger) results.get(0).getValue(), 
     89                                 (BigInteger) results.get(1).getValue());
     90                     }
     91                 });
     92     }
     93 
     94     public RemoteCall<TransactionReceipt> transfer(String from_account, String to_account, BigInteger amount) {
     95         final Function function = new Function(
     96                 FUNC_TRANSFER, 
     97                 Arrays.<Type>asList(new Utf8String(from_account),
     98                 new Utf8String(to_account),
     99                 new Uint256(amount)),
    100                 Collections.<TypeReference<?>>emptyList());
    101         return executeRemoteCallTransaction(function);
    102     }
    103 
    104     public void transfer(String from_account, String to_account, BigInteger amount, TransactionSucCallback callback) {
    105         final Function function = new Function(
    106                 FUNC_TRANSFER, 
    107                 Arrays.<Type>asList(new Utf8String(from_account),
    108                 new Utf8String(to_account),
    109                 new Uint256(amount)),
    110                 Collections.<TypeReference<?>>emptyList());
    111         asyncExecuteTransaction(function, callback);
    112     }
    113 
    114     public String transferSeq(String from_account, String to_account, BigInteger amount) {
    115         final Function function = new Function(
    116                 FUNC_TRANSFER, 
    117                 Arrays.<Type>asList(new Utf8String(from_account),
    118                 new Utf8String(to_account),
    119                 new Uint256(amount)),
    120                 Collections.<TypeReference<?>>emptyList());
    121         return createTransactionSeq(function);
    122     }
    123 
    124     public RemoteCall<TransactionReceipt> register(String account, BigInteger asset_value) {
    125         final Function function = new Function(
    126                 FUNC_REGISTER, 
    127                 Arrays.<Type>asList(new Utf8String(account),
    128                 new Uint256(asset_value)),
    129                 Collections.<TypeReference<?>>emptyList());
    130         return executeRemoteCallTransaction(function);
    131     }
    132 
    133     public void register(String account, BigInteger asset_value, TransactionSucCallback callback) {
    134         final Function function = new Function(
    135                 FUNC_REGISTER, 
    136                 Arrays.<Type>asList(new Utf8String(account),
    137                 new Uint256(asset_value)),
    138                 Collections.<TypeReference<?>>emptyList());
    139         asyncExecuteTransaction(function, callback);
    140     }
    141 
    142     public String registerSeq(String account, BigInteger asset_value) {
    143         final Function function = new Function(
    144                 FUNC_REGISTER, 
    145                 Arrays.<Type>asList(new Utf8String(account),
    146                 new Uint256(asset_value)),
    147                 Collections.<TypeReference<?>>emptyList());
    148         return createTransactionSeq(function);
    149     }
    150 
    151     public List<RegisterEventEventResponse> getRegisterEventEvents(TransactionReceipt transactionReceipt) {
    152         List<EventValuesWithLog> valueList = extractEventParametersWithLog(REGISTEREVENT_EVENT, transactionReceipt);
    153         ArrayList<RegisterEventEventResponse> responses = new ArrayList<RegisterEventEventResponse>(valueList.size());
    154         for (EventValuesWithLog eventValues : valueList) {
    155             RegisterEventEventResponse typedResponse = new RegisterEventEventResponse();
    156             typedResponse.log = eventValues.getLog();
    157             typedResponse.ret = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
    158             typedResponse.account = (String) eventValues.getNonIndexedValues().get(1).getValue();
    159             typedResponse.asset_value = (BigInteger) eventValues.getNonIndexedValues().get(2).getValue();
    160             responses.add(typedResponse);
    161         }
    162         return responses;
    163     }
    164 
    165     public Flowable<RegisterEventEventResponse> registerEventEventFlowable(BcosFilter filter) {
    166         return web3j.logFlowable(filter).map(new io.reactivex.functions.Function<Log, RegisterEventEventResponse>() {
    167             @Override
    168             public RegisterEventEventResponse apply(Log log) {
    169                 EventValuesWithLog eventValues = extractEventParametersWithLog(REGISTEREVENT_EVENT, log);
    170                 RegisterEventEventResponse typedResponse = new RegisterEventEventResponse();
    171                 typedResponse.log = log;
    172                 typedResponse.ret = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
    173                 typedResponse.account = (String) eventValues.getNonIndexedValues().get(1).getValue();
    174                 typedResponse.asset_value = (BigInteger) eventValues.getNonIndexedValues().get(2).getValue();
    175                 return typedResponse;
    176             }
    177         });
    178     }
    179 
    180     public Flowable<RegisterEventEventResponse> registerEventEventFlowable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    181         BcosFilter filter = new BcosFilter(startBlock, endBlock, getContractAddress());
    182         filter.addSingleTopic(EventEncoder.encode(REGISTEREVENT_EVENT));
    183         return registerEventEventFlowable(filter);
    184     }
    185 
    186     public List<TransferEventEventResponse> getTransferEventEvents(TransactionReceipt transactionReceipt) {
    187         List<EventValuesWithLog> valueList = extractEventParametersWithLog(TRANSFEREVENT_EVENT, transactionReceipt);
    188         ArrayList<TransferEventEventResponse> responses = new ArrayList<TransferEventEventResponse>(valueList.size());
    189         for (EventValuesWithLog eventValues : valueList) {
    190             TransferEventEventResponse typedResponse = new TransferEventEventResponse();
    191             typedResponse.log = eventValues.getLog();
    192             typedResponse.ret = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
    193             typedResponse.from_account = (String) eventValues.getNonIndexedValues().get(1).getValue();
    194             typedResponse.to_account = (String) eventValues.getNonIndexedValues().get(2).getValue();
    195             typedResponse.amount = (BigInteger) eventValues.getNonIndexedValues().get(3).getValue();
    196             responses.add(typedResponse);
    197         }
    198         return responses;
    199     }
    200 
    201     public Flowable<TransferEventEventResponse> transferEventEventFlowable(BcosFilter filter) {
    202         return web3j.logFlowable(filter).map(new io.reactivex.functions.Function<Log, TransferEventEventResponse>() {
    203             @Override
    204             public TransferEventEventResponse apply(Log log) {
    205                 EventValuesWithLog eventValues = extractEventParametersWithLog(TRANSFEREVENT_EVENT, log);
    206                 TransferEventEventResponse typedResponse = new TransferEventEventResponse();
    207                 typedResponse.log = log;
    208                 typedResponse.ret = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
    209                 typedResponse.from_account = (String) eventValues.getNonIndexedValues().get(1).getValue();
    210                 typedResponse.to_account = (String) eventValues.getNonIndexedValues().get(2).getValue();
    211                 typedResponse.amount = (BigInteger) eventValues.getNonIndexedValues().get(3).getValue();
    212                 return typedResponse;
    213             }
    214         });
    215     }
    216 
    217     public Flowable<TransferEventEventResponse> transferEventEventFlowable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    218         BcosFilter filter = new BcosFilter(startBlock, endBlock, getContractAddress());
    219         filter.addSingleTopic(EventEncoder.encode(TRANSFEREVENT_EVENT));
    220         return transferEventEventFlowable(filter);
    221     }
    222 
    223     @Deprecated
    224     public static Asset load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    225         return new Asset(contractAddress, web3j, credentials, gasPrice, gasLimit);
    226     }
    227 
    228     @Deprecated
    229     public static Asset load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    230         return new Asset(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    231     }
    232 
    233     public static Asset load(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
    234         return new Asset(contractAddress, web3j, credentials, contractGasProvider);
    235     }
    236 
    237     public static Asset load(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
    238         return new Asset(contractAddress, web3j, transactionManager, contractGasProvider);
    239     }
    240 
    241     public static RemoteCall<Asset> deploy(Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
    242         return deployRemoteCall(Asset.class, web3j, credentials, contractGasProvider, BINARY, "");
    243     }
    244 
    245     public static RemoteCall<Asset> deploy(Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
    246         return deployRemoteCall(Asset.class, web3j, transactionManager, contractGasProvider, BINARY, "");
    247     }
    248 
    249     @Deprecated
    250     public static RemoteCall<Asset> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    251         return deployRemoteCall(Asset.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
    252     }
    253 
    254     @Deprecated
    255     public static RemoteCall<Asset> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    256         return deployRemoteCall(Asset.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
    257     }
    258 
    259     public static class RegisterEventEventResponse {
    260         public Log log;
    261 
    262         public BigInteger ret;
    263 
    264         public String account;
    265 
    266         public BigInteger asset_value;
    267     }
    268 
    269     public static class TransferEventEventResponse {
    270         public Log log;
    271 
    272         public BigInteger ret;
    273 
    274         public String from_account;
    275 
    276         public String to_account;
    277 
    278         public BigInteger amount;
    279     }
    280 }

    指定的单元测试代码

    package customTest;
    
    import javafx.concurrent.Service;
    import org.fisco.bcos.Application;
    import org.fisco.bcos.solidity.Asset;
    import org.fisco.bcos.temp.HelloWorld;
    import org.fisco.bcos.web3j.crypto.Credentials;
    import org.fisco.bcos.web3j.crypto.gm.GenCredential;
    import org.fisco.bcos.web3j.protocol.Web3j;
    import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt;
    import org.fisco.bcos.web3j.tuples.generated.Tuple2;
    import org.fisco.bcos.web3j.tx.gas.StaticGasProvider;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.math.BigInteger;
    
    import static org.junit.Assert.assertTrue;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = Application.class)
    public class AssetTest {
        private Credentials credentials;
        private static BigInteger gasPrice = new BigInteger("300000000");
        private static BigInteger gasLimit = new BigInteger("300000000");
        @Autowired
        Web3j web3j;
    
        //这很重要,没有这个无法通过
        @Before
        public void setUp() throws Exception {
           /* credentials =
                    GenCredential.create(
                            "b83261efa42895c38c6c2364ca878f43e77f3cddbc922bf57d0d48070f79feb6");
            if (credentials == null) {
                throw new Exception("create Credentials failed");
            }*/
    
             credentials = GenCredential.create();
        }
    
        @After
        public void tearDown() {
        }
    
        @Test
        public void DoAsset() throws Exception {
            AssetRegisterAndQuery();
            //DeployAsset();
    
        }
        @Test
        //部署合约
        public void DeployAsset() throws Exception {
            // 部署合约
            Asset asset = Asset.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
    
            if (asset != null) {
                System.out.println("Asset address is: " + asset.getContractAddress());
            }
    
        }
        @Test
        //用户注册 资产查询
        public void AssetRegisterAndQuery()throws Exception  {
            String contractAddress = "0x9fb2cc10c87e0caedb7e325f43eab0dbaa83f5ca";//需要根据部署地址修改
            // 加载合约地址
            Asset asset = Asset.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
    
            if (asset != null) {
                System.out.println("Asset address is: " + asset.getContractAddress());
                // call set function
                System.out.println("1、注册用户,并注册资产--------------------------------------");
                String assetAccount1="0x608153babb8b00f11523f6b1b2b225ea9e7dfd8b";
                String assetAccount2="0x86f17b879ce121e5d00351a120de0bd39867bf4c";
                // register接口调用
                TransactionReceipt receipt1 = asset.register(assetAccount1, new BigInteger("121210000") ).send();
                TransactionReceipt receipt2 = asset.register(assetAccount2, new BigInteger("121121213") ).send();
    
                System.out.println("receipt1="+receipt1.toString());
                System.out.println("receipt2="+receipt2.toString());
    
                System.out.println("2、查询用户资产----------------------------------------------");
                // select接口调用
                Tuple2<BigInteger, BigInteger> result1 = asset.select("abc").send();
                Tuple2<BigInteger, BigInteger> result2 = asset.select("ddd").send();
                System.out.println("Tuple2<BigInteger, BigInteger> result1="+result1.toString());
                System.out.println("Tuple2<BigInteger, BigInteger> result2="+result2.toString());
                //assertTrue("Hello, World!".equals(result));
            }
    
    
        }
    
        // 资产交易
        @Test
        public  void AssetTransfer() throws  Exception{
    
            String contractAddress = "0xf9343346a8d80c3d2f2026bf72fff3aec48a4133";
            // 加载合约地址
            Asset asset = Asset.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
    
            String fromAssetAccount="0x608153babb8b00f11523f6b1b2b225ea9e7dfd8b";
            String  toAssetAccount="0x86f17b879ce121e5d00351a120de0bd39867bf4c";
          BigInteger amount = new BigInteger("121210000");
            if (asset != null) {
                // transfer接口
                TransactionReceipt receipt = asset.transfer( fromAssetAccount,toAssetAccount, amount).send();
                System.out.println("AssetTest.AssetTransfer receipt="+receipt.toString());
            }
    
        }
    }

    分别进行合约的部署、生成账户等操作,最后和控制台对比结果

    至此,我们完成国密版本的部署搭建、以及对应的国密版本合约开发、sdk 调用等工作

    读后感觉不错,有收获可以微信请作者喝杯咖啡,读后有疑问请加微信,拉群研讨,注明来意

  • 相关阅读:
    提高PHP程序运行效率的方法
    必须知道的sql编写技巧。多条件查询不拼接字符串·的写法
    数据库SQL优化大总结之 百万级数据库优化方案
    编程一开始就应该养成的好习惯
    php图像处理
    jqurey 简单的,我也简单
    菜单上下级 (全国地区)
    think php v5.0
    正则表达式
    有感赠朵朵
  • 原文地址:https://www.cnblogs.com/linbin524/p/11190547.html
Copyright © 2011-2022 走看看