public static async Task <T> GetBlockExecutedDataAsync <T>( this IBlockchainExecutedDataService blockchainExecutedDataService, IBlockIndex chainContext, string key) { var byteString = await blockchainExecutedDataService.GetBlockExecutedDataAsync(chainContext, key); return(SerializationHelper.Deserialize <T>(byteString?.ToByteArray())); }
public async Task BlockExecutedData_Test() { var chain = await _blockchainService.GetChainAsync(); var blockStateSet = new BlockStateSet { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight, }; await _blockStateSetManger.SetBlockStateSetAsync(blockStateSet); var transactionDic = new Dictionary <string, Transaction>(); for (int 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( string.Join("/", KernelConstants.BlockExecutedDataKey, nameof(Transaction), transaction.GetHash().ToString()), transaction); } await _blockchainExecutedDataService.AddBlockExecutedDataAsync(chain.BestChainHash, transactionDic); var transactionResult = new TransactionResult { TransactionId = transactionDic.First().Value.GetHash() }; var transactionResultKey = string.Join("/", KernelConstants.BlockExecutedDataKey, nameof(TransactionResult), transactionResult.TransactionId.ToString()); await _blockchainExecutedDataService.AddBlockExecutedDataAsync(chain.BestChainHash, transactionResultKey, transactionResult); var chainKey = string.Join("/", KernelConstants.BlockExecutedDataKey, nameof(Chain)); await _blockchainExecutedDataService.AddBlockExecutedDataAsync(chain.BestChainHash, chainKey, chain); 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)); var chainContext = new ChainContext { BlockHash = chain.BestChainHash, BlockHeight = chain.BestChainHeight }; var chainFromBlockExecutedData = await _blockchainExecutedDataService.GetBlockExecutedDataAsync <Chain>(chainContext, chainKey); chainFromBlockExecutedData.ShouldBe(chain); var transactionResultFromBlockExecutedData = await _blockchainExecutedDataService.GetBlockExecutedDataAsync <TransactionResult>(chainContext, transactionResultKey); transactionResultFromBlockExecutedData.ShouldBe(transactionResult); foreach (var keyPair in transactionDic) { var transaction = await _blockchainExecutedDataService.GetBlockExecutedDataAsync <Transaction>(chainContext, keyPair.Key); transaction.ShouldBe(keyPair.Value); } }