Esempio n. 1
0
        public async Task <bool> ValidateBlockBeforeExecuteAsync(IBlock block)
        {
            if (block?.Header == null || block.Body == null)
            {
                return(false);
            }

            if (block.Body.TransactionsCount == 0)
            {
                return(false);
            }

            // Verify that the transaction has been packaged in the current branch
            foreach (var transactionId in block.TransactionIds)
            {
                var blockIndex =
                    await _transactionBlockIndexService.GetCachedTransactionBlockIndexAsync(transactionId,
                                                                                            block.Header.PreviousBlockHash);

                if (blockIndex != null)
                {
                    Logger.LogWarning($"Transaction: {transactionId} repackaged.");
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 2
0
        public async Task UpdateOneBlockIndexWithoutBestChain()
        {
            var previousBlockHeader = _kernelTestHelper.BestBranchBlockList.Last().Header;
            var block = _kernelTestHelper.GenerateBlock(previousBlockHeader.Height, Hash.FromString("PreBlockHash"));
            var chain = await _blockchainService.GetChainAsync();

            await AddBlockAsync(chain, block);

            var txId       = Hash.FromString("Transaction");
            var blockIndex = new BlockIndex
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            };
            await _transactionBlockIndexService.UpdateTransactionBlockIndexAsync(new List <Hash> {
                txId
            }, blockIndex);

            var actual = await _transactionBlockIndexService.GetTransactionBlockIndexAsync(txId);

            Assert.Null(actual);

            var cacheInBestBranch = await _transactionBlockIndexService.GetCachedTransactionBlockIndexAsync(txId);

            Assert.Null(cacheInBestBranch);

            var cacheInForkBranch =
                await _transactionBlockIndexService.GetCachedTransactionBlockIndexAsync(txId, block.GetHash());

            Assert.NotNull(cacheInForkBranch);
        }