github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/rpc/estimateGas.test.js (about) 1 'use strict'; 2 var expect = require('chai').expect; 3 var FS = require('fs'); 4 var BigNumber = require('bignumber.js'); 5 var HttpRequest = require("../../node-request"); 6 var rpc_client = require('./rpc_client/rpc_client.js'); 7 var Wallet = require("nebulas"); 8 var Neb = Wallet.Neb; 9 var neb = new Neb(); 10 var Account = Wallet.Account; 11 var Transaction = Wallet.Transaction; 12 var Utils = Wallet.Utils; 13 var Unit = Wallet.Unit; 14 15 var protocol_version = '/neb/1.0.0' 16 var node_version = '0.7.0' 17 var server_address = 'localhost:8684'; 18 var coinbase = "n1QZMXSZtW7BUerroSms4axNfyBGyFGkrh5"; 19 var sourceAccount = new Wallet.Account('d80f115bdbba5ef215707a8d7053c16f4e65588fd50b0f83369ad142b99891b5'); 20 neb.setRequest(new HttpRequest("http://localhost:8685")) 21 var chain_id = 100; 22 var env = 'local'; 23 chain_id = 1002; 24 if (env === 'testneb1') { 25 chain_id = 1001; 26 sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 27 coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17"; 28 server_address = "35.182.48.19:8684"; 29 30 } else if (env === "testneb2") { 31 chain_id = 1002; 32 sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 33 coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17"; 34 server_address = "34.205.26.12:8684"; 35 36 } else if (env === "testneb3") { 37 chain_id = 1003; 38 sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 39 coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17"; 40 server_address = "35.177.214.138:8684"; 41 42 } else if (env === "testneb4") { //super node 43 chain_id = 1004; 44 sourceAccount = new Wallet.Account("c75402f6ffe6edcc2c062134b5932151cb39b6486a7beb984792bb9da3f38b9f"); 45 coinbase = "n1EzGmFsVepKduN1U5QFyhLqpzFvM9sRSmG"; 46 server_address = "35.154.108.11:8684"; 47 } else if (env === "testneb4_normalnode"){ 48 chain_id = 1004; 49 sourceAccount = new Wallet.Account("c75402f6ffe6edcc2c062134b5932151cb39b6486a7beb984792bb9da3f38b9f"); 50 coinbase = "n1EzGmFsVepKduN1U5QFyhLqpzFvM9sRSmG"; 51 server_address = "18.197.107.228:8684"; 52 } else if (env === "local") { 53 chain_id = 100; 54 sourceAccount = new Wallet.Account("d80f115bdbba5ef215707a8d7053c16f4e65588fd50b0f83369ad142b99891b5"); 55 coinbase = "n1QZMXSZtW7BUerroSms4axNfyBGyFGkrh5"; 56 server_address = "127.0.0.1:8684"; 57 58 } else if (env === "maintest"){ 59 chain_id = 2; 60 sourceAccount = new Wallet.Account("d2319a8a63b1abcb0cc6d4183198e5d7b264d271f97edf0c76cfdb1f2631848c"); 61 coinbase = "n1dZZnqKGEkb1LHYsZRei1CH6DunTio1j1q"; 62 server_address = "54.149.15.132:8684"; 63 } else { 64 throw new Error("invalid env (" + env + ")."); 65 } 66 neb.setRequest(new HttpRequest("http://127.0.0.1:8685")); 67 68 var api_client; 69 var normalOutput; 70 var txHash; 71 var nonce; 72 var contractAddress; 73 74 var maxCheckTime = 20; 75 var checkTimes = 0; 76 77 function checkTransaction(hash, callback) { 78 checkTimes += 1; 79 if (checkTimes > maxCheckTime) { 80 console.log("check tx receipt timeout:" + hash); 81 checkTimes = 0; 82 callback(); 83 return; 84 } 85 neb.api.getTransactionReceipt(hash).then(function (resp) { 86 87 console.log("tx receipt status:" + resp.status); 88 if (resp.status === 2) { 89 setTimeout(function () { 90 checkTransaction(hash, callback); 91 }, 2000); 92 } else { 93 checkTimes = 0; 94 callback(resp); 95 } 96 }).catch(function (err) { 97 console.log(err.error); 98 console.log("maybe packing"); 99 setTimeout(function () { 100 checkTransaction(hash, callback); 101 }, 2000); 102 }); 103 } 104 105 //should not throw err 106 function verify(gas, testInput, done) { 107 try { 108 var balanceBeforeTx, balanceAfterTx; 109 } catch (err) { 110 done(err); 111 return; 112 } 113 114 neb.api.getAccountState(sourceAccount.getAddressString()).then(function (state) { 115 try { 116 balanceBeforeTx = new BigNumber(state.balance); 117 } catch (err) { 118 done(err) 119 return; 120 } 121 var admin_client = rpc_client.new_client(server_address, 'AdminService'); 122 var tx = new Transaction(chain_id, sourceAccount, sourceAccount.getAddressString(),testInput.value, testInput.nonce, 123 testInput.gas_price, testInput.gas_limit, testInput.contract); 124 tx.signTransaction(); 125 // console.log(tx.toString()); 126 return neb.api.sendRawTransaction(tx.toProtoString()); 127 }).then(function (resp) { 128 console.log(resp); 129 checkTransaction(resp.txhash, function (receipt) { 130 try { 131 console.log(receipt); 132 expect(receipt.status).not.to.be.a('undefined'); 133 } catch (err) { 134 done(err); 135 return; 136 } 137 try { 138 neb.api.getAccountState(sourceAccount.getAddressString()).then(function (state) { 139 balanceAfterTx = new BigNumber(state.balance); 140 var gasConsumed = balanceBeforeTx.sub(balanceAfterTx).div(new BigNumber(testInput.gas_price)); 141 expect((new BigNumber(gas)).toString()).to.be.equal(gasConsumed.toString()); 142 done(); 143 }).catch(function (err) { 144 console.log(err); 145 console.log("silent_debugggggg"); 146 done(err); 147 return; 148 }); 149 } catch (err) { 150 done(err.error); 151 return; 152 } 153 }); 154 }).catch(function (err) { 155 console.log("silent_debug"); 156 console.log(err); 157 done(err.error); 158 return; 159 }); 160 161 } 162 163 164 165 function testRpc(testInput, testExpect, done) { 166 api_client.EstimateGas(testInput.rpcInput, function (err, response) { 167 if (err) { 168 try { 169 expect(testExpect.errMsg).to.be.equal(err.details); 170 } catch (err) { 171 172 done(err); 173 return; 174 } 175 done(); 176 } else { 177 console.log(JSON.stringify(response)); 178 try { 179 expect(response.err).equal(testExpect.exeMsg); 180 } catch (err) { 181 console.log("unexpected errpr :", err); 182 done(err); 183 return; 184 } 185 var gas = parseInt(response.gas); 186 console.log(gas, "to verify"); 187 verify(gas, testInput.verifyInput, done); 188 } 189 }); 190 191 } 192 193 describe('rpc: estimateGas', function () { 194 // //unlock the sourceAccount 195 // before((done) => { 196 // var admin_client = rpc_client.new_client(server_address, 'AdminService'); 197 // var args = { 198 // address: sourceAccount.getAddressString(), 199 // passphrase: "passphrase", 200 // } 201 // admin_client.UnlockAccount(args, (err, resp) => { 202 // expect(err).to.be.equal(null); 203 // done(err); 204 // }) 205 // }); 206 //get nonce 207 beforeEach((done) => { 208 api_client = rpc_client.new_client(server_address); 209 api_client.GetAccountState({ address: sourceAccount.getAddressString() }, (err, resp) => { 210 try{ 211 expect(err).to.be.equal(null); 212 nonce = parseInt(resp.nonce); 213 console.log("nonce is: ", nonce); 214 done(err); 215 } catch(err) { 216 done(err); 217 return; 218 } 219 }); 220 }); 221 222 it('normal rpc', function (done) { 223 nonce = nonce + 1; 224 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 225 var contract = { 226 "source": erc20, 227 "source_type": "js", 228 "args": '["NebulasToken", "NAS", 1000000000]' 229 }; 230 231 var contractVerify = { 232 "source": erc20, 233 "sourceType": "js", 234 "args": '["NebulasToken", "NAS", 1000000000]' 235 }; 236 237 var testInput = { 238 rpcInput: { 239 from: sourceAccount.getAddressString(), 240 to: sourceAccount.getAddressString(), 241 value: "0", 242 nonce: nonce, 243 gas_price: "1000000", 244 gas_limit: "2000000", 245 contract: contract, 246 }, 247 verifyInput: { 248 from: sourceAccount.getAddressString(), 249 to: sourceAccount.getAddressString(), 250 value: "0", 251 nonce: nonce, 252 gas_price: "1000000", 253 gas_limit: "2000000", 254 contract: contractVerify, 255 }, 256 } 257 var testExpect = { 258 exeMsg: "", 259 } 260 testRpc(testInput, testExpect, done); 261 }); 262 263 it('value is invalid', function (done) { 264 nonce = nonce + 1; 265 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 266 var contract = { 267 "source": erc20, 268 "source_type": "js", 269 "args": '["NebulasToken", "NAS", 1000000000]' 270 } 271 272 var contractVerify = { 273 "source": erc20, 274 "sourceType": "js", 275 "args": '["NebulasToken", "NAS", 1000000000]' 276 }; 277 var testInput = { 278 rpcInput: { 279 from: sourceAccount.getAddressString(), 280 to: sourceAccount.getAddressString(), 281 value: "0a", 282 nonce: 100000000, 283 gas_price: "1000000", 284 gas_limit: "200000", 285 contract: contract, 286 }, 287 verifyInput: { 288 from: sourceAccount.getAddressString(), 289 to: sourceAccount.getAddressString(), 290 value: "0", 291 nonce: nonce, 292 gas_price: "1000000", 293 gas_limit: "200000", 294 contract: contractVerify, 295 }, 296 } 297 298 var testExpect = { 299 errMsg: 'invalid value' 300 } 301 302 testRpc(testInput, testExpect, done); 303 }) 304 305 it('value is empty', function (done) { 306 nonce = nonce + 1; 307 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 308 309 var contract = { 310 "source": erc20, 311 "source_type": "js", 312 "args": '["NebulasToken", "NAS", 1000000000]', 313 }; 314 var contractVerify = { 315 "source": erc20, 316 "sourceType": "js", 317 "args": '["NebulasToken", "NAS", 1000000000]' 318 }; 319 var testInput = { 320 rpcInput: { 321 from: sourceAccount.getAddressString(), 322 to: sourceAccount.getAddressString(), 323 nonce: 100000000, 324 gas_price: "1000000", 325 gas_limit: "200000", 326 contract: contract, 327 }, 328 verifyInput: { 329 from: sourceAccount.getAddressString(), 330 to: sourceAccount.getAddressString(), 331 nonce: nonce, 332 gas_price: "1000000", 333 gas_limit: "200000", 334 contract: contractVerify, 335 }, 336 } 337 338 var testExpect = { 339 errMsg: 'invalid value' 340 } 341 testRpc(testInput, testExpect, done); 342 }); 343 344 it('nonce is large', function (done) { 345 nonce = nonce + 1; 346 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 347 var contract = { 348 "source": erc20, 349 "source_type": "js", 350 "args": '["NebulasToken", "NAS", 1000000000]' 351 } 352 var contractVerify = { 353 "source": erc20, 354 "sourceType": "js", 355 "args": '["NebulasToken", "NAS", 1000000000]' 356 }; 357 var testInput = { 358 rpcInput: { 359 from: sourceAccount.getAddressString(), 360 to: sourceAccount.getAddressString(), 361 value: "0", 362 nonce: 100000000, 363 gas_price: "1000000", 364 gas_limit: "200000", 365 contract: contract, 366 }, 367 verifyInput: { 368 from: sourceAccount.getAddressString(), 369 to: sourceAccount.getAddressString(), 370 value: "0", 371 nonce: nonce, 372 gas_price: "1000000", 373 gas_limit: "200000", 374 contract: contractVerify, 375 }, 376 } 377 var testExpect = { 378 exeMsg: "" 379 }; 380 testRpc(testInput, testExpect, done); 381 }); 382 383 it('nonce is empty', function (done) { 384 nonce = nonce + 1; 385 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 386 var contract = { 387 "source": erc20, 388 "source_type": "js", 389 "args": '["NebulasToken", "NAS", 1000000000]' 390 }; 391 var contractVerify = { 392 "source": erc20, 393 "sourceType": "js", 394 "args": '["NebulasToken", "NAS", 1000000000]' 395 }; 396 var testInput = { 397 rpcInput: { 398 from: sourceAccount.getAddressString(), 399 to: sourceAccount.getAddressString(), 400 value: "0", 401 gas_price: "1000000", 402 gas_limit: "200000", 403 contract: contract, 404 }, 405 verifyInput: { 406 from: sourceAccount.getAddressString(), 407 to: sourceAccount.getAddressString(), 408 value: "0", 409 nonce: nonce, 410 gas_price: "1000000", 411 gas_limit: "200000", 412 contract: contractVerify, 413 }, 414 } 415 var testExpect = { 416 exeMsg: "" 417 }; 418 testRpc(testInput, testExpect, done); 419 }) 420 421 it('nonce is small', function (done) { 422 nonce = nonce + 1; 423 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 424 var contract = { 425 "source": erc20, 426 "source_type": "js", 427 "args": '["NebulasToken", "NAS", 1000000000]' 428 } 429 var contractVerify = { 430 "source": erc20, 431 "sourceType": "js", 432 "args": '["NebulasToken", "NAS", 1000000000]' 433 }; 434 var testInput = { 435 rpcInput: { 436 from: sourceAccount.getAddressString(), 437 to: sourceAccount.getAddressString(), 438 value: "0", 439 nonce: 1, 440 gas_price: "1000000", 441 gas_limit: "200000", 442 contract: contract, 443 }, 444 verifyInput: { 445 from: sourceAccount.getAddressString(), 446 to: sourceAccount.getAddressString(), 447 value: "0", 448 nonce: nonce, 449 gas_price: "1000000", 450 gas_limit: "200000", 451 contract: contractVerify, 452 }, 453 } 454 var testExpect = { 455 exeMsg: "" 456 }; 457 testRpc(testInput, testExpect, done); 458 }); 459 460 it('gasPrice is negative', function (done) { 461 nonce = nonce + 1; 462 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 463 var contract = { 464 "source": erc20, 465 "source_type": "js", 466 "args": '["NebulasToken", "NAS", 1000000000]' 467 }; 468 var contractVerify = { 469 "source": erc20, 470 "sourceType": "js", 471 "args": '["NebulasToken", "NAS", 1000000000]' 472 }; 473 var testInput = { 474 rpcInput: { 475 from: sourceAccount.getAddressString(), 476 to: sourceAccount.getAddressString(), 477 value: "0", 478 nonce: nonce, 479 gas_price: "-1", 480 gas_limit: "200000", 481 contract: contract, 482 }, 483 verifyInput: { 484 from: sourceAccount.getAddressString(), 485 to: sourceAccount.getAddressString(), 486 value: "0", 487 nonce: nonce, 488 gas_price: "1000000", 489 gas_limit: "200000", 490 contract: contractVerify, 491 }, 492 } 493 494 var testExpect = { 495 errMsg: 'invalid gasPrice' 496 } 497 498 testRpc(testInput, testExpect, done); 499 }); 500 501 it('gas_price is less than gasPrince of tx pool', function (done) { 502 nonce = nonce + 1; 503 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 504 var contract = { 505 "source": erc20, 506 "source_type": "js", 507 "args": '["NebulasToken", "NAS", 1000000000]' 508 }; 509 var contractVerify = { 510 "source": erc20, 511 "sourceType": "js", 512 "args": '["NebulasToken", "NAS", 1000000000]' 513 }; 514 var testInput = { 515 rpcInput: { 516 from: sourceAccount.getAddressString(), 517 to: sourceAccount.getAddressString(), 518 value: "0", 519 nonce: 1, 520 gas_price: "100", 521 gas_limit: "200000", 522 contract: contract, 523 }, 524 verifyInput: { 525 from: sourceAccount.getAddressString(), 526 to: sourceAccount.getAddressString(), 527 value: "0", 528 nonce: nonce, 529 gas_price: "1000000", 530 gas_limit: "200000", 531 contract: contractVerify, 532 }, 533 } 534 var testExpect = { 535 exeMsg: "" 536 }; 537 testRpc(testInput, testExpect, done); 538 }); 539 540 it('gasLimit is neg', function (done) { 541 nonce = nonce + 1; 542 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 543 var contract = { 544 "source": erc20, 545 "source_type": "js", 546 "args": '["NebulasToken", "NAS", 1000000000]' 547 } 548 var contractVerify = { 549 "source": erc20, 550 "sourceType": "js", 551 "args": '["NebulasToken", "NAS", 1000000000]' 552 }; 553 var testInput = { 554 rpcInput: { 555 from: sourceAccount.getAddressString(), 556 to: sourceAccount.getAddressString(), 557 value: "0", 558 nonce: 1, 559 gas_price: "1000000", 560 gas_limit: "-1", 561 contract: contract, 562 }, 563 verifyInput: { 564 from: sourceAccount.getAddressString(), 565 to: sourceAccount.getAddressString(), 566 value: "0", 567 nonce: nonce, 568 gas_price: "1000000", 569 gas_limit: "200000", 570 contract: contractVerify 571 }, 572 } 573 574 var testExpect = { 575 errMsg: 'invalid gasLimit' 576 } 577 578 testRpc(testInput, testExpect, done); 579 }); 580 581 it('gasLimit is sufficient', function (done) { 582 nonce = nonce + 1; 583 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 584 var contract = { 585 "source": erc20, 586 "source_type": "js", 587 "args": '["NebulasToken", "NAS", 1000000000]' 588 } 589 var contractVerify = { 590 "source": erc20, 591 "sourceType": "js", 592 "args": '["NebulasToken", "NAS", 1000000000]' 593 }; 594 var testInput = { 595 rpcInput: { 596 from: sourceAccount.getAddressString(), 597 to: sourceAccount.getAddressString(), 598 value: "0", 599 nonce: nonce, 600 gas_price: "1000000", 601 gas_limit: "2000", 602 contract: contract, 603 }, 604 verifyInput: { 605 from: sourceAccount.getAddressString(), 606 to: sourceAccount.getAddressString(), 607 value: "0", 608 nonce: nonce, 609 gas_price: "1000000", 610 gas_limit: "200000", 611 contract: contractVerify 612 }, 613 } 614 615 var testExpect = { 616 exeMsg: '' 617 } 618 619 testRpc(testInput, testExpect, done); 620 }); 621 622 it('tx failed to execute', function (done) { 623 nonce = nonce + 1; 624 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.initError.js", "utf-8"); 625 var contract = { 626 "source": erc20, 627 "source_type": "js", 628 "args": '["NebulasToken", "NAS", 1000000000]' 629 } 630 var contractVerify = { 631 "source": erc20, 632 "sourceType": "js", 633 "args": '["NebulasToken", "NAS", 1000000000]' 634 }; 635 var testInput = { 636 rpcInput: { 637 from: sourceAccount.getAddressString(), 638 to: sourceAccount.getAddressString(), 639 value: "0", 640 nonce: nonce, 641 gas_price: "1000000", 642 gas_limit: "200000", 643 contract: contract, 644 }, 645 verifyInput: { 646 from: sourceAccount.getAddressString(), 647 to: sourceAccount.getAddressString(), 648 value: "0", 649 nonce: nonce, 650 gas_price: "1000000", 651 gas_limit: "200000", 652 contract: contractVerify 653 }, 654 } 655 656 var testExpect = { 657 exeMsg: 'Deploy: fail to init' 658 } 659 660 testRpc(testInput, testExpect, done); 661 }); 662 663 it('contract type is empty', function (done) { 664 nonce = nonce + 1; 665 var erc20 = FS.readFileSync("./nf/nvm/test/ERC20.js", "utf-8"); 666 var contract = { 667 "source": erc20, 668 "args": '["NebulasToken", "NAS", 1000000000]' 669 } 670 var contractVerify = { 671 "source": erc20, 672 "sourceType": "js", 673 "args": '["NebulasToken", "NAS", 1000000000]' 674 }; 675 var testInput = { 676 rpcInput: { 677 from: sourceAccount.getAddressString(), 678 to: sourceAccount.getAddressString(), 679 value: "0", 680 nonce: nonce, 681 gas_price: "1000000", 682 gas_limit: "200000", 683 contract: contract, 684 }, 685 verifyInput: { 686 from: sourceAccount.getAddressString(), 687 to: sourceAccount.getAddressString(), 688 value: "0", 689 nonce: nonce, 690 gas_price: "1000000", 691 gas_limit: "200000", 692 contract: contractVerify 693 }, 694 } 695 696 var testExpect = { 697 errMsg: 'invalid source type of deploy payload' 698 } 699 700 testRpc(testInput, testExpect, done); 701 }); 702 703 }); 704