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