github.com/eris-ltd/erisdb@v0.25.0/deploy/jobs/jobs_test_jobs.go (about) 1 package jobs 2 3 import ( 4 "fmt" 5 "strconv" 6 7 hex "github.com/tmthrgd/go-hex" 8 9 "github.com/hyperledger/burrow/deploy/def" 10 "github.com/hyperledger/burrow/deploy/util" 11 "github.com/hyperledger/burrow/execution/evm/abi" 12 "github.com/hyperledger/burrow/logging" 13 ) 14 15 func QueryContractJob(query *def.QueryContract, do *def.DeployArgs, script *def.Playbook, client *def.Client, logger *logging.Logger) (string, []*abi.Variable, error) { 16 var queryDataArray []interface{} 17 var err error 18 query.Function, queryDataArray, err = util.PreProcessInputData(query.Function, query.Data, do, script, client, false, logger) 19 if err != nil { 20 return "", nil, err 21 } 22 23 // Get the packed data from the ABI functions 24 var data string 25 var packedBytes []byte 26 if query.Bin != "" { 27 packedBytes, _, err = abi.EncodeFunctionCallFromFile(query.Bin, script.BinPath, query.Function, logger, queryDataArray...) 28 data = hex.EncodeToString(packedBytes) 29 } 30 if query.Bin == "" || err != nil { 31 packedBytes, _, err = abi.EncodeFunctionCallFromFile(query.Destination, script.BinPath, query.Function, logger, queryDataArray...) 32 data = hex.EncodeToString(packedBytes) 33 } 34 if err != nil { 35 var err = util.ABIErrorHandler(err, nil, query, logger) 36 return "", nil, err 37 } 38 39 // Call the client 40 txe, err := client.QueryContract(&def.QueryArg{ 41 Input: query.Source, 42 Address: query.Destination, 43 Data: data, 44 }, logger) 45 if err != nil { 46 return "", nil, err 47 } 48 49 // Formally process the return 50 if query.Bin != "" { 51 logger.TraceMsg("Decoding Raw Result", 52 "return", hex.EncodeUpperToString(txe.Result.Return), 53 "Abi", query.Bin) 54 query.Variables, err = abi.DecodeFunctionReturnFromFile(query.Bin, script.BinPath, query.Function, txe.Result.Return, logger) 55 } 56 if query.Bin == "" || err != nil { 57 logger.TraceMsg("Decoding Raw Result", 58 "return", hex.EncodeUpperToString(txe.Result.Return), 59 "Abi", query.Destination) 60 query.Variables, err = abi.DecodeFunctionReturnFromFile(query.Destination, script.BinPath, query.Function, txe.Result.Return, logger) 61 } 62 if err != nil { 63 return "", nil, err 64 } 65 66 result2 := util.GetReturnValue(query.Variables, logger) 67 // Finalize 68 if result2 != "" { 69 logger.InfoMsg("Return Value", "value", result2) 70 } else { 71 logger.InfoMsg("No return.") 72 } 73 return result2, query.Variables, nil 74 } 75 76 func QueryAccountJob(query *def.QueryAccount, client *def.Client, logger *logging.Logger) (string, error) { 77 // Perform Query 78 arg := fmt.Sprintf("%s:%s", query.Account, query.Field) 79 logger.InfoMsg("Query Account", "argument", arg) 80 81 result, err := util.AccountsInfo(query.Account, query.Field, client, logger) 82 if err != nil { 83 return "", err 84 } 85 86 // Result 87 if result != "" { 88 logger.InfoMsg("Return Value", "value", result) 89 } else { 90 logger.InfoMsg("No return.") 91 } 92 return result, nil 93 } 94 95 func QueryNameJob(query *def.QueryName, client *def.Client, logger *logging.Logger) (string, error) { 96 // Peform query 97 logger.InfoMsg("Querying", 98 "name", query.Name, 99 "field", query.Field) 100 result, err := util.NamesInfo(query.Name, query.Field, client, logger) 101 if err != nil { 102 return "", err 103 } 104 105 if result != "" { 106 logger.InfoMsg("Return Value", "result", result) 107 } else { 108 logger.InfoMsg("No return.") 109 } 110 return result, nil 111 } 112 113 func QueryValsJob(query *def.QueryVals, client *def.Client, logger *logging.Logger) (interface{}, error) { 114 logger.InfoMsg("Querying Vals", "query", query.Query) 115 result, err := util.ValidatorsInfo(query.Query, client, logger) 116 if err != nil { 117 return "", fmt.Errorf("error querying validators with jq-style query %s: %v", query.Query, err) 118 } 119 120 if result != nil { 121 logger.InfoMsg("Return Value", "result", result) 122 } else { 123 logger.InfoMsg("No return.") 124 } 125 return result, nil 126 } 127 128 func AssertJob(assertion *def.Assert, logger *logging.Logger) (string, error) { 129 // Switch on relation 130 logger.InfoMsg("Assertion", 131 "key", assertion.Key, 132 "relation", assertion.Relation, 133 "value", assertion.Value) 134 135 switch assertion.Relation { 136 case "==", "eq": 137 /*log.Debug("Compare", strings.Compare(assertion.Key, assertion.Value)) 138 log.Debug("UTF8?: ", utf8.ValidString(assertion.Key)) 139 log.Debug("UTF8?: ", utf8.ValidString(assertion.Value)) 140 log.Debug("UTF8?: ", utf8.RuneCountInString(assertion.Key)) 141 log.Debug("UTF8?: ", utf8.RuneCountInString(assertion.Value))*/ 142 if assertion.Key == assertion.Value { 143 return assertPass("==", assertion.Key, assertion.Value, logger) 144 } else { 145 return assertFail("==", assertion.Key, assertion.Value, logger) 146 } 147 case "!=", "ne": 148 if assertion.Key != assertion.Value { 149 return assertPass("!=", assertion.Key, assertion.Value, logger) 150 } else { 151 return assertFail("!=", assertion.Key, assertion.Value, logger) 152 } 153 case ">", "gt": 154 k, v, err := bulkConvert(assertion.Key, assertion.Value) 155 if err != nil { 156 return convFail() 157 } 158 if k > v { 159 return assertPass(">", assertion.Key, assertion.Value, logger) 160 } else { 161 return assertFail(">", assertion.Key, assertion.Value, logger) 162 } 163 case ">=", "ge": 164 k, v, err := bulkConvert(assertion.Key, assertion.Value) 165 if err != nil { 166 return convFail() 167 } 168 if k >= v { 169 return assertPass(">=", assertion.Key, assertion.Value, logger) 170 } else { 171 return assertFail(">=", assertion.Key, assertion.Value, logger) 172 } 173 case "<", "lt": 174 k, v, err := bulkConvert(assertion.Key, assertion.Value) 175 if err != nil { 176 return convFail() 177 } 178 if k < v { 179 return assertPass("<", assertion.Key, assertion.Value, logger) 180 } else { 181 return assertFail("<", assertion.Key, assertion.Value, logger) 182 } 183 case "<=", "le": 184 k, v, err := bulkConvert(assertion.Key, assertion.Value) 185 if err != nil { 186 return convFail() 187 } 188 if k <= v { 189 return assertPass("<=", assertion.Key, assertion.Value, logger) 190 } else { 191 return assertFail("<=", assertion.Key, assertion.Value, logger) 192 } 193 default: 194 return "", fmt.Errorf("Error: Bad assert relation: \"%s\" is not a valid relation. See documentation for more information.", assertion.Relation) 195 } 196 } 197 198 func bulkConvert(key, value string) (int, int, error) { 199 k, err := strconv.Atoi(key) 200 if err != nil { 201 return 0, 0, err 202 } 203 v, err := strconv.Atoi(value) 204 if err != nil { 205 return 0, 0, err 206 } 207 return k, v, nil 208 } 209 210 func assertPass(typ, key, val string, logger *logging.Logger) (string, error) { 211 logger.InfoMsg("Assertion Succeeded", 212 "operation", typ, 213 "key", key, 214 "value", val) 215 return "passed", nil 216 } 217 218 func assertFail(typ, key, val string, logger *logging.Logger) (string, error) { 219 logger.InfoMsg("Assertion Failed", 220 "operation", typ, 221 "key", key, 222 "value", val) 223 return "failed", fmt.Errorf("assertion failed") 224 } 225 226 func convFail() (string, error) { 227 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.") 228 }