/
utils.go
229 lines (207 loc) · 5.95 KB
/
utils.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
// Copyright (C) 2017, 2018, 2019 EGAAS S.A.
//
// This program 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 2 of the License, or (at
// your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package smart
import (
"encoding/json"
"fmt"
"strings"
"github.com/AplaProject/go-apla/packages/conf"
"github.com/AplaProject/go-apla/packages/consts"
"github.com/AplaProject/go-apla/packages/script"
"github.com/AplaProject/go-apla/packages/types"
"github.com/shopspring/decimal"
log "github.com/sirupsen/logrus"
)
func logError(err error, errType string, comment string) error {
log.WithFields(log.Fields{"type": errType, "error": err}).Error(comment)
return err
}
func logErrorf(pattern string, param interface{}, errType string, comment string) error {
err := fmt.Errorf(pattern, param)
log.WithFields(log.Fields{"type": errType, "error": err}).Error(comment)
return err
}
func logErrorShort(err error, errType string) error {
return logError(err, errType, err.Error())
}
func logErrorfShort(pattern string, param interface{}, errType string) error {
return logErrorShort(fmt.Errorf(pattern, param), errType)
}
func logErrorValue(err error, errType string, comment, value string) error {
log.WithFields(log.Fields{"type": errType, "error": err, "value": value}).Error(comment)
return err
}
func logErrorDB(err error, comment string) error {
return logError(err, consts.DBError, comment)
}
func unmarshalJSON(input []byte, v interface{}, comment string) (err error) {
if err = json.Unmarshal(input, v); err != nil {
return logErrorValue(err, consts.JSONUnmarshallError, comment, string(input))
}
return nil
}
func marshalJSON(v interface{}, comment string) (out []byte, err error) {
out, err = json.Marshal(v)
if err != nil {
logError(err, consts.JSONMarshallError, comment)
}
return
}
func validateAccess(funcName string, sc *SmartContract, contracts ...string) error {
if conf.Config.FuncBench {
return nil
}
if !accessContracts(sc, contracts...) {
err := fmt.Errorf(eAccessContract, funcName, strings.Join(contracts, ` or `))
return logError(err, consts.IncorrectCallingContract, err.Error())
}
return nil
}
func FillTxData(fieldInfos []*script.FieldInfo, params map[string]interface{}) (map[string]interface{}, error) {
txData := make(map[string]interface{})
for _, fitem := range fieldInfos {
var (
v interface{}
ok bool
err error
index = fitem.Name
)
if _, ok := params[index]; !ok {
if fitem.ContainsTag(script.TagOptional) {
txData[index] = getFieldDefaultValue(fitem.Original)
continue
}
return nil, fmt.Errorf(eParamNotFound, index)
}
switch fitem.Original {
case script.DtBool:
if v, ok = params[index].(bool); !ok {
err = fmt.Errorf("Invalid bool type")
break
}
case script.DtFloat:
switch val := params[index].(type) {
case float64:
v = val
case uint64:
v = float64(val)
case int64:
v = float64(val)
default:
err = fmt.Errorf("Invalid float type")
break
}
case script.DtInt, script.DtAddress:
switch t := params[index].(type) {
case int64:
v = t
case uint64:
v = int64(t)
default:
err = fmt.Errorf("Invalid int type")
}
case script.DtMoney:
var s string
if s, ok = params[index].(string); !ok {
err = fmt.Errorf("Invalid money type")
break
}
v, err = decimal.NewFromString(s)
if err != nil {
break
}
case script.DtString:
if v, ok = params[index].(string); !ok {
err = fmt.Errorf("Invalid string type")
break
}
case script.DtBytes:
if v, ok = params[index].([]byte); !ok {
err = fmt.Errorf("Invalid bytes type")
break
}
case script.DtArray:
if v, ok = params[index].([]interface{}); !ok {
err = fmt.Errorf("Invalid array type")
break
}
for i, subv := range v.([]interface{}) {
switch val := subv.(type) {
case map[interface{}]interface{}:
imap := make(map[string]interface{})
for ikey, ival := range val {
imap[fmt.Sprint(ikey)] = ival
}
v.([]interface{})[i] = types.LoadMap(imap)
}
}
case script.DtMap:
var val map[interface{}]interface{}
if val, ok = params[index].(map[interface{}]interface{}); !ok {
err = fmt.Errorf("Invalid map type")
break
}
imap := make(map[string]interface{})
for ikey, ival := range val {
imap[fmt.Sprint(ikey)] = ival
}
v = types.LoadMap(imap)
case script.DtFile:
var val map[interface{}]interface{}
if val, ok = params[index].(map[interface{}]interface{}); !ok {
err = fmt.Errorf("Invalid file type")
break
}
if v, ok = types.NewFileFromMap(val); !ok {
err = fmt.Errorf("Invalid attrs of file")
break
}
}
if err != nil {
return nil, fmt.Errorf("Invalid param '%s': %s", index, err)
}
if _, ok = txData[fitem.Name]; !ok {
txData[fitem.Name] = v
}
}
if len(txData) != len(fieldInfos) {
return nil, fmt.Errorf("Invalid number of parameters")
}
return txData, nil
}
func getFieldDefaultValue(fieldType uint32) interface{} {
switch fieldType {
case script.DtBool:
return false
case script.DtFloat:
return float64(0)
case script.DtInt, script.DtAddress:
return int64(0)
case script.DtMoney:
return decimal.New(0, consts.MoneyDigits)
case script.DtString:
return ""
case script.DtBytes:
return []byte{}
case script.DtArray:
return []interface{}{}
case script.DtMap:
return types.NewMap()
case script.DtFile:
return types.NewFile()
}
return nil
}