From 65e4072f4d00799c045eec072b6b5e4c152e53a1 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 30 Jan 2025 10:44:03 +0000 Subject: [PATCH 1/2] Always trace invalid block even if succeeds on trace --- .../Nethermind.Blockchain/LogTraceDumper.cs | 32 ++++++++++++++++--- .../Processing/BlockchainProcessor.cs | 11 ++++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/Nethermind/Nethermind.Blockchain/LogTraceDumper.cs b/src/Nethermind/Nethermind.Blockchain/LogTraceDumper.cs index 6ccaf5af62b..39687822c4b 100644 --- a/src/Nethermind/Nethermind.Blockchain/LogTraceDumper.cs +++ b/src/Nethermind/Nethermind.Blockchain/LogTraceDumper.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using Nethermind.Core; using Nethermind.Core.Crypto; @@ -47,10 +48,31 @@ public static void LogDiagnosticRlp( public static void LogDiagnosticTrace( IBlockTracer blockTracer, - Hash256 blockHash, + object blocksOrHash, ILogger logger) { string fileName = string.Empty; + string condition; + string blockHash; + if (blocksOrHash is Hash256 failedBlockHash) + { + condition = "invalid"; + blockHash = failedBlockHash.ToString(); + } + else + { + List blocks = blocksOrHash as List; + condition = "valid on rerun"; + + if (blocks.Count == 1) + { + blockHash = blocks[0].Hash.ToString(); + } + else + { + blockHash = string.Join("|", blocks.Select(b => b.Hash)); + } + } try { @@ -61,7 +83,7 @@ public static void LogDiagnosticTrace( IReadOnlyList receipts = receiptsTracer.TxReceipts; EthereumJsonSerializer.SerializeToStream(diagnosticFile, receipts, true); if (logger.IsInfo) - logger.Info($"Created a Receipts trace of invalid block {blockHash} in file {diagnosticFile.Name}"); + logger.Info($"Created a Receipts trace of {condition} block {blockHash} in file {diagnosticFile.Name}"); } if (blockTracer is GethLikeBlockMemoryTracer gethTracer) @@ -71,7 +93,7 @@ public static void LogDiagnosticTrace( IReadOnlyCollection trace = gethTracer.BuildResult(); EthereumJsonSerializer.SerializeToStream(diagnosticFile, trace, true); if (logger.IsInfo) - logger.Info($"Created a Geth-style trace of invalid block {blockHash} in file {diagnosticFile.Name}"); + logger.Info($"Created a Geth-style trace of {condition} block {blockHash} in file {diagnosticFile.Name}"); } if (blockTracer is ParityLikeBlockTracer parityTracer) @@ -81,13 +103,13 @@ public static void LogDiagnosticTrace( IReadOnlyCollection trace = parityTracer.BuildResult(); EthereumJsonSerializer.SerializeToStream(diagnosticFile, trace, true); if (logger.IsInfo) - logger.Info($"Created a Parity-style trace of invalid block {blockHash} in file {diagnosticFile.Name}"); + logger.Info($"Created a Parity-style trace of {condition} block {blockHash} in file {diagnosticFile.Name}"); } } catch (IOException e) { if (logger.IsError) - logger.Error($"Cannot save trace of block {blockHash} in file {fileName}", e); + logger.Error($"Cannot save trace of {condition} block {blockHash} in file {fileName}", e); } } diff --git a/src/Nethermind/Nethermind.Consensus/Processing/BlockchainProcessor.cs b/src/Nethermind/Nethermind.Consensus/Processing/BlockchainProcessor.cs index 7ec939b56e0..3be964b2012 100644 --- a/src/Nethermind/Nethermind.Consensus/Processing/BlockchainProcessor.cs +++ b/src/Nethermind/Nethermind.Consensus/Processing/BlockchainProcessor.cs @@ -432,15 +432,11 @@ private void TraceFailingBranch(in ProcessingBranch processingBranch, Processing processingBranch.BlocksToProcess, options, blockTracer); + BlockTraceDumper.LogDiagnosticTrace(blockTracer, processingBranch.BlocksToProcess, _logger); } catch (InvalidBlockException ex) { BlockTraceDumper.LogDiagnosticTrace(blockTracer, ex.InvalidBlock.Hash!, _logger); - Metrics.BadBlocks++; - if (ex.InvalidBlock.IsByNethermindNode()) - { - Metrics.BadBlocksByNethermindNodes++; - } } catch (Exception ex) { @@ -484,6 +480,11 @@ void DeleteInvalidBlocks(in ProcessingBranch processingBranch, Hash256 invalidBl Block? invalidBlock = processingBranch.BlocksToProcess.FirstOrDefault(b => b.Hash == invalidBlockHash); if (invalidBlock is not null) { + Metrics.BadBlocks++; + if (ex.InvalidBlock.IsByNethermindNode()) + { + Metrics.BadBlocksByNethermindNodes++; + } InvalidBlock?.Invoke(this, new IBlockchainProcessor.InvalidBlockEventArgs { InvalidBlock = invalidBlock, }); BlockTraceDumper.LogDiagnosticRlp(invalidBlock, _logger, From 888b72d6e0acb9ad111ebd6e6d647436751fda40 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 30 Jan 2025 10:45:19 +0000 Subject: [PATCH 2/2] ToString --- src/Nethermind/Nethermind.Blockchain/LogTraceDumper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Blockchain/LogTraceDumper.cs b/src/Nethermind/Nethermind.Blockchain/LogTraceDumper.cs index 39687822c4b..3b58b941cac 100644 --- a/src/Nethermind/Nethermind.Blockchain/LogTraceDumper.cs +++ b/src/Nethermind/Nethermind.Blockchain/LogTraceDumper.cs @@ -70,7 +70,7 @@ public static void LogDiagnosticTrace( } else { - blockHash = string.Join("|", blocks.Select(b => b.Hash)); + blockHash = string.Join("|", blocks.Select(b => b.Hash.ToString())); } }