Ejemplo n.º 1
0
        public async Task ValidateBlockBeforeAttachAsync_Test()
        {
            var block          = _kernelTestHelper.GenerateBlock(9, HashHelper.ComputeFrom("PreviousBlockHash"));
            var validateResult = await _blockValidationService.ValidateBlockBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            block.Body.TransactionIds.Add(Hash.Empty);
            block.Header.MerkleTreeRootOfTransactions = block.Body.CalculateMerkleTreeRoot();
            block.Header.ChainId = 0;

            block.Header.Signature =
                ByteString.CopyFrom(CryptoHelper.SignWithPrivateKey(_kernelTestHelper.KeyPair.PrivateKey,
                                                                    block.GetHash().ToByteArray()));

            validateResult = await _blockValidationService.ValidateBlockBeforeAttachAsync(block);

            validateResult.ShouldBeFalse();

            _systemTransactionExtraDataProvider.SetSystemTransactionCount(1, block.Header);
            block.Header.Signature =
                ByteString.CopyFrom(CryptoHelper.SignWithPrivateKey(_kernelTestHelper.KeyPair.PrivateKey,
                                                                    block.GetHash().ToByteArray()));
            validateResult = await _blockValidationService.ValidateBlockBeforeAttachAsync(block);

            validateResult.ShouldBeTrue();
        }
Ejemplo n.º 2
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 <bool> ValidateBlockBeforeAttachAsync(BlockWithTransactions blockWithTransactions)
        {
            if (!await _blockValidationService.ValidateBlockBeforeAttachAsync(blockWithTransactions))
            {
                return(false);
            }

            if (!await ValidateTransactionAsync(blockWithTransactions))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 4
0
        public async Task AttachBlockAsync(Block block)
        {
            var existBlock = await _blockchainService.GetBlockHeaderByHashAsync(block.GetHash());

            if (existBlock != null)
            {
                Logger.LogDebug($"Try attaching block but already exist, {block}");
                return;
            }
            if (!await _blockValidationService.ValidateBlockBeforeAttachAsync(block))
            {
                Logger.LogWarning($"Validate block failed (before attach to chain), {block}");
                return;
            }

            await _blockchainService.AddBlockAsync(block);

            var chain = await _blockchainService.GetChainAsync();

            var status = await _blockchainService.AttachBlockToChainAsync(chain, block);

            await _blockchainExecutingService.ExecuteBlocksAttachedToLongestChain(chain, status);
        }