forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
136 lines (109 loc) · 3.6 KB
/
map.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
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
// This chaincode implements a simple map that is stored in the state.
// The following operations are available.
// Invoke operations
// put - requires two arguments, a key and value
// remove - requires a key
// Query operations
// get - requires one argument, a key, and returns a value
// keys - requires no arguments, returns all keys
// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}
// Init is a no-op
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
return nil, nil
}
// Invoke has two functions
// put - takes two arguements, a key and value, and stores them in the state
// remove - takes one argument, a key, and removes if from the state
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
switch function {
case "put":
if len(args) < 2 {
return nil, errors.New("put operation must include two arguments, a key and value")
}
key := args[0]
value := args[1]
err := stub.PutState(key, []byte(value))
if err != nil {
fmt.Printf("Error putting state %s", err)
return nil, fmt.Errorf("put operation failed. Error updating state: %s", err)
}
return nil, nil
case "remove":
if len(args) < 1 {
return nil, errors.New("remove operation must include one argument, a key")
}
key := args[0]
err := stub.DelState(key)
if err != nil {
return nil, fmt.Errorf("remove operation failed. Error updating state: %s", err)
}
return nil, nil
default:
return nil, errors.New("Unsupported operation")
}
}
// Query has two functions
// get - takes one argument, a key, and returns the value for the key
// keys - returns all keys stored in this chaincode
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
switch function {
case "get":
if len(args) < 1 {
return nil, errors.New("get operation must include one argument, a key")
}
key := args[0]
value, err := stub.GetState(key)
if err != nil {
return nil, fmt.Errorf("get operation failed. Error accessing state: %s", err)
}
return value, nil
case "keys":
keysIter, err := stub.RangeQueryState("", "")
if err != nil {
return nil, fmt.Errorf("keys operation failed. Error accessing state: %s", err)
}
defer keysIter.Close()
var keys []string
for keysIter.HasNext() {
key, _, iterErr := keysIter.Next()
if iterErr != nil {
return nil, fmt.Errorf("keys operation failed. Error accessing state: %s", err)
}
keys = append(keys, key)
}
jsonKeys, err := json.Marshal(keys)
if err != nil {
return nil, fmt.Errorf("keys operation failed. Error marshaling JSON: %s", err)
}
return jsonKeys, nil
default:
return nil, errors.New("Unsupported operation")
}
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting chaincode: %s", err)
}
}