public async Task ValidateBlockBeforeExecute_Repackaged_ReturnFalse()
        {
            var transaction = _kernelTestHelper.GenerateTransaction();
            var block       = await _kernelTestHelper.AttachBlockToBestChain(new List <Transaction>
            {
                transaction
            });

            await _transactionBlockIndexService.UpdateTransactionBlockIndexAsync(new List <Hash> {
                transaction.GetHash()
            },
                                                                                 new BlockIndex
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            });

            var validateResult = await _blockValidationProvider.ValidateBlockBeforeExecuteAsync(block);

            validateResult.ShouldBeTrue();

            var repackagedBlock = await _kernelTestHelper.AttachBlockToBestChain(new List <Transaction>
            {
                transaction
            });

            validateResult = await _blockValidationProvider.ValidateBlockBeforeExecuteAsync(repackagedBlock);

            validateResult.ShouldBeFalse();
        }
Exemple #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);
        }
        private async Task AddTransactionResultsWithPostMiningAsync(Block block, IEnumerable <TransactionResult> results)
        {
            // Add block to chain
            var chain = await _blockchainService.GetChainAsync();

            await _blockchainService.AddBlockAsync(block);

            await _blockchainService.AttachBlockToChainAsync(chain, block);

            // TransactionResults are added during execution
            // Add TransactionResult before completing and adding block
            await _transactionResultService.AddTransactionResultsAsync(results.ToList(), block.Header);

            // Set best chain after execution
            await _blockchainService.SetBestChainAsync(chain, block.Height, block.GetHash());

            var blockIndex = new BlockIndex
            {
                BlockHash   = block.GetHash(),
                BlockHeight = block.Height
            };

            await _transactionBlockIndexService.UpdateTransactionBlockIndexAsync(block.Body.TransactionIds, blockIndex);
        }
Exemple #4
0
        public async Task ProcessTransactionResultAfterExecutionAsync(BlockHeader blockHeader,
                                                                      List <Hash> transactionIds)
        {
            var preMiningHash = blockHeader.GetPreMiningHash();
            var blockIndex    = new BlockIndex
            {
                BlockHash   = blockHeader.GetHash(),
                BlockHeight = blockHeader.Height
            };

            if (transactionIds.Count == 0)
            {
                // This will only happen during test environment
                return;
            }

            var firstTransaction = transactionIds.First();
            var withBlockHash    = await _transactionResultManager.HasTransactionResultAsync(
                firstTransaction, blockHeader.GetHash());

            var withPreMiningHash = await _transactionResultManager.HasTransactionResultAsync(
                firstTransaction, preMiningHash);

            if (!withBlockHash)
            {
                // TransactionResult is not saved with real BlockHash
                // Save results with real (post mining) Hash, so that it can be queried with TransactionBlockIndex
                var result = await _transactionResultManager.GetTransactionResultsAsync(transactionIds, preMiningHash);

                await _transactionResultManager.AddTransactionResultsAsync(result, blockIndex.BlockHash);
            }

            // Add TransactionBlockIndex
            if (withPreMiningHash)
            {
                await _transactionResultManager.RemoveTransactionResultsAsync(transactionIds, preMiningHash);
            }

            await _transactionBlockIndexService.UpdateTransactionBlockIndexAsync(transactionIds, blockIndex);
        }