github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/testnet/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 = 100; 10 var EachAccountSendTimes = 100; 11 12 var args = process.argv.splice(2); 13 14 if (args.length != 3) { 15 // give default config 16 env = "local"; 17 AddressNumber = 100; 18 } else { 19 env = args[0]; // local testneb1 testneb2 20 21 AddressNumber = parseInt(args[1]); 22 EachAccountSendTimes = parseInt(args[2]); 23 } 24 25 if (AddressNumber <= 0 || EachAccountSendTimes <= 0) { 26 27 console.log("please input correct AddressNumber and SendTimes"); 28 return; 29 } 30 31 var Neb = Wallet.Neb; 32 var neb = new Neb(); 33 34 var ChainID; 35 var from; 36 var accountArray; 37 // var to = Wallet.Account.NewAccount(); 38 var lastnonce = 0; 39 40 // statics for tps check start time. 41 var startTime; 42 43 var nodes = new Array(); 44 45 //local 46 if (env == 'local') { 47 neb.setRequest(new HttpRequest("http://127.0.0.1:8685")); //https://testnet.nebulas.io 48 ChainID = 100; 49 from = new Wallet.Account("a6e5eb290e1438fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f"); 50 nodes.push("http://127.0.0.1:8685"); 51 } else if (env == 'testneb1') { 52 neb.setRequest(new HttpRequest("http://35.182.48.19:8685")); 53 ChainID = 1001; 54 from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 55 nodes.push("http://35.182.48.19:8685"); 56 nodes.push("http://13.57.245.249:8685"); 57 nodes.push("http://54.219.151.126:8685"); 58 nodes.push("http://18.218.165.90:8685"); 59 nodes.push("http://18.219.28.97:8685"); 60 nodes.push("http://13.58.44.3:8685"); 61 nodes.push("http://35.177.214.138:8685"); 62 nodes.push("http://35.176.94.224:8685"); 63 64 } else if (env == "testneb2") { 65 neb.setRequest(new HttpRequest("http://34.205.26.12:8685")); 66 ChainID = 1002; 67 from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 68 nodes.push("http://34.205.26.12:8685"); 69 } else if (env == "testneb3") { 70 neb.setRequest(new HttpRequest("http://35.177.214.138:8685")); 71 ChainID = 1003; 72 from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 73 nodes.push("http://13.57.120.136:8685"); 74 nodes.push("http://18.218.165.90:8685"); 75 nodes.push("http://35.177.214.138:8685"); 76 nodes.push("http://35.176.94.224:8685"); 77 nodes.push("http://35.182.205.40:8685"); 78 nodes.push("http://52.47.199.42:8685"); 79 80 }else { 81 console.log("please input correct env local testneb1 testneb2") 82 return; 83 } 84 85 var j = schedule.scheduleJob('10,40 */1 * * *', function() { 86 neb.api.getAccountState(from.getAddressString()).then(function (resp) { 87 console.log("master accountState resp:" + JSON.stringify(resp)); 88 lastnonce = parseInt(resp.nonce); 89 console.log("lastnonce:", lastnonce); 90 91 //claimTokens(lastnonce); 92 claimSubMaster(lastnonce); 93 }); 94 }); 95 96 var maxCliamTime = 2 97 var cliamTimes = 0 98 var subMaster 99 //claim a new account to distribute money instead of master account 100 function claimSubMaster(nonce){ 101 console.log("initializing the subMaster account to distribute coins..."); 102 subMaster = Wallet.Account.NewAccount(); 103 var value = (maxCliamTime * AddressNumber + 1) * 1000000000000000; 104 105 var transaction = new Wallet.Transaction(ChainID, from, subMaster, value.toString(), ++nonce); 106 transaction.signTransaction(); 107 var rawTx = transaction.toProtoString(); 108 cliamTimes ++; 109 neb.api.sendRawTransaction(rawTx) 110 .then(function (rawTxResp){ 111 console.log("\tresp of send raw Tx for claiming subMaster:" + JSON.stringify(rawTxResp)); 112 checkTransaction(rawTxResp.txhash, function (resp) { 113 //console.log("resp" + JSON.stringify(resp)); 114 try { 115 if (resp && resp.status === 1) { 116 cliamTimes = 0; 117 console.log("send TX to sumMaster account success."); 118 claimTokens(0); //thr nonce of a new account is 0 119 } else if (cliamTimes < maxCliamTime) { 120 claimSubMaster(nonce); 121 } else { 122 cliamTimes = 0; 123 console.log("claim sumMaster failed!!!!"); 124 } 125 }catch (err) { 126 console.log(JSON.stringify(err)); 127 console.log(err); 128 } 129 }); 130 }).catch(function (err) { 131 console.log(err); 132 claimSubMaster(nonce); 133 }); 134 } 135 136 var maxCheckTime = 20; 137 var checkTimes = 0; 138 //check tx result to make sure the tx is completed 139 function checkTransaction(hash, callback) { 140 checkTimes += 1; 141 if (checkTimes > maxCheckTime) { 142 console.log("\tcheck tx receipt timeout:" + hash); 143 checkTimes = 0; 144 callback(); 145 return; 146 } 147 148 neb.api.getTransactionReceipt(hash).then(function (resp) { 149 console.log("\ttx receipt status:" + resp.status); 150 if (resp.status === 2) { 151 setTimeout(function () { 152 checkTransaction(hash, callback); 153 }, 2000); 154 } else { 155 checkTimes = 0; 156 callback(resp); 157 } 158 }).catch(function (err) { 159 console.log("\tfail to get tx receipt hash: " + hash); 160 console.log("\tit may because the tx is being packing, we are going on to check it!"); 161 console.log("\t" + err.error); 162 setTimeout(function () { 163 checkTransaction(hash, callback); 164 }, 2000); 165 }); 166 } 167 168 function claimTokens(nonce) { 169 console.log("initializing " + AddressNumber + " accounts with coins !!!") 170 accountArray = new Array(); 171 for (var i = 0; i < AddressNumber; i++) { 172 var account = Wallet.Account.NewAccount(); 173 accountArray.push(account); 174 175 sendTransaction(0, 1, subMaster, account, "1000000000000000", ++nonce); 176 177 sleep(10); 178 } 179 180 checkClaimTokens(); 181 } 182 183 function sendTransaction(index, totalTimes, from, to, value, nonce, randomToAddr) { 184 if (index < totalTimes) { 185 186 if (randomToAddr !== null && randomToAddr === true){ 187 var randomTo = Math.floor((Math.random() * AddressNumber)); 188 to = accountArray[randomTo]; 189 } 190 191 var transaction = new Wallet.Transaction(ChainID, from, to, value, nonce); 192 transaction.signTransaction(); 193 var rawTx = transaction.toProtoString(); 194 195 var i = Math.floor((Math.random() * nodes.length)); 196 var node = nodes[i]; 197 neb.setRequest(new HttpRequest(node)); 198 neb.api.sendRawTransaction(rawTx).then(function (resp) { 199 console.log("\tsend raw transaction resp:" + JSON.stringify(resp)); 200 if (resp.txhash) { 201 if (nonce % 10 === 0){ 202 sleep(10); 203 } 204 sendTransaction(++index, totalTimes, from, to, value, ++nonce, randomToAddr); 205 } 206 }); 207 } 208 } 209 210 function checkClaimTokens() { 211 var interval = setInterval(function () { 212 neb.api.getAccountState(subMaster.getAddressString()).then(function (resp) { 213 console.log("\tsubMaster accountState resp:" + JSON.stringify(resp)); 214 if (resp.nonce >= AddressNumber) { 215 clearInterval(interval); 216 217 sendTransactionsForTps(); 218 } 219 }); 220 }, 2000); 221 } 222 223 function sendTransactionsForTps() { 224 225 console.log("start tps transaction sending..."); 226 227 startTime = new Date().getTime(); 228 229 for (var i = 0; i < AddressNumber; i++) { 230 231 var node = nodes[i % nodes.length]; 232 neb.setRequest(new HttpRequest(node)); 233 234 sendTransaction(0, EachAccountSendTimes, accountArray[i], null, "0.001", 1, true /*random to addr*/); 235 sleep(10); 236 } 237 }