Example #1
0
        public async Task GetIndexedCrossChainBlockData_WithIndex_Test()
        {
            var chainId                = _chainOptions.ChainId;
            var fakeMerkleTreeRoot1    = Hash.FromString("fakeMerkleTreeRoot1");
            var fakeSideChainBlockData = new SideChainBlockData
            {
                Height  = 1,
                ChainId = chainId,
                TransactionStatusMerkleTreeRoot = fakeMerkleTreeRoot1
            };

            var fakeIndexedCrossChainBlockData = new CrossChainBlockData();

            fakeIndexedCrossChainBlockData.SideChainBlockData.AddRange(new [] { fakeSideChainBlockData });

            _crossChainTestHelper.AddFakeIndexedCrossChainBlockData(fakeSideChainBlockData.Height, fakeIndexedCrossChainBlockData);
            _crossChainTestHelper.AddFakeSideChainIdHeight(chainId, 0);

            AddFakeCacheData(new Dictionary <int, List <IBlockCacheEntity> >
            {
                {
                    chainId,
                    new List <IBlockCacheEntity>
                    {
                        fakeSideChainBlockData
                    }
                }
            });

            var res = await _crossChainIndexingDataService.GetIndexedCrossChainBlockDataAsync(
                fakeSideChainBlockData.BlockHeaderHash, 1);

            Assert.True(res.SideChainBlockData[0].Height == fakeSideChainBlockData.Height);
            Assert.True(res.SideChainBlockData[0].ChainId == chainId);
        }
        public async Task Validate_IncompatibleCacheData()
        {
            var fakeMerkleTreeRoot1    = Hash.FromString("fakeMerkleTreeRoot1");
            var fakeSideChainId        = 123;
            var fakeSideChainBlockData = new SideChainBlockData
            {
                SideChainHeight           = 1,
                TransactionMerkleTreeRoot = fakeMerkleTreeRoot1,
                SideChainId = fakeSideChainId
            };

            var fakeTxnMerkleTreeRoot2  = Hash.FromString("fakeMerkleTreeRoot2");
            var fakeSideChainBlockData2 = new SideChainBlockData
            {
                SideChainHeight           = 1,
                TransactionMerkleTreeRoot = fakeTxnMerkleTreeRoot2,
                SideChainId = fakeSideChainId
            };

            CreateFakeCacheAndStateData(fakeSideChainId, fakeSideChainBlockData2, 2);
            var sideChainTxMerkleTreeRoot = ComputeRootHash(new [] { fakeSideChainBlockData });
            var block = CreateFilledBlock(sideChainTxMerkleTreeRoot);
            var res   = await _crossChainBlockValidationProvider.ValidateBlockAfterExecuteAsync(block);

            Assert.False(res);
        }
Example #3
0
        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);
        }
Example #4
0
        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 GenerateTransactions_Test()
        {
            var transactions = new List <Transaction>();

            _crossChainIndexingTransactionGenerator.GenerateTransactions(SampleAddress.AddressList[0], 0, Hash.Empty, ref transactions);
            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);

            _crossChainIndexingTransactionGenerator.GenerateTransactions(SampleAddress.AddressList[0], previousBlockHeight, previousBlockHash, ref transactions);

            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);
        }
        private void CreateFakeStateData(int fakeSideChainId, SideChainBlockData fakeSideChainBlockData,
                                         long height = 1)
        {
            var fakeIndexedCrossChainBlockData = new CrossChainBlockData();

            fakeIndexedCrossChainBlockData.SideChainBlockDataList.AddRange(new[] { fakeSideChainBlockData });

            // mock data in state
            _crossChainTestHelper.AddFakeIndexedCrossChainBlockData(height, fakeIndexedCrossChainBlockData);
        }
Example #7
0
        public async Task GenerateTransaction_PendingProposal_NotApproved_Test()
        {
            var sideChainId             = 123;
            var sideChainBlockInfoCache = new List <IBlockCacheEntity>();
            var previousBlockHash       = Hash.FromString("PreviousBlockHash");
            var previousBlockHeight     = 1;

            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);
            }

            _crossChainTestHelper.AddFakePendingCrossChainIndexingProposal(
                new GetPendingCrossChainIndexingProposalOutput());
            _crossChainTestHelper.AddFakeSideChainIdHeight(sideChainId, 1);
            AddFakeCacheData(new Dictionary <int, List <IBlockCacheEntity> > {
                { sideChainId, sideChainBlockInfoCache }
            });

            var pendingProposal = new GetPendingCrossChainIndexingProposalOutput
            {
                Proposer   = SampleAddress.AddressList[0],
                ProposalId = Hash.FromString("ProposalId"),
                ProposedCrossChainBlockData = new CrossChainBlockData(),
                ToBeReleased = false,
                ExpiredTime  = TimestampHelper.GetUtcNow().AddSeconds(10)
            };

            _crossChainTestHelper.AddFakePendingCrossChainIndexingProposal(pendingProposal);
            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);

            Assert.Empty(transactions);
        }
        private void CreateFakeCacheAndStateData(int fakeSideChainId, SideChainBlockData fakeSideChainBlockData, long height = 1)
        {
            var fakeIndexedCrossChainBlockData = new CrossChainBlockData();

            fakeIndexedCrossChainBlockData.SideChainBlockData.AddRange(new [] { fakeSideChainBlockData });

            // mock data in state
            _crossChainTestHelper.AddFakeIndexedCrossChainBlockData(height, fakeIndexedCrossChainBlockData);
            _crossChainTestHelper.AddFakeSideChainIdHeight(fakeSideChainId, 0);

            // mock data in cache
            AddFakeCacheData(new Dictionary <int, List <IBlockInfo> >
            {
                { fakeSideChainId, new List <IBlockInfo> {
                      fakeSideChainBlockData
                  } }
            });
        }
Example #9
0
        public async Task GetIndexedCrossChainBlockData_WithoutIndex_Test()
        {
            var chainId = _chainOptions.ChainId;
            var fakeSideChainBlockData = new SideChainBlockData
            {
                Height  = 1,
                ChainId = chainId
            };

            var fakeIndexedCrossChainBlockData = new CrossChainBlockData();

            fakeIndexedCrossChainBlockData.SideChainBlockData.AddRange(new [] { fakeSideChainBlockData });

            var res = await _crossChainIndexingDataService.GetIndexedCrossChainBlockDataAsync(
                fakeSideChainBlockData.BlockHeaderHash, 1);

            Assert.True(res == null);
        }
        public async Task Validate_WithoutCache()
        {
            var fakeMerkleTreeRoot1 = Hash.FromString("fakeMerkleTreeRoot1");
            int chainId             = 123;
            var sideChainBlockData  = new SideChainBlockData
            {
                SideChainHeight           = 1,
                TransactionMerkleTreeRoot = fakeMerkleTreeRoot1,
                SideChainId = chainId
            };
            var sideChainTxMerkleTreeRoot = ComputeRootHash(new [] { sideChainBlockData });
            var block = CreateFilledBlock(sideChainTxMerkleTreeRoot);
            var fakeIndexedCrossChainData = new CrossChainBlockData();

            fakeIndexedCrossChainData.SideChainBlockData.Add(sideChainBlockData);
            _crossChainTestHelper.AddFakeIndexedCrossChainBlockData(2, fakeIndexedCrossChainData);

            await Assert.ThrowsAsync <ValidateNextTimeBlockValidationException>(() =>
                                                                                _crossChainBlockValidationProvider.ValidateBlockAfterExecuteAsync(block));
        }
Example #11
0
        public async Task Validate_WithoutCache_Test()
        {
            var fakeMerkleTreeRoot1 = Hash.FromString("fakeMerkleTreeRoot1");
            int chainId             = ChainHelper.ConvertBase58ToChainId("2112");
            var sideChainBlockData  = new SideChainBlockData
            {
                Height = 1,
                TransactionStatusMerkleTreeRoot = fakeMerkleTreeRoot1,
                ChainId = chainId
            };
            var sideChainTxMerkleTreeRoot = ComputeRootHash(new[] { sideChainBlockData });
            var block = CreateFilledBlock(sideChainTxMerkleTreeRoot);

            block.Header.Bloom = ByteString.CopyFrom(GetSideChainBlockDataIndexedEventBloom().Data);

            var fakeIndexedCrossChainData = new CrossChainBlockData();

            fakeIndexedCrossChainData.SideChainBlockData.Add(sideChainBlockData);
            _crossChainTestHelper.AddFakeIndexedCrossChainBlockData(2, fakeIndexedCrossChainData);

            await Assert.ThrowsAsync <BlockValidationException>(() =>
                                                                _crossChainBlockValidationProvider.ValidateBlockAfterExecuteAsync(block));
        }
        public async Task Validate_EmptyHeader_WithIndexedData()
        {
            var block = new Block
            {
                Header = new BlockHeader
                {
                    Height = 2
                }
            };

            var fakeMerkleTreeRoot1    = Hash.FromString("fakeMerkleTreeRoot1");
            int chainId                = 123;
            var fakeSideChainBlockData = new SideChainBlockData
            {
                SideChainHeight           = 1,
                TransactionMerkleTreeRoot = fakeMerkleTreeRoot1,
                SideChainId = chainId
            };

            CreateFakeCacheAndStateData(chainId, fakeSideChainBlockData, block.Height);
            var res = await _crossChainBlockValidationProvider.ValidateBlockAfterExecuteAsync(block);

            Assert.False(res);
        }
Example #13
0
        public async Task GenerateTransaction_PendingProposal_Test()
        {
            var sideChainId             = 123;
            var sideChainBlockInfoCache = new List <IBlockCacheEntity>();
            var previousBlockHash       = Hash.FromString("PreviousBlockHash");
            var previousBlockHeight     = 1;

            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);
            }

            _crossChainTestHelper.AddFakePendingCrossChainIndexingProposal(
                new GetPendingCrossChainIndexingProposalOutput());
            _crossChainTestHelper.AddFakeSideChainIdHeight(sideChainId, 1);
            AddFakeCacheData(new Dictionary <int, List <IBlockCacheEntity> > {
                { sideChainId, sideChainBlockInfoCache }
            });

            var pendingProposal = new GetPendingCrossChainIndexingProposalOutput
            {
                Proposer   = SampleAddress.AddressList[0],
                ProposalId = Hash.FromString("ProposalId"),
                ProposedCrossChainBlockData = new CrossChainBlockData(),
                ToBeReleased = true,
                ExpiredTime  = TimestampHelper.GetUtcNow().AddSeconds(10)
            };

            _crossChainTestHelper.AddFakePendingCrossChainIndexingProposal(pendingProposal);


            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.ReleaseCrossChainIndexing));

            var proposalIdInParam = Hash.Parser.ParseFrom(transactions[0].Params);

            Assert.Equal(pendingProposal.ProposalId, proposalIdInParam);
        }