0
点赞
收藏
分享

微信扫一扫

web3sdk 是什么 Spring Boot Starter怎么用

灵魂跑者 2022-03-30 阅读 77


一、Web3SDK是什么

Web3SDK为​​FISCO BCOS​​提供Java API。

利用FISCO BCOS JAVA SDK可以简单快捷的基于FISCO-BCOS进行区块链应用开发。

此版本只支持​​FISCO BCOS 2.0+​​。

web3sdk 是什么 Spring Boot Starter怎么用_区块链




关键特性

  • 实现FISCO BCOS的​​JSON-RPC​​的Java API。
  • 支持​​预编译合约​​管理区块链。
  • 支持​​链上信使协议​​为联盟链提供安全高效的消息信道。
  • 支持使用​​国密算法​​发送交易。


源码编译

环境要求​:

  • ​​JDK8及以上​​

一定要安装java8


  • Gradle 5.0及以上


编译运行如下命令​:

$ cd web3sdk
$ ./gradlew build

编译结果​: 编译的web3sdk jar位于:

web3sdk/dist/apps/web3sdk.jar

文档

  • ​​英文​​
  • ​​中文​​

快速入门

提供基于SDK的​​spring boot starter​​示例项目,示例项目使用了SDK的核心特性, 包括:

  • 如何连接FISCO BCOS区块链节点。
  • 在区块链网络中部署合约。
  • 读取部署合约的变量值。
  • 更新部署合约的变量值。
  • 提供SDK API接口的测试案例。


​​https://gitee.com/FISCO-BCOS/web3sdk/​​



二、怎么用

Spring Boot Starter

  该项目是基于​​Web3SDK​​的spring boot版本的示例项目。提供FISCO BCOS区块链应用开发的基本框架和基本的测试案例,帮助开发者基于FISCO BCOS区块链快速进行应用开发。​此版本只支持​​FISCO BCOS 2.0+​​。


快速启动

前置条件

搭建FISCO BCOS区块链,具体步骤​​参考这里​​。


获取源码

git clone https://github.com/FISCO-BCOS/spring-boot-starter.git


节点证书配置

将节点所在目录​​nodes/${ip}/sdk​​​下的​​ca.crt​​​、​​sdk.crt​​​和​​sdk.key​​​文件拷贝到项目的​​src/main/resources​​​目​​录下供SDK使用​

​(FISCO BCOS 2.1以前,证书为​​​ca.crt​​、​​​node.crt​​和​​node.key`)。

web3sdk 是什么 Spring Boot Starter怎么用_java_02

web3sdk 是什么 Spring Boot Starter怎么用_fisco bcos_03

web3sdk 是什么 Spring Boot Starter怎么用_区块链_04



配置文件设置

spring boot项目的配置文件application.yml如下图所示,其中加了注释的内容根据区块链节点配置做相应修改。

web3sdk 是什么 Spring Boot Starter怎么用_spring_05

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

group-channel-connections-config:
caCert: classpath:ca.crt
sslCert: classpath:sdk.crt
sslKey: classpath:sdk.key
all-channel-connections:
- group-id: 1 #group ID
connections-str:
- 192.168.64.129:20200 # node listen_ip:channel_listen_port
- 192.168.64.131:20200
- 192.168.64.132:20200
- 192.168.64.130:20200
- group-id: 2
connections-str:
- 127.0.0.1:20202 # node listen_ip:channel_listen_port
- 127.0.0.1:20203

channel-service:
group-id: 1 # The specified group to which the SDK connects
agency-name: agencyA # agency name

accounts:
pem-file: 0xcdcce60801c0a2e6bb534322c32ae528b9dec8d2.pem
p12-file: 0x98333491efac02f8ce109b0c499074d47e7779a6.p12
password: 123456


项目中关于SDK配置的详细说明请​​参考这里​​。



运行

编译并运行测试案例,在项目根目录下运行:

cd spring-boot-starter
./gradlew build
./gradlew test

web3sdk 是什么 Spring Boot Starter怎么用_fisco bcos_06

当所有测试案例运行成功,则代表

  • 区块链运行正常,
  • 该项目通过SDK连接区块链正常。

web3sdk 是什么 Spring Boot Starter怎么用_java_07

可以通过这个网址看错误报告file:///home/shijianfeng/fisco/spring-boot-starter/build/reports/tests/test/index.html

开发者可以基于该项目进行具体应用开发。

注:如果在IntelliJ IDEA或Eclipse中运行该demo工程,则使用gradle wrapper模式,此外IntelliJ IDEA需要在设置中开启​​Annotation Processors​​功能。

Web3SDK - 常见的异常启动问题​错误查看​​https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/faq/sdk.html?highlight=Web3SDK​​


测试案例介绍

该示例项目提供的测试案例,供开发者参考使用。测试案例主要分为对​​Web3j API​​​,​​Precompiled Serveice API​​、Solidity合约文件转Java合约文件、部署和调用合约的测试。


Web3j API测试

提供Web3jApiTest测试类测试Web3j API。示例测试如下:

@Test
public void getBlockNumber() throws IOException {
BigInteger blockNumber = web3j.getBlockNumber().send().getBlockNumber();
System.out.println(blockNumber);
assertTrue(blockNumber.compareTo(new BigInteger("0"))>= 0);
}

温馨提示:​ Application类初始化了web3j对象,在业务代码需要的地方可用注解的方式直接使用,使用方式如下:

@Autowired
private Web3j web3j


Precompiled Service API测试

提供PrecompiledServiceApiTest测试类测试Precompiled Service API。示例测试如下:

@Test
public void testSystemConfigService() throws Exception {
SystemConfigSerivce systemConfigSerivce = new SystemConfigSerivce(web3j, credentials);
systemConfigSerivce.setValueByKey("tx_count_limit", "2000");
String value = web3j.getSystemConfigByKey("tx_count_limit").send().getSystemConfigByKey();
System.out.println(value);
assertTrue("2000".equals(value));
}


Solidity合约文件转Java合约文件测试

提供SolidityFunctionWrapperGeneratorTest测试类测试Solidity合约文件转Java合约文件。示例测试如下:

@Test
public void compileSolFilesToJavaTest() throws IOException {
File solFileList = new File("src/test/resources/contract");
File[] solFiles = solFileList.listFiles();

for (File solFile : solFiles) {

SolidityCompiler.Result res = SolidityCompiler.compile(solFile, true, ABI, BIN, INTERFACE, METADATA);
System.out.println("Out: '" + res.output + "'");
System.out.println("Err: '" + res.errors + "'");
CompilationResult result = CompilationResult.parse(res.output);
System.out.println("contractname " + solFile.getName());
Path source = Paths.get(solFile.getPath());
String contractname = solFile.getName().split("\\.")[0];
CompilationResult.ContractMetadata a = result.getContract(solFile.getName().split("\\.")[0]);
System.out.println("abi " + a.abi);
System.out.println("bin " + a.bin);
FileUtils.writeStringToFile(new File("src/test/resources/solidity/" + contractname + ".abi"), a.abi);
FileUtils.writeStringToFile(new File("src/test/resources/solidity/" + contractname + ".bin"), a.bin);
String binFile;
String abiFile;
String tempDirPath = new File("src/test/java/").getAbsolutePath();
String packageName = "org.fisco.bcos.temp";
String filename = contractname;
abiFile = "src/test/resources/solidity/" + filename + ".abi";
binFile = "src/test/resources/solidity/" + filename + ".bin";
SolidityFunctionWrapperGenerator.main(Arrays.asList(
"-a", abiFile,
"-b", binFile,
"-p", packageName,
"-o", tempDirPath
).toArray(new String[0]));
}
System.out.println("generate successfully");
}

该测试案例将src/test/resources/contract目录下的所有Solidity合约文件(默认提供HelloWorld合约)均转为相应的abi和bin文件,保存在src/test/resources/solidity目录下。然后将abi文件和对应的bin文件组合转换为Java合约文件,保存在src/test/java/org/fisco/bcos/temp目录下。SDK将利用Java合约文件进行合约部署与调用。


部署和调用合约测试

提供ContractTest测试类测试部署和调用合约。示例测试如下:

@Test
public void deployAndCallHelloWorld() throws Exception {
//deploy contract
HelloWorld helloWorld = HelloWorld.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
if (helloWorld != null) {
System.out.println("HelloWorld address is: " + helloWorld.getContractAddress());
//call set function
helloWorld.set("Hello, World!").send();
//call get function
String result = helloWorld.get().send();
System.out.println(result);
assertTrue( "Hello, World!".equals(result));
}
}

​​https://github.com/FISCO-BCOS/spring-boot-starter/blob/master/doc/README_CN.md​​


举报

相关推荐

0 条评论