-
Notifications
You must be signed in to change notification settings - Fork 145
/
main.go
136 lines (108 loc) · 4.22 KB
/
main.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
package main
import (
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
// HeroesServiceChaincode implementation of Chaincode
type HeroesServiceChaincode struct {
}
// Init of the chaincode
// This function is called only one when the chaincode is instantiated.
// So the goal is to prepare the ledger to handle future requests.
func (t *HeroesServiceChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("########### HeroesServiceChaincode Init ###########")
// Get the function and arguments from the request
function, _ := stub.GetFunctionAndParameters()
// Check if the request is the init function
if function != "init" {
return shim.Error("Unknown function call")
}
// Put in the ledger the key/value hello/world
err := stub.PutState("hello", []byte("world"))
if err != nil {
return shim.Error(err.Error())
}
// Return a successful message
return shim.Success(nil)
}
// Invoke
// All future requests named invoke will arrive here.
func (t *HeroesServiceChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("########### HeroesServiceChaincode Invoke ###########")
// Get the function and arguments from the request
function, args := stub.GetFunctionAndParameters()
// Check whether it is an invoke request
if function != "invoke" {
return shim.Error("Unknown function call")
}
// Check whether the number of arguments is sufficient
if len(args) < 1 {
return shim.Error("The number of arguments is insufficient.")
}
// In order to manage multiple type of request, we will check the first argument.
// Here we have one possible argument: query (every query request will read in the ledger without modification)
if args[0] == "query" {
return t.query(stub, args)
}
// The update argument will manage all update in the ledger
if args[0] == "invoke" {
return t.invoke(stub, args)
}
// If the arguments given don’t match any function, we return an error
return shim.Error("Unknown action, check the first argument")
}
// query
// Every readonly functions in the ledger will be here
func (t *HeroesServiceChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
fmt.Println("########### HeroesServiceChaincode query ###########")
// Check whether the number of arguments is sufficient
if len(args) < 2 {
return shim.Error("The number of arguments is insufficient.")
}
// Like the Invoke function, we manage multiple type of query requests with the second argument.
// We also have only one possible argument: hello
if args[1] == "hello" {
// Get the state of the value matching the key hello in the ledger
state, err := stub.GetState("hello")
if err != nil {
return shim.Error("Failed to get state of hello")
}
// Return this value in response
return shim.Success(state)
}
// If the arguments given don’t match any function, we return an error
return shim.Error("Unknown query action, check the second argument.")
}
// invoke
// Every functions that read and write in the ledger will be here
func (t *HeroesServiceChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
fmt.Println("########### HeroesServiceChaincode invoke ###########")
if len(args) < 2 {
return shim.Error("The number of arguments is insufficient.")
}
// Check if the ledger key is "hello" and process if it is the case. Otherwise it returns an error.
if args[1] == "hello" && len(args) == 3 {
// Write the new value in the ledger
err := stub.PutState("hello", []byte(args[2]))
if err != nil {
return shim.Error("Failed to update state of hello")
}
// Notify listeners that an event "eventInvoke" have been executed (check line 19 in the file invoke.go)
err = stub.SetEvent("eventInvoke", []byte{})
if err != nil {
return shim.Error(err.Error())
}
// Return this value in response
return shim.Success(nil)
}
// If the arguments given don’t match any function, we return an error
return shim.Error("Unknown invoke action, check the second argument.")
}
func main() {
// Start the chaincode and make it ready for futures requests
err := shim.Start(new(HeroesServiceChaincode))
if err != nil {
fmt.Printf("Error starting Heroes Service chaincode: %s", err)
}
}