github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/testnet3/schedule.stress.test.js (about) 1 'use strict'; 2 var schedule = require('node-schedule'); 3 var Wallet = require('../../../cmd/console/neb.js/lib/wallet.js'); 4 var HttpRequest = require("../../node-request"); 5 var FS = require("fs"); 6 var logStream = FS.createWriteStream('stress.test.log', {flags: 'a'}); 7 process.stdout.write = process.stderr.write = logStream.write.bind(logStream) 8 9 var env; // local testneb1 testneb2 10 var AddressNumber; 11 var SendTimes; 12 13 var args = process.argv.splice(2); 14 15 if (args.length !=3 ){ 16 // give default config 17 env = "testneb3"; 18 AddressNumber = 200; 19 SendTimes = 30; 20 } else { 21 env = args[0]; // local testneb1 testneb2 22 23 AddressNumber = parseInt(args[1]); 24 SendTimes = parseInt(args[2]); 25 } 26 27 if (AddressNumber <=0 || SendTimes <=0 ){ 28 29 console.log("please input correct AddressNumber and SendTimes"); 30 return; 31 } 32 33 var Neb = Wallet.Neb; 34 var neb = new Neb(); 35 36 var ChainID; 37 var from; 38 var accountArray; 39 40 //local 41 if (env == 'local'){ 42 neb.setRequest(new HttpRequest("http://127.0.0.1:8685"));//https://testnet.nebulas.io 43 ChainID = 100; 44 from = new Wallet.Account("a6e5eb290e1438fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f"); 45 }else if(env == 'testneb1'){ 46 neb.setRequest(new HttpRequest("http://13.57.245.249:8685")); 47 ChainID = 1001; 48 from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 49 }else if(env == "testneb2"){ 50 neb.setRequest(new HttpRequest("http://34.205.26.12:8685")); 51 ChainID = 1002; 52 from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 53 }else if(env == "testneb3"){ 54 neb.setRequest(new HttpRequest("http://35.177.214.138:8685")); 55 ChainID = 1003; 56 from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 57 }else{ 58 console.log("please input correct env local testneb1 testneb2"); 59 return; 60 } 61 62 var lastnonce = 0; 63 64 var j = schedule.scheduleJob('10,40 * * * *', function(){ 65 console.log("start transaction stress test"); 66 neb.api.getAccountState(from.getAddressString()).then(function (resp) { 67 68 console.log("master accountState resp:" + JSON.stringify(resp)); 69 lastnonce = parseInt(resp.nonce); 70 console.log("lastnonce:", lastnonce); 71 72 sendTransactions(); 73 }); 74 }); 75 76 function sendTransactions() { 77 78 accountArray = new Array(); 79 for (var i = 0; i < AddressNumber; i++) { 80 var account = Wallet.Account.NewAccount(); 81 accountArray.push(account); 82 } 83 84 var type = Math.floor(Math.random()*2); 85 switch(type) { 86 case 0: 87 console.log("send normal transactions!!!"); 88 sendNormalTransactions(SendTimes, "1"); 89 break; 90 case 1: 91 console.log("send contract transactions!!!"); 92 sendContractTransactions(); 93 break; 94 } 95 } 96 97 function sendNormalTransactions(totalTimes, value) { 98 var nonce = lastnonce; 99 for (var i = 0; i < AddressNumber; i++) { 100 sendTransaction(0, totalTimes, value, nonce, accountArray[i]); 101 nonce = nonce + totalTimes; 102 } 103 } 104 105 function sendTransaction(sendtimes, totalTimes, value, nonce, address) { 106 if (sendtimes < totalTimes) { 107 var transaction = new Wallet.Transaction(ChainID, from, address, value, ++nonce); 108 transaction.signTransaction(); 109 var rawTx = transaction.toProtoString(); 110 neb.api.sendRawTransaction(rawTx).then(function (resp) { 111 console.log("send raw transaction resp:" + JSON.stringify(resp)); 112 sendtimes++; 113 if (resp.txhash) { 114 sendTransaction(sendtimes, totalTimes, value, nonce, address); 115 } 116 }); 117 } 118 119 } 120 121 function sendContractTransactions() { 122 //claim tokens 123 sendNormalTransactions(1, neb.nasToBasic(1)); 124 lastnonce += AddressNumber; 125 126 var intervalAccount = setInterval(function () { 127 neb.api.getAccountState(from.getAddressString()).then(function (resp) { 128 // console.log("master accountState resp:" + JSON.stringify(resp)); 129 var nonce = parseInt(resp.nonce); 130 console.log("lastnonce:", lastnonce, "resp_nonce:", nonce); 131 132 if (lastnonce <= nonce){ 133 clearInterval(intervalAccount); 134 deployContract(); 135 } 136 }); 137 }, 2000); 138 } 139 140 function deployContract(){ 141 142 var nonce = lastnonce; 143 console.log("deploy contract"); 144 145 neb.api.getAccountState(from.getAddressString()).then(function (state) { 146 lastnonce = parseInt(state.nonce); 147 console.log("lastnonce:", lastnonce); 148 149 // create contract 150 var bank = FS.readFileSync("/neb/golang/src/github.com/nebulasio/go-nebulas/nf/nvm/test/bank_vault_contract.js", "utf-8"); 151 var contract = { 152 "source": bank, 153 "sourceType": "js", 154 "args": "" 155 }; 156 157 var transaction = new Wallet.Transaction(ChainID, from, from, "0", ++lastnonce, "1000000", "2000000", contract); 158 transaction.signTransaction(); 159 var rawTx = transaction.toProtoString(); 160 neb.api.sendRawTransaction(rawTx).then(function (resp) { 161 console.log("send raw contract transaction resp:" + JSON.stringify(resp)); 162 // ContractHash = resp.txhash; 163 // ContractAddress = resp.contract_address; 164 165 checkContractDeployed(resp.txhash); 166 }); 167 }); 168 } 169 170 function checkContractDeployed(ContractHash){ 171 172 var retry = 0; 173 174 // contract status and get contract_address 175 var interval = setInterval(function () { 176 console.log("getTransactionReceipt hash:"+ContractHash); 177 neb.api.getTransactionReceipt(ContractHash).then(function (resp) { 178 179 console.log("tx receipt:" + resp.status); 180 181 if(resp.status && resp.status === 1) { 182 clearInterval(interval); 183 sendMutilContractTransaction(resp.contract_address); 184 } 185 }).catch(function (err) { 186 retry++; 187 console.log("error!", JSON.stringify(err.error)); 188 if (retry > 10) { 189 console.log("deploy contract failed"); 190 console.log(JSON.stringify(err.error)); 191 clearInterval(interval); 192 } 193 }); 194 195 }, 2000); 196 } 197 198 function sendMutilContractTransaction(contract){ 199 for (var i = 0; i < AddressNumber; i++) { 200 sendContractTransaction(0, 0, accountArray[i], contract); 201 } 202 } 203 204 function sendContractTransaction(sendtimes, nonce, from_address, contract_address) { 205 if(sendtimes < SendTimes) { 206 var call = { 207 "function": "save", 208 "args":"[10000]" 209 }; 210 211 console.log("send contract nonce:",nonce); 212 var transaction = new Wallet.Transaction(ChainID, from_address, contract_address, "0", ++nonce, "1000000", "2000000", call); 213 transaction.signTransaction(); 214 var rawTx = transaction.toProtoString(); 215 neb.api.sendRawTransaction(rawTx).then(function (resp) { 216 console.log("send raw contract transaction resp:" + JSON.stringify(resp)); 217 sendtimes++; 218 if(resp.txhash) { 219 sendContractTransaction(sendtimes, nonce, from_address, contract_address); 220 } 221 }); 222 } 223 224 }