public async Task GenerateTransactions_Test() { var sideChainId = 123; var sideChainBlockInfoCache = new List <IBlockCacheEntity>(); var previousBlockHash = Hash.FromString("PreviousBlockHash"); var previousBlockHeight = 1; var crossChainBlockData = new CrossChainBlockData { PreviousBlockHeight = previousBlockHeight }; var cachingCount = 5; for (int i = 1; i < cachingCount + CrossChainConstants.DefaultBlockCacheEntityCount; i++) { var sideChainBlockData = new SideChainBlockData() { ChainId = sideChainId, Height = (i + 1), TransactionStatusMerkleTreeRoot = Hash.FromString((sideChainId + 1).ToString()) }; sideChainBlockInfoCache.Add(sideChainBlockData); if (i <= CrossChainConstants.DefaultBlockCacheEntityCount) { crossChainBlockData.SideChainBlockDataList.Add(sideChainBlockData); } } _crossChainTestHelper.AddFakeSideChainIdHeight(sideChainId, 1); AddFakeCacheData(new Dictionary <int, List <IBlockCacheEntity> > { { sideChainId, sideChainBlockInfoCache } }); var res = await _crossChainIndexingDataService.PrepareExtraDataForNextMiningAsync(previousBlockHash, previousBlockHeight); Assert.Empty(res); var smartContractAddress = SampleAddress.AddressList[0]; _smartContractAddressService.SetAddress(CrossChainSmartContractAddressNameProvider.Name, smartContractAddress); var transactions = await _crossChainIndexingTransactionGenerator.GenerateTransactionsAsync(SampleAddress.AddressList[0], previousBlockHeight, previousBlockHash); transactions.Count.ShouldBe(1); transactions[0].From.ShouldBe(SampleAddress.AddressList[0]); transactions[0].To.ShouldBe(smartContractAddress); transactions[0].RefBlockNumber.ShouldBe(previousBlockHeight); transactions[0].RefBlockPrefix.ShouldBe(ByteString.CopyFrom(previousBlockHash.Value.Take(4).ToArray())); transactions[0].MethodName .ShouldBe(nameof(CrossChainContractContainer.CrossChainContractStub.ProposeCrossChainIndexing)); var crossChainBlockDataInParam = CrossChainBlockData.Parser.ParseFrom(transactions[0].Params); Assert.Equal(crossChainBlockData, crossChainBlockDataInParam); }
public async Task GenerateTransactions_Test() { var sideChainId = 123; var previousBlockHash = HashHelper.ComputeFrom("PreviousBlockHash"); var previousBlockHeight = 1; var crossChainBlockData = new CrossChainBlockData { }; var cachingCount = 5; for (int i = 1; i < cachingCount + CrossChainConstants.DefaultBlockCacheEntityCount; i++) { var sideChainBlockData = new SideChainBlockData() { ChainId = sideChainId, Height = (i + 1), TransactionStatusMerkleTreeRoot = HashHelper.ComputeFrom((sideChainId + 1).ToString()) }; if (i <= CrossChainConstants.DefaultBlockCacheEntityCount) { crossChainBlockData.SideChainBlockDataList.Add(sideChainBlockData); } } var crossChainTransactionInput = new CrossChainTransactionInput { Value = crossChainBlockData.ToByteString(), MethodName = nameof(CrossChainContractImplContainer.CrossChainContractImplStub.ProposeCrossChainIndexing), PreviousBlockHeight = previousBlockHeight }; _crossChainTestHelper.AddFakeCrossChainTransactionInput(previousBlockHash, crossChainTransactionInput); // AddFakeCacheData(new Dictionary<int, List<ICrossChainBlockEntity>> {{sideChainId, sideChainBlockInfoCache}}); await _blockStateSetManger.SetBlockStateSetAsync(new BlockStateSet { BlockHash = previousBlockHash, BlockHeight = previousBlockHeight }); var transactions = await _crossChainIndexingTransactionGenerator.GenerateTransactionsAsync(SampleAddress.AddressList[0], previousBlockHeight, previousBlockHash); transactions.Count.ShouldBe(1); transactions[0].From.ShouldBe(SampleAddress.AddressList[0]); transactions[0].To.ShouldBeNull(); transactions[0].RefBlockNumber.ShouldBe(previousBlockHeight); transactions[0].RefBlockPrefix.ShouldBe(BlockHelper.GetRefBlockPrefix(previousBlockHash)); transactions[0].MethodName .ShouldBe(nameof(CrossChainContractImplContainer.CrossChainContractImplStub.ProposeCrossChainIndexing)); var crossChainBlockDataInParam = CrossChainBlockData.Parser.ParseFrom(transactions[0].Params); Assert.Equal(crossChainBlockData, crossChainBlockDataInParam); }
public async Task Generate_SystemTransactions() { var transactions = await _systemTransactionGenerator.GenerateTransactionsAsync(SampleAddress.AddressList[0], 0L, Hash.Empty); transactions.Count.ShouldBe(2); }
public async Task GenerateTransactionsAsync_Without_PackedTransaction_Test() { var address = NormalAddress; var blockIndex = new BlockIndex { BlockHash = HashHelper.ComputeFrom("BlockHash"), BlockHeight = 10 }; var proposalId = HashHelper.ComputeFrom("proposal"); _proposalTestHelper.AddNotVotedProposalIdList(new List <Hash> { proposalId }); var proposalCacheProvider = GetRequiredService <IProposalProvider>(); proposalCacheProvider.AddProposal(proposalId, 5); await _transactionPackingOptionProvider.SetTransactionPackingOptionAsync(blockIndex, false); var transactionList = await _proposalApprovalTransactionGenerator.GenerateTransactionsAsync(address, blockIndex.BlockHeight, blockIndex.BlockHash); transactionList.Count.ShouldBe(1); }
public async Task GenerateTransactions_Test() { var transactions = new List <Transaction>(); transactions.AddRange( await _crossChainIndexingTransactionGenerator.GenerateTransactionsAsync(SampleAddress.AddressList[0], 0, Hash.Empty)); transactions.Count.ShouldBe(0); var chainId = _kernelTestHelper.BestBranchBlockList[0].Header.ChainId; var previousBlockHash = _kernelTestHelper.BestBranchBlockList[3].GetHash(); var previousBlockHeight = _kernelTestHelper.BestBranchBlockList[3].Height; _crossChainTestHelper.AddFakeSideChainIdHeight(chainId, previousBlockHeight); var blockInfoCache = new List <IBlockCacheEntity>(); var cachingCount = CrossChainConstants.MinimalBlockCacheEntityCount + 1; for (var i = 1; i <= cachingCount; i++) { var sideChainBlockData = new SideChainBlockData { ChainId = chainId, Height = previousBlockHeight + i, }; blockInfoCache.Add(sideChainBlockData); } var fakeCache = new Dictionary <int, List <IBlockCacheEntity> > { { chainId, blockInfoCache } }; AddFakeCacheData(fakeCache); var smartContractAddress = SampleAddress.AddressList[0]; _smartContractAddressService.SetAddress(CrossChainSmartContractAddressNameProvider.Name, smartContractAddress); await _crossChainIndexingDataService.GetCrossChainBlockDataForNextMiningAsync(previousBlockHash, previousBlockHeight); transactions.AddRange( await _crossChainIndexingTransactionGenerator.GenerateTransactionsAsync(SampleAddress.AddressList[0], previousBlockHeight, previousBlockHash)); transactions.Count.ShouldBe(1); transactions[0].From.ShouldBe(SampleAddress.AddressList[0]); transactions[0].To.ShouldBe(smartContractAddress); transactions[0].RefBlockNumber.ShouldBe(previousBlockHeight); transactions[0].RefBlockPrefix.ShouldBe(ByteString.CopyFrom(previousBlockHash.Value.Take(4).ToArray())); transactions[0].MethodName.ShouldBe(CrossChainConstants.CrossChainIndexingMethodName); var crossChainBlockData = CrossChainBlockData.Parser.ParseFrom(transactions[0].Params); crossChainBlockData.PreviousBlockHeight.ShouldBe(previousBlockHeight); crossChainBlockData.ParentChainBlockData.Count.ShouldBe(0); crossChainBlockData.SideChainBlockData.Count.ShouldBe( cachingCount - CrossChainConstants.MinimalBlockCacheEntityCount); crossChainBlockData.SideChainBlockData[0].ChainId.ShouldBe(chainId); crossChainBlockData.SideChainBlockData[0].Height.ShouldBe(previousBlockHeight + 1); }