コード例 #1
0
        public void Notify_WithSync_SetPullerLocationToPreviousBlockMatchingStartHash()
        {
            var blocks = this.CreateBlocks(3);

            var chain = new ConcurrentChain(blocks[0].Header);

            this.AppendBlocksToChain(chain, blocks.Skip(1).Take(2));

            var dataFolder        = CreateDataFolder(this);
            var connectionManager = new Mock <IConnectionManager>();

            connectionManager.Setup(c => c.ConnectedPeers).Returns(new NetworkPeerCollection());
            connectionManager.Setup(c => c.NodeSettings).Returns(new NodeSettings().LoadArguments(new string[] { $"-datadir={dataFolder.WalletPath}" }));
            connectionManager.Setup(c => c.Parameters).Returns(new NetworkPeerConnectionParameters());

            var lookAheadBlockPuller = new LookaheadBlockPuller(chain, connectionManager.Object, new Mock <ILoggerFactory>().Object);
            var lifetime             = new NodeLifetime();

            var notification = new BlockNotification(this.LoggerFactory.Object, chain, lookAheadBlockPuller, new Signals.Signals(), new AsyncLoopFactory(new LoggerFactory()), lifetime);

            notification.SyncFrom(blocks[0].GetHash());
            notification.SyncFrom(blocks[0].GetHash());

            notification.Notify(lifetime.ApplicationStopping);

            Assert.Equal(0, lookAheadBlockPuller.Location.Height);
            Assert.Equal(lookAheadBlockPuller.Location.Header.GetHash(), blocks[0].Header.GetHash());
        }
コード例 #2
0
        public void Notify_Reorg_PushesPullerBackToForkPoint_SignalsNewLookaheadResult()
        {
            List <Block> blocks = this.CreateBlocks(3);

            this.chain = new ConcurrentChain(this.Network, new ChainedHeader(blocks[0].Header, blocks[0].GetHash(), 0));
            this.AppendBlocksToChain(this.chain, blocks.Skip(1));

            var source = new CancellationTokenSource();
            CancellationToken token = source.Token;

            this.signals.Setup(s => s.OnBlockConnected.Notify(It.Is <ChainedHeaderBlock>(b => b.Block.GetHash() == blocks[0].GetHash())))
            .Callback(() =>
            {
                source.Cancel();
            }).Verifiable();

            var notification = new BlockNotification(this.LoggerFactory.Object, this.chain, this.consensusManager.Object,
                                                     this.signals.Object, new AsyncLoopFactory(new LoggerFactory()), this.lifetime);

            try
            {
                notification.SyncFrom(blocks[0].GetHash());
                notification.Notify(token);
            }
            catch (OperationCanceledException)
            {
            }

            this.signals.Verify();
        }
コード例 #3
0
        public void NotifyBroadcastsOnNextBlock()
        {
            var startBlockId = new uint256(156);
            var chain        = new Mock <ConcurrentChain>();
            var header       = new BlockHeader();

            chain.Setup(c => c.GetBlock(startBlockId))
            .Returns(new ChainedBlock(header, 0));

            var stub = new Mock <ILookaheadBlockPuller>();

            stub.SetupSequence(s => s.NextBlock(this.source.Token))
            .Returns(new Block())
            .Returns(new Block())
            .Returns((Block)null);

            var signals      = new Mock <ISignals>();
            var signalerMock = new Mock <ISignaler <Block> >();

            signals.Setup(s => s.Blocks)
            .Returns(signalerMock.Object);

            var notification = new BlockNotification(chain.Object, stub.Object, signals.Object);

            notification.Notify(this.source.Token);
            notification.SyncFrom(startBlockId);

            Thread.Sleep(100);
            signalerMock.Verify(s => s.Broadcast(It.IsAny <Block>()), Times.Exactly(2));
        }
コード例 #4
0
        public async Task NotifyWithSyncFromSetBroadcastsOnNextBlock()
        {
            var lifetime = new NodeLifetime();

            new CancellationTokenSource(100).Token.Register(() => lifetime.StopApplication());

            var startBlockId = new uint256(156);
            var chain        = new Mock <ConcurrentChain>();
            var header       = new BlockHeader();

            chain.Setup(c => c.GetBlock(startBlockId))
            .Returns(new ChainedBlock(header, 0));

            var stub = new Mock <ILookaheadBlockPuller>();

            stub.SetupSequence(s => s.NextBlock(lifetime.ApplicationStopping))
            .Returns(new Block())
            .Returns(new Block())
            .Returns((Block)null);

            var signals      = new Mock <ISignals>();
            var signalerMock = new Mock <ISignaler <Block> >();

            signals.Setup(s => s.Blocks)
            .Returns(signalerMock.Object);

            var notification = new BlockNotification(chain.Object, stub.Object, signals.Object, new AsyncLoopFactory(new LoggerFactory()), lifetime);

            notification.SyncFrom(startBlockId);
            await notification.Notify();

            signalerMock.Verify(s => s.Broadcast(It.IsAny <Block>()), Times.Exactly(2));
        }
コード例 #5
0
        public void Notify_Completes_StartHashNotSet()
        {
            var notification = new BlockNotification(this.LoggerFactory.Object, this.chain, this.consensusManager.Object, this.signals.Object, new AsyncLoopFactory(new LoggerFactory()), this.lifetime);

            notification.Notify(this.lifetime.ApplicationStopping);

            this.signals.Verify(s => s.OnBlockConnected.Notify(It.IsAny <ChainedHeaderBlock>()), Times.Exactly(0));
        }
コード例 #6
0
ファイル: Tracker.cs プロジェクト: dangershony/Breeze
 public Tracker(IWalletManager walletManager, ConcurrentChain chain, Signals signals, BlockNotification blockNotification)
 {
     this.walletManager     = walletManager as WalletManager;
     this.chain             = chain;
     this.signals           = signals;
     this.blockNotification = blockNotification;
     this.coinType          = chain.GetCoinType();
 }
コード例 #7
0
        public void SyncFrom_StartHashIsNull_SetsStartHashToBlockNotification()
        {
            var notification = new BlockNotification(this.LoggerFactory.Object, this.chain, this.consensusManager.Object,
                                                     this.signals.Object, new AsyncLoopFactory(new LoggerFactory()), this.lifetime);

            notification.SyncFrom(null);

            Assert.Null(notification.StartHash);
        }
コード例 #8
0
 public LightWalletSyncManager(ILoggerFactory loggerFactory, IWalletManager walletManager, ConcurrentChain chain, Network network,
                               BlockNotification blockNotification, Signals signals)// :base(loggerFactory, walletManager, chain, network)
 {
     this.walletManager     = walletManager as WalletManager;
     this.chain             = chain;
     this.signals           = signals;
     this.blockNotification = blockNotification;
     this.coinType          = (CoinType)network.Consensus.CoinType;
     this.logger            = loggerFactory.CreateLogger(this.GetType().FullName);
 }
コード例 #9
0
        public void Notify_Completes_StartHashNotOnChain()
        {
            var startBlockId = new uint256(156);
            var notification = new BlockNotification(this.LoggerFactory.Object, this.chain, this.consensusManager.Object, this.signals.Object, new AsyncLoopFactory(new LoggerFactory()), this.lifetime);

            notification.SyncFrom(startBlockId);
            notification.Notify(this.lifetime.ApplicationStopping);

            this.signals.Verify(s => s.SignalBlockConnected(It.IsAny <ChainedHeaderBlock>()), Times.Exactly(0));
        }
コード例 #10
0
        public void Start_RunsAsyncLoop()
        {
            var asyncLoopFactory = new Mock <IAsyncLoopFactory>();

            var notification = new BlockNotification(this.LoggerFactory.Object, this.chain, this.consensusManager.Object, this.signals.Object, asyncLoopFactory.Object, this.lifetime);

            notification.Start();

            asyncLoopFactory.Verify(a => a.Run("Notify", It.IsAny <Func <CancellationToken, Task> >(), It.IsAny <CancellationToken>(), null, null));
        }
コード例 #11
0
 public LightWalletSyncManager(ILoggerFactory loggerFactory, IWalletManager walletManager, ConcurrentChain chain, Network network,
                               BlockNotification blockNotification, Signals signals, FullNode.CancellationProvider cancellationProvider)
 {
     this.walletManager        = walletManager as WalletManager;
     this.chain                = chain;
     this.signals              = signals;
     this.blockNotification    = blockNotification;
     this.coinType             = (CoinType)network.Consensus.CoinType;
     this.logger               = loggerFactory.CreateLogger(this.GetType().FullName);
     this.cancellationProvider = cancellationProvider;
 }
コード例 #12
0
        public void NotifyStartHashNotOnChainCompletes()
        {
            var startBlockId = new uint256(156);
            var chain        = new Mock <ConcurrentChain>();

            chain.Setup(c => c.GetBlock(startBlockId))
            .Returns((ChainedBlock)null);

            var notification = new BlockNotification(chain.Object, new Mock <ILookaheadBlockPuller>().Object, new Bitcoin.Signals.Signals(), new AsyncLoopFactory(new LoggerFactory()), new NodeLifetime());

            notification.Notify();
        }
コード例 #13
0
        public void Notify_Completes_StartHashNotSet()
        {
            var lifetime = new NodeLifetime();
            var signals  = new Mock <ISignals>();
            var chain    = new Mock <ConcurrentChain>();

            var notification = new BlockNotification(this.LoggerFactory.Object, chain.Object, new Mock <ILookaheadBlockPuller>().Object, signals.Object, new AsyncLoopFactory(new LoggerFactory()), lifetime);

            notification.Notify(lifetime.ApplicationStopping);

            signals.Verify(s => s.SignalBlock(It.IsAny <Block>()), Times.Exactly(0));
        }
コード例 #14
0
        public void NotifyStartHashNotOnChainCompletes()
        {
            var startBlockId = new uint256(156);
            var chain        = new Mock <ConcurrentChain>();

            chain.Setup(c => c.GetBlock(startBlockId))
            .Returns((ChainedBlock)null);

            var notification = new BlockNotification(chain.Object, new Mock <ILookaheadBlockPuller>().Object, new Signals());

            notification.Notify(this.source.Token);
        }
コード例 #15
0
        public void SyncFrom_StartHashIsNull_SetsStartHashToBlockNotification()
        {
            var lifetime = new NodeLifetime();
            var chain    = new Mock <ConcurrentChain>();
            var puller   = new Mock <ILookaheadBlockPuller>();
            var signals  = new Mock <ISignals>();

            var notification = new BlockNotification(this.LoggerFactory.Object, chain.Object, puller.Object, signals.Object, new AsyncLoopFactory(new LoggerFactory()), lifetime);

            notification.SyncFrom(null);

            Assert.Null(notification.StartHash);
        }
コード例 #16
0
        public void Start_RunsAsyncLoop()
        {
            var lifetime         = new NodeLifetime();
            var chain            = new Mock <ConcurrentChain>();
            var puller           = new Mock <ILookaheadBlockPuller>();
            var signals          = new Mock <ISignals>();
            var asyncLoopFactory = new Mock <IAsyncLoopFactory>();

            var notification = new BlockNotification(this.LoggerFactory.Object, chain.Object, puller.Object, signals.Object, asyncLoopFactory.Object, lifetime);

            notification.Start();

            asyncLoopFactory.Verify(a => a.Run("Notify", It.IsAny <Func <CancellationToken, Task> >(), It.IsAny <CancellationToken>(), null, null));
        }
コード例 #17
0
        public void SyncFrom_StartHashIsNotNull_GetsBlockBasedOnStartHash_SetsPullerAndTipToPreviousBlock()
        {
            List <Block> blocks = this.CreateBlocks(3);

            this.chain = new ConcurrentChain(this.Network, new ChainedHeader(blocks[0].Header, blocks[0].GetHash(), 0));
            this.AppendBlocksToChain(this.chain, blocks.Skip(1));

            var notification = new BlockNotification(this.LoggerFactory.Object, this.chain, this.consensusManager.Object, this.signals.Object, new AsyncLoopFactory(new LoggerFactory()), this.lifetime);

            notification.SyncFrom(blocks[0].GetHash());
            notification.SyncFrom(blocks[2].GetHash());

            Assert.Equal(notification.StartHash, blocks[2].GetHash());
        }
コード例 #18
0
        public void Notify_Completes_StartHashNotOnChain()
        {
            var lifetime = new NodeLifetime();
            var signals  = new Mock <ISignals>();

            var startBlockId = new uint256(156);
            var chain        = new ConcurrentChain(this.Network);
            var notification = new BlockNotification(this.LoggerFactory.Object, chain, new Mock <ILookaheadBlockPuller>().Object, signals.Object, new AsyncLoopFactory(new LoggerFactory()), lifetime);

            notification.SyncFrom(startBlockId);
            notification.Notify(lifetime.ApplicationStopping);

            signals.Verify(s => s.SignalBlockConnected(It.IsAny <Block>()), Times.Exactly(0));
        }
コード例 #19
0
        public void CallingSyncFromUpdatesStartHashAccordingly()
        {
            var notification = new BlockNotification(this.LoggerFactory.Object, this.chain, this.consensusManager.Object,
                                                     this.signals.Object, new AsyncLoopFactory(new LoggerFactory()), this.lifetime);

            var blockId1 = new uint256(150);
            var blockId2 = new uint256(151);

            Assert.Null(notification.StartHash);
            notification.SyncFrom(blockId1);

            Assert.NotNull(notification.StartHash);
            Assert.Equal(blockId1, notification.StartHash);

            notification.SyncFrom(blockId2);
            Assert.Equal(blockId2, notification.StartHash);
        }
コード例 #20
0
        public void Notify_Reorg_PushesPullerBackToForkPoint_SignalsNewLookaheadResult()
        {
            var lifetime = new NodeLifetime();
            var puller   = new Mock <ILookaheadBlockPuller>();
            var signals  = new Mock <ISignals>();

            List <Block> blocks = this.CreateBlocks(3);

            var chain = new ConcurrentChain(this.Network, new ChainedHeader(blocks[0].Header, blocks[0].GetHash(), 0));

            this.AppendBlocksToChain(chain, blocks.Skip(1));

            puller.SetupSequence(p => p.NextBlock(It.IsAny <CancellationToken>()))
            .Returns(new LookaheadResult()
            {
                Block = null
            })
            .Returns(new LookaheadResult()
            {
                Block = blocks[0]
            });


            var source = new CancellationTokenSource();
            CancellationToken token = source.Token;

            signals.Setup(s => s.SignalBlock(It.Is <Block>(b => b.GetHash() == blocks[0].GetHash())))
            .Callback(() =>
            {
                source.Cancel();
            }).Verifiable();

            var notification = new BlockNotification(this.LoggerFactory.Object, chain, puller.Object, signals.Object, new AsyncLoopFactory(new LoggerFactory()), lifetime);

            try
            {
                notification.SyncFrom(blocks[0].GetHash());
                notification.Notify(token);
            }
            catch (OperationCanceledException)
            {
            }

            puller.Verify(p => p.SetLocation(It.Is <ChainedHeader>(b => b.HashBlock == chain.GetBlock(0).HashBlock)));
            signals.Verify();
        }
コード例 #21
0
        public void Stop_DisposesAsyncLoop()
        {
            var lifetime         = new NodeLifetime();
            var chain            = new ConcurrentChain(this.Network);
            var puller           = new Mock <ILookaheadBlockPuller>();
            var signals          = new Mock <ISignals>();
            var asyncLoop        = new Mock <IAsyncLoop>();
            var asyncLoopFactory = new Mock <IAsyncLoopFactory>();

            asyncLoopFactory.Setup(a => a.Run("Notify", It.IsAny <Func <CancellationToken, Task> >(), It.IsAny <CancellationToken>(), null, null))
            .Returns(asyncLoop.Object);

            var notification = new BlockNotification(this.LoggerFactory.Object, chain, puller.Object, signals.Object, asyncLoopFactory.Object, lifetime);

            notification.Start();
            notification.Stop();

            asyncLoop.Verify(a => a.Dispose());
        }
コード例 #22
0
 public LightWalletSyncManager(
     ILoggerFactory loggerFactory,
     IWalletManager walletManager,
     ConcurrentChain chain,
     Network network,
     BlockNotification blockNotification,
     Signals signals,
     INodeLifetime nodeLifetime,
     IAsyncLoopFactory asyncLoopFactory)
 {
     this.walletManager     = walletManager as WalletManager;
     this.chain             = chain;
     this.signals           = signals;
     this.blockNotification = blockNotification;
     this.coinType          = (CoinType)network.Consensus.CoinType;
     this.logger            = loggerFactory.CreateLogger(this.GetType().FullName);
     this.nodeLifetime      = nodeLifetime;
     this.asyncLoopFactory  = asyncLoopFactory;
 }
コード例 #23
0
        public void NotifySetsPullerLocationToBlockMatchingStartHash()
        {
            var startBlockId = new uint256(156);
            var chain        = new Mock <ConcurrentChain>();
            var header       = new BlockHeader();

            chain.Setup(c => c.GetBlock(startBlockId))
            .Returns(new ChainedBlock(header, 0));

            var stub = new Mock <ILookaheadBlockPuller>();

            stub.Setup(s => s.NextBlock(this.source.Token))
            .Returns((Block)null);

            var notification = new BlockNotification(chain.Object, stub.Object, new Signals());

            notification.Notify(this.source.Token);
            notification.SyncFrom(startBlockId);
            stub.Verify(s => s.SetLocation(It.Is <ChainedBlock>(c => c.Height == 0 && c.Header.GetHash() == header.GetHash())));
        }
コード例 #24
0
        public void SyncFrom_StartHashIsNotNull_GetsBlockBasedOnStartHash_SetsPullerAndTipToPreviousBlock()
        {
            var lifetime = new NodeLifetime();
            var puller   = new Mock <ILookaheadBlockPuller>();
            var signals  = new Mock <ISignals>();

            var blocks = this.CreateBlocks(3);

            var chain = new ConcurrentChain(blocks[0].Header);

            this.AppendBlocksToChain(chain, blocks.Skip(1));

            var notification = new BlockNotification(this.LoggerFactory.Object, chain, puller.Object, signals.Object, new AsyncLoopFactory(new LoggerFactory()), lifetime);

            notification.SyncFrom(blocks[0].GetHash());
            notification.SyncFrom(blocks[2].GetHash());

            Assert.Equal(notification.StartHash, blocks[2].GetHash());
            puller.Verify(p => p.SetLocation(It.Is <ChainedBlock>(b => b.GetHashCode() == chain.GetBlock(1).GetHashCode())));
        }
コード例 #25
0
        public void CallingSyncFromUpdatesStartHashAccordingly()
        {
            var lifetime = new NodeLifetime();
            var chain    = new Mock <ConcurrentChain>();
            var puller   = new Mock <ILookaheadBlockPuller>();
            var signals  = new Mock <ISignals>();

            var notification = new BlockNotification(this.LoggerFactory.Object, chain.Object, puller.Object, signals.Object, new AsyncLoopFactory(new LoggerFactory()), lifetime);

            var blockId1 = new uint256(150);
            var blockId2 = new uint256(151);

            Assert.Null(notification.StartHash);
            notification.SyncFrom(blockId1);

            Assert.NotNull(notification.StartHash);
            Assert.Equal(blockId1, notification.StartHash);

            notification.SyncFrom(blockId2);
            Assert.Equal(blockId2, notification.StartHash);
        }
コード例 #26
0
        public void NotifySetsPullerLocationToBlockMatchingStartHash()
        {
            var startBlockId = new uint256(156);
            var chain        = new Mock <ConcurrentChain>();
            var header       = new BlockHeader();

            chain.Setup(c => c.GetBlock(startBlockId))
            .Returns(new ChainedBlock(header, 0));

            var stub     = new Mock <ILookaheadBlockPuller>();
            var lifetime = new NodeLifetime();

            stub.Setup(s => s.NextBlock(lifetime.ApplicationStopping))
            .Returns((Block)null);

            var notification = new BlockNotification(chain.Object, stub.Object, new Bitcoin.Signals.Signals(), new AsyncLoopFactory(new LoggerFactory()), lifetime);

            notification.Notify();
            notification.SyncFrom(startBlockId);
            notification.SyncFrom(startBlockId);
            stub.Verify(s => s.SetLocation(It.Is <ChainedBlock>(c => c.Height == 0 && c.Header.GetHash() == header.GetHash())));
        }
コード例 #27
0
        public RegistrationFeature(ILoggerFactory loggerFactory,
                                   NodeSettings nodeSettings,
                                   RegistrationManager registrationManager,
                                   RegistrationStore registrationStore,
                                   ConcurrentChain chain,
                                   Signals signals,
                                   IWatchOnlyWalletManager watchOnlyWalletManager,
                                   IBlockNotification blockNotification,
                                   IWalletSyncManager walletSyncManager)
        {
            this.loggerFactory       = loggerFactory;
            this.logger              = loggerFactory.CreateLogger(this.GetType().FullName);
            this.nodeSettings        = nodeSettings;
            this.registrationManager = registrationManager;
            this.registrationStore   = registrationStore;
            this.chain   = chain;
            this.signals = signals;
            this.network = nodeSettings.Network;
            this.watchOnlyWalletManager = watchOnlyWalletManager;
            this.blockNotification      = blockNotification as BlockNotification;
            this.walletSyncManager      = walletSyncManager;

            if (nodeSettings.Network == Network.Main || nodeSettings.Network == Network.TestNet || nodeSettings.Network == Network.RegTest)
            {
                // Bitcoin networks - these currently only interrogate the registration store for initial masternode selection
                this.isBitcoin = true;
            }
            else
            {
                // Stratis networks - these write to the registration store as new registrations come in via blocks
                this.isBitcoin = false;

                // Force registration store to be kept in same folder as other node data
                this.registrationStore.SetStorePath(this.nodeSettings.DataDir);
            }
        }
コード例 #28
0
 public NodeController(BlockNotification blockNotification)
 {
     this.blockNotification = blockNotification;
 }
コード例 #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NotificationsController"/> class.
 /// </summary>
 /// <param name="blockNotification">The block notification.</param>
 /// <param name="chain">The chain.</param>
 public NotificationsController(BlockNotification blockNotification, ConcurrentChain chain)
 {
     this.blockNotification = blockNotification;
     this.chain             = chain;
 }
コード例 #30
0
 private void OnBlockNotification(BlockNotification notification)
 {
 }