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