github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/stress/stress.test.same.fromTo.bignonce.js (about)

     1  'use strict';
     2  
     3  var Wallet = require("nebulas");
     4  //var HttpRequest = Wallet.HttpRequest
     5  var HttpRequest = require("../../node-request.js");
     6  var utils = Wallet.Utils;
     7  var schedule = require('node-schedule');
     8  var sleep = require("system-sleep");
     9  
    10  var env; // local testneb1 testneb2
    11  var AddressNumber = 20;
    12  var EachAccountSendTimes = 20;
    13  
    14  var args = process.argv.splice(2);
    15  env = args[0];
    16  
    17  var Neb = Wallet.Neb;
    18  var neb = new Neb();
    19  
    20  var ChainID;
    21  var from;
    22  var accountArray;
    23  // var to = Wallet.Account.NewAccount();
    24  var lastnonce = 0;
    25  
    26  // statics for tps check start time.
    27  var startTime;
    28  
    29  var nodes = new Array();
    30  
    31  console.log(args);
    32  
    33  //local
    34  if (env === 'local') {
    35      neb.setRequest(new HttpRequest("http://127.0.0.1:8685")); //https://testnet.nebulas.io
    36      ChainID = 100;
    37      from = new Wallet.Account("a6e5eb290e1438fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f");
    38      nodes.push("http://127.0.0.1:8685");
    39  } else if (env === 'testneb1') {
    40      neb.setRequest(new HttpRequest("http://35.182.48.19:8685"));
    41      ChainID = 1001;
    42      from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    43      nodes.push("http://35.182.48.19:8685");
    44      nodes.push("http://13.57.245.249:8685");
    45      nodes.push("http://54.219.151.126:8685");
    46      nodes.push("http://18.218.165.90:8685");
    47      nodes.push("http://18.219.28.97:8685");
    48      nodes.push("http://13.58.44.3:8685");
    49      nodes.push("http://35.177.214.138:8685");
    50      nodes.push("http://35.176.94.224:8685");
    51  } else if (env === "testneb2") {
    52      neb.setRequest(new HttpRequest("http://34.205.26.12:8685"));
    53      ChainID = 1002;
    54      from = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52");
    55      nodes.push("http://34.205.26.12:8685");
    56      // nodes.push("http://54.206.9.246:8685");
    57      // nodes.push("http://54.252.158.117:8685");
    58      // nodes.push("http://34.206.53.244:8685");
    59      // nodes.push("http://34.205.53.3:8685");
    60      // nodes.push("http://52.3.226.40:8685");
    61  
    62  } else if (env === "testneb3") {
    63  
    64      //neb.setRequest(new HttpRequest("http://52.47.199.42:8685"));
    65      neb.setRequest(new HttpRequest("http://35.177.214.138:8685"));
    66      //neb.setRequest(new HttpRequest("http://13.127.227.177:8685"));
    67      ChainID = 1003;
    68      from = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52");
    69      //nodes.push("http://13.127.227.177:8685");
    70      nodes.push("http://35.177.214.138:8685");
    71      //nodes.push("http://52.47.199.42:8685");
    72  
    73  } else if (env === "testneb4") {
    74      neb.setRequest(new HttpRequest("http://34.208.233.164:8685"));
    75      ChainID = 1004;
    76      from = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52");
    77      nodes.push("http://34.208.233.164:8685");
    78      nodes.push("http://54.245.29.152:8685");
    79      nodes.push("http://52.34.73.0:8685");
    80      nodes.push("http://54.71.175.99:8685");
    81      nodes.push("http://34.213.130.120:8685");
    82      nodes.push("http://18.197.107.228:8685");
    83      nodes.push("http://18.197.106.150:8685");
    84      nodes.push("http://54.93.121.146:8685");
    85      nodes.push("http://18.195.159.210:8685");
    86      nodes.push("http://18.197.157.46:8685");
    87      nodes.push("http://18.228.3.118:8685");
    88      nodes.push("http://18.231.173.99:8685");
    89      nodes.push("http://18.231.124.140:8685");
    90      nodes.push("http://18.231.183.193:8685");
    91      nodes.push("http://18.231.162.23:8685");
    92      nodes.push("http://34.253.237.122:8685");
    93      nodes.push("http://34.244.129.30:8685");
    94      nodes.push("http://54.229.241.235:8685");
    95      nodes.push("http://54.229.177.109:8685");
    96      nodes.push("http://34.250.18.201:8685");
    97      nodes.push("http://13.127.227.177:8685");
    98  
    99  } else if (env === "liuliang") {
   100      neb.setRequest(new HttpRequest("http://35.154.108.11:8685"));
   101      ChainID = 1001;
   102      from = new Wallet.Account("c75402f6ffe6edcc2c062134b5932151cb39b6486a7beb984792bb9da3f38b9f");
   103      nodes.push("http://35.154.108.11:8685");
   104  
   105  } else if (env === "maintest"){
   106      ChainID = 2;
   107      from = new Wallet.Account("d2319a8a63b1abcb0cc6d4183198e5d7b264d271f97edf0c76cfdb1f2631848c");
   108      neb.setRequest(new HttpRequest("http://54.149.15.132:8685"));
   109      nodes.push("http://54.149.15.132:8685");
   110      nodes.push("http://18.188.27.35:8685");
   111      nodes.push("http://34.201.23.199:8685");
   112      nodes.push("http://13.251.33.39:8685");
   113      nodes.push("http://52.56.55.238:8685");
   114  
   115  } else if (env === "testnet_cal_super") {
   116      neb.setRequest(new HttpRequest("http://13.57.96.40:8685"));
   117      ChainID = 1001;
   118      from = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52");
   119      // from = new Wallet.Account("d2319a8a63b1abcb0cc6d4183198e5d7b264d271f97edf0c76cfdb1f2631848c");
   120  
   121      nodes.push("http://52.60.150.236:8685");
   122      nodes.push("http://13.57.96.40:8685");
   123  
   124  } else {
   125      console.log("please input correct env local testneb1 testneb2");
   126      return;
   127  }
   128  
   129  (function () {
   130      console.log("sending request to ", env);
   131      neb.api.getAccountState(from.getAddressString()).then(function (resp) {
   132          console.log("master accountState resp:" + JSON.stringify(resp));
   133          //lastnonce = parseInt(resp.result.nonce);
   134          lastnonce = parseInt(resp.nonce);
   135          console.log("lastnonce:", lastnonce);
   136  
   137          claimTokens(lastnonce);
   138      });
   139  })();
   140  
   141  function claimTokens(nonce) {
   142      console.log("initializing " + AddressNumber + " accounts with coins !!!")
   143      console.log(from.getAddressString());
   144  
   145      accountArray = new Array();
   146      for (var i = 0; i < AddressNumber; i++) {
   147          var account = Wallet.Account.NewAccount();
   148          accountArray.push(account);
   149          sendTransaction(0, 1, from, account, "1000000000000000", ++nonce, false, false, false);
   150          sleep(5);
   151      }
   152      checkClaimTokens();
   153  }
   154  
   155  function sendTransaction(index, totalTimes, from, to, value, nonce, randomToAddr, fromEqTo, bignonce) {
   156      if (index < totalTimes) {
   157  
   158          if (randomToAddr !== null && randomToAddr === true){
   159              var randomTo = Math.floor((Math.random() * AddressNumber));
   160              to = accountArray[randomTo];
   161          }
   162  
   163          var transaction = new Wallet.Transaction(ChainID, from, fromEqTo ? from : to, value, nonce);
   164          transaction.signTransaction();
   165          var rawTx = transaction.toProtoString();
   166  
   167          var i = Math.floor((Math.random() * nodes.length));
   168          var node = nodes[i];
   169          neb.setRequest(new HttpRequest(node));
   170          neb.api.sendRawTransaction(rawTx).then(function (resp) {
   171              console.log("send raw tx resp:" + JSON.stringify(resp) + " (" + index + "/" + totalTimes + ")");
   172              if (resp.txhash) {
   173                  if (nonce % 10 === 0){
   174                      sleep(20);
   175                  }
   176                  if (bignonce) {
   177                      nonce += 20;
   178                  }
   179                  sendTransaction(++index, totalTimes, from, to, value, ++nonce, randomToAddr, fromEqTo, bignonce);
   180              }
   181          }).catch(err => {
   182              console.log(randomToAddr, fromEqTo, bignonce, err);
   183          });
   184      }
   185  }
   186  
   187  function checkClaimTokens() {
   188      var interval = setInterval(function () {
   189          neb.api.getAccountState(from.getAddressString()).then(function (resp) {
   190              console.log("checking claim tokens from master accountState resp:" + JSON.stringify(resp));
   191              //if (resp.result.nonce >= lastnonce + AddressNumber) {
   192              if (resp.nonce >= lastnonce + AddressNumber) {
   193                  clearInterval(interval);
   194                  sendTransactionsForTps();
   195              }
   196          });
   197      }, 2000);
   198  }
   199  
   200  function sendTransactionsForTps() {
   201  
   202      console.log("start tps transaction sending...");
   203  
   204      startTime = new Date().getTime();
   205  
   206      for (var i = 0; i < AddressNumber; i++) {
   207          var node = nodes[i % nodes.length];
   208          neb.setRequest(new HttpRequest(node));
   209          var randomValue = Math.floor((Math.random() * 100));
   210  
   211          let acc = accountArray[i];
   212          neb.api.getAccountState(acc.getAddressString()).then(function (resp) {
   213              console.log("tps from accountState resp:" + JSON.stringify(resp));
   214              //lastnonce = parseInt(resp.result.nonce);
   215              let nc = parseInt(resp.nonce);
   216              console.log("tps from accountState:", nc);
   217      
   218              sendTransaction(0, EachAccountSendTimes, acc, null, randomValue, nc + 1, true /*random to addr*/, true, true);
   219          });
   220          
   221          sleep(10);
   222          console.log("current AddrssNumber: " + i);
   223      }
   224  }