public VerifyMessageRequestObserverTests()
        {
            _signingContext = new SigningContext
            {
                NetworkType   = NetworkType.Devnet,
                SignatureType = SignatureType.ProtocolRpc
            };

            _testPeerId = PeerIdHelper.GetPeerId("TestPeerIdentifier");

            var peerSettings = _testPeerId.ToSubstitutedPeerSettings();

            _keySigner = Substitute.For <FakeKeySigner>();
            _keySigner.CryptoContext.Returns(new FfiWrapper());

            var logger = Substitute.For <ILogger>();

            _fakeContext = Substitute.For <IChannelHandlerContext>();

            var fakeChannel = Substitute.For <IChannel>();

            _fakeContext.Channel.Returns(fakeChannel);
            _verifyMessageRequestObserver = new VerifyMessageRequestObserver(peerSettings, logger, _keySigner);

            _verifyMessageRequest = GetValidVerifyMessageRequest();
        }
Пример #2
0
        public DeltaBuilderTests()
        {
            _hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("blake2b-256"));

            _random = new Random();

            _randomFactory = Substitute.For <IDeterministicRandomFactory>();
            _randomFactory.GetDeterministicRandomFromSeed(Arg.Any <byte[]>())
            .Returns(ci => new IsaacRandom(((byte[])ci[0]).ToHex()));

            _producerId   = PeerIdHelper.GetPeerId("producer");
            _peerSettings = _producerId.ToSubstitutedPeerSettings();

            _previousDeltaHash = _hashProvider.ComputeUtf8MultiHash("previousDelta").CreateCid();
            _zeroCoinbaseEntry = new CoinbaseEntry
            {
                Amount            = UInt256.Zero.ToUint256ByteString(),
                ReceiverPublicKey = _producerId.PublicKey.ToByteString()
            };

            _logger = Substitute.For <ILogger>();

            _cache = Substitute.For <IDeltaCache>();

            _dateTimeProvider = new DateTimeProvider();
        }
Пример #3
0
        public void Init()
        {
            _hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));

            _random = new Random(1);

            _randomFactory = Substitute.For <IDeterministicRandomFactory>();
            _randomFactory.GetDeterministicRandomFromSeed(Arg.Any <byte[]>())
            .Returns(ci => new IsaacRandom(((byte[])ci[0]).ToHex()));

            _producerId   = PeerIdHelper.GetPeerId("producer");
            _peerSettings = _producerId.ToSubstitutedPeerSettings();

            _previousDeltaHash = _hashProvider.ComputeUtf8MultiHash("previousDelta").ToCid();
            _zeroCoinbaseEntry = new CoinbaseEntry
            {
                Amount            = UInt256.Zero.ToUint256ByteString(),
                ReceiverPublicKey = _producerId.PublicKey.ToByteString()
            };

            _logger = Substitute.For <ILogger>();

            _cache = Substitute.For <IDeltaCache>();

            Delta previousDelta = new Delta();

            previousDelta.StateRoot = ByteString.CopyFrom(Keccak.EmptyTreeHash.Bytes);
            _cache.TryGetOrAddConfirmedDelta(Arg.Any <Cid>(), out Arg.Any <Delta>()).Returns(x =>
            {
                x[1] = previousDelta;
                return(true);
            });

            _dateTimeProvider = new DateTimeProvider();

            IDb             codeDb       = new MemDb();
            ISnapshotableDb stateDb      = new StateDb();
            ISpecProvider   specProvider = new CatalystSpecProvider();

            _cryptoContext = new FfiWrapper();
            _stateProvider = new StateProvider(stateDb, codeDb, LimboLogs.Instance);
            IStorageProvider  storageProvider = new StorageProvider(stateDb, _stateProvider, LimboLogs.Instance);
            KatVirtualMachine virtualMachine  = new KatVirtualMachine(_stateProvider,
                                                                      storageProvider,
                                                                      new StateUpdateHashProvider(),
                                                                      specProvider,
                                                                      new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256")),
                                                                      new FfiWrapper(),
                                                                      LimboLogs.Instance);

            _deltaExecutor = new DeltaExecutor(specProvider,
                                               _stateProvider,
                                               storageProvider,
                                               virtualMachine,
                                               _cryptoContext,
                                               _logger);
        }
        public async Task Can_Process_GetNeighbourRequest_Correctly()
        {
            // mock a random set of peers
            var randomPeers = new List <Peer>
            {
                new Peer {
                    PeerId = PeerIdHelper.GetPeerId("peer1"), LastSeen = DateTime.Now
                },
                new Peer {
                    PeerId = PeerIdHelper.GetPeerId("peer2"), LastSeen = DateTime.Now
                },
                new Peer {
                    PeerId = PeerIdHelper.GetPeerId("peer3"), LastSeen = DateTime.Now
                },
                new Peer {
                    PeerId = PeerIdHelper.GetPeerId("peer4"), LastSeen = DateTime.Now
                },
                new Peer {
                    PeerId = PeerIdHelper.GetPeerId("peer5"), LastSeen = DateTime.Now
                },
                new Peer {
                    PeerId = PeerIdHelper.GetPeerId("peer6")
                }
            };

            // add them to the mocked repository, and set return expectation
            AddMockPeerToDbAndSetReturnExpectation(randomPeers, _subbedPeerRepository);

            var peerSettings            = _peerId.ToSubstitutedPeerSettings();
            var neighbourRequestHandler = new GetNeighbourRequestObserver(peerSettings,
                                                                          _subbedPeerRepository,
                                                                          _subbedLogger
                                                                          );

            var peerNeighbourRequestMessage = new PeerNeighborsRequest();

            var fakeContext  = Substitute.For <IChannelHandlerContext>();
            var channeledAny = new ObserverDto(fakeContext,
                                               peerNeighbourRequestMessage.ToProtocolMessage(PeerIdHelper.GetPeerId(),
                                                                                             CorrelationId.GenerateCorrelationId()));
            var observableStream = new[] { channeledAny }.ToObservable(_testScheduler);

            neighbourRequestHandler.StartObserving(observableStream);

            var peerNeighborsResponseMessage = new PeerNeighborsResponse();

            for (var i = 0; i < 5; i++)
            {
                peerNeighborsResponseMessage.Peers.Add(PeerIdHelper.GetPeerId());
            }

            _testScheduler.Start();

            await fakeContext.Channel.ReceivedWithAnyArgs(1)
            .WriteAndFlushAsync(
                peerNeighborsResponseMessage.ToProtocolMessage(_peerId, CorrelationId.GenerateCorrelationId()));
        }
Пример #5
0
        public void Init()
        {
            _hashProvider     = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));
            _broadcastManager = Substitute.For <IBroadcastManager>();
            var logger = Substitute.For <ILogger>();

            _peerId     = PeerIdHelper.GetPeerId("me");
            _dfsService = Substitute.For <IDfsService>();
            _hub        = new DeltaHubWithFastRetryPolicy(_broadcastManager, _peerId.ToSubstitutedPeerSettings(), _dfsService, _hashProvider, logger);
        }
Пример #6
0
        public DeltaHubTests()
        {
            _hashProvider     = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("blake2b-256"));
            _broadcastManager = Substitute.For <IBroadcastManager>();
            var logger = Substitute.For <ILogger>();

            _peerId = PeerIdHelper.GetPeerId("me");
            _dfs    = Substitute.For <IDfs>();
            _hub    = new DeltaHubWithFastRetryPolicy(_broadcastManager, _peerId.ToSubstitutedPeerSettings(), _dfs,
                                                      logger);
        }
Пример #7
0
        public BroadcastManagerTests()
        {
            _senderPeerId = PeerIdHelper.GetPeerId("sender");
            _keySigner    = Substitute.For <IKeySigner>();
            var fakeSignature = Substitute.For <ISignature>();

            _keySigner.Sign(Arg.Any <byte[]>(), default).ReturnsForAnyArgs(fakeSignature);
            _keySigner.CryptoContext.SignatureLength.Returns(64);
            _peers        = Substitute.For <IPeerRepository>();
            _cache        = new MemoryCache(new MemoryCacheOptions());
            _peerSettings = _senderPeerId.ToSubstitutedPeerSettings();
        }
Пример #8
0
        public AddFileToDfsRequestObserverTests()
        {
            _hashProvider     = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("blake2b-256"));
            _manualResetEvent = new ManualResetEvent(false);
            _senderIdentifier = PeerIdHelper.GetPeerId("sender");
            var peerSettings = _senderIdentifier.ToSubstitutedPeerSettings();

            _fakeDfs = Substitute.For <IDfs>();
            var logger = Substitute.For <ILogger>();

            _fakeContext                 = Substitute.For <IChannelHandlerContext>();
            _nodeFileTransferFactory     = Substitute.For <IDownloadFileTransferFactory>();
            _addFileToDfsRequestObserver = new AddFileToDfsRequestObserver(_fakeDfs,
                                                                           peerSettings,
                                                                           _nodeFileTransferFactory,
                                                                           logger);
        }
        public DeltaVoterTests()
        {
            _hashProvider = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("blake2b-256"));

            _cache = Substitute.For <IMemoryCache>();

            _previousDeltaHash = _hashProvider.ComputeMultiHash(ByteUtil.GenerateRandomByteArray(32));

            _producerIds = "1234"
                           .Select((c, i) => PeerIdHelper.GetPeerId(c.ToString()))
                           .Shuffle();
            _producersProvider = Substitute.For <IDeltaProducersProvider>();
            _producersProvider.GetDeltaProducersFromPreviousDelta(Arg.Any <MultiHash>())
            .Returns(_producerIds);

            _localIdentifier = PeerIdHelper.GetPeerId("myself, a producer");
            _peerSettings    = _localIdentifier.ToSubstitutedPeerSettings();
            _logger          = Substitute.For <ILogger>();
        }
        private SetPeerBlacklistResponse GetSetPeerBlacklistRequest(SetPeerBlacklistRequest request)
        {
            var protocolMessage = request.ToProtocolMessage(_senderId);
            var messageStream   = MessageStreamHelper.CreateStreamWithMessage(_fakeContext, _testScheduler, protocolMessage);

            var peerSettings = _senderId.ToSubstitutedPeerSettings();
            var handler      = new PeerBlackListingRequestObserver(peerSettings, _logger, _peerRepository);

            handler.StartObserving(messageStream);

            _testScheduler.Start();

            var receivedCalls = _fakeContext.Channel.ReceivedCalls().ToList();

            receivedCalls.Count.Should().Be(1);

            var sentResponseDto = (IMessageDto <ProtocolMessage>)receivedCalls.Single().GetArguments().Single();

            return(sentResponseDto.Content.FromProtocolMessage <SetPeerBlacklistResponse>());
        }