forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chaincode.go
56 lines (44 loc) · 1.2 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package caller
import (
"github.com/hyperledger/fabric-chaincode-go/shim"
pb "github.com/hyperledger/fabric-protos-go/peer"
)
// CC example simple Chaincode implementation
type CC struct {
}
func (t *CC) Init(stub shim.ChaincodeStubInterface) pb.Response {
err := stub.PutState("foo", []byte("caller:foo"))
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *CC) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
fn, args := stub.GetFunctionAndParameters()
switch fn {
case "INVOKE":
err := stub.PutState("foo", []byte("caller:bar"))
if err != nil {
return shim.Error(err.Error())
}
return stub.InvokeChaincode(string(args[0]), [][]byte{[]byte("INVOKE")}, "")
case "QUERYCALLEE":
response := stub.InvokeChaincode(string(args[0]), [][]byte{[]byte("QUERY")}, args[1])
if response.Status >= 400 {
return shim.Error(response.Message)
}
return shim.Success(response.Payload)
case "QUERY":
val, err := stub.GetState("foo")
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(val)
default:
return shim.Error("unknown function")
}
}