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