<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using BitcoinUtilities.Collections;
using BitcoinUtilities.P2P;
using BitcoinUtilities.P2P.Messages;
using BitcoinUtilities.P2P.Primitives;
using BitcoinUtilities.Storage;

namespace BitcoinUtilities.Node.Services
{
    public class BlockContentDownloadService : INodeService
    {
        private const int MaxRequiredBlocks = 1024;
        private const int MaxAdvertisedBlocksPerNode = 1024;
        private const int MaxConcurrentBlockRequestsPerNode = 50;
        private const int MaxConcurrentBlockRequestsPerBlock = 2;

        private readonly CancellationToken cancellationToken;

        private readonly object lockObject = new object();

        private readonly BitcoinNode node;

        private readonly Dictionary<BitcoinEndpoint, EndpointState> endpoints = new Dictionary<BitcoinEndpoint, EndpointState>();

        private readonly Dictionary<byte[], BlockState> requiredBlocks = new Dictionary<byte[], BlockState>(ByteArrayComparer.Instance);

        public BlockContentDownloadService(BitcoinNode node, CancellationToken cancellationToken)
        {
            this.node = node;
            this.cancellationToken = cancellationToken;

            UpdateRequiredBlocks();
        }

        public void Run()
        {
            while (!cancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)))
            {
                //todo: resend timeouted request
            }
        }

        public void OnNodeConnected(BitcoinEndpoint endpoint)
        {
            BlockLocator locator = null;

            lock (lockObject)
            {
                endpoints.Add(endpoint, new EndpointState(endpoint));
                if (requiredBlocks.Any())
                {
                    int minRequiredBlockHeight = requiredBlocks.Values.Min(b => b.Height);
                    List<StoredBlock> parentBlocks = node.Blockchain.GetBlocksByHeight(BlockLocator.GetRequiredBlockHeights(minRequiredBlockHeight));
                    locator = new BlockLocator();
                    foreach (StoredBlock block in parentBlocks)
                    {
                        locator.AddHash(block.Height, block.Hash);
                    }
                }
            }
<commit_msg>Fixed bug with first requested block in BlockContentDownloadService.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using BitcoinUtilities.Collections;
using BitcoinUtilities.P2P;
using BitcoinUtilities.P2P.Messages;
using BitcoinUtilities.P2P.Primitives;
using BitcoinUtilities.Storage;

namespace BitcoinUtilities.Node.Services
{
    public class BlockContentDownloadService : INodeService
    {
        private const int MaxRequiredBlocks = 1024;
        private const int MaxAdvertisedBlocksPerNode = 1024;
        private const int MaxConcurrentBlockRequestsPerNode = 50;
        private const int MaxConcurrentBlockRequestsPerBlock = 2;

        private readonly CancellationToken cancellationToken;

        private readonly object lockObject = new object();

        private readonly BitcoinNode node;

        private readonly Dictionary<BitcoinEndpoint, EndpointState> endpoints = new Dictionary<BitcoinEndpoint, EndpointState>();

        private readonly Dictionary<byte[], BlockState> requiredBlocks = new Dictionary<byte[], BlockState>(ByteArrayComparer.Instance);

        public BlockContentDownloadService(BitcoinNode node, CancellationToken cancellationToken)
        {
            this.node = node;
            this.cancellationToken = cancellationToken;

            UpdateRequiredBlocks();
        }

        public void Run()
        {
            while (!cancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)))
            {
                //todo: resend timeouted request
            }
        }

        public void OnNodeConnected(BitcoinEndpoint endpoint)
        {
            BlockLocator locator = null;

            lock (lockObject)
            {
                endpoints.Add(endpoint, new EndpointState(endpoint));
                if (requiredBlocks.Any())
                {
                    int minRequiredBlockHeight = requiredBlocks.Values.Min(b => b.Height);
                    int locatorHeight = minRequiredBlockHeight - 1;
                    List<StoredBlock> parentBlocks = node.Blockchain.GetBlocksByHeight(BlockLocator.GetRequiredBlockHeights(locatorHeight));
                    locator = new BlockLocator();
                    foreach (StoredBlock block in parentBlocks)
                    {
                        locator.AddHash(block.Height, block.Hash);
                    }
                }
            }

{'fraction_non_alphanumeric': 0.06776094276094276, 'fraction_numerical': 0.006944444444444444, 'mean_word_length': 2.380512091038407, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2767303', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1093, 'n_words': 259}