-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaincode_test.go
More file actions
146 lines (114 loc) · 4.25 KB
/
Copy pathchaincode_test.go
File metadata and controls
146 lines (114 loc) · 4.25 KB
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
package main
/* Imports
* 4 utility libraries for formatting, handling bytes, reading and writing JSON, and string manipulation
* 2 specific Hyperledger Fabric specific libraries for Smart Contracts
*/
import (
"bytes" //bytes包提供了对字节切片进行读写操作的一系列函数
"encoding/json"
"fmt" //打印等函数的包
"strconv"//字符转换包
"github.com/hyperledger/fabric/core/chaincode/shim"
sc "github.com/hyperledger/fabric/protos/peer"
)
// Define the Smart Contract structure
type SmartContract struct {
}
// Define the Coupon structure, with 4 properties. Structure tags are used by encoding/json library
type Coupon struct {
Number string `json:"number"`
Amount string `json:"amount"`
Flag string `json:"flag"`
Owner string `json:"owner"`
}
/*
* The Init method is called when the Smart Contract "fabcar" is instantiated by the blockchain network
* Best practice is to have any Ledger initialization in separate function -- see initLedger()
*/
func (s *SmartContract) Init(APIstub shim.ChaincodeStubInterface) sc.Response {
return shim.Success(nil)
}
/*
* The Invoke method is called as a result of an application request to run the Smart Contract "fabcar"
* The calling application program has also specified the particular smart contract function to be called, with arguments
*/
func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {
// Retrieve the requested Smart Contract function and arguments
function, args := APIstub.GetFunctionAndParameters()
// Route to the appropriate handler function to interact with the ledger appropriately
if function == "queryCoupon" {
return s.queryCoupon(APIstub, args)
} else if function == "initLedger" {
return s.initLedger(APIstub)
} else if function == "createCoupon" {
return s.createCoupon(APIstub, args)
} else if function == "consumeCoupon" { //消耗代金券
return s.queryAllCoupon(APIstub)
} else if function == "changeCouponOwner" {
return s.changeCouponOwner(APIstub, args)
}
return shim.Error("Invalid Smart Contract function name.")
}
func (s *SmartContract) queryCoupon(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
couponAsBytes, _ := APIstub.GetState(args[0])
return shim.Success(couponAsBytes)
}
func (s *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {
coupons := []Coupon{
Coupon{Number: "001", Amount: "100", Flag: "0", Owner: "hanshuang"},
}
i := 0
for i < len(coupons) {
fmt.Println("i is ", i)
couponAsBytes, _ := json.Marshal(coupons[i])
APIstub.PutState("COUPON"+strconv.Itoa(i), couponAsBytes)
fmt.Println("Added", coupons[i])
i = i + 1
}
return shim.Success(nil)
}
func (s *SmartContract) createCoupon(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 5 {
return shim.Error("Incorrect number of arguments. Expecting 5")
}
var coupon = Coupon{Number: args[1], Amount: args[2], Flag: args[3], Owner: args[4]}
couponAsBytes, _ := json.Marshal(coupon)
APIstub.PutState(args[0], couponAsBytes)
return shim.Success(nil)
}
func (s *SmartContract) consumeCoupon(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
couponAsBytes, _ := APIstub.GetState(args[0])
coupon := Coupon{}
json.Unmarshal(couponAsBytes, &coupon)
coupon.Flag = args[1]
couponAsBytes, _ = json.Marshal(coupon)
APIstub.PutState(args[0], couponAsBytes)
return shim.Success(nil)
}
func (s *SmartContract) changeCouponOwner(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
couponAsBytes, _ := APIstub.GetState(args[0])
coupon := Coupon{}
json.Unmarshal(couponAsBytes, &coupon)
coupon.Owner = args[1]
cuponAsBytes, _ = json.Marshal(coupon)
APIstub.PutState(args[0], couponAsBytes)
return shim.Success(nil)
}
//demo
// The main function is only relevant in unit test mode. Only included here for completeness.
func main() {
// Create a new Smart Contract
err := shim.Start(new(SmartContract))
if err != nil {
fmt.Printf("Error creating new Smart Contract: %s", err)
}
}