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