-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaincode.go
215 lines (163 loc) · 7.36 KB
/
chaincode.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
"os"
"strconv"
)
var logger = shim.NewLogger("fabric-boilerplate")
//==============================================================================================================================
// Structure Definitions
//==============================================================================================================================
// SimpleChaincode - A blank struct for use with Shim (An IBM Blockchain included go file used for get/put state
// and other IBM Blockchain functions)
//==============================================================================================================================
type SimpleChaincode struct {
}
//==============================================================================================================================
// Index string & Constants
//==============================================================================================================================
var companiesIndexStr = "_companies"
var mappCodesIndexStr = "_mappCodes"
var indexes = []string{companiesIndexStr, mappCodesIndexStr}
//==============================================================================================================================
// Invoke - Called on chaincode invoke. Takes a function name passed and calls that function. Passes the
// initial arguments passed are passed on to the called function.
//==============================================================================================================================
func (t *SimpleChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
logger.Infof("Invoke is running " + function)
if function == "init" {
return t.Init(stub, "init", args)
} else if function == "reset_indexes" {
return t.reset_indexes(stub, args)
} else if function == "add_company" {
return t.add_company(stub, args)
}
return nil, errors.New("Received unknown invoke function name")
}
//=================================================================================================================================
// Query - Called on chaincode query. Takes a function name passed and calls that function. Passes the
// initial arguments passed are passed on to the called function.
//=================================================================================================================================
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
logger.Infof("Query is running " + function)
if function == "get_company" {
return t.get_company(stub, args)
} else if function == "get_all_companies" {
return t.get_all_companies(stub, args)
}
return nil, errors.New("Received unknown query function name")
}
//=================================================================================================================================
// Main - main - Starts up the chaincode
//=================================================================================================================================
func main() {
// LogDebug, LogInfo, LogNotice, LogWarning, LogError, LogCritical (Default: LogDebug)
logger.SetLevel(shim.LogInfo)
logLevel, _ := shim.LogLevel(os.Getenv("SHIM_LOGGING_LEVEL"))
shim.SetLoggingLevel(logLevel)
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting SimpleChaincode: %s", err)
}
}
//==============================================================================================================================
// Init Function - Called when the user deploys the chaincode
//==============================================================================================================================
func (t *SimpleChaincode) Init(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
return nil, nil
}
//==============================================================================================================================
// Utility Functions
//==============================================================================================================================
//==============================================================================================================================
// Invoke Functions
//==============================================================================================================================
func (t *SimpleChaincode) reset_indexes(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
for _, i := range indexes {
// Marshal the index
var emptyIndex []string
empty, err := json.Marshal(emptyIndex)
if err != nil {
return nil, errors.New("Error marshalling")
}
err = stub.PutState(i, empty)
if err != nil {
return nil, errors.New("Error deleting index")
}
logger.Infof("Delete with success from ledger: " + i)
}
return nil, nil
}
func append_id(stub *shim.ChaincodeStub, indexStr string, id string, create bool) ([]byte, error) {
indexAsBytes, err := stub.GetState(indexStr)
if err != nil {
return nil, errors.New("Failed to get " + indexStr)
}
// Unmarshal the index
var tmpIndex []string
json.Unmarshal(indexAsBytes, &tmpIndex)
// Create new id
var newId = id
if create {
newId += strconv.Itoa(len(tmpIndex) + 1)
}
// append the new id to the index
tmpIndex = append(tmpIndex, newId)
jsonAsBytes, _ := json.Marshal(tmpIndex)
err = stub.PutState(indexStr, jsonAsBytes)
if err != nil {
return nil, errors.New("Error storing new " + indexStr + " into ledger")
}
return []byte(newId), nil
}
func (t *SimpleChaincode) add_company(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
//Args
// 0 1
// index company JSON object (as string)
id, err := append_id(stub, companiesIndexStr, args[0], false)
if err != nil {
return nil, errors.New("Error creating new id for user " + args[0])
}
err = stub.PutState(string(id), []byte(args[1]))
if err != nil {
return nil, errors.New("Error putting user data on ledger")
}
return nil, nil
}
//==============================================================================================================================
// Query Functions
//==============================================================================================================================
func (t *SimpleChaincode) get_company(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
bytes, err := stub.GetState(args[0])
if err != nil {
return nil, errors.New("Could not retrieve information for the given company ID")
}
return bytes, nil
}
func (t *SimpleChaincode) get_all_companies(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
indexAsBytes, err := stub.GetState(companiesIndexStr)
if err != nil {
return nil, errors.New("Failed to get " + companiesIndexStr)
}
// Unmarshal the index
var companyIndex []string
json.Unmarshal(indexAsBytes, &companyIndex)
var companies []Company
for _, company := range companyIndex {
bytes, err := stub.GetState(company)
if err != nil {
return nil, errors.New("Unable to get thing with ID: " + company)
}
var c Company
json.Unmarshal(bytes, &c)
companies = append(companies, c)
}
companiesAsJsonBytes, _ := json.Marshal(companies)
if err != nil {
return nil, errors.New("Could not convert things to JSON ")
}
return companiesAsJsonBytes, nil
}