示例#1
0
        public async Task SetIrreversibleBlockAsync(Chain chain, long irreversibleBlockHeight,
                                                    Hash irreversibleBlockHash)
        {
            // Create before IChainManager.SetIrreversibleBlockAsync so that we can correctly get the previous LIB info
            var eventDataToPublish = new NewIrreversibleBlockFoundEvent()
            {
                PreviousIrreversibleBlockHash   = chain.LastIrreversibleBlockHash,
                PreviousIrreversibleBlockHeight = chain.LastIrreversibleBlockHeight,
                BlockHash   = irreversibleBlockHash,
                BlockHeight = irreversibleBlockHeight
            };

            var success = await _chainManager.SetIrreversibleBlockAsync(chain, irreversibleBlockHash);

            if (!success)
            {
                return;
            }
            // TODO: move to background job, it will slow down our system
            // Clean last branches and not linked
            var toCleanBlocks = await _chainManager.CleanBranchesAsync(chain, eventDataToPublish.PreviousIrreversibleBlockHash,
                                                                       eventDataToPublish.PreviousIrreversibleBlockHeight);

            await RemoveBlocksAsync(toCleanBlocks);

            await LocalEventBus.PublishAsync(eventDataToPublish);
        }
示例#2
0
文件: TxHub.cs 项目: zhoujue1110/AElf
        public async Task HandleNewIrreversibleBlockFoundAsync(NewIrreversibleBlockFoundEvent eventData)
        {
            CleanTransactions(_expiredByExpiryBlock, eventData.BlockHeight);
            CleanTransactions(_invalidatedByBlock, eventData.BlockHeight);

            await Task.CompletedTask;
        }
        public Task HandleEventAsync(NewIrreversibleBlockFoundEvent eventData)
        {
            _taskQueueManager.Enqueue(
                async() => { await _resourceExtractionService.HandleNewIrreversibleBlockFoundAsync(eventData); },
                KernelConstants.ChainCleaningQueueName);

            return(Task.CompletedTask);
        }
示例#4
0
        public async Task HandleNewIrreversibleBlockFoundAsync(NewIrreversibleBlockFoundEvent eventData)
        {
            foreach (var txIds in _expiredByExpiryBlock.Where(kv => kv.Key <= eventData.BlockHeight))
            {
                foreach (var txId in txIds.Value.Keys)
                {
                    _allTransactions.TryRemove(txId, out _);
                }
            }

            await Task.CompletedTask;
        }
        public async Task HandleNewIrreversibleBlockFoundAsync(NewIrreversibleBlockFoundEvent eventData)
        {
            var contractInfoCache = _smartContractExecutiveService.GetContractInfoCache();
            var addresses         = contractInfoCache.Where(c => c.Value <= eventData.BlockHeight).Select(c => c.Key).ToArray();
            var transactionIds    = _resourceCache.Where(c => c.Value.Address.IsIn(addresses) && c.Value.ResourceInfo.ParallelType != ParallelType.NonParallelizable).Select(c => c.Key);

            ClearResourceCache(transactionIds.Concat(_resourceCache
                                                     .Where(c => c.Value.RefBlockNumber <= eventData.BlockHeight)
                                                     .Select(c => c.Key)).Distinct());
            _smartContractExecutiveService.ClearContractInfoCache(eventData.BlockHeight);

            await Task.CompletedTask;
        }
示例#6
0
        public async Task HandleNewIrreversibleBlockFoundAsync(NewIrreversibleBlockFoundEvent eventData)
        {
            try
            {
                ClearResourceCache(_resourceCache
                                   .Where(c => c.Value.ResourceUsedBlockHeight <= eventData.BlockHeight)
                                   .Select(c => c.Key).Distinct().ToList());
            }
            catch (InvalidOperationException e)
            {
                Logger.LogError(e, "Unexpected case occured when clear resource info.");
            }

            await Task.CompletedTask;
        }
示例#7
0
        public async Task SetIrreversibleBlockAsync(Chain chain, long irreversibleBlockHeight,
                                                    Hash irreversibleBlockHash)
        {
            // Create before IChainManager.SetIrreversibleBlockAsync so that we can correctly get the previous LIB info
            var eventDataToPublish = new NewIrreversibleBlockFoundEvent()
            {
                PreviousIrreversibleBlockHash   = chain.LastIrreversibleBlockHash,
                PreviousIrreversibleBlockHeight = chain.LastIrreversibleBlockHeight,
                BlockHash   = irreversibleBlockHash,
                BlockHeight = irreversibleBlockHeight
            };

            var success = await _chainManager.SetIrreversibleBlockAsync(chain, irreversibleBlockHash);

            if (!success)
            {
                return;
            }

            await LocalEventBus.PublishAsync(eventDataToPublish);
        }
        public async Task HandleNewIrreversibleBlockFoundAsync(NewIrreversibleBlockFoundEvent eventData)
        {
            var contractInfoCache = _smartContractExecutiveService.GetContractInfoCache();
            var addresses         = contractInfoCache.Where(c => c.Value <= eventData.BlockHeight).Select(c => c.Key).ToArray();

            try
            {
                var transactionIds = _resourceCache.Where(c =>
                                                          c.Value.Address.IsIn(addresses) &&
                                                          c.Value.ResourceInfo.ParallelType != ParallelType.NonParallelizable).Select(c => c.Key);

                ClearResourceCache(transactionIds.Concat(_resourceCache
                                                         .Where(c => c.Value.ResourceUsedBlockHeight <= eventData.BlockHeight)
                                                         .Select(c => c.Key)).Distinct().ToList());

                _smartContractExecutiveService.ClearContractInfoCache(eventData.BlockHeight);
            }
            catch (InvalidOperationException e)
            {
                Logger.LogError(e, "Unexpected case occured when clear resource info.");
            }

            await Task.CompletedTask;
        }
 public async Task HandleEventAsync(NewIrreversibleBlockFoundEvent eventData)
 {
     await _txHub.HandleNewIrreversibleBlockFoundAsync(eventData);
 }
示例#10
0
 public async Task HandleNewIrreversibleBlockFoundAsync(NewIrreversibleBlockFoundEvent eventData)
 {
     await Task.CompletedTask;
 }
示例#11
0
 public Task HandleNewIrreversibleBlockFoundAsync(NewIrreversibleBlockFoundEvent eventData)
 {
     throw new System.NotImplementedException();
 }
        public async Task Set_IrreversibleBlock_Test()
        {
            NewIrreversibleBlockFoundEvent eventData = null;

            _eventBus.Subscribe <NewIrreversibleBlockFoundEvent>(d =>
            {
                eventData = d;
                return(Task.CompletedTask);
            });

            var chain = await _fullBlockchainService.GetChainAsync();

            {
                //         LIB height: 7
                //
                //             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
                //        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
                //     Longest Branch:                                   (h)-> l -> m  -> n  -> o  -> p
                //        Fork Branch:                    (e)-> q -> r -> s -> t -> u
                //    Unlinked Branch:                                              v  -> w  -> x  -> y  -> z
                await _fullBlockchainService.SetIrreversibleBlockAsync(chain, _kernelTestHelper.BestBranchBlockList[6]
                                                                       .Height, _kernelTestHelper.BestBranchBlockList[6].GetHash());

                chain = await _fullBlockchainService.GetChainAsync();

                chain.LastIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].GetHash());
                chain.LastIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].Height);

                eventData.ShouldNotBeNull();
                eventData.BlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].GetHash());
                eventData.BlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].Height);
                eventData.PreviousIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[4].GetHash());
                eventData.PreviousIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[4].Height);

                var blockLink =
                    await _chainManager.GetChainBlockLinkAsync(_kernelTestHelper.BestBranchBlockList[6].GetHash());

                while (blockLink != null)
                {
                    blockLink.IsIrreversibleBlock.ShouldBeTrue();
                    var chainBlockIndex = await _chainManager.GetChainBlockIndexAsync(blockLink.Height);

                    chainBlockIndex.BlockHash.ShouldBe(blockLink.BlockHash);

                    blockLink = await _chainManager.GetChainBlockLinkAsync(blockLink.PreviousBlockHash);
                }
            }

            {
                //         LIB height: 11
                //
                //             Height: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14
                //        Best Branch: a -> b -> c -> d -> e -> f -> g -> h -> i -> j  -> k
                //     Longest Branch: (-)                               (h)-> l -> m  -> n  -> o  -> p
                //        Fork Branch: (-)                (e)-> q -> r -> s -> t -> u
                //    Unlinked Branch:                                             v(-) -> w  -> x  -> y  -> z
                eventData = null;
                await _fullBlockchainService.SetIrreversibleBlockAsync(chain, _kernelTestHelper.BestBranchBlockList[10]
                                                                       .Height, _kernelTestHelper.BestBranchBlockList[10].GetHash());

                chain = await _fullBlockchainService.GetChainAsync();

                chain.LastIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].GetHash());
                chain.LastIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].Height);

                eventData.ShouldNotBeNull();
                eventData.BlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].GetHash());
                eventData.BlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[10].Height);
                eventData.PreviousIrreversibleBlockHash.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].GetHash());
                eventData.PreviousIrreversibleBlockHeight.ShouldBe(_kernelTestHelper.BestBranchBlockList[6].Height);

                var blockLink =
                    await _chainManager.GetChainBlockLinkAsync(_kernelTestHelper.BestBranchBlockList[10].GetHash());

                while (blockLink != null)
                {
                    blockLink.IsIrreversibleBlock.ShouldBeTrue();
                    var chainBlockIndex = await _chainManager.GetChainBlockIndexAsync(blockLink.Height);

                    chainBlockIndex.BlockHash.ShouldBe(blockLink.BlockHash);

                    blockLink = await _chainManager.GetChainBlockLinkAsync(blockLink.PreviousBlockHash);
                }
            }

            {
                // Set lib failed
                eventData = null;
                await _fullBlockchainService.SetIrreversibleBlockAsync(chain, _kernelTestHelper.BestBranchBlockList[9]
                                                                       .Height, _kernelTestHelper.BestBranchBlockList[9].GetHash());

                eventData.ShouldBeNull();
            }
        }
示例#13
0
 public async Task HandleEventAsync(NewIrreversibleBlockFoundEvent eventData)
 {
     await _resourceExtractionService.HandleNewIrreversibleBlockFoundAsync(eventData);
 }
示例#14
0
 public async Task HandleEventAsync(NewIrreversibleBlockFoundEvent eventData)
 {
     await _transactionPoolService.UpdateTransactionPoolByLibAsync(eventData.BlockHeight);
 }