public async Task RequestBlockAsync_Failed() { _grpcPeer = CreateNewPeer("127.0.0.1:3000", false); _pool.AddPeer(_grpcPeer); var blockHeader = await _blockchainService.GetBestChainLastBlockHeaderAsync(); var block = await _grpcPeer.RequestBlockAsync(blockHeader.GetHash()); block.ShouldBeNull(); }
public GrpcPeerTests() { _blockchainService = GetRequiredService <IBlockchainService>(); _networkServer = GetRequiredService <IAElfNetworkServer>(); _pool = GetRequiredService <IPeerPool>(); _acc = GetRequiredService <IAccountService>(); _eventBus = GetRequiredService <ILocalEventBus>(); _osTestHelper = GetRequiredService <OSTestHelper>(); _grpcPeer = CreateNewPeer(); _pool.AddPeer(_grpcPeer); }
public async Task GetPeersTest() { var ipAddressOne = "192.168.1.1:1680"; var channelOne = new Channel(ipAddressOne, ChannelCredentials.Insecure); var peerOne = new GrpcPeer(channelOne, new PeerService.PeerServiceClient(channelOne), "0454dcd0afc20d015e328666d8d25f3f28b13ccd9744eb6b153e4a69709aab399", ipAddressOne); _peerPool.AddPeer(peerOne); var ipAddressTwo = "192.168.1.2:1680"; var channelTwo = new Channel(ipAddressTwo, ChannelCredentials.Insecure); var peerTwo = new GrpcPeer(channelTwo, new PeerService.PeerServiceClient(channelTwo), "0624dcd0afc20d015e328666d8d25f3f28b13ccd9744eb6b153e4a69709aab390", ipAddressTwo); _peerPool.AddPeer(peerTwo); var peers = await GetResponseAsObjectAsync <List <string> >("api/net/peers"); peers.Count.ShouldBe(2); peers.ShouldContain((peer) => peer.IsIn(ipAddressOne, ipAddressTwo)); }
protected void Page_Load(object sender, EventArgs e) { IServerResponse res = ServerResponseBase.GetServerResponseInstance(); try { IPeerRequest rq = PeerRequestBase.GetRequestInstance(Request.RawUrl); Trace.Write("Request Object = " + rq); IPeer peer = PeerBase.GetPeer(); peer.IP = (string.IsNullOrEmpty(rq.IP)) ? Request.UserHostAddress : rq.IP; peer.Port = rq.Port; peer.PeerID = rq.PeerID; IPeerPoolManager manager = PeerPoolManagerBase.GetPeerPoolManager(); IPeerPool pool = manager.GetPeerPoolByInfoHash(rq.InfoHash); pool.AddPeer(peer); res.Interval = Convert.ToInt32(ConfigurationManager.AppSettings["Interval"]); res.MinInterval = Convert.ToInt32(ConfigurationManager.AppSettings["MinInterval"]); foreach (IPeer p in pool.GetPeerList()) { if (p.IP == peer.IP && p.Port == peer.Port) { continue; // skip the request peer itself } res.Peers.Add(p); } Response.Clear(); byte[] binRes = res.GetBinaryResponse(); Trace.Write("binRes=" + HexEncoding.ToString(binRes)); Response.BinaryWrite(binRes); } catch (Exception Ex) { Trace.Write("ERROR: " + Ex); res.FailureReason = Ex.ToString(); Response.Clear(); Response.Write(res); } Response.End(); }
/// <summary> /// First step of the connect/auth process. Used to initiate a connection. The provided payload should be the /// clients authentication information. When receiving this call, protocol dictates you send the client your auth /// information. The response says whether or not you can connect. /// </summary> public override async Task <ConnectReply> Connect(Handshake handshake, ServerCallContext context) { Logger.LogTrace($"{context.Peer} has initiated a connection request."); if (handshake?.HskData == null) { return new ConnectReply { Err = AuthError.InvalidHandshake } } ; // verify chain id if (handshake.HskData.ChainId != _blockChainService.GetChainId()) { return new ConnectReply { Err = AuthError.ChainMismatch } } ; // verify protocol if (handshake.HskData.Version != KernelConstants.ProtocolVersion) { return new ConnectReply { Err = AuthError.ProtocolMismatch } } ; // verify signature var validData = await _accountService.VerifySignatureAsync(handshake.Sig.ToByteArray(), Hash.FromMessage(handshake.HskData).ToByteArray(), handshake.HskData.PublicKey.ToByteArray()); if (!validData) { return new ConnectReply { Err = AuthError.WrongSig } } ; var peer = GrpcUrl.Parse(context.Peer); if (peer == null) { return new ConnectReply { Err = AuthError.InvalidPeer } } ; var peerAddress = peer.IpAddress + ":" + handshake.HskData.ListeningPort; Logger.LogDebug($"Attempting to create channel to {peerAddress}"); Channel channel = new Channel(peerAddress, ChannelCredentials.Insecure); var client = new PeerService.PeerServiceClient(channel.Intercept(metadata => { metadata.Add(GrpcConsts.PubkeyMetadataKey, AsyncHelper.RunSync(() => _accountService.GetPublicKeyAsync()).ToHex()); return(metadata); })); if (channel.State != ChannelState.Ready) { var c = channel.WaitForStateChangedAsync(channel.State); } var pubKey = handshake.HskData.PublicKey.ToHex(); var grpcPeer = new GrpcPeer(channel, client, pubKey, peerAddress); // Verify auth bool valid = _peerPool.IsAuthenticatePeer(pubKey); if (!valid) { await channel.ShutdownAsync(); Logger.LogDebug($"Failed to reach {grpcPeer}"); return(new ConnectReply { Err = AuthError.WrongAuth }); } // send our credentials var hsk = await _peerPool.GetHandshakeAsync(); // If auth ok -> add it to our peers _peerPool.AddPeer(grpcPeer); return(new ConnectReply { Handshake = hsk }); }