github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/testnet3/schedule.stress.dag.js (about)

     1  'use strict';
     2  
     3  var Wallet = require('../../../cmd/console/neb.js/lib/wallet.js');
     4  var HttpRequest = require("../../node-request");
     5  var schedule = require('node-schedule');
     6  var sleep = require("system-sleep");
     7  
     8  var env; // local testneb1 testneb2
     9  var AddressNumber = 1200;
    10  var EachAccountSendTimes = 1000;
    11  
    12  var args = process.argv.splice(2);
    13  
    14  if (args.length != 3) {
    15      // give default config
    16      env = "testneb3";
    17  } else {
    18      env = args[0]; // local testneb1 testneb2
    19  
    20      AddressNumber = parseInt(args[1]);
    21      EachAccountSendTimes = parseInt(args[2]);
    22  }
    23  
    24  if (AddressNumber <= 0 || EachAccountSendTimes <= 0) {
    25  
    26      console.log("please input correct AddressNumber and SendTimes");
    27      return;
    28  }
    29  
    30  var Neb = Wallet.Neb;
    31  var neb = new Neb();
    32  
    33  var ChainID;
    34  var from;
    35  var accountArray;
    36  // var to = Wallet.Account.NewAccount();
    37  var lastnonce = 0;
    38  
    39  // statics for tps check start time.
    40  var startTime;
    41  
    42  var nodes = new Array();
    43  
    44  //local
    45  if (env == 'local') {
    46      neb.setRequest(new HttpRequest("http://127.0.0.1:8685")); //https://testnet.nebulas.io
    47      ChainID = 100;
    48      from = new Wallet.Account("a6e5eb290e1438fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f");
    49      nodes.push("http://127.0.0.1:8685");
    50  } else if (env == 'testneb1') {
    51      neb.setRequest(new HttpRequest("http://35.182.48.19:8685"));
    52      ChainID = 1001;
    53      from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    54      nodes.push("http://35.182.48.19:8685");
    55      nodes.push("http://13.57.245.249:8685");
    56      nodes.push("http://54.219.151.126:8685");
    57      nodes.push("http://18.218.165.90:8685");
    58      nodes.push("http://18.219.28.97:8685");
    59      nodes.push("http://13.58.44.3:8685");
    60      nodes.push("http://35.177.214.138:8685");
    61      nodes.push("http://35.176.94.224:8685");
    62  } else if (env == "testneb2") {
    63      neb.setRequest(new HttpRequest("http://34.205.26.12:8685"));
    64      ChainID = 1002;
    65      from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    66      nodes.push("http://34.205.26.12:8685");
    67  } else if (env == "testneb3") {
    68      neb.setRequest(new HttpRequest("http://35.177.214.138:8685"));
    69      ChainID = 1003;
    70      from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    71      nodes.push("http://35.177.214.138:8685");
    72      nodes.push("http://13.57.19.76:8685");
    73      nodes.push("http://18.218.165.90:8685");
    74      nodes.push("http://35.176.94.224:8685");
    75      nodes.push("http://35.182.205.40:8685");
    76      nodes.push("http://52.47.199.42:8685");
    77  
    78  } else {
    79      console.log("please input correct env local testneb1 testneb2");
    80      return;
    81  }
    82  
    83  var j = schedule.scheduleJob('*/30 * * * *', function () {
    84      neb.api.getAccountState(from.getAddressString()).then(function (resp) {
    85          console.log("master accountState resp:" + JSON.stringify(resp));
    86          lastnonce = parseInt(resp.nonce);
    87          console.log("lastnonce:", lastnonce);
    88  
    89          claimTokens(lastnonce);
    90      });
    91  });
    92  
    93  function claimTokens(nonce) {
    94      console.log("initializing " + AddressNumber + " accounts with coins !!!")
    95      accountArray = new Array();
    96      for (var i = 0; i < AddressNumber; i++) {
    97          var account = Wallet.Account.NewAccount();
    98          accountArray.push(account);
    99          sendTransaction(0, 1, from, account, "1000000000000000", ++nonce);
   100          sleep(10);
   101      }
   102      checkClaimTokens();
   103  }
   104  
   105  function sendTransaction(index, totalTimes, from, to, value, nonce, randomToAddr) {
   106      if (index < totalTimes) {
   107  
   108          if (randomToAddr !== null && randomToAddr === true) {
   109              var randomTo = Math.floor((Math.random() * AddressNumber));
   110              to = accountArray[randomTo];
   111          }
   112  
   113          var transaction = new Wallet.Transaction(ChainID, from, to, value, nonce);
   114          transaction.signTransaction();
   115          var rawTx = transaction.toProtoString();
   116  
   117          var i = Math.floor((Math.random() * nodes.length));
   118          var node = nodes[i];
   119          neb.setRequest(new HttpRequest(node));
   120          neb.api.sendRawTransaction(rawTx).then(function (resp) {
   121              console.log("send raw transaction resp:" + JSON.stringify(resp));
   122              if (resp.txhash) {
   123                  if (nonce % 10 === 0) {
   124                      sleep(2);
   125                  }
   126                  sendTransaction(++index, totalTimes, from, to, value, ++nonce, randomToAddr);
   127              }
   128          }).catch(function (err) {
   129              console.log("send tx error, retry: " + "from:" + from.getAddressString() + " tx_index: (" + index + "/" + totalTimes + ")" + " node:" + node);
   130              sleep(20);
   131              sendTransaction(index, totalTimes, from, to, value, nonce, randomToAddr);
   132          });
   133      }
   134  }
   135  
   136  function checkClaimTokens() {
   137      var interval = setInterval(function () {
   138          neb.api.getAccountState(from.getAddressString()).then(function (resp) {
   139              console.log("master accountState resp:" + JSON.stringify(resp));
   140              if (resp.nonce >= lastnonce + AddressNumber) {
   141                  clearInterval(interval);
   142                  sendTransactionsForTps();
   143              }
   144          });
   145      }, 2000);
   146  }
   147  
   148  function sendTransactionsForTps() {
   149  
   150      console.log("start tps transaction sending...");
   151  
   152      startTime = new Date().getTime();
   153  
   154      for (var i = 0; i < AddressNumber; i++) {
   155  
   156          var node = nodes[i % nodes.length];
   157          neb.setRequest(new HttpRequest(node));
   158          var randomValue = Math.floor((Math.random() * 10));
   159          sendTransaction(0, EachAccountSendTimes, accountArray[i], null, randomValue, 1, true /*random to addr*/ );
   160          sleep(20);
   161      }
   162  }