private static void FillBatchResponses(ReceiptsSyncBatch batch)
 {
     batch.Response = new TxReceipt[batch.Request.Length][];
     for (int i = 0; i < batch.Response.Length; i++)
     {
         batch.Response[i] = new[] { Build.A.Receipt.TestObject };
     }
 }
        public void Can_create_a_final_batch()
        {
            LoadScenario(_64BodiesWithOneTxEachFollowedByEmpty);
            ReceiptsSyncBatch request = _feed.PrepareRequest().Result;

            request.Should().NotBeNull();
            request !.MinNumber.Should().Be(1024);
            request.Prioritized.Should().Be(true);
        }
        public void Returns_same_batch_until_filled()
        {
            LoadScenario(_256BodiesWithOneTxEach);
            ReceiptsSyncBatch request = _feed.PrepareRequest().Result;

            _feed.HandleResponse(request);
            ReceiptsSyncBatch request2 = _feed.PrepareRequest().Result;

            request2.Should().Be(request);
        }
        public void Can_sync_final_batch()
        {
            LoadScenario(_64BodiesWithOneTxEach);
            ReceiptsSyncBatch batch = _feed.PrepareRequest().Result;

            FillBatchResponses(batch);
            _feed.HandleResponse(batch);
            _receiptStorage.LowestInsertedReceiptBlock.Returns(1);
            _feed.PrepareRequest().Result.Should().Be(null);

            _feed.CurrentState.Should().Be(SyncFeedState.Finished);
        }
        public void If_comes_back_empty_then_it_can_be_reused()
        {
            LoadScenario(_1024BodiesWithOneTxEach);
            ReceiptsSyncBatch          batch          = _feed.PrepareRequest().Result;
            SyncResponseHandlingResult handlingResult = _feed.HandleResponse(batch);

            handlingResult.Should().Be(SyncResponseHandlingResult.NotAssigned);
            ReceiptsSyncBatch batch2 = _feed.PrepareRequest().Result;

            batch2.Should().BeSameAs(batch);
            batch2.Retries.Should().Be(1);
        }
        public void When_configured_to_skip_receipts_then_finishes_immediately()
        {
            LoadScenario(_256BodiesWithOneTxEach);
            _syncConfig.DownloadReceiptsInFastSync = false;

            ReceiptsSyncBatch request = _feed.PrepareRequest().Result;

            request.Should().BeNull();
            _feed.CurrentState.Should().Be(SyncFeedState.Finished);
            _measuredProgress.HasEnded.Should().BeTrue();
            _measuredProgressQueue.HasEnded.Should().BeTrue();
        }
 private void PrepareReceiptsResponse(ReceiptsSyncBatch receiptSyncBatch, LatencySyncPeerMock syncPeer, IBlockTree tree)
 {
     receiptSyncBatch.Response = new TxReceipt[receiptSyncBatch.Request.Length][];
     for (int i = 0; i < receiptSyncBatch.Request.Length; i++)
     {
         Block block = tree.FindBlock(receiptSyncBatch.Request[i], false);
         receiptSyncBatch.Response[i] = new TxReceipt[block.Transactions.Length];
         for (int j = 0; j < block.Transactions.Length; j++)
         {
             receiptSyncBatch.Response[i][j] = _remoteReceiptStorage.Find(block.Transactions[j].Hash);
         }
     }
 }
        public void If_comes_back_filled_with_empty_responses_then_it_can_be_reused()
        {
            LoadScenario(_1024BodiesWithOneTxEach);
            ReceiptsSyncBatch batch = _feed.PrepareRequest().Result;

            batch.Response = new TxReceipt[batch.Request.Length][];
            SyncResponseHandlingResult handlingResult = _feed.HandleResponse(batch);

            handlingResult.Should().Be(SyncResponseHandlingResult.NoProgress);
            ReceiptsSyncBatch batch2 = _feed.PrepareRequest().Result;

            batch2.Should().BeSameAs(batch);
            batch2.Retries.Should().Be(1);
        }
        public void Can_have_many_pending()
        {
            LoadScenario(_1024BodiesWithOneTxEach);
            ReceiptsSyncBatch batch  = _feed.PrepareRequest().Result;
            ReceiptsSyncBatch batch2 = _feed.PrepareRequest().Result;
            ReceiptsSyncBatch batch3 = _feed.PrepareRequest().Result;

            _feed.HandleResponse(batch3);
            _feed.HandleResponse(batch);
            _feed.HandleResponse(batch2);

            _feed.PrepareRequest().Result.Should().BeSameAs(batch3);
            _feed.PrepareRequest().Result.Should().BeSameAs(batch);
            _feed.PrepareRequest().Result.Should().BeSameAs(batch2);
        }
        public void If_only_one_valid_item_comes_back_then_we_create_a_filler_batch()
        {
            LoadScenario(_1024BodiesWithOneTxEach);
            ReceiptsSyncBatch batch = _feed.PrepareRequest().Result;

            batch.Response    = new TxReceipt[batch.Request.Length][];
            batch.Response[0] = new [] { Build.A.Receipt.TestObject };

            SyncResponseHandlingResult handlingResult = _feed.HandleResponse(batch);

            handlingResult.Should().Be(SyncResponseHandlingResult.OK);

            ReceiptsSyncBatch batch2 = _feed.PrepareRequest().Result;

            batch2.Request.Length.Should().Be(batch.Request.Length - 1);
        }
        public void Can_create_a_smaller_request()
        {
            int expectedBatchSize = 64;

            LoadScenario(_64BodiesWithOneTxEach);
            ReceiptsSyncBatch request = _feed.PrepareRequest().Result;

            request.Should().NotBeNull();
            request.MinNumber.Should().Be(_pivotNumber - expectedBatchSize + 1);
            request.Blocks.Length.Should().Be(expectedBatchSize);
            request.Predecessors.Length.Should().Be(expectedBatchSize);
            request.Request.Length.Should().Be(expectedBatchSize);
            request.StartNumber.Should().Be(_pivotNumber - expectedBatchSize + 1);
            request.EndNumber.Should().Be(_pivotNumber);
            request.On.Should().Be(long.MaxValue);
            request.Description.Should().NotBeNull();
            request.Prioritized.Should().Be(true);
        }
        public void Can_have_many_dependent_batches()
        {
            LoadScenario(_1024BodiesWithOneTxEach);
            ReceiptsSyncBatch batch  = _feed.PrepareRequest().Result;
            ReceiptsSyncBatch batch2 = _feed.PrepareRequest().Result;
            ReceiptsSyncBatch batch3 = _feed.PrepareRequest().Result;

            FillBatchResponses(batch);
            FillBatchResponses(batch2);
            FillBatchResponses(batch3);

            _feed.HandleResponse(batch3).Should().Be(SyncResponseHandlingResult.OK);
            _feed.HandleResponse(batch2).Should().Be(SyncResponseHandlingResult.OK);
            _feed.HandleResponse(batch).Should().Be(SyncResponseHandlingResult.OK);
            _receiptStorage.LowestInsertedReceiptBlock.Returns(897);

            _feed.PrepareRequest();
            _receiptStorage.Received().Insert(Arg.Is <Block>(b => b.Number == 896), true, Arg.Any <TxReceipt[]>());
        }
        public void Can_create_non_geth_requests()
        {
            int expectedBatchSize = 256;

            _syncConfig.UseGethLimitsInFastBlocks = false;
            LoadScenario(_256BodiesWithOneTxEach, _syncConfig);
            ReceiptsSyncBatch request = _feed.PrepareRequest().Result;

            request.Should().NotBeNull();
            request.MinNumber.Should().Be(_pivotNumber - expectedBatchSize + 1);
            request.Blocks.Length.Should().Be(expectedBatchSize);
            request.Predecessors.Length.Should().Be(expectedBatchSize);
            request.Request.Length.Should().Be(expectedBatchSize);
            request.StartNumber.Should().Be(_pivotNumber - expectedBatchSize + 1);
            request.EndNumber.Should().Be(_pivotNumber);
            request.On.Should().Be(long.MaxValue);
            request.Description.Should().NotBeNull();
            request.Prioritized.Should().Be(true);
        }
Exemple #14
0
        private void PrepareReceiptsResponse(ReceiptsSyncBatch receiptSyncBatch, LatencySyncPeerMock syncPeer, IBlockTree tree)
        {
            receiptSyncBatch.Response = new TxReceipt[receiptSyncBatch.Request.Length][];
            for (int i = 0; i < receiptSyncBatch.Request.Length; i++)
            {
                Block block = tree.FindBlock(receiptSyncBatch.Request[i], BlockTreeLookupOptions.None);
                receiptSyncBatch.Response[i] = new TxReceipt[block.Transactions.Length];
                for (int j = 0; j < block.Transactions.Length; j++)
                {
                    receiptSyncBatch.Response[i][j] = _remoteReceiptStorage.Find(block.Transactions[j].Hash);

                    if (i < 10 && j == 0 && _maliciousByInvalidReceipts.Contains(syncPeer))
                    {
                        receiptSyncBatch.Response[i][j]                      = new TxReceipt();
                        receiptSyncBatch.Response[i][j].StatusCode           = (byte)(1 - receiptSyncBatch.Response[i][j].StatusCode);
                        receiptSyncBatch.Response[i][j].PostTransactionState = Keccak.Compute(receiptSyncBatch.Response[i][j].PostTransactionState?.Bytes ?? new byte[] { 1 });
                    }
                }
            }
        }
        public void If_receipts_root_comes_invalid_then_reports_breach_of_protocol()
        {
            LoadScenario(_1024BodiesWithOneTxEach);
            ReceiptsSyncBatch batch = _feed.PrepareRequest().Result;

            batch.Response = new TxReceipt[batch.Request.Length][];

            // default receipts that we use when constructing receipt root for tests have stats code 0
            // so by using 1 here we create a different tx root
            batch.Response[0] = new [] { Build.A.Receipt.WithStatusCode(1).TestObject };

            PeerInfo peerInfo = new PeerInfo(Substitute.For <ISyncPeer>());

            batch.ResponseSourcePeer = peerInfo;

            SyncResponseHandlingResult handlingResult = _feed.HandleResponse(batch);

            handlingResult.Should().Be(SyncResponseHandlingResult.NoProgress);

            _syncPeerPool.Received().ReportBreachOfProtocol(peerInfo, Arg.Any <string>());
        }