forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs_test_jobs.go
224 lines (202 loc) · 6.22 KB
/
jobs_test_jobs.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
package jobs
import (
"encoding/hex"
"fmt"
"strconv"
"github.com/hyperledger/burrow/deploy/def"
"github.com/hyperledger/burrow/deploy/util"
"github.com/hyperledger/burrow/execution/evm/abi"
log "github.com/sirupsen/logrus"
)
func QueryContractJob(query *def.QueryContract, do *def.DeployArgs, client *def.Client) (string, []*abi.Variable, error) {
var queryDataArray []string
var err error
query.Function, queryDataArray, err = util.PreProcessInputData(query.Function, query.Data, do, client, false)
if err != nil {
return "", nil, err
}
// Get the packed data from the ABI functions
var data string
var packedBytes []byte
if query.Bin != "" {
packedBytes, err = abi.ReadAbiFormulateCallFile(query.Bin, do.BinPath, query.Function, queryDataArray)
data = hex.EncodeToString(packedBytes)
}
if query.Bin == "" || err != nil {
packedBytes, err = abi.ReadAbiFormulateCallFile(query.Destination, do.BinPath, query.Function, queryDataArray)
data = hex.EncodeToString(packedBytes)
}
if err != nil {
var err = util.ABIErrorHandler(err, nil, query)
return "", nil, err
}
// Call the client
txe, err := client.QueryContract(&def.QueryArg{
Input: query.Source,
Address: query.Destination,
Data: data,
})
if err != nil {
return "", nil, err
}
// Formally process the return
log.WithField("res", txe.Result.Return).Debug("Decoding Raw Result")
if query.Bin != "" {
log.WithField("abi", query.Bin).Debug()
query.Variables, err = abi.ReadAndDecodeContractReturn(query.Bin, do.BinPath, query.Function, txe.Result.Return)
}
if query.Bin == "" || err != nil {
log.WithField("abi", query.Destination).Debug()
query.Variables, err = abi.ReadAndDecodeContractReturn(query.Destination, do.BinPath, query.Function, txe.Result.Return)
}
if err != nil {
return "", nil, err
}
result2 := util.GetReturnValue(query.Variables)
// Finalize
if result2 != "" {
log.WithField("=>", result2).Warn("Return Value")
} else {
log.Debug("No return.")
}
return result2, query.Variables, nil
}
func QueryAccountJob(query *def.QueryAccount, client *def.Client) (string, error) {
// Perform Query
arg := fmt.Sprintf("%s:%s", query.Account, query.Field)
log.WithField("=>", arg).Info("Querying Account")
result, err := util.AccountsInfo(query.Account, query.Field, client)
if err != nil {
return "", err
}
// Result
if result != "" {
log.WithField("=>", result).Warn("Return Value")
} else {
log.Debug("No return.")
}
return result, nil
}
func QueryNameJob(query *def.QueryName, client *def.Client) (string, error) {
// Peform query
log.WithFields(log.Fields{
"name": query.Name,
"field": query.Field,
}).Info("Querying")
result, err := util.NamesInfo(query.Name, query.Field, client)
if err != nil {
return "", err
}
if result != "" {
log.WithField("=>", result).Warn("Return Value")
} else {
log.Debug("No return.")
}
return result, nil
}
func QueryValsJob(query *def.QueryVals, client *def.Client) (interface{}, error) {
log.WithField("=>", query.Query).Info("Querying Vals")
result, err := util.ValidatorsInfo(query.Query, client)
if err != nil {
return "", fmt.Errorf("error querying validators with jq-style query %s: %v", query.Query, err)
}
if result != nil {
log.WithField("=>", result).Warn("Return Value")
} else {
log.Debug("No return.")
}
return result, nil
}
func AssertJob(assertion *def.Assert) (string, error) {
var result string
// Switch on relation
log.WithFields(log.Fields{
"key": assertion.Key,
"relation": assertion.Relation,
"value": assertion.Value,
}).Info("Assertion =>")
switch assertion.Relation {
case "==", "eq":
/*log.Debug("Compare", strings.Compare(assertion.Key, assertion.Value))
log.Debug("UTF8?: ", utf8.ValidString(assertion.Key))
log.Debug("UTF8?: ", utf8.ValidString(assertion.Value))
log.Debug("UTF8?: ", utf8.RuneCountInString(assertion.Key))
log.Debug("UTF8?: ", utf8.RuneCountInString(assertion.Value))*/
if assertion.Key == assertion.Value {
return assertPass("==", assertion.Key, assertion.Value)
} else {
return assertFail("==", assertion.Key, assertion.Value)
}
case "!=", "ne":
if assertion.Key != assertion.Value {
return assertPass("!=", assertion.Key, assertion.Value)
} else {
return assertFail("!=", assertion.Key, assertion.Value)
}
case ">", "gt":
k, v, err := bulkConvert(assertion.Key, assertion.Value)
if err != nil {
return convFail()
}
if k > v {
return assertPass(">", assertion.Key, assertion.Value)
} else {
return assertFail(">", assertion.Key, assertion.Value)
}
case ">=", "ge":
k, v, err := bulkConvert(assertion.Key, assertion.Value)
if err != nil {
return convFail()
}
if k >= v {
return assertPass(">=", assertion.Key, assertion.Value)
} else {
return assertFail(">=", assertion.Key, assertion.Value)
}
case "<", "lt":
k, v, err := bulkConvert(assertion.Key, assertion.Value)
if err != nil {
return convFail()
}
if k < v {
return assertPass("<", assertion.Key, assertion.Value)
} else {
return assertFail("<", assertion.Key, assertion.Value)
}
case "<=", "le":
k, v, err := bulkConvert(assertion.Key, assertion.Value)
if err != nil {
return convFail()
}
if k <= v {
return assertPass("<=", assertion.Key, assertion.Value)
} else {
return assertFail("<=", assertion.Key, assertion.Value)
}
default:
return "", fmt.Errorf("Error: Bad assert relation: \"%s\" is not a valid relation. See documentation for more information.", assertion.Relation)
}
return result, nil
}
func bulkConvert(key, value string) (int, int, error) {
k, err := strconv.Atoi(key)
if err != nil {
return 0, 0, err
}
v, err := strconv.Atoi(value)
if err != nil {
return 0, 0, err
}
return k, v, nil
}
func assertPass(typ, key, val string) (string, error) {
log.WithField("=>", fmt.Sprintf("%s %s %s", key, typ, val)).Warn("Assertion Succeeded")
return "passed", nil
}
func assertFail(typ, key, val string) (string, error) {
log.WithField("=>", fmt.Sprintf("%s %s %s", key, typ, val)).Warn("Assertion Failed")
return "failed", fmt.Errorf("assertion failed")
}
func convFail() (string, error) {
return "", fmt.Errorf("The Key of your assertion cannot be converted into an integer.\nFor string conversions please use the equal or not equal relations.")
}