/// <summary> /// </summary> /// <param name="getPeerReputationRequest"></param> /// <param name="channelHandlerContext"></param> /// <param name="senderPeerId"></param> /// <param name="correlationId"></param> /// <returns></returns> protected override GetPeerReputationResponse HandleRequest(GetPeerReputationRequest getPeerReputationRequest, IChannelHandlerContext channelHandlerContext, PeerId senderPeerId, ICorrelationId correlationId) { Guard.Argument(getPeerReputationRequest, nameof(getPeerReputationRequest)).NotNull(); Guard.Argument(channelHandlerContext, nameof(channelHandlerContext)).NotNull(); Guard.Argument(senderPeerId, nameof(senderPeerId)).NotNull(); Logger.Debug("received message of type PeerReputationRequest"); return(new GetPeerReputationResponse { Reputation = _peerRepository.GetAll().Where(m => m.PeerId.Ip == getPeerReputationRequest.Ip && m.PeerId.PublicKey.KeyToString() == getPeerReputationRequest.PublicKey.KeyToString()) .Select(x => x.Reputation).DefaultIfEmpty(int.MinValue).First() }); }
/// <summary> /// /// </summary> /// <param name="setPeerBlackListRequest"></param> /// <param name="channelHandlerContext"></param> /// <param name="senderPeerId"></param> /// <param name="correlationId"></param> /// <returns></returns> protected override SetPeerBlacklistResponse HandleRequest(SetPeerBlacklistRequest setPeerBlackListRequest, IChannelHandlerContext channelHandlerContext, PeerId senderPeerId, ICorrelationId correlationId) { Guard.Argument(setPeerBlackListRequest, nameof(setPeerBlackListRequest)).NotNull(); Guard.Argument(channelHandlerContext, nameof(channelHandlerContext)).NotNull(); Guard.Argument(senderPeerId, nameof(senderPeerId)).NotNull(); Logger.Information("received message of type PeerBlackListingRequest"); var peerItem = _peerRepository.GetAll() .FirstOrDefault(m => m.PeerId.Ip == setPeerBlackListRequest.Ip && m.PeerId.PublicKey.KeyToString() == setPeerBlackListRequest.PublicKey.KeyToString()); return(peerItem == null ? ReturnResponse(false, ByteString.Empty, ByteString.Empty) : ReturnResponse(setPeerBlackListRequest.Blacklist, setPeerBlackListRequest.PublicKey, setPeerBlackListRequest.Ip)); }
/// <summary> /// </summary> /// <param name="getPeerListRequest"></param> /// <param name="channelHandlerContext"></param> /// <param name="senderPeerId"></param> /// <param name="correlationId"></param> /// <returns></returns> protected override GetPeerListResponse HandleRequest(GetPeerListRequest getPeerListRequest, IChannelHandlerContext channelHandlerContext, PeerId senderPeerId, ICorrelationId correlationId) { Guard.Argument(getPeerListRequest, nameof(getPeerListRequest)).NotNull(); Guard.Argument(channelHandlerContext, nameof(channelHandlerContext)).NotNull(); Guard.Argument(senderPeerId, nameof(senderPeerId)).NotNull(); Logger.Debug("received message of type PeerListRequest"); var peers = _peerRepository.GetAll().Select(x => x.PeerId); var response = new GetPeerListResponse(); response.Peers.AddRange(peers); return(response); }
public PeerBlackListingRequestObserverTests() { _logger = Substitute.For <ILogger>(); _fakeContext = Substitute.For <IChannelHandlerContext>(); var fakeChannel = Substitute.For <IChannel>(); _fakeContext.Channel.Returns(fakeChannel); _fakeContext.Channel.RemoteAddress.Returns(EndpointBuilder.BuildNewEndPoint("192.0.0.1", 42042)); _testScheduler = new TestScheduler(); _peerRepository = Substitute.For <IPeerRepository>(); var fakePeers = PreparePeerRepositoryContent(); _peerRepository.GetAll().Returns(fakePeers); _senderId = PeerIdHelper.GetPeerId("sender"); }
private void CheckHeartbeat() { foreach (var peer in _peerRepository.GetAll()) { Task.Run(async() => { var result = await _peerChallengeRequest.ChallengePeerAsync(peer.PeerId); var counterValue = _nonResponsivePeerMap.GetOrAdd(peer.DocumentId, 0); _logger.Verbose( $"Heartbeat result: {result.ToString()} Peer: {peer.PeerId} Non-Responsive Counter: {counterValue}"); if (!result) { // @TODO touch last seen on peer _nonResponsivePeerMap[peer.DocumentId] += 1; counterValue += 1; if (counterValue >= _maxNonResponsiveCounter) { // Remove all non POA nodes at the moment until node is using same p2p discovery if (!peer.IsPoaNode) { _peerRepository.Delete(peer.DocumentId); _nonResponsivePeerMap.TryRemove(peer.DocumentId, out _); } _logger.Verbose( $"Peer reached maximum non-responsive count: {peer.PeerId}. Evicted from repository"); } } else { peer.Touch(); _peerRepository.Update(peer); _nonResponsivePeerMap[peer.DocumentId] = 0; } }).ConfigureAwait(false); } }
public PoaTestNode(string name, IPrivateKey privateKey, IPeerSettings nodeSettings, IEnumerable <PeerId> knownPeerIds, IFileSystem parentTestFileSystem, ITestOutputHelper output) { Name = name; _nodeSettings = nodeSettings; _nodeDirectory = parentTestFileSystem.GetCatalystDataDir().SubDirectoryInfo(Name); var nodeFileSystem = Substitute.ForPartsOf <FileSystem>(); nodeFileSystem.GetCatalystDataDir().Returns(_nodeDirectory); _rpcSettings = RpcSettingsHelper.GetRpcServerSettings(nodeSettings.Port + 100); _nodePeerId = nodeSettings.PeerId; var baseDfsFolder = Path.Combine(parentTestFileSystem.GetCatalystDataDir().FullName, "dfs"); var hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("blake2b-256")); _dfs = new DevDfs(parentTestFileSystem, hashProvider, baseDfsFolder); _memPool = new Mempool(new TestMempoolRepository(new InMemoryRepository <TransactionBroadcastDao, string>())); _peerRepository = Substitute.For <IPeerRepository>(); var peersInRepo = knownPeerIds.Select(p => new Peer { PeerId = p }).ToList(); _peerRepository.AsQueryable().Returns(peersInRepo.AsQueryable()); _peerRepository.GetAll().Returns(peersInRepo); _peerRepository.Get(Arg.Any <string>()).Returns(ci => { return(peersInRepo.First(p => p.DocumentId.Equals((string)ci[0]))); }); _containerProvider = new ContainerProvider(new[] { Constants.NetworkConfigFile(NetworkType.Devnet), Constants.SerilogJsonConfigFile } .Select(f => Path.Combine(Constants.ConfigSubFolder, f)), parentTestFileSystem, output); Program.RegisterNodeDependencies(_containerProvider.ContainerBuilder, excludedModules: new List <Type> { typeof(ApiModule), typeof(RpcServerModule) } ); _containerProvider.ConfigureContainerBuilder(true, true); OverrideContainerBuilderRegistrations(); _scope = _containerProvider.Container.BeginLifetimeScope(Name); _node = _scope.Resolve <ICatalystNode>(); var keyStore = _scope.Resolve <IKeyStore>(); var keyRegistry = _scope.Resolve <IKeyRegistry>(); keyRegistry.AddItemToRegistry(KeyRegistryTypes.DefaultKey, privateKey); keyStore.KeyStoreEncryptAsync(privateKey, nodeSettings.NetworkType, KeyRegistryTypes.DefaultKey).ConfigureAwait(false).GetAwaiter() .GetResult(); }
private void SetRepoReturnValue(IEnumerable <Peer> list) { _subbedPeerRepository.GetAll().Returns(list); }
public bool ContainsPeerHistory() { return(_peerRepository.GetAll().Any()); }
public IActionResult Get() { IEnumerable <Peer> peer = _dataRepository.GetAll(); return(Ok(peer)); }