/// <summary>
 /// Prepares the simulator that moves packets through the network.
 /// </summary>
 private void SetupSimulator()
 {
     simulator      = new NetworkSimulator(model);
     simulatorTimer = new DispatcherTimer {
         Interval = TimeSpan.FromMilliseconds(1500)
     };
     simulatorTimer.Tick      += delegate { simulator.Tick(); };
     simulator.SomethingBroke += OnNetworkFailure;
 }
    //public Mesh testMesh = null;
    // Start is called before the first frame update
    void Start()
    {
        if (NetworkSimulatorSingleton != null)
        {
            Destroy(this);
            return;
        }
        lineRenderers             = new Stack <LineRenderer>();
        incomingLineRenderers     = new ConcurrentQueue <lrStruct>();
        outgoingQueue             = new ConcurrentQueue <messagePackage>();
        NetworkSimulatorSingleton = this;

#if !UNITY_EDITOR
        Listen();
#endif
    }
示例#3
0
        public void MiningNodeWithOneConnectionAlwaysSynced()
        {
            NetworkSimulator simulator = new NetworkSimulator();

            simulator.Initialize(4);

            var miner        = simulator.Nodes[0];
            var connector    = simulator.Nodes[1];
            var networkNode1 = simulator.Nodes[2];
            var networkNode2 = simulator.Nodes[3];

            // Connect nodes with each other. Miner is connected to connector and connector, node1, node2 are connected with each other.
            miner.CreateRPCClient().AddNode(connector.Endpoint, true);
            connector.CreateRPCClient().AddNode(networkNode1.Endpoint, true);
            connector.CreateRPCClient().AddNode(networkNode2.Endpoint, true);
            networkNode1.CreateRPCClient().AddNode(networkNode2.Endpoint, true);

            simulator.MakeSureEachNodeCanMineAndSync();

            int networkHeight = miner.FullNode.Chain.Height;

            Assert.Equal(networkHeight, simulator.Nodes.Count);

            // Random node on network generates a block.
            networkNode1.GenerateStratis(1);

            // Wait until connector get the hash of network's block.
            while ((connector.FullNode.ChainBehaviorState.ConsensusTip.HashBlock != networkNode1.FullNode.ChainBehaviorState.ConsensusTip.HashBlock) ||
                   (networkNode1.FullNode.ChainBehaviorState.ConsensusTip.Height == networkHeight))
            {
                Thread.Sleep(1);
            }

            // Make sure that miner did not advance yet but connector did.
            Assert.NotEqual(miner.FullNode.Chain.Tip.HashBlock, networkNode1.FullNode.Chain.Tip.HashBlock);
            Assert.Equal(connector.FullNode.Chain.Tip.HashBlock, networkNode1.FullNode.Chain.Tip.HashBlock);
            Assert.Equal(miner.FullNode.Chain.Tip.Height, networkHeight);
            Assert.Equal(connector.FullNode.Chain.Tip.Height, networkHeight + 1);

            // Miner mines the block.
            miner.GenerateStratis(1);
            TestHelper.WaitLoop(() => TestHelper.IsNodeSynced(miner));

            networkHeight++;

            // Make sure that at this moment miner's tip != network's and connector's tip.
            Assert.NotEqual(miner.FullNode.Chain.Tip.HashBlock, networkNode1.FullNode.Chain.Tip.HashBlock);
            Assert.Equal(connector.FullNode.Chain.Tip.HashBlock, networkNode1.FullNode.Chain.Tip.HashBlock);
            Assert.Equal(miner.FullNode.Chain.Tip.Height, networkHeight);
            Assert.Equal(connector.FullNode.Chain.Tip.Height, networkHeight);

            connector.GenerateStratis(1);
            networkHeight++;

            int delay = 0;

            while (true)
            {
                Thread.Sleep(50);
                if (simulator.DidAllNodesReachHeight(networkHeight))
                {
                    break;
                }
                delay += 50;

                Assert.True(delay < 10 * 1000, "Miner node was not able to advance!");
            }

            Assert.Equal(networkNode1.FullNode.Chain.Tip.HashBlock, miner.FullNode.Chain.Tip.HashBlock);
        }
 public BlockChainController(ILogger <BlockChainController> logger, IBlockChainService blockChainService, NetworkSimulator networkSimulator)
 {
     _logger            = logger;
     _blockChainService = blockChainService;
     _networkSimulator  = networkSimulator;
 }