Fabric2.0 Java链码开发实战(Maven工程)
本文主要介绍如何编写Fabric 2.0的Java链码,选用的是Maven工程(Gradle工程会有所不同)
基础环境
jdk:1.8
maven:3.6.3
fabric java:2.0.1
创建工程
创建maven项目,Archetype选择quickstart即可。
修改pom.xml,注意配置mainClass(值为定义的包名.类名)
<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>maxisvest</groupId>
<artifactId>fabric-java-chaincode-sample</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<fabric-chaincode-java.version>2.0.1</fabric-chaincode-java.version>
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!– Test –>
<junit.version>4.11</junit.version>
</properties>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://www.jitpack.io</url>
</repository>
<repository>
<id>artifactory</id>
<url>https://hyperledger.jfrog.io/hyperledger/fabric-maven</url>
</repository>
</repositories>
<dependencies>
<!– fabric-chaincode-java –>
<dependency>
<groupId>org.hyperledger.fabric-chaincode-java</groupId>
<artifactId>fabric-chaincode-shim</artifactId>
<version>${fabric-chaincode-java.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hyperledger.fabric-chaincode-java</groupId>
<artifactId>fabric-chaincode-protos</artifactId>
<version>${fabric-chaincode-java.version}</version>
<scope>compile</scope>
</dependency>
<!– fabric-sdk-java –>
<!– Logging with SLF4J & LogBack –>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!– Test Artifacts –>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>chaincode</finalName>
<transformers>
<transformer
implementation=”org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”>
<mainClass>org.example.SimpleAssetChaincode</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<!– filter out signature files from signed dependencies, else repackaging fails with security ex –>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
编写链码SimpleAssetChaincode.java
package org.example;
import org.hyperledger.fabric.shim.ChaincodeBase;
import org.hyperledger.fabric.shim.ChaincodeStub;
import java.util.List;
public class SimpleAssetChaincode extends ChaincodeBase {
@Override
public Response init(ChaincodeStub stub) {
try {
// Get the args from the transaction proposal
List<String> args = stub.getStringArgs();
if (args.size() != 2) {
newErrorResponse(“Incorrect arguments. Expecting a key and a value”);
}
// Set up any variables or assets here by calling stub.putState()
// We store the key and the value on the ledger
stub.putStringState(args.get(0), args.get(1));
return newSuccessResponse();
} catch (Throwable e) {
return newErrorResponse(“Failed to create asset”);
}
}
@Override
public Response invoke(ChaincodeStub stub) {
try {
// Extract the function and args from the transaction proposal
String func = stub.getFunction();
List<String> params = stub.getParameters();
if (func.equals(“set”)) {
// Return result as success payload
return newSuccessResponse(set(stub, params));
} else if (func.equals(“get”)) {
// Return result as success payload
return newSuccessResponse(get(stub, params));
}
return newErrorResponse(“Invalid invoke function name. Expecting one of: [\”set\”, \”get\””);
} catch (Throwable e) {
return newErrorResponse(e.getMessage());
}
}
private String get(ChaincodeStub stub, List<String> args) {
if (args.size() != 1) {
throw new RuntimeException(“Incorrect arguments. Expecting a key”);
}
String value = stub.getStringState(args.get(0));
if (value == null || value.isEmpty()) {
throw new RuntimeException(“Asset not found: ” + args.get(0));
}
return value;
}
private String set(ChaincodeStub stub, List<String> args) {
if (args.size() != 2) {
throw new RuntimeException(“Incorrect arguments. Expecting a key and a value”);
}
stub.putStringState(args.get(0), args.get(1));
return args.get(1);
}
public static void main(String[] args) {
new SimpleAssetChaincode().start(args);
}
}
————————————————
版权声明:本文为CSDN博主「Ddkjawed」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43839871/article/details/107047015