请先阅读前置文章 《fisco区块链部署》

SpringBoot的版本必须选择2 , jdk版本必须选择11 ,可参照文章<<SpringBoot 项目起步>>

编写合约

位置: ~/fisco/console/contracts/solidity,可通过文件夹下案例或网络资料模仿编写


部署合约于fisco

开启fisco节点

1
bash ~/fisco/nodes/127.0.0.1/start_all.sh

进入控制台

1
bash ~/fisco/console/start.sh

部署合约

1
deploy {合约名}.sol

记住部署后输出的contractAddress


编译合约

1
2
3
# 使用sol2java.sh将contracts/solidity下的所有合约编译产生bin,abi,java工具类。
# 当前目录~/fisco/console
bash sol2java.sh -p org.com.fisco

pow文件引入fisco依赖

1
2
3
4
5
6
<!-- fisco -->
<dependency>
<groupId>org.fisco-bcos.java-sdk</groupId>
<artifactId>fisco-bcos-java-sdk</artifactId>
<version>2.9.1</version>
</dependency>

创建并编写配置文件

位置: SpringBoot项目的resource/config-fisco.toml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
[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 cores

maxBlockingQueueSize = "102400" # The max blocking queue size of the thread pool

编译后的合约整合进项目

将~/fisco/console/concontracts/sdk下的abi和bin文件夹复制到SpringBoot的resource文件夹下


区块链证书整合进项目

将~/fisco/nodes/127.0.0.1/sdk下的所有文件复制到SpringBoot的resource/conf文件夹下


编写SpringBoot的fisco调用组件

初始化组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// component/SoildityComponent.java

@Component
@Slf4j
public class SoildityComponent {

public String configFile;

public String abiFile;

private String contractAddress = "部署在fisco后的给出的contractAddress";

private AssembleTransactionProcessor transactionProcessor;

public SoildityComponent() throws Exception {

configFile = SoildityComponent.class.getClassLoader().getResource("config-fisco.toml").getPath();
abiFile = SoildityComponent.class.getClassLoader().getResource("abi/").getPath();

// 初始化BcosSDK对象
BcosSDK sdk = BcosSDK.build(configFile);
// 获取Client对象,此处传入的群组ID为1
Client client = sdk.getClient(Integer.valueOf(1));
// 构造AssembleTransactionProcessor对象,需要传入client对象,CryptoKeyPair对象和abi、binary文件存放的路径。abi和binary文件需要在上一步复制到定义的文件夹中。
CryptoKeyPair keyPair = client.getCryptoSuite().createKeyPair();

transactionProcessor = TransactionProcessorFactory.createAssembleTransactionProcessor
(client, keyPair, abiFile, "");
}
}

组件方法示例(仅供参考,具体需对应合约)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public String addRecord(String account, String type_id, String consume, String id) throws ABICodecException, TransactionBaseException {
List<Object> params = new ArrayList<>();
params.add(account);
params.add(type_id);
params.add(consume);
params.add(id);
TransactionResponse transactionResponse =
transactionProcessor.sendTransactionAndGetResponseByContractLoader("Carbon_trading", contractAddress, "addRecord", params);
List<Object> valuesList = transactionResponse.getValuesList();
log.info("区块链发起交易成功返回map_id为:{}", valuesList.get(1).toString());
return valuesList.get(1).toString();
}

public Carbon_trading getRecord(String map_id) throws ABICodecException, TransactionBaseException {
List<Object> params = new ArrayList<>();
params.add(map_id);
CallResponse callResponse =
transactionProcessor.sendCallByContractLoader("Carbon_trading", contractAddress, "getRecord", params);
List<Object> valuesList = callResponse.getReturnObject();
log.info("返回交易信息为:{}", valuesList);
Carbon_trading carbon_trading = Carbon_trading.builder()
.account(valuesList.get(0).toString())
.type_id(valuesList.get(1).toString())
.consume(valuesList.get(2).toString())
.map_id(valuesList.get(3).toString())
.build();
return carbon_trading;
}

完结撒花