-
Notifications
You must be signed in to change notification settings - Fork 22
/
ndid_error_code.go
168 lines (149 loc) · 5.77 KB
/
ndid_error_code.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
/**
* 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"
"fmt"
"strings"
"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"
)
func (*ABCIApplication) checkErrorCodeType(errorCodeType string) bool {
return contains(errorCodeType, []string{"idp", "as"})
}
type AddErrorCodeParam struct {
ErrorCode int32 `json:"error_code"`
Description string `json:"description"`
Type string `json:"type"`
}
func (app *ABCIApplication) addErrorCode(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("AddErrorCode, Parameter: %s", param)
var funcParam AddErrorCodeParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// convert error type to lower case
funcParam.Type = strings.ToLower(funcParam.Type)
if !app.checkErrorCodeType(funcParam.Type) {
return app.ReturnDeliverTxLog(code.InvalidErrorCode, "Invalid error code type", "")
}
if funcParam.ErrorCode == 0 {
return app.ReturnDeliverTxLog(code.InvalidErrorCode, "ErrorCode cannot be 0", "")
}
errorCode := data.ErrorCode{
ErrorCode: funcParam.ErrorCode,
Description: funcParam.Description,
}
// add error code
errorCodeBytes, err := utils.ProtoDeterministicMarshal(&errorCode)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
errorKey := errorCodeKeyPrefix + keySeparator + funcParam.Type + keySeparator + fmt.Sprintf("%d", errorCode.ErrorCode)
hasErrorKey, err := app.state.Has([]byte(errorKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if hasErrorKey {
return app.ReturnDeliverTxLog(code.InvalidErrorCode, "ErrorCode is already in the database", "")
}
app.state.Set([]byte(errorKey), []byte(errorCodeBytes))
// add error code to ErrorCodeList
var errorCodeList data.ErrorCodeList
errorsKey := errorCodeListKeyPrefix + keySeparator + funcParam.Type
errorCodeListBytes, err := app.state.Get([]byte(errorsKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if errorCodeListBytes != nil {
err := proto.Unmarshal(errorCodeListBytes, &errorCodeList)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
}
errorCodeList.ErrorCode = append(errorCodeList.ErrorCode, &errorCode)
errorCodeListBytes, err = utils.ProtoDeterministicMarshal(&errorCodeList)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
app.state.Set([]byte(errorsKey), []byte(errorCodeListBytes))
return app.ReturnDeliverTxLog(code.OK, "success", "")
}
type RemoveErrorCodeParam struct {
ErrorCode int32 `json:"error_code"`
Type string `json:"type"`
}
func (app *ABCIApplication) removeErrorCode(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("RemoveErrorCode, Parameter: %s", param)
var funcParam RemoveErrorCodeParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// remove error code from ErrorCode index
errorKey := errorCodeKeyPrefix + keySeparator + funcParam.Type + keySeparator + fmt.Sprintf("%d", funcParam.ErrorCode)
hasErrorKey, err := app.state.Has([]byte(errorKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if !hasErrorKey {
return app.ReturnDeliverTxLog(code.InvalidErrorCode, "ErrorCode not exists", "")
}
err = app.state.Delete([]byte(errorKey))
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
// remove ErrorCode from ErrorCodeList
var errorCodeList data.ErrorCodeList
errorsKey := errorCodeListKeyPrefix + keySeparator + funcParam.Type
errorCodeListBytes, err := app.state.Get([]byte(errorsKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if errorCodeListBytes != nil {
err := proto.Unmarshal(errorCodeListBytes, &errorCodeList)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
}
newErrorCodeList := data.ErrorCodeList{
ErrorCode: make([]*data.ErrorCode, 0, len(errorCodeList.ErrorCode)),
}
for _, errorCode := range errorCodeList.ErrorCode {
if errorCode.ErrorCode != funcParam.ErrorCode {
newErrorCodeList.ErrorCode = append(newErrorCodeList.ErrorCode, errorCode)
}
}
if len(newErrorCodeList.ErrorCode) != len(errorCodeList.ErrorCode)-1 {
return app.ReturnDeliverTxLog(code.InvalidErrorCode, "ErrorCode not exists", "")
}
errorCodeListBytes, err = utils.ProtoDeterministicMarshal(&newErrorCodeList)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
app.state.Set([]byte(errorsKey), []byte(errorCodeListBytes))
return app.ReturnDeliverTxLog(code.OK, "success", "")
}