Exemple #1
0
        private bool OnUnvalidatedBlockCreated(object sender, BlockCreatedEventArgs ev)
        {
            if (ev.Block.Header.MagicNumber != _networkIdentifier)
            {
                return(false);
            }
            var blockExists = true;

            if (sender != this)
            {
                try
                {
                    _blockchainRepo.GetBlockByHash(ev.Block.Header.Hash, ev.Block.Header.MagicNumber);
                }
                catch (KeyNotFoundException)
                {
                    blockExists = false;
                }
            }

            if (!blockExists)
            {
                CheckForDifficultyUpdate();
                var target = BlockchainConstants.MaximumTarget / difficulty;
                try
                {
                    _blockValidator.ValidateBlock(ev.Block, target, _blockchain, true, true);
                    _logger.LogInformation("Received block from remote node");
                    _logger.LogDebug("Current height: {0}", _blockchain.CurrentHeight);
                    // Do not restart the task because that's buggy (it stops, but doesn't start)

                    foreach (var tx in ev.Block.Transactions)
                    {
                        _txPool.RemoveTransaction(tx);
                    }
                }
                catch (BlockRejectedException ex)
                {
                    _logger.LogInformation("Block with hash {0} was rejected: {1}", ex.Block.Header.Hash, ex.Message);
                    return(false);
                }
                catch (TransactionRejectedException ex)
                {
                    _logger.LogInformation("Block with transaction hash {0} was rejected: {1}", ex.Transaction.Hash, ex.Message);
                    return(false);
                }
            }
            return(true);
        }
 private void OnValidatedBlockCreated(object sender, BlockCreatedEventArgs eventHandler)
 {
     hotRestart = true;
 }