private async Task <bool> TryExecuteBlockAsync(Block block)
        {
            var blockHash = block.GetHash();

            var blockState = await _blockchainStateManager.GetBlockStateSetAsync(blockHash);

            if (blockState != null)
            {
                return(true);
            }

            var transactions = await _blockchainService.GetTransactionsAsync(block.TransactionIds);

            var executedBlock = await _blockExecutingService.ExecuteBlockAsync(block.Header, transactions);

            var blockHashWithoutCache = executedBlock.GetHashWithoutCache();

            if (blockHashWithoutCache != blockHash)
            {
                blockState = await _blockchainStateManager.GetBlockStateSetAsync(blockHashWithoutCache);

                Logger.LogWarning($"Block execution failed. BlockStateSet: {blockState}");
                Logger.LogWarning(
                    $"Block execution failed. Block header: {executedBlock.Header}, Block body: {executedBlock.Body}");

                return(false);
            }

            return(true);
        }
Example #2
0
        public async Task GlobalSetup()
        {
            _chains = GetRequiredService <IBlockchainStore <Chain> >();
            _chainStateInfoCollection = GetRequiredService <IStateStore <ChainStateInfo> >();
            _blockchainStateManager   = GetRequiredService <IBlockchainStateManager>();
            _blockchainStateService   = GetRequiredService <IBlockchainStateService>();
            _blockchainService        = GetRequiredService <IBlockchainService>();
            _osTestHelper             = GetRequiredService <OSTestHelper>();
            _chainManager             = GetRequiredService <IChainManager>();
            _blockManager             = GetRequiredService <IBlockManager>();
            _transactionManager       = GetRequiredService <ITransactionManager>();
            _transactionResultManager = GetRequiredService <ITransactionResultManager>();
            _txHub = GetRequiredService <ITxHub>();

            _blockStateSets = new List <BlockStateSet>();
            _blocks         = new List <Block>();

            _chain = await _blockchainService.GetChainAsync();

            var blockHash = _chain.BestChainHash;

            while (true)
            {
                var blockState = await _blockchainStateManager.GetBlockStateSetAsync(blockHash);

                _blockStateSets.Add(blockState);

                var blockHeader = await _blockchainService.GetBlockHeaderByHashAsync(blockHash);

                blockHash = blockHeader.PreviousBlockHash;
                if (blockHash == _chain.LastIrreversibleBlockHash)
                {
                    break;
                }
            }

            await _blockchainStateService.MergeBlockStateAsync(_chain.BestChainHeight, _chain.BestChainHash);

            for (var i = 0; i < BlockCount; i++)
            {
                var transactions = await _osTestHelper.GenerateTransferTransactions(TransactionCount);

                await _osTestHelper.BroadcastTransactions(transactions);

                var block = await _osTestHelper.MinedOneBlock();

                _blocks.Add(block);

                var blockState = await _blockchainStateManager.GetBlockStateSetAsync(block.GetHash());

                _blockStateSets.Add(blockState);
            }

            var chain = await _blockchainService.GetChainAsync();

            await _chainManager.SetIrreversibleBlockAsync(chain, chain.BestChainHash);

            _chainStateInfo = await _chainStateInfoCollection.GetAsync(chain.Id.ToStorageKey());
        }
Example #3
0
        /// <summary>
        /// Get the current state about a given block
        /// </summary>
        /// <param name="blockHash">block hash</param>
        /// <returns></returns>
        public async Task <BlockStateDto> GetBlockStateAsync(string blockHash)
        {
            var blockState = await _blockchainStateManager.GetBlockStateSetAsync(HashHelper.HexStringToHash(blockHash));

            if (blockState == null)
            {
                throw new UserFriendlyException(Error.Message[Error.NotFound], Error.NotFound.ToString());
            }
            return(JsonConvert.DeserializeObject <BlockStateDto>(blockState.ToString()));
        }
Example #4
0
        private async Task <bool> ExecuteBlock(ChainBlockLink blockLink, Block block)
        {
            var blockState = await _blockchainStateManager.GetBlockStateSetAsync(block.GetHash());

            if (blockState != null)
            {
                return(true);
            }

            var blockHash     = block.GetHash();
            var executedBlock = await _blockExecutingService.ExecuteBlockAsync(block.Header, block.Body.TransactionList);

            return(executedBlock.GetHash().Equals(blockHash));
        }
Example #5
0
        private async Task <bool> TryExecuteBlockAsync(Block block)
        {
            var blockHash = block.GetHash();

            var blockState = await _blockchainStateManager.GetBlockStateSetAsync(blockHash);

            if (blockState != null)
            {
                return(true);
            }

            var transactions = await _blockchainService.GetTransactionsAsync(block.TransactionIds);

            var executedBlock = await _blockExecutingService.ExecuteBlockAsync(block.Header, transactions);

            return(executedBlock.GetHashWithoutCache().Equals(blockHash));
        }
Example #6
0
        public async Task Get_Block_State_FewBlocks_Later()
        {
            var transactions = new List <Transaction>();

            for (int i = 0; i < 3; i++)
            {
                transactions.Add(await _osTestHelper.GenerateTransferTransaction());
            }

            await _osTestHelper.BroadcastTransactions(transactions);

            var block = await _osTestHelper.MinedOneBlock();

            //Continue generate block
            for (int i = 0; i < 10; i++)
            {
                await _osTestHelper.MinedOneBlock();
            }

            //Check Block State
            var blockState = await GetResponseAsObjectAsync <BlockStateDto>(
                $"/api/blockChain/blockState?blockHash={block.GetHash().ToHex()}");

            blockState.ShouldNotBeNull();
            blockState.BlockHash.ShouldBe(block.GetHash().ToHex());
            blockState.BlockHeight.ShouldBe(12);
            blockState.PreviousHash.ShouldBe(block.Header.PreviousBlockHash.ToHex());
            blockState.Changes.ShouldNotBeNull();

            var blockStateSet = await _blockchainStateManager.GetBlockStateSetAsync(block.GetHash());

            await _blockchainStateMergingService.MergeBlockStateAsync(blockStateSet.BlockHeight,
                                                                      blockStateSet.BlockHash);

            var errorResponse = await GetResponseAsObjectAsync <WebAppErrorResponse>(
                $"/api/blockChain/blockState?blockHash={block.GetHash().ToHex()}",
                expectedStatusCode : HttpStatusCode.Forbidden);

            errorResponse.Error.Code.ShouldBe(Error.NotFound.ToString());
            errorResponse.Error.Message.ShouldBe(Error.Message[Error.NotFound]);
        }