Ejemplo n.º 1
0
        public async Task AttachBlockWithTransactionsAsync(BlockWithTransactions blockWithTransactions,
                                                           Func <Task> attachFinishedCallback = null)
        {
            var valid = await _validationService.ValidateBlockBeforeAttachAsync(blockWithTransactions);

            if (!valid)
            {
                throw new InvalidOperationException(
                          $"The block was invalid, block hash: {blockWithTransactions}.");
            }

            await _blockchainService.AddTransactionsAsync(blockWithTransactions.Transactions);

            var block = blockWithTransactions.ToBlock();
            await _blockchainService.AddBlockAsync(block);

            _blockSyncQueueService.Enqueue(async() =>
            {
                try
                {
                    await _blockAttachService.AttachBlockAsync(block);
                }
                finally
                {
                    if (attachFinishedCallback != null)
                    {
                        await attachFinishedCallback();
                    }
                }
            },
                                           KernelConstants.UpdateChainQueueName);
        }
        public async Task AttachBlockWithTransactionsAsync(BlockWithTransactions blockWithTransactions,
                                                           string senderPubkey, Func <Task> attachFinishedCallback = null)
        {
            var blockValid = await _blockSyncValidationService.ValidateBlockBeforeAttachAsync(blockWithTransactions);

            if (!blockValid)
            {
                Logger.LogDebug(
                    $"Sync block validation failed, peer: {senderPubkey}, block hash: {blockWithTransactions.GetHash()}, block height: {blockWithTransactions.Height}");
                await LocalEventBus.PublishAsync(new AbnormalPeerFoundEventData
                {
                    BlockHash   = blockWithTransactions.GetHash(),
                    BlockHeight = blockWithTransactions.Height,
                    PeerPubkey  = senderPubkey
                });

                return;
            }

            await _blockchainService.AddTransactionsAsync(blockWithTransactions.Transactions);

            var block = blockWithTransactions.ToBlock();
            await _blockchainService.AddBlockAsync(block);

            _blockSyncQueueService.Enqueue(async() =>
            {
                try
                {
                    await _blockAttachService.AttachBlockAsync(block);
                }
                finally
                {
                    if (attachFinishedCallback != null)
                    {
                        await attachFinishedCallback();
                    }
                }
            },
                                           KernelConstants.UpdateChainQueueName);
        }