コード例 #1
0
ファイル: Program.cs プロジェクト: nistec/MQueue
        static void rq_MessageReceived(object sender, QueueItemEventArgs e)
        {
            IAsyncQueue rq = (IAsyncQueue)sender;

            rq.Completed(e.Item.ItemId, (int)ItemState.Commit, e.Item.HasAttach);
            Console.WriteLine("MessageReceived: " + e.Item.ItemId);
        }
コード例 #2
0
        public BlockStoreSignaled(
            IBlockStoreQueue blockStoreQueue,
            StoreSettings storeSettings,
            IChainState chainState,
            IConnectionManager connection,
            INodeLifetime nodeLifetime,
            ILoggerFactory loggerFactory,
            IInitialBlockDownloadState initialBlockDownloadState,
            ISignals signals,
            IAsyncProvider asyncProvider)
        {
            this.blockStoreQueue           = blockStoreQueue;
            this.chainState                = chainState;
            this.connection                = connection;
            this.nodeLifetime              = nodeLifetime;
            this.logger                    = loggerFactory.CreateLogger(GetType().FullName);
            this.storeSettings             = storeSettings;
            this.initialBlockDownloadState = initialBlockDownloadState;
            this.signals                   = signals;
            this.asyncProvider             = asyncProvider;

            this.blocksToAnnounce = asyncProvider.CreateAsyncQueue <ChainedHeader>();
            this.dequeueLoopTask  = DequeueContinuouslyAsync();

            this.asyncProvider.RegisterTask($"{nameof(BlockStoreSignaled)}.{nameof(this.dequeueLoopTask)}",
                                            this.dequeueLoopTask);
        }
コード例 #3
0
 /// <summary>
 /// 清空队列。
 /// </summary>
 public static async Task Clear <T>(this IAsyncQueue <T> aq, TimeSpan timeout)
 {
     using (var source = new CancellationTokenSource(timeout))
     {
         await aq.Clear(source.Token);
     }
 }
コード例 #4
0
 /// <summary>
 /// 移除并返回位于队列开始处的对象。
 /// </summary>
 public static async Task <T> Dequeue <T>(this IAsyncQueue <T> aq, TimeSpan timeout)
 {
     using (var source = new CancellationTokenSource(timeout))
     {
         return(await aq.Dequeue(source.Token));
     }
 }
コード例 #5
0
ファイル: Program.cs プロジェクト: nistec/MQueue
        static void rq_MessageArraived(object sender, QueueItemEventArgs e)
        {
            IAsyncQueue rq   = (IAsyncQueue)sender;
            IQueueItem  item = rq.Dequeue();

            Console.WriteLine("MessageArraived: " + item.ItemId);
        }
コード例 #6
0
 /// <summary>
 /// 清空队列。
 /// </summary>
 public static async Task Clear <T>(this IAsyncQueue <T> aq, int millisecondsTimeout)
 {
     using (var source = new CancellationTokenSource(millisecondsTimeout))
     {
         await aq.Clear(source.Token);
     }
 }
コード例 #7
0
 public ILargeMessageQueue <T> Create <T>(
     IAsyncQueue <LargeMessageReference> referenceQueue,
     IAsyncBlockBlobRepository blobRepository,
     ILargeMessageQueueErrorHandler errorHandler) where T : class
 {
     return(new LargeMessageQueue <T>(_serializer, referenceQueue, blobRepository, _loggerFactory.CreateLogger <LargeMessageQueue <T> >(), errorHandler));
 }
コード例 #8
0
ファイル: AsyncProviderTest.cs プロジェクト: georgepinca/src
        /// <summary>
        /// Worker of <see cref="AsyncQueue_DequeueParallelAsync"/> test that tries to consume items from the queue
        /// until the last item is reached or cancellation is triggered.
        /// </summary>
        /// <param name="asyncQueue">Queue to consume items from.</param>
        /// <param name="list">List to add consumed items to.</param>
        /// <param name="lastItem">Value of the last item that will be added to the queue.</param>
        /// <param name="cts">Cancellation source to cancel when we are done.</param>
        private async Task AsyncQueue_DequeueParallelAsync_WorkerAsync(IAsyncQueue <int> asyncQueue, List <int> list, int lastItem, CancellationTokenSource cts)
        {
            while (true)
            {
                try
                {
                    int item = await asyncQueue.DequeueAsync(cts.Token);

                    await Task.Delay(this.random.Next(10));

                    list.Add(item);

                    // If we reached the last item, signal cancel to the other worker and finish.
                    if (item == lastItem)
                    {
                        cts.Cancel();
                        break;
                    }
                }
                catch (OperationCanceledException)
                {
                    break;
                }
            }
        }
コード例 #9
0
 /// <summary>
 /// 将对象添加到队列的结尾处。
 /// </summary>
 /// <param name="item">要添加到的对象。</param>
 public static async Task Enqueue <T>(this IAsyncQueue <T> aq, T item, TimeSpan timeout)
 {
     using (var source = new CancellationTokenSource(timeout))
     {
         await aq.Enqueue(item, CancellationToken.None);
     }
 }
コード例 #10
0
ファイル: AsyncQueue.cs プロジェクト: lmm713281/DataCommander
 /// <summary>
 /// Calls <see cref="Initialize"/> method.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="asyncQueue"></param>
 /// <param name="consumerCount"></param>
 /// <param name="priority"></param>
 public AsyncQueue(
     string name,
     IAsyncQueue asyncQueue,
     int consumerCount,
     ThreadPriority priority)
 {
     Initialize(name, asyncQueue, consumerCount, priority);
 }
コード例 #11
0
        /// <summary>
        /// Initializes the instance of the object and subscribes to the peer's message producer.
        /// </summary>
        /// <param name="peer">Connected network peer that we receive messages from.</param>
        public NetworkPeerListener(INetworkPeer peer, IAsyncProvider asyncProvider)
        {
            this.peer          = peer;
            this.asyncProvider = asyncProvider;
            this.asyncIncomingMessagesQueue = asyncProvider.CreateAsyncQueue <IncomingMessage>();

            this.messageProducerRegistration = peer.MessageProducer.AddMessageListener(this);
        }
コード例 #12
0
        public ILargeMessageQueue <T> Create <T>(string queueName, string blobContainerName, ILargeMessageQueueErrorHandler errorHandler) where T : class
        {
            IAsyncQueue <LargeMessageReference> referenceQueue = _queueFactory.CreateAsyncQueue <LargeMessageReference>(queueName);
            IAsyncBlockBlobRepository           blobRepository = _blobRepositoryFactory.CreateAsyncBlockBlobRepository(blobContainerName);

            ILargeMessageQueue <T> queue = new LargeMessageQueue <T>(_serializer, referenceQueue, blobRepository, _loggerFactory.CreateLogger <LargeMessageQueue <T> >(), errorHandler);

            return(queue);
        }
コード例 #13
0
 /// <summary>
 /// 将对象添加到队列的结尾处。
 /// </summary>
 /// <param name="item">要添加到的对象。</param>
 public static async Task EnqueueRange <T>(this IAsyncQueue <T> aq, IEnumerable <T> items)
 {
     if (items != null)
     {
         foreach (var item in items)
         {
             await aq.Enqueue(item, CancellationToken.None);
         }
     }
 }
コード例 #14
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="backoffPolicy">The back off policy to use.</param>
 /// <param name="queue">The queue to be processed</param>
 /// <param name="logger">The logger to use for reporting issues</param>
 /// <param name="dequeErrorHandler">Optional error handler for dequeue failures. This can return true / false or throw an exception</param>
 protected AbstractBackoffQueueProcessor(
     IAsyncBackoffPolicy backoffPolicy,
     IAsyncQueue <T> queue,
     ILogger <AbstractBackoffQueueProcessor <T> > logger,
     Func <Exception, Task <bool> > dequeErrorHandler = null)
 {
     _backoffPolicy     = backoffPolicy;
     _queue             = queue;
     _logger            = logger;
     _dequeErrorHandler = dequeErrorHandler;
 }
        public static async void Consumer(IAsyncQueue <int> queue)
        {
            while (true)
            {
                Console.WriteLine("{0}:Waiting", Thread.CurrentThread.ManagedThreadId);
                int val = await queue.Dequeue();

                Console.WriteLine("{0}: Processing {1}", Thread.CurrentThread.ManagedThreadId, val);
                Thread.Sleep(2000);
            }
        }
コード例 #16
0
ファイル: Program.cs プロジェクト: IDisposable/language-ext
    static Producer <RT, A, Unit> ToProducer <RT, A>(this IAsyncQueue <A> q)
        where RT : struct, HasCancel <RT>
    {
        return(Proxy.enumerate(go()));

        async IAsyncEnumerable <A> go()
        {
            while (true)
            {
                yield return(await q.DequeueAsync());
            }
        }
    }
コード例 #17
0
 /// <summary>
 /// 将对象添加到队列的结尾处。
 /// </summary>
 /// <param name="item">要添加到的对象。</param>
 public static async Task EnqueueRange <T>(this IAsyncQueue <T> aq, IEnumerable <T> items, TimeSpan timeout)
 {
     if (items != null)
     {
         using (var source = new CancellationTokenSource(timeout))
         {
             foreach (var item in items)
             {
                 await aq.Enqueue(item, CancellationToken.None);
             }
         }
     }
 }
コード例 #18
0
        public void CheckBlocksAnnounced_AndQueueEmptiesOverTime()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode stratisNodeSync = builder.CreateStratisPowNode(this.network, "bss-1-stratisNodeSync").WithReadyBlockchainData(ReadyBlockchain.BitcoinRegTest10Miner).Start();
                CoreNode stratisNode1    = builder.CreateStratisPowNode(this.network, "bss-1-stratisNode1").Start();

                // Change the second node's list of default behaviours include the test behaviour in it.
                // We leave the other behaviors alone for this test because we want to see what messages the node gets under normal operation.
                IConnectionManager node1ConnectionManager = stratisNode1.FullNode.NodeService <IConnectionManager>();
                node1ConnectionManager.Parameters.TemplateBehaviors.Add(new TestBehavior());

                // Connect node1 to initial node.
                TestHelper.Connect(stratisNode1, stratisNodeSync);

                INetworkPeer connectedPeer = node1ConnectionManager.ConnectedPeers.FindByEndpoint(stratisNodeSync.Endpoint);
                TestBehavior testBehavior  = connectedPeer.Behavior <TestBehavior>();

                TestBase.WaitLoop(() => TestHelper.AreNodesSynced(stratisNode1, stratisNodeSync));

                HashSet <uint256> advertised = new HashSet <uint256>();

                // Check to see that all blocks got advertised to node1 via the "headers" payload.
                foreach (IncomingMessage message in testBehavior.receivedMessageTracker["headers"])
                {
                    if (message.Message.Payload is HeadersPayload)
                    {
                        foreach (BlockHeader header in ((HeadersPayload)message.Message.Payload).Headers)
                        {
                            advertised.Add(header.GetHash());
                        }
                    }
                }

                foreach (ChainedHeader chainedHeader in stratisNodeSync.FullNode.ChainIndexer.EnumerateToTip(this.network.GenesisHash))
                {
                    if ((!advertised.Contains(chainedHeader.HashBlock)) && (!(chainedHeader.HashBlock == this.network.GenesisHash)))
                    {
                        throw new Exception($"An expected block was not advertised to peer: {chainedHeader.HashBlock}");
                    }
                }

                // Check current state of announce queue
                BlockStoreSignaled blockStoreSignaled = stratisNodeSync.FullNode.NodeService <BlockStoreSignaled>();

                IAsyncQueue <ChainedHeader> blocksToAnnounce = (IAsyncQueue <ChainedHeader>)blockStoreSignaled.GetMemberValue("blocksToAnnounce");
                Queue <ChainedHeader>       queueItems       = (Queue <ChainedHeader>)blocksToAnnounce.GetMemberValue("items");

                TestBase.WaitLoop(() => queueItems.Count == 0);
            }
        }
コード例 #19
0
        public LargeMessageQueue(
            IQueueSerializer serializer,
            IAsyncQueue <LargeMessageReference> referenceQueue,
            IAsyncBlockBlobRepository blobRepository,
            ILogger <LargeMessageQueue <T> > logger,
            ILargeMessageQueueErrorHandler errorHandler)
        {
            _serializer     = serializer;
            _referenceQueue = referenceQueue;
            _blobRepository = blobRepository;
            _logger         = logger;
            _errorHandler   = errorHandler;

            _logger.LogTrace("LargeMessageQueue<T>: constructing");
        }
コード例 #20
0
        public BatchSenderThread(IAsyncQueue <TQueueItem> dataSource, IMessageSender <TQueueItem> dataTarget, Func <TQueueItem, string> serializedData, ILogger logger)
            : base(SafeLogger.FromLogger(logger))
        {
            if (dataSource == null || dataTarget == null)
            {
                throw new ArgumentException("data source and data target cannot be null");
            }

            _operational      = new AutoResetEvent(false);
            _doWork           = new AutoResetEvent(false);
            _running          = false;
            _dataSource       = dataSource;
            _dataTarget       = dataTarget;
            _serializedData   = serializedData;
            _outstandingTasks = 0;
        }
コード例 #21
0
ファイル: AsyncQueue.cs プロジェクト: lmm713281/DataCommander
        /// <summary>
        /// Inherited class must call this initializer method first.
        /// </summary>
        /// <param name="name">The name of the <see cref="AsyncQueue"/></param>
        /// <param name="asyncQueue">The <see cref="IAsyncQueue"/> implementation</param>
        /// <param name="consumerCount">Number of consumers</param>
        /// <param name="priority">priority of consumer threads</param>
        protected void Initialize(
            string name,
            IAsyncQueue asyncQueue,
            int consumerCount,
            ThreadPriority priority)
        {
            Assert.IsTrue(consumerCount > 0);

            _name       = name;
            _asyncQueue = asyncQueue;

            for (var id = 0; id < consumerCount; id++)
            {
                var consumerThread = new ConsumerThread(this, id, priority);
                Consumers.Add(consumerThread.Thread);
            }
        }
コード例 #22
0
 public ActorHost(IActorHandlerMap actorHandlerMap,
                  IAsyncQueue <AsyncMessageContext> asyncQueue,
                  IAsyncQueue <ActorRegistration> actorRegistrationsQueue,
                  ISecurityProvider securityProvider,
                  ILocalSocket <IMessage> localRouterSocket,
                  ILocalSendingSocket <InternalRouteRegistration> internalRegistrationsSender,
                  ILocalSocketFactory localSocketFactory,
                  ILogger logger)
 {
     this.logger                      = logger;
     this.actorHandlerMap             = actorHandlerMap;
     this.securityProvider            = securityProvider;
     this.localRouterSocket           = localRouterSocket;
     this.internalRegistrationsSender = internalRegistrationsSender;
     this.asyncQueue                  = asyncQueue;
     this.actorRegistrationsQueue     = actorRegistrationsQueue;
     receivingSocket                  = localSocketFactory.Create <IMessage>();
 }
コード例 #23
0
ファイル: ActorHost.cs プロジェクト: gitter-badger/kino
 public ActorHost(ISocketFactory socketFactory,
                  IActorHandlerMap actorHandlerMap,
                  IAsyncQueue<AsyncMessageContext> asyncQueue,
                  IAsyncQueue<IActor> actorRegistrationsQueue,
                  RouterConfiguration routerConfiguration,
                  IMessageTracer messageTracer,
                  ILogger logger)
 {
     this.logger = logger;
     this.messageTracer = messageTracer;
     this.actorHandlerMap = actorHandlerMap;
     localSocketIdentityPromise = new TaskCompletionSource<byte[]>();
     this.socketFactory = socketFactory;
     this.routerConfiguration = routerConfiguration;
     this.asyncQueue = asyncQueue;
     this.actorRegistrationsQueue = actorRegistrationsQueue;
     cancellationTokenSource = new CancellationTokenSource();
 }
コード例 #24
0
        //--//

        public GatewayService(IAsyncQueue <QueuedItem> queue, EventProcessor processor, Func <string, string, QueuedItem> dataTransform = null)
        {
            if (queue == null || processor == null)
            {
                throw new ArgumentException("task queue and event processor cannot be null");
            }

            if (dataTransform != null)
            {
                _dataTransform = dataTransform;
            }
            else
            {
                _dataTransform = (deviceId, json) => new QueuedItem(deviceId, json);
            }

            _queue          = queue;
            _eventProcessor = processor;
        }
コード例 #25
0
ファイル: Program.cs プロジェクト: nistec/MQueue
        private static IQueueItem[] CreateItems(int count, IAsyncQueue q)
        {
            IQueueItem[] items = new IQueueItem[count];

            for (int i = 0; i < count; i++)
            {
                Priority  p    = (i % 5 == 0) ? Priority.High : (i % 2 == 0) ? Priority.Medium : Priority.Normal;
                QueueItem item = new QueueItem(p);
                item.MessageId   = i;
                item.Body        = "this is a test  אכן זוהי דוגמא";
                item.Subject     = "test";
                item.Sender      = "ibm";
                item.Destination = "nissim";
                q.Enqueue(item);
                items[i] = item;
                //logger.WriteLoge("Enqueue: " + ItemToString(item), Mode.INFO);
                counter = i;
            }
            return(items);
        }
コード例 #26
0
        //--//

        public GatewayService( IAsyncQueue<QueuedItem> queue, EventProcessor processor, Func<string, QueuedItem> dataTransform = null )
        {
            if( queue == null || processor == null )
            {
                throw new ArgumentException( "task queue and event processor cannot be null" );
            }

            if( dataTransform != null )
            {
                _dataTransform = dataTransform;
            }
            else
            {
                _dataTransform = m => new QueuedItem
                    {
                        JsonData = m
                    };
            }

            _queue = queue;
            _eventProcessor = processor;
        }
コード例 #27
0
        /// <summary>
        /// Creates a new instance of ChannelBase
        /// </summary>
        /// <param name="transport"></param>
        /// <param name="sendTimeout"></param>
        /// <param name="buffersLimit"></param>
        /// <param name="fillEnvelopeRecipients">Indicates if the from and to properties of sent and received envelopes should be filled with the session information if not defined.</param>
        /// <param name="autoReplyPings">Indicates if the channel should reply automatically to ping request commands. In this case, the ping command are not returned by the ReceiveCommandAsync method.</param>
        public ChannelBase(ITransport transport, TimeSpan sendTimeout, int buffersLimit, bool fillEnvelopeRecipients, bool autoReplyPings)
        {
            if (transport == null)
            {
                throw new ArgumentNullException("transport");
            }

            this.Transport          = transport;
            this.Transport.Closing += Transport_Closing;

            _sendTimeout            = sendTimeout;
            _fillEnvelopeRecipients = fillEnvelopeRecipients;
            _autoReplyPings         = autoReplyPings;

            _channelCancellationTokenSource = new CancellationTokenSource();

            this.State = SessionState.New;

#if MONO
            _messageBuffer      = new AsyncQueue <Message> (buffersLimit, buffersLimit);
            _commandBuffer      = new AsyncQueue <Command> (buffersLimit, buffersLimit);
            _notificationBuffer = new AsyncQueue <Notification> (buffersLimit, buffersLimit);
            _sessionBuffer      = new AsyncQueue <Session> (1, 1);
#else
            var bufferOptions = new System.Threading.Tasks.Dataflow.DataflowBlockOptions()
            {
                BoundedCapacity = buffersLimit
            };

            _messageBuffer      = new BufferBlock <Message>(bufferOptions);
            _commandBuffer      = new BufferBlock <Command>(bufferOptions);
            _notificationBuffer = new BufferBlock <Notification>(bufferOptions);
            _sessionBuffer      = new BufferBlock <Session>(
                new DataflowBlockOptions()
            {
                BoundedCapacity = 1
            });
#endif
        }
コード例 #28
0
        public BlockStoreQueue(
            ChainIndexer chainIndexer,
            IChainState chainState,
            IBlockStoreQueueFlushCondition blockStoreQueueFlushCondition,
            StoreSettings storeSettings,
            IBlockRepository blockRepository,
            ILoggerFactory loggerFactory,
            INodeStats nodeStats,
            IAsyncProvider asyncProvider,
            IInitialBlockDownloadState initialBlockDownloadState)
        {
            Guard.NotNull(blockStoreQueueFlushCondition, nameof(blockStoreQueueFlushCondition));
            Guard.NotNull(chainIndexer, nameof(chainIndexer));
            Guard.NotNull(chainState, nameof(chainState));
            Guard.NotNull(storeSettings, nameof(storeSettings));
            Guard.NotNull(loggerFactory, nameof(loggerFactory));
            Guard.NotNull(blockRepository, nameof(blockRepository));
            Guard.NotNull(nodeStats, nameof(nodeStats));

            this.initialBlockDownloadState     = initialBlockDownloadState;
            this.blockStoreQueueFlushCondition = blockStoreQueueFlushCondition;
            this.chainIndexer           = chainIndexer;
            this.chainState             = chainState;
            this.storeSettings          = storeSettings;
            this.blockRepository        = blockRepository;
            this.asyncProvider          = asyncProvider;
            this.batch                  = new List <ChainedHeaderBlock>();
            this.blocksCacheLock        = new object();
            this.blocksQueue            = asyncProvider.CreateAsyncQueue <ChainedHeaderBlock>();
            this.pendingBlocksCache     = new Dictionary <uint256, ChainedHeaderBlock>();
            this.logger                 = loggerFactory.CreateLogger(this.GetType().FullName);
            this.cancellation           = new CancellationTokenSource();
            this.saveAsyncLoopException = null;

            this.BatchThresholdSizeBytes = storeSettings.MaxCacheSize * 1024 * 1024;

            nodeStats.RegisterStats(this.AddComponentStats, StatsType.Component, this.GetType().Name);
        }
コード例 #29
0
 public RequestsController(IForwardSender forwardSender, IAsyncQueue <RequestData> backwardRequestQueue)
 {
     this.forwardSender        = forwardSender;
     this.backwardRequestQueue = backwardRequestQueue;
 }
コード例 #30
0
 public ILargeMessageQueue <T> Create <T>(IAsyncQueue <LargeMessageReference> referenceQueue, IAsyncBlockBlobRepository blobRepository) where T : class
 {
     return(Create <T>(referenceQueue, blobRepository, null));
 }
コード例 #31
0
        public void MustNotAnnounceABlock_WhenNotInBestChain()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode stratisNodeSync = builder.CreateStratisPowNode(this.network, "bss-2-stratisNodeSync").WithReadyBlockchainData(ReadyBlockchain.BitcoinRegTest10Miner).Start();
                CoreNode stratisNode1    = builder.CreateStratisPowNode(this.network, "bss-2-stratisNode1").WithReadyBlockchainData(ReadyBlockchain.BitcoinRegTest10Listener).Start();
                CoreNode stratisNode2    = builder.CreateStratisPowNode(this.network, "bss-2-stratisNode2").WithReadyBlockchainData(ReadyBlockchain.BitcoinRegTest10NoWallet).Start();

                // Store block 1 of chain0 for later usage
                ChainedHeader firstBlock = null;
                foreach (ChainedHeader chainedHeader in stratisNodeSync.FullNode.ChainIndexer.EnumerateToTip(this.network.GenesisHash))
                {
                    if (chainedHeader.Height == 1)
                    {
                        firstBlock = chainedHeader;
                    }
                }

                Assert.NotNull(firstBlock);

                // Mine longer chain1 using node1
                TestHelper.MineBlocks(stratisNode1, 15);

                IConnectionManager node1ConnectionManager = stratisNode1.FullNode.NodeService <IConnectionManager>();
                node1ConnectionManager.Parameters.TemplateBehaviors.Add(new TestBehavior());

                IConnectionManager node2ConnectionManager = stratisNode2.FullNode.NodeService <IConnectionManager>();
                node2ConnectionManager.Parameters.TemplateBehaviors.Add(new TestBehavior());

                // Connect node0 and node1
                TestHelper.Connect(stratisNode1, stratisNodeSync);

                INetworkPeer connectedPeer = node1ConnectionManager.ConnectedPeers.FindByEndpoint(stratisNodeSync.Endpoint);
                TestBehavior testBehavior  = connectedPeer.Behavior <TestBehavior>();

                // We expect that node0 will abandon the 10 block chain and use the 15 block chain from node1
                TestBase.WaitLoop(() => TestHelper.AreNodesSynced(stratisNode1, stratisNodeSync));

                // Connect all nodes together
                TestHelper.Connect(stratisNode2, stratisNodeSync);
                TestHelper.Connect(stratisNode1, stratisNode2);

                INetworkPeer connectedPeer2 = node2ConnectionManager.ConnectedPeers.FindByEndpoint(stratisNodeSync.Endpoint);
                TestBehavior testBehavior2  = connectedPeer2.Behavior <TestBehavior>();

                // Wait for node2 to sync; it should have the 15 block chain
                TestBase.WaitLoop(() => TestHelper.AreNodesSynced(stratisNode2, stratisNodeSync));

                // Insert block 1 from chain0 into node1's announce queue
                BlockStoreSignaled node1BlockStoreSignaled = stratisNode1.FullNode.NodeService <BlockStoreSignaled>();

                IAsyncQueue <ChainedHeader> node1BlocksToAnnounce = (IAsyncQueue <ChainedHeader>)node1BlockStoreSignaled.GetMemberValue("blocksToAnnounce");

                Queue <ChainedHeader> node1QueueItems = (Queue <ChainedHeader>)node1BlocksToAnnounce.GetMemberValue("items");

                TestBase.WaitLoop(() => node1QueueItems.Count == 0);

                // Check that node2 does not have block 1 in test behaviour advertised list
                foreach (IncomingMessage message in testBehavior2.receivedMessageTracker["headers"])
                {
                    if (message.Message.Payload is HeadersPayload)
                    {
                        foreach (BlockHeader header in ((HeadersPayload)message.Message.Payload).Headers)
                        {
                            if (header.GetHash() == firstBlock.Header.GetHash())
                            {
                                throw new Exception("Should not have received payload announcing block from wrong chain");
                            }
                        }
                    }
                }
            }
        }
コード例 #32
0
        public void CheckBlocksAnnounced_AndQueueEmptiesOverTime_ForMultiplePeers_WhenOneIsDisconnected()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode stratisNodeSync = builder.CreateStratisPowNode(this.network, "bss-2-stratisNodeSync").WithReadyBlockchainData(ReadyBlockchain.BitcoinRegTest10Miner).Start();

                CoreNode stratisNode1 = builder.CreateStratisPowNode(this.network, "bss-2-stratisNode1").Start();
                CoreNode stratisNode2 = builder.CreateStratisPowNode(this.network, "bss-2-stratisNode2").Start();
                CoreNode stratisNode3 = builder.CreateStratisPowNode(this.network, "bss-2-stratisNode3").Start();

                // Change the other nodes' lists of default behaviours include the test behaviour in it.
                // We leave the other behaviors alone for this test because we want to see what messages the node gets under normal operation.
                IConnectionManager node1ConnectionManager = stratisNode1.FullNode.NodeService <IConnectionManager>();
                node1ConnectionManager.Parameters.TemplateBehaviors.Add(new TestBehavior());

                IConnectionManager node2ConnectionManager = stratisNode2.FullNode.NodeService <IConnectionManager>();
                node2ConnectionManager.Parameters.TemplateBehaviors.Add(new TestBehavior());

                // Connect other nodes to initial node.
                TestHelper.Connect(stratisNode1, stratisNodeSync);
                TestHelper.Connect(stratisNode2, stratisNodeSync);
                TestHelper.Connect(stratisNode3, stratisNodeSync);

                // Make node3 unable to respond to anything, effectively disconnecting it.
                IConnectionManager node3ConnectionManager = stratisNode3.FullNode.NodeService <IConnectionManager>();
                node3ConnectionManager.Parameters.TemplateBehaviors.Clear();
                node3ConnectionManager.Parameters.TemplateBehaviors.Add(new TestBehavior());

                INetworkPeer connectedPeer1 = node1ConnectionManager.ConnectedPeers.FindByEndpoint(stratisNodeSync.Endpoint);
                TestBehavior testBehavior1  = connectedPeer1.Behavior <TestBehavior>();

                INetworkPeer connectedPeer2 = node2ConnectionManager.ConnectedPeers.FindByEndpoint(stratisNodeSync.Endpoint);
                TestBehavior testBehavior2  = connectedPeer2.Behavior <TestBehavior>();

                INetworkPeer connectedPeer3 = node3ConnectionManager.ConnectedPeers.FindByEndpoint(stratisNodeSync.Endpoint);
                TestBehavior testBehavior3  = connectedPeer3.Behavior <TestBehavior>();

                // If the announce queue is not getting stalled, the other 2 nodes should sync properly.
                TestBase.WaitLoop(() => TestHelper.AreNodesSynced(stratisNode1, stratisNodeSync));
                TestBase.WaitLoop(() => TestHelper.AreNodesSynced(stratisNode2, stratisNodeSync));

                HashSet <uint256> advertised = new HashSet <uint256>();

                // Check to see that all blocks got advertised to node1 via the "headers" payload.
                foreach (IncomingMessage message in testBehavior1.receivedMessageTracker["headers"])
                {
                    if (message.Message.Payload is HeadersPayload)
                    {
                        foreach (BlockHeader header in ((HeadersPayload)message.Message.Payload).Headers)
                        {
                            advertised.Add(header.GetHash());
                        }
                    }
                }

                foreach (ChainedHeader chainedHeader in stratisNodeSync.FullNode.ChainIndexer.EnumerateToTip(this.network.GenesisHash))
                {
                    if ((!advertised.Contains(chainedHeader.HashBlock)) && (!(chainedHeader.HashBlock == this.network.GenesisHash)))
                    {
                        throw new Exception($"An expected block was not advertised to peer 1: {chainedHeader.HashBlock}");
                    }
                }

                advertised.Clear();

                // Check to see that all blocks got advertised to node1 via the "headers" payload.
                foreach (IncomingMessage message in testBehavior2.receivedMessageTracker["headers"])
                {
                    if (message.Message.Payload is HeadersPayload)
                    {
                        foreach (BlockHeader header in ((HeadersPayload)message.Message.Payload).Headers)
                        {
                            advertised.Add(header.GetHash());
                        }
                    }
                }

                foreach (ChainedHeader chainedHeader in stratisNodeSync.FullNode.ChainIndexer.EnumerateToTip(this.network.GenesisHash))
                {
                    if ((!advertised.Contains(chainedHeader.HashBlock)) && (!(chainedHeader.HashBlock == this.network.GenesisHash)))
                    {
                        throw new Exception($"An expected block was not advertised to peer 2: {chainedHeader.HashBlock}");
                    }
                }

                // Check current state of announce queue.
                BlockStoreSignaled blockStoreSignaled = stratisNodeSync.FullNode.NodeService <BlockStoreSignaled>();

                IAsyncQueue <ChainedHeader> blocksToAnnounce = (IAsyncQueue <ChainedHeader>)blockStoreSignaled.GetMemberValue("blocksToAnnounce");
                Queue <ChainedHeader>       queueItems       = (Queue <ChainedHeader>)blocksToAnnounce.GetMemberValue("items");

                // It should still eventually empty despite not being able to communicate with node3.
                TestBase.WaitLoop(() => queueItems.Count == 0);
            }
        }