|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +package org.hiero.block.tools.blocks.validation; |
| 3 | + |
| 4 | +import java.time.Instant; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.LinkedHashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +/** |
| 12 | + * Per-block signature validation statistics, providing richer data than the |
| 13 | + * simple pass/fail result of {@link SignatureValidation#validate}. |
| 14 | + * |
| 15 | + * <p>Collected by {@link SignatureValidation} when detailed stats are enabled, |
| 16 | + * and delivered to {@link SignatureStatsCollector} for per-day aggregation and CSV output. |
| 17 | + * |
| 18 | + * @param blockNumber the block number |
| 19 | + * @param blockTime the block's consensus timestamp |
| 20 | + * @param stakeWeighted true if stake-weighted consensus was used |
| 21 | + * @param totalNodes total nodes in the address book |
| 22 | + * @param totalStake total stake across all nodes (equal to totalNodes in equal-weight mode) |
| 23 | + * @param threshold minimum stake required for consensus |
| 24 | + * @param validatedNodes set of node IDs that produced valid signatures |
| 25 | + * @param validatedStake accumulated stake from validated nodes |
| 26 | + * @param totalSignatures total signature entries in the block proof |
| 27 | + * @param nodeStakes stake weights per node (nodeId to stake); empty in equal-weight mode |
| 28 | + * @param perNodeResults per-node verification outcome (keyed by nodeId) |
| 29 | + */ |
| 30 | +public record SignatureBlockStats( |
| 31 | + long blockNumber, |
| 32 | + Instant blockTime, |
| 33 | + boolean stakeWeighted, |
| 34 | + int totalNodes, |
| 35 | + long totalStake, |
| 36 | + long threshold, |
| 37 | + Set<Long> validatedNodes, |
| 38 | + long validatedStake, |
| 39 | + int totalSignatures, |
| 40 | + Map<Long, Long> nodeStakes, |
| 41 | + Map<Long, NodeResult> perNodeResults) { |
| 42 | + |
| 43 | + /** Result of signature verification for a single node. */ |
| 44 | + public enum NodeResult { |
| 45 | + /** Signature was verified successfully. */ |
| 46 | + VERIFIED, |
| 47 | + /** Signature verification failed (bad signature). */ |
| 48 | + FAILED, |
| 49 | + /** Error during verification (e.g. unknown node, key lookup failure). */ |
| 50 | + ERROR, |
| 51 | + /** Node is in the address book but no signature was present. */ |
| 52 | + NOT_PRESENT |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Builds a {@link SignatureBlockStats} from pre-verified signature data. This is used by |
| 57 | + * commands that verify signatures outside of {@link SignatureValidation} (e.g. the wrap |
| 58 | + * command's Stage 1 pre-verification). |
| 59 | + * |
| 60 | + * @param blockNumber the block number |
| 61 | + * @param blockTime the block's consensus timestamp |
| 62 | + * @param verifiedNodeIds node IDs that produced valid signatures |
| 63 | + * @param totalSignatures total signature entries |
| 64 | + * @param addressBookNodeIds all node IDs from the address book |
| 65 | + * @param stakeMap node stake weights (nodeId to stake), or null for equal-weight |
| 66 | + * @return the constructed stats record |
| 67 | + */ |
| 68 | + public static SignatureBlockStats fromPreVerifiedData( |
| 69 | + long blockNumber, |
| 70 | + Instant blockTime, |
| 71 | + List<Long> verifiedNodeIds, |
| 72 | + int totalSignatures, |
| 73 | + List<Long> addressBookNodeIds, |
| 74 | + Map<Long, Long> stakeMap) { |
| 75 | + |
| 76 | + int totalNodes = addressBookNodeIds.size(); |
| 77 | + long rawTotalStake = stakeMap != null |
| 78 | + ? stakeMap.values().stream().mapToLong(Long::longValue).sum() |
| 79 | + : 0; |
| 80 | + boolean stakeWeighted = stakeMap != null && rawTotalStake > 0; |
| 81 | + long totalStake = stakeWeighted ? rawTotalStake : totalNodes; |
| 82 | + long threshold = stakeWeighted ? (totalStake / 3) + ((totalStake % 3 == 0) ? 0 : 1) : (totalNodes / 3) + 1; |
| 83 | + |
| 84 | + Set<Long> uniqueValidated = new HashSet<>(); |
| 85 | + long validatedStake = 0; |
| 86 | + Map<Long, NodeResult> perNodeResults = new LinkedHashMap<>(); |
| 87 | + for (long nodeId : verifiedNodeIds) { |
| 88 | + if (uniqueValidated.add(nodeId)) { |
| 89 | + long weight = stakeWeighted ? stakeMap.getOrDefault(nodeId, 0L) : 1; |
| 90 | + validatedStake += weight; |
| 91 | + } |
| 92 | + perNodeResults.put(nodeId, NodeResult.VERIFIED); |
| 93 | + } |
| 94 | + for (long nodeId : addressBookNodeIds) { |
| 95 | + perNodeResults.putIfAbsent(nodeId, NodeResult.NOT_PRESENT); |
| 96 | + } |
| 97 | + |
| 98 | + return new SignatureBlockStats( |
| 99 | + blockNumber, |
| 100 | + blockTime, |
| 101 | + stakeWeighted, |
| 102 | + totalNodes, |
| 103 | + totalStake, |
| 104 | + threshold, |
| 105 | + Set.copyOf(uniqueValidated), |
| 106 | + validatedStake, |
| 107 | + totalSignatures, |
| 108 | + stakeMap != null ? Map.copyOf(stakeMap) : Map.of(), |
| 109 | + Map.copyOf(perNodeResults)); |
| 110 | + } |
| 111 | +} |
0 commit comments