-
Notifications
You must be signed in to change notification settings - Fork 24
/
samplesyscc2.go
executable file
·129 lines (111 loc) · 3.43 KB
/
samplesyscc2.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
/*
This file is part of go-palletone.
go-palletone is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
go-palletone is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with go-palletone. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright IBM Corp. All Rights Reserved.
* @author PalletOne core developers <dev@pallet.one>
* @date 2018
*/
package samplesyscc2
import (
"fmt"
"strconv"
"github.com/palletone/go-palletone/contracts/shim"
pb "github.com/palletone/go-palletone/core/vmContractPub/protos/peer"
)
//var instance SampleSysCC2
// SampleSysCC example simple Chaincode implementation
type SampleSysCC2 struct {
}
// Init initializes the sample system chaincode by storing the key and value
// arguments passed in as parameters
func (t *SampleSysCC2) Init(stub shim.ChaincodeStubInterface) pb.Response {
//as system chaincodes do not take part in consensus and are part of the system,
//best practice to do nothing (or very little) in Init.
//fmt.Println("***sample system contract init***")
return shim.Success(nil)
}
// Invoke gets the supplied key and if it exists, updates the key with the newly
// supplied value.
func (t *SampleSysCC2) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
f, args := stub.GetFunctionAndParameters()
switch f {
case "add":
{
a, _ := strconv.Atoi(args[0])
b, _ := strconv.Atoi(args[1])
rspStr := fmt.Sprintf("Value2:%d", a+b)
return shim.Success([]byte(rspStr))
}
case "sub":
{
a, _ := strconv.Atoi(args[0])
b, _ := strconv.Atoi(args[1])
rspStr := fmt.Sprintf("Value2:%d", a-b)
return shim.Success([]byte(rspStr))
}
case "mul":
{
a, _ := strconv.Atoi(args[0])
b, _ := strconv.Atoi(args[1])
rspStr := fmt.Sprintf("Value2:%d", a*b)
return shim.Success([]byte(rspStr))
}
case "div":
{
a, _ := strconv.Atoi(args[0])
b, _ := strconv.Atoi(args[1])
rspStr := fmt.Sprintf("Value2:%d", a/b)
return shim.Success([]byte(rspStr))
}
case "putval":
if len(args) < 2 {
return shim.Error("need 2 args (key and a value)")
}
// Initialize the chaincode
key := args[0]
val := args[1]
_, err := stub.GetState(key)
if err != nil {
jsonResp := "{\"Error\":\"Failed to get val for " + key + "\"}"
return shim.Error(jsonResp)
}
// Write the state to the ledger
err = stub.PutState(key, []byte(val))
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
case "getval":
var err error
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting key to query")
}
key := args[0]
// Get the state from the ledger
valbytes, err := stub.GetState(key)
//return shim.Success([]byte("abc"))
if err != nil {
jsonResp := "{\"Error\":\"Failed to get state for " + key + "\"}"
return shim.Error(jsonResp)
}
if valbytes == nil {
jsonResp := "{\"Error\":\"Nil val for " + key + "\"}"
return shim.Error(jsonResp)
}
return shim.Success(valbytes)
default:
jsonResp := "{\"Error\":\"Unknown function " + f + "\"}"
return shim.Error(jsonResp)
}
}