Beispiel #1
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);
        }
Beispiel #2
0
        public async Task Validate_WithoutProvidedSideChainBlockData_Test()
        {
            int chainId        = _chainOptions.ChainId;
            var blockInfoCache = new List <IBlockCacheEntity>();

            for (int i = 0; i <= CrossChainConstants.MinimalBlockCacheEntityCount; i++)
            {
                blockInfoCache.Add(new SideChainBlockData
                {
                    Height  = (1 + i),
                    ChainId = chainId
                });
            }

            _crossChainTestHelper.AddFakeSideChainIdHeight(chainId, 0);
            var fakeCache = new Dictionary <int, List <IBlockCacheEntity> > {
                { chainId, blockInfoCache }
            };

            AddFakeCacheData(fakeCache);

            var list = new List <SideChainBlockData>();
            var res  = await _crossChainIndexingDataService.ValidateSideChainBlockDataAsync(list, Hash.Empty, 1);

            Assert.True(res);
        }
        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);
        }
        public async Task Validate_WithoutEmptyInput_Test()
        {
            int chainId        = _chainOptions.ChainId;
            var blockInfoCache = new List <IBlockCacheEntity>();

            for (int i = 0; i <= CrossChainConstants.DefaultBlockCacheEntityCount; i++)
            {
                blockInfoCache.Add(new SideChainBlockData
                {
                    Height  = 1 + i,
                    ChainId = chainId
                });
            }

            _crossChainTestHelper.AddFakeSideChainIdHeight(chainId, 0);
            var fakeCache = new Dictionary <int, List <IBlockCacheEntity> > {
                { chainId, blockInfoCache }
            };

            AddFakeCacheData(fakeCache);

            var crossChainBlockData = new CrossChainBlockData();
            var res = await _crossChainIndexingDataValidationService.ValidateCrossChainIndexingDataAsync(crossChainBlockData,
                                                                                                         Hash.Empty, 1);

            Assert.True(res);
        }
        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
                  } }
            });
        }
Beispiel #6
0
        public async Task GetSideChainBlock_WithoutEnoughCache()
        {
            int chainId   = 123;
            var fakeCache = new Dictionary <int, List <IBlockInfo> >
            {
                {
                    chainId, new List <IBlockInfo>
                    {
                        new SideChainBlockData
                        {
                            SideChainHeight = 1
                        }
                    }
                }
            };

            AddFakeCacheData(fakeCache);
            _crossChainTestHelper.AddFakeSideChainIdHeight(chainId, 0);
            var res = await _crossChainDataProvider.GetSideChainBlockDataAsync(Hash.Empty, 1);

            Assert.Empty(res);
        }
        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.SideChainBlockDataList.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.SideChainBlockDataList[0].Height == fakeSideChainBlockData.Height);
            Assert.True(res.SideChainBlockDataList[0].ChainId == chainId);
        }
        public async Task FillExtraData_Test()
        {
            var fakeMerkleTreeRoot1 = Hash.FromString("fakeMerkleTreeRoot1");
            var fakeMerkleTreeRoot2 = Hash.FromString("fakeMerkleTreeRoot2");
            var fakeMerkleTreeRoot3 = Hash.FromString("fakeMerkleTreeRoot3");

            int chainId1 = ChainHelper.ConvertBase58ToChainId("2112");
            int chainId2 = ChainHelper.ConvertBase58ToChainId("2113");
            int chainId3 = ChainHelper.ConvertBase58ToChainId("2114");
            var fakeSideChainBlockDataList = new List <SideChainBlockData>
            {
                new SideChainBlockData
                {
                    Height = 1,
                    TransactionStatusMerkleTreeRoot = fakeMerkleTreeRoot1,
                    ChainId = chainId1
                },
                new SideChainBlockData
                {
                    Height = 1,
                    TransactionStatusMerkleTreeRoot = fakeMerkleTreeRoot2,
                    ChainId = chainId2
                },
                new SideChainBlockData
                {
                    Height = 1,
                    TransactionStatusMerkleTreeRoot = fakeMerkleTreeRoot3,
                    ChainId = chainId3
                }
            };

            var list1 = new List <IBlockCacheEntity>();
            var list2 = new List <IBlockCacheEntity>();
            var list3 = new List <IBlockCacheEntity>();

            list1.Add(fakeSideChainBlockDataList[0]);
            list2.Add(fakeSideChainBlockDataList[1]);
            list3.Add(fakeSideChainBlockDataList[2]);

            for (int i = 2; i < CrossChainConstants.MinimalBlockCacheEntityCount + 2; i++)
            {
                list1.Add(new SideChainBlockData
                {
                    Height = i,
                    TransactionStatusMerkleTreeRoot = fakeMerkleTreeRoot1,
                    ChainId = chainId1
                });
                list2.Add(new SideChainBlockData
                {
                    Height = i,
                    TransactionStatusMerkleTreeRoot = fakeMerkleTreeRoot2,
                    ChainId = chainId2
                });
                list3.Add(new SideChainBlockData
                {
                    Height = i,
                    TransactionStatusMerkleTreeRoot = fakeMerkleTreeRoot3,
                    ChainId = chainId3
                });
            }

            AddFakeCacheData(new Dictionary <int, List <IBlockCacheEntity> >
            {
                { chainId1, list1 },
                { chainId2, list2 },
                { chainId3, list3 }
            });

            _crossChainTestHelper.AddFakeSideChainIdHeight(chainId1, 0);
            _crossChainTestHelper.AddFakeSideChainIdHeight(chainId2, 0);
            _crossChainTestHelper.AddFakeSideChainIdHeight(chainId3, 0);
            _crossChainTestHelper.SetFakeLibHeight(1);
            var header = new BlockHeader
            {
                PreviousBlockHash = Hash.FromString("PreviousHash"),
                Height            = 2
            };

            var sideChainTxMerkleTreeRoot =
                await _crossChainBlockExtraDataProvider.GetExtraDataForFillingBlockHeaderAsync(header);

            var merkleTreeRoot = BinaryMerkleTree
                                 .FromLeafNodes(fakeSideChainBlockDataList.Select(sideChainBlockData =>
                                                                                  sideChainBlockData.TransactionStatusMerkleTreeRoot)).Root;
            var expected = new CrossChainExtraData {
                TransactionStatusMerkleTreeRoot = merkleTreeRoot
            }.ToByteString();

            Assert.Equal(expected, sideChainTxMerkleTreeRoot);
        }