Exemple #1
0
        public async Task ValidateBlockBeforeAttach_Success()
        {
            var chain = await _blockchainService.GetChainAsync();

            var transactions = await _osTestHelper.GenerateTransferTransactions(3);

            var block = _osTestHelper.GenerateBlockWithTransactions(chain.BestChainHash, chain.BestChainHeight,
                                                                    transactions);

            var result = await _blockSyncValidationService.ValidateBlockBeforeAttachAsync(block);

            result.ShouldBeTrue();
        }
        public async Task ValidateBlockBeforeAttach_InvalidBlock_ReturnFalse()
        {
            var invalidBlock = _osTestHelper.GenerateBlockWithTransactions(HashHelper.ComputeFrom("BadBlock"), 10000);
            var result       = await _blockSyncValidationService.ValidateBlockBeforeAttachAsync(invalidBlock);

            result.ShouldBeFalse();
        }
        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);
        }