github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/contract/callBetweenContract.test.js (about) 1 "use strict"; 2 3 var BigNumber = require('bignumber.js'); 4 var Wallet = require("nebulas"); 5 var HttpRequest = require("../../node-request.js"); 6 var TestNetConfig = require("../testnet_config.js"); 7 var Neb = Wallet.Neb; 8 var Transaction = Wallet.Transaction; 9 var FS = require("fs"); 10 var expect = require('chai').expect; 11 var Unit = Wallet.Unit; 12 13 // mocha cases/contract/xxx testneb2 -t 2000000 14 var args = process.argv.splice(2); 15 var env = args[1]; 16 // env = 'local'; 17 var testNetConfig = new TestNetConfig(env); 18 19 var neb = new Neb(); 20 var ChainID = testNetConfig.ChainId; 21 var sourceAccount = testNetConfig.sourceAccount; 22 var coinbase = testNetConfig.coinbase; 23 var apiEndPoint = testNetConfig.apiEndPoint; 24 neb.setRequest(new HttpRequest(apiEndPoint)); 25 26 var toAddress = Wallet.Account.NewAccount(); 27 var nonce; 28 var contractNonce = 0; 29 30 /* 31 * set this value according to the status of your testnet. 32 * the smaller the value, the faster the test, with the risk of causing error 33 */ 34 35 var maxCheckTime = 30; 36 var checkTimes = 0; 37 var beginCheckTime; 38 39 console.log("env:", env); 40 41 function checkTransaction(hash, callback) { 42 if (checkTimes === 0) { 43 beginCheckTime = new Date().getTime(); 44 } 45 checkTimes += 1; 46 if (checkTimes > maxCheckTime) { 47 console.log("check tx receipt timeout:" + hash); 48 checkTimes = 0; 49 callback(); 50 return; 51 } 52 53 neb.api.getTransactionReceipt(hash).then(function (resp) { 54 55 console.log("tx receipt status:" + resp.status); 56 if (resp.status === 2) { 57 setTimeout(function () { 58 checkTransaction(hash, callback); 59 }, 2000); 60 } else { 61 checkTimes = 0; 62 var endCheckTime = new Date().getTime(); 63 console.log("check tx time: : " + (endCheckTime - beginCheckTime) / 1000); 64 callback(resp); 65 } 66 }).catch(function (err) { 67 console.log("fail to get tx receipt hash: " + hash); 68 console.log("it may becuase the tx is being packing, we are going on to check it!"); 69 // console.log(err); 70 setTimeout(function () { 71 checkTransaction(hash, callback); 72 }, 2000); 73 }); 74 } 75 76 77 function doTest(testInput, testExpect, done) { 78 try { 79 nonce = nonce + 1; 80 var gasLimit = 2000000; 81 if (testInput.gasLimit) { 82 gasLimit = testInput.gasLimit; 83 } 84 var tx = new Transaction(ChainID, sourceAccount, callerContractAddress, Unit.nasToBasic(testInput.value), nonce, 1000000, gasLimit, testInput.contract); 85 // tx.to = contractAddress; 86 tx.signTransaction(); 87 // console.log("silent_debug"); 88 var coinState; 89 neb.api.getAccountState(coinbase).then(function(state){ 90 coinState = state; 91 return neb.api.sendRawTransaction(tx.toProtoString()); 92 }).then(function(resp) { 93 console.log("----step1. call callerTx ", resp); 94 checkTransaction(resp.txhash, function(resp) { 95 try { 96 expect(resp).to.not.be.a('undefined'); 97 console.log("----step2. have been on chain, To check balances"); 98 console.log("================result", JSON.stringify(resp)); 99 expect(resp.status).to.be.equal(testExpect.txStatus); 100 neb.api.getAccountState(callerContractAddress).then(function(state){ 101 expect(state.balance).to.be.equal(testExpect.callerBalance); 102 return neb.api.getAccountState(calleeContractAddress); 103 }).then(function(state) { 104 expect(state.balance).to.be.equal(testExpect.calleeBalance); 105 console.log("----step3, to check the result"); 106 return neb.api.call(sourceAccount.getAddressString(), callerContractAddress, 107 Unit.nasToBasic(0), nonce, 1000000, 2000000, testInput.resultCheckContract); 108 }).then(function(result){ 109 console.log(JSON.stringify(result)); 110 //result = {"result":"{\"key\":\"msg1\",\"value\":\"湖人总冠军\"}","execute_err":"","estimate_gas":"20511"} 111 if (testExpect.txStatus == 1) { 112 expect(result.result).equal(testExpect.result); 113 } 114 console.log("----step4, to check the err info by get event"); 115 return neb.api.getEventsByHash(resp.hash); 116 }).then(function(result){ 117 console.log(JSON.stringify(result)); 118 if (testExpect.txStatus == 0) { 119 expect(JSON.parse(result.events[0].data).error).equal(testExpect.errInfo); 120 } 121 return neb.api.getAccountState(coinbase); 122 }).then(function (state) { 123 console.log("get coinbase account state after tx:" + JSON.stringify(state)); 124 var reward = new BigNumber(state.balance).sub(coinState.balance); 125 reward = reward.mod(new BigNumber(1.42694).mul(new BigNumber(10).pow(18))); 126 // The transaction should be only 127 console.log("=========================",reward.toString()); 128 done(); 129 expect(reward.toString()).to.equal(testExpect.reward); 130 }).catch(function(err) { 131 console.log(err); 132 console.log("unexpected err in level 2" ); 133 done(err); 134 }); 135 136 } catch(err) { 137 console.log("check tx err :" + err); 138 done(err); 139 return; 140 } 141 }); 142 }).catch(function(err) { 143 console.log("unexpected err in level 1" ); 144 done(err); 145 }); 146 } catch(err) { 147 console.log("unexpected err in level 0"); 148 done(err); 149 } 150 } 151 152 153 154 var calleeContractSrc = FS.readFileSync("nf/nvm/test/kvStore.js", "utf-8"); 155 var callerContractSrc = FS.readFileSync("nf/nvm/test/kvStoreProxy.js", "utf-8"); 156 var calleeContractAddress; 157 var callerContractAddress; 158 var notExistAddress = "n1i8P4uhhgmHQmagmFRsk9cRzfJSkfnv2cp"; 159 var calleeBalance = 0; 160 var callerBalance = 0; 161 162 describe('test transfer from contract', function () { 163 before('0. deploy contracts', function (done) { 164 try { 165 neb.api.getAccountState(sourceAccount.getAddressString()).then(function(resp) { 166 console.log("----step0. get source account state: " + JSON.stringify(resp)); 167 var calleeContract = { 168 "source": calleeContractSrc, 169 "sourceType": "js", 170 "args": '' 171 }; 172 nonce = parseInt(resp.nonce); 173 nonce = nonce + 1; 174 var tx = new Transaction(ChainID, sourceAccount, sourceAccount, 0, nonce, 1000000, 20000000, calleeContract); 175 tx.signTransaction(); 176 return neb.api.sendRawTransaction(tx.toProtoString()); 177 }).then(function(resp) { 178 console.log("----step1. deploy callee contract: " + JSON.stringify(resp)); 179 calleeContractAddress = resp.contract_address; 180 // checkTransaction(resp.txhash, function(resp) { 181 // expect(resp).to.not.be.a('undefined'); 182 // console.log("----step2. have been on chain"); 183 // done(); 184 // }); 185 186 var callerContract = { 187 "source": callerContractSrc, 188 "sourceType": "js", 189 "args": '' 190 }; 191 192 nonce = nonce + 1; 193 var tx = new Transaction(ChainID, sourceAccount, sourceAccount, 0, nonce, 1000000, 20000000, callerContract); 194 tx.signTransaction(); 195 console.log(tx.contract); 196 return neb.api.sendRawTransaction(tx.toProtoString()); 197 }).then(function(resp) { 198 console.log("----step2. deploy caller contract: " + JSON.stringify(resp)); 199 callerContractAddress = resp.contract_address; 200 checkTransaction(resp.txhash, function(resp) { 201 try { 202 expect(resp).to.not.be.a('undefined'); 203 expect(resp.status).to.be.equal(1); 204 console.log("----step3. have been on chain"); 205 done(); 206 } catch(err) { 207 console.log("check tx err :" + err); 208 done(err); 209 return; 210 } 211 }); 212 }).catch(function(err) { 213 console.log("unexpected err: " + err); 214 done(err); 215 }); 216 } catch (err) { 217 console.log("unexpected err: " + err); 218 done(err); 219 } 220 }); 221 222 it ('1# test normal call', function(done) { 223 var testInput = { 224 contract: { 225 "function": "save", 226 "args": "[\"" + calleeContractAddress + "\",\"msg1\", \"湖人总冠军\"]" 227 }, 228 resultCheckContract: { 229 "function": "get", 230 "args": "[\"" + calleeContractAddress + "\",\"msg1\"]" 231 }, 232 value: 2 233 }; 234 235 calleeBalance += 2000000000000000000; 236 237 var testExpect = { 238 txStatus: 1, 239 reward: "57549000000", 240 callerBalance: callerBalance.toString(), 241 calleeBalance: calleeBalance.toString(), 242 result: "{\"key\":\"msg1\",\"value\":\"湖人总冠军\"}", 243 } 244 245 doTest(testInput, testExpect, done); 246 }); 247 248 it ('2# test normal call by use "call"', function(done) { 249 var testInput = { 250 contract: { 251 "function": "saveByCall", 252 "args": "[\"" + calleeContractAddress + "\",\"msg2\", \"湖人总冠军\"]" 253 }, 254 resultCheckContract: { 255 "function": "get", 256 "args": "[\"" + calleeContractAddress + "\",\"msg2\"]" 257 }, 258 value: 2 259 }; 260 calleeBalance += 2000000000000000000; 261 var testExpect = { 262 txStatus: 1, 263 reward: "57555000000", 264 callerBalance: callerBalance.toString(), 265 calleeBalance: calleeBalance.toString(), 266 result: "{\"key\":\"msg2\",\"value\":\"湖人总冠军\"}", 267 } 268 269 doTest(testInput, testExpect, done); 270 }); 271 272 it ('3# not exsit callee contract', function(done) { 273 var testInput = { 274 contract: { 275 "function": "save", 276 "args": "[\"" + notExistAddress + "\",\"msg1.5\", \"湖人总冠军\"]" 277 }, 278 value: 2 279 }; 280 281 var testExpect = { 282 txStatus: 0, 283 reward: "25188000000", 284 callerBalance: callerBalance.toString(), 285 calleeBalance: calleeBalance.toString(), 286 errInfo: "Call: Inner Call: no contract at this address", 287 } 288 289 doTest(testInput, testExpect, done); 290 }); 291 292 it ('4# function not exsit in callee contract', function(done) { 293 var testInput = { 294 contract: { 295 "function": "testFuncNotExist", 296 "args": "[\"" + calleeContractAddress + "\",\"msg1.5\", \"湖人总冠军\"]" 297 }, 298 value: 2 299 }; 300 301 var testExpect = { 302 txStatus: 0, 303 reward: "57245000000", 304 callerBalance: callerBalance.toString(), 305 calleeBalance: calleeBalance.toString(), 306 errInfo: "execution failed", 307 } 308 309 doTest(testInput, testExpect, done); 310 }); 311 312 it ('5# test usage of value', function(done) { 313 var testInput = { 314 contract: { 315 "function": "testUsageOfValue", 316 "args": "[\"" + calleeContractAddress + "\",\"msg3\", \"湖人总冠军\"]" 317 }, 318 resultCheckContract: { 319 "function": "get", 320 "args": "[\"" + calleeContractAddress + "\",\"msg3\"]" 321 }, 322 value: 0 323 }; 324 325 var testExpect = { 326 txStatus: 1, 327 reward: "57538000000", 328 callerBalance: callerBalance.toString(), 329 calleeBalance: calleeBalance.toString(), 330 result: "{\"key\":\"msg3\",\"value\":\"湖人总冠军\"}", 331 } 332 333 doTest(testInput, testExpect, done); 334 }); 335 336 it ('6# caller contract has not enough balance', function(done) { 337 var testInput = { 338 contract: { 339 "function": "save", 340 "args": "[\"" + calleeContractAddress + "\",\"msg4\", \"湖人总冠军\"]" 341 }, 342 value: 1 343 }; 344 345 var testExpect = { 346 txStatus: 0, 347 reward: "55210000000", 348 callerBalance: callerBalance.toString(), 349 calleeBalance: calleeBalance.toString(), 350 errInfo: "Call: Inner Contract: inner transfer failed", 351 } 352 353 doTest(testInput, testExpect, done); 354 }); 355 356 it ('7# gasLimit is not enough', function(done) { 357 var testInput = { 358 contract: { 359 "function": "save", 360 "args": "[\"" + calleeContractAddress + "\",\"msg4\", \"湖人总冠军\"]" 361 }, 362 value: 2, 363 gasLimit: 32300 364 }; 365 366 var testExpect = { 367 txStatus: 0, 368 reward: "32300000000", //TODO: to check 369 callerBalance: callerBalance.toString(), 370 calleeBalance: calleeBalance.toString(), 371 errInfo: "insufficient gas", 372 } 373 374 doTest(testInput, testExpect, done); 375 }); 376 377 it ('8# nas is not enough and but catch the error', function(done) { 378 var testInput = { 379 contract: { 380 "function": "safeSave", 381 "args": "[\"" + calleeContractAddress + "\",\"msg4\", \"湖人总冠军\"]" 382 }, 383 value: 1, 384 }; 385 386 var testExpect = { 387 txStatus: 0, 388 reward: "57214000000", 389 callerBalance: callerBalance.toString(), 390 calleeBalance: calleeBalance.toString(), 391 errInfo: "Call: Inner Contract: inner transfer failed", 392 } 393 394 doTest(testInput, testExpect, done); 395 }); 396 397 it ('9# trigger the err in callee contract and but catch the error', function(done) { 398 var testInput = { 399 contract: { 400 "function": "testTryCatch", 401 "args": "[\"" + calleeContractAddress + "\",\"msg4\", \"湖人总冠军\"]" 402 }, 403 value: 2, 404 }; 405 406 var testExpect = { 407 txStatus: 0, 408 reward: "57245000000", 409 callerBalance: callerBalance.toString(), 410 calleeBalance: calleeBalance.toString(), 411 errInfo: "execution failed", //TODO: ["execution failed", ...] 412 } 413 414 doTest(testInput, testExpect, done); 415 }); 416 417 it ('10# callee contract timeout ', function(done) { 418 var testInput = { 419 contract: { 420 "function": "testTimeOut", 421 "args": "[\"" + calleeContractAddress + "\",\"msg4\", \"湖人总冠军\"]" 422 }, 423 value: 2, 424 }; 425 426 var testExpect = { 427 txStatus: 0, 428 reward: "2000000000000", 429 callerBalance: callerBalance.toString(), 430 calleeBalance: calleeBalance.toString(), 431 errInfo: "insufficient gas", 432 } 433 434 doTest(testInput, testExpect, done); 435 }); 436 437 it ('11# callee contract out of memory', function(done) { 438 var testInput = { 439 contract: { 440 "function": "testOom", 441 "args": "[\"" + calleeContractAddress + "\",\"msg4\", \"湖人总冠军\"]" 442 }, 443 value: 2, 444 gasLimit: 5000000, 445 }; 446 447 var testExpect = { 448 txStatus: 0, 449 reward: "5000000000000", 450 callerBalance: callerBalance.toString(), 451 calleeBalance: calleeBalance.toString(), 452 errInfo: "exceed memory limits", 453 } 454 455 doTest(testInput, testExpect, done); 456 }); 457 458 it ('12# callee contract out of nvm gas limit', function(done) { 459 var testInput = { 460 contract: { 461 "function": "testOom", 462 "args": "[\"" + calleeContractAddress + "\",\"msg4\", \"湖人总冠军\"]" 463 }, 464 value: 2, 465 gasLimit: 5000000000, 466 }; 467 468 var testExpect = { 469 txStatus: 0, 470 reward: "10020163000000", 471 callerBalance: callerBalance.toString(), 472 calleeBalance: calleeBalance.toString(), 473 errInfo: "exceed memory limits", 474 } 475 476 doTest(testInput, testExpect, done); 477 }); 478 });