-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathycsb.go
150 lines (121 loc) · 3.52 KB
/
ycsb.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"crypto/sha512"
"encoding/hex"
"strconv"
"strings"
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
)
const ERROR_UNKNOWN_FUNC = "Unknown function"
const ERROR_WRONG_ARGS = "Wrong arguments of function"
const ERROR_SYSTEM = "System exception"
const ERR_NOT_FOUND = "Could not find specified Key"
const ERROR_PUT_STATE = "Failed to put state"
var namespace = hexdigest("ycsb")[:6]
type YCSBChaincode struct {
}
func (t *YCSBChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success(nil)
}
func (t *YCSBChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
switch function {
case "insert":
return t.Insert(stub, args)
case "update":
return t.Update(stub, args)
case "readmodifywrite":
return t.ReadModifyWrite(stub, args)
case "remove":
return t.Delete(stub, args)
case "query":
return t.Read(stub, args)
case "multirw":
return t.MultiRW(stub, args)
case "empty":
return t.Empty(stub, args)
default:
return errormsg(ERROR_UNKNOWN_FUNC + ": " + function)
}
}
func (t *YCSBChaincode) Empty(stub shim.ChaincodeStubInterface, args []string) pb.Response {
return shim.Success(nil)
}
func (t *YCSBChaincode) Insert(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 2 {
return errormsg(ERROR_WRONG_ARGS + " Insert")
}
err := stub.PutState(args[0], []byte(args[1]))
if err != nil {
return systemerror(err.Error())
}
return shim.Success(nil)
}
func (t *YCSBChaincode) Update(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 2 {
return errormsg(ERROR_WRONG_ARGS + " Update")
}
err := stub.PutState(args[0], []byte(args[1]))
if err != nil {
return systemerror(err.Error())
}
return shim.Success(nil)
}
func (t *YCSBChaincode) ReadModifyWrite(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 2 {
return errormsg(ERROR_WRONG_ARGS + " ReadModifyWrite")
}
valBytes, err := stub.GetState(args[0])
if err != nil {
return systemerror(err.Error())
}
err = stub.PutState(args[0], []byte(args[1]))
if err != nil {
return systemerror(err.Error())
}
return shim.Success(valBytes)
}
func (t *YCSBChaincode) Delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 { // should be [key]
return errormsg(ERROR_WRONG_ARGS + " Delete")
}
err := stub.PutState(args[0], []byte(""))
if err != nil {
return systemerror(err.Error())
}
return shim.Success(nil)
}
func (t *YCSBChaincode) Read(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 { // should be [key]
return errormsg(ERROR_WRONG_ARGS + " Read")
}
valBytes, err := stub.GetState(args[0])
if err != nil {
return systemerror(err.Error())
}
return shim.Success(valBytes)
}
func (t *YCSBChaincode) MultiRW(stub shim.ChaincodeStubInterface, args []string) pb.Response {
arg_count, _ := strconv.Atoi(args[0])
for i := 1; i <= arg_count; i++ {
valBytes, _ := stub.GetState(args[i])
_ = stub.PutState(args[i], valBytes)
}
return shim.Success(nil)
}
func main() {
_ = shim.Start(new(YCSBChaincode))
}
func errormsg(msg string) pb.Response {
return shim.Error("{\"error\":" + msg + "}")
}
func systemerror(err string) pb.Response {
return errormsg(ERROR_SYSTEM + ":" + err)
}
func hexdigest(str string) string {
hash := sha512.New()
hash.Write([]byte(str))
hashBytes := hash.Sum(nil)
return strings.ToLower(hex.EncodeToString(hashBytes))
}