Exemple #1
0
        public static byte[] EncodeNetworkAddressWithTime(NetworkAddressWithTime networkAddress)
        {
            var stream = new MemoryStream();

            EncodeNetworkAddressWithTime(stream, networkAddress);
            return(stream.ToArray());
        }
Exemple #2
0
 public static void EncodeNetworkAddressWithTime(Stream stream, NetworkAddressWithTime networkAddress)
 {
     using (var writer = new BinaryWriter(stream, Encoding.ASCII, leaveOpen: true))
     {
         writer.Write4Bytes(networkAddress.Time);
         EncodeNetworkAddress(stream, networkAddress.NetworkAddress);
     }
 }
Exemple #3
0
 public static byte[] EncodeNetworkAddressWithTime(NetworkAddressWithTime networkAddressWithTime)
 {
     using (var stream = new MemoryStream())
         using (var writer = new BinaryWriter(stream))
         {
             EncodeNetworkAddressWithTime(writer, networkAddressWithTime);
             return(stream.ToArray());
         }
 }
Exemple #4
0
        private void HandlePeerHandshakeCompleted(Peer peer)
        {
            var remoteAddressWithTime = new NetworkAddressWithTime(DateTimeOffset.Now, peer.RemoteEndPoint.ToNetworkAddress(/*TODO*/ services: 0));

            this.networkPeerStorage[remoteAddressWithTime.NetworkAddress.GetKey()] = remoteAddressWithTime;

            WirePeerEvents(peer);

            this.statsWorker.NotifyWork();
            this.blockRequestWorker.NotifyWork();
        }
Exemple #5
0
 public static void EncodeNetworkAddressWithTime(BinaryWriter writer, NetworkAddressWithTime networkAddressWithTime)
 {
     writer.WriteUInt32((uint)networkAddressWithTime.Time.ToUnixTimeSeconds()); //TODO is time LE or BE on network messages?
     EncodeNetworkAddress(writer, networkAddressWithTime.NetworkAddress);
 }
Exemple #6
0
        private async Task<bool> ConnectAndHandshake(RemoteNode remoteNode, bool isIncoming)
        {
            // wire node
            WireNode(remoteNode);

            // connect
            await remoteNode.ConnectAsync();

            if (remoteNode.IsConnected)
            {
                //TODO
                RemoteNode ignore;
                this.pendingPeers.TryRemove(remoteNode.RemoteEndPoint, out ignore);
                this.connectedPeers.TryAdd(remoteNode.RemoteEndPoint, remoteNode);

                // setup task to wait for verack
                var verAckTask = remoteNode.Receiver.WaitForMessage(x => x.Command == "verack", HANDSHAKE_TIMEOUT_MS);

                // setup task to wait for version
                var versionTask = remoteNode.Receiver.WaitForMessage(x => x.Command == "version", HANDSHAKE_TIMEOUT_MS);

                // start listening for messages after tasks have been setup
                remoteNode.Receiver.Listen();

                // send our local version
                var nodeId = (((UInt64)random.Next()) << 32) + (UInt64)random.Next(); //TODO should be generated and verified on version message

                var currentBlockchainLocal = this.blockchainDaemon.CurrentBlockchain;
                var currentHeight = !currentBlockchainLocal.IsDefault ? (UInt32)currentBlockchainLocal.Height : 0;
                await remoteNode.Sender.SendVersion(Messaging.GetExternalIPEndPoint(), remoteNode.RemoteEndPoint, nodeId, currentHeight);

                // wait for our local version to be acknowledged by the remote peer
                // wait for remote peer to send their version
                await Task.WhenAll(verAckTask, versionTask);

                //TODO shouldn't have to decode again
                var versionMessage = versionTask.Result;
                var versionPayload = NetworkEncoder.DecodeVersionPayload(versionMessage.Payload.ToArray().ToMemoryStream(), versionMessage.Payload.Length);

                var remoteAddressWithTime = new NetworkAddressWithTime
                (
                    Time: DateTime.UtcNow.ToUnixTime(),
                    NetworkAddress: new NetworkAddress
                    (
                        Services: versionPayload.LocalAddress.Services,
                        IPv6Address: versionPayload.LocalAddress.IPv6Address,
                        Port: versionPayload.LocalAddress.Port
                    )
                );

                if (!isIncoming)
                    this.knownAddressCache.UpdateValue(remoteAddressWithTime.GetKey(), remoteAddressWithTime);

                // acknowledge their version
                await remoteNode.Sender.SendVersionAcknowledge();

                return true;
            }
            else
            {
                return false;
            }
        }
Exemple #7
0
 public static byte[] EncodeNetworkAddressWithTime(NetworkAddressWithTime networkAddress)
 {
     var stream = new MemoryStream();
     EncodeNetworkAddressWithTime(stream, networkAddress);
     return stream.ToArray();
 }
Exemple #8
0
 public static void EncodeNetworkAddressWithTime(Stream stream, NetworkAddressWithTime networkAddress)
 {
     using (var writer = new BinaryWriter(stream, Encoding.ASCII, leaveOpen: true))
     {
         writer.Write4Bytes(networkAddress.Time);
         EncodeNetworkAddress(stream, networkAddress.NetworkAddress);
     }
 }