智能合约
package main
import (
"errors"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
type SaveState1Chaincode struct {
}
func (t *SaveState1Chaincode) init(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
fmt.Printf("Init called with function %s!\n", function)
}
func (t *SaveState1Chaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var key, value string
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
key = args[0]
value = args[1]
var err error
err = stub.PutState(key, []byte(value))
if err == nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SaveState1Chaincode) query(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var key string
if len(args) != 1{
return nil, err
}
key = args[0]
valInBytes, err := stub.GetState(key)
if err != nil {
return nil, errors.New("Failed to get state for " + key)
}
message := "State for " + key + " = " + string(valInBytes)
return []byte(message), nil;
}
func main() {
err := shim.Start(new(SaveState1Chaincode))
if err != nil {
fmt.Printf("Error starting Save State chaincode: %s", err)
}
}
java代码
package com.hyperledger.fabric.sdk.handler;
import static com.hyperledger.fabric.sdk.common.Config.PEER0_ORG1_EVENT_URL;
import static com.hyperledger.fabric.sdk.common.Config.PEER0_ORG1_GRPC_URL;
import static com.hyperledger.fabric.sdk.common.Config.PEER0_ORG1_NAME;
import java.util.ArrayList;
import java.util.Collection;
import org.hyperledger.fabric.sdk.ChaincodeID;
import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.HFClient;
import com.hyperledger.fabric.sdk.entity.dto.api.BuildClientDTO;
import com.hyperledger.fabric.sdk.entity.dto.api.ExecuteCCDTO;
import com.hyperledger.fabric.sdk.entity.dto.api.InstallCCDTO;
import com.hyperledger.fabric.sdk.entity.dto.api.PeerNodeDTO;
public class Installchaincode {
public static void main(String[] args) throws Exception {
String cxtPath = APITest.class.getClassLoader().getResource("").getPath();
/* 通道名称 */
String channelName = "mychannel";
/* 智能合约配置信息 */
String chaincodeName = "hashstore1";
String chaincodeVersion = "1.0";
String chaincodePath = "github.com/testchaincode";
ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(chaincodeName).setVersion(chaincodeVersion).setPath(chaincodePath).build();
System.out.println("1---start");
// 1. 初始化客户端
String mspPath = "crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/";
BuildClientDTO buildClientDTO = new BuildClientDTO.Builder()
.name("org1.example.com").mspId("Org1MSP").mspPath(mspPath).build();
HFClient client = ApiHandler.clientBuild(buildClientDTO);
System.out.println("1---end---2---start");
// 2. 定义peer
Collection<PeerNodeDTO> peerNodeDTOS = new ArrayList<>();
peerNodeDTOS.add(new PeerNodeDTO(PEER0_ORG1_NAME, PEER0_ORG1_GRPC_URL, PEER0_ORG1_EVENT_URL));
System.out.println("2---end---3---start");
// 3. 安装智能合约
InstallCCDTO installCCDTO = new InstallCCDTO.Builder().chaincodeID(chaincodeID).chaincodeSourceLocation(cxtPath + "chaincodes/sample").peerNodeDTOS(peerNodeDTOS).build();
ApiHandler.installChainCode(client, installCCDTO);
System.out.println("3---end---4---start");
// 4 创建通道
Channel channel = ApiHandler.createChannel(client, channelName);
System.out.println("4---end---5---start");
// 5. 测试智能合约invoke
ExecuteCCDTO invokeCCDTO = new ExecuteCCDTO.Builder().funcName("invoke").params(new String[] {"hn", "haonan"}).chaincodeID(chaincodeID).build();
ApiHandler.invokeChainCode(client, channel, invokeCCDTO);
System.out.println("5---end---6---start");
// 5. 测试智能合约query
ExecuteCCDTO invokeCCDTO1 = new ExecuteCCDTO.Builder().funcName("query").params(new String[] {"hn"}).chaincodeID(chaincodeID).build();
ApiHandler.queryChainCode(client, channel, invokeCCDTO1);
System.out.println("6---end");
}
}
是在你写的例子基础上我自己书写的一个类。
如果可以最好给我个例子看一下。