예제 #1
0
        private async Task ConnectAndHandshake(Peer peer, bool isIncoming)
        {
            // connect
            await peer.ConnectAsync();

            if (!peer.IsConnected)
            {
                throw new Exception();
            }

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

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

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

            // send our local version
            var nodeId = random.NextUInt64(); //TODO should be generated and verified on version message

            var currentHeight = this.coreDaemon.CurrentChain.Height;
            await peer.Sender.SendVersion(Messaging.GetExternalIPEndPoint(), peer.RemoteEndPoint, nodeId, (UInt32)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 = NodeEncoder.DecodeVersionPayload(versionMessage.Payload.ToArray(), 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
                )
                                        );

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

            if (isIncoming)
            {
                Interlocked.Increment(ref this.incomingCount);
            }

            this.pendingPeers.TryRemove(peer);
            this.connectedPeers.TryAdd(peer);

            peer.OnDisconnect += DisconnectPeer;
            RaisePeerConnected(peer);
        }
예제 #2
0
        private Message WireDecodeMessage(UInt32 magic, Stream stream)
        {
            byte[]  payload;
            Message message;

            using (var reader = new BinaryReader(stream, Encoding.ASCII, leaveOpen: true))
            {
                var command         = reader.ReadFixedString(12);
                var payloadSize     = reader.ReadUInt32();
                var payloadChecksum = reader.ReadUInt32();

                payload = reader.ReadBytes(payloadSize.ToIntChecked());

                if (!Messaging.VerifyPayloadChecksum(payloadChecksum, payload))
                {
                    throw new Exception(string.Format("Checksum failed for {0}", command));
                }

                message = new Message
                          (
                    Magic: magic,
                    Command: command,
                    PayloadSize: payloadSize,
                    PayloadChecksum: payloadChecksum,
                    Payload: payload.ToImmutableArray()
                          );
            }

            switch (message.Command)
            {
            case "addr":
            {
                var addressPayload = NodeEncoder.DecodeAddressPayload(payload);

                var handler = this.OnReceivedAddresses;
                if (handler != null)
                {
                    handler(addressPayload.NetworkAddresses);
                }
            }
            break;

            case "alert":
            {
                var alertPayload = NodeEncoder.DecodeAlertPayload(payload);
            }
            break;

            case "block":
            {
                var block = DataEncoder.DecodeBlock(payload);

                var handler = this.OnBlock;
                if (handler != null)
                {
                    handler(this.owner, block);
                }
            }
            break;

            case "getblocks":
            {
                var getBlocksPayload = NodeEncoder.DecodeGetBlocksPayload(payload);

                var handler = this.OnGetBlocks;
                if (handler != null)
                {
                    handler(getBlocksPayload);
                }
            }
            break;

            case "getheaders":
            {
                var getHeadersPayload = NodeEncoder.DecodeGetBlocksPayload(payload);

                var handler = this.OnGetHeaders;
                if (handler != null)
                {
                    handler(getHeadersPayload);
                }
            }
            break;

            case "getdata":
            {
                var invPayload = NodeEncoder.DecodeInventoryPayload(payload);

                var handler = this.OnGetData;
                if (handler != null)
                {
                    handler(invPayload);
                }
            }
            break;

            case "headers":
            {
                var blockHeaders = ImmutableList.CreateBuilder <BlockHeader>();

                using (var headerStream = new MemoryStream(payload))
                    using (var reader = new BinaryReader(headerStream))
                    {
                        var headerCount = reader.ReadVarInt().ToIntChecked();

                        for (var i = 0; i < headerCount; i++)
                        {
                            var blockHeader = DataEncoder.DecodeBlockHeader(headerStream);
                            //TODO wiki says this is a byte and a var int, which is it?
                            var txCount = reader.ReadVarInt();

                            blockHeaders.Add(blockHeader);
                        }
                    }

                var handler = this.OnBlockHeaders;
                if (handler != null)
                {
                    handler(this.owner, blockHeaders.ToImmutable());
                }
            }
            break;

            case "inv":
            {
                var invPayload = NodeEncoder.DecodeInventoryPayload(payload);

                var handler = this.OnInventoryVectors;
                if (handler != null)
                {
                    handler(invPayload.InventoryVectors);
                }
            }
            break;

            case "notfound":
            {
                var invPayload = NodeEncoder.DecodeInventoryPayload(payload);

                var handler = this.OnNotFound;
                if (handler != null)
                {
                    handler(invPayload.InventoryVectors);
                }
            }
            break;

            case "ping":
            {
                var handler = this.OnPing;
                if (handler != null)
                {
                    handler(payload.ToImmutableArray());
                }
            }
            break;

            case "tx":
            {
                var tx = DataEncoder.DecodeTransaction(payload);

                var handler = this.OnTransaction;
                if (handler != null)
                {
                    handler(tx);
                }
            }
            break;

            case "version":
            {
                var versionPayload = NodeEncoder.DecodeVersionPayload(payload, payload.Length);

                var handler = this.OnVersion;
                if (handler != null)
                {
                    handler(versionPayload);
                }
            }
            break;

            case "verack":
            {
                var handler = this.OnVersionAcknowledged;
                if (handler != null)
                {
                    handler();
                }
            }
            break;

            default:
            {
                this.logger.Warn("Unhandled incoming message: {0}".Format2(message.Command));
            }
            break;
            }

            //TODO
            //if (payloadStream.Position != payloadStream.Length)
            //{
            //    var exMessage = string.Format("Wrong number of bytes read for {0}, parser error: read {1} bytes from a {2} byte payload", message.Command, payloadStream.Position, payloadStream.Length);
            //    Debug.WriteLine(exMessage);
            //    throw new Exception(exMessage);
            //}

            return(message);
        }
예제 #3
0
        public void TestWireDecodeVersionPayloadWithRelay()
        {
            var actual = NodeEncoder.EncodeVersionPayload(NodeEncoder.DecodeVersionPayload(VERSION_PAYLOAD_2_RELAY_BYTES.ToArray(), VERSION_PAYLOAD_2_RELAY_BYTES.Length), withRelay: true);

            CollectionAssert.AreEqual(VERSION_PAYLOAD_2_RELAY_BYTES.ToList(), actual.ToList());
        }
예제 #4
0
        public void TestWireDecodeVersionPayloadWithoutRelay()
        {
            var actual = NodeEncoder.EncodeVersionPayload(NodeEncoder.DecodeVersionPayload(VERSION_PAYLOAD_1_NO_RELAY_BYTES.ToArray(), VERSION_PAYLOAD_1_NO_RELAY_BYTES.Count), withRelay: false);

            CollectionAssert.AreEqual(VERSION_PAYLOAD_1_NO_RELAY_BYTES.ToList(), actual.ToList());
        }