public async Task Can_Process_DeltaHeightRequest_Correctly()
        {
            var fakeContext = Substitute.For <IChannelHandlerContext>();
            var deltaHistoryRequestMessage = new DeltaHistoryRequest();

            var channeledAny = new ObserverDto(fakeContext,
                                               deltaHistoryRequestMessage.ToProtocolMessage(PeerIdHelper.GetPeerId(),
                                                                                            CorrelationId.GenerateCorrelationId()
                                                                                            )
                                               );

            var observableStream = new[] { channeledAny }.ToObservable(_testScheduler);

            _deltaHistoryRequestObserver.StartObserving(observableStream);
            _testScheduler.Start();

            var response = new DeltaHistoryResponse();
            var hp            = new HashProvider(HashingAlgorithm.GetAlgorithmMetadata("keccak-256"));
            var lastDeltaHash = hp.ComputeMultiHash(ByteUtil.GenerateRandomByteArray(32));

            for (uint x = 0; x < 10; x++)
            {
                var delta = new Delta
                {
                    PreviousDeltaDfsHash = lastDeltaHash.Digest.ToByteString()
                };

                var index = new DeltaIndex
                {
                    Height = 10,
                    Cid    = delta.ToByteString()
                };

                response.DeltaIndex.Add(index);
                lastDeltaHash = hp.ComputeMultiHash(ByteUtil.GenerateRandomByteArray(32));
            }

            await fakeContext.Channel.ReceivedWithAnyArgs(1)
            .WriteAndFlushAsync(response.ToProtocolMessage(PeerIdHelper.GetPeerId(), CorrelationId.GenerateCorrelationId())).ConfigureAwait(false);

            _subbedLogger.ReceivedWithAnyArgs(1);
        }
Exemplo n.º 2
0
        private void DeltaHistoryOnNext(DeltaHistoryResponse deltaHistoryResponse)
        {
            //First block is always previous block
            if (deltaHistoryResponse.DeltaIndex.Count <= 1)
            {
                return;
            }

            var deltaHistoryRanker = _deltaHistoryRanker;

            if (deltaHistoryRanker == null)
            {
                return;
            }

            var startHeight = (int)deltaHistoryResponse.DeltaIndex.FirstOrDefault()?.Height;

            if (startHeight == deltaHistoryRanker.Height)
            {
                deltaHistoryRanker.Add(deltaHistoryResponse.DeltaIndex);
            }
        }
Exemplo n.º 3
0
        private DeltaHistoryResponse GenerateSampleData(int height, int range, int maxHeight = -1)
        {
            var deltaHeightResponse = new DeltaHistoryResponse();
            var deltaIndexList      = new List <DeltaIndex>();
            var heightSum           = height + range;

            if (heightSum > maxHeight)
            {
                heightSum = maxHeight;
            }

            for (var i = height; i <= heightSum; i++)
            {
                deltaIndexList.Add(new DeltaIndex
                {
                    Cid    = ByteString.CopyFrom(_hashProvider.ComputeUtf8MultiHash(i.ToString()).ToCid().ToArray()),
                    Height = (uint)i
                });
            }

            deltaHeightResponse.DeltaIndex.Add(deltaIndexList);
            return(deltaHeightResponse);
        }