コード例 #1
0
 public static async Task AddBlockExecutedDataAsync <T>(
     this IBlockchainExecutedDataService blockchainExecutedDataService,
     Hash blockHash, IDictionary <string, T> blockExecutedData)
 {
     var dic = blockExecutedData.ToDictionary(
         keyPair => keyPair.Key,
         keyPair => ByteString.CopyFrom(SerializationHelper.Serialize(keyPair.Value)));
     await blockchainExecutedDataService.AddBlockExecutedDataAsync(blockHash, dic);
 }
コード例 #2
0
 public static async Task AddBlockExecutedDataAsync <T>(
     this IBlockchainExecutedDataService blockchainExecutedDataService,
     Hash blockHash, string key, T blockExecutedData)
 {
     var dic = new Dictionary <string, ByteString>
     {
         { key, ByteString.CopyFrom(SerializationHelper.Serialize(blockExecutedData)) }
     };
     await blockchainExecutedDataService.AddBlockExecutedDataAsync(blockHash, dic);
 }
コード例 #3
0
        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);
            }
        }