-
Notifications
You must be signed in to change notification settings - Fork 1
/
hstx.go
192 lines (162 loc) · 6.27 KB
/
hstx.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
package main
import (
"fmt"
"github.com/Akachain/akc-go-sdk/common"
hdl "github.com/Akachain/hstx-go-sdk/handler"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
// Chaincode struct
type Chaincode struct {
}
var handler = new(hdl.Handler)
// Init method is called when the Chain code" is instantiated by the blockchain network
func (s *Chaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success(nil)
}
// Invoke method is called as a result of an application request to run the chain code
// The calling application program has also specified the particular smart contract function to be called, with arguments
func (s *Chaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
common.Logger.Info("########### Hstx Invoke ###########")
handler.InitHandler()
// Retrieve the requested Smart Contract function and arguments
functionName, args := stub.GetFunctionAndParameters()
router := map[string]func(shim.ChaincodeStubInterface, []string) pb.Response{
"CreateSuperAdmin": createSuperAdmin,
"CreateAdmin": createAdmin,
"CreateProposal": createProposal,
"CreateApproval": createApproval,
"CommitProposal": commitProposal,
// "UpdateSuperAdmin": handler.SuperAdminHandler.UpdateSuperAdmin,
// "UpdateAdmin": handler.AdminHandler.UpdateAdmin,
// "UpdateProposal": handler.ProposalHandler.UpdateProposal,
// "UpdateApproval": handler.ApprovalHandler.UpdateApproval,
}
invokeFunc := router[functionName]
if invokeFunc != nil {
return invokeFunc(stub, args)
}
return s.Query(stub)
}
// Query callback representing the query of a chaincode
func (s *Chaincode) Query(stub shim.ChaincodeStubInterface) pb.Response {
common.Logger.Info("########### Hstx Query ###########")
// Retrieve the requested Smart Contract function and arguments
functionName, args := stub.GetFunctionAndParameters()
router := map[string]func(shim.ChaincodeStubInterface, []string) pb.Response{
// "GetAllSuperAdmin": handler.SuperAdminHandler.GetAllSuperAdmin,
// "GetSuperAdminByID": handler.SuperAdminHandler.GetSuperAdminByID,
// "GetAllAdmin": handler.AdminHandler.GetAllAdmin,
// "GetAdminByID": handler.AdminHandler.GetAdminByID,
// "GetAllProposal": handler.ProposalHandler.GetAllProposal,
// "GetProposalByID": handler.ProposalHandler.GetProposalByID,
// "GetPendingProposalBySuperAdminID": handler.ProposalHandler.GetPendingProposalBySuperAdminID,
// "GetAllApproval": handler.ApprovalHandler.GetAllApproval,
// "GetApprovalByID": handler.ApprovalHandler.GetApprovalByID,
}
queryFunc := router[functionName]
return queryFunc(stub, args)
}
// createSuperAdmin
func createSuperAdmin(stub shim.ChaincodeStubInterface, args []string) pb.Response {
superAdminStr := args[0]
created, err := handler.SuperAdminHandler.CreateSuperAdmin(stub, superAdminStr)
if err != nil {
// Returning error: Can't create data
return common.RespondError(common.ResponseError{
ResCode: common.ERR4,
Msg: fmt.Sprintf("%s %s %s", common.ResCodeDict[common.ERR4], err.Error(), common.GetLine()),
})
}
// Returning success with payload is created data
resSuc := common.ResponseSuccess{
ResCode: common.SUCCESS,
Msg: common.ResCodeDict[common.SUCCESS],
Payload: *created,
}
return common.RespondSuccess(resSuc)
}
// createAdmin
func createAdmin(stub shim.ChaincodeStubInterface, args []string) pb.Response {
adminStr := args[0]
created, err := handler.AdminHandler.CreateAdmin(stub, adminStr)
if err != nil {
// Returning error: Can't create data
return common.RespondError(common.ResponseError{
ResCode: common.ERR4,
Msg: fmt.Sprintf("%s %s %s", common.ResCodeDict[common.ERR4], err.Error(), common.GetLine()),
})
}
// Returning success with payload is created data
resSuc := common.ResponseSuccess{
ResCode: common.SUCCESS,
Msg: common.ResCodeDict[common.SUCCESS],
Payload: *created,
}
return common.RespondSuccess(resSuc)
}
// createProposal
func createProposal(stub shim.ChaincodeStubInterface, args []string) pb.Response {
proposalStr := args[0]
created, err := handler.ProposalHandler.CreateProposal(stub, proposalStr)
if err != nil {
// Returning error: Can't create data
return common.RespondError(common.ResponseError{
ResCode: common.ERR4,
Msg: fmt.Sprintf("%s %s %s", common.ResCodeDict[common.ERR4], err.Error(), common.GetLine()),
})
}
// Returning success with payload is created data
resSuc := common.ResponseSuccess{
ResCode: common.SUCCESS,
Msg: common.ResCodeDict[common.SUCCESS],
Payload: *created,
}
return common.RespondSuccess(resSuc)
}
// createApproval
func createApproval(stub shim.ChaincodeStubInterface, args []string) pb.Response {
approvalStr := args[0]
created, err := handler.ApprovalHandler.CreateApproval(stub, approvalStr)
if err != nil {
// Returning error: Can't create data
return common.RespondError(common.ResponseError{
ResCode: common.ERR4,
Msg: fmt.Sprintf("%s %s %s", common.ResCodeDict[common.ERR4], err.Error(), common.GetLine()),
})
}
// Returning success with payload is created data
resSuc := common.ResponseSuccess{
ResCode: common.SUCCESS,
Msg: common.ResCodeDict[common.SUCCESS],
Payload: *created,
}
return common.RespondSuccess(resSuc)
}
// commitProposal
func commitProposal(stub shim.ChaincodeStubInterface, args []string) pb.Response {
approvalStr := args[0]
created, err := handler.ProposalHandler.CommitProposal(stub, approvalStr)
if err != nil {
// Returning error: Can't create data
return common.RespondError(common.ResponseError{
ResCode: common.ERR4,
Msg: fmt.Sprintf("%s %s %s", common.ResCodeDict[common.ERR4], err.Error(), common.GetLine()),
})
}
// Returning success with payload is created data
resSuc := common.ResponseSuccess{
ResCode: common.SUCCESS,
Msg: common.ResCodeDict[common.SUCCESS],
Payload: *created,
}
return common.RespondSuccess(resSuc)
}
// The main function is only relevant in unit test mode. Only included here for completeness.
func main() {
// Create a new Chain code
err := shim.Start(new(Chaincode))
if err != nil {
fmt.Printf("Error creating new Chain code: %s", err)
}
}