github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/contract/contract.call.test.js (about) 1 'use strict'; 2 3 var sleep = require("system-sleep"); 4 var FS = require("fs"); 5 var expect = require('chai').expect; 6 var BigNumber = require('bignumber.js'); 7 var TestNetConfig = require("../testnet_config"); 8 9 var Nebulas = require('nebulas'); 10 var HttpRequest = require('../../node-request'); 11 var Account = Nebulas.Account; 12 var Transaction = Nebulas.Transaction; 13 var CryptoUtils = Nebulas.CryptoUtils; 14 var Neb = Nebulas.Neb; 15 16 // mocha cases/contract/contract.call.test.js testneb2 -t 200000 17 18 var args = process.argv.splice(2); 19 var env = args[1]; 20 var testNetConfig = new TestNetConfig(env); 21 22 var source, deploy, from, fromState, contractAddr; 23 var coinState; 24 var testCases = new Array(); 25 var caseIndex = 0; 26 27 var neb = new Neb(); 28 var ChainID = testNetConfig.ChainId; 29 var originSource = testNetConfig.sourceAccount; 30 var coinbase = testNetConfig.coinbase; 31 var apiEndPoint = testNetConfig.apiEndPoint; 32 neb.setRequest(new HttpRequest(apiEndPoint)) 33 34 var lastnonce = 0; 35 36 37 console.log("running script, env:", env, " ChainId:", ChainID, " apiEndPoint:", " time:", new Date()); 38 39 function prepareSource(done) { 40 neb.api.getAccountState(originSource.getAddressString()).then(function (resp) { 41 console.log("prepare source account state:" + JSON.stringify(resp)); 42 var nonce = parseInt(resp.nonce); 43 44 source = Account.NewAccount(); 45 46 var tx = new Transaction(ChainID, originSource, source, neb.nasToBasic(1000), nonce + 1, "1000000", "200000"); 47 tx.signTransaction(); 48 49 console.log("cliam source tx:", tx.toString()); 50 51 return neb.api.sendRawTransaction(tx.toProtoString()); 52 }).then(function (resp) { 53 console.log("send Raw Tx:" + JSON.stringify(resp)); 54 expect(resp).to.be.have.property('txhash'); 55 checkTransaction(resp.txhash, 0, function (receipt) { 56 console.log("tx receipt : " + JSON.stringify(receipt)); 57 expect(receipt).to.be.have.property('status').equal(1); 58 59 done(); 60 }); 61 }).catch(function (err) { 62 done(err); 63 }); 64 } 65 66 function prepareContractCall(testCase, done) { 67 neb.api.getAccountState(source.getAddressString()).then(function (resp) { 68 console.log("source account state:" + JSON.stringify(resp)); 69 lastnonce = parseInt(resp.nonce); 70 71 var accounts = new Array(); 72 var values = new Array(); 73 // if (typeof contractAddr === "undefined") { 74 deploy = Account.NewAccount(); 75 accounts.push(deploy); 76 values.push(neb.nasToBasic(1)); 77 // } 78 79 from = Account.NewAccount(); 80 accounts.push(from); 81 82 var fromBalance = (typeof testCase.testInput.fromBalance === "undefined") ? neb.nasToBasic(1) : testCase.testInput.fromBalance; 83 values.push(fromBalance); 84 85 cliamTokens(accounts, values, function () { 86 // if (typeof contractAddr === "undefined") { 87 deployContract(done); 88 // } else { 89 // done(); 90 // } 91 }); 92 93 }); 94 } 95 96 function testContractCall(testInput, testExpect, done) { 97 var fromAcc = (typeof testInput.from === "undefined") ? from : testInput.from; 98 var to = (typeof testInput.to === "undefined") ? Account.fromAddress(contractAddr) : testInput.to; 99 100 neb.api.getAccountState(from.getAddressString()).then(function (resp) { 101 fromState = resp; 102 console.log("from state:", JSON.stringify(resp)); 103 return neb.api.getAccountState(coinbase); 104 }).catch (function (err) { 105 done(err); 106 }).then(function (resp) { 107 console.log("coin state:", JSON.stringify(resp)); 108 coinState = resp; 109 110 var tx = new Transaction(ChainID, fromAcc, to, testInput.value, parseInt(fromState.nonce) + testInput.nonce, testInput.gasPrice, testInput.gasLimit, testInput.contract); 111 // test invalid address 112 tx.from.address = fromAcc.address; 113 tx.to.address = to.address; 114 tx.gasPrice = new BigNumber(testInput.gasPrice); 115 tx.gasLimit = new BigNumber(testInput.gasLimit); 116 if (testInput.sign) { 117 tx.signTransaction(); 118 } else if (testInput.fakeSign) { 119 //replcce the privkey to sign 120 tx.signTransaction(); 121 console.log("this is the right signature:" + tx.sign.toString('hex')); 122 console.log("repalce the privkey and sign another signatrue..."); 123 var newAccount = new Account("a6e5eb222e4538fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f"); 124 var privKey = tx.from.privKey; 125 tx.from.privKey = newAccount.privKey; 126 tx.signTransaction(); 127 console.log("now signatrue is: " + tx.sign.toString('hex')); 128 tx.from.privKey = privKey; 129 } 130 // console.log("tx raw:", tx.toString()); 131 return neb.api.sendRawTransaction(tx.toProtoString()); 132 }).then(function (rawResp) { 133 if (true === testExpect.canSendTx) { 134 console.log("send Raw Tx:" + JSON.stringify(rawResp)); 135 expect(rawResp).to.be.have.property('txhash'); 136 checkTransaction(rawResp.txhash, 0, function (receipt) { 137 console.log("tx receipt : " + JSON.stringify(receipt)); 138 try { 139 if (true === testExpect.canSubmitTx) { 140 expect(receipt).to.not.be.a('undefined'); 141 if (true === testExpect.canExcuteTx) { 142 expect(receipt).to.be.have.property('status').equal(1); 143 } else { 144 expect(receipt).to.be.have.property('status').equal(0); 145 } 146 147 neb.api.getAccountState(receipt.from).then(function (state) { 148 149 console.log("get from account state :" + JSON.stringify(state)); 150 expect(state.balance).to.equal(testExpect.fromBalanceAfterTx); 151 return neb.api.getAccountState(contractAddr); 152 }).then(function (state) { 153 154 console.log("get contractAddr account state :" + JSON.stringify(state)); 155 expect(state.balance).to.equal(testExpect.toBalanceAfterTx); 156 return neb.api.getAccountState(coinbase); 157 }).then(function (state) { 158 159 console.log("get coinbase account state before tx:" + JSON.stringify(coinState)); 160 console.log("get coinbase account state after tx:" + JSON.stringify(state)); 161 var reward = new BigNumber(state.balance).sub(coinState.balance); 162 reward = reward.mod(new BigNumber(1.42694).mul(new BigNumber(10).pow(18))); 163 // The transaction should be only 164 expect(reward.toString()).to.equal(testExpect.transferReward); 165 if (receipt.gasUsed) { 166 var txCost = new BigNumber(receipt.gasUsed).mul(receipt.gasPrice).toString(10); 167 expect(txCost).to.equal(testExpect.transferReward); 168 } 169 170 return neb.api.getEventsByHash(receipt.hash); 171 }).then(function (events) { 172 for (var i = 0; i < events.events.length; i++) { 173 var event = events.events[i]; 174 //console.log("tx event:", JSON.stringify(event,null,'\t')); 175 console.log("tx event:", event.data); 176 if (event.topic === "chain.transactionResult") { 177 var result = JSON.parse(event.data); 178 expect(result.status).to.equal(testExpect.status); 179 180 if (testExpect.hasOwnProperty("eventErr")){ 181 console.log("Event error checked."); 182 expect(result.error).to.equal(testExpect.eventErr); 183 } 184 } 185 } 186 done(); 187 }).catch(function (err) { 188 console.log("exe tx err:", err); 189 done(err); 190 }); 191 } else { 192 if (receipt.status) { 193 expect(receipt.status).to.equal(2); 194 } 195 console.log("transaction can send but submit failed"); 196 done(); 197 } 198 } catch (err) { 199 console.log("submit tx err:", err.message); 200 done(err); 201 } 202 }); 203 } else { 204 console.log("send tx unexpected:", rawResp); 205 done(new Error("send tx should failed")); 206 } 207 }).catch(function (err) { 208 if (true === testExpect.canSendTx) { 209 done(err); 210 } else { 211 console.log("send tx failed:", err.message); 212 done(); 213 } 214 }); 215 216 } 217 218 function cliamTokens(accounts, values, done) { 219 for (var i = 0; i < accounts.length; i++) { 220 console.log("acc:"+accounts[i].getAddressString()+" value:"+values[i]); 221 sendTransaction(source, accounts[i], values[i], ++lastnonce); 222 sleep(30); 223 } 224 checkCliamTokens(done); 225 } 226 227 function sendTransaction(from, address, value, nonce) { 228 var transaction = new Transaction(ChainID, from, address, value, nonce, "1000000", "200000"); 229 transaction.signTransaction(); 230 var rawTx = transaction.toProtoString(); 231 neb.api.sendRawTransaction(rawTx).then(function (resp) { 232 console.log("send raw transaction resp:" + JSON.stringify(resp)); 233 }); 234 } 235 236 function checkCliamTokens(done) { 237 var intervalAccount = setInterval(function () { 238 neb.api.getAccountState(source.getAddressString()).then(function (resp) { 239 // console.log("master accountState resp:" + JSON.stringify(resp)); 240 var nonce = parseInt(resp.nonce); 241 console.log("check cliam tokens nonce:", lastnonce); 242 243 if (lastnonce <= nonce){ 244 console.log("cliam tokens success"); 245 clearInterval(intervalAccount); 246 done(); 247 } 248 }); 249 }, 2000); 250 } 251 252 function deployContract(done){ 253 254 // create contract 255 var source = FS.readFileSync("../../../nf/nvm/test/NRC20.js", "utf-8"); 256 var contract = { 257 "source": source, 258 "sourceType": "js", 259 "args": "[\"StandardToken\", \"NRC\", 18, \"1000000000\"]" 260 }; 261 262 var transaction = new Transaction(ChainID, deploy, deploy, "0", 1, "10000000", "2000000", contract); 263 transaction.signTransaction(); 264 var rawTx = transaction.toProtoString(); 265 266 // console.log("contract:" + rawTx); 267 neb.api.sendRawTransaction(rawTx).then(function (resp) { 268 console.log("deploy contract:" + JSON.stringify(resp)); 269 270 checkTransaction(resp.txhash, 0, done); 271 }); 272 } 273 274 function checkTransaction(txhash, retry, done){ 275 276 var maxRetry = 45; 277 278 // contract status and get contract_address 279 var interval = setTimeout(function () { 280 neb.api.getTransactionReceipt(txhash).then(function (resp) { 281 retry++; 282 283 console.log("check transaction status:" + resp.status); 284 if(resp.status && resp.status === 1) { 285 // clearInterval(interval); 286 287 if (resp.contract_address) { 288 console.log("deploy private key:" + deploy.getPrivateKeyString()); 289 console.log("deploy address:" + deploy.getAddressString()); 290 console.log("deploy contract address:" + resp.contract_address); 291 // console.log("deploy receipt:" + JSON.stringify(resp)); 292 293 contractAddr = resp.contract_address; 294 295 // checkNRCBalance(resp.from, resp.contract_address); 296 } 297 298 done(resp); 299 } else if (resp.status && resp.status === 2) { 300 if (retry > maxRetry) { 301 console.log("check transaction time out"); 302 // clearInterval(interval); 303 done(resp); 304 } else { 305 checkTransaction(txhash, retry++, done); 306 } 307 } else { 308 // clearInterval(interval); 309 console.log("transaction execution failed"); 310 done(resp); 311 } 312 }).catch(function (err) { 313 retry++; 314 console.log("check transaction not found retry"); 315 if (retry > maxRetry) { 316 console.log(JSON.stringify(err.error)); 317 // clearInterval(interval); 318 done(err); 319 } else { 320 checkTransaction(txhash, retry++, done); 321 } 322 }); 323 324 }, 2000); 325 } 326 327 function checkNRCBalance(address, contractAddress) { 328 var contract = { 329 "function": "balanceOf", 330 "args": "[\"" + address + "\"]" 331 }; 332 333 neb.api.call(address, contractAddress, "0", 2, "1000000", "2000000", contract).then(function (resp) { 334 console.log("balance of NRC:" + JSON.stringify(resp)); 335 }); 336 } 337 338 var testCase = { 339 "name": "1. normal call", 340 "testInput": { 341 sign: true, 342 from: from, 343 to: contractAddr, 344 value: "1", 345 nonce: 1, 346 gasPrice: 1000000, 347 gasLimit: 2000000, 348 contract: { 349 "function": "name", 350 "args": "" 351 } 352 }, 353 "testExpect": { 354 canSendTx: true, 355 canSubmitTx: true, 356 canExcuteTx: true, 357 status: 1, 358 fromBalanceAfterTx: '999999979861999999', 359 toBalanceAfterTx: '1', 360 transferReward: '20138000000', 361 eventErr: "" 362 } 363 }; 364 testCases.push(testCase); 365 366 var invalidFrom = Account.NewAccount(); 367 invalidFrom.address = CryptoUtils.toBuffer("12af"); 368 testCase = { 369 "name": "2. from address invalid", 370 "testInput": { 371 sign: true, 372 from: invalidFrom, 373 to: contractAddr, 374 value: "1", 375 nonce: 1, 376 gasPrice: 1000000, 377 gasLimit: 2000000, 378 contract: { 379 "function": "name", 380 "args": "" 381 } 382 }, 383 "testExpect": { 384 canSendTx: false, 385 canSubmitTx: false, 386 canExcuteTx: false, 387 status: 0, 388 fromBalanceAfterTx: neb.nasToBasic(1), 389 toBalanceAfterTx: '0', 390 transferReward: '0' 391 } 392 }; 393 testCases.push(testCase); 394 395 var invalidTo = Account.NewAccount(); 396 invalidTo.address = CryptoUtils.toBuffer("12af"); 397 testCase = { 398 "name": "3. to address invalid", 399 "testInput": { 400 sign: true, 401 from: from, 402 to: invalidTo, 403 value: "1", 404 nonce: 1, 405 gasPrice: 1000000, 406 gasLimit: 2000000, 407 contract: { 408 "function": "name", 409 "args": "" 410 } 411 }, 412 "testExpect": { 413 canSendTx: false, 414 canSubmitTx: false, 415 canExcuteTx: false, 416 status: 0, 417 fromBalanceAfterTx: neb.nasToBasic(1), 418 toBalanceAfterTx: '0', 419 transferReward: '0' 420 } 421 }; 422 testCases.push(testCase); 423 424 testCase = { 425 "name": "4. not contract address", 426 "testInput": { 427 sign: true, 428 from: from, 429 to: Account.NewAccount(), 430 value: "1", 431 nonce: 1, 432 gasPrice: 1000000, 433 gasLimit: 2000000, 434 contract: { 435 "function": "name", 436 "args": "" 437 } 438 }, 439 "testExpect": { 440 canSendTx: false, 441 canSubmitTx: false, 442 canExcuteTx: false, 443 status: 0, 444 fromBalanceAfterTx: neb.nasToBasic(1), 445 toBalanceAfterTx: '0', 446 transferReward: '0' 447 } 448 }; 449 testCases.push(testCase); 450 451 testCase = { 452 "name": "5. signature invalid", 453 "testInput": { 454 sign: false, 455 from: from, 456 to: contractAddr, 457 value: "1", 458 nonce: 1, 459 gasPrice: 1000000, 460 gasLimit: 2000000, 461 contract: { 462 "function": "name", 463 "args": "" 464 } 465 }, 466 "testExpect": { 467 canSendTx: false, 468 canSubmitTx: false, 469 canExcuteTx: false, 470 status: 0, 471 fromBalanceAfterTx: neb.nasToBasic(1), 472 toBalanceAfterTx: '0', 473 transferReward: '0' 474 } 475 }; 476 testCases.push(testCase); 477 478 testCase = { 479 "name": "5. signature is fake", 480 "testInput": { 481 fakeSign: true, 482 from: from, 483 to: contractAddr, 484 value: "1", 485 nonce: 1, 486 gasPrice: 1000000, 487 gasLimit: 2000000, 488 contract: { 489 "function": "name", 490 "args": "" 491 } 492 }, 493 "testExpect": { 494 canSendTx: false, 495 canSubmitTx: false, 496 canExcuteTx: false, 497 status: 0, 498 fromBalanceAfterTx: neb.nasToBasic(1), 499 toBalanceAfterTx: '0', 500 transferReward: '0' 501 } 502 }; 503 testCases.push(testCase); 504 505 testCase = { 506 "name": "6. nonce < from.nonce + 1", 507 "testInput": { 508 sign: true, 509 from: from, 510 to: contractAddr, 511 value: "1", 512 nonce: 0, 513 gasPrice: 1000000, 514 gasLimit: 2000000, 515 contract: { 516 "function": "name", 517 "args": "" 518 } 519 }, 520 "testExpect": { 521 canSendTx: false, 522 canSubmitTx: false, 523 canExcuteTx: false, 524 status: 0, 525 fromBalanceAfterTx: neb.nasToBasic(1), 526 toBalanceAfterTx: '0', 527 transferReward: '0' 528 } 529 }; 530 testCases.push(testCase); 531 532 testCase = { 533 "name": "7. nonce = from.nonce + 1", 534 "testInput": { 535 sign: true, 536 from: from, 537 to: contractAddr, 538 value: "1", 539 nonce: 1, 540 gasPrice: 1000000, 541 gasLimit: 2000000, 542 contract: { 543 "function": "name", 544 "args": "" 545 } 546 }, 547 "testExpect": { 548 canSendTx: true, 549 canSubmitTx: true, 550 canExcuteTx: true, 551 status: 1, 552 fromBalanceAfterTx: "999999979861999999", 553 toBalanceAfterTx: '1', 554 transferReward: '20138000000', 555 eventErr: "" 556 } 557 }; 558 testCases.push(testCase); 559 560 testCase = { 561 "name": "8. nonce > from.nonce + 1", 562 "testInput": { 563 sign: true, 564 from: from, 565 to: contractAddr, 566 value: "1", 567 nonce: 2, 568 gasPrice: 1000000, 569 gasLimit: 2000000, 570 contract: { 571 "function": "name", 572 "args": "" 573 } 574 }, 575 "testExpect": { 576 canSendTx: true, 577 canSubmitTx: false, 578 canExcuteTx: false, 579 status: 2, 580 fromBalanceAfterTx: neb.nasToBasic(1), 581 toBalanceAfterTx: '0', 582 transferReward: '0', 583 eventErr: "" 584 } 585 }; 586 testCases.push(testCase); 587 588 testCase = { 589 "name": "9. gasPrice = 0", 590 "testInput": { 591 sign: true, 592 from: from, 593 to: contractAddr, 594 value: "1", 595 nonce: 1, 596 gasPrice: 0, 597 gasLimit: 2000000, 598 contract: { 599 "function": "name", 600 "args": "" 601 } 602 }, 603 "testExpect": { 604 canSendTx: false, 605 canSubmitTx: false, 606 canExcuteTx: false, 607 status: 1, 608 fromBalanceAfterTx: neb.nasToBasic(1), 609 toBalanceAfterTx: '0', 610 transferReward: '0', 611 eventErr: "" 612 } 613 }; 614 testCases.push(testCase); 615 616 testCase = { 617 "name": "10. gasPrice > 0 && gasPrice < txpool.gasPrice", 618 "testInput": { 619 sign: true, 620 from: from, 621 to: contractAddr, 622 value: "1", 623 nonce: 1, 624 gasPrice: 10000, 625 gasLimit: 2000000, 626 contract: { 627 "function": "name", 628 "args": "" 629 } 630 }, 631 "testExpect": { 632 canSendTx: false, 633 canSubmitTx: false, 634 canExcuteTx: false, 635 status: 0, 636 fromBalanceAfterTx: neb.nasToBasic(1), 637 toBalanceAfterTx: '0', 638 transferReward: '0' 639 } 640 }; 641 testCases.push(testCase); 642 643 testCase = { 644 "name": "11. gasPrice = txpool.gasPrice", 645 "testInput": { 646 sign: true, 647 from: from, 648 to: contractAddr, 649 value: "1", 650 nonce: 1, 651 gasPrice: 1000000, 652 gasLimit: 2000000, 653 contract: { 654 "function": "name", 655 "args": "" 656 } 657 }, 658 "testExpect": { 659 canSendTx: true, 660 canSubmitTx: true, 661 canExcuteTx: true, 662 status: 1, 663 fromBalanceAfterTx: "999999979861999999", 664 toBalanceAfterTx: '1', 665 transferReward: '20138000000', 666 eventErr: "" 667 } 668 }; 669 testCases.push(testCase); 670 671 testCase = { 672 "name": "12. gasPrice > txpool.gasPrice", 673 "testInput": { 674 sign: true, 675 from: from, 676 to: contractAddr, 677 value: "1", 678 nonce: 1, 679 gasPrice: 2000000, 680 gasLimit: 2000000, 681 contract: { 682 "function": "name", 683 "args": "" 684 } 685 }, 686 "testExpect": { 687 canSendTx: true, 688 canSubmitTx: true, 689 canExcuteTx: true, 690 status: 1, 691 fromBalanceAfterTx: "999999959723999999", 692 toBalanceAfterTx: '1', 693 transferReward: '40276000000', 694 eventErr: "" 695 } 696 }; 697 testCases.push(testCase); 698 699 testCase = { 700 "name": "13. gasLimit = 0", 701 "testInput": { 702 sign: true, 703 from: from, 704 to: contractAddr, 705 value: "1", 706 nonce: 1, 707 gasPrice: 1000000, 708 gasLimit: 0, 709 contract: { 710 "function": "name", 711 "args": "" 712 } 713 }, 714 "testExpect": { 715 canSendTx: false, 716 canSubmitTx: false, 717 canExcuteTx: false, 718 status: 0, 719 fromBalanceAfterTx: neb.nasToBasic(1), 720 toBalanceAfterTx: '0', 721 transferReward: '0' 722 } 723 }; 724 testCases.push(testCase); 725 726 testCase = { 727 "name": "14. gasLimit < TxBaseGasCount + gasCountOfPayload", 728 "testInput": { 729 sign: true, 730 from: from, 731 to: contractAddr, 732 value: "1", 733 nonce: 1, 734 gasPrice: 1000000, 735 gasLimit: 20000, 736 contract: { 737 "function": "name", 738 "args": "" 739 } 740 }, 741 "testExpect": { 742 canSendTx: true, 743 canSubmitTx: false, 744 canExcuteTx: false, 745 status: 0, 746 fromBalanceAfterTx: neb.nasToBasic(1), 747 toBalanceAfterTx: '0', 748 transferReward: '0' 749 } 750 }; 751 testCases.push(testCase); 752 753 testCase = { 754 "name": "15. gasLimit = TxBaseGasCount + gasCountOfPayload", 755 "testInput": { 756 sign: true, 757 from: from, 758 to: contractAddr, 759 value: "1", 760 nonce: 1, 761 gasPrice: 1000000, 762 gasLimit: 20029, 763 contract: { 764 "function": "name", 765 "args": "" 766 } 767 }, 768 "testExpect": { 769 canSendTx: true, 770 canSubmitTx: true, 771 canExcuteTx: false, 772 status: 0, 773 fromBalanceAfterTx: "999999979971000000", 774 toBalanceAfterTx: '0', 775 transferReward: '20029000000', 776 eventErr: "out of gas limit" 777 } 778 }; 779 testCases.push(testCase); 780 781 testCase = { 782 "name": "16. gasLimit < TxBaseGasCount + gasCountOfPayload + gasCountOfpayloadExecuted", 783 "testInput": { 784 sign: true, 785 from: from, 786 to: contractAddr, 787 value: "1", 788 nonce: 1, 789 gasPrice: 1000000, 790 gasLimit: 20100, 791 contract: { 792 "function": "name", 793 "args": "" 794 } 795 }, 796 "testExpect": { 797 canSendTx: true, 798 canSubmitTx: true, 799 canExcuteTx: false, 800 status: 0, 801 fromBalanceAfterTx: "999999979900000000", 802 toBalanceAfterTx: '0', 803 transferReward: '20100000000', 804 eventErr: 'insufficient gas' 805 } 806 }; 807 testCases.push(testCase); 808 809 testCase = { 810 "name": "17. gasLimit = TxBaseGasCount + gasCountOfPayload + gasCountOfpayloadExecuted", 811 "testInput": { 812 sign: true, 813 from: from, 814 to: contractAddr, 815 value: "1", 816 nonce: 1, 817 gasPrice: 1000000, 818 gasLimit: 20138, 819 contract: { 820 "function": "name", 821 "args": "" 822 } 823 }, 824 "testExpect": { 825 canSendTx: true, 826 canSubmitTx: true, 827 canExcuteTx: true, 828 status: 1, 829 fromBalanceAfterTx: "999999979861999999", 830 toBalanceAfterTx: '1', 831 transferReward: '20138000000', 832 eventErr: "" 833 } 834 }; 835 testCases.push(testCase); 836 837 testCase = { 838 "name": "18. gasLimit > TxBaseGasCount + gasCountOfPayload + gasCountOfpayloadExecuted", 839 "testInput": { 840 sign: true, 841 from: from, 842 to: contractAddr, 843 value: "1", 844 nonce: 1, 845 gasPrice: 1000000, 846 gasLimit: 2000000, 847 contract: { 848 "function": "name", 849 "args": "" 850 } 851 }, 852 "testExpect": { 853 canSendTx: true, 854 canSubmitTx: true, 855 canExcuteTx: true, 856 status: 1, 857 fromBalanceAfterTx: "999999979861999999", 858 toBalanceAfterTx: '1', 859 transferReward: '20138000000', 860 eventErr: "" 861 } 862 }; 863 testCases.push(testCase); 864 865 testCase = { 866 "name": "19. gasLimit > txpool.gasLimit", 867 "testInput": { 868 sign: true, 869 from: from, 870 to: contractAddr, 871 value: "1", 872 nonce: 1, 873 gasPrice: 1000000, 874 gasLimit: "500000000000", 875 contract: { 876 "function": "name", 877 "args": "" 878 } 879 }, 880 "testExpect": { 881 canSendTx: false, 882 canSubmitTx: false, 883 canExcuteTx: false, 884 status: 0, 885 fromBalanceAfterTx: neb.nasToBasic(1), 886 toBalanceAfterTx: '0', 887 transferReward: '0' 888 } 889 }; 890 testCases.push(testCase); 891 892 testCase = { 893 "name": "20. balanceOfFrom < gasPrice*gasLimit", 894 "testInput": { 895 fromBalance: "1", 896 sign: true, 897 from: from, 898 to: contractAddr, 899 value: "1", 900 nonce: 1, 901 gasPrice: 1000000, 902 gasLimit: 2000000, 903 contract: { 904 "function": "name", 905 "args": "" 906 } 907 }, 908 "testExpect": { 909 canSendTx: true, 910 canSubmitTx: false, 911 canExcuteTx: false, 912 status: 0, 913 fromBalanceAfterTx: "1", 914 toBalanceAfterTx: '0', 915 transferReward: '0' 916 } 917 }; 918 testCases.push(testCase); 919 920 testCase = { 921 "name": "21. balanceOfFrom = gasPrice*gasLimit", 922 "testInput": { 923 fromBalance: "20138000000", 924 sign: true, 925 from: from, 926 to: contractAddr, 927 value: "1", 928 nonce: 1, 929 gasPrice: 1000000, 930 gasLimit: 20138, 931 contract: { 932 "function": "name", 933 "args": "" 934 } 935 }, 936 "testExpect": { 937 canSendTx: true, 938 canSubmitTx: false, 939 canExcuteTx: false, 940 status: 0, 941 fromBalanceAfterTx: "0", 942 toBalanceAfterTx: '0', 943 transferReward: '20138000000' 944 } 945 }; 946 testCases.push(testCase); 947 948 testCase = { 949 "name": "22. balanceOfFrom < (TxBaseGasCount + TxPayloadBaseGasCount[payloadType] + gasCountOfPayload + gasCountOfPayloadExecuted) * gasPrice + valueOfTx", 950 "testInput": { 951 fromBalance: "20138100000", 952 sign: true, 953 from: from, 954 to: contractAddr, 955 value: "1000000", 956 nonce: 1, 957 gasPrice: 1000000, 958 gasLimit: 20138, 959 contract: { 960 "function": "name", 961 "args": "" 962 } 963 }, 964 "testExpect": { 965 canSendTx: true, 966 canSubmitTx: false, 967 canExcuteTx: false, 968 status: 0, 969 fromBalanceAfterTx: "100000", 970 toBalanceAfterTx: '0', 971 transferReward: '20138000000' 972 } 973 }; 974 testCases.push(testCase); 975 976 testCase = { 977 "name": "23. balanceOfFrom = (TxBaseGasCount + TxPayloadBaseGasCount[payloadType] + gasCountOfPayload + gasCountOfPayloadExecuted) * gasPrice + valueOfTx", 978 "testInput": { 979 fromBalance: "20139000000", 980 sign: true, 981 from: from, 982 to: contractAddr, 983 value: "1000000", 984 nonce: 1, 985 gasPrice: 1000000, 986 gasLimit: 20138, 987 contract: { 988 "function": "name", 989 "args": "" 990 } 991 }, 992 "testExpect": { 993 canSendTx: true, 994 canSubmitTx: true, 995 canExcuteTx: true, 996 status: 1, 997 fromBalanceAfterTx: "0", 998 toBalanceAfterTx: '1000000', 999 transferReward: '20138000000', 1000 eventErr: "" 1001 } 1002 }; 1003 testCases.push(testCase); 1004 1005 testCase = { 1006 "name": "24. balanceOfFrom > (TxBaseGasCount + TxPayloadBaseGasCount[payloadType] + gasCountOfPayload + gasCountOfPayloadExecuted) * gasPrice + valueOfTx", 1007 "testInput": { 1008 fromBalance: "20140000000", 1009 sign: true, 1010 from: from, 1011 to: contractAddr, 1012 value: "1000000", 1013 nonce: 1, 1014 gasPrice: 1000000, 1015 gasLimit: 20138, 1016 contract: { 1017 "function": "name", 1018 "args": "" 1019 } 1020 }, 1021 "testExpect": { 1022 canSendTx: true, 1023 canSubmitTx: true, 1024 canExcuteTx: true, 1025 status: 1, 1026 fromBalanceAfterTx: "1000000", 1027 toBalanceAfterTx: '1000000', 1028 transferReward: '20138000000' 1029 } 1030 }; 1031 testCases.push(testCase); 1032 1033 testCase = { 1034 "name": "25. function not found", 1035 "testInput": { 1036 sign: true, 1037 from: from, 1038 to: contractAddr, 1039 value: "1", 1040 nonce: 1, 1041 gasPrice: 1000000, 1042 gasLimit: 2000000, 1043 contract: { 1044 "function": "functionNotFound", 1045 "args": "" 1046 } 1047 }, 1048 "testExpect": { 1049 canSendTx: true, 1050 canSubmitTx: true, 1051 canExcuteTx: false, 1052 status: 0, 1053 fromBalanceAfterTx: "999999979854000000", 1054 toBalanceAfterTx: '0', 1055 transferReward: '20146000000', 1056 eventErr: "Call: TypeError: Cannot read property 'apply' of undefined" 1057 } 1058 }; 1059 testCases.push(testCase); 1060 1061 testCase = { 1062 "name": "26. args more", 1063 "testInput": { 1064 sign: true, 1065 from: from, 1066 to: contractAddr, 1067 value: "1", 1068 nonce: 1, 1069 gasPrice: 1000000, 1070 gasLimit: 2000000, 1071 contract: { 1072 "function": "name", 1073 "args": "[1]" 1074 } 1075 }, 1076 "testExpect": { 1077 canSendTx: true, 1078 canSubmitTx: true, 1079 canExcuteTx: true, 1080 status: 1, 1081 fromBalanceAfterTx: "999999979858999999", 1082 toBalanceAfterTx: '1', 1083 transferReward: '20141000000', 1084 eventErr: "" 1085 } 1086 }; 1087 testCases.push(testCase); 1088 1089 testCase = { 1090 "name": "27. args less", 1091 "testInput": { 1092 sign: true, 1093 from: from, 1094 to: contractAddr, 1095 value: "1", 1096 nonce: 1, 1097 gasPrice: 1000000, 1098 gasLimit: 2000000, 1099 contract: { 1100 "function": "balanceOf", 1101 "args": "" 1102 } 1103 }, 1104 "testExpect": { 1105 canSendTx: true, 1106 canSubmitTx: true, 1107 canExcuteTx: true, 1108 status: 1, 1109 fromBalanceAfterTx: "999999979841999999", 1110 toBalanceAfterTx: '1', 1111 transferReward: '20158000000', 1112 eventErr: "" 1113 } 1114 }; 1115 testCases.push(testCase); 1116 1117 testCase = { 1118 "name": "28. args err", 1119 "testInput": { 1120 sign: true, 1121 from: from, 1122 to: contractAddr, 1123 value: "1", 1124 nonce: 1, 1125 gasPrice: 1000000, 1126 gasLimit: 2000000, 1127 contract: { 1128 "function": "transfer", 1129 "args": "[\"asda\", \"asda\"]" 1130 } 1131 }, 1132 "testExpect": { 1133 canSendTx: true, 1134 canSubmitTx: true, 1135 canExcuteTx: false, 1136 status: 0, 1137 fromBalanceAfterTx: "999999979831000000", 1138 toBalanceAfterTx: '0', 1139 transferReward: '20169000000', 1140 eventErr: "Call: BigNumber Error: new BigNumber() not a number: asda" 1141 } 1142 }; 1143 testCases.push(testCase); 1144 1145 testCase = { 1146 "name": "29. execution failed", 1147 "testInput": { 1148 sign: true, 1149 from: from, 1150 to: contractAddr, 1151 value: "1", 1152 nonce: 1, 1153 gasPrice: 1000000, 1154 gasLimit: 2000000, 1155 contract: { 1156 "function": "transfer", 1157 "args": "[\"n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17\", 1]" 1158 } 1159 }, 1160 "testExpect": { 1161 canSendTx: true, 1162 canSubmitTx: true, 1163 canExcuteTx: false, 1164 status: 0, 1165 fromBalanceAfterTx: "999999979734000000", 1166 toBalanceAfterTx: '0', 1167 transferReward: '20266000000', 1168 eventErr: "Call: Error: transfer failed." 1169 } 1170 }; 1171 testCases.push(testCase); 1172 1173 testCase = { 1174 "name": "30. execution success", 1175 "testInput": { 1176 sign: true, 1177 from: from, 1178 to: contractAddr, 1179 value: "1", 1180 nonce: 1, 1181 gasPrice: 1000000, 1182 gasLimit: 2000000, 1183 contract: { 1184 "function": "balanceOf", 1185 "args": "[\"n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17\"]" 1186 } 1187 }, 1188 "testExpect": { 1189 canSendTx: true, 1190 canSubmitTx: true, 1191 canExcuteTx: true, 1192 status: 1, 1193 fromBalanceAfterTx: "999999979800999999", 1194 toBalanceAfterTx: '1', 1195 transferReward: '20199000000', 1196 eventErr: "" 1197 } 1198 }; 1199 testCases.push(testCase); 1200 1201 1202 describe('contract call test', function () { 1203 before(function (done) { 1204 prepareSource(done); 1205 }); 1206 1207 // var testCase = testCases[27]; 1208 // it(testCase.name, function (done) { 1209 // prepareContractCall(testCase, function (err) { 1210 // if (err instanceof Error) { 1211 // done(err); 1212 // } else { 1213 // testContractCall(testCase.testInput, testCase.testExpect, done); 1214 // } 1215 // }); 1216 // }); 1217 1218 for (var i = 0; i < testCases.length; i++) { 1219 1220 it(testCases[i].name, function (done) { 1221 var testCase = testCases[caseIndex]; 1222 prepareContractCall(testCase, function (err) { 1223 if (err instanceof Error) { 1224 done(err); 1225 } else { 1226 testContractCall(testCase.testInput, testCase.testExpect, done); 1227 } 1228 }); 1229 }); 1230 } 1231 afterEach(function () { 1232 caseIndex++; 1233 console.log("case index:", caseIndex); 1234 }); 1235 });