github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/contract/contract.features.test.js (about) 1 "use strict"; 2 3 var Wallet = require("nebulas"); 4 var HttpRequest = require("../../node-request.js"); 5 var TestNetConfig = require("../testnet_config.js"); 6 var Neb = Wallet.Neb; 7 var Transaction = Wallet.Transaction; 8 var FS = require("fs"); 9 var expect = require('chai').expect; 10 var Unit = Wallet.Unit; 11 12 // mocha cases/contract/xxx testneb2 -t 2000000 13 var args = process.argv.splice(2); 14 var env = args[1]; 15 if (env == null){ 16 env = "local"; 17 } 18 var testNetConfig = new TestNetConfig(env); 19 20 var neb = new Neb(); 21 var ChainID = testNetConfig.ChainId; 22 var sourceAccount = testNetConfig.sourceAccount; 23 var coinbase = testNetConfig.coinbase; 24 var apiEndPoint = testNetConfig.apiEndPoint; 25 neb.setRequest(new HttpRequest(apiEndPoint)); 26 27 var contractAddress; 28 var toAddress = Wallet.Account.NewAccount(); 29 var nonce; 30 var contractNonce = 0; 31 32 /* 33 * set this value according to the status of your testnet. 34 * the smaller the value, the faster the test, with the risk of causing error 35 */ 36 37 var maxCheckTime = 50; 38 var checkTimes = 0; 39 var beginCheckTime; 40 41 console.log("env:", env); 42 43 function checkTransaction(hash, callback) { 44 if (checkTimes === 0) { 45 beginCheckTime = new Date().getTime(); 46 } 47 checkTimes += 1; 48 if (checkTimes > maxCheckTime) { 49 console.log("check tx receipt timeout:" + hash); 50 checkTimes = 0; 51 callback(); 52 return; 53 } 54 55 neb.api.getTransactionReceipt(hash).then(function (resp) { 56 57 console.log("tx receipt status:" + resp.status); 58 if (resp.status === 2) { 59 setTimeout(function () { 60 checkTransaction(hash, callback); 61 }, 2000); 62 } else { 63 checkTimes = 0; 64 var endCheckTime = new Date().getTime(); 65 console.log("check tx time: : " + (endCheckTime - beginCheckTime) / 1000); 66 callback(resp); 67 } 68 }).catch(function (err) { 69 console.log("fail to get tx receipt hash: " + hash); 70 console.log("it may becuase the tx is being packing, we are going on to check it!"); 71 // console.log(err); 72 setTimeout(function () { 73 checkTransaction(hash, callback); 74 }, 2000); 75 }); 76 } 77 78 describe('test transfer from contract', function () { 79 before('0. deploy contract', function (done) { 80 try { 81 neb.api.getAccountState(sourceAccount.getAddressString()).then(function(resp) { 82 console.log("----step0. get source account state: " + JSON.stringify(resp)); 83 var contractSource = FS.readFileSync("./nf/nvm/test/test_contract_features.js", "UTF-8"); 84 var contract = { 85 'source': contractSource, 86 "sourceType": "js", 87 "args": '' 88 }; 89 nonce = parseInt(resp.nonce); 90 nonce = nonce + 1; 91 var tx = new Transaction(ChainID, sourceAccount, sourceAccount, 0, nonce, 1000000, 20000000, contract); 92 tx.signTransaction(); 93 return neb.api.sendRawTransaction(tx.toProtoString()); 94 }).then(function(resp) { 95 console.log("----step1. deploy contract: " + JSON.stringify(resp)); 96 contractAddress = resp.contract_address; 97 checkTransaction(resp.txhash, function(resp) { 98 try { 99 expect(resp).to.not.be.a('undefined'); 100 expect(resp.status).to.be.equal(1); 101 console.log("----step2. have been on chain"); 102 done(); 103 } catch(err) { 104 console.log(err); 105 done(err); 106 } 107 }); 108 }).catch(function(err) { 109 console.log("unexpected err: " + err); 110 done(err); 111 }); 112 } catch (err) { 113 console.log("unexpected err: " + err); 114 done(err); 115 } 116 }); 117 118 it ('1. test feature getAccoutState 1/2', function (done) { 119 var contract = { 120 'function': 'testGetAccountState1', 121 "args": '[]', 122 }; 123 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 124 console.log(JSON.stringify(resp)); 125 expect(resp.execute_err).to.be.equal(""); 126 done(); 127 }).catch(function(err) { 128 console.log(err); 129 done(err); 130 }); 131 }); 132 133 it ('2. test feature getAccoutState 2/2', function (done) { 134 var contract = { 135 'function': 'testGetAccountState2', 136 "args": '[]', 137 }; 138 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 139 console.log(JSON.stringify(resp)); 140 expect(resp.execute_err).to.be.equal("Call: Blockchain.getAccountState(), parse address failed"); 141 done(); 142 }).catch(function(err) { 143 console.log(err); 144 done(err); 145 }); 146 }); 147 148 it ('3. test feature getPreBlockHash 1/5[offset is 1]', function (done) { 149 var offset = 1; 150 var contract = { 151 'function': 'testGetPreBlockHash', 152 "args": '[' + offset + ']', 153 }; 154 var hash; 155 var height; 156 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 157 console.log(JSON.stringify(resp)); 158 var result = JSON.parse(resp.result); 159 hash = result.hash; 160 height = result.height; 161 return neb.api.getBlockByHeight(height - offset); 162 }).then(function(resp) { 163 // console.log(JSON.stringify(resp)); 164 expect(resp.hash).to.be.equal(hash); 165 done(); 166 }).catch(function(err) { 167 console.log(err); 168 done(err); 169 }); 170 }); 171 172 it ('4. test feature getPreBlockHash 2/5[offset is "1"]', function (done) { 173 var offset = 1; 174 var contract = { 175 'function': 'testGetPreBlockHash', 176 "args": "[\"" + offset + "\"]", 177 }; 178 var hash; 179 var height; 180 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 181 console.log(JSON.stringify(resp)); 182 var result = JSON.parse(resp.result); 183 hash = result.hash; 184 height = result.height; 185 return neb.api.getBlockByHeight(height - offset); 186 }).then(function(resp) { 187 // console.log(JSON.stringify(resp)); 188 expect(resp.hash).to.be.equal(hash); 189 done(); 190 }).catch(function(err) { 191 console.log(err); 192 done(err); 193 }); 194 }); 195 196 it ('5. test feature getPreBlockHash 3/5[offset is "#1"]', function (done) { 197 var offset = "#1"; 198 var contract = { 199 'function': 'testGetPreBlockHash', 200 "args": "[\"" + offset + "\"]", 201 }; 202 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 203 console.log(JSON.stringify(resp)); 204 expect(resp.execute_err).to.be.equal("Call: getPreBlockHash: invalid offset"); 205 done(); 206 }).catch(function(err) { 207 console.log(err); 208 done(err); 209 }); 210 }); 211 212 it ('6. test feature getPreBlockHash 4/5[offset is 0]', function (done) { 213 var offset = 0; 214 var contract = { 215 'function': 'testGetPreBlockHash', 216 "args": '[' + offset + ']', 217 }; 218 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 219 console.log(JSON.stringify(resp)); 220 expect(resp.execute_err).to.be.equal("Call: getPreBlockHash: invalid offset"); 221 done(); 222 }).catch(function(err) { 223 console.log(err); 224 done(err); 225 }); 226 }); 227 228 it ('7. test feature getPreBlockHash 5/5[offset is too large]', function (done) { 229 var offset = 11111111111111; 230 var contract = { 231 'function': 'testGetPreBlockHash', 232 "args": '[' + offset + ']', 233 }; 234 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 235 console.log(JSON.stringify(resp)); 236 expect(resp.execute_err).to.be.equal("Call: getPreBlockHash: block not exist"); 237 done(); 238 }).catch(function(err) { 239 console.log(err); 240 done(err); 241 }); 242 }); 243 244 it ('8. test feature getPreBlockSeed 1/5[offset is 1]', function (done) { 245 var offset = 1; 246 var contract = { 247 'function': 'testGetPreBlockSeed', 248 "args": '[' + offset + ']', 249 }; 250 var seed; 251 var height; 252 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 253 console.log(JSON.stringify(resp)); 254 var result = JSON.parse(resp.result); 255 seed = result.seed; 256 console.log(seed); 257 height = result.height; 258 return neb.api.getBlockByHeight(height - offset); 259 }).then(function(resp) { 260 console.log(JSON.stringify(resp)); 261 expect(resp.randomSeed).to.be.equal(seed); 262 done(); 263 }).catch(function(err) { 264 console.log(err); 265 done(err); 266 }); 267 }); 268 269 it ('9. test feature getPreBlockSeed 2/5[offset is "1"]', function (done) { 270 var offset = 1; 271 var contract = { 272 'function': 'testGetPreBlockSeed', 273 "args": "[\"" + offset + "\"]", 274 }; 275 var seed; 276 var height; 277 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 278 console.log(JSON.stringify(resp)); 279 var result = JSON.parse(resp.result); 280 seed = result.seed; 281 height = result.height; 282 return neb.api.getBlockByHeight(height - offset); 283 }).then(function(resp) { 284 // console.log(JSON.stringify(resp)); 285 expect(resp.randomSeed).to.be.equal(seed); 286 done(); 287 }).catch(function(err) { 288 console.log(err); 289 done(err); 290 }); 291 }); 292 293 it ('10. test feature getPreBlockSeed 3/5[offset is 0]', function (done) { 294 var offset = 0; 295 var contract = { 296 'function': 'testGetPreBlockSeed', 297 "args": '[' + offset + ']', 298 }; 299 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 300 console.log(JSON.stringify(resp)); 301 expect(resp.execute_err).to.be.equal("Call: getPreBlockSeed: invalid offset"); 302 done(); 303 }).catch(function(err) { 304 console.log(err); 305 done(err); 306 }); 307 }); 308 309 it ('11. test feature getPreBlockSeed 4/5[offset is too large]', function (done) { 310 var offset = 11111111111111; 311 var contract = { 312 'function': 'testGetPreBlockSeed', 313 "args": '[' + offset + ']', 314 }; 315 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 316 console.log(JSON.stringify(resp)); 317 expect(resp.execute_err).to.be.equal("Call: getPreBlockSeed: block not exist"); 318 done(); 319 }).catch(function(err) { 320 console.log(err); 321 done(err); 322 }); 323 }); 324 it ('12. test feature getPreBlockSeed 5/5[offset is "#1"]', function (done) { 325 var offset = "#1"; 326 var contract = { 327 'function': 'testGetPreBlockSeed', 328 "args": "[\"" + offset + "\"]", 329 }; 330 neb.api.call(sourceAccount.getAddressString(), contractAddress, Unit.nasToBasic(10), nonce, 200000, 1000000, contract).then(function(resp){ 331 console.log(JSON.stringify(resp)); 332 expect(resp.execute_err).to.be.equal("Call: getPreBlockSeed: invalid offset"); 333 done(); 334 }).catch(function(err) { 335 console.log(err); 336 done(err); 337 }); 338 }); 339 });