示例#1
0
        /// <summary>
        /// Mock a chain with a best branch, and some fork branches
        /// </summary>
        /// <returns>
        ///       Mock Chain
        ///    BestChainHeight: 11
        /// LongestChainHeight: 13
        ///         LIB height: 5
        ///
        ///             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14 -> 15 -> 16 -> 17 -> 18 -> 19
        ///        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
        ///     Longest Branch:                                   (h)-> l -> m  -> n  -> o  ->  p ->  q ->  r ->  s ->  t ->  u ->  v
        ///        Fork Branch:                    (e)-> q -> r -> s -> t -> u
        ///    Unlinked Branch:                                              v  -> w  -> x  -> y  -> z
        /// </returns>
        public async Task <Chain> MockChainAsync()
        {
            var chain = await CreateChain();

            var genesisBlock = await _blockchainService.GetBlockByHashAsync(chain.GenesisBlockHash);

            BestBranchBlockList.Add(genesisBlock);

            BestBranchBlockList.AddRange(await AddBestBranch());

            LongestBranchBlockList =
                await AddForkBranch(BestBranchBlockList[7].Height, BestBranchBlockList[7].GetHash(), 11);

            foreach (var block in LongestBranchBlockList)
            {
                var chainBlockLink = await _chainManager.GetChainBlockLinkAsync(block.GetHash());

                await _chainManager.SetChainBlockLinkExecutionStatusAsync(chainBlockLink,
                                                                          ChainBlockLinkExecutionStatus.ExecutionFailed);
            }

            ForkBranchBlockList =
                await AddForkBranch(BestBranchBlockList[4].Height, BestBranchBlockList[4].GetHash());

            UnlinkedBranchBlockList = await AddForkBranch(9, Hash.FromString("UnlinkBlock"));

            // Set lib
            chain = await _blockchainService.GetChainAsync();

            await _blockchainService.SetIrreversibleBlockAsync(chain, BestBranchBlockList[4].Height,
                                                               BestBranchBlockList[4].GetHash());

            return(chain);
        }
示例#2
0
        /// <summary>
        /// Returns the block with the specified height, searching from <see cref="chainBranchBlockHash"/>. If the height
        /// is in the irreversible section of the chain, it will get the block from the indexed blocks.
        /// </summary>
        /// <param name="chain">the chain to search</param>
        /// <param name="height">the height of the block</param>
        /// <param name="chainBranchBlockHash">the block from which to start the search.</param>
        /// <returns></returns>
        public async Task <Hash> GetBlockHashByHeightAsync(Chain chain, long height, Hash chainBranchBlockHash)
        {
            if (chain.LastIrreversibleBlockHeight >= height)
            {
                // search irreversible section of the chain
                return((await _chainManager.GetChainBlockIndexAsync(height)).BlockHash);
            }

            // TODO: may introduce cache to improve the performance

            var chainBlockLink = await _chainManager.GetChainBlockLinkAsync(chainBranchBlockHash);

            if (chainBlockLink.Height < height)
            {
                return(null);
            }
            while (true)
            {
                if (chainBlockLink.Height == height)
                {
                    return(chainBlockLink.BlockHash);
                }

                chainBranchBlockHash = chainBlockLink.PreviousBlockHash;
                chainBlockLink       = await _chainManager.GetChainBlockLinkAsync(chainBranchBlockHash);

                if (chainBlockLink == null)
                {
                    return(null);
                }
            }
        }
        public async Task ExecuteBlocksAttachedToLongestChain_ExecuteFailed()
        {
            var chain = await _blockchainService.GetChainAsync();

            var bestChainHeight = chain.BestChainHeight;
            var bestChainHash   = chain.BestChainHash;

            var newBlock = _kernelTestHelper.GenerateBlock(chain.BestChainHeight, chain.BestChainHash,
                                                           new List <Transaction> {
                _kernelTestHelper.GenerateTransaction()
            });

            await _blockchainService.AddBlockAsync(newBlock);

            var status = await _blockchainService.AttachBlockToChainAsync(chain, newBlock);

            var attachResult =
                await _fullBlockchainExecutingService.ExecuteBlocksAttachedToLongestChain(chain, status);

            attachResult.Count.ShouldBe(1);
            attachResult.Last().Height.ShouldBe(16);
            attachResult.Last().BlockHash.ShouldBe(newBlock.GetHash());

            chain = await _blockchainService.GetChainAsync();

            var newBlockLink = await _chainManager.GetChainBlockLinkAsync(newBlock.GetHash());

            newBlockLink.ExecutionStatus.ShouldBe(ChainBlockLinkExecutionStatus.ExecutionFailed);
            chain.BestChainHash.ShouldBe(bestChainHash);
            chain.BestChainHeight.ShouldBe(bestChainHeight);
        }
示例#4
0
        public async Task ExecuteBlocksAttachedToLongestChain_ValidateFailed()
        {
            var chain = await _blockchainService.GetChainAsync();

            var bestChainHeight = chain.BestChainHeight;
            var bestChainHash   = chain.BestChainHash;

            var previousHash   = chain.BestChainHash;
            var previousHeight = chain.BestChainHeight;

            BlockAttachOperationStatus status = BlockAttachOperationStatus.None;
            var blockList = new List <Block>();
//            Block lastBlock = null;
            int count = 0;

            while (!status.HasFlag(BlockAttachOperationStatus.LongestChainFound))
            {
                var transactions = new List <Transaction> {
                    _kernelTestHelper.GenerateTransaction()
                };
                var lastBlock = _kernelTestHelper.GenerateBlock(previousHeight, previousHash, transactions);

                await _blockchainService.AddBlockAsync(lastBlock);

                await _blockchainService.AddTransactionsAsync(transactions);

                status = await _blockchainService.AttachBlockToChainAsync(chain, lastBlock);

                count++;
                previousHash   = lastBlock.GetHash();
                previousHeight = lastBlock.Height;
                blockList.Add(lastBlock);
            }

            var attachResult =
                await _fullBlockchainExecutingService.ExecuteBlocksAttachedToLongestChain(chain, status);

            attachResult.ShouldBeNull();

            chain = await _blockchainService.GetChainAsync();

            var newBlockLink = await _chainManager.GetChainBlockLinkAsync(blockList.First().GetHash());

            newBlockLink.ExecutionStatus.ShouldBe(ChainBlockLinkExecutionStatus.ExecutionFailed);
            chain.BestChainHash.ShouldBe(bestChainHash);
            chain.BestChainHeight.ShouldBe(bestChainHeight);
            chain.LongestChainHash.ShouldBe(bestChainHash);
            chain.LongestChainHeight.ShouldBe(bestChainHeight);
            chain.Branches.ShouldNotContainKey(previousHash.ToStorageKey());
        }
        public async Task Process_Success_BlockExecutionResult()
        {
            var chain = await _blockchainService.GetChainAsync();

            await _blockchainService.RemoveLongestBranchAsync(chain);

            var block = _kernelTestHelper.GenerateBlock(chain.BestChainHeight, chain.BestChainHash);
            await _blockchainService.AttachBlockToChainAsync(chain, block);

            chain = await _blockchainService.GetChainAsync();

            var block2 = _kernelTestHelper.GenerateBlock(block.Height, block.GetHash());
            await _blockchainService.AttachBlockToChainAsync(chain, block2);

            var executionResult = new BlockExecutionResult
            {
                SuccessBlockExecutedSets = { new BlockExecutedSet {
                                                 Block = block
                                             }, new BlockExecutedSet{
                                                 Block = block2
                                             } }
            };
            await _blockExecutionResultProcessingService.ProcessBlockExecutionResultAsync(chain, executionResult);

            chain = await _blockchainService.GetChainAsync();

            chain.BestChainHeight.ShouldBe(block2.Height);
            chain.BestChainHash.ShouldBe(block2.GetHash());

            var chainBlockLink = await _chainManager.GetChainBlockLinkAsync(block.GetHash());

            chainBlockLink.ExecutionStatus.ShouldBe(ChainBlockLinkExecutionStatus.ExecutionSuccess);
            var chainBlockLink2 = await _chainManager.GetChainBlockLinkAsync(block2.GetHash());

            chainBlockLink2.ExecutionStatus.ShouldBe(ChainBlockLinkExecutionStatus.ExecutionSuccess);
        }
示例#6
0
        /// <summary>
        /// Returns the block with the specified height, searching from <see cref="chainBranchBlockHash"/>. If the height
        /// is in the irreversible section of the chain, it will get the block from the indexed blocks.
        /// </summary>
        /// <param name="chain">the chain to search</param>
        /// <param name="height">the height of the block</param>
        /// <param name="chainBranchBlockHash">the block from which to start the search.</param>
        /// <returns></returns>
        public async Task <Hash> GetBlockHashByHeightAsync(Chain chain, long height, Hash chainBranchBlockHash)
        {
            // 1 2 3 4(lib) 5 6
            // 4 >= height
            // return any(1,2,3,4)

            if (chain.LastIrreversibleBlockHeight >= height)
            {
                // search irreversible section of the chain
                return((await _chainManager.GetChainBlockIndexAsync(height)).BlockHash);
            }

            // Last irreversible block can make the loops in acceptable size, do not need cache

            // 1 2 3 4(lib) 5 6
            // 4 < height
            // return any(5,6)


            var chainBlockLink = await _chainManager.GetChainBlockLinkAsync(chainBranchBlockHash);

            if (chainBlockLink.Height < height)
            {
                Logger.LogWarning(
                    $"Start searching height: {chainBlockLink.Height},target height: {height},cannot get block hash");
                return(null);
            }

            while (true)
            {
                if (chainBlockLink.Height == height)
                {
                    return(chainBlockLink.BlockHash);
                }

                chainBranchBlockHash = chainBlockLink.PreviousBlockHash;
                chainBlockLink       = await _chainManager.GetChainBlockLinkAsync(chainBranchBlockHash);

                if (chainBlockLink == null || chainBlockLink.Height <= chain.LastIrreversibleBlockHeight)
                {
                    return(null);
                }
            }
        }
        public async Task ExecuteBlocksAttachedToLongestChain_ValidateFailed()
        {
            var chain = await _blockchainService.GetChainAsync();

            var bestChainHeight = chain.BestChainHeight;
            var bestChainHash   = chain.BestChainHash;

            var transactions = new List <Transaction> {
                _kernelTestHelper.GenerateTransaction()
            };

            var newBlock = _kernelTestHelper.GenerateBlock(chain.BestChainHeight, chain.BestChainHash,
                                                           transactions);

            await _blockchainService.AddBlockAsync(newBlock);

            await _blockchainService.AddTransactionsAsync(transactions);

            var status = await _blockchainService.AttachBlockToChainAsync(chain, newBlock);

            chain = await _blockchainService.GetChainAsync();

            chain.LongestChainHash.ShouldBe(newBlock.GetHash());
            chain.LongestChainHeight.ShouldBe(newBlock.Height);
            chain.Branches.ShouldContainKey(newBlock.GetHash().ToStorageKey());

            var attachResult =
                await _fullBlockchainExecutingService.ExecuteBlocksAttachedToLongestChain(chain, status);

            attachResult.ShouldBeNull();

            chain = await _blockchainService.GetChainAsync();

            var newBlockLink = await _chainManager.GetChainBlockLinkAsync(newBlock.GetHash());

            newBlockLink.ExecutionStatus.ShouldBe(ChainBlockLinkExecutionStatus.ExecutionFailed);
            chain.BestChainHash.ShouldBe(bestChainHash);
            chain.BestChainHeight.ShouldBe(bestChainHeight);
            chain.LongestChainHash.ShouldBe(bestChainHash);
            chain.LongestChainHeight.ShouldBe(bestChainHeight);
            chain.Branches.ShouldNotContainKey(newBlock.GetHash().ToStorageKey());
        }
示例#8
0
        public async Task Set_IrreversibleBlock_Concurrence()
        {
            var chain = await _fullBlockchainService.GetChainAsync();

            //         LIB height: 8
            //
            //             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
            //        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
            //     Longest Branch:                                   (h)-> l -> m  -> n  -> o  -> p
            //        Fork Branch:                    (e)-> q -> r -> s -> t -> u
            //    Unlinked Branch:                                    ae   v -> w  -> x  -> y  -> z
            await _fullBlockchainService.SetIrreversibleBlockAsync(chain, _kernelTestHelper.BestBranchBlockList[7]
                                                                   .Height, _kernelTestHelper.BestBranchBlockList[7].GetHash());

            var newUnlinkedBlock = _kernelTestHelper.GenerateBlock(7, Hash.FromString("NewUnlinked"),
                                                                   new List <Transaction> {
                _kernelTestHelper.GenerateTransaction()
            });
            await _fullBlockchainService.AddBlockAsync(newUnlinkedBlock);

            await _fullBlockchainService.AttachBlockToChainAsync(chain, newUnlinkedBlock);

            var previousBlockHeight = _kernelTestHelper.BestBranchBlockList.Last().Height;
            var previousBlockHash   = _kernelTestHelper.BestBranchBlockList.Last().GetHash();

            // Miner mined one block
            var minerAttachBlock = await _kernelTestHelper.AttachBlock(previousBlockHeight, previousBlockHash);

            chain = await _fullBlockchainService.GetChainAsync();

            await _fullBlockchainService.SetBestChainAsync(chain, minerAttachBlock.Height,
                                                           minerAttachBlock.GetHash());

            var chainBlockLink = await _chainManager.GetChainBlockLinkAsync(minerAttachBlock.GetHash());

            await _chainManager.SetChainBlockLinkExecutionStatus(chainBlockLink,
                                                                 ChainBlockLinkExecutionStatus.ExecutionSuccess);

            var minerAttachChain = await _fullBlockchainService.GetChainAsync();

            // Network sync two blocks
            Block syncAttachBlock = null;

            for (var i = 0; i < 2; i++)
            {
                syncAttachBlock = await _kernelTestHelper.AttachBlock(previousBlockHeight, previousBlockHash);

                chain = await _fullBlockchainService.GetChainAsync();

                await _fullBlockchainService.SetBestChainAsync(chain, syncAttachBlock.Height,
                                                               syncAttachBlock.GetHash());

                chainBlockLink = await _chainManager.GetChainBlockLinkAsync(syncAttachBlock.GetHash());

                await _chainManager.SetChainBlockLinkExecutionStatus(chainBlockLink,
                                                                     ChainBlockLinkExecutionStatus.ExecutionSuccess);

                previousBlockHeight = syncAttachBlock.Height;
                previousBlockHash   = syncAttachBlock.GetHash();
            }
            var syncAttachChain = await _fullBlockchainService.GetChainAsync();

            {
                //         LIB height: 9
                //
                //             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
                //        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
                //     Longest Branch: (-)                               (h)-> l -> m  -> n  -> o  -> p
                //        Fork Branch: (-)                (e)-> q -> r -> s -> t -> u
                //    Unlinked Branch:                                   ae(-) v  -> w  -> x  -> y  -> z
                //       Miner Branch:                                                    (k) -> aa
                //Network Sync Branch:                                                    (k) -> ab -> ac
                syncAttachChain.NotLinkedBlocks.Count.ShouldBe(6);
                BlocksShouldExist(new List <Hash> {
                    newUnlinkedBlock.GetHash()
                });

                await _fullBlockchainService.SetIrreversibleBlockAsync(syncAttachChain, _kernelTestHelper
                                                                       .BestBranchBlockList[8].Height, _kernelTestHelper.BestBranchBlockList[8].GetHash());

                chain = await _fullBlockchainService.GetChainAsync();

                chain.LastIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[8].GetHash());
                chain.LastIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[8].Height);
                chain.Branches.Count.ShouldBe(3);
                chain.NotLinkedBlocks.Count.ShouldBe(5);
                BlocksShouldNotExist(_kernelTestHelper.ForkBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldExist(_kernelTestHelper.BestBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldExist(_kernelTestHelper.LongestBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldExist(_kernelTestHelper.UnlinkedBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldNotExist(new List <Hash> {
                    newUnlinkedBlock.GetHash()
                });
            }

            {
                //         LIB height: 10
                //
                //             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
                //        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
                //     Longest Branch: (-)                               (h)-> l -> m  -> n  -> o  -> p
                //        Fork Branch: (-)                (e)-> q -> r -> s -> t -> u  -> ad
                //    Unlinked Branch:                                   ae(-) v  -> w  -> x  -> y  -> z
                //       Miner Branch:                                                    (k) -> aa
                //Network Sync Branch: (-)                                                (k) -> ab -> ac
                var newBlock = _kernelTestHelper.GenerateBlock(_kernelTestHelper.ForkBranchBlockList.Last().Height,
                                                               _kernelTestHelper.ForkBranchBlockList.Last().GetHash(),
                                                               new List <Transaction> {
                    _kernelTestHelper.GenerateTransaction()
                });
                await _fullBlockchainService.AddBlockAsync(newBlock);

                await _fullBlockchainService.AttachBlockToChainAsync(minerAttachChain, newBlock);

                minerAttachChain.Branches.Count.ShouldBe(3);

                await _fullBlockchainService.SetIrreversibleBlockAsync(minerAttachChain, _kernelTestHelper
                                                                       .BestBranchBlockList[9].Height, _kernelTestHelper.BestBranchBlockList[9].GetHash());

                chain = await _fullBlockchainService.GetChainAsync();

                chain.LastIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[9].Height);
                chain.LastIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[9].GetHash());
                chain.Branches.Count.ShouldBe(2);
                chain.NotLinkedBlocks.Count.ShouldBe(5);
                BlocksShouldNotExist(_kernelTestHelper.ForkBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldExist(_kernelTestHelper.BestBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldExist(_kernelTestHelper.LongestBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldExist(_kernelTestHelper.UnlinkedBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldNotExist(new List <Hash> {
                    newBlock.GetHash()
                });
            }

            {
                //         LIB height: 11
                //
                //             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
                //        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
                //     Longest Branch: (-)                               (h)-> l -> m  -> n  -> o  -> p
                //        Fork Branch: (-)                (e)-> q -> r -> s -> t -> u
                //    Unlinked Branch:                                   ae(-) v(-)-> w  -> x  -> y  -> z
                //       Miner Branch:                                                    (k) -> aa
                //Network Sync Branch: (-)                                                (k) -> ab -> ac

                await _fullBlockchainService.SetIrreversibleBlockAsync(chain, _kernelTestHelper
                                                                       .BestBranchBlockList[10].Height, _kernelTestHelper.BestBranchBlockList[10].GetHash());

                chain = await _fullBlockchainService.GetChainAsync();

                chain.LastIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].Height);
                chain.LastIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].GetHash());
                chain.LongestChainHash.ShouldBe(minerAttachBlock.GetHash());
                chain.LongestChainHeight.ShouldBe(minerAttachBlock.Height);
                chain.Branches.Count.ShouldBe(1);
                chain.NotLinkedBlocks.Count.ShouldBe(4);
                BlocksShouldNotExist(_kernelTestHelper.ForkBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldExist(_kernelTestHelper.BestBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldNotExist(_kernelTestHelper.LongestBranchBlockList.Select(b => b.GetHash()).ToList());
                BlocksShouldNotExist(new List <Hash> {
                    _kernelTestHelper.UnlinkedBranchBlockList[0].GetHash()
                });
                BlocksShouldExist(new List <Hash>
                {
                    _kernelTestHelper.UnlinkedBranchBlockList[1].GetHash(),
                    _kernelTestHelper.UnlinkedBranchBlockList[2].GetHash(),
                    _kernelTestHelper.UnlinkedBranchBlockList[3].GetHash(),
                    _kernelTestHelper.UnlinkedBranchBlockList[4].GetHash()
                });
            }
        }
        public async Task Set_IrreversibleBlock_Test()
        {
            NewIrreversibleBlockFoundEvent eventData = null;

            _eventBus.Subscribe <NewIrreversibleBlockFoundEvent>(d =>
            {
                eventData = d;
                return(Task.CompletedTask);
            });

            var chain = await _fullBlockchainService.GetChainAsync();

            {
                //         LIB height: 7
                //
                //             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
                //        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
                //     Longest Branch:                                   (h)-> l -> m  -> n  -> o  -> p
                //        Fork Branch:                    (e)-> q -> r -> s -> t -> u
                //    Unlinked Branch:                                              v  -> w  -> x  -> y  -> z
                await _fullBlockchainService.SetIrreversibleBlockAsync(chain, _kernelTestHelper.BestBranchBlockList[6]
                                                                       .Height, _kernelTestHelper.BestBranchBlockList[6].GetHash());

                chain = await _fullBlockchainService.GetChainAsync();

                chain.LastIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].GetHash());
                chain.LastIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].Height);

                eventData.ShouldNotBeNull();
                eventData.BlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].GetHash());
                eventData.BlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].Height);
                eventData.PreviousIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[4].GetHash());
                eventData.PreviousIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[4].Height);

                var blockLink =
                    await _chainManager.GetChainBlockLinkAsync(_kernelTestHelper.BestBranchBlockList[6].GetHash());

                while (blockLink != null)
                {
                    blockLink.IsIrreversibleBlock.ShouldBeTrue();
                    var chainBlockIndex = await _chainManager.GetChainBlockIndexAsync(blockLink.Height);

                    chainBlockIndex.BlockHash.ShouldBe(blockLink.BlockHash);

                    blockLink = await _chainManager.GetChainBlockLinkAsync(blockLink.PreviousBlockHash);
                }
            }

            {
                //         LIB height: 11
                //
                //             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
                //        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
                //     Longest Branch: (-)                               (h)-> l -> m  -> n  -> o  -> p
                //        Fork Branch: (-)                (e)-> q -> r -> s -> t -> u
                //    Unlinked Branch:                                             v(-) -> w  -> x  -> y  -> z
                eventData = null;
                await _fullBlockchainService.SetIrreversibleBlockAsync(chain, _kernelTestHelper.BestBranchBlockList[10]
                                                                       .Height, _kernelTestHelper.BestBranchBlockList[10].GetHash());

                chain = await _fullBlockchainService.GetChainAsync();

                chain.LastIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].GetHash());
                chain.LastIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].Height);

                eventData.ShouldNotBeNull();
                eventData.BlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].GetHash());
                eventData.BlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].Height);
                eventData.PreviousIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].GetHash());
                eventData.PreviousIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].Height);

                var blockLink =
                    await _chainManager.GetChainBlockLinkAsync(_kernelTestHelper.BestBranchBlockList[10].GetHash());

                while (blockLink != null)
                {
                    blockLink.IsIrreversibleBlock.ShouldBeTrue();
                    var chainBlockIndex = await _chainManager.GetChainBlockIndexAsync(blockLink.Height);

                    chainBlockIndex.BlockHash.ShouldBe(blockLink.BlockHash);

                    blockLink = await _chainManager.GetChainBlockLinkAsync(blockLink.PreviousBlockHash);
                }
            }

            {
                // Set lib failed
                eventData = null;
                await _fullBlockchainService.SetIrreversibleBlockAsync(chain, _kernelTestHelper.BestBranchBlockList[9]
                                                                       .Height, _kernelTestHelper.BestBranchBlockList[9].GetHash());

                eventData.ShouldBeNull();
            }
        }
示例#10
0
        public async Task Set_IrreversibleBlock_Test()
        {
            var chain = await _fullBlockchainService.GetChainAsync();

            {
                //         LIB height: 7
                //
                //             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
                //        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
                //     Longest Branch:                                   (h)-> l -> m  -> n  -> o  -> p
                //        Fork Branch:                    (e)-> q -> r -> s -> t -> u
                //    Unlinked Branch:                                              v  -> w  -> x  -> y  -> z
                await _fullBlockchainService.SetIrreversibleBlockAsync(chain, _kernelTestHelper.BestBranchBlockList[6]
                                                                       .Height, _kernelTestHelper.BestBranchBlockList[6].GetHash());

                chain = await _fullBlockchainService.GetChainAsync();

                chain.LastIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].GetHash());
                chain.LastIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].Height);

                var blockLink =
                    await _chainManager.GetChainBlockLinkAsync(_kernelTestHelper.BestBranchBlockList[6].GetHash());

                while (blockLink != null)
                {
                    blockLink.IsIrreversibleBlock.ShouldBeTrue();
                    var chainBlockIndex = await _chainManager.GetChainBlockIndexAsync(blockLink.Height);

                    chainBlockIndex.BlockHash.ShouldBe(blockLink.BlockHash);

                    blockLink = await _chainManager.GetChainBlockLinkAsync(blockLink.PreviousBlockHash);
                }
            }

            {
                //         LIB height: 11
                //
                //             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
                //        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
                //     Longest Branch: (-)                               (h)-> l -> m  -> n  -> o  -> p
                //        Fork Branch: (-)                (e)-> q -> r -> s -> t -> u
                //    Unlinked Branch:                                             v(-) -> w  -> x  -> y  -> z
                await _fullBlockchainService.SetIrreversibleBlockAsync(chain, _kernelTestHelper.BestBranchBlockList[10]
                                                                       .Height, _kernelTestHelper.BestBranchBlockList[10].GetHash());

                chain = await _fullBlockchainService.GetChainAsync();

                chain.LastIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].GetHash());
                chain.LastIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].Height);

                var blockLink =
                    await _chainManager.GetChainBlockLinkAsync(_kernelTestHelper.BestBranchBlockList[10].GetHash());

                while (blockLink != null)
                {
                    blockLink.IsIrreversibleBlock.ShouldBeTrue();
                    var chainBlockIndex = await _chainManager.GetChainBlockIndexAsync(blockLink.Height);

                    chainBlockIndex.BlockHash.ShouldBe(blockLink.BlockHash);

                    blockLink = await _chainManager.GetChainBlockLinkAsync(blockLink.PreviousBlockHash);
                }
            }
        }
示例#11
0
        public async Task SetChainBlockLinkExecutionStatusAsync(Hash blockHash, ChainBlockLinkExecutionStatus status)
        {
            var chainBlockLink = await _chainManager.GetChainBlockLinkAsync(blockHash);

            await _chainManager.SetChainBlockLinkExecutionStatusAsync(chainBlockLink, status);
        }