Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit396e556

Browse files
committed
add chaincode
1 parent4c0c934 commit396e556

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+
typeSimpleChaincodestruct {
11+
}
12+
//记录当前链码上有多少个记录
13+
varRNO=0
14+
varChainCodeVersion="v0.1"
15+
16+
typeRecordstruct {
17+
Keystring
18+
Valuestring
19+
}
20+
21+
/*
22+
智能合约初始化调用,一个链码版本只运行一次
23+
24+
*/
25+
func (t*SimpleChaincode)Init(stub shim.ChaincodeStubInterface) pb.Response {
26+
fmt.Println("Init ChainCode...............",ChainCodeVersion)
27+
returnshim.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+
iffunction=="addkv" {
43+
fmt.Println("run function addcard",function," ",args)
44+
returnt.addkv(stub,args)
45+
}elseiffunction=="updatekv" {
46+
fmt.Println("run function updatecard",function," ",args)
47+
returnt.updatekv(stub,args)
48+
}elseiffunction=="delkv" {
49+
fmt.Println("run function deletecard",function," ",args)
50+
returnt.delkv(stub,args)
51+
}elseiffunction=="query" {
52+
fmt.Println("run function query",function," ",args)
53+
returnt.query(stub,args)
54+
}elseiffunction=="queryhistory" {
55+
fmt.Println("run function queryhistory",function," ",args)
56+
returnt.queryhistory(stub,args)
57+
}
58+
returnshim.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+
varpertmpRecord
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+
iferr!=nil {
73+
returnshim.Error("Error retrieving data")
74+
}
75+
returnshim.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+
returnshim.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+
iferr!=nil {
95+
returnshim.Error("Error retrieving data")
96+
}
97+
fmt.Println("删除成功")
98+
returnshim.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+
iferr!=nil {
109+
shim.Error("Error retrieving data")
110+
}
111+
fmt.Println("query resoult"+string(redBytes))
112+
returnshim.Success(redBytes)
113+
}
114+
/*
115+
根据给定的键key,查询该键的所有历史记录
116+
117+
*/
118+
func (t*SimpleChaincode)queryhistory(stub shim.ChaincodeStubInterface,args []string) pb.Response {
119+
varrecords []Record
120+
fmt.Println("query key histroy......................",args[0])
121+
historyIer,err:=stub.GetHistoryForKey(args[0])
122+
iferr!=nil {
123+
fmt.Println(err)
124+
returnshim.Error("query error")
125+
}
126+
forhistoryIer.HasNext() {
127+
vartmpRecord=Record{}
128+
modification,err:=historyIer.Next()
129+
iferr!=nil {
130+
fmt.Println(err)
131+
returnshim.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+
returnshim.Success(jsonrecode)
139+
}
140+
funcmain() {
141+
fmt.Println("Fabric Epoint Common Chain Code ..........")
142+
err:=shim.Start(new(SimpleChaincode))
143+
iferr!=nil {
144+
fmt.Printf("Error starting Simple chaincode: %s",err)
145+
}
146+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp