/
as_service_price.go
351 lines (303 loc) · 12.4 KB
/
as_service_price.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* Copyright (c) 2018, 2019 National Digital ID COMPANY LIMITED
*
* This file is part of NDID software.
*
* NDID is the free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or any later
* version.
*
* NDID 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 Affero GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* along with the NDID source code. If not, see https://www.gnu.org/licenses/agpl.txt.
*
* Please contact info@ndid.co.th for any further questions
*
*/
package app
import (
"encoding/json"
"time"
"github.com/tendermint/tendermint/abci/types"
"google.golang.org/protobuf/proto"
"github.com/ndidplatform/smart-contract/v7/abci/code"
"github.com/ndidplatform/smart-contract/v7/abci/utils"
data "github.com/ndidplatform/smart-contract/v7/protos/data"
)
type SetServicePriceParam struct {
ServiceID string `json:"service_id"`
PriceByCurrencyList []ServicePriceByCurrency `json:"price_by_currency_list"`
EffectiveDatetime time.Time `json:"effective_datetime"`
MoreInfoURL string `json:"more_info_url"`
Detail string `json:"detail"`
}
type ServicePriceByCurrency struct {
Currency string `json:"currency"`
MinPrice float64 `json:"min_price"`
MaxPrice float64 `json:"max_price"`
}
func (app *ABCIApplication) setServicePrice(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("SetServicePrice, Parameter: %s", param)
var funcParam SetServicePriceParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// check if service ID exists
serviceKey := serviceKeyPrefix + keySeparator + funcParam.ServiceID
exists, err := app.state.Has([]byte(serviceKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if !exists {
return app.ReturnDeliverTxLog(code.ServiceIDNotFound, "Service ID not found", "")
}
// Get service's price ceiling
servicePriceCeilingKey := servicePriceCeilingKeyPrefix + keySeparator + funcParam.ServiceID
servicePriceCeilingListBytes, err := app.state.Get([]byte(servicePriceCeilingKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if servicePriceCeilingListBytes == nil {
return app.ReturnDeliverTxLog(code.ServicePriceCeilingListNotFound, "Service price ceiling list not found", "")
}
var servicePriceCeilingList data.ServicePriceCeilingList
err = proto.Unmarshal([]byte(servicePriceCeilingListBytes), &servicePriceCeilingList)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
var servicePriceByCurrencyListToSet []*data.ServicePriceByCurrency = make([]*data.ServicePriceByCurrency, 0)
priceToSetLoop:
for _, priceToSet := range funcParam.PriceByCurrencyList {
for _, priceCeiling := range servicePriceCeilingList.PriceCeilingByCurrencyList {
if priceCeiling.Currency == priceToSet.Currency {
if priceToSet.MaxPrice >= 0 {
if priceToSet.MaxPrice > priceCeiling.Price {
return app.ReturnDeliverTxLog(code.ServiceMaxPriceGreaterThanPriceCeiling, "Service max price is greater than price ceiling", "")
}
}
// less then 0 price ceiling means no ceiling/limit
if priceToSet.MinPrice > priceToSet.MaxPrice {
return app.ReturnDeliverTxLog(code.ServicePriceMinCannotBeGreaterThanMax, "Service minimum price cannot be greater than maximum price", "")
}
servicePriceByCurrencyListToSet = append(servicePriceByCurrencyListToSet, &data.ServicePriceByCurrency{
Currency: priceToSet.Currency,
MinPrice: priceToSet.MinPrice,
MaxPrice: priceToSet.MaxPrice,
})
continue priceToSetLoop
}
}
return app.ReturnDeliverTxLog(code.ServicePriceCeilingNotFound, "Service price ceiling not found", "")
}
var newServicePrice *data.ServicePrice = &data.ServicePrice{
PriceByCurrencyList: servicePriceByCurrencyListToSet,
EffectiveDatetime: funcParam.EffectiveDatetime.UnixMilli(), // in milliseconds
MoreInfoUrl: funcParam.MoreInfoURL,
Detail: funcParam.Detail,
CreationBlockHeight: app.state.CurrentBlockHeight,
CreationChainId: app.CurrentChain,
}
servicePriceListKey := servicePriceListKeyPrefix + keySeparator + nodeID + keySeparator + funcParam.ServiceID
currentServicePriceListBytes, err := app.state.Get([]byte(servicePriceListKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if currentServicePriceListBytes != nil {
var currentServicePriceList data.ServicePriceList
err = proto.Unmarshal([]byte(currentServicePriceListBytes), ¤tServicePriceList)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// prepend new service price to list
currentServicePriceList.ServicePriceList = append([]*data.ServicePrice{newServicePrice}, currentServicePriceList.ServicePriceList...)
newCurrentServicePriceListBytes, err := utils.ProtoDeterministicMarshal(¤tServicePriceList)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
app.state.Set([]byte(servicePriceListKey), []byte(newCurrentServicePriceListBytes))
} else {
var newServicePriceList data.ServicePriceList = data.ServicePriceList{
ServicePriceList: []*data.ServicePrice{newServicePrice},
}
newCurrentServicePriceListBytes, err := utils.ProtoDeterministicMarshal(&newServicePriceList)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
app.state.Set([]byte(servicePriceListKey), []byte(newCurrentServicePriceListBytes))
}
return app.ReturnDeliverTxLog(code.OK, "success", "")
}
type GetServicePriceListParam struct {
NodeID string `json:"node_id"`
ServiceID string `json:"service_id"`
}
type GetServicePriceListResult struct {
ServicePriceListByNode []ServicePriceListByNode `json:"price_list_by_node"`
}
type ServicePriceListByNode struct {
NodeID string `json:"node_id"`
ServicePriceList []ServicePrice `json:"price_list"`
}
type ServicePrice struct {
PriceByCurrencyList []ServicePriceByCurrency `json:"price_by_currency_list"`
EffectiveDatetime time.Time `json:"effective_datetime"`
MoreInfoURL string `json:"more_info_url"`
Detail string `json:"detail"`
CreationBlockHeight int64 `json:"creation_block_height"`
CreationChainID string `json:"creation_chain_id"`
}
func (app *ABCIApplication) getServicePriceList(param string) types.ResponseQuery {
app.logger.Infof("GetServicePriceList, Parameter: %s", param)
var funcParam GetServicePriceListParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
serviceKey := serviceKeyPrefix + keySeparator + funcParam.ServiceID
serviceValue, err := app.state.Get([]byte(serviceKey), true)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
if serviceValue == nil {
return app.ReturnQuery(nil, "not found", app.state.Height)
}
var service data.ServiceDetail
err = proto.Unmarshal([]byte(serviceValue), &service)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
getServicePriceListByNode := func(nodeID string) (*ServicePriceListByNode, error) {
servicePriceListKey := servicePriceListKeyPrefix + keySeparator + nodeID + keySeparator + funcParam.ServiceID
servicePriceListBytes, err := app.state.Get([]byte(servicePriceListKey), false)
if err != nil {
return nil, err
}
if servicePriceListBytes == nil {
var servicePriceByNode *ServicePriceListByNode = &ServicePriceListByNode{
NodeID: nodeID,
ServicePriceList: make([]ServicePrice, 0),
}
return servicePriceByNode, nil
}
var servicePriceList data.ServicePriceList
err = proto.Unmarshal([]byte(servicePriceListBytes), &servicePriceList)
if err != nil {
return nil, err
}
var servicePriceListByNode *ServicePriceListByNode = &ServicePriceListByNode{
NodeID: nodeID,
ServicePriceList: make([]ServicePrice, 0),
}
for _, servicePrice := range servicePriceList.ServicePriceList {
var retValServicePrice ServicePrice = ServicePrice{
PriceByCurrencyList: make([]ServicePriceByCurrency, 0),
EffectiveDatetime: time.UnixMilli(servicePrice.EffectiveDatetime), // in milliseconds
MoreInfoURL: servicePrice.MoreInfoUrl,
Detail: servicePrice.Detail,
CreationBlockHeight: servicePrice.CreationBlockHeight,
CreationChainID: servicePrice.CreationChainId,
}
for _, priceByCurrency := range servicePrice.PriceByCurrencyList {
var retValServicePriceByCurrency ServicePriceByCurrency = ServicePriceByCurrency{
Currency: priceByCurrency.Currency,
MinPrice: priceByCurrency.MinPrice,
MaxPrice: priceByCurrency.MaxPrice,
}
retValServicePrice.PriceByCurrencyList = append(retValServicePrice.PriceByCurrencyList, retValServicePriceByCurrency)
}
servicePriceListByNode.ServicePriceList = append(servicePriceListByNode.ServicePriceList, retValServicePrice)
}
return servicePriceListByNode, nil
}
var retVal GetServicePriceListResult
retVal.ServicePriceListByNode = make([]ServicePriceListByNode, 0)
if funcParam.NodeID != "" {
servicePriceListByNode, err := getServicePriceListByNode(funcParam.NodeID)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
retVal.ServicePriceListByNode = append(retVal.ServicePriceListByNode, *servicePriceListByNode)
} else {
if !service.Active {
retValJSON, err := json.Marshal(retVal)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
return app.ReturnQuery(retValJSON, "service is not active", app.state.Height)
}
// Get all AS nodes providing service
serviceDestinationListKey := serviceDestinationKeyPrefix + keySeparator + funcParam.ServiceID
serviceDestinationListValue, err := app.state.Get([]byte(serviceDestinationListKey), true)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
if serviceDestinationListValue == nil {
retValJSON, err := json.Marshal(retVal)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
return app.ReturnQuery(retValJSON, "success", app.state.Height)
}
var serviceDestinationList data.ServiceDesList
err = proto.Unmarshal([]byte(serviceDestinationListValue), &serviceDestinationList)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
for _, serviceDestination := range serviceDestinationList.Node {
// filter out inactive service destination
if !serviceDestination.Active {
continue
}
// filter out not approved by NDID
approveServiceKey := approvedServiceKeyPrefix + keySeparator + funcParam.ServiceID + keySeparator + serviceDestination.NodeId
approveServiceJSON, err := app.state.Get([]byte(approveServiceKey), true)
if err != nil {
continue
}
if approveServiceJSON == nil {
continue
}
var approveService data.ApproveService
err = proto.Unmarshal([]byte(approveServiceJSON), &approveService)
if err != nil {
continue
}
if !approveService.Active {
continue
}
nodeDetailKey := nodeIDKeyPrefix + keySeparator + serviceDestination.NodeId
nodeDetailValue, err := app.state.Get([]byte(nodeDetailKey), true)
if err != nil {
continue
}
if nodeDetailValue == nil {
continue
}
var nodeDetail data.NodeDetail
err = proto.Unmarshal(nodeDetailValue, &nodeDetail)
if err != nil {
continue
}
// filter out inactive node
if !nodeDetail.Active {
continue
}
servicePriceListByNode, err := getServicePriceListByNode(serviceDestination.NodeId)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
retVal.ServicePriceListByNode = append(retVal.ServicePriceListByNode, *servicePriceListByNode)
}
}
retValJSON, err := json.Marshal(retVal)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
return app.ReturnQuery(retValJSON, "success", app.state.Height)
}