コード例 #1
0
        public void AddPeer()
        {
            var pubKey0 = new PrivateKey().PublicKey;
            var pubKey1 = new PrivateKey().PublicKey;
            var pubKey2 = new PrivateKey().PublicKey;
            var pubKey3 = new PrivateKey().PublicKey;
            var table   = new RoutingTable(
                pubKey0.ToAddress(),
                1,
                2,
                new System.Random(),
                Logger.None);
            var peer1 = new BoundPeer(pubKey1, new DnsEndPoint("0.0.0.0", 1234), AppProtocolVer);
            var peer2 = new BoundPeer(pubKey2, new DnsEndPoint("0.0.0.0", 1234), AppProtocolVer);
            var peer3 = new BoundPeer(pubKey3, new DnsEndPoint("0.0.0.0", 1234), AppProtocolVer);

            table.AddPeer(peer1);
            table.AddPeer(peer2);
            table.AddPeer(peer3);
            table.AddPeer(peer1);
            table.AddPeer(peer3);
            Assert.Equal(
                new HashSet <BoundPeer> {
                peer1, peer2
            },
                table.Peers.ToHashSet()
                );
        }
コード例 #2
0
        public void PeersToRefreshInSingleBucket()
        {
            var       publicKey = new PrivateKey().PublicKey;
            var       table     = new RoutingTable(publicKey.ToAddress(), 1);
            const int peerCount = 10;

            BoundPeer[] peers = Enumerable.Range(0, peerCount)
                                .Select(
                i => new BoundPeer(
                    new PrivateKey().PublicKey,
                    new DnsEndPoint("0.0.0.0", 1000 + i)))
                                .ToArray();
            for (int i = 0; i < peerCount; i++)
            {
                table.AddPeer(
                    peers[i],
                    DateTimeOffset.UtcNow - TimeSpan.FromMinutes(2) + TimeSpan.FromSeconds(i));
            }

            Assert.Equal(peerCount, table.Peers.Count());
            for (int i = 0; i < peerCount; i++)
            {
                Assert.Equal(peers[i], table.PeersToRefresh(TimeSpan.FromMinutes(1)).First());
                table.AddPeer(peers[i]);
            }

            Assert.Empty(table.PeersToRefresh(TimeSpan.FromMinutes(1)));
        }
コード例 #3
0
        public void AddNull()
        {
            var          pubKey = new PrivateKey().PublicKey;
            RoutingTable table  = CreateTable(pubKey.ToAddress());

            Assert.Throws <ArgumentNullException>(() => table.AddPeer(null));
        }
コード例 #4
0
        public void PeersToRefresh()
        {
            var(publicKey, publicKeys) = GeneratePeersDifferentBuckets();
            var table     = new RoutingTable(publicKey.ToAddress());
            int peerCount = publicKeys.Length;

            BoundPeer[] peers = publicKeys
                                .Select(
                key => new BoundPeer(
                    key,
                    new DnsEndPoint("0.0.0.0", 1234)))
                                .ToArray();
            for (var i = 0; i < peerCount; i++)
            {
                table.AddPeer(
                    peers[i],
                    DateTimeOffset.UtcNow - (i % 2 == 0 ? TimeSpan.Zero : TimeSpan.FromMinutes(2)));
            }

            Assert.Equal(peerCount, table.Peers.Count());
            Assert.Equal(
                Enumerable
                .Range(0, peerCount / 2)
                .Select(i => peers[i * 2 + 1]).ToHashSet(),
                table.PeersToRefresh(TimeSpan.FromMinutes(1)).ToHashSet());
        }
コード例 #5
0
        public void PeersToBroadcast()
        {
            var(publicKey, publicKeys) = GeneratePeersDifferentBuckets();

            var table = new RoutingTable(publicKey.ToAddress());
            var peers = publicKeys
                        .Select(pk => new BoundPeer(pk, new DnsEndPoint("0.0.0.0", 1234)))
                        .ToArray();

            Assert.Equal(10, peers.Length);
            for (var i = 0; i < peers.Length; i++)
            {
                var peer = peers[i];
                table.AddPeer(peer);
                Assert.Equal(i / 2, table.GetBucketIndexOf(peer.Address));
            }

            var broadcastCandidate = table.PeersToBroadcast(null, 0).ToArray();

            Assert.Equal(5, broadcastCandidate.Length);
            Assert.Equal(
                new HashSet <int> {
                0, 1, 2, 3, 4
            },
                broadcastCandidate.Select(peer => table.GetBucketIndexOf(peer.Address))
                .ToHashSet());

            broadcastCandidate = table.PeersToBroadcast(null, 10).ToArray();
            Assert.Equal(10, broadcastCandidate.Length);
            Assert.Equal(peers.ToHashSet(), broadcastCandidate.ToHashSet());
        }
コード例 #6
0
        public void AddSelf()
        {
            var          pubKey = new PrivateKey().PublicKey;
            RoutingTable table  = CreateTable(pubKey.ToAddress());
            var          peer   = new BoundPeer(pubKey, new DnsEndPoint("0.0.0.0", 1234), AppProtocolVer);

            Assert.Throws <ArgumentException>(() => table.AddPeer(peer));
        }
コード例 #7
0
        public void RemovePeer()
        {
            var pubKey1 = new PrivateKey().PublicKey;
            var pubKey2 = new PrivateKey().PublicKey;
            var table   = new RoutingTable(pubKey1.ToAddress(), 1, 2);
            var peer1   = new BoundPeer(pubKey1, new DnsEndPoint("0.0.0.0", 1234));
            var peer2   = new BoundPeer(pubKey2, new DnsEndPoint("0.0.0.0", 1234));

            Assert.Throws <ArgumentException>(() => table.RemovePeer(peer1));

            bool ret = table.RemovePeer(peer2);

            Assert.False(ret);
            table.AddPeer(peer2);
            ret = table.RemovePeer(peer2);
            Assert.True(ret);
        }
コード例 #8
0
        public void RemovePeer()
        {
            var pubKey1 = new PrivateKey().PublicKey;
            var pubKey2 = new PrivateKey().PublicKey;
            var table   = new RoutingTable(
                pubKey1.ToAddress(),
                1,
                2,
                new System.Random(),
                Logger.None);
            var peer1 = new BoundPeer(pubKey1, new DnsEndPoint("0.0.0.0", 1234), AppProtocolVer);
            var peer2 = new BoundPeer(pubKey2, new DnsEndPoint("0.0.0.0", 1234), AppProtocolVer);

            Assert.Throws <ArgumentException>(() => table.RemovePeer(peer1));
            Assert.Throws <ArgumentNullException>(() => table.RemovePeer(null));

            bool ret = table.RemovePeer(peer2);

            Assert.False(ret);
            table.AddPeer(peer2);
            ret = table.RemovePeer(peer2);
            Assert.True(ret);
        }
コード例 #9
0
        public async Task BroadcastMessage()
        {
            var        address    = new PrivateKey().ToAddress();
            ITransport transportA = null;
            ITransport transportB = CreateTransport(
                privateKey: GeneratePrivateKeyOfBucketIndex(address, 0));
            ITransport transportC = CreateTransport(
                privateKey: GeneratePrivateKeyOfBucketIndex(address, 1));
            ITransport transportD = CreateTransport(
                privateKey: GeneratePrivateKeyOfBucketIndex(address, 2));

            var tcsB = new TaskCompletionSource <Message>();
            var tcsC = new TaskCompletionSource <Message>();
            var tcsD = new TaskCompletionSource <Message>();

            transportB.ProcessMessageHandler.Register(MessageHandler(tcsB));
            transportC.ProcessMessageHandler.Register(MessageHandler(tcsC));
            transportD.ProcessMessageHandler.Register(MessageHandler(tcsD));

            Func <Message, Task> MessageHandler(TaskCompletionSource <Message> tcs)
            {
                return(async message =>
                {
                    if (message is Ping)
                    {
                        tcs.SetResult(message);
                    }

                    await Task.Yield();
                });
            }

            try
            {
                await InitializeAsync(transportB);
                await InitializeAsync(transportC);
                await InitializeAsync(transportD);

                var table = new RoutingTable(address, bucketSize: 1);
                table.AddPeer(transportB.AsPeer as BoundPeer);
                table.AddPeer(transportC.AsPeer as BoundPeer);
                table.AddPeer(transportD.AsPeer as BoundPeer);

                transportA = CreateTransport();
                await InitializeAsync(transportA);

                transportA.BroadcastMessage(
                    table.PeersToBroadcast(transportD.AsPeer.Address),
                    new Ping());

                await Task.WhenAll(tcsB.Task, tcsC.Task);

                Assert.IsType <Ping>(tcsB.Task.Result);
                Assert.IsType <Ping>(tcsC.Task.Result);
                Assert.False(tcsD.Task.IsCompleted);

                tcsD.SetCanceled();
            }
            finally
            {
                transportA?.Dispose();
                transportB.Dispose();
                transportC.Dispose();
                transportD.Dispose();
            }
        }