/// <summary>
        /// Relays a transaction to the connected nodes.
        /// </summary>
        /// <param name="hash">Hash of the transaction.</param>
        public Task RelayTransaction(uint256 hash)
        {
            this.logger.LogTrace("({0}:'{1}')", nameof(hash), hash);
            IReadOnlyNodesCollection nodes = this.connectionManager.ConnectedNodes;

            if (!nodes.Any())
            {
                this.logger.LogTrace("(-)[NO_NODES]");
                return(Task.CompletedTask);
            }

            // find all behaviours then start an exclusive task
            // to add the hash to each local collection
            IEnumerable <MempoolBehavior> behaviours = nodes.Select(s => s.Behavior <MempoolBehavior>());

            this.logger.LogTrace("(-)");
            return(this.manager.MempoolLock.WriteAsync(() =>
            {
                foreach (MempoolBehavior mempoolBehavior in behaviours)
                {
                    if (mempoolBehavior?.AttachedNode.PeerVersion.Relay ?? false)
                    {
                        if (!mempoolBehavior.filterInventoryKnown.ContainsKey(hash))
                        {
                            mempoolBehavior.inventoryTxToSend.TryAdd(hash, hash);
                        }
                    }
                }
            }));
        }
Ejemplo n.º 2
0
        protected BlockPuller(ConcurrentChain chain, IReadOnlyNodesCollection nodes, ProtocolVersion protocolVersion)
        {
            this.Chain                   = chain;
            this.Nodes                   = nodes;
            this.DownloadedBlocks        = new ConcurrentDictionary <uint256, DownloadedBlock>();
            this.pendingInventoryVectors = new ConcurrentBag <uint256>();
            this.map = new ConcurrentDictionary <uint256, BlockPullerBehavior>();

            // set the default requirements
            this.requirements = new NodeRequirement
            {
                MinVersion       = protocolVersion,
                RequiredServices = NodeServices.Network
            };
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the object having a chain of block headers and a list of available nodes.
        /// </summary>
        /// <param name="chain">Chain of block headers.</param>
        /// <param name="nodes">Network peers of the node.</param>
        /// <param name="protocolVersion">Version of the protocol that the node supports.</param>
        /// <param name="loggerFactory">Factory to be used to create logger for the puller.</param>
        protected BlockPuller(ConcurrentChain chain, IReadOnlyNodesCollection nodes, ProtocolVersion protocolVersion, ILoggerFactory loggerFactory)
        {
            this.Chain                   = chain;
            this.Nodes                   = nodes;
            this.logger                  = loggerFactory.CreateLogger(this.GetType().FullName);
            this.downloadedBlocks        = new Dictionary <uint256, DownloadedBlock>();
            this.pendingInventoryVectors = new Queue <uint256>();
            this.assignedBlockTasks      = new Dictionary <uint256, BlockPullerBehavior>();
            this.peerQuality             = new QualityScore(QualityScoreHistoryLength, loggerFactory);

            // Set the default requirements.
            this.requirements = new NodeRequirement
            {
                MinVersion       = protocolVersion,
                RequiredServices = NodeServices.Network
            };
        }