github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/cases/network/sync.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 - 1);
    16  var connected = 0;
    17  nodes.Start();
    18  
    19  describe('check sync', function () {
    20      before(function (done) {
    21          this.timeout(300000);
    22          setTimeout(done, 5000);
    23      });
    24  
    25      it('check', function (done) {
    26          // start servers
    27          console.log("start servers");
    28          let height = 0;
    29          while (true) {
    30              nodes.RPC(0).api.blockDump(1).then(function (resp) {
    31                  var block = JSON.parse(resp.data)[0];
    32                  console.log(block);
    33                  if (height < block.height) {
    34                      height = block.height;
    35                      console.log("H", height);
    36                  }
    37              });
    38              console.log(height);
    39              if (height > 3) {
    40                  console.log("√ the height of current tail block is higher than 3");
    41                  break;
    42              }
    43              sleep(3000);
    44          }
    45  
    46          // start another node to test sync
    47          nodes.NewNode(nodeCnt - 1);
    48          sleep(15000);
    49          var blockSeed;
    50          nodes.RPC(0).api.blockDump(1).then(function (resp) {
    51              blockSeed = JSON.parse(resp.data)[0];
    52          });
    53  
    54          var block;
    55          nodes.RPC(5).api.blockDump(1).then(function (resp) {
    56              block = JSON.parse(resp.data)[0];
    57          });
    58          sleep(3000);
    59          console.log(blockSeed, " vs ", block);
    60          expect(blockSeed.height).to.be.equal(block.height);
    61          console.log("√ start a new node and the new node has synced all the blocks");
    62  
    63          // verify block sync
    64          nodes.RPC(5).admin.changeNetworkID(10);
    65  
    66          sleep(10000);
    67          var blockA;
    68          nodes.RPC(0).api.blockDump(1).then(function (resp) {
    69              blockA = JSON.parse(resp.data)[0];
    70          });
    71  
    72          var blockB;
    73          nodes.RPC(5).api.blockDump(1).then(function (resp) {
    74              blockB = JSON.parse(resp.data)[0];
    75          });
    76          sleep(10000);
    77          console.log(blockA, " vs ", blockB);
    78          expect(blockA.hash).not.to.be.equal(blockB.hash);
    79          console.log("√ changed the new node networkID to 10 and the new node go to forked");
    80  
    81          nodes.RPC(5).admin.changeNetworkID(1);
    82          sleep(20000);
    83          nodes.RPC(0).api.blockDump(1).then(function (resp) {
    84              blockA = JSON.parse(resp.data)[0];
    85          });
    86  
    87          nodes.RPC(5).api.blockDump(1).then(function (resp) {
    88              blockB = JSON.parse(resp.data)[0];
    89          });
    90          sleep(10000);
    91  
    92          console.log(blockA, " vs ", blockB);
    93          expect(blockA.hash).to.be.equal(blockB.hash);
    94          console.log("√ recover the new node networkID to 1 and the new node is synchronized");
    95  
    96          // quit
    97          nodes.Stop();
    98          done();
    99      });
   100  });