public Task SendStatusAsync(string peerId, RpcDirection rpcDirection, PeeringStatus peeringStatus)
        {
            byte[]      peerUtf8 = Encoding.UTF8.GetBytes(peerId);
            Span <byte> encoded  = new byte[Ssz.Ssz.PeeringStatusLength];

            Ssz.Ssz.Encode(encoded, peeringStatus);

            if (_logger.IsDebug())
            {
                LogDebug.RpcSend(_logger, rpcDirection, nameof(MethodUtf8.Status), peerId, encoded.Length, null);
            }
            if (rpcDirection == RpcDirection.Request)
            {
                if (!_mothraLibp2p.SendRpcRequest(MethodUtf8.Status, peerUtf8, encoded))
                {
                    if (_logger.IsWarn())
                    {
                        Log.RpcRequestNotSentAsPeeeringNotStarted(_logger, nameof(MethodUtf8.Status), null);
                    }
                }
            }
            else
            {
                if (!_mothraLibp2p.SendRpcResponse(MethodUtf8.Status, peerUtf8, encoded))
                {
                    if (_logger.IsWarn())
                    {
                        Log.RpcResponseNotSentAsPeeeringNotStarted(_logger, nameof(MethodUtf8.Status), null);
                    }
                }
            }

            return(Task.CompletedTask);
        }
Example #2
0
        private void OnRpcReceived(ReadOnlySpan <byte> methodUtf8, int requestResponseFlag, ReadOnlySpan <byte> peerUtf8,
                                   ReadOnlySpan <byte> data)
        {
            Activity activity = new Activity("rpc-received");

            activity.Start();
            try
            {
                string       peerId       = Encoding.UTF8.GetString(peerUtf8);
                RpcDirection rpcDirection = requestResponseFlag == 0 ? RpcDirection.Request : RpcDirection.Response;

                // Even though the value '/eth2/beacon_chain/req/status/1/' is sent, when Mothra calls the received event it is 'HELLO'
                // if (methodUtf8.SequenceEqual(MethodUtf8.Status)
                //     || methodUtf8.SequenceEqual(MethodUtf8.StatusMothraAlternative))
                if (data.Length == Ssz.Ssz.PeeringStatusLength)
                {
                    if (_logger.IsDebug())
                    {
                        LogDebug.RpcReceived(_logger, rpcDirection, requestResponseFlag,
                                             Encoding.UTF8.GetString(methodUtf8),
                                             peerId, data.Length, nameof(MethodUtf8.Status), null);
                    }

                    PeeringStatus peeringStatus = Ssz.Ssz.DecodePeeringStatus(data);
                    RpcMessage <PeeringStatus> statusRpcMessage =
                        new RpcMessage <PeeringStatus>(peerId, rpcDirection, peeringStatus);
                    _rpcPeeringStatusProcessor.Enqueue(statusRpcMessage);
                }
                //else if (methodUtf8.SequenceEqual(MethodUtf8.BeaconBlocksByRange))
                else if (data.Length == Ssz.Ssz.BeaconBlocksByRangeLength ||
                         data.Length >= _minimumSignedBeaconBlockLength)
                {
                    if (_logger.IsDebug())
                    {
                        LogDebug.RpcReceived(_logger, rpcDirection, requestResponseFlag,
                                             Encoding.UTF8.GetString(methodUtf8),
                                             peerId, data.Length, nameof(MethodUtf8.BeaconBlocksByRange), null);
                    }

                    //if (rpcDirection == RpcDirection.Request)
                    if (data.Length == Ssz.Ssz.BeaconBlocksByRangeLength)
                    {
                        BeaconBlocksByRange beaconBlocksByRange     = Ssz.Ssz.DecodeBeaconBlocksByRange(data);
                        RpcMessage <BeaconBlocksByRange> rpcMessage =
                            new RpcMessage <BeaconBlocksByRange>(peerId, rpcDirection, beaconBlocksByRange);
                        _rpcBeaconBlocksByRangeProcessor.Enqueue(rpcMessage);
                    }
                    else
                    {
                        SignedBeaconBlock signedBeaconBlock = Ssz.Ssz.DecodeSignedBeaconBlock(data);
                        _signedBeaconBlockProcessor.Enqueue(signedBeaconBlock, peerId);
                    }
                }
                else
                {
                    // TODO: handle other RPC
                    if (_logger.IsWarn())
                    {
                        Log.UnknownRpcReceived(_logger, rpcDirection, requestResponseFlag,
                                               Encoding.UTF8.GetString(methodUtf8), peerId,
                                               data.Length, null);
                    }
                }
            }
            catch (Exception ex)
            {
                if (_logger.IsError())
                {
                    Log.RpcReceivedError(_logger, Encoding.UTF8.GetString(methodUtf8), ex.Message, ex);
                }
            }
            finally
            {
                activity.Stop();
            }
        }
Example #3
0
 public RpcMessage(string peerId, RpcDirection direction, T content)
 {
     PeerId    = peerId;
     Direction = direction;
     Content   = content;
 }