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

     1  'use strict';
     2  
     3  var Wallet = require('nebulas');
     4  var sleep = require("system-sleep");
     5  var HttpRequest = require("../../node-request");
     6  
     7  var args = process.argv.splice(2);
     8  
     9  if (args.length != 3) {
    10  	console.log("please input args 0:env(local,testneb1,testneb2,testneb3) 1:address number(concurrency) 2:sendtimes");
    11  	return;
    12  }
    13  
    14  var env = args[0]; // local testneb1 testneb2
    15  
    16  const AddressNumber = parseInt(args[1]);
    17  const SendTimes = parseInt(args[2]);
    18  
    19  if (AddressNumber <= 0 || SendTimes <= 0) {
    20  
    21  	console.log("please input correct AddressNumber and SendTimes");
    22  	return;
    23  }
    24  
    25  var Neb = Wallet.Neb;
    26  var neb = new Neb();
    27  
    28  var ChainID;
    29  var from;
    30  
    31  //local
    32  if (env == 'local') {
    33  	neb.setRequest(new HttpRequest("http://127.0.0.1:8685")); //https://testnet.nebulas.io
    34  	ChainID = 100;
    35  	from = new Wallet.Account("a6e5eb290e1438fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f");
    36  } else if (env == 'testneb1') {
    37  	neb.setRequest(new HttpRequest("http://35.182.48.19:8685"));
    38  	ChainID = 1001;
    39  	from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    40  } else if (env == "testneb2") {
    41  	neb.setRequest(new HttpRequest("http://34.205.26.12:8685"));
    42  	ChainID = 1002;
    43  	from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    44  } else if (env == "testneb3") {
    45  	neb.setRequest(new HttpRequest("http://35.177.214.138:8685"));
    46  	ChainID = 1003;
    47  	from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3");
    48  } else if (env == "mariana") {
    49      neb.setRequest(new HttpRequest("http://127.0.0.1:9685")); //https://testnet.nebulas.io
    50      ChainID = 1111;
    51      from = new Wallet.Account("830ccbac2029b880eb07aa9a19c65ce6dad41702d409771eada791d6a6a83a1e");
    52  } else {
    53  	console.log("please input correct env local testneb1 testneb2 testneb3");
    54  	return;
    55  }
    56  
    57  var FS = require("fs");
    58  
    59  var lastnonce = 0;
    60  
    61  // new account  to get address
    62  var accountArray = new Array();
    63  for (var i = 0; i < AddressNumber; i++) {
    64  	var account = Wallet.Account.NewAccount();
    65  	//var hash = account.getAddressString();
    66  	accountArray.push(account);
    67  }
    68  
    69  neb.api.getAccountState(from.getAddressString()).then(function (resp) {
    70  	console.log("master accountState resp:" + JSON.stringify(resp));
    71  	lastnonce = parseInt(resp.nonce);
    72  	console.log("lastnonce:", lastnonce);
    73  });
    74  
    75  sleep(2000);
    76  
    77  cliamTokens();
    78  
    79  var ContractHash;
    80  var ContractAddress;
    81  
    82  var intervalAccount = setInterval(function () {
    83  	neb.api.getAccountState(from.getAddressString()).then(function (resp) {
    84  		console.log("master accountState resp:" + JSON.stringify(resp));
    85  		var nonce = parseInt(resp.nonce);
    86  		console.log("lastnonce:", lastnonce, "resp_nonce:", nonce);
    87  
    88  		if (lastnonce <= nonce) {
    89  			clearInterval(intervalAccount);
    90  			deployContract();
    91  		}
    92  	});
    93  }, 2000);
    94  
    95  function cliamTokens() {
    96  	var nonce = lastnonce + 1;
    97  	for (var j = 0; j < AddressNumber; j++) {
    98  		sendTransaction(nonce, accountArray[j]);
    99  		++nonce;
   100  		sleep(30);
   101  	}
   102  
   103  	lastnonce = nonce - 1;
   104  }
   105  
   106  function deployContract() {
   107  
   108  	var nonce = lastnonce;
   109  	console.log("nonce:" + nonce);
   110  	// create contract
   111  	var bank = FS.readFileSync("../nf/nvm/test/bank_vault_contract.js", "utf-8");
   112  	var contract = {
   113  		"source": bank,
   114  		"sourceType": "js",
   115  		"args": ""
   116  	};
   117  
   118  	var transaction = new Wallet.Transaction(ChainID, from, from, "0", ++nonce, "1000000", "20000000000", contract);
   119  	transaction.signTransaction();
   120  	var rawTx = transaction.toProtoString();
   121  
   122  	// console.log("contract:" + rawTx);
   123  
   124  	neb.api.sendRawTransaction(rawTx).then(function (resp) {
   125  		console.log("send raw contract transaction resp:" + JSON.stringify(resp));
   126  		ContractHash = resp.txhash;
   127  		ContractAddress = resp.contract_address;
   128  
   129  		checkContractDeployed();
   130  	});
   131  
   132  	++lastnonce;
   133  }
   134  
   135  function checkContractDeployed() {
   136  
   137  	var retry = 0;
   138  
   139  	// contract status and get contract_address 
   140  	var interval = setInterval(function () {
   141  		console.log("getTransactionReceipt hash:" + ContractHash);
   142  		neb.api.getTransactionReceipt(ContractHash).then(function (resp) {
   143  
   144  			console.log("tx receipt:" + resp.status);
   145  
   146  			if (resp.status && resp.status === 1) {
   147  				clearInterval(interval);
   148  				sendMutilContractTransaction(ContractAddress)
   149  			}
   150  		}).catch(function (err) {
   151  			retry++;
   152  			console.log("error!", JSON.stringify(err.error));
   153  			if (retry > 10) {
   154  				console.log(JSON.stringify(err.error));
   155  				clearInterval(interval);
   156  			}
   157  		});
   158  
   159  	}, 2000);
   160  }
   161  
   162  
   163  function sendTransaction(nonce, address) {
   164  	var transaction = new Wallet.Transaction(ChainID, from, address, neb.nasToBasic(1), nonce);
   165  	transaction.signTransaction();
   166  	var rawTx = transaction.toProtoString();
   167  	neb.api.sendRawTransaction(rawTx).then(function (resp) {
   168  		console.log("send raw transaction resp:" + JSON.stringify(resp));
   169  	});
   170  }
   171  
   172  
   173  
   174  // get current height
   175  var BeginHeight;
   176  //
   177  function sendMutilContractTransaction(address) {
   178  
   179  	neb.api.getNebState().then(function (resp) {
   180  		BeginHeight = resp.height;
   181  		console.log("get NebState resp:" + JSON.stringify(resp));
   182  	});
   183  
   184  	sleep(1000);
   185  	var nonce = lastnonce;
   186  	var t1 = new Date().getTime();
   187  	for (var j = 0; j < AddressNumber; j++) {
   188  		nonce = 0;
   189  		sendContractTransaction(0, nonce, accountArray[j], address);
   190  		//nonce = nonce + SendTimes;
   191  	}
   192  
   193  	lastnonce = SendTimes;
   194  	sleep(1000 * SendTimes)
   195  	getTransactionNumberByHeight();
   196  }
   197  
   198  
   199  
   200  function sendContractTransaction(sendtimes, nonce, from_address, contract_address) {
   201  	if (sendtimes < SendTimes) {
   202  		var call = {
   203  			"function": "save",
   204  			"args": "[10000]"
   205  		}
   206  
   207  		console.log("send contract nonce:", nonce);
   208  		var transaction = new Wallet.Transaction(ChainID, from_address, contract_address, "0", ++nonce, "1000000", "2000000000", call);
   209  		transaction.signTransaction();
   210  		var rawTx = transaction.toProtoString();
   211  		neb.api.sendRawTransaction(rawTx).then(function (resp) {
   212  			console.log("send raw contract transaction resp:" + JSON.stringify(resp));
   213  			sendtimes++;
   214  			if (resp.txhash) {
   215  				sendContractTransaction(sendtimes, nonce, from_address, contract_address);
   216  			}
   217  		});
   218  	}
   219  }
   220  
   221  function getTransactionNumberByHeight() {
   222  
   223  	var intervalHeight = setInterval(function () {
   224  		neb.api.getAccountState(accountArray[0].getAddressString()).then(function (resp) {
   225  			console.log("master accountState resp:" + JSON.stringify(resp));
   226  			var nonce = parseInt(resp.nonce);
   227  			console.log("lastnonce:", lastnonce, "resp_nonce:", nonce);
   228  
   229  			if (lastnonce <= nonce) {
   230  				clearInterval(intervalHeight)
   231  				sleep(2000)
   232  				neb.api.getNebState().then(function (resp) {
   233  					var EndHeight = resp.height
   234  					console.log("BeginHeight:" + BeginHeight + " EndHeight:" + EndHeight);
   235  					var sum = 0;
   236  					var max = 0;
   237  					var height = BeginHeight
   238  					var h = EndHeight - BeginHeight
   239  					for (; height <= EndHeight; height++) {
   240  						neb.api.getBlockByHeight(height, false).then(function (resp) {
   241  							if (resp.transactions) {
   242  								//console.log("master accountState resp:" + JSON.stringify(resp));
   243  								console.log(resp.height, resp.transactions.length)
   244  								sum += resp.transactions.length
   245  								max = resp.transactions.length > max ? resp.transactions.length : max
   246  							} else {
   247  								console.log(resp.height, 0)
   248  							}
   249  							--h;
   250  						});
   251  						sleep(10)
   252  					}
   253  
   254  					sleep(1000)
   255  					var intervalH = setInterval(function () {
   256  						if (h < 0) {
   257  							clearInterval(intervalH);
   258  							console.log("====================")
   259  							console.log("env is ", env)
   260  							console.log("concurrency number is ", AddressNumber)
   261  							console.log("total number is ", AddressNumber * SendTimes)
   262  							console.log("height from ", BeginHeight, " to ", EndHeight)
   263  							console.log("max of block is ", max)
   264  							console.log("avg of block is ", sum / (EndHeight - BeginHeight))
   265  							console.log("max of tps is ", max / 5)
   266  							console.log("avg of tps is ", sum / (5 * (EndHeight - BeginHeight)))
   267  							console.log("====================")
   268  						}
   269  					}, 2000);
   270  
   271  				});
   272  			}
   273  		})
   274  	}, 1000);
   275  }