1.安装环境(Ubantu)
Linux IDEA下载: https://blog.csdn.net/JOJO_jiongjiong/article/details/123087307
Linux Maven下载: https://zhuanlan.zhihu.com/p/443389963
最好在setting.xml 把maven本地仓库也改一下。
Linux java( 8-14都可以)下载:https://blog.csdn.net/baoqiaoben/article/details/78936955
2.搭建区块链
单机4节点区块链网络搭建: https://blog.csdn.net/Qhx20040819/article/details/131909957
3.开发智能合约应用
1. 使用 IDEA 创建一个Maven应用
2.引入 fisco java sdk
<!-- fisco sdk--><dependency><groupId>org.fisco-bcos.java-sdk</groupId><artifactId>fisco-bcos-java-sdk</artifactId><version>2.9.1</version></dependency>
3. 配置SDK证书
将节点的sdk证书拷贝到resources目录下面。
# 假设SDK证书位于~/fisco/nodes/127.0.0.1/sdk/目录
mkdir -p conf && cp -r ~/fisco/nodes/127.0.0.1/sdk/* conf
4. 将合约生成对应的java 文件
我们用拉取fisco 的控制台里默认带的HelloWorld合约来演示。
在 console/目录下
若控制台版本大于等于2.8.0:
# 使用sol2java.sh将contracts/solidity下的所有合约编译产生bin,abi,java工具类。
# 当前目录~/fisco/console
$ bash sol2java.sh -p org.com.fisco
# 以上命令中参数“org.com.fisco”是指定产生的java类所属的包名。
# 通过命令./sol2java.sh -h可查看该脚本使用方法
若控制台版本小于2.8.0:
# 使用sol2java.sh将contracts/solidity下的所有合约编译产生bin,abi,java工具类。
# 当前目录~/fisco/console
$ bash sol2java.sh org.com.fisco
# 以上命令中参数“org.com.fisco”是指定产生的java类所属的包名。
# ./sol2java.sh [packageName] [solidityFilePath] [javaCodeOutputDir]
查看编译结果
$ ls contracts/sdk/java/org/com/fisco
# 得到返回
# HelloWorld.java KVTableTest.java ShaTest.java Table.java TableTest.java
将该java 文件移动到你的java项目里。
PS :注意,生成java文件包名要跟你项目里对应的包名要一致。
5. 创建配置文件
PS :我直接从文档粘贴的,详情请见:配置说明 — FISCO BCOS v2 v2.9.0 文档
[cryptoMaterial]certPath = "conf" # The certification path# The following configurations take the certPath by default if commented
# caCert = "conf/ca.crt" # CA cert file path# If connect to the GM node, default CA cert path is ${certPath}/gm/gmca.crt# sslCert = "conf/sdk.crt" # SSL cert file path# If connect to the GM node, the default SDK cert path is ${certPath}/gm/gmsdk.crt# sslKey = "conf/sdk.key" # SSL key file path# If connect to the GM node, the default SDK privateKey path is ${certPath}/gm/gmsdk.key# enSslCert = "conf/gm/gmensdk.crt" # GM encryption cert file path# default load the GM SSL encryption cert from ${certPath}/gm/gmensdk.crt# enSslKey = "conf/gm/gmensdk.key" # GM ssl cert file path# default load the GM SSL encryption privateKey from ${certPath}/gm/gmensdk.key[network]
peers=["127.0.0.1:20200", "127.0.0.1:20201"] # The peer list to connect# AMOP configuration
# You can use following two methods to configure as a private topic message sender or subscriber.
# Usually, the public key and private key is generated by subscriber.
# Message sender receive public key from topic subscriber then make configuration.
# But, please do not config as both the message sender and the subscriber of one private topic, or you may send the message to yourself.# Configure a private topic as a topic message sender.
# [[amop]]
# topicName = "PrivateTopic"
# publicKeys = [ "conf/amop/consumer_public_key_1.pem" ] # Public keys of the nodes that you want to send AMOP message of this topic to.# Configure a private topic as a topic subscriber.
# [[amop]]
# topicName = "PrivateTopic"
# privateKey = "conf/amop/consumer_private_key.p12" # Your private key that used to subscriber verification.
# password = "123456"[account]
keyStoreDir = "account" # The directory to load/store the account file, default is "account"
# accountFilePath = "" # The account file path (default load from the path specified by the keyStoreDir)
accountFileFormat = "pem" # The storage format of account file (Default is "pem", "p12" as an option)# accountAddress = "" # The transactions sending account address# Default is a randomly generated account# The randomly generated account is stored in the path specified by the keyStoreDir# password = "" # The password used to load the account file[threadPool]
# channelProcessorThreadSize = "16" # The size of the thread pool to process channel callback# Default is the number of cpu cores# receiptProcessorThreadSize = "16" # The size of the thread pool to process transaction receipt notification# Default is the number of cpu coresmaxBlockingQueueSize = "102400" # The max blocking queue size of the thread pool
将该配置文件也移动到conf同级目录。
6. 使用测试文件部署和调用智能合约
public class BcosSDKTest
{// 获取配置文件路径public final String configFile = BcosSDKTest.class.getClassLoader().getResource("config.toml").getPath();public void testClient() throws ConfigException, ContractException {// 初始化BcosSDKBcosSDK sdk = BcosSDK.build(configFile);// 为群组1初始化clientClient client = sdk.getClient(Integer.valueOf(1));// 获取群组1的块高BlockNumber blockNumber = client.getBlockNumber();// 向群组1部署HelloWorld合约CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().getCryptoKeyPair();HelloWorld helloWorld = HelloWorld.deploy(client, cryptoKeyPair);// 调用HelloWorld合约的get接口String getValue = helloWorld.get();// 调用HelloWorld合约的set接口TransactionReceipt receipt = helloWorld.set("Hello, fisco");}public static void main(String[] args) throws ContractException, ConfigException {BcosSDKTest bcosSDKTest = new BcosSDKTest();bcosSDKTest.testClient();}
}
运行之后,控制台出现如此效果,代表成功: