github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/rpc/GetEventsByHash.test.js (about)

     1  'use strict';
     2  
     3  var Wallet = require("nebulas");
     4  
     5  var expect = require('chai').expect;
     6  var rpc_client = require('./rpc_client/rpc_client.js');
     7  
     8  
     9  var coinbase,
    10      sourceAccount,
    11      toAddress = Wallet.Account.NewAccount(),
    12      txhash,
    13      client,
    14      chain_id,
    15      server_address;
    16  
    17  var env = process.env.NET || 'local';
    18  env = "maintest";
    19  if (env === 'testneb1') {
    20      chain_id = 1001;
    21      sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52");
    22      coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17";
    23      server_address = "35.182.48.19:8684";
    24    
    25    } else if (env === "testneb2") {
    26      chain_id = 1002;
    27      sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52");
    28      coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17";
    29      server_address = "34.205.26.12:8684";
    30    
    31    } else if (env === "testneb3") {
    32      chain_id = 1003;
    33      sourceAccount = new Wallet.Account("25a3a441a34658e7a595a0eda222fa43ac51bd223017d17b420674fb6d0a4d52");
    34      coinbase = "n1SAeQRVn33bamxN4ehWUT7JGdxipwn8b17";
    35      server_address = "35.177.214.138:8684";
    36    
    37    } else if (env === "testneb4") { //super node
    38      chain_id = 1004;
    39      sourceAccount = new Wallet.Account("c75402f6ffe6edcc2c062134b5932151cb39b6486a7beb984792bb9da3f38b9f");
    40      coinbase = "n1EzGmFsVepKduN1U5QFyhLqpzFvM9sRSmG";
    41      server_address = "35.154.108.11:8684";
    42    } else if (env === "testneb4_normalnode"){
    43      chain_id = 1004;
    44      sourceAccount = new Wallet.Account("c75402f6ffe6edcc2c062134b5932151cb39b6486a7beb984792bb9da3f38b9f");
    45      coinbase = "n1EzGmFsVepKduN1U5QFyhLqpzFvM9sRSmG";
    46      server_address = "18.197.107.228:8684";
    47    } else if (env === "local") {
    48      chain_id = 100;
    49      sourceAccount = new Wallet.Account("d80f115bdbba5ef215707a8d7053c16f4e65588fd50b0f83369ad142b99891b5");
    50      coinbase = "n1QZMXSZtW7BUerroSms4axNfyBGyFGkrh5";
    51      server_address = "127.0.0.1:8684";
    52    
    53    } else if (env === "maintest"){
    54      chain_id = 2;
    55      sourceAccount = new Wallet.Account("d2319a8a63b1abcb0cc6d4183198e5d7b264d271f97edf0c76cfdb1f2631848c");
    56      coinbase = "n1dZZnqKGEkb1LHYsZRei1CH6DunTio1j1q";
    57      server_address = "54.149.15.132:8684";
    58    } else {
    59      throw new Error("invalid env (" + env + ").");
    60    }
    61  
    62  function testGetEventsByHash(testInput, testExpect, done) {
    63      try {
    64          client.GetEventsByHash(testInput, (err, resp) => {
    65              try {
    66                  expect(!!err).to.equal(testExpect.hasError);
    67  
    68                  if (err) {
    69                      console.log("call return err: " + JSON.stringify(err));
    70                      expect(err).have.property('details').string(testExpect.errorMsg);
    71                  } else {
    72                      console.log("call return success: " + JSON.stringify(resp));
    73                      expect(resp).to.have.property('events');
    74                  }
    75                  done();
    76              } catch (err) {
    77                  done(err);
    78              }
    79          });
    80      } catch(err) {
    81          if (testExpect.hasError) {
    82              try {
    83                  expect(err.toString()).to.have.string(testExpect.errorMsg);
    84                  done()
    85                  return;
    86              } catch(er) {}
    87          } 
    88          done(err)
    89      }
    90  }
    91  
    92  function checkTx(hash, done) {
    93      client.GetTransactionReceipt({hash: txhash}, (err, resp) => {
    94          console.log("err: " + JSON.stringify(err))
    95          console.log("resp: " + JSON.stringify(resp))
    96          
    97          if ((err && expect(err).to.have.property("details").equal("transaction not found")) || resp.status == 2) {
    98              setTimeout(() => {
    99                  checkTx(hash, done)
   100              }, 2000); 
   101              return;
   102          } 
   103          if (resp.status == 1) {
   104              console.log("tx send done: " + JSON.stringify(resp));
   105              done();
   106          } else {
   107              done(new Error("tx failed"));
   108          }
   109      });
   110  }
   111  
   112  describe("rpc: GetEventsByHash", () => {
   113      before((done) => {
   114          client = rpc_client.new_client(server_address);
   115  
   116          try {
   117              /* client.NewAccount({passphrase: "passphrase"}, (err, resp) => {
   118                  expect(!!err).to.be.false;
   119                  expect(resp).to.have.property('address');
   120                  toAddress = resp.address;
   121                  console.log("create new `to` account: " + address); */
   122                  
   123                  client.GetAccountState({address: sourceAccount.getAddressString()}, (err, resp) => {
   124                      console.log("sourceAccount state: " + JSON.stringify(resp));
   125                      var coinbaseNonce = resp.nonce;
   126  
   127                      var adminclient = rpc_client.new_client(server_address, 'AdminService');
   128                      adminclient.SendTransactionWithPassphrase({
   129                          transaction: {
   130                              from: sourceAccount.getAddressString(),
   131                              to: toAddress.getAddressString(),
   132                              value: "100000000",
   133                              nonce: parseInt(coinbaseNonce) + 1,
   134                              gas_price: "1000000",
   135                              gas_limit: "2000000"
   136                          },
   137                          passphrase: 'passphrase'
   138                      }, (err, resp) => {
   139                          if (err) {
   140                              console.log("send tx error: " + JSON.stringify(err));
   141                              done(err)
   142                              return;
   143                          }
   144                          console.log("send tx response: " + JSON.stringify(resp));
   145                          try {
   146                              expect(resp).to.have.property('txhash');
   147                              txhash = resp.txhash;
   148  
   149                              // check tx done
   150                              checkTx(txhash, done);
   151                          } catch (err) {
   152                              done(err);
   153                          }
   154                      });
   155                  });
   156              // });
   157          } catch(err) {
   158              done(err)
   159          }
   160      });
   161  
   162      it('1. nonexistent `hash`', done => {
   163          var testInput = {
   164              hash: "02930f09029f0f"
   165          }
   166  
   167          var testExpect = {
   168              hasError: true,
   169              errorMsg: 'invalid argument(s)'
   170          }
   171  
   172          testGetEventsByHash(testInput, testExpect, done);
   173      });
   174  
   175      it('2. odd length `hash`', done => {
   176          var testInput = {
   177              hash: "02930f09029ff"
   178          }
   179  
   180          var testExpect = {
   181              hasError: true,
   182              errorMsg: 'encoding/hex: odd length hex string'
   183          }
   184  
   185          testGetEventsByHash(testInput, testExpect, done);
   186      });
   187  
   188      it('3. non-hexadecimal char `hash`', done => {
   189          var testInput = {
   190              hash: "02930fg09029ff"
   191          }
   192  
   193          var testExpect = {
   194              hasError: true,
   195              errorMsg: 'encoding/hex: invalid byte'
   196          }
   197  
   198          testGetEventsByHash(testInput, testExpect, done);
   199      });
   200  
   201      it('4. empty `hash`', done => {
   202          var testInput = {
   203              hash: ""
   204          }
   205  
   206          var testExpect = {
   207              hasError: true,
   208              errorMsg: 'please input valid hash'
   209          }
   210  
   211          testGetEventsByHash(testInput, testExpect, done);
   212      });
   213  
   214      it('5. missing `hash`', done => {
   215          var testInput = {}
   216  
   217          var testExpect = {
   218              hasError: true,
   219              errorMsg: 'please input valid hash'
   220          }
   221  
   222          testGetEventsByHash(testInput, testExpect, done);
   223      });
   224  
   225      it('6. normal `hash`', done => {
   226          var testInput = {
   227              hash: txhash
   228          }
   229  
   230          var testExpect = {
   231              hasError: false,
   232              errorMsg: ''
   233          }
   234  
   235          testGetEventsByHash(testInput, testExpect, done);
   236      });
   237  })