private void CheckBlockExecutedData(BlockStateSet blockStateSet, Chain chain,
                                            TransactionResult transactionResult, Dictionary <string, Transaction> transactionDic)
        {
            var chainContext = new ChainContext
            {
                BlockHash   = blockStateSet.BlockHash,
                BlockHeight = blockStateSet.BlockHeight
            };
            var chainFromBlockExecutedData =
                _chainBlockchainExecutedDataService.GetBlockExecutedData(chainContext,
                                                                         GetBlockExecutedDataKey <Chain>());

            chainFromBlockExecutedData.ShouldBe(chain);

            var transactionResultFromBlockExecutedData =
                _transactionResultBlockchainExecutedDataService.GetBlockExecutedData(chainContext,
                                                                                     GetBlockExecutedDataKey <TransactionResult>(transactionResult.TransactionId));

            transactionResultFromBlockExecutedData.ShouldBe(transactionResult);
            foreach (var keyPair in transactionDic)
            {
                var transaction =
                    _transactionBlockchainExecutedDataService.GetBlockExecutedData(chainContext, keyPair.Key);
                transaction.ShouldBe(keyPair.Value);
            }
        }
示例#2
0
        private void CheckBlockExecutedData(BlockStateSet blockStateSet,
                                            Dictionary <string, CalculateFunction> functionMap)
        {
            var chainContext = new ChainContext
            {
                BlockHash   = blockStateSet.BlockHash,
                BlockHeight = blockStateSet.BlockHeight
            };
            var functionMapFromBlockExecutedData =
                _calculateFunctionExecutedDataService.GetBlockExecutedData(chainContext,
                                                                           GetBlockExecutedDataKey());

            foreach (var key in functionMap.Keys)
            {
                var fromExecutedData = functionMapFromBlockExecutedData.Values.Single(d =>
                                                                                      ((FeeTypeEnum)d.CalculateFeeCoefficients.FeeTokenType).ToString().ToUpper() == key);
                var actual = functionMap.Values.Single(d =>
                                                       ((FeeTypeEnum)d.CalculateFeeCoefficients.FeeTokenType).ToString().ToUpper() == key);
                fromExecutedData.CalculateFeeCoefficients.ShouldBe(actual.CalculateFeeCoefficients);
            }
        }
示例#3
0
 protected T GetBlockExecutedData(IBlockIndex chainContext, IMessage key = null)
 {
     return(_cachedBlockchainExecutedDataService.GetBlockExecutedData(chainContext, GetBlockExecutedDataKey(key)));
 }
        public async Task BlockExecutedData_Test()
        {
            var genesisBlock = _kernelTestHelper.GenerateBlock(0, Hash.Empty, new List <Transaction>());
            var chain        = await _blockchainService.CreateChainAsync(genesisBlock, new List <Transaction>());

            var blockStateSet = new BlockStateSet
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            };
            await _blockStateSetManger.SetBlockStateSetAsync(blockStateSet);

            var transactionDic = new Dictionary <string, Transaction>();

            for (var i = 0; i < 5; i++)
            {
                var transaction = new Transaction
                {
                    From           = SampleAddress.AddressList[i],
                    To             = SampleAddress.AddressList[i + 1],
                    RefBlockNumber = chain.BestChainHeight - 1,
                    MethodName     = "Test"
                };
                transactionDic.Add(GetBlockExecutedDataKey <Transaction>(transaction.GetHash()), transaction);
            }

            await _transactionBlockchainExecutedDataService.AddBlockExecutedDataAsync(new BlockIndex
            {
                BlockHash   = blockStateSet.BlockHash,
                BlockHeight = blockStateSet.BlockHeight
            }, transactionDic);

            var transactionResult = new TransactionResult
            {
                TransactionId = transactionDic.First().Value.GetHash()
            };
            var transactionResultKey = GetBlockExecutedDataKey <TransactionResult>(transactionResult.TransactionId);
            await _transactionResultBlockchainExecutedDataService.AddBlockExecutedDataAsync(new BlockIndex
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            },
                                                                                            transactionResultKey,
                                                                                            transactionResult);

            var chainKey = GetBlockExecutedDataKey <Chain>();
            await _chainBlockchainExecutedDataService.AddBlockExecutedDataAsync(new BlockIndex
            {
                BlockHash   = blockStateSet.BlockHash,
                BlockHeight = blockStateSet.BlockHeight
            },
                                                                                chainKey, chain);

            CheckBlockExecutedDataCache(new BlockIndex
            {
                BlockHash   = blockStateSet.BlockHash,
                BlockHeight = blockStateSet.BlockHeight
            }, chain, transactionResult, transactionDic, false, false);

            var newBlockStateSet = await _blockStateSetManger.GetBlockStateSetAsync(chain.BestChainHash);

            newBlockStateSet.BlockHash.ShouldBe(blockStateSet.BlockHash);
            newBlockStateSet.BlockHeight.ShouldBe(blockStateSet.BlockHeight);
            newBlockStateSet.BlockExecutedData.Count.ShouldBe(7);
            newBlockStateSet.BlockExecutedData.Keys.ShouldContain(key => key.Contains(typeof(Transaction).Name));
            newBlockStateSet.BlockExecutedData.Keys.ShouldContain(key => key.Contains(typeof(TransactionResult).Name));
            newBlockStateSet.BlockExecutedData.Keys.ShouldContain(key => key.Contains(typeof(Chain).Name));

            blockStateSet = await AddBlockStateSetAsync(blockStateSet);

            CheckBlockExecutedData(blockStateSet, chain, transactionResult, transactionDic);
            CheckBlockExecutedDataCache(new BlockIndex
            {
                BlockHash   = blockStateSet.BlockHash,
                BlockHeight = blockStateSet.BlockHeight
            }, chain, transactionResult, transactionDic, false, false);

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

            CheckBlockExecutedData(blockStateSet, chain, transactionResult, transactionDic);
            CheckBlockExecutedDataCache(new BlockIndex
            {
                BlockHash   = blockStateSet.BlockHash,
                BlockHeight = blockStateSet.BlockHeight
            }, chain, transactionResult, transactionDic, false, true);

            blockStateSet = await AddBlockStateSetAsync(blockStateSet);

            CheckBlockExecutedData(blockStateSet, chain, transactionResult, transactionDic);

            chain = await _blockchainService.GetChainAsync();

            await _chainBlockchainExecutedDataService.AddBlockExecutedDataAsync(new BlockIndex
            {
                BlockHash   = chain.BestChainHash,
                BlockHeight = chain.BestChainHeight
            },
                                                                                chainKey, chain);

            _chainBlockchainExecutedDataCacheProvider
            .TryGetChangeHeight(GetBlockExecutedDataKey <Chain>(), out var chainChangeHeight)
            .ShouldBeTrue();
            chainChangeHeight.ShouldBe(chain.BestChainHeight);

            _chainBlockchainExecutedDataCacheProvider
            .TryGetBlockExecutedData(GetBlockExecutedDataKey <Chain>(), out var chainExecutedData)
            .ShouldBeFalse();

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

            _chainBlockchainExecutedDataService.CleanChangeHeight(chain.BestChainHeight);

            _chainBlockchainExecutedDataCacheProvider
            .TryGetChangeHeight(GetBlockExecutedDataKey <Chain>(), out chainChangeHeight)
            .ShouldBeFalse();

            _chainBlockchainExecutedDataService.GetBlockExecutedData(new ChainContext
            {
                BlockHash   = blockStateSet.BlockHash,
                BlockHeight = blockStateSet.BlockHeight
            }, GetBlockExecutedDataKey <Chain>());

            _chainBlockchainExecutedDataCacheProvider
            .TryGetBlockExecutedData(GetBlockExecutedDataKey <Chain>(), out chainExecutedData)
            .ShouldBeTrue();
            chainExecutedData.ShouldBe(chain);
        }