区块链系列教程(3)
通过jav
a
SDK 实
现个性化智能合约的部署与测
试
哈希科技作为一家区块链技术服务商,有责任也有义务为推广区块链贡献一份力量。计划连续推出区块链教程,欢迎大家交流。
哈希科技拥有全面的区块链能力,支持Hyperledger Fabric、Ethereum、FISCO BCOS及腾讯Trust SQL等主流框架。
FISCO BCOS是微众银行、腾讯等联合推出的,安全可控、适用于金融行业且完全开源的区块链底层平台。哈希科技与微众区块链团队进行过深度对接合作,也得到了微众团队童鞋们的鼎力支持和帮助,在此一并感谢!
本教程作者为哈希科技CTO林滨,这是一位才华在线颜值担当的90后有为青年,推荐大家关注。
前提:
已经部署好底层,外网可以正常请求访问。
1、基础合约处理
https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/tutorial/sdk_application.html#id2
将官方的Asset.sol 代码copy,使用vim Asset.sol 命令创建,copy 到里面。
上一小节,我们根据业务需求设计了合约
Asset.sol
的存储与接口,给出了完整实现,但是Java程序无法直接调用Solidity合约,需要先将Solidity合约文件编译为Java文件。
控制台提供了编译工具,可以将
Asset.sol
合约文件存放在
console/contracts/solidity
目录。利用console目录下提供的
sol2java.sh
脚本进行编译,操作如下:
# 切换到fisco/console/目录 $ cd ~/fisco/console/ # 编译合约,后面指定一个Java的包名参数,可以根据实际项目路径指定包名 $ ./sol2java.sh org.fisco.bcos.asset.contract
2、合约转化
用官方的asset_app,自己生成的在sdk 那边用有问题
运行成功之后,将会在
console/contracts/sdk
目录生成java、abi和bin目录,如下所示。
java目录下生成了
org/fisco/bcos/asset/contract/
包路径目录,该目录下包含
Asset.java
和
Table.java
两个文件,其中
Asset.java
是Java应用调用
Asset.sol
合约需要的文件。
3、SDK配置
我们提供了一个Java工程项目供开发使用,首先获取Java工程项目:
# 获取Java工程项目压缩包 $ cd ~ $ curl -LO https://github.com/FISCO-BCOS/LargeFiles/raw/master/tools/asset-app.tar.gz # 解压得到Java工程项目asset-app目录 $ tar -zxf asset-app.tar.gz
asset-app项目的目录结构如下:
4、使用sdk进行开发
将asset_app 的sol、指定的合约的java、abi、bin等文件copy 到项目中,使用winscp等软件copy
5、底层部署和测试
•
编译
# 切换到项目目录 $ cd ~/asset-app # 编译项目 $ ./gradlew build
编译成功之后,将在项目根目录下生成dist目录。dist目录下有一个
asset_run.sh
脚本,简化项目运行。现在开始一一验证本文开始定下的需求。
•
部署Asset.sol合约
# 进入dist目录 $ cd dist $ bash asset_run.sh deploy Deploy Asset succesfully, contract address is 0xd09ad04220e40bb8666e885730c8c460091a4775
•
注册资产
$ bash asset_run.sh register Alice 100000 Register account succesfully=> account: Alice, value: 100000 $ bash asset_run.sh register Bob 100000 Register account succesfully=> account: Bob, value: 100000
•
查询资产
$ bash asset_run.sh query Alice account Alice, value 100000 $ bash asset_run.sh query Bob account Bob, value 100000
•
资产转移
$ bash asset_run.sh transfer Alice Bob 50000 Transfer successfully=> from_account: Alice, to_account: Bob, amount: 50000 $ bash asset_run.sh query Alice account Alice, value 50000 $ bash asset_run.sh query Bob account Bob, value 150000
6、本地编写单元测试
1 package customTest;
2
3 import javafx.concurrent.Service;
4 import org.fisco.bcos.Application;
5 import org.fisco.bcos.solidity.Asset;
6 import org.fisco.bcos.temp.HelloWorld;
7 import org.fisco.bcos.web3j.crypto.Credentials;
8 import org.fisco.bcos.web3j.crypto.gm.GenCredential;
9 import org.fisco.bcos.web3j.protocol.Web3j;
10 import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt;
11 import org.fisco.bcos.web3j.tuples.generated.Tuple2;
12 import org.fisco.bcos.web3j.tx.gas.StaticGasProvider;
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.springframework.beans.factory.annotation.Autowired;
18 import org.springframework.boot.test.context.SpringBootTest;
19 import org.springframework.context.ApplicationContext;
20 import org.springframework.context.support.ClassPathXmlApplicationContext;
21 import org.springframework.test.context.junit4.SpringRunner;
22
23 import java.math.BigInteger;
24
25 import static org.junit.Assert.assertTrue;
26
27 @RunWith(SpringRunner.class)
28 @SpringBootTest(classes = Application.class)
29 public class AssetTest {
30 private Credentials credentials;
31 private static BigInteger gasPrice = new BigInteger("300000000");
32 private static BigInteger gasLimit = new BigInteger("300000000");
33 @Autowired
34 Web3j web3j;
35
36 //这很重要,没有这个无法通过
37 @Before
38 public void setUp() throws Exception {
39 /* credentials =
40 GenCredential.create(
41 "b83261efa42895c38c6c2364ca878f43e77f3cddbc922bf57d0d48070f79feb6");
42 if (credentials == null) {
43 throw new Exception("create Credentials failed");
44 }*/
45
46 credentials = GenCredential.create();
47 }
48
49 @After
50 public void tearDown() {
51 }
52
53 @Test
54 public void DoAsset() throws Exception {
55 AssetRegisterAndQuery();
56 //DeployAsset();
57
58 }
59 @Test