mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-23 01:55:10 +00:00
Can implement a simple CURD
Leave the serialization of parameters to the upper level Signed-off-by: D <gvssimux@qq.com> Co-authored-by: D <gvssimux@qq.com>
This commit is contained in:
parent
29ff95e2c6
commit
525a39fadb
5 changed files with 387 additions and 0 deletions
138
chaincode/GeneralChainCode/java/pom.xml
Normal file
138
chaincode/GeneralChainCode/java/pom.xml
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>com.gvssimux</groupId>
|
||||
<artifactId>FabricGeneralChainCode</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>aliyun</id>
|
||||
<url>https://maven.aliyun.com/repository/public</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<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>
|
||||
<dependency>
|
||||
<groupId>org.hyperledger.fabric-chaincode-java</groupId>
|
||||
<artifactId>fabric-chaincode-shim</artifactId>
|
||||
<version>2.4.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hyperledger.fabric-chaincode-java</groupId>
|
||||
<artifactId>fabric-chaincode-protos</artifactId>
|
||||
<version>2.4.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.22</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>4.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>1.15</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.79</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.owlike</groupId>
|
||||
<artifactId>genson</artifactId>
|
||||
<version>1.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</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.hyperledger.fabric.contract.ContractRouter</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
<filters>
|
||||
<filter>
|
||||
<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>
|
||||
100
chaincode/GeneralChainCode/java/src/main/java/ChainCode.java
Normal file
100
chaincode/GeneralChainCode/java/src/main/java/ChainCode.java
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import lombok.extern.java.Log;
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
import org.hyperledger.fabric.contract.Context;
|
||||
import org.hyperledger.fabric.contract.ContractInterface;
|
||||
import org.hyperledger.fabric.contract.annotation.*;
|
||||
import org.hyperledger.fabric.shim.ChaincodeStub;
|
||||
import org.hyperledger.fabric.shim.ledger.KeyValue;
|
||||
import org.hyperledger.fabric.shim.ledger.QueryResultsIterator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Contract(
|
||||
name = "GeneralChainCode",
|
||||
info = @Info(
|
||||
title = "GeneralChainCode contract",
|
||||
description = "The hyperlegendary FabricGeneralChainCode contract",
|
||||
version = "0.0.1-SNAPSHOT",
|
||||
license = @License(
|
||||
name = "Apache 2.0 License",
|
||||
url = "http://www.apache.org/licenses/LICENSE-2.0.html"),
|
||||
contact = @Contact(
|
||||
email = "gvssimux@qq.com",
|
||||
name = "Shanzj",
|
||||
url = "www.gvssimux.com")))
|
||||
@Default
|
||||
@Log
|
||||
public class ChainCode implements ContractInterface {
|
||||
|
||||
@Transaction
|
||||
public void initLedger(final Context ctx) {
|
||||
String value = "{\n" +
|
||||
" \"name\":\"GeneralChainCode\",\n" +
|
||||
" \"email\":\"gvssimux@qq.com\",\n" +
|
||||
" \"author\":\"ShanZJ\",\n" +
|
||||
" \"url\":\"www.gvssimux.com\"\n" +
|
||||
'}';
|
||||
ChaincodeStub stub = ctx.getStub();
|
||||
stub.putStringState("FabricGeneralChainCode" , value);
|
||||
}
|
||||
|
||||
@Transaction
|
||||
public static QueryResultList richQuery(final Context ctx, String s) {
|
||||
return InvokingMethod.richQuery(ctx,s);
|
||||
}
|
||||
|
||||
@Transaction
|
||||
public static QueryResultList queryAllByKey(final Context ctx, final String key){
|
||||
return InvokingMethod.queryAllByKey(ctx,key);
|
||||
}
|
||||
|
||||
@Transaction
|
||||
public String queryData(final Context ctx, final String key) {
|
||||
return InvokingMethod.queryData(ctx,key);
|
||||
}
|
||||
|
||||
@Transaction
|
||||
public static String createData(final Context ctx, String key, String jsonData) throws ClassNotFoundException {
|
||||
return InvokingMethod.createData(ctx,key,jsonData);
|
||||
}
|
||||
@Transaction
|
||||
public static String deleteData(final Context ctx, final String key){
|
||||
return InvokingMethod.deleteData(ctx,key);
|
||||
}
|
||||
|
||||
@Transaction
|
||||
public static String updateData(final Context ctx, final String key,String value){
|
||||
return InvokingMethod.updateData(ctx,key,value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void beforeTransaction(Context ctx) {
|
||||
log.info("*************************************** beforeTransaction ***************************************");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterTransaction(Context ctx, Object result) {
|
||||
log.info("*************************************** afterTransaction ***************************************");
|
||||
System.out.println("result --------> " + result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import com.google.inject.internal.util.Lists;
|
||||
import lombok.extern.java.Log;
|
||||
import org.apache.commons.collections4.IterableUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hyperledger.fabric.contract.Context;
|
||||
import org.hyperledger.fabric.contract.annotation.Transaction;
|
||||
import org.hyperledger.fabric.shim.ChaincodeException;
|
||||
import org.hyperledger.fabric.shim.ChaincodeStub;
|
||||
import org.hyperledger.fabric.shim.ledger.KeyValue;
|
||||
import org.hyperledger.fabric.shim.ledger.QueryResultsIterator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Log
|
||||
public class InvokingMethod {
|
||||
|
||||
@Transaction
|
||||
public static QueryResultList richQuery(final Context ctx, String s) {
|
||||
QueryResultList resultList = new QueryResultList();
|
||||
QueryResultsIterator<KeyValue> queryResult = ctx.getStub().getQueryResult(s);
|
||||
List<QueryResult> results = Lists.newArrayList();
|
||||
if (! IterableUtils.isEmpty(queryResult)) {
|
||||
for (KeyValue kv : queryResult) {
|
||||
QueryResult Result = new QueryResult();
|
||||
Result.setKey(kv.getKey());
|
||||
Result.setJson(kv.getStringValue());
|
||||
results.add(Result);
|
||||
}
|
||||
resultList.setResultList(results);
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@Transaction()
|
||||
public static QueryResultList queryAllByKey(final Context ctx, String key) {
|
||||
ChaincodeStub stub = ctx.getStub();
|
||||
final String startKey = key+"1";
|
||||
final String endKey = key+"99";
|
||||
QueryResultList resultList = new QueryResultList();
|
||||
QueryResultsIterator<KeyValue> queryResult = stub.getStateByRange(startKey, endKey);
|
||||
List<QueryResult> results = Lists.newArrayList();
|
||||
if (! IterableUtils.isEmpty(queryResult)) {
|
||||
for (KeyValue kv: queryResult) {
|
||||
QueryResult Result = new QueryResult();
|
||||
Result.setKey(kv.getKey());
|
||||
Result.setJson(kv.getStringValue());
|
||||
results.add(Result);
|
||||
}
|
||||
resultList.setResultList(results);
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Transaction
|
||||
public static String queryData(final Context ctx, final String key) {
|
||||
ChaincodeStub stub = ctx.getStub();
|
||||
String State = stub.getStringState(key);
|
||||
if (StringUtils.isBlank(State)) {
|
||||
String errorMessage = String.format("key: %s does not exist", key);
|
||||
System.out.println(errorMessage);
|
||||
throw new ChaincodeException(errorMessage);
|
||||
}
|
||||
return State;
|
||||
}
|
||||
|
||||
@Transaction
|
||||
public static String createData(final Context ctx, final String key,String json) {
|
||||
ChaincodeStub stub = ctx.getStub();
|
||||
String teaAreaState = stub.getStringState(key);
|
||||
if (StringUtils.isNotBlank(teaAreaState)) {
|
||||
String errorMessage = String.format("key: %s already exists", key);
|
||||
System.out.println(errorMessage);
|
||||
throw new ChaincodeException(errorMessage);
|
||||
}
|
||||
stub.putStringState(key, json);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Transaction
|
||||
public static String deleteData(final Context ctx,final String key){
|
||||
ChaincodeStub stub = ctx.getStub();
|
||||
String state = stub.getStringState(key);
|
||||
if (StringUtils.isBlank(state)){
|
||||
String errorMessage = String.format("Key %s does not exist",key);
|
||||
System.out.println(errorMessage);
|
||||
throw new ChaincodeException(errorMessage);
|
||||
}
|
||||
stub.delState(key);
|
||||
return state;
|
||||
}
|
||||
|
||||
@Transaction
|
||||
public static String updateData(final Context ctx, final String key,String json) {
|
||||
ChaincodeStub stub = ctx.getStub();
|
||||
String state = stub.getStringState(key);
|
||||
if (StringUtils.isBlank(state)) {
|
||||
String errorMessage = String.format("key: %s does exists", key);
|
||||
System.out.println(errorMessage);
|
||||
throw new ChaincodeException(errorMessage);
|
||||
}
|
||||
stub.putStringState(key, json);
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.hyperledger.fabric.contract.annotation.DataType;
|
||||
import org.hyperledger.fabric.contract.annotation.Property;
|
||||
|
||||
@DataType()
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class QueryResult {
|
||||
@Property()
|
||||
private String key;
|
||||
|
||||
@Property()
|
||||
private String json;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
import lombok.Data;
|
||||
import org.hyperledger.fabric.contract.annotation.DataType;
|
||||
import org.hyperledger.fabric.contract.annotation.Property;
|
||||
import java.util.List;
|
||||
|
||||
@DataType
|
||||
@Data
|
||||
public class QueryResultList {
|
||||
|
||||
@Property
|
||||
List<QueryResult> resultList;
|
||||
}
|
||||
Loading…
Reference in a new issue