From 7212761b95b941abb54023d15e0e128989ccb65c Mon Sep 17 00:00:00 2001 From: Simon Dudley Date: Wed, 11 Oct 2023 10:29:01 +1000 Subject: [PATCH] Append counts to list subcommand Signed-off-by: Simon Dudley --- .../operator/TrieLogSubCommand.java | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/besu/src/main/java/org/hyperledger/besu/cli/subcommands/operator/TrieLogSubCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/subcommands/operator/TrieLogSubCommand.java index d2fc1640a61..eef2d783b68 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/subcommands/operator/TrieLogSubCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/subcommands/operator/TrieLogSubCommand.java @@ -267,6 +267,10 @@ public void run() { rangeFilter = blockHashRange::contains; } + final AtomicInteger count = new AtomicInteger(); + final AtomicInteger canonicalCount = new AtomicInteger(); + final AtomicInteger forkCount = new AtomicInteger(); + final AtomicInteger orphanCount = new AtomicInteger(); trieLogStorage .streamKeys() .map(Bytes32::wrap) @@ -274,25 +278,37 @@ public void run() { .map(Hash::fromHexString) .filter(rangeFilter) .forEach( - hash -> - out.printf( - "%s | %s\n", - hash, - blockchain - .getBlockHeader(hash) - .map( - (header) -> { - long number = header.getNumber(); - final Optional headerByNumber = - blockchain.getBlockHeader(number); - if (headerByNumber.isPresent() - && headerByNumber.get().getHash().equals(hash)) { - return String.valueOf(number); - } else { - return "fork of " + number; - } - }) - .orElse("not in blockchain"))); + hash -> { + count.getAndIncrement(); + out.printf( + "%s | %s\n", + hash, + blockchain + .getBlockHeader(hash) + .map( + (header) -> { + long number = header.getNumber(); + final Optional headerByNumber = + blockchain.getBlockHeader(number); + if (headerByNumber.isPresent() + && headerByNumber.get().getHash().equals(hash)) { + canonicalCount.getAndIncrement(); + return String.valueOf(number); + } else { + forkCount.getAndIncrement(); + return "fork of " + number; + } + }) + .orElseGet( + () -> { + orphanCount.getAndIncrement(); + return "not in blockchain"; + })); + }); + + out.printf( + "trieLog count: %s\n - canonical count: %s\n - fork count: %s\n - orphaned count: %s\n", + count, canonicalCount, forkCount, orphanCount); } else { out.println("Subcommand only works with Bonsai"); }