public async Task RequestCrossChainDataTest()
        {
            var crossChainClientCreationContext = new CrossChainClientCreationContext
            {
                LocalChainId          = 0,
                RemoteChainId         = 1,
                IsClientToParentChain = false,
                RemoteServerHost      = "localhost",
                RemoteServerPort      = 5000
            };
            var communicationHelper = GetRequiredService <CrossChainCommunicationTestHelper>();

            communicationHelper.SetClientConnected(crossChainClientCreationContext.RemoteChainId, true);
            await _crossChainClientService.CreateClientAsync(crossChainClientCreationContext);

            _crossChainCacheEntityService.RegisterNewChain(crossChainClientCreationContext.RemoteChainId, 10);
            communicationHelper.CheckClientConnected(-1).ShouldBeFalse();
            await _crossChainRequestService.RequestCrossChainDataFromOtherChainsAsync();

            communicationHelper.CheckClientConnected(-1).ShouldBeTrue();

            var chainInitializationData = await _crossChainRequestService.RequestChainInitializationDataAsync(crossChainClientCreationContext.RemoteChainId);

            chainInitializationData.CreationHeightOnParentChain.ShouldBe(1);
        }
Example #2
0
        public async Task <bool> ValidateBlockAfterExecuteAsync(IBlock block)
        {
            if (block.Header.Height == AElfConstants.GenesisBlockHeight)
            {
                return(true);
            }

            try
            {
                var isSideChainBlockDataIndexed = await TryDiscoverIndexedSideChainBlockDataAsync(block);

                Logger.LogDebug($"Try discovery indexed side chain block data: {isSideChainBlockDataIndexed}");
                var extraData        = ExtractCrossChainExtraData(block.Header);
                var validationResult = true;
                if (!isSideChainBlockDataIndexed && !extraData.IsNullOrEmpty())
                {
                    // cross chain extra data in block header should be null if no side chain block data indexed in contract
                    validationResult = false;
                }
                else if (isSideChainBlockDataIndexed)
                {
                    var indexedCrossChainBlockData =
                        await _crossChainIndexingDataService.GetIndexedSideChainBlockDataAsync(block.Header.GetHash(),
                                                                                               block.Header.Height);

                    if (indexedCrossChainBlockData.IsNullOrEmpty() ^ extraData.IsNullOrEmpty())
                    {
                        validationResult = false;
                    }
                    else if (!indexedCrossChainBlockData.IsNullOrEmpty())
                    {
                        validationResult = ValidateBlockExtraDataAsync(indexedCrossChainBlockData, extraData);
                    }
                }

                if (!validationResult)
                {
                    Logger.LogDebug(
                        $"Invalid cross chain extra data, block height {block.Header.Height}, hash {block.GetHash()}.");
                }
                return(validationResult);
            }
            finally
            {
                _ = _crossChainRequestService.RequestCrossChainDataFromOtherChainsAsync();
            }
        }
        public async Task RequestCrossChainDataFromOtherChains_Test()
        {
            int chainId = ChainHelper.GetChainId(1);

            var blockInfoCache = new List <IBlockCacheEntity>
            {
                new ParentChainBlockData {
                    ChainId = chainId, Height = 1
                }
            };

            AddFakeParentChainIdHeight(chainId, 1);
            var fakeCache = new Dictionary <int, List <IBlockCacheEntity> > {
                { chainId, blockInfoCache }
            };

            AddFakeCacheData(fakeCache);
            await _chainRequestService.RequestCrossChainDataFromOtherChainsAsync();
        }
 public async Task HandleEventAsync(CrossChainDataValidatedEvent eventData)
 {
     _ = _crossChainRequestService.RequestCrossChainDataFromOtherChainsAsync();
 }