public Task HandleEventAsync(CleanBlockExecutedDataChangeHeightEventData eventData) { _cachedBlockchainExecutedDataService.CleanChangeHeight(eventData.IrreversibleBlockHeight); return(Task.CompletedTask); }
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); }