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

     1  'use strict';
     2  
     3  var LocalNodes = require('../../local-nodes');
     4  var BigNumber = require('bignumber.js');
     5  var expect = require('chai').expect;
     6  var sleep = require("system-sleep")
     7  var os = require('os');
     8  
     9  var nodeCnt = 6;
    10  var validators = 6;
    11  var blockInterval = 5;
    12  var dynastyInterval = 60;
    13  var reward = new BigNumber("48e16");
    14  var initial = new BigNumber("1e18");
    15  var nodes = new LocalNodes(nodeCnt);
    16  var now = new Date().getTime() / 1000;
    17  var connected = 0;
    18  nodes.Start();
    19  
    20  function unlockAccount() {
    21      var node = nodes.Node(1);
    22      return node.RPC().admin.unlockAccount(node.Coinbase(), node.Passphrase());
    23  }
    24  
    25  function changeCandidate() {
    26      var node = nodes.Node(1);
    27      return node.RPC().api.sendTransaction(
    28          node.Coinbase(), node.Coinbase(),
    29          0, 1, 0, 20000000,
    30          null, { action: "logout" }, null)
    31  }
    32  
    33  function changeVote() {
    34      var node = nodes.Node(1);
    35      return node.RPC().api.sendTransaction(
    36          node.Coinbase(), node.Coinbase(),
    37          0, 2, 0, 20000000,
    38          null, null, { action: "do", delegatee: "fc751b484bd5296f8d267a8537d33f25a848f7f7af8cfcf6" });
    39  }
    40  
    41  
    42  function check(resp) {
    43      return new Promise(function (res, rej) {
    44          (resp != undefined && dynasty > 2) ? res() : rej()
    45      })
    46  }
    47  
    48  function Stop(done) {
    49      nodes.Stop();
    50      done();
    51  }
    52  
    53  describe('right miner', function () {
    54      before(function (done) {
    55          this.timeout(300000);
    56          setTimeout(done, 5000);
    57      });
    58  
    59      it('check', function (done) {
    60          // start servers
    61          console.log("start servers");
    62          while (true) {
    63              if (connected == nodeCnt - 1) break;
    64              var nodeInfo = nodes.RPC(0).api.nodeInfo().then(function (resp) {
    65                  console.log(resp)
    66                  connected = resp.route_table.length
    67              });
    68              sleep(3000);
    69          }
    70  
    71          Promise.resolve()
    72              .then(unlockAccount)
    73              .then(function (resp) {
    74                  console.log(resp);
    75                  expect(resp).to.not.have.property("error");
    76              })
    77              .then(changeCandidate)
    78              .then(function (resp) {
    79                  console.log(resp);
    80                  expect(resp).to.not.have.property("error");
    81              })
    82              .then(changeVote)
    83              .then(function (resp) {
    84                  console.log(resp);
    85                  expect(resp).to.be.have.property("txhash");
    86              });
    87  
    88          for (var i = 0; i < dynastyInterval * 2 + blockInterval; i++) {
    89              console.log(i, dynastyInterval * 2 + blockInterval);
    90              sleep(1000);
    91          }
    92  
    93          nodes.RPC(0).admin.getDynasty()
    94              .then(function (resp) {
    95                  console.log(resp);
    96                  expect(resp.delegatees[1]).to.be.equal("333cb3ed8c417971845382ede3cf67a0a96270c05fe2f700");
    97                  expect(resp.delegatees[validators - 1]).to.be.equal("fc751b484bd5296f8d267a8537d33f25a848f7f7af8cfcf6");
    98              })
    99              .then(function () {
   100                  Stop(done);
   101              })
   102              .catch(function (err) {
   103                  console.log(err);
   104                  Stop(done);
   105              });
   106      });
   107  });