github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/testnet/schedule.test.js (about) 1 'use strict'; 2 3 var HttpRequest = require("../../node-request"); 4 var schedule = require('node-schedule'); 5 var fs = require("fs"); 6 var Wallet = require("../../../cmd/console/neb.js/lib/wallet"); 7 var Neb = Wallet.Neb; 8 var neb = new Neb(); 9 var Account = Wallet.Account; 10 var Transaction = Wallet.Transaction; 11 var Unit = Wallet.Unit; 12 13 var expect = require('chai').expect; 14 var BigNumber = require('bignumber.js'); 15 16 var ChainID; 17 var sourceAccount; 18 /* 19 * make sure every node of testnet has the same coinbase, and substitute the address below 20 */ 21 var coinbase = "eb31ad2d8a89a0ca6935c308d5425730430bc2d63f2573b8"; 22 var coinState; 23 24 var env = 'testneb1'; 25 if (env === 'testneb1') { 26 neb.setRequest(new HttpRequest("http://35.182.48.19:8685")); 27 ChainID = 1001; 28 sourceAccount = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 29 coinbase = "0b9cd051a6d7129ab44b17833c63fe4abead40c3714cde6d"; 30 } else if (env === "testneb2") { 31 neb.setRequest(new HttpRequest("http://34.205.26.12:8685")); 32 ChainID = 1002; 33 sourceAccount = new Wallet.Account("43181d58178263837a9a6b08f06379a348a5b362bfab3631ac78d2ac771c5df3"); 34 coinbase = "0b9cd051a6d7129ab44b17833c63fe4abead40c3714cde6d"; 35 } 36 37 var from; 38 var fromState; 39 var initFromBalance = 10; 40 41 var testCases = new Array(); 42 43 /* 44 * set this value according to the status of your testnet. 45 * the smaller the value, the faster the test, with the risk of causing error 46 */ 47 var maxCheckTime = 10; 48 var checkTimes = 0; 49 50 function checkTransaction(hash, callback) { 51 checkTimes += 1; 52 53 if (checkTimes > maxCheckTime) { 54 console.log("check tx receipt timeout:" + hash); 55 checkTimes = 0; 56 callback(); 57 return; 58 } 59 60 neb.api.getTransactionReceipt(hash).then(function (resp) { 61 62 console.log("tx receipt status:" + resp.status); 63 if (resp.status === 2) { 64 setTimeout(function () { 65 checkTransaction(hash, callback); 66 }, 2000); 67 } else { 68 checkTimes = 0; 69 callback(resp); 70 } 71 }).catch(function (err) { 72 console.log("fail to get tx receipt hash: " + hash); 73 console.log("it may becuase the tx is being packing, we are going on to check it!"); 74 console.log(err.error); 75 setTimeout(function () { 76 checkTransaction(hash, callback); 77 }, 2000); 78 }); 79 } 80 81 function test_transfer(testInput, testExpect, done) { 82 neb.api.getAccountState(from.getAddressString()).then(function (state) { 83 84 fromState = state; 85 console.log("from state:" + JSON.stringify(state)); 86 return neb.api.getAccountState(coinbase); 87 }).then(function (resp) { 88 89 var toAddr = Account.NewAccount(); 90 if (testInput.isSameAddr === true) { 91 toAddr = from; 92 } 93 coinState = resp; 94 console.log("get coinbase state before tx:" + JSON.stringify(resp)); 95 var tx = new Transaction(ChainID, from, toAddr, Unit.nasToBasic(testInput.transferValue), parseInt(fromState.nonce) + testInput.nonceIncrement, testInput.gasPrice, testInput.gasLimit); 96 tx.signTransaction(); 97 return neb.api.sendRawTransaction(tx.toProtoString()); 98 }).catch(function (err) { 99 if (true === testExpect.canSendTx) { 100 done(err); 101 } else { 102 done(); 103 } 104 }).then(function (resp) { 105 106 if (true === testExpect.canSendTx) { 107 console.log("send Rax Tx:" + JSON.stringify(resp)); 108 expect(resp).to.be.have.property('txhash'); 109 checkTransaction(resp.txhash, function (receipt) { 110 111 try { 112 if (true === testExpect.canSubmitTx) { 113 expect(receipt).to.not.be.a('undefined'); 114 if (true === testExpect.canExcuteTx) { 115 expect(receipt).to.be.have.property('status').equal(1); 116 } else { 117 expect(receipt).to.not.have.property('status'); 118 } 119 console.log("tx receipt : " + JSON.stringify(receipt)); 120 neb.api.getAccountState(receipt.from).then(function (state) { 121 122 console.log("get from account state :" + JSON.stringify(state)); 123 expect(state.balance).to.equal(testExpect.fromBalanceAfterTx); 124 return neb.api.getAccountState(receipt.to); 125 }).then(function (state) { 126 127 console.log("get to account state :" + JSON.stringify(state)); 128 expect(state.balance).to.equal(testExpect.toBalanceAfterTx); 129 return neb.api.getAccountState(coinbase); 130 }).then(function (state) { 131 132 console.log("get coinbase account state after tx:" + JSON.stringify(state)); 133 var reward = new BigNumber(state.balance).sub(coinState.balance); 134 reward = reward.mod(new BigNumber(1.4269).mul(new BigNumber(10).pow(18))); 135 // The transaction should be only 136 expect(reward.toString()).to.equal(testExpect.transferReward); 137 done(); 138 }).catch(function (err) { 139 140 console.log(JSON.stringify(err)); 141 done(err); 142 }); 143 } else { 144 expect(receipt).to.be.a('undefined'); 145 done(); 146 } 147 } catch (err) { 148 console.log(JSON.stringify(err)); 149 done(err); 150 } 151 }); 152 } else { 153 expect(resp).to.be.a('undefined'); 154 } 155 }).catch(function (err) { 156 157 console.log(JSON.stringify(err)); 158 done(err); 159 }); 160 } 161 162 function prepareTest(done) { 163 from = Account.NewAccount(); 164 neb.api.getAccountState(sourceAccount.getAddressString()).then(function (resp) { 165 166 console.log("source state:" + JSON.stringify(resp)); 167 var tx = new Transaction(ChainID, sourceAccount, from, Unit.nasToBasic(initFromBalance), parseInt(resp.nonce) + 1); 168 tx.signTransaction(); 169 // console.log("source tx:" + tx.toString()); 170 return neb.api.sendRawTransaction(tx.toProtoString()); 171 }).then(function (resp) { 172 173 checkTransaction(resp.txhash, function (resp) { 174 try { 175 expect(resp).to.be.have.property('status').equal(1); 176 console.log("complete from address claim."); 177 done(); 178 } catch (err) { 179 done(err); 180 } 181 }); 182 }).catch(function (err) { 183 console.log("claim token failed:" + JSON.stringify(err)); 184 done(err); 185 }); 186 } 187 188 var testCases = [ 189 { 190 "name": "normal transfer", 191 "testInput": { 192 transferValue: 1, 193 isSameAddr: false, 194 isAddressValid: true, 195 gasLimit: -1, 196 gasPrice: -1, 197 nonceIncrement: 1 198 }, 199 "testExpect": { 200 canSendTx: true, 201 canSubmitTx: true, 202 canExcuteTx: true, 203 fromBalanceAfterTx: '8999999980000000000', 204 toBalanceAfterTx: '1000000000000000000', 205 transferReward: '20000000000' 206 } 207 }, 208 { 209 "name": "from & to are same", 210 "testInput": { 211 transferValue: 1, 212 isSameAddr: true, 213 isAddressValid: true, 214 gasLimit: -1, 215 gasPrice: -1, 216 nonceIncrement: 1 217 }, 218 "testExpect": { 219 canSendTx: true, 220 canSubmitTx: true, 221 canExcuteTx: true, 222 fromBalanceAfterTx: '9999999980000000000', 223 toBalanceAfterTx: '9999999980000000000', 224 transferReward: '20000000000' 225 } 226 }, 227 { 228 "name": "from balance is insufficient", 229 "testInput": { 230 transferValue: 11, 231 isSameAddr: false, 232 isAddressValid: true, 233 gasLimit: -1, 234 gasPrice: -1, 235 nonceIncrement: 1 236 }, 237 "testExpect": { 238 canSendTx: true, 239 canSubmitTx: true, 240 canExcuteTx: false, 241 fromBalanceAfterTx: '9999999980000000000', 242 toBalanceAfterTx: '0', 243 transferReward: '20000000000' 244 } 245 }, 246 { 247 "name": "the sum of gas and balance is insufficient", 248 "testInput": { 249 transferValue: 9.999999999999, 250 isSameAddr: false, 251 isAddressValid: true, 252 gasLimit: -1, 253 gasPrice: -1, 254 nonceIncrement: 1 255 }, 256 "testExpect": { 257 canSendTx: true, 258 canSubmitTx: true, 259 canExcuteTx: false, 260 fromBalanceAfterTx: '9999999980000000000', 261 toBalanceAfterTx: '0', 262 transferReward: '20000000000' 263 } 264 }, 265 { 266 "name": "gas cost is more then fromBalance", 267 "testInput": { 268 transferValue: 1, 269 isSameAddr: false, 270 isAddressValid: true, 271 gasLimit: -1, 272 gasPrice: 1000000000000000000, 273 nonceIncrement: 1 274 }, 275 "testExpect": { 276 canSendTx: true, 277 canSubmitTx: false, 278 canExcuteTx: false, 279 fromBalanceAfterTx: '-1', 280 toBalanceAfterTx: '-1', 281 transferReward: '-1' 282 } 283 }, 284 { 285 "name": "gas price is too samll", 286 "testInput": { 287 transferValue: 1, 288 isSameAddr: false, 289 isAddressValid: true, 290 gasLimit: -1, 291 gasPrice: 1, 292 nonceIncrement: 1 293 }, 294 "testExpect": { 295 canSendTx: false, 296 canSubmitTx: false, 297 canExcuteTx: false, 298 fromBalanceAfterTx: '-1', 299 toBalanceAfterTx: '-1', 300 transferReward: '-1' 301 } 302 }, 303 { 304 "name": "gas Limit is too large", 305 "testInput": { 306 transferValue: 1, 307 isSameAddr: false, 308 isAddressValid: true, 309 gasLimit: 1000000000000000000, 310 gasPrice: -1, 311 nonceIncrement: 1 312 }, 313 "testExpect": { 314 canSendTx: false, 315 canSubmitTx: false, 316 canExcuteTx: false, 317 fromBalanceAfterTx: '-1', 318 toBalanceAfterTx: '-1', 319 transferReward: '-1' 320 } 321 }, 322 { 323 "name": "gas Limit is too samll", 324 "testInput": { 325 transferValue: 1, 326 isSameAddr: false, 327 isAddressValid: true, 328 gasLimit: 1, 329 gasPrice: -1, 330 nonceIncrement: 1 331 }, 332 "testExpect": { 333 canSendTx: true, 334 canSubmitTx: false, 335 canExcuteTx: false, 336 fromBalanceAfterTx: '-1', 337 toBalanceAfterTx: '-1', 338 transferReward: '-1' 339 } 340 }, 341 { 342 "name": "nonce is below", 343 "testInput": { 344 transferValue: 1, 345 isSameAddr: false, 346 isAddressValid: true, 347 gasLimit: 1, 348 gasPrice: -1, 349 nonceIncrement: 0 350 }, 351 "testExpect": { 352 canSendTx: false, 353 canSubmitTx: false, 354 canExcuteTx: false, 355 fromBalanceAfterTx: '-1', 356 toBalanceAfterTx: '-1', 357 transferReward: '-1' 358 } 359 }, 360 { 361 "name": "nonce is bigger", 362 "testInput": { 363 transferValue: 1, 364 isSameAddr: false, 365 isAddressValid: true, 366 gasLimit: 1, 367 gasPrice: -1, 368 nonceIncrement: 2 369 }, 370 "testExpect": { 371 canSendTx: true, 372 canSubmitTx: false, 373 canExcuteTx: false, 374 fromBalanceAfterTx: '-1', 375 toBalanceAfterTx: '-1', 376 transferReward: '-1' 377 } 378 } 379 ]; 380 381 382 function testCase(index) { 383 var test = testCases[index]; 384 prepareTest(function (err) { 385 if (err) { 386 testResult(test.name, err); 387 } else { 388 try { 389 test_transfer(test.testInput, test.testExpect, function (err) { 390 testResult(test.name, err); 391 testCase(++index); 392 }); 393 } catch (err) { 394 testResult(test.name, err); 395 testCase(++index); 396 } 397 } 398 }); 399 } 400 401 function testResult(name, err) { 402 const filePath = "/neb/app/logs/testResult.txt"; 403 console.log("*****************************"); 404 var testName = "case:" + name; 405 var testResult = "result:" + (err ? false : true); 406 var content = testName + "\n" + testResult + "\n"; 407 if (err) { 408 content = content + err + "\n"; 409 } 410 411 fs.appendFile(filePath, content,'utf8',function(err){ 412 if(err) 413 { 414 console.log(err); 415 } 416 }); 417 } 418 419 function doTest() { 420 testCase(0); 421 } 422 423 var j = schedule.scheduleJob('*/5 * * * *', function(){ 424 console.log("start transaction test case"); 425 doTest(); 426 });