public static void ToProtocolMessage_When_Processing_Request_Should_Generate_New_CorrelationId_If_Not_Specified() { var peerId = PeerIdHelper.GetPeerId("someone"); var request = new GetPeerCountRequest().ToProtocolMessage(peerId); request.CorrelationId.ToCorrelationId().Should().NotBe(default);
public HastingsDiscoveryTests() { _testScheduler = new TestScheduler(); _settings = PeerSettingsHelper.TestPeerSettings(); _ownNode = PeerIdHelper.GetPeerId("ownNode"); }
public HastingsMementoTests() { _peer = PeerIdHelper.GetPeerId("current_peer"); }
public void When_candidate_is_produced_by_unexpected_producer_should_log_and_return_without_hitting_the_cache() { var candidateFromUnknownProducer = DeltaHelper.GetCandidateDelta(_hashProvider, producerId: PeerIdHelper.GetPeerId("unknown_producer")); _voter = new DeltaVoter(_cache, _producersProvider, _peerSettings, _logger); _voter.OnNext(candidateFromUnknownProducer); _logger.Received(1).Error(Arg.Is <Exception>(e => e is KeyNotFoundException), Arg.Any <string>(), Arg.Any <CandidateDeltaBroadcast>()); _cache.DidNotReceiveWithAnyArgs().TryGetValue(Arg.Any <object>(), out Arg.Any <object>()); _cache.DidNotReceiveWithAnyArgs().CreateEntry(Arg.Any <object>()); }
public FavouritesComparisonData() { Add(null, null, true); Add(new FavouriteDeltaBroadcast(), new FavouriteDeltaBroadcast(), true); Add(null, new FavouriteDeltaBroadcast(), false); Add(new FavouriteDeltaBroadcast(), null, false); var voter1 = PeerIdHelper.GetPeerId("voter1"); var voter2 = PeerIdHelper.GetPeerId("voter2"); var producer1 = PeerIdHelper.GetPeerId("producer1"); var producer2 = PeerIdHelper.GetPeerId("producer2"); var hash1 = ByteUtil.GenerateRandomByteArray(32).ToByteString(); var hash2 = ByteUtil.GenerateRandomByteArray(32).ToByteString(); var previousHash1 = ByteUtil.GenerateRandomByteArray(32).ToByteString(); var previousHash2 = ByteUtil.GenerateRandomByteArray(32).ToByteString(); Add(new FavouriteDeltaBroadcast { Candidate = new CandidateDeltaBroadcast { Hash = hash1, ProducerId = producer1, PreviousDeltaDfsHash = previousHash1 }, VoterId = voter1 }, new FavouriteDeltaBroadcast { Candidate = new CandidateDeltaBroadcast { Hash = hash2, ProducerId = producer1, PreviousDeltaDfsHash = previousHash1 }, VoterId = voter1 }, false); Add(new FavouriteDeltaBroadcast { Candidate = new CandidateDeltaBroadcast { Hash = hash1, ProducerId = producer1, PreviousDeltaDfsHash = previousHash1 }, VoterId = voter1 }, new FavouriteDeltaBroadcast { Candidate = new CandidateDeltaBroadcast { Hash = hash1, ProducerId = producer1, PreviousDeltaDfsHash = previousHash1 }, VoterId = voter2 }, false); Add(new FavouriteDeltaBroadcast { Candidate = new CandidateDeltaBroadcast { Hash = hash1, ProducerId = producer1, PreviousDeltaDfsHash = previousHash1 }, VoterId = voter1 }, new FavouriteDeltaBroadcast { Candidate = new CandidateDeltaBroadcast { Hash = hash1, ProducerId = producer2, PreviousDeltaDfsHash = previousHash2 }, VoterId = voter1 }, true); }
public async Task Can_Process_PingRequest_Correctly() { var pingRequestMessage = new PingRequest(); var fakeContext = Substitute.For <IChannelHandlerContext>(); var channeledAny = new ObserverDto(fakeContext, pingRequestMessage.ToProtocolMessage(PeerIdHelper.GetPeerId(), CorrelationId.GenerateCorrelationId())); var observableStream = new[] { channeledAny }.ToObservable(_testScheduler); _pingRequestObserver.StartObserving(observableStream); _testScheduler.Start(); await fakeContext.Channel.ReceivedWithAnyArgs(1) .WriteAndFlushAsync(new PingResponse().ToProtocolMessage(PeerIdHelper.GetPeerId(), CorrelationId.GenerateCorrelationId())); _subbedLogger.ReceivedWithAnyArgs(1); }
public void Can_Invalidate_Untrusted_Peer() { _repositoryAuthenticationStrategy.Authenticate(PeerIdHelper.GetPeerId("NotTrusted")) .Should().BeFalse(); }
public void Init() { _cancellationToken = new CancellationToken(); _manualResetEventSlim = new ManualResetEventSlim(false); _testScheduler = new TestScheduler(); _hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256")); _peerSettings = Substitute.For <IPeerSettings>(); _peerSettings.PeerId.Returns(PeerIdHelper.GetPeerId()); _deltaDfsReader = Substitute.For <IDeltaDfsReader>(); _deltaDfsReader.TryReadDeltaFromDfs(Arg.Any <Cid>(), out Arg.Any <Delta>()).Returns(x => true); _deltaCache = Substitute.For <IDeltaCache>(); _deltaCache.GenesisHash.Returns("bafk2bzacecji5gcdd6lxsoazgnbg46c3vttjwwkptiw27enachziizhhkir2w".ToCid()); _ledger = Substitute.For <ILedger>(); _peerService = Substitute.For <IPeerService>(); _deltaHashProvider = new DeltaHashProvider(_deltaCache, Substitute.For <IDeltaIndexService>(), Substitute.For <ILogger>()); _deltaIndexService = new DeltaIndexService(new InMemoryRepository <DeltaIndexDao, string>()); _deltaIndexService.Add(new DeltaIndexDao { Cid = _hashProvider.ComputeUtf8MultiHash("0").ToCid(), Height = 0 }); _peerClient = Substitute.For <IPeerClient>(); ModifyPeerClient <LatestDeltaHashRequest>((request, senderPeerIdentifier) => { var deltaHeightResponse = new LatestDeltaHashResponse { DeltaIndex = new DeltaIndex { Cid = _hashProvider.ComputeUtf8MultiHash(_syncTestHeight.ToString()).ToCid().ToArray() .ToByteString(), Height = (uint)_syncTestHeight } }; _deltaHeightReplaySubject.OnNext(new ObserverDto(Substitute.For <IChannelHandlerContext>(), deltaHeightResponse.ToProtocolMessage(senderPeerIdentifier, CorrelationId.GenerateCorrelationId()))); }); ModifyPeerClient <DeltaHistoryRequest>((request, senderPeerIdentifier) => { var data = GenerateSampleData((int)request.Height, (int)request.Range, (int)_syncTestHeight); _deltaIndexService.Add(data.DeltaIndex.Select(x => DeltaIndexDao.ToDao <DeltaIndex>(x, _mapperProvider))); _deltaHistoryReplaySubject.OnNext(new ObserverDto(Substitute.For <IChannelHandlerContext>(), data .ToProtocolMessage(senderPeerIdentifier, CorrelationId.GenerateCorrelationId()))); }); _peerRepository = new PeerRepository(new InMemoryRepository <Peer, string>()); Enumerable.Repeat(new Peer { PeerId = PeerIdHelper.GetPeerId() }, 5).ToList().ForEach(_peerRepository.Add); _deltaHeightReplaySubject = new ReplaySubject <IObserverDto <ProtocolMessage> >(1); _deltaHistoryReplaySubject = new ReplaySubject <IObserverDto <ProtocolMessage> >(1); var mergeMessageStreams = _deltaHeightReplaySubject.AsObservable() .Merge(_deltaHistoryReplaySubject.AsObservable()); _peerService.MessageStream.Returns(mergeMessageStreams); _deltaHashProvider = Substitute.For <IDeltaHashProvider>(); _deltaHashProvider.TryUpdateLatestHash(Arg.Any <Cid>(), Arg.Any <Cid>()).Returns(true); _mapperProvider = new TestMapperProvider(); _userOutput = Substitute.For <IUserOutput>(); _deltaHeightWatcher = new DeltaHeightWatcher(_peerClient, _peerRepository, _peerService, minimumPeers: 0); var dfsService = Substitute.For <IDfsService>(); _peerSyncManager = new PeerSyncManager(_peerClient, _peerRepository, _peerService, _userOutput, _deltaHeightWatcher, Substitute.For <IDfsService>(), 0.7, 0); }