Exemplo n.º 1
0
        public async Task <bool> ValidateTransactionBlockIndexExistsInBranchAsync(Hash txId, Hash chainBranchBlockHash)
        {
            if (!_transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId, out var transactionBlockIndex))
            {
                return(false);
            }
            var chain = await _blockchainService.GetChainAsync();

            return(await GetBlockIndexAsync(chain, transactionBlockIndex,
                                            chainBranchBlockHash ?? chain.BestChainHash) != null);
        }
Exemplo n.º 2
0
        public void TransactionBlockIndexCacheTest()
        {
            var txId1 = Hash.FromString("TxId1");
            var transactionBlockIndex1 = new TransactionBlockIndex
            {
                BlockHash   = Hash.FromString("BlockHash"),
                BlockHeight = 100
            };

            _transactionBlockIndexProvider.AddTransactionBlockIndex(txId1, transactionBlockIndex1);
            var getResult = _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId1, out var cachedTransactionBlockIndex);

            getResult.ShouldBeTrue();
            cachedTransactionBlockIndex.ShouldBe(transactionBlockIndex1);

            var txId2 = Hash.FromString("TxId2");
            var transactionBlockIndex2 = new TransactionBlockIndex
            {
                BlockHash   = Hash.FromString("BlockHash2"),
                BlockHeight = 101,
                PreviousExecutionBlockIndexList =
                {
                    new BlockIndex {
                        BlockHash = Hash.FromString("PreviousBlockHash"), BlockHeight = 100
                    }
                }
            };

            _transactionBlockIndexProvider.AddTransactionBlockIndex(txId2, transactionBlockIndex2);

            var txId3 = Hash.FromString("TxId3");
            var transactionBlockIndex3 = new TransactionBlockIndex
            {
                BlockHash   = Hash.FromString("BlockHash3"),
                BlockHeight = 101,
                PreviousExecutionBlockIndexList =
                {
                    new BlockIndex {
                        BlockHash = Hash.FromString("PreviousBlockHash2"), BlockHeight = 102
                    },
                    new BlockIndex {
                        BlockHash = Hash.FromString("PreviousBlockHash3"), BlockHeight = 103
                    }
                }
            };

            _transactionBlockIndexProvider.AddTransactionBlockIndex(txId3, transactionBlockIndex3);

            var transactionBlockIndex4 = new TransactionBlockIndex
            {
                BlockHash   = Hash.FromString("BlockHash4"),
                BlockHeight = 104,
                PreviousExecutionBlockIndexList =
                {
                    new BlockIndex {
                        BlockHash = Hash.FromString("PreviousBlockHash1"), BlockHeight = 101
                    },
                    new BlockIndex {
                        BlockHash = Hash.FromString("PreviousBlockHash2"), BlockHeight = 102
                    },
                    new BlockIndex {
                        BlockHash = Hash.FromString("PreviousBlockHash3"), BlockHeight = 103
                    }
                }
            };

            _transactionBlockIndexProvider.AddTransactionBlockIndex(txId3, transactionBlockIndex4);

            {
                _transactionBlockIndexProvider.CleanByHeight(99);
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId1, out var cacheBlockIndex1);
                cacheBlockIndex1.ShouldBe(transactionBlockIndex1);
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId2, out var cacheBlockIndex2);
                cacheBlockIndex2.ShouldBe(transactionBlockIndex2);
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId3, out var cacheBlockIndex3);
                cacheBlockIndex3.ShouldBe(transactionBlockIndex4);
            }

            {
                _transactionBlockIndexProvider.CleanByHeight(100);
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId1, out var cacheBlockIndex1);
                cacheBlockIndex1.ShouldBeNull();
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId2, out var cacheBlockIndex2);
                cacheBlockIndex2.ShouldBe(transactionBlockIndex2);
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId3, out var cacheBlockIndex3);
                cacheBlockIndex3.ShouldBe(transactionBlockIndex4);
            }

            {
                _transactionBlockIndexProvider.CleanByHeight(101);
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId1, out var cacheBlockIndex1);
                cacheBlockIndex1.ShouldBeNull();
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId2, out var cacheBlockIndex2);
                cacheBlockIndex2.ShouldBeNull();
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId3, out var cacheBlockIndex3);
                cacheBlockIndex3.ShouldBe(transactionBlockIndex4);
            }

            {
                _transactionBlockIndexProvider.CleanByHeight(102);
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId1, out var cacheBlockIndex1);
                cacheBlockIndex1.ShouldBeNull();
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId2, out var cacheBlockIndex2);
                cacheBlockIndex2.ShouldBeNull();
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId3, out var cacheBlockIndex3);
                cacheBlockIndex3.ShouldBe(transactionBlockIndex4);
            }

            {
                _transactionBlockIndexProvider.CleanByHeight(103);
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId1, out var cacheBlockIndex1);
                cacheBlockIndex1.ShouldBeNull();
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId2, out var cacheBlockIndex2);
                cacheBlockIndex2.ShouldBeNull();
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId3, out var cacheBlockIndex3);
                cacheBlockIndex3.ShouldNotBeNull();
            }

            {
                _transactionBlockIndexProvider.CleanByHeight(104);
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId1, out var cacheBlockIndex1);
                cacheBlockIndex1.ShouldBeNull();
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId2, out var cacheBlockIndex2);
                cacheBlockIndex2.ShouldBeNull();
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId3, out var cacheBlockIndex3);
                cacheBlockIndex3.ShouldBeNull();
            }
        }
        public async Task GetTransactionBlockIndexBelowLibTest()
        {
            var previousBlockHeader = _kernelTestHelper.BestBranchBlockList.Last().Header;
            var block1 =
                _kernelTestHelper.GenerateBlock(previousBlockHeader.Height, HashHelper.ComputeFrom("PreBlockHash1"));
            var block2 =
                _kernelTestHelper.GenerateBlock(previousBlockHeader.Height, previousBlockHeader.PreviousBlockHash);

            var txId  = HashHelper.ComputeFrom("Transaction");
            var chain = await _blockchainService.GetChainAsync();

            await AddBlockAsync(chain, block1);

            var blockIndex =
                await _transactionBlockIndexService.GetTransactionBlockIndexAsync(txId);

            blockIndex.ShouldBeNull();

            var blockIndex1 = new BlockIndex
            {
                BlockHash   = block1.GetHash(),
                BlockHeight = block1.Height
            };
            await _transactionBlockIndexService.AddBlockIndexAsync(new List <Hash> {
                txId
            }, blockIndex1);

            var blockIndex2 = new BlockIndex
            {
                BlockHash   = block2.GetHash(),
                BlockHeight = block2.Height
            };
            await _transactionBlockIndexService.AddBlockIndexAsync(new List <Hash> {
                txId
            }, blockIndex2);

            chain = await _blockchainService.GetChainAsync();
            await AddBlockAsync(chain, block2);
            await SetBestChain(chain, block2);

            {
                var existsOnForkBranch =
                    await _transactionBlockIndexService.ValidateTransactionBlockIndexExistsInBranchAsync(txId,
                                                                                                         block1.GetHash());

                Assert.True(existsOnForkBranch);

                var actualBlockIndexOnBestChain =
                    await _transactionBlockIndexService.GetTransactionBlockIndexAsync(txId);

                Assert.Equal(blockIndex2, actualBlockIndexOnBestChain);

                var transactionBlockIndex = await _transactionBlockIndexManager.GetTransactionBlockIndexAsync(txId);

                transactionBlockIndex.BlockHash.ShouldBe(blockIndex2.BlockHash);
                transactionBlockIndex.BlockHeight.ShouldBe(blockIndex2.BlockHeight);
                transactionBlockIndex.PreviousExecutionBlockIndexList.Count.ShouldBe(1);
                transactionBlockIndex.PreviousExecutionBlockIndexList[0].BlockHash.ShouldBe(blockIndex1.BlockHash);
                transactionBlockIndex.PreviousExecutionBlockIndexList[0].BlockHeight.ShouldBe(blockIndex1.BlockHeight);

                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId, out var transactionBlockIndexCache)
                .ShouldBeTrue();
                transactionBlockIndexCache.ShouldBe(transactionBlockIndex);
            }

            var i            = 0;
            var currentBlock = block2;

            while (i++ < KernelConstants.ReferenceBlockValidPeriod)
            {
                var block = await _kernelTestHelper.AttachBlock(currentBlock.Height, currentBlock.GetHash());

                // await AddBlockAsync(chain, block);
                currentBlock = block;
            }

            await SetBestChain(chain, currentBlock);

            await _blockchainService.SetIrreversibleBlockAsync(chain, currentBlock.Height, currentBlock.GetHash());

            await _transactionBlockIndexService.UpdateTransactionBlockIndicesByLibHeightAsync(currentBlock.Height);

            {
                _transactionBlockIndexProvider.TryGetTransactionBlockIndex(txId, out _).ShouldBeFalse();

                var existsOnForkBranch =
                    await _transactionBlockIndexService.ValidateTransactionBlockIndexExistsInBranchAsync(txId,
                                                                                                         block1.GetHash());

                Assert.False(existsOnForkBranch);
                var actualBlockIndexOnBestChain =
                    await _transactionBlockIndexService.GetTransactionBlockIndexAsync(txId);

                Assert.Equal(blockIndex2, actualBlockIndexOnBestChain);

                var transactionBlockIndex = await _transactionBlockIndexManager.GetTransactionBlockIndexAsync(txId);

                transactionBlockIndex.BlockHash.ShouldBe(blockIndex2.BlockHash);
                transactionBlockIndex.BlockHeight.ShouldBe(blockIndex2.BlockHeight);
                transactionBlockIndex.PreviousExecutionBlockIndexList.Count.ShouldBe(0);
            }
        }