コード例 #1
0
        public Block[] Process(Keccak branchStateRoot, Block[] suggestedBlocks, bool tryOnly)
        {
            int    dbSnapshot        = _dbProvider.TakeSnapshot();
            Keccak snapshotStateRoot = _stateProvider.StateRoot;

            if (branchStateRoot != null && _stateProvider.StateRoot != branchStateRoot)
            {
                // discarding one of the branches
                _storageProvider.ClearCaches();
                _stateProvider.Reset();
                _stateProvider.StateRoot = branchStateRoot;
            }

            Block[] processedBlocks = new Block[suggestedBlocks.Length];
            try
            {
                for (int i = 0; i < suggestedBlocks.Length; i++)
                {
                    processedBlocks[i] = ProcessOne(suggestedBlocks[i], tryOnly);
                }

                if (tryOnly)
                {
                    if (_logger.IsDebugEnabled)
                    {
                        _logger.Debug($"REVERTING BLOCKS - STATE ROOT {_stateProvider.StateRoot}");
                    }

                    _dbProvider.Restore(dbSnapshot);
                    _storageProvider.ClearCaches();
                    _stateProvider.Reset();
                    _stateProvider.StateRoot = snapshotStateRoot;

                    if (_logger.IsDebugEnabled)
                    {
                        _logger.Debug($"REVERTED BLOCKS (JUST VALIDATED FOR MINING) - STATE ROOT {_stateProvider.StateRoot}");
                    }
                }

                return(processedBlocks);
            }
            catch (InvalidBlockException) // TODO: which exception to catch here?
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"REVERTING BLOCKS - STATE ROOT {_stateProvider.StateRoot}");
                }

                _dbProvider.Restore(dbSnapshot);
                _storageProvider.ClearCaches();
                _stateProvider.Reset();
                _stateProvider.StateRoot = snapshotStateRoot;

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"REVERTED BLOCKS - STATE ROOT {_stateProvider.StateRoot}");
                }

                if (_logger.IsErrorEnabled)
                {
                    _logger.Error($"THROWING INVALID BLOCK");
                }

                throw;
            }
        }
コード例 #2
0
        public Block[] Process(Keccak branchStateRoot, Block[] suggestedBlocks, bool tryOnly)
        {
            if (suggestedBlocks.Length == 0)
            {
                return(Array.Empty <Block>());
            }

            IDb    db                = _dbProvider.GetOrCreateStateDb();
            int    dbSnapshot        = _dbProvider.TakeSnapshot();
            Keccak snapshotStateRoot = _stateProvider.StateRoot;

            if (branchStateRoot != null && _stateProvider.StateRoot != branchStateRoot)
            {
                // discarding one of the branches
                _storageProvider.ClearCaches();
                _stateProvider.Reset();
                _stateProvider.StateRoot = branchStateRoot;
            }

            Block[] processedBlocks = new Block[suggestedBlocks.Length];
            try
            {
                for (int i = 0; i < suggestedBlocks.Length; i++)
                {
                    processedBlocks[i] = ProcessOne(suggestedBlocks[i], tryOnly);
                }

                if (tryOnly)
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"REVERTING BLOCKS - STATE ROOT {_stateProvider.StateRoot}");
                    }

                    _dbProvider.Restore(dbSnapshot);
                    _storageProvider.ClearCaches();
                    _stateProvider.Reset();
                    _stateProvider.StateRoot = snapshotStateRoot;

                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"REVERTED BLOCKS (JUST VALIDATED FOR MINING) - STATE ROOT {_stateProvider.StateRoot}");
                    }
                }
                else
                {
                    db.StartBatch();
                    _dbProvider.Commit(_specProvider.GetSpec(suggestedBlocks[0].Number));
                    db.CommitBatch();
                }

                return(processedBlocks);
            }
            catch (InvalidBlockException) // TODO: which exception to catch here?
            {
                if (_logger.IsTrace)
                {
                    _logger.Trace($"REVERTING BLOCKS - STATE ROOT {_stateProvider.StateRoot}");
                }

                _dbProvider.Restore(dbSnapshot);
                _storageProvider.ClearCaches();
                _stateProvider.Reset();
                _stateProvider.StateRoot = snapshotStateRoot;

                if (_logger.IsTrace)
                {
                    _logger.Trace($"REVERTED BLOCKS - STATE ROOT {_stateProvider.StateRoot}");
                }

                if (_logger.IsWarn)
                {
                    _logger.Warn($"Invalid block");
                }

                throw;
            }
        }