github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/stress/stress.test.dag.js (about) 1 'use strict'; 2 3 var Wallet = require("nebulas"); 4 //var HttpRequest = Wallet.HttpRequest 5 var HttpRequest = require("../../node-request.js"); 6 var utils = Wallet.Utils; 7 var schedule = require('node-schedule'); 8 var sleep = require("system-sleep"); 9 10 var env; // local testneb1 testneb2 11 var AddressNumber = 300; 12 var EachAccountSendTimes = 50; 13 14 var args = process.argv.splice(2); 15 env = args[0]; 16 17 var Neb = Wallet.Neb; 18 var neb = new Neb(); 19 20 var ChainID; 21 var from; 22 var accountArray; 23 // var to = Wallet.Account.NewAccount(); 24 var lastnonce = 0; 25 26 // statics for tps check start time. 27 var startTime; 28 29 var nodes = new Array(); 30 31 console.log(args); 32 33 //local 34 if (env === 'local') { 35 neb.setRequest(new HttpRequest("http://127.0.0.1:8685")); //https://testnet.nebulas.io 36 ChainID = 100; 37 from = new Wallet.Account("a6e5eb290e1438fce79f5cb8774a72621637c2c9654c8b2525ed1d7e4e73653f"); 38 nodes.push("http://127.0.0.1:8685"); 39 } else if (env === 'testneb1') { 40 neb.setRequest(new HttpRequest("http://35.182.48.19:8685")); 41 ChainID = 1001; 42 from = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 43 nodes.push("http://35.182.48.19:8685"); 44 nodes.push("http://13.57.245.249:8685"); 45 nodes.push("http://54.219.151.126:8685"); 46 nodes.push("http://18.218.165.90:8685"); 47 nodes.push("http://18.219.28.97:8685"); 48 nodes.push("http://13.58.44.3:8685"); 49 nodes.push("http://35.177.214.138:8685"); 50 nodes.push("http://35.176.94.224:8685"); 51 } else if (env === "testneb2") { 52 neb.setRequest(new HttpRequest("http://34.205.26.12:8685")); 53 ChainID = 1002; 54 from = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 55 nodes.push("http://34.205.26.12:8685"); 56 // nodes.push("http://54.206.9.246:8685"); 57 // nodes.push("http://54.252.158.117:8685"); 58 // nodes.push("http://34.206.53.244:8685"); 59 // nodes.push("http://34.205.53.3:8685"); 60 // nodes.push("http://52.3.226.40:8685"); 61 62 } else if (env === "testneb3") { 63 64 //neb.setRequest(new HttpRequest("http://52.47.199.42:8685")); 65 neb.setRequest(new HttpRequest("http://35.177.214.138:8685")); 66 //neb.setRequest(new HttpRequest("http://13.127.227.177:8685")); 67 ChainID = 1003; 68 from = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 69 //nodes.push("http://13.127.227.177:8685"); 70 nodes.push("http://35.177.214.138:8685"); 71 //nodes.push("http://52.47.199.42:8685"); 72 73 } else if (env === "testneb4") { 74 neb.setRequest(new HttpRequest("http://34.208.233.164:8685")); 75 ChainID = 1004; 76 from = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 77 nodes.push("http://34.208.233.164:8685"); 78 nodes.push("http://54.245.29.152:8685"); 79 nodes.push("http://52.34.73.0:8685"); 80 nodes.push("http://54.71.175.99:8685"); 81 nodes.push("http://34.213.130.120:8685"); 82 nodes.push("http://18.197.107.228:8685"); 83 nodes.push("http://18.197.106.150:8685"); 84 nodes.push("http://54.93.121.146:8685"); 85 nodes.push("http://18.195.159.210:8685"); 86 nodes.push("http://18.197.157.46:8685"); 87 nodes.push("http://18.228.3.118:8685"); 88 nodes.push("http://18.231.173.99:8685"); 89 nodes.push("http://18.231.124.140:8685"); 90 nodes.push("http://18.231.183.193:8685"); 91 nodes.push("http://18.231.162.23:8685"); 92 nodes.push("http://34.253.237.122:8685"); 93 nodes.push("http://34.244.129.30:8685"); 94 nodes.push("http://54.229.241.235:8685"); 95 nodes.push("http://54.229.177.109:8685"); 96 nodes.push("http://34.250.18.201:8685"); 97 nodes.push("http://13.127.227.177:8685"); 98 99 } else if (env === "liuliang") { 100 neb.setRequest(new HttpRequest("http://35.154.108.11:8685")); 101 ChainID = 1001; 102 from = new Wallet.Account("c75402f6ffe6edcc2c062134b5932151cb39b6486a7beb984792bb9da3f38b9f"); 103 nodes.push("http://35.154.108.11:8685"); 104 105 } else if (env === "maintest"){ 106 ChainID = 2; 107 from = new Wallet.Account("d2319a8a63b1abcb0cc6d4183198e5d7b264d271f97edf0c76cfdb1f2631848c"); 108 neb.setRequest(new HttpRequest("http://54.149.15.132:8685")); 109 nodes.push("http://54.149.15.132:8685"); 110 nodes.push("http://18.188.27.35:8685"); 111 nodes.push("http://34.201.23.199:8685"); 112 nodes.push("http://13.251.33.39:8685"); 113 nodes.push("http://52.56.55.238:8685"); 114 115 } else if (env === "testnet_cal_super") { 116 neb.setRequest(new HttpRequest("http://13.57.96.40:8685")); 117 ChainID = 1001; 118 from = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52"); 119 // from = new Wallet.Account("d2319a8a63b1abcb0cc6d4183198e5d7b264d271f97edf0c76cfdb1f2631848c"); 120 121 nodes.push("http://52.60.150.236:8685"); 122 nodes.push("http://13.57.96.40:8685"); 123 124 } else { 125 console.log("please input correct env local testneb1 testneb2"); 126 return; 127 } 128 129 130 var count = AddressNumber * EachAccountSendTimes; 131 132 var interval = setInterval(function () { 133 134 if (count >= AddressNumber * EachAccountSendTimes) { 135 count = 0; 136 137 console.log("sending request to ", env); 138 neb.api.getAccountState(from.getAddressString()).then(function (resp) { 139 console.log("master accountState resp:" + JSON.stringify(resp)); 140 //lastnonce = parseInt(resp.result.nonce); 141 lastnonce = parseInt(resp.nonce); 142 console.log("lastnonce:", lastnonce); 143 144 claimTokens(lastnonce); 145 }); 146 } 147 148 }, 20000); 149 150 151 152 153 154 155 function claimTokens(nonce) { 156 console.log("initializing " + AddressNumber + " accounts with coins !!!") 157 console.log(from.getAddressString()); 158 159 accountArray = new Array(); 160 for (var i = 0; i < AddressNumber; i++) { 161 var account = Wallet.Account.NewAccount(); 162 accountArray.push(account); 163 sendTransaction(0, 1, from, account, "1000000000000000", ++nonce); 164 sleep(5); 165 } 166 checkClaimTokens(); 167 } 168 169 function sendTransaction(index, totalTimes, from, to, value, nonce, randomToAddr) { 170 if (index < totalTimes) { 171 172 if (randomToAddr !== null && randomToAddr === true){ 173 var randomTo = Math.floor((Math.random() * AddressNumber)); 174 to = accountArray[randomTo]; 175 } 176 177 var transaction = new Wallet.Transaction(ChainID, from, to, value, nonce); 178 transaction.signTransaction(); 179 var rawTx = transaction.toProtoString(); 180 181 var i = Math.floor((Math.random() * nodes.length)); 182 var node = nodes[i]; 183 neb.setRequest(new HttpRequest(node)); 184 count ++; 185 neb.api.sendRawTransaction(rawTx).then(function (resp) { 186 console.log("send raw tx resp:" + JSON.stringify(resp) + " (" + index + "/" + totalTimes + ")"); 187 //if (resp.result.txhash) { 188 if (resp.txhash) { 189 if (nonce % 10 === 0){ 190 sleep(20); 191 } 192 sendTransaction(++index, totalTimes, from, to, value, ++nonce, randomToAddr); 193 } 194 }); 195 196 // .catch(function (err) { 197 // console.log(err); 198 // console.log(JSON.stringify(err)); 199 // console.log("send tx error, retry: " + "from:" + from.getAddressString() + " tx_index: (" + index + "/" + totalTimes + ")" + " node:" + node); 200 // sleep(10); 201 // sendTransaction(index, totalTimes, from, to, value, nonce, randomToAddr); 202 // } 203 // ); 204 } 205 } 206 207 function checkClaimTokens() { 208 var interval = setInterval(function () { 209 neb.api.getAccountState(from.getAddressString()).then(function (resp) { 210 console.log("master accountState resp:" + JSON.stringify(resp)); 211 //if (resp.result.nonce >= lastnonce + AddressNumber) { 212 if (resp.nonce >= lastnonce + AddressNumber) { 213 clearInterval(interval); 214 sendTransactionsForTps(); 215 } 216 }); 217 }, 2000); 218 } 219 220 function sendTransactionsForTps() { 221 222 console.log("start tps transaction sending..."); 223 224 startTime = new Date().getTime(); 225 226 for (var i = 0; i < AddressNumber; i++) { 227 var node = nodes[i % nodes.length]; 228 neb.setRequest(new HttpRequest(node)); 229 var randomValue = Math.floor((Math.random() * 100)); 230 sendTransaction(0, EachAccountSendTimes, accountArray[i], null, randomValue, 1, true /*random to addr*/); 231 sleep(5); 232 } 233 }