Exemple #1
0
        public async Task ResumeChainListenerFromHigher()
        {
            // root -> A -> B(invalidated) -> C
            // resume from C to A
            var c           = _cli.RPCClient;
            var blockSource = new BitcoinRPCBlockSource(c);

            var rootBlock = await c.GetBestBlockAsync(GetBlockVerbosity.WithFullTx);

            var listener = new DummyChainListener(rootBlock.Block, (uint)rootBlock.Height, _output);
            GetBlockRPCResponse?oldTip = null;

            for (int i = 0; i < 3; i++)
            {
                await c.GenerateToOwnAddressAsync(1);

                oldTip = await c.GetBestBlockAsync(GetBlockVerbosity.WithFullTx);

                listener.BlockConnected(oldTip.Block, (uint)oldTip.Height);
            }
            var blockB = await c.GetBlockHeaderAsync(rootBlock.Height + 2);

            await c.InvalidateBlockAsync(blockB.GetHash());

            var newTip = await c.GetBestBlockAsync(GetBlockVerbosity.WithFullTx);

            await listener.SyncChainListener(oldTip?.Header.GetHash(), newTip.Header, (uint)newTip.Height, blockSource, Network.RegTest, _loggerFactory.CreateLogger(nameof(ResumeChainListenerFromHigher)));

            Assert.True(listener.Blocks.Peek().ToBytes().SequenceEqual(newTip.Block.ToBytes()));
        }
Exemple #2
0
        public async Task ResumeChainListenerFromLowerFork()
        {
            // root -> A(invalidated)
            //      \
            //       B -> C
            // resume from A to C
            var c           = _cli.RPCClient;
            var blockSource = new BitcoinRPCBlockSource(c);
            var rootBlock   = await c.GetBestBlockAsync(GetBlockVerbosity.WithFullTx);

            var listener = new DummyChainListener(rootBlock.Block, (uint)rootBlock.Height, _output);

            var addr = await c.GetNewAddressAsync();

            await c.GenerateToAddressAsync(1, addr);

            var oldTipHash = await c.GetBestBlockHashAsync();

            var oldTip = await c.GetBlockAsync(oldTipHash, GetBlockVerbosity.WithFullTx);

            listener.BlockConnected(oldTip.Block, (uint)oldTip.Height);
            await c.InvalidateBlockAsync(oldTipHash);

            addr = await c.GetNewAddressAsync();

            await c.GenerateToAddressAsync(2, addr);

            var newTipHash = await c.GetBestBlockHashAsync();

            var newTip = await c.GetBlockAsync(newTipHash, GetBlockVerbosity.WithFullTx);

            Assert.True(oldTip.Height < newTip.Height);
            await listener.SyncChainListener(oldTipHash, newTip.Header, (uint)newTip.Height, blockSource, Network.RegTest, _loggerFactory.CreateLogger(nameof(ResumeChainListenerFromLowerFork)));

            Assert.True(listener.Blocks.Peek().ToBytes().SequenceEqual(newTip.Block.ToBytes()));

            var blockB = await c.GetBlockAsync(newTip.Block.Header.HashPrevBlock, GetBlockVerbosity.WithFullTx);

            Assert.True(listener.Blocks.Peek().ToBytes().SequenceEqual(newTip.Block.ToBytes()));
            Assert.True(listener.Blocks.Peek().ToBytes().SequenceEqual(newTip.Block.ToBytes()));
            Assert.Equal(listener.Blocks.Count - 2, blockB.Height);
            Assert.Equal(listener.Blocks.ElementAt(1).Header.GetHash(), blockB.Block.Header.GetHash());
            Assert.True(listener.Blocks.ElementAt(1).ToBytes().SequenceEqual(blockB.Block.ToBytes()));
        }