-
Notifications
You must be signed in to change notification settings - Fork 0
/
File_onChain.go
65 lines (55 loc) · 1.51 KB
/
File_onChain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
type File_onChain struct{
Filename string `json:"Filename"`
File_branch string `json:"File_branch"`
Access_time string `json:"Access_time"`
Visitor string `json:"Visitor"`
Visitor_branch string `json:"Visitor_branch"`
Visitor_role string `json:"Visitor_role"`
}
func (t *File_onChain) Init (stub shim.ChaincodeStubInterface) pb.Response{
return shim.Success(nil)
}
func (t *File_onChain) Invoke(stub shim.ChaincodeStubInterface) pb.Response{
funcName,args := stub.GetFunctionAndParameters()
if(funcName=="save"){
return t.saveBasic(stub,args)
}else if(funcName=="query"){
return t.queryBasic(stub,args)
}else{
return shim.Error("no such function")
}
}
func (t *File_onChain) saveBasic(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if(len(args)!=2){
return shim.Error("except five args")
}else{
err:=stub.PutState(args[0],[]byte(args[1]))
if(err!=nil) {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
}
func (t *File_onChain) queryBasic(stub shim.ChaincodeStubInterface, args []string) pb.Response{
if(len(args)!=1){
return shim.Error("except one arg")
}else{
value,err :=stub.GetState(args[0])
if(err!=nil){
shim.Error("no data found")
}
return shim.Success(value)
}
}
func main(){
err:=shim.Start(new(File_onChain))
if(err!=nil){
fmt.Println("emr File_onChain chaincode start error")
}
}