github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/stress/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 sleep = require("system-sleep");
     6  
     7  var env; // local testneb1 testneb2
     8  var AddressNumber = 100;
     9  var EachAccountSendTimes = 100;
    10  
    11  var args = process.argv.splice(2);
    12  
    13  if (args.length != 3) {
    14      // give default config
    15      env = "local";
    16      AddressNumber = 100;
    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 {
    68      console.log("please input correct env local testneb1 testneb2")
    69      return;
    70  }
    71  
    72  neb.api.getAccountState(from.getAddressString()).then(function (resp) {
    73      console.log("master accountState resp:" + JSON.stringify(resp));
    74      lastnonce = parseInt(resp.nonce);
    75      console.log("lastnonce:", lastnonce);
    76  
    77      claimTokens(lastnonce);
    78  });
    79  
    80  function claimTokens(nonce) {
    81      console.log("initializing " + AddressNumber + " accounts with coins !!!")
    82      accountArray = new Array();
    83      for (var i = 0; i < AddressNumber; i++) {
    84          var account = Wallet.Account.NewAccount();
    85          accountArray.push(account);
    86  
    87          sendTransaction(0, 1, from, account, "1000000000000000", ++nonce);
    88  
    89          sleep(10);
    90      }
    91  
    92      checkClaimTokens();
    93  }
    94  
    95  function sendTransaction(index, totalTimes, from, to, value, nonce, randomToAddr) {
    96      if (index < totalTimes) {
    97  
    98          if (randomToAddr !== null && randomToAddr === true){
    99              var randomTo = Math.floor((Math.random() * AddressNumber));
   100              to = accountArray[randomTo];
   101          }
   102  
   103          var transaction = new Wallet.Transaction(ChainID, from, to, value, nonce);
   104          transaction.signTransaction();
   105          var rawTx = transaction.toProtoString();
   106  
   107          neb.api.sendRawTransaction(rawTx).then(function (resp) {
   108              console.log("send raw transaction resp:" + JSON.stringify(resp));
   109              if (resp.txhash) {
   110                  if (nonce % 10 === 0){
   111                      sleep(10);
   112                  }
   113                  sendTransaction(++index, totalTimes, from, to, value, ++nonce, randomToAddr);
   114              }
   115          });
   116      }
   117  }
   118  
   119  function checkClaimTokens() {
   120      var interval = setInterval(function () {
   121          neb.api.getAccountState(from.getAddressString()).then(function (resp) {
   122              console.log("master accountState resp:" + JSON.stringify(resp));
   123              if (resp.nonce >= lastnonce + AddressNumber) {
   124                  clearInterval(interval);
   125  
   126                  sendTransactionsForTps();
   127              }
   128          });
   129      }, 2000);
   130  }
   131  
   132  function sendTransactionsForTps() {
   133  
   134      console.log("start tps transaction sending...");
   135  
   136      startTime = new Date().getTime();
   137  
   138      for (var i = 0; i < AddressNumber; i++) {
   139  
   140          var node = nodes[i % nodes.length];
   141          neb.setRequest(new HttpRequest(node));
   142  
   143          sendTransaction(0, EachAccountSendTimes, accountArray[i], null, "0.001", 1, true /*random to addr*/);
   144          sleep(10);
   145      }
   146  }