Esempio n. 1
0
        public void Generate()
        {
            var table = new RoutingTable(
                new Address(
                    new byte[]
            {
                0xaa, 0xba, 0xf4, 0x9a, 0x08, 0x49, 0xaf, 0xa2, 0x43, 0x0b, 0x8e, 0x2b,
                0xf7, 0xaf, 0x9c, 0x48, 0x05, 0xb7, 0x63, 0xb9,
            }));
            const int targetBucket = 5;
            int       count        = 0;
            PublicKey publicKey;

            do
            {
                count++;
                publicKey = new PrivateKey().PublicKey;
            }while (table.GetBucketIndexOf(publicKey.ToAddress()) != targetBucket);

            Log.Debug(
                "Found public key of bucket index {Index} in {Count} tries: {Key}",
                table.GetBucketIndexOf(publicKey.ToAddress()),
                count,
                ByteArrayToString(publicKey.Format(true)));
            Assert.Equal(targetBucket, table.GetBucketIndexOf(publicKey.ToAddress()));
        }
Esempio n. 2
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());
        }
Esempio n. 3
0
        public static PrivateKey GeneratePrivateKeyOfBucketIndex(Address tableAddress, int target)
        {
            var        table = new RoutingTable(tableAddress);
            PrivateKey privateKey;

            do
            {
                privateKey = new PrivateKey();
            }while (table.GetBucketIndexOf(privateKey.ToAddress()) != target);

            return(privateKey);
        }