コード例 #1
0
        private async Task <byte[][]> SendRequest(GetNodeDataMessage message, CancellationToken token)
        {
            if (Logger.IsTrace)
            {
                Logger.Trace("Sending node fata request:");
                Logger.Trace($"Keys count: {message.Keys.Length}");
            }

            var request = new Request <GetNodeDataMessage, byte[][]>(message);

            _nodeDataRequests.Add(request, token);

            Send(request.Message);

            Task <byte[][]> task      = request.CompletionSource.Task;
            var             firstTask = await Task.WhenAny(task, Task.Delay(Timeouts.Eth, token));

            if (firstTask.IsCanceled)
            {
                token.ThrowIfCancellationRequested();
            }

            if (firstTask == task)
            {
                return(task.Result);
            }

            throw new TimeoutException($"{Session.RemoteNodeId} Request timeout in {nameof(GetNodeDataMessage)}");
        }
コード例 #2
0
        public override int MessageIdSpaceSize => 17; // magic number here following Go

        public override void HandleMessage(ZeroPacket message)
        {
            base.HandleMessage(message);
            int size = message.Content.ReadableBytes;

            switch (message.PacketType)
            {
            case Eth63MessageCode.GetReceipts:
                GetReceiptsMessage getReceiptsMessage = Deserialize <GetReceiptsMessage>(message.Content);
                ReportIn(getReceiptsMessage);
                Handle(getReceiptsMessage);
                break;

            case Eth63MessageCode.Receipts:
                ReceiptsMessage receiptsMessage = Deserialize <ReceiptsMessage>(message.Content);
                ReportIn(receiptsMessage);
                Handle(receiptsMessage, size);
                break;

            case Eth63MessageCode.GetNodeData:
                GetNodeDataMessage getNodeDataMessage = Deserialize <GetNodeDataMessage>(message.Content);
                ReportIn(getNodeDataMessage);
                Handle(getNodeDataMessage);
                break;

            case Eth63MessageCode.NodeData:
                NodeDataMessage nodeDataMessage = Deserialize <NodeDataMessage>(message.Content);
                ReportIn(nodeDataMessage);
                Handle(nodeDataMessage, size);
                break;
            }
        }
コード例 #3
0
        public override async Task <byte[][]> GetNodeData(Keccak[] keys, CancellationToken token)
        {
            var msg = new GetNodeDataMessage(keys);

            byte[][] receipts = await SendRequest(msg, token);

            return(receipts);
        }
コード例 #4
0
 private void Handle(GetNodeDataMessage msg)
 {
     byte[][] nodeData = SyncServer.GetNodeData(msg.Keys);
     Interlocked.Increment(ref _counter);
     if (Logger.IsTrace)
     {
         Logger.Trace($"OUT {_counter:D5} NodeData to {Node:s}");
     }
     Send(new NodeDataMessage(nodeData));
 }
コード例 #5
0
        protected NodeDataMessage FulfillNodeDataRequest(GetNodeDataMessage msg)
        {
            if (msg.Hashes.Count > 4096)
            {
                throw new EthSyncException("Incoming node data request for more than 4096 nodes");
            }

            byte[][] nodeData = SyncServer.GetNodeData(msg.Hashes);

            return(new NodeDataMessage(nodeData));
        }
コード例 #6
0
        public override async Task <byte[][]> GetNodeData(IList <Keccak> keys, CancellationToken token)
        {
            if (keys.Count == 0)
            {
                return(Array.Empty <byte[]>());
            }

            var msg = new GetNodeDataMessage(keys);

            byte[][] receipts = await SendRequest(msg, token);

            return(receipts);
        }
コード例 #7
0
        private void Handle(GetNodeDataMessage msg)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            byte[][] nodeData = SyncServer.GetNodeData(msg.Keys);
            Interlocked.Increment(ref Counter);
            Send(new NodeDataMessage(nodeData));
            stopwatch.Stop();
            if (Logger.IsTrace)
            {
                Logger.Trace($"OUT {Counter:D5} NodeData to {Node:c} in {stopwatch.Elapsed.TotalMilliseconds}ms");
            }
        }
コード例 #8
0
        private void Handle(GetNodeDataMessage msg)
        {
            Metrics.Eth63GetNodeDataReceived++;

            Stopwatch stopwatch = Stopwatch.StartNew();

            Send(FulfillNodeDataRequest(msg));
            stopwatch.Stop();
            if (Logger.IsTrace)
            {
                Logger.Trace($"OUT {Counter:D5} NodeData to {Node:c} in {stopwatch.Elapsed.TotalMilliseconds}ms");
            }
        }
コード例 #9
0
        public override async Task <byte[][]> GetNodeData(IList <Keccak> keys, CancellationToken token)
        {
            if (keys.Count == 0)
            {
                return(Array.Empty <byte[]>());
            }

            GetNodeDataMessage msg = new GetNodeDataMessage(keys);

            // if node data is a disposable pooled array wrapper here then we could save around 1.6% allocations
            // on a sample 3M blocks Goerli fast sync
            byte[][] nodeData = await SendRequest(msg, token);

            return(nodeData);
        }
コード例 #10
0
        private void Handle(GetNodeDataMessage msg)
        {
            Metrics.Eth63GetNodeDataReceived++;
            if (msg.Hashes.Count > 4096)
            {
                throw new EthSyncException("Incoming node data request for more than 4096 nodes");
            }

            Stopwatch stopwatch = Stopwatch.StartNew();

            byte[][] nodeData = SyncServer.GetNodeData(msg.Hashes);
            Send(new NodeDataMessage(nodeData));
            stopwatch.Stop();
            if (Logger.IsTrace)
            {
                Logger.Trace($"OUT {Counter:D5} NodeData to {Node:c} in {stopwatch.Elapsed.TotalMilliseconds}ms");
            }
        }
コード例 #11
0
        private void Handle(GetNodeDataMessage msg)
        {
            if (msg.Keys.Count > 4096)
            {
                throw new EthSynchronizationException("Incoming node data request for more than 4096 nodes");
            }

            Stopwatch stopwatch = Stopwatch.StartNew();

            byte[][] nodeData = SyncServer.GetNodeData(msg.Keys);
            Interlocked.Increment(ref Counter);
            Send(new NodeDataMessage(nodeData));
            stopwatch.Stop();
            if (Logger.IsTrace)
            {
                Logger.Trace($"OUT {Counter:D5} NodeData to {Node:c} in {stopwatch.Elapsed.TotalMilliseconds}ms");
            }
        }
コード例 #12
0
        private async Task <byte[][]> SendRequest(GetNodeDataMessage message, CancellationToken token)
        {
            if (Logger.IsTrace)
            {
                Logger.Trace("Sending node fata request:");
                Logger.Trace($"Keys count: {message.Keys.Count}");
            }

            var request = new Request <GetNodeDataMessage, byte[][]>(message);

            _nodeDataRequests.Add(request, token);

            var perfCalcId = _perfService.StartPerfCalc();

            Send(request.Message);
            Task <byte[][]> task = request.CompletionSource.Task;

            CancellationTokenSource delayCancellation     = new CancellationTokenSource();
            CancellationTokenSource compositeCancellation = CancellationTokenSource.CreateLinkedTokenSource(token, delayCancellation.Token);
            var firstTask = await Task.WhenAny(task, Task.Delay(Timeouts.Eth, compositeCancellation.Token));

            if (firstTask.IsCanceled)
            {
                token.ThrowIfCancellationRequested();
            }

            if (firstTask == task)
            {
                delayCancellation.Cancel();
                var latency = _perfService.EndPerfCalc(perfCalcId);
                if (latency.HasValue)
                {
                    // block headers here / ok
                    StatsManager.ReportLatencyCaptureEvent(Session.Node, NodeLatencyStatType.BlockHeaders, latency.Value);
                }

                return(task.Result);
            }

            StatsManager.ReportLatencyCaptureEvent(Session.Node, NodeLatencyStatType.BlockHeaders, (long)Timeouts.Eth.TotalMilliseconds);
            _perfService.EndPerfCalc(perfCalcId);
            throw new TimeoutException($"{Session} Request timeout in {nameof(GetNodeDataMessage)}");
        }
コード例 #13
0
        private async Task <byte[][]> SendRequest(GetNodeDataMessage message, CancellationToken token)
        {
            if (Logger.IsTrace)
            {
                Logger.Trace("Sending node fata request:");
                Logger.Trace($"Keys count: {message.Hashes.Count}");
            }

            Request <GetNodeDataMessage, byte[][]> request = new Request <GetNodeDataMessage, byte[][]>(message);

            request.StartMeasuringTime();
            _nodeDataRequests.Add(request, token);

            Send(request.Message);
            Task <byte[][]> task = request.CompletionSource.Task;

            using CancellationTokenSource delayCancellation = new CancellationTokenSource();
            using CancellationTokenSource compositeCancellation
                      = CancellationTokenSource.CreateLinkedTokenSource(token, delayCancellation.Token);
            Task firstTask = await Task.WhenAny(task, Task.Delay(Timeouts.Eth, compositeCancellation.Token));

            if (firstTask.IsCanceled)
            {
                token.ThrowIfCancellationRequested();
            }

            if (firstTask == task)
            {
                delayCancellation.Cancel();
                long elapsed             = request.FinishMeasuringTime();
                long bytesPerMillisecond = (long)((decimal)request.ResponseSize / Math.Max(1, elapsed));
                if (Logger.IsTrace)
                {
                    Logger.Trace($"{this} speed is {request.ResponseSize}/{elapsed} = {bytesPerMillisecond}");
                }
                StatsManager.ReportTransferSpeedEvent(Session.Node, TransferSpeedType.NodeData, bytesPerMillisecond);

                return(task.Result);
            }

            StatsManager.ReportTransferSpeedEvent(Session.Node, TransferSpeedType.NodeData, 0L);
            throw new TimeoutException($"{Session} Request timeout in {nameof(GetNodeDataMessage)}");
        }
コード例 #14
0
 private void Handle(GetNodeDataMessage msg)
 {
     byte[][] nodeData = SyncServer.GetNodeData(msg.Keys);
     Send(new NodeDataMessage(nodeData));
 }