Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid block interceptor intercepted wrong block #4300

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// 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 System;
using Nethermind.Consensus.Validators;
using Nethermind.Core;
using Nethermind.Core.Test.Builders;
Expand All @@ -41,15 +42,15 @@ public void Setup()
_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)
{
Expand All @@ -60,25 +61,26 @@ public void TestValidateSuggestedBlock(bool baseReturnValue, bool isInvalidBlock
_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;
Block suggestedBlock = Build.A.Block.WithExtraData(new byte[]{ 1 }).TestObject;
TxReceipt[] txs = { };
_baseValidator.ValidateProcessedBlock(block, txs, block).Returns(baseReturnValue);
_invalidBlockInterceptor.ValidateProcessedBlock(block, txs, block);
_tracker.Received().SetChildParent(block.Hash, block.ParentHash);
_baseValidator.ValidateProcessedBlock(block, txs, suggestedBlock).Returns(baseReturnValue);
_invalidBlockInterceptor.ValidateProcessedBlock(block, txs, suggestedBlock);

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

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// 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;
Expand All @@ -36,7 +36,7 @@ public InvalidBlockInterceptor(
_invalidChainTracker = invalidChainTracker;
_logger = logManager.GetClassLogger(typeof(InvalidBlockInterceptor));
}

public bool Validate(BlockHeader header, BlockHeader? parent, bool isUncle = false)
{
_invalidChainTracker.SetChildParent(header.Hash!, header.ParentHash!);
Expand Down Expand Up @@ -76,12 +76,12 @@ public bool ValidateSuggestedBlock(Block block)

public bool ValidateProcessedBlock(Block block, TxReceipt[] receipts, Block suggestedBlock)
{
_invalidChainTracker.SetChildParent(block.Hash!, block.ParentHash!);
_invalidChainTracker.SetChildParent(suggestedBlock.Hash!, suggestedBlock.ParentHash!);
bool result = _baseValidator.ValidateProcessedBlock(block, receipts, suggestedBlock);
if (!result)
{
if (_logger.IsTrace) _logger.Trace($"Intercepted a bad block {block}");
_invalidChainTracker.OnInvalidBlock(block.Hash!, block.ParentHash);
_invalidChainTracker.OnInvalidBlock(suggestedBlock.Hash!, suggestedBlock.ParentHash);
}

return result;
Expand Down