Exemple #1
0
        public void CanExchangeFastPingPong()
        {
            using (var tester = new NodeServerTester())
            {
                var n1 = tester.Node1;
                n1.Behaviors.Add(new PingPongBehavior
                {
                    PingInterval    = TimeSpan.FromSeconds(0.1),
                    TimeoutInterval = TimeSpan.FromSeconds(0.8)
                });

                n1.VersionHandshake();
                Assert.Equal(NodeState.HandShaked, n1.State);
                Assert.True(!n1.Inbound);

                var n2 = tester.Node2;
                n2.Behaviors.Add(new PingPongBehavior
                {
                    PingInterval    = TimeSpan.FromSeconds(0.1),
                    TimeoutInterval = TimeSpan.FromSeconds(0.5)
                });
                Assert.Equal(NodeState.HandShaked, n2.State);
                Assert.True(n2.Inbound);
                Thread.Sleep(2000);
                Assert.Equal(NodeState.HandShaked, n2.State);
                n1.Behaviors.Clear();
                Thread.Sleep(1200);
                Assert.True(n2.State == NodeState.Disconnecting || n2.State == NodeState.Offline);
                Assert.True(n2.DisconnectReason.Reason.StartsWith("Pong timeout", StringComparison.Ordinal));
            }
        }
Exemple #2
0
        public void MaxConnectionLimit()
        {
            using (var tester = new NodeServerTester())
            {
                tester.Server1.MaxConnections = 4;
                Node.Connect(tester.Network, tester.Server1.ExternalEndpoint).VersionHandshake();
                Node.Connect(tester.Network, tester.Server1.ExternalEndpoint).VersionHandshake();
                Node.Connect(tester.Network, tester.Server1.ExternalEndpoint).VersionHandshake();
                Node.Connect(tester.Network, tester.Server1.ExternalEndpoint).VersionHandshake();

                TestUtils.Eventually(() => tester.Server1.ConnectedNodes.Count == 4);

                var connect = Node.Connect(tester.Network, tester.Server1.ExternalEndpoint);
                try
                {
                    connect.VersionHandshake();
                }
                catch
                {
                }

                TestUtils.Eventually(() => tester.Server1.ConnectedNodes.Count == 4);
                TestUtils.Eventually(() => connect.IsConnected == false);
            }
        }
Exemple #3
0
        public void CanConnectMultipleTimeToServer()
        {
            using (var tester = new NodeServerTester())
            {
                var nodeCount = 0;
                tester.Server1.NodeAdded   += (s, a) => nodeCount++;
                tester.Server1.NodeRemoved += (s, a) => nodeCount--;

                var n1 = Node.Connect(tester.Server1.Network, tester.Server1.ExternalEndpoint);
                n1.VersionHandshake();
                Thread.Sleep(100);
                Assert.Equal(1, nodeCount);
                n1.PingPong();
                var n2 = Node.Connect(tester.Server1.Network, tester.Server1.ExternalEndpoint);
                n2.VersionHandshake();
                Thread.Sleep(100);
                Assert.Equal(2, nodeCount);
                n2.PingPong();
                n1.PingPong();
                Assert.Throws <ProtocolException>(() => n2.VersionHandshake());
                Thread.Sleep(100);
                n2.PingPong();
                Assert.Equal(2, nodeCount);
                n2.Disconnect();
                Thread.Sleep(100);
                Assert.Equal(1, nodeCount);
            }
        }
Exemple #4
0
 public void CantConnectToYourself()
 {
     using (var tester = new NodeServerTester())
     {
         tester.Server2.Nonce = tester.Server1.Nonce;
         Assert.Throws(typeof(InvalidOperationException), () => { tester.Node1.VersionHandshake(); });
     }
 }
Exemple #5
0
 public void CanReceiveHandshake()
 {
     using (var tester = new NodeServerTester())
     {
         var toS2 = tester.Node1;
         toS2.VersionHandshake();
         Assert.Equal(NodeState.HandShaked, toS2.State);
         Thread.Sleep(100);                 //Let the time to Server2 to add the new node, else the test was failing sometimes.
         Assert.Equal(NodeState.HandShaked, tester.Node2.State);
         Assert.NotNull(toS2.TimeOffset);
     }
 }
Exemple #6
0
 public void ServerDisconnectCorrectlyFromDroppingClient()
 {
     using (var tester = new NodeServerTester())
     {
         var to2 = tester.Node1;
         to2.VersionHandshake();
         Assert.True(tester.Server1.IsConnectedTo(tester.Server2.ExternalEndpoint));
         Thread.Sleep(100);
         Assert.True(tester.Server2.IsConnectedTo(tester.Server1.ExternalEndpoint));
         to2.Disconnect();
         Thread.Sleep(100);
         Assert.False(tester.Server1.IsConnectedTo(tester.Server2.ExternalEndpoint));
         Thread.Sleep(100);
         Assert.False(tester.Server2.IsConnectedTo(tester.Server1.ExternalEndpoint));
     }
 }
Exemple #7
0
 public void CanRespondToPong()
 {
     using (var tester = new NodeServerTester())
     {
         var toS2 = tester.Node1;
         toS2.VersionHandshake();
         var ping   = new PingPayload();
         var cancel = new CancellationTokenSource();
         cancel.CancelAfter(10000);
         using (var list = toS2.CreateListener())
         {
             toS2.SendMessageAsync(ping);
             while (true)
             {
                 var pong = list.ReceivePayload <PongPayload>(cancel.Token);
                 if (ping.Nonce == pong.Nonce)
                 {
                     break;
                 }
             }
         }
     }
 }
Exemple #8
0
        public void CanMaintainChainWithChainBehavior()
        {
            using (var builder = NodeBuilderEx.Create())
            {
                var node = builder.CreateNode(true).CreateNodeClient();
                builder.Nodes[0].Generate(600);
                var rpc   = builder.Nodes[0].CreateRPCClient();
                var chain = node.GetChain(rpc.GetBlockHash(500));
                Assert.True(chain.Height == 500);
                using (var tester = new NodeServerTester())
                {
                    var n1 = tester.Node1;
                    n1.Behaviors.Add(new ChainBehavior(chain));
                    n1.VersionHandshake();
                    Assert.True(n1.MyVersion.StartHeight == 500);
                    var n2 = tester.Node2;
                    Assert.True(n2.MyVersion.StartHeight == 0);
                    Assert.True(n2.PeerVersion.StartHeight == 500);
                    Assert.True(n1.State == NodeState.HandShaked);
                    Assert.True(n2.State == NodeState.HandShaked);
                    var behavior = new ChainBehavior(new ConcurrentChain(Network.RegTest));
                    n2.Behaviors.Add(behavior);
                    TestUtils.Eventually(() => behavior.Chain.Height == 500);
                    var chain2 = n2.GetChain(rpc.GetBlockHash(500));
                    Assert.True(chain2.Height == 500);
                    var chain1 = n1.GetChain(rpc.GetBlockHash(500));
                    Assert.True(chain1.Height == 500);
                    chain1 = n1.GetChain(rpc.GetBlockHash(499));
                    Assert.True(chain1.Height == 499);

                    //Should not broadcast above HighestValidatorPoW
                    n1.Behaviors.Find <ChainBehavior>().SharedState.HighestValidatedPoW = chain1.GetBlock(300);
                    chain1 = n2.GetChain(rpc.GetBlockHash(499));
                    Assert.True(chain1.Height == 300);
                }
            }
        }