Skip to content

Commit 396e556

Browse files
committed
add chaincode
1 parent 4c0c934 commit 396e556

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
```bash
1010
├── pom.xml
11+
├── doc # 运行结果图片
12+
├── cc # 使用Go实现的链码
1113
├── src
1214
│   ├── main
1315
│   │   ├── java

cc/epointchaincodecommon.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/hyperledger/fabric/core/chaincode/shim"
7+
pb "github.com/hyperledger/fabric/protos/peer"
8+
)
9+
10+
type SimpleChaincode struct {
11+
}
12+
//记录当前链码上有多少个记录
13+
var RNO = 0
14+
var ChainCodeVersion = "v0.1"
15+
16+
type Record struct {
17+
Key string
18+
Value string
19+
}
20+
21+
/*
22+
智能合约初始化调用,一个链码版本只运行一次
23+
24+
*/
25+
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
26+
fmt.Println("Init ChainCode...............",ChainCodeVersion)
27+
return shim.Success(nil)
28+
}
29+
30+
/*
31+
智能合约发起请求时调用 {"Args":["invoke","a","b","10"]}
32+
func (stub *ChaincodeStub) GetFunctionAndParameters() (function string, params []string)
33+
返回参数 function 标记调用方法的名称"invoke"
34+
返回参数 params 标记调用方法的参数数组["a","b","10"]
35+
*/
36+
37+
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
38+
// 获取请求调用智能合约的方法和参数
39+
function, args := stub.GetFunctionAndParameters()
40+
fmt.Println("recive params....",function," ",args)
41+
// Route to the appropriate handler function to interact with the ledger appropriately
42+
if function == "addkv" {
43+
fmt.Println("run function addcard",function," ",args)
44+
return t.addkv(stub, args)
45+
} else if function == "updatekv" {
46+
fmt.Println("run function updatecard",function," ",args)
47+
return t.updatekv(stub, args)
48+
} else if function == "delkv" {
49+
fmt.Println("run function deletecard",function," ",args)
50+
return t.delkv(stub, args)
51+
} else if function == "query" {
52+
fmt.Println("run function query",function," ",args)
53+
return t.query(stub, args)
54+
} else if function == "queryhistory" {
55+
fmt.Println("run function queryhistory",function," ",args)
56+
return t.queryhistory(stub, args)
57+
}
58+
return shim.Success(nil)
59+
}
60+
61+
/*
62+
向链码中添加一个KV结构
63+
*/
64+
func (t *SimpleChaincode) addkv(stub shim.ChaincodeStubInterface, args []string) pb.Response {
65+
fmt.Println("向账本中添加k-v ",args[0],args[1])
66+
var pertmp Record
67+
pertmp = Record{Key: args[0], Value: args[1]}
68+
fmt.Println("Record is ",pertmp)
69+
jsonrecode,_ := json.Marshal(&pertmp)
70+
err:= stub.PutState(pertmp.Key, jsonrecode)
71+
fmt.Println("put key is ", pertmp.Key)
72+
if err != nil {
73+
return shim.Error("Error retrieving data")
74+
}
75+
return shim.Success(jsonrecode)
76+
}
77+
78+
/*
79+
向链码中已经存在的记录做一次更新操作
80+
81+
*/
82+
func (t *SimpleChaincode) updatekv(stub shim.ChaincodeStubInterface, args []string) pb.Response {
83+
t.addkv(stub,args)
84+
return shim.Success(nil)
85+
}
86+
87+
/*
88+
删除链码中的一个记录,调用DelState函数操作
89+
*/
90+
func (t *SimpleChaincode) delkv(stub shim.ChaincodeStubInterface, args []string) pb.Response {
91+
fmt.Println("删除账本中的一条数据 Keys:",args[0])
92+
deletekey:=args[0]
93+
err:=stub.DelState(deletekey)
94+
if err != nil {
95+
return shim.Error("Error retrieving data")
96+
}
97+
fmt.Println("删除成功")
98+
return shim.Success(nil)
99+
}
100+
101+
/*
102+
查询链码中的数据
103+
*/
104+
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
105+
fmt.Println("查询账本中的数据 Keys:",args[0])
106+
queryparams:=args[0]
107+
redBytes, err := stub.GetState(queryparams)
108+
if err != nil {
109+
shim.Error("Error retrieving data")
110+
}
111+
fmt.Println("query resoult"+string(redBytes))
112+
return shim.Success(redBytes)
113+
}
114+
/*
115+
根据给定的键key,查询该键的所有历史记录
116+
117+
*/
118+
func (t *SimpleChaincode) queryhistory(stub shim.ChaincodeStubInterface, args []string) pb.Response {
119+
var records []Record
120+
fmt.Println("query key histroy......................",args[0])
121+
historyIer, err := stub.GetHistoryForKey(args[0])
122+
if err != nil {
123+
fmt.Println(err)
124+
return shim.Error("query error")
125+
}
126+
for historyIer.HasNext() {
127+
var tmp Record=Record{}
128+
modification, err := historyIer.Next()
129+
if err != nil {
130+
fmt.Println(err)
131+
return shim.Error("query error")
132+
}
133+
fmt.Println("Returning information about", string(modification.Value))
134+
err=json.Unmarshal(modification.Value,&tmp)
135+
records=append(records,tmp)
136+
}
137+
jsonrecode,_ := json.Marshal(&records)
138+
return shim.Success(jsonrecode)
139+
}
140+
func main() {
141+
fmt.Println("Fabric Epoint Common Chain Code ..........")
142+
err := shim.Start(new(SimpleChaincode))
143+
if err != nil {
144+
fmt.Printf("Error starting Simple chaincode: %s", err)
145+
}
146+
}

0 commit comments

Comments
 (0)