Example #1
0
        private void respondPeerDiscovery(NetworkPacket packet)
        {
            PeerDiscoveryMsg request = new PeerDiscoveryMsg();
            request.Deserialize(packet.Data);

            // process incoming peer list
            processNewPeerList(request.knownPeers);

            // send message with own peer list
            Hash token = packet.Token;
            Hash peer = packet.PublicKeySource;
            PeerDiscoveryMsg response = new PeerDiscoveryMsg();
            response.knownPeers = KnownPeers;
            byte[] message = response.Serialize();
            NetworkPacket newpacket = new NetworkPacket(nodeConfig.PublicKey, PacketType.TPT_PEER_DISCOVERY_RESPONSE, message, token);
            networkPacketSwitch.AddToQueue(peer, newpacket);
        }
Example #2
0
        /// <summary>
        /// Initiate gossip-style peer-discovery protocol with a node
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void initiatePeerDiscovery(object sender, ElapsedEventArgs e)
        {
            int count = nodeState.ConnectedValidators.Count;
            if (count <= 0)
                return;
            int select = rng.Next(count);
            Print("init: selecting " + select + " of " + count);
            Hash peer = nodeState.ConnectedValidators.ToArray()[select];
            Hash token = TNetUtils.GenerateNewToken();

            //save locally
            requestRecipient = peer;
            requestToken = token;

            //send message
            PeerDiscoveryMsg request = new PeerDiscoveryMsg();
            request.knownPeers = KnownPeers;
            byte[] message = request.Serialize();
            NetworkPacket packet = new NetworkPacket(nodeConfig.PublicKey, PacketType.TPT_PEER_DISCOVERY_INIT, message, token);
            networkPacketSwitch.AddToQueue(peer, packet);
        }