Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -202,6 +202,64 @@ public void ValidateProcessedBlock_StateRootIsWrong_ErrorIsSet()
Assert.That(error, Does.StartWith("InvalidStateRoot"));
}

[Test]
public void ValidateProcessedBlock_ReceiptCountMismatch_ReturnsFalse()
{
TxValidator txValidator = new(TestBlockchainIds.ChainId);
ISpecProvider specProvider = Substitute.For<ISpecProvider>();
BlockValidator sut = new(txValidator, Always.Valid, Always.Valid, specProvider, LimboLogs.Instance);
Block suggestedBlock = Build.A.Block.TestObject;
Block processedBlock = Build.A.Block
.WithStateRoot(Keccak.Zero)
.WithTransactions(2, specProvider)
.TestObject;

Assert.That(sut.ValidateProcessedBlock(
processedBlock,
[],
suggestedBlock), Is.False);
}

[Test]
public void ValidateProcessedBlock_ReceiptCountMismatch_ErrorIsSet()
{
TxValidator txValidator = new(TestBlockchainIds.ChainId);
ISpecProvider specProvider = Substitute.For<ISpecProvider>();
BlockValidator sut = new(txValidator, Always.Valid, Always.Valid, specProvider, LimboLogs.Instance);
Block suggestedBlock = Build.A.Block.TestObject;
Block processedBlock = Build.A.Block
.WithStateRoot(Keccak.Zero)
.WithTransactions(3, specProvider)
.TestObject;

sut.ValidateProcessedBlock(
processedBlock,
[Build.A.Receipt.TestObject],
suggestedBlock, out string? error);

Assert.That(error, Does.StartWith("ReceiptCountMismatch"));
}

[Test]
public void ValidateProcessedBlock_EmptyReceiptsWithTransactions_ErrorIsSet()
{
TxValidator txValidator = new(TestBlockchainIds.ChainId);
ISpecProvider specProvider = Substitute.For<ISpecProvider>();
BlockValidator sut = new(txValidator, Always.Valid, Always.Valid, specProvider, LimboLogs.Instance);
Block suggestedBlock = Build.A.Block.TestObject;
Block processedBlock = Build.A.Block
.WithStateRoot(Keccak.Zero)
.WithTransactions(1, specProvider)
.TestObject;

sut.ValidateProcessedBlock(
processedBlock,
[],
suggestedBlock, out string? error);

Assert.That(error, Does.StartWith("ReceiptCountMismatch"));
}

private static IEnumerable<TestCaseData> BadSuggestedBlocks()
{
BlockHeader parent = Build.A.BlockHeader.TestObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ public bool ValidateProcessedBlock(Block processedBlock, TxReceipt[] receipts, B
if (_logger.IsWarn) _logger.Warn($"Processed block {processedBlock.ToString(Block.Format.Short)} is invalid:");
if (_logger.IsWarn) _logger.Warn($"- hash: expected {suggestedBlock.Hash}, got {processedBlock.Hash}");
error = null;

if (receipts.Length != processedBlock.Transactions.Length)
{
if (_logger.IsWarn) _logger.Warn($"- receipt count mismatch: expected {processedBlock.Transactions.Length} receipts to match transaction count, got {receipts.Length}");
error = BlockErrorMessages.ReceiptCountMismatch(processedBlock.Transactions.Length, receipts.Length);
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Dont return. This method is just for populating error. See the next few lines.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And put this right before using the receipts. Which is for checking the transactions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 2ebfafe. Moved the check to right before the loop that uses receipts (line 215) and changed to use error ??= pattern instead of returning early.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit 2ebfafe. The check is now at line 215, right before the loop that accesses the receipts array.

}

if (processedBlock.Header.GasUsed != suggestedBlock.Header.GasUsed)
{
if (_logger.IsWarn) _logger.Warn($"- gas used: expected {suggestedBlock.Header.GasUsed}, got {processedBlock.Header.GasUsed} (diff: {processedBlock.Header.GasUsed - suggestedBlock.Header.GasUsed})");
Expand Down
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Core/Messages/BlockErrorMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,7 @@ public static string InvalidDepositEventLayout(string error) =>

public static string ExceededBlockSizeLimit(int limit) =>
$"ExceededBlockSizeLimit: Exceeded block size limit of {limit} bytes.";

public static string ReceiptCountMismatch(int expectedCount, int actualCount) =>
$"ReceiptCountMismatch: Expected {expectedCount} receipts to match transaction count, but got {actualCount}.";
}