第八章 Fisco Bcos 國密版本的部署、控制檯搭建、合約的部署、sdk 調用

鑑於筆者之前各大博客教程都有不少人提問,早期創建一個技術交流羣,裏面技術體系可能比較雜,想了解相關區塊鏈開發,技術提問,請加QQ羣:538327407html

參考資料

證書說明:https://mp.weixin.qq.com/s/nRwHxt2E1TJiopJqLSvMOQjava

參考資料:https://blog.csdn.net/FISCO_BCOS/article/details/95496272 (國密版本部署,很不錯)node


準備工做

一、機構A ,做爲創世區塊的服務器react

148.70.170.96 (公) git

172.27.16.14 (內)github

二、機構B,聯盟鏈機構web

148.70.71.251 (公) spring

172.27.16.17 (內)shell


1、下載安裝 企業版部署工具

一、下載

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

 

二、安裝

此操做要求用戶具備sudo權限。bash

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

 

三、檢查是否安裝成功,若成功,輸出 usage: generator xxx

./generator -h

查看版本

./generator -v

> INFO | v1.0.0

四、拉取節點二進制

拉取最新fisco-bcos二進制文件到meta中 這裏要修改下載國密版本(源程序)

./generator --download_fisco ./meta -g

 

-g參數表示國密,去掉-g則下載的是普通版FISCO BCOS,執行完該命令,國密版FISCO BCOS會下載到generator/meta/下。

五、檢查二進制版本

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

./meta/fisco-bcos -v

 

顯示國密版本

 



正常操做流程

 

一、建立鏈證書

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

二、機構向聯盟委員會申請機構證書

生成國密和普通的版本的機構證書

機構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 給其餘機構


三、創世區塊所處的機構,修改配置文件 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節點通訊地址的配置。

四、機構A修改conf文件夾下的group_genesis.ini,並生成羣組創世區塊

備註:這一步須要其餘參與羣組機構的節點證書和p2p鏈接信息。羣組組成以後,
已有節點的退出和新節點的加入都不要再修改創世區塊(創世區塊一經建立不可再修改)。

./generator --create_group_genesis ./group -g

 

 

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


同時將group.1.genesis copy 給其餘機構meta


五、機構生成所屬節點

 

機構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下。節點鏈接信息的收集過程,實際部署的時候,
由負責生成羣組創世區塊的機構分發給羣組的下屬機構比較好,由於在生成羣組創世區塊的時候就收集了這些信息,
避免了羣組的機構之間冗餘的交流成本。

 

六、啓動節點

機構A

bash ./nodeA/start_all.sh

 

機構B

bash ./nodeB/start_all.sh

 


 

控制檯操做

一、下載控制檯

在generator 目錄下,能夠在一個機構中下載控制檯,配置多個鏈接配置,使用switch 2 (表示切換爲羣組2) 等命令切換

#下載控制檯,該命令意爲下載並執行連接中的shell腳本,運行的結果是目錄下載了控制檯程序的壓縮包,並解壓爲console文件夾

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

 


二、替換國密版本的編譯包

 

跳轉到指定的控制檯目錄下:

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 ./


三、修改console 的配置

conf/applicationContext.xml(新增)

能夠經過conf/applicationContext-sample.xml 修更名字,再修改配置操做

 

 

備註:實際部署控制檯時,不必定要將控制檯放在節點的部署程序裏,控制檯程序的目錄位置不受限制,
甚至能夠部署在非節點服務器上,只要將控制檯的配置文件中的節點channel鏈接地址配置正確,
保證控制檯能鏈接到鏈便可。


四、爲控制檯配置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目錄中

 

 

 

五、啓動控制檯

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

 

初始化查看各個機構的控制檯 getBlockNumber 都爲0

機構A進行合約部署,機構B的控制檯再次查看getBlockNumber 是否變爲1,若是是則共識成功

 

 6 合約部署測試

使用測試合約部署,並查看共識,確認完成相應的測試

 

 

 


 

進行sdk國密版本

一、將控制檯的conf 配置copy 到實際項目中。

 

二、 sdk 配置版本爲國密

application.yml 設置爲

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

 

三、合約也必須轉化爲國密版本

第一種:在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,將合約放入,去生成對應的代碼,調用單元測試

 四、進行合約部署測試

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("一、註冊用戶,並註冊資產--------------------------------------");
            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("二、查詢用戶資產----------------------------------------------");
            // 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 調用等工做

相關文章
相關標籤/搜索