-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkvstore-multi-ops.go
104 lines (83 loc) · 2.43 KB
/
kvstore-multi-ops.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
package main
import (
"errors"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
type KVStore struct {
}
func main() {
err := shim.Start(new(KVStore))
if err != nil {
fmt.Printf("Error starting kv-store: %s", err)
}
}
// Init the kv-store
func (t *KVStore) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
return nil, nil
}
func (t *KVStore) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if function == "write" {
return t.write(stub, args)
} else if function == "delete" {
return t.del(stub, args)
}
return nil, errors.New("Received unknown function invocation")
}
func (t *KVStore) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
fmt.Println("query is running " + function)
if function == "read" {
return t.read(stub, args)
}
return nil, errors.New("Received unknown function query")
}
func (t *KVStore) write(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var key, value string
var err error
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2. name of the key and value to set")
}
key = args[0]
value = args[1]
err = stub.PutState(key, []byte(value))
_, err = stub.GetState(key)
err = stub.PutState(key+"1", []byte(value))
_, err = stub.GetState(key + "1")
err = stub.PutState(key+"2", []byte(value))
_, err = stub.GetState(key + "2")
err = stub.PutState(key+"3", []byte(value))
_, err = stub.GetState(key + "3")
err = stub.PutState(key+"4", []byte(value))
_, err = stub.GetState(key + "4")
if err != nil {
return nil, err
}
return nil, nil
}
func (t *KVStore) del(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var key string
var err error
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting name of the key to delete")
}
key = args[0]
err = stub.DelState(key)
if err != nil {
return nil, err
}
return nil, nil
}
func (t *KVStore) read(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
var key, jsonResp string
var err error
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting name of the key to query")
}
key = args[0]
valAsbytes, err := stub.GetState(key)
if err != nil {
jsonResp = "{\"Error\":\"Failed to get state for " + key + "\"}"
return nil, errors.New(jsonResp)
}
return valAsbytes, nil
}