Skip to content

Commit

Permalink
Fixes some hive test related to invalid hash (#4102)
Browse files Browse the repository at this point in the history
* Add invalid chain tracker

* Integrate with blockchain processor

* Forgot block validator interceptor before

* Fix test

* Minor cleanup

* Addressing comment

* Added interceptor test

* BuildInvalidPayloadStatusV1 refactor

* Small refactoring to C# event standard

* fix

Co-authored-by: lukasz.rozmej <lukasz.rozmej@gmail.com>
  • Loading branch information
asdacap and LukaszRozmej authored Jun 8, 2022
1 parent 21d474f commit ce81e1e
Show file tree
Hide file tree
Showing 24 changed files with 1,025 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class BlockchainProcessor : IBlockchainProcessor, IBlockProcessingQueue
private int _currentRecoveryQueueSize;
private readonly CompositeBlockTracer _compositeBlockTracer = new();
private Stopwatch _stopwatch = new();

public event EventHandler<IBlockchainProcessor.InvalidBlockEventArgs> InvalidBlock;

/// <summary>
///
Expand Down Expand Up @@ -418,9 +420,7 @@ void DeleteInvalidBlocks(Keccak invalidBlockHash)
if (processingBranch.BlocksToProcess[i].Hash == invalidBlockHash)
{
_blockTree.DeleteInvalidBlock(processingBranch.BlocksToProcess[i]);
if (_logger.IsDebug)
_logger.Debug(
$"Skipped processing of {processingBranch.BlocksToProcess[^1].ToString(Block.Format.FullHashAndNumber)} because of {processingBranch.BlocksToProcess[i].ToString(Block.Format.FullHashAndNumber)} is invalid");
if (_logger.IsDebug) _logger.Debug($"Skipped processing of {processingBranch.BlocksToProcess[^1].ToString(Block.Format.FullHashAndNumber)} because of {processingBranch.BlocksToProcess[i].ToString(Block.Format.FullHashAndNumber)} is invalid");
}
}
}
Expand All @@ -437,6 +437,11 @@ void DeleteInvalidBlocks(Keccak invalidBlockHash)
}
catch (InvalidBlockException ex)
{
InvalidBlock?.Invoke(this, new IBlockchainProcessor.InvalidBlockEventArgs
{
InvalidBlockHash = ex.InvalidBlockHash,
});

invalidBlockHash = ex.InvalidBlockHash;
TraceFailingBranch(
processingBranch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

using System;
using System.Threading.Tasks;
using Nethermind.Blockchain;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Evm.Tracing;

namespace Nethermind.Consensus.Processing
Expand All @@ -32,5 +34,12 @@ public interface IBlockchainProcessor : IDisposable
Block? Process(Block block, ProcessingOptions options, IBlockTracer tracer);

bool IsProcessingBlocks(ulong? maxProcessingInterval);

event EventHandler<InvalidBlockEventArgs> InvalidBlock;

public class InvalidBlockEventArgs : EventArgs
{
public Keccak InvalidBlockHash { get; init; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Threading.Tasks;
using Nethermind.Blockchain;
using Nethermind.Core;
using Nethermind.Db;
using Nethermind.Evm.Tracing;
Expand Down Expand Up @@ -69,10 +70,11 @@ public bool IsProcessingBlocks(ulong? maxProcessingInterval)
{
return _processor.IsProcessingBlocks(maxProcessingInterval);
}

#pragma warning disable 67
public event EventHandler<BlockProcessedEventArgs> BlockProcessed;
public event EventHandler<BlockProcessedEventArgs> BlockInvalid;
public event EventHandler<IBlockchainProcessor.InvalidBlockEventArgs>? InvalidBlock;
#pragma warning restore 67

public void Dispose()
Expand Down
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Core/BlockHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public string ToString(string indent)
builder.AppendLine($"{indent}Receipts Root: {ReceiptsRoot}");
builder.AppendLine($"{indent}State Root: {StateRoot}");
builder.AppendLine($"{indent}BaseFeePerGas: {BaseFeePerGas}");
builder.AppendLine($"{indent}IsPostMerge: {IsPostMerge}");
builder.AppendLine($"{indent}TotalDifficulty: {TotalDifficulty}");

return builder.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@
using Nethermind.Consensus.Processing;
using Nethermind.Consensus.Producers;
using Nethermind.Consensus.Rewards;
using Nethermind.Consensus.Transactions;
using Nethermind.Consensus.Validators;
using Nethermind.Core;
using Nethermind.Core.Specs;
using Nethermind.Core.Test;
using Nethermind.Core.Test.Blockchain;
using Nethermind.Core.Test.Builders;
using Nethermind.Core.Timers;
using Nethermind.Db;
using Nethermind.Facade.Eth;
Expand All @@ -43,11 +41,8 @@
using Nethermind.Merge.Plugin.Handlers.V1;
using Nethermind.Merge.Plugin.Synchronization;
using Nethermind.Specs;
using Nethermind.Specs.ChainSpecStyle;
using Nethermind.Specs.Forks;
using Nethermind.State;
using Nethermind.Synchronization;
using Nethermind.Synchronization.ParallelSync;
using NSubstitute;

namespace Nethermind.Merge.Plugin.Test
Expand All @@ -65,11 +60,39 @@ private IEngineRpcModule CreateEngineModule(MergeTestBlockchain chain, ISyncConf

chain.BeaconPivot = new BeaconPivot(syncConfig ?? new SyncConfig(), new MemDb(), chain.BlockTree, chain.LogManager);
BlockCacheService blockCacheService = new();
InvalidChainTracker.InvalidChainTracker invalidChainTracker = new(
chain.PoSSwitcher,
chain.BlockTree,
blockCacheService,
new TestErrorLogManager());
chain.BeaconSync = new BeaconSync(chain.BeaconPivot, chain.BlockTree, syncConfig ?? new SyncConfig(), blockCacheService, chain.LogManager);
return new EngineRpcModule(
new GetPayloadV1Handler(chain.PayloadPreparationService!, chain.LogManager),
new NewPayloadV1Handler(chain.BlockValidator, chain.BlockTree, chain.BlockchainProcessor, new InitConfig(), chain.PoSSwitcher, chain.BeaconSync, chain.BeaconPivot, blockCacheService, chain.BlockProcessingQueue, chain.BeaconSync, chain.LogManager),
new ForkchoiceUpdatedV1Handler(chain.BlockTree, chain.BlockFinalizationManager, chain.PoSSwitcher, chain.PayloadPreparationService!, blockCacheService, chain.BeaconSync, peerRefresher, chain.LogManager),
new GetPayloadV1Handler(
chain.PayloadPreparationService!,
chain.LogManager),
new NewPayloadV1Handler(
chain.BlockValidator,
chain.BlockTree,
chain.BlockchainProcessor,
new InitConfig(),
chain.PoSSwitcher,
chain.BeaconSync,
chain.BeaconPivot,
blockCacheService,
chain.BlockProcessingQueue,
invalidChainTracker,
chain.BeaconSync,
chain.LogManager),
new ForkchoiceUpdatedV1Handler(
chain.BlockTree,
chain.BlockFinalizationManager,
chain.PoSSwitcher,
chain.PayloadPreparationService!,
blockCacheService,
invalidChainTracker,
chain.BeaconSync,
peerRefresher,
chain.LogManager),
new ExecutionStatusHandler(chain.BlockTree),
new GetPayloadBodiesV1Handler(chain.BlockTree, chain.LogManager),
new ExchangeTransitionConfigurationV1Handler(chain.PoSSwitcher, chain.LogManager),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using Nethermind.Consensus.Validators;
using Nethermind.Core;
using Nethermind.Core.Test.Builders;
using Nethermind.Logging;
using Nethermind.Merge.Plugin.InvalidChainTracker;
using NSubstitute;
using NUnit.Framework;

namespace Nethermind.Merge.Plugin.Test;

public class InvalidBlockInterceptorTest
{
private IBlockValidator _baseValidator;
private IInvalidChainTracker _tracker;
private InvalidBlockInterceptor _invalidBlockInterceptor;

[SetUp]
public void Setup()
{
_baseValidator = Substitute.For<IBlockValidator>();
_tracker = Substitute.For<IInvalidChainTracker>();
_invalidBlockInterceptor = new(
_baseValidator,
_tracker,
NullLogManager.Instance);
}

[TestCase(true, false)]
[TestCase(false, true)]
public void TestValidateSuggestedBlock(bool baseReturnValue, bool isInvalidBlockReported)
{
Block block = Build.A.Block.TestObject;
_baseValidator.ValidateSuggestedBlock(block).Returns(baseReturnValue);
_invalidBlockInterceptor.ValidateSuggestedBlock(block);

_tracker.Received().SetChildParent(block.Hash, block.ParentHash);
if (isInvalidBlockReported)
{
_tracker.Received().OnInvalidBlock(block.Hash, block.ParentHash);
}
else
{
_tracker.DidNotReceive().OnInvalidBlock(block.Hash, block.ParentHash);
}
}

[TestCase(true, false)]
[TestCase(false, true)]
public void TestValidateProcessedBlock(bool baseReturnValue, bool isInvalidBlockReported)
{
Block block = Build.A.Block.TestObject;
TxReceipt[] txs = { };
_baseValidator.ValidateProcessedBlock(block, txs, block).Returns(baseReturnValue);
_invalidBlockInterceptor.ValidateProcessedBlock(block, txs, block);

_tracker.Received().SetChildParent(block.Hash, block.ParentHash);
if (isInvalidBlockReported)
{
_tracker.Received().OnInvalidBlock(block.Hash, block.ParentHash);
}
else
{
_tracker.DidNotReceive().OnInvalidBlock(block.Hash, block.ParentHash);
}
}

}
Loading

0 comments on commit ce81e1e

Please sign in to comment.