From e85a436032caab7abf022fe3fefbb00da495fa06 Mon Sep 17 00:00:00 2001 From: Matt Whitehead Date: Wed, 20 Nov 2024 13:27:28 +0000 Subject: [PATCH 1/3] Don't create reward account if no reward (#7826) * Don't create reward account if no reward Signed-off-by: Matthew Whitehead * Remove unnecessary test stub Signed-off-by: Matthew Whitehead * Extra check to only create 0 balance accounts if clearEmptyAccounts isn't set Signed-off-by: Matthew Whitehead --------- Signed-off-by: Matthew Whitehead --- .../besu/ethereum/mainnet/MainnetTransactionProcessor.java | 7 ++++--- .../ethereum/mainnet/MainnetTransactionProcessorTest.java | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java index 5ff2129972f..c662c188644 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessor.java @@ -532,9 +532,10 @@ public TransactionProcessingResult processTransaction( coinbaseCalculator.price(usedGas, transactionGasPrice, blockHeader.getBaseFee()); operationTracer.traceBeforeRewardTransaction(worldUpdater, transaction, coinbaseWeiDelta); - - final var coinbase = evmWorldUpdater.getOrCreate(miningBeneficiary); - coinbase.incrementBalance(coinbaseWeiDelta); + if (!coinbaseWeiDelta.isZero() || !clearEmptyAccounts) { + final var coinbase = evmWorldUpdater.getOrCreate(miningBeneficiary); + coinbase.incrementBalance(coinbaseWeiDelta); + } operationTracer.traceEndTransaction( evmWorldUpdater.updater(), diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessorTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessorTest.java index d8d9f3777ea..c40da7210e2 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessorTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/mainnet/MainnetTransactionProcessorTest.java @@ -108,7 +108,6 @@ void shouldWarmCoinbaseIfRequested() { .thenReturn(ValidationResult.valid()); when(transactionValidatorFactory.get().validateForSender(any(), any(), any())) .thenReturn(ValidationResult.valid()); - when(worldState.getOrCreate(any())).thenReturn(senderAccount); when(worldState.getOrCreateSenderAccount(any())).thenReturn(senderAccount); when(worldState.updater()).thenReturn(worldState); From 73a1e5beeecf8be2a09a48234ae3c278c079fe2b Mon Sep 17 00:00:00 2001 From: Fabio Di Fabio Date: Wed, 20 Nov 2024 17:38:39 +0100 Subject: [PATCH 2/3] Simplify difficulty calculator interface (#7893) Signed-off-by: Fabio Di Fabio --- .../CliqueBesuControllerBuilder.java | 6 +-- .../clique/CliqueDifficultyCalculator.java | 8 +--- .../besu/consensus/clique/CliqueHelpers.java | 18 ++++++-- .../consensus/clique/CliqueMiningTracker.java | 5 +-- .../clique/CliqueProtocolSchedule.java | 43 +------------------ .../CliqueDifficultyValidationRule.java | 3 +- .../CliqueDifficultyCalculatorTest.java | 14 ++---- .../clique/CliqueProtocolScheduleTest.java | 6 +++ .../blockcreation/CliqueBlockCreatorTest.java | 9 ++-- .../CliqueMinerExecutorTest.java | 10 +++-- .../CliqueMiningCoordinatorTest.java | 2 + .../CliqueDifficultyValidationRuleTest.java | 2 + .../bft/BaseBftProtocolScheduleBuilder.java | 2 +- .../merge/MergeProtocolSchedule.java | 2 +- .../merge/MergeProtocolScheduleTest.java | 3 +- .../blockcreation/AbstractBlockCreator.java | 3 +- .../fixed/FixedDifficultyCalculators.java | 2 +- .../mainnet/ClassicDifficultyCalculators.java | 8 ++-- .../mainnet/DifficultyCalculator.java | 4 +- .../mainnet/MainnetDifficultyCalculators.java | 25 ++++------- .../CalculatedDifficultyValidationRule.java | 2 +- .../fixed/FixedProtocolScheduleTest.java | 6 +-- .../referencetests/ReferenceTestEnv.java | 3 +- .../mainnet/DifficultyCalculatorTests.java | 2 +- 24 files changed, 71 insertions(+), 117 deletions(-) diff --git a/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java index edb55a11265..ccd4363412d 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/CliqueBesuControllerBuilder.java @@ -14,12 +14,11 @@ */ package org.hyperledger.besu.controller; -import static org.hyperledger.besu.consensus.clique.CliqueHelpers.installCliqueBlockChoiceRule; - import org.hyperledger.besu.config.CliqueConfigOptions; import org.hyperledger.besu.consensus.clique.CliqueBlockInterface; import org.hyperledger.besu.consensus.clique.CliqueContext; import org.hyperledger.besu.consensus.clique.CliqueForksSchedulesFactory; +import org.hyperledger.besu.consensus.clique.CliqueHelpers; import org.hyperledger.besu.consensus.clique.CliqueMiningTracker; import org.hyperledger.besu.consensus.clique.CliqueProtocolSchedule; import org.hyperledger.besu.consensus.clique.blockcreation.CliqueBlockScheduler; @@ -167,7 +166,8 @@ protected CliqueContext createConsensusContext( blockchain, epochManager, blockInterface), epochManager, blockInterface); - installCliqueBlockChoiceRule(blockchain, cliqueContext); + CliqueHelpers.setCliqueContext(cliqueContext); + CliqueHelpers.installCliqueBlockChoiceRule(blockchain, cliqueContext); return cliqueContext; } diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculator.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculator.java index a5771238d2a..b0869feb23c 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculator.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculator.java @@ -15,7 +15,6 @@ package org.hyperledger.besu.consensus.clique; import org.hyperledger.besu.datatypes.Address; -import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.mainnet.DifficultyCalculator; @@ -39,12 +38,9 @@ public CliqueDifficultyCalculator(final Address localAddress) { } @Override - public BigInteger nextDifficulty( - final long time, final BlockHeader parent, final ProtocolContext context) { + public BigInteger nextDifficulty(final long time, final BlockHeader parent) { - final Address nextProposer = - CliqueHelpers.getProposerForBlockAfter( - parent, context.getConsensusContext(CliqueContext.class).getValidatorProvider()); + final Address nextProposer = CliqueHelpers.getProposerForBlockAfter(parent); return nextProposer.equals(localAddress) ? IN_TURN_DIFFICULTY : OUT_OF_TURN_DIFFICULTY; } } diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueHelpers.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueHelpers.java index cba98a5a5eb..d64fe899a66 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueHelpers.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueHelpers.java @@ -27,9 +27,20 @@ /** The Clique helpers. */ public class CliqueHelpers { + private static CliqueContext cliqueContext; + /** Default constructor. */ CliqueHelpers() {} + /** + * Set the clique consensus context + * + * @param cliqueContext the clique consensus context + */ + public static void setCliqueContext(final CliqueContext cliqueContext) { + CliqueHelpers.cliqueContext = cliqueContext; + } + /** * Gets proposer of block. * @@ -45,12 +56,11 @@ public static Address getProposerOfBlock(final BlockHeader header) { * Gets proposer for block after. * * @param parent the parent - * @param validatorProvider the validator provider * @return the proposer for block after */ - static Address getProposerForBlockAfter( - final BlockHeader parent, final ValidatorProvider validatorProvider) { - final CliqueProposerSelector proposerSelector = new CliqueProposerSelector(validatorProvider); + static Address getProposerForBlockAfter(final BlockHeader parent) { + final CliqueProposerSelector proposerSelector = + new CliqueProposerSelector(cliqueContext.getValidatorProvider()); return proposerSelector.selectProposerForNextBlock(parent); } diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueMiningTracker.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueMiningTracker.java index d6362122835..8f350609dc8 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueMiningTracker.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueMiningTracker.java @@ -42,10 +42,7 @@ public CliqueMiningTracker(final Address localAddress, final ProtocolContext pro * @return the boolean */ public boolean isProposerAfter(final BlockHeader header) { - final Address nextProposer = - CliqueHelpers.getProposerForBlockAfter( - header, - protocolContext.getConsensusContext(CliqueContext.class).getValidatorProvider()); + final Address nextProposer = CliqueHelpers.getProposerForBlockAfter(header); return localAddress.equals(nextProposer); } diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueProtocolSchedule.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueProtocolSchedule.java index 2ceedb71893..a60fcc58125 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueProtocolSchedule.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/CliqueProtocolSchedule.java @@ -44,8 +44,6 @@ import java.util.Optional; import java.util.function.Function; -import com.google.common.annotations.VisibleForTesting; - /** Defines the protocol behaviours for a blockchain using Clique. */ public class CliqueProtocolSchedule { @@ -63,7 +61,7 @@ public class CliqueProtocolSchedule { * @param privacyParameters the privacy parameters * @param isRevertReasonEnabled the is revert reason enabled * @param evmConfiguration the evm configuration - * @param miningConfiguration the mining parameters + * @param miningConfiguration the mining configuration * @param badBlockManager the cache to use to keep invalid blocks * @param isParallelTxProcessingEnabled indicates whether parallel transaction is enabled * @param metricsSystem A metricSystem instance to be able to expose metrics in the underlying @@ -122,45 +120,6 @@ public static ProtocolSchedule create( .createProtocolSchedule(); } - /** - * Create protocol schedule. - * - * @param config the config - * @param forksSchedule the transitions - * @param nodeKey the node key - * @param isRevertReasonEnabled the is revert reason enabled - * @param evmConfiguration the evm configuration - * @param miningConfiguration the mining parameters - * @param badBlockManager the cache to use to keep invalid blocks - * @param isParallelTxProcessingEnabled indicates whether parallel transaction is enabled - * @param metricsSystem A metricSystem instance to be able to expose metrics in the underlying - * calls - * @return the protocol schedule - */ - @VisibleForTesting - public static ProtocolSchedule create( - final GenesisConfigOptions config, - final ForksSchedule forksSchedule, - final NodeKey nodeKey, - final boolean isRevertReasonEnabled, - final EvmConfiguration evmConfiguration, - final MiningConfiguration miningConfiguration, - final BadBlockManager badBlockManager, - final boolean isParallelTxProcessingEnabled, - final MetricsSystem metricsSystem) { - return create( - config, - forksSchedule, - nodeKey, - PrivacyParameters.DEFAULT, - isRevertReasonEnabled, - evmConfiguration, - miningConfiguration, - badBlockManager, - isParallelTxProcessingEnabled, - metricsSystem); - } - private static ProtocolSpecBuilder applyCliqueSpecificModifications( final EpochManager epochManager, final long secondsBetweenBlocks, diff --git a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRule.java b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRule.java index a14b5187ef7..87c83b2cb08 100644 --- a/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRule.java +++ b/consensus/clique/src/main/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRule.java @@ -38,10 +38,9 @@ public CliqueDifficultyValidationRule() {} public boolean validate( final BlockHeader header, final BlockHeader parent, final ProtocolContext protocolContext) { final Address actualBlockCreator = CliqueHelpers.getProposerOfBlock(header); - final CliqueDifficultyCalculator diffCalculator = new CliqueDifficultyCalculator(actualBlockCreator); - final BigInteger expectedDifficulty = diffCalculator.nextDifficulty(0, parent, protocolContext); + final BigInteger expectedDifficulty = diffCalculator.nextDifficulty(0, parent); final BigInteger actualDifficulty = header.getDifficulty().toBigInteger(); diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculatorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculatorTest.java index 3c9da2520d9..74537b2ad1a 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculatorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueDifficultyCalculatorTest.java @@ -23,8 +23,6 @@ import org.hyperledger.besu.crypto.KeyPair; import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; import org.hyperledger.besu.datatypes.Address; -import org.hyperledger.besu.ethereum.ProtocolContext; -import org.hyperledger.besu.ethereum.chain.BadBlockManager; import org.hyperledger.besu.ethereum.core.AddressHelpers; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; @@ -43,9 +41,7 @@ public class CliqueDifficultyCalculatorTest { private Address localAddr; private final List
validatorList = Lists.newArrayList(); - private ProtocolContext cliqueProtocolContext; private BlockHeaderTestFixture blockHeaderBuilder; - private final CliqueBlockInterface blockInterface = new CliqueBlockInterface(); @BeforeEach public void setup() { @@ -56,9 +52,7 @@ public void setup() { final ValidatorProvider validatorProvider = mock(ValidatorProvider.class); when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); - - final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); - cliqueProtocolContext = new ProtocolContext(null, null, cliqueContext, new BadBlockManager()); + CliqueHelpers.setCliqueContext(new CliqueContext(validatorProvider, null, null)); blockHeaderBuilder = new BlockHeaderTestFixture(); } @@ -68,8 +62,7 @@ public void inTurnValidatorProducesDifficultyOfTwo() { final BlockHeader parentHeader = blockHeaderBuilder.number(1).buildHeader(); - assertThat(calculator.nextDifficulty(0, parentHeader, cliqueProtocolContext)) - .isEqualTo(BigInteger.valueOf(2)); + assertThat(calculator.nextDifficulty(0, parentHeader)).isEqualTo(BigInteger.valueOf(2)); } @Test @@ -78,7 +71,6 @@ public void outTurnValidatorProducesDifficultyOfOne() { final BlockHeader parentHeader = blockHeaderBuilder.number(2).buildHeader(); - assertThat(calculator.nextDifficulty(0, parentHeader, cliqueProtocolContext)) - .isEqualTo(BigInteger.valueOf(1)); + assertThat(calculator.nextDifficulty(0, parentHeader)).isEqualTo(BigInteger.valueOf(1)); } } diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueProtocolScheduleTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueProtocolScheduleTest.java index 083604af6f0..47741206229 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueProtocolScheduleTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/CliqueProtocolScheduleTest.java @@ -33,6 +33,7 @@ import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.MiningConfiguration; +import org.hyperledger.besu.ethereum.core.PrivacyParameters; import org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec; @@ -66,6 +67,7 @@ public void protocolSpecsAreCreatedAtBlockDefinedInJson() { config, new ForksSchedule<>(List.of()), NODE_KEY, + PrivacyParameters.DEFAULT, false, EvmConfiguration.DEFAULT, MiningConfiguration.MINING_DISABLED, @@ -92,6 +94,7 @@ public void parametersAlignWithMainnetWithAdjustments() { GenesisConfigFile.DEFAULT.getConfigOptions(), forksSchedule, NODE_KEY, + PrivacyParameters.DEFAULT, false, EvmConfiguration.DEFAULT, MiningConfiguration.MINING_DISABLED, @@ -118,6 +121,7 @@ public void zeroEpochLengthThrowsException() { genesisConfig, new ForksSchedule<>(List.of()), NODE_KEY, + PrivacyParameters.DEFAULT, false, EvmConfiguration.DEFAULT, MiningConfiguration.MINING_DISABLED, @@ -140,6 +144,7 @@ public void negativeEpochLengthThrowsException() { genesisConfig, new ForksSchedule<>(List.of()), NODE_KEY, + PrivacyParameters.DEFAULT, false, EvmConfiguration.DEFAULT, MiningConfiguration.MINING_DISABLED, @@ -166,6 +171,7 @@ public void shouldValidateBaseFeeMarketTransition() { config, forksSchedule, NODE_KEY, + PrivacyParameters.DEFAULT, false, EvmConfiguration.DEFAULT, MiningConfiguration.MINING_DISABLED, diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java index 12467cda4ee..0ece2823327 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java @@ -122,6 +122,7 @@ void setup() { new NoOpMetricsSystem()); final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); + CliqueHelpers.setCliqueContext(cliqueContext); final Block genesis = GenesisState.fromConfig(GenesisConfigFile.mainnet(), protocolSchedule).getBlock(); @@ -149,7 +150,7 @@ public void proposerAddressCanBeExtractFromAConstructedBlock() { final Address coinbase = AddressHelpers.ofValue(1); - final MiningConfiguration miningConfiguration = createMiningParameters(extraData, coinbase); + final MiningConfiguration miningConfiguration = createMiningConfiguration(extraData, coinbase); final CliqueBlockCreator blockCreator = new CliqueBlockCreator( @@ -178,7 +179,7 @@ public void insertsValidVoteIntoConstructedBlock() { when(voteProvider.getVoteAfterBlock(any(), any())) .thenReturn(Optional.of(new ValidatorVote(VoteType.ADD, coinbase, a1))); - final MiningConfiguration miningConfiguration = createMiningParameters(extraData, coinbase); + final MiningConfiguration miningConfiguration = createMiningConfiguration(extraData, coinbase); final CliqueBlockCreator blockCreator = new CliqueBlockCreator( @@ -212,7 +213,7 @@ public void insertsNoVoteWhenAtEpoch() { when(mockVoteProvider.getVoteAfterBlock(any(), any())) .thenReturn(Optional.of(new ValidatorVote(VoteType.ADD, coinbase, a1))); - final MiningConfiguration miningConfiguration = createMiningParameters(extraData, coinbase); + final MiningConfiguration miningConfiguration = createMiningConfiguration(extraData, coinbase); final CliqueBlockCreator blockCreator = new CliqueBlockCreator( @@ -255,7 +256,7 @@ private TransactionPool createTransactionPool() { return transactionPool; } - private static MiningConfiguration createMiningParameters( + private static MiningConfiguration createMiningConfiguration( final Bytes extraData, final Address coinbase) { final MiningConfiguration miningConfiguration = ImmutableMiningConfiguration.builder() diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java index 7000d145b6b..9c2bf04bce7 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java @@ -26,6 +26,7 @@ import org.hyperledger.besu.consensus.clique.CliqueBlockInterface; import org.hyperledger.besu.consensus.clique.CliqueContext; import org.hyperledger.besu.consensus.clique.CliqueExtraData; +import org.hyperledger.besu.consensus.clique.CliqueHelpers; import org.hyperledger.besu.consensus.clique.CliqueProtocolSchedule; import org.hyperledger.besu.consensus.common.EpochManager; import org.hyperledger.besu.consensus.common.ForksSchedule; @@ -98,6 +99,7 @@ public void setup() { when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); + CliqueHelpers.setCliqueContext(cliqueContext); cliqueProtocolContext = new ProtocolContext(null, null, cliqueContext, new BadBlockManager()); cliqueProtocolSchedule = CliqueProtocolSchedule.create( @@ -119,7 +121,7 @@ public void setup() { public void extraDataCreatedOnEpochBlocksContainsValidators() { final Bytes vanityData = generateRandomVanityData(); - final MiningConfiguration miningConfiguration = createMiningParameters(vanityData); + final MiningConfiguration miningConfiguration = createMiningConfiguration(vanityData); final CliqueMinerExecutor executor = new CliqueMinerExecutor( @@ -155,7 +157,7 @@ public void extraDataCreatedOnEpochBlocksContainsValidators() { public void extraDataForNonEpochBlocksDoesNotContainValidaors() { final Bytes vanityData = generateRandomVanityData(); - final MiningConfiguration miningConfiguration = createMiningParameters(vanityData); + final MiningConfiguration miningConfiguration = createMiningConfiguration(vanityData); final CliqueMinerExecutor executor = new CliqueMinerExecutor( @@ -191,7 +193,7 @@ public void shouldUseLatestVanityData() { final Bytes initialVanityData = generateRandomVanityData(); final Bytes modifiedVanityData = generateRandomVanityData(); - final MiningConfiguration miningConfiguration = createMiningParameters(initialVanityData); + final MiningConfiguration miningConfiguration = createMiningConfiguration(initialVanityData); final CliqueMinerExecutor executor = new CliqueMinerExecutor( @@ -255,7 +257,7 @@ private Bytes generateRandomVanityData() { return Bytes.wrap(vanityData); } - private static MiningConfiguration createMiningParameters(final Bytes vanityData) { + private static MiningConfiguration createMiningConfiguration(final Bytes vanityData) { return ImmutableMiningConfiguration.builder() .mutableInitValues( MutableInitValues.builder() diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMiningCoordinatorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMiningCoordinatorTest.java index 543d110ef45..fd39a46cfcf 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMiningCoordinatorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMiningCoordinatorTest.java @@ -25,6 +25,7 @@ import org.hyperledger.besu.consensus.clique.CliqueBlockInterface; import org.hyperledger.besu.consensus.clique.CliqueContext; +import org.hyperledger.besu.consensus.clique.CliqueHelpers; import org.hyperledger.besu.consensus.clique.CliqueMiningTracker; import org.hyperledger.besu.consensus.clique.TestHelpers; import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; @@ -258,6 +259,7 @@ private void setupCliqueContextAndBlockchain() { when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators); final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); + CliqueHelpers.setCliqueContext(cliqueContext); when(protocolContext.getConsensusContext(CliqueContext.class)).thenReturn(cliqueContext); when(protocolContext.getBlockchain()).thenReturn(blockChain); diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java index d970d83d80e..d7e4d074091 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/headervalidationrules/CliqueDifficultyValidationRuleTest.java @@ -21,6 +21,7 @@ import org.hyperledger.besu.consensus.clique.CliqueBlockInterface; import org.hyperledger.besu.consensus.clique.CliqueContext; +import org.hyperledger.besu.consensus.clique.CliqueHelpers; import org.hyperledger.besu.consensus.clique.TestHelpers; import org.hyperledger.besu.consensus.common.validator.ValidatorProvider; import org.hyperledger.besu.crypto.KeyPair; @@ -58,6 +59,7 @@ public void setup() { when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList); final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface); + CliqueHelpers.setCliqueContext(cliqueContext); cliqueProtocolContext = new ProtocolContext(null, null, cliqueContext, new BadBlockManager()); blockHeaderBuilder = new BlockHeaderTestFixture(); } diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BaseBftProtocolScheduleBuilder.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BaseBftProtocolScheduleBuilder.java index 29ec30d6679..f92b58bbfa6 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BaseBftProtocolScheduleBuilder.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/bft/BaseBftProtocolScheduleBuilder.java @@ -133,7 +133,7 @@ private ProtocolSpecBuilder applyBftChanges( .blockBodyValidatorBuilder(MainnetBlockBodyValidator::new) .blockValidatorBuilder(MainnetProtocolSpecs.blockValidatorBuilder()) .blockImporterBuilder(MainnetBlockImporter::new) - .difficultyCalculator((time, parent, protocolContext) -> BigInteger.ONE) + .difficultyCalculator((time, parent) -> BigInteger.ONE) .skipZeroBlockRewards(true) .blockHeaderFunctions(BftBlockHeaderFunctions.forOnchainBlock(bftExtraDataCodec)) .blockReward(Wei.of(configOptions.getBlockRewardWei())) diff --git a/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/MergeProtocolSchedule.java b/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/MergeProtocolSchedule.java index a8c84d5a093..66e177efeef 100644 --- a/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/MergeProtocolSchedule.java +++ b/consensus/merge/src/main/java/org/hyperledger/besu/consensus/merge/MergeProtocolSchedule.java @@ -129,7 +129,7 @@ private static ProtocolSpecBuilder applyParisSpecificModifications( gasCalculator, chainId.orElse(BigInteger.ZERO), EvmConfiguration.DEFAULT)) .blockHeaderValidatorBuilder(MergeProtocolSchedule::getBlockHeaderValidator) .blockReward(Wei.ZERO) - .difficultyCalculator((a, b, c) -> BigInteger.ZERO) + .difficultyCalculator((a, b) -> BigInteger.ZERO) .skipZeroBlockRewards(true) .isPoS(true) .name("Paris"); diff --git a/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/MergeProtocolScheduleTest.java b/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/MergeProtocolScheduleTest.java index ba83f8aad9c..2fff61a8299 100644 --- a/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/MergeProtocolScheduleTest.java +++ b/consensus/merge/src/test/java/org/hyperledger/besu/consensus/merge/MergeProtocolScheduleTest.java @@ -193,8 +193,7 @@ public void parametersAlignWithMainnetWithAdjustments() { private static void assertProofOfStakeConfigIsEnabled(final ProtocolSpec spec) { assertThat(spec.isPoS()).isTrue(); assertThat(spec.getEvm().getOperationsUnsafe()[0x44]).isInstanceOf(PrevRanDaoOperation.class); - assertThat(spec.getDifficultyCalculator().nextDifficulty(-1, null, null)) - .isEqualTo(BigInteger.ZERO); + assertThat(spec.getDifficultyCalculator().nextDifficulty(-1, null)).isEqualTo(BigInteger.ZERO); assertThat(spec.getBlockReward()).isEqualTo(Wei.ZERO); assertThat(spec.isSkipZeroBlockRewards()).isTrue(); assertThat(spec.getBlockProcessor()).isInstanceOf(MainnetBlockProcessor.class); diff --git a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java index 51cde269e05..c4a3f89aaa3 100644 --- a/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java +++ b/ethereum/blockcreation/src/main/java/org/hyperledger/besu/ethereum/blockcreation/AbstractBlockCreator.java @@ -437,8 +437,7 @@ private ProcessableBlockHeader createPendingBlockHeader( newBlockNumber); final DifficultyCalculator difficultyCalculator = protocolSpec.getDifficultyCalculator(); - final BigInteger difficulty = - difficultyCalculator.nextDifficulty(timestamp, parentHeader, protocolContext); + final BigInteger difficulty = difficultyCalculator.nextDifficulty(timestamp, parentHeader); final Wei baseFee = Optional.of(protocolSpec.getFeeMarket()) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedDifficultyCalculators.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedDifficultyCalculators.java index ceaf06e3530..692d3810c31 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedDifficultyCalculators.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedDifficultyCalculators.java @@ -34,6 +34,6 @@ public static boolean isFixedDifficultyInConfig(final GenesisConfigOptions confi public static DifficultyCalculator calculator(final GenesisConfigOptions config) { long difficulty = config.getEthashConfigOptions().getFixedDifficulty().getAsLong(); - return (time, parent, context) -> BigInteger.valueOf(difficulty); + return (time, parent) -> BigInteger.valueOf(difficulty); } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ClassicDifficultyCalculators.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ClassicDifficultyCalculators.java index 9a8f30d01f4..6e15eb87e09 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ClassicDifficultyCalculators.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/ClassicDifficultyCalculators.java @@ -32,7 +32,7 @@ public abstract class ClassicDifficultyCalculators { private static final long DELAY = (CONTINUE_BLOCK - PAUSE_BLOCK) / EXPONENTIAL_DIFF_PERIOD; public static DifficultyCalculator DIFFICULTY_BOMB_PAUSED = - (time, parent, protocolContext) -> { + (time, parent) -> { final BigInteger parentDifficulty = difficulty(parent.getDifficulty()); final BigInteger difficulty = ensureMinimumDifficulty( @@ -43,7 +43,7 @@ public abstract class ClassicDifficultyCalculators { }; public static DifficultyCalculator DIFFICULTY_BOMB_DELAYED = - (time, parent, protocolContext) -> { + (time, parent) -> { final BigInteger parentDifficulty = difficulty(parent.getDifficulty()); final BigInteger difficulty = ensureMinimumDifficulty( @@ -55,7 +55,7 @@ public abstract class ClassicDifficultyCalculators { }; public static DifficultyCalculator DIFFICULTY_BOMB_REMOVED = - (time, parent, protocolContext) -> { + (time, parent) -> { final BigInteger parentDifficulty = difficulty(parent.getDifficulty()); final BigInteger difficulty = ensureMinimumDifficulty( @@ -66,7 +66,7 @@ public abstract class ClassicDifficultyCalculators { }; public static DifficultyCalculator EIP100 = - (time, parent, protocolContext) -> { + (time, parent) -> { final BigInteger parentDifficulty = difficulty(parent.getDifficulty()); final boolean hasOmmers = !parent.getOmmersHash().equals(Hash.EMPTY_LIST_HASH); final BigInteger difficulty = diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculator.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculator.java index d03bede977d..408f83cb510 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculator.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculator.java @@ -14,7 +14,6 @@ */ package org.hyperledger.besu.ethereum.mainnet; -import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.core.BlockHeader; import java.math.BigInteger; @@ -28,8 +27,7 @@ public interface DifficultyCalculator { * * @param time the time the block was generated * @param parent the block's parent block header - * @param context the context in which the difficulty calculator should operate * @return the block difficulty */ - BigInteger nextDifficulty(long time, BlockHeader parent, ProtocolContext context); + BigInteger nextDifficulty(long time, BlockHeader parent); } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetDifficultyCalculators.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetDifficultyCalculators.java index 46f0a05e5dc..2129b10d5d0 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetDifficultyCalculators.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/MainnetDifficultyCalculators.java @@ -45,7 +45,7 @@ public abstract class MainnetDifficultyCalculators { private MainnetDifficultyCalculators() {} static final DifficultyCalculator FRONTIER = - (time, parent, protocolContext) -> { + (time, parent) -> { final BigInteger parentDifficulty = difficulty(parent.getDifficulty()); final BigInteger adjust = parentDifficulty.divide(DIFFICULTY_BOUND_DIVISOR); BigInteger difficulty; @@ -60,7 +60,7 @@ private MainnetDifficultyCalculators() {} }; static final DifficultyCalculator HOMESTEAD = - (time, parent, protocolContext) -> { + (time, parent) -> { final BigInteger parentDifficulty = difficulty(parent.getDifficulty()); final BigInteger difficulty = ensureMinimumDifficulty( @@ -72,35 +72,28 @@ private MainnetDifficultyCalculators() {} }; static final DifficultyCalculator BYZANTIUM = - (time, parent, protocolContext) -> - calculateThawedDifficulty(time, parent, BYZANTIUM_FAKE_BLOCK_OFFSET); + (time, parent) -> calculateThawedDifficulty(time, parent, BYZANTIUM_FAKE_BLOCK_OFFSET); static final DifficultyCalculator CONSTANTINOPLE = - (time, parent, protocolContext) -> - calculateThawedDifficulty(time, parent, CONSTANTINOPLE_FAKE_BLOCK_OFFSET); + (time, parent) -> calculateThawedDifficulty(time, parent, CONSTANTINOPLE_FAKE_BLOCK_OFFSET); static final DifficultyCalculator MUIR_GLACIER = - (time, parent, protocolContext) -> - calculateThawedDifficulty(time, parent, MUIR_GLACIER_FAKE_BLOCK_OFFSET); + (time, parent) -> calculateThawedDifficulty(time, parent, MUIR_GLACIER_FAKE_BLOCK_OFFSET); // As per https://eips.ethereum.org/EIPS/eip-3554 static final DifficultyCalculator LONDON = - (time, parent, protocolContext) -> - calculateThawedDifficulty(time, parent, LONDON_FAKE_BLOCK_OFFSET); + (time, parent) -> calculateThawedDifficulty(time, parent, LONDON_FAKE_BLOCK_OFFSET); // As per https://eips.ethereum.org/EIPS/eip-4345 static final DifficultyCalculator ARROW_GLACIER = - (time, parent, protocolContext) -> - calculateThawedDifficulty(time, parent, ARROW_GLACIER_FAKE_BLOCK_OFFSET); + (time, parent) -> calculateThawedDifficulty(time, parent, ARROW_GLACIER_FAKE_BLOCK_OFFSET); // As per https://eips.ethereum.org/EIPS/eip-5133 static final DifficultyCalculator GRAY_GLACIER = - (time, parent, protocolContext) -> - calculateThawedDifficulty(time, parent, GRAY_GLACIER_FAKE_BLOCK_OFFSET); + (time, parent) -> calculateThawedDifficulty(time, parent, GRAY_GLACIER_FAKE_BLOCK_OFFSET); // Proof-of-Stake difficulty must not be altered - static final DifficultyCalculator PROOF_OF_STAKE_DIFFICULTY = - (time, parent, protocolContext) -> BigInteger.ZERO; + static final DifficultyCalculator PROOF_OF_STAKE_DIFFICULTY = (time, parent) -> BigInteger.ZERO; private static BigInteger calculateThawedDifficulty( final long time, final BlockHeader parent, final long fakeBlockOffset) { diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/CalculatedDifficultyValidationRule.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/CalculatedDifficultyValidationRule.java index bcc4e213586..1eb4f044c2f 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/CalculatedDifficultyValidationRule.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/mainnet/headervalidationrules/CalculatedDifficultyValidationRule.java @@ -39,7 +39,7 @@ public boolean validate( final BigInteger actualDifficulty = new BigInteger(1, header.getDifficulty().toArray()); final BigInteger expectedDifficulty = - difficultyCalculator.nextDifficulty(header.getTimestamp(), parent, context); + difficultyCalculator.nextDifficulty(header.getTimestamp(), parent); if (actualDifficulty.compareTo(expectedDifficulty) != 0) { LOG.info( diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedProtocolScheduleTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedProtocolScheduleTest.java index 02bb41feb90..9a6ddeaeb73 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedProtocolScheduleTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/difficulty/fixed/FixedProtocolScheduleTest.java @@ -49,21 +49,21 @@ public void reportedDifficultyForAllBlocksIsAFixedValue() { schedule .getByBlockHeader(blockHeader(0)) .getDifficultyCalculator() - .nextDifficulty(1, parentHeader, null)) + .nextDifficulty(1, parentHeader)) .isEqualTo(FixedDifficultyCalculators.DEFAULT_DIFFICULTY); assertThat( schedule .getByBlockHeader(blockHeader(500)) .getDifficultyCalculator() - .nextDifficulty(1, parentHeader, null)) + .nextDifficulty(1, parentHeader)) .isEqualTo(FixedDifficultyCalculators.DEFAULT_DIFFICULTY); assertThat( schedule .getByBlockHeader(blockHeader(500_000)) .getDifficultyCalculator() - .nextDifficulty(1, parentHeader, null)) + .nextDifficulty(1, parentHeader)) .isEqualTo(FixedDifficultyCalculators.DEFAULT_DIFFICULTY); } diff --git a/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/ReferenceTestEnv.java b/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/ReferenceTestEnv.java index 25c0cd24841..5c571986698 100644 --- a/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/ReferenceTestEnv.java +++ b/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/ReferenceTestEnv.java @@ -224,8 +224,7 @@ public BlockHeader parentBlockHeader(final ProtocolSpec protocolSpec) { BlockHeaderBuilder.createDefault() .difficulty(Difficulty.fromHexOrDecimalString(parentDifficulty)) .number(number - 1) - .buildBlockHeader(), - null))); + .buildBlockHeader()))); } if (parentExcessBlobGas != null && parentBlobGasUsed != null) { builder.excessBlobGas(BlobGas.of(Long.decode(parentExcessBlobGas))); diff --git a/ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculatorTests.java b/ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculatorTests.java index 3333f02e585..ea09a1eb983 100644 --- a/ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculatorTests.java +++ b/ethereum/referencetests/src/reference-test/java/org/hyperledger/besu/ethereum/mainnet/DifficultyCalculatorTests.java @@ -181,7 +181,7 @@ private void testDifficulty( UInt256.fromHexString(value.get("currentDifficulty").asText()); final var spec = protocolSchedule.getByBlockHeader(testHeader); final var calculator = spec.getDifficultyCalculator(); - assertThat(UInt256.valueOf(calculator.nextDifficulty(currentTime, testHeader, null))) + assertThat(UInt256.valueOf(calculator.nextDifficulty(currentTime, testHeader))) .describedAs("File %s Test %s", testFile, entry.getKey()) .isEqualTo(currentDifficulty); } From fe462896d293e878721318389aae18c2ed06030a Mon Sep 17 00:00:00 2001 From: Fabio Di Fabio Date: Wed, 20 Nov 2024 18:42:36 +0100 Subject: [PATCH 3/3] Add support for counter metric with external supplied value (#7894) * Add support for counter metric with external supplied value Signed-off-by: Fabio Di Fabio * Update CHANGELOG Signed-off-by: Fabio Di Fabio --------- Signed-off-by: Fabio Di Fabio --- CHANGELOG.md | 3 +- .../org/hyperledger/besu/cli/BesuCommand.java | 2 +- .../eth/manager/MonitoredExecutors.java | 4 +- .../manager/peertask/PeerTaskExecutor.java | 6 +- .../transactions/TransactionPoolMetrics.java | 18 ++--- .../besu/metrics/noop/NoOpMetricsSystem.java | 51 +++++++------- .../besu/metrics/noop/NoOpValueCollector.java | 10 +-- .../opentelemetry/OpenTelemetryGauge.java | 50 ++------------ .../OpenTelemetryLabelledSuppliedMetric.java | 67 +++++++++++++++++++ .../OpenTelemetrySuppliedCounter.java | 43 ++++++++++++ .../opentelemetry/OpenTelemetrySystem.java | 20 +++++- .../prometheus/PrometheusMetricsSystem.java | 31 +++++++-- ... => PrometheusSuppliedValueCollector.java} | 26 +++---- .../besu/metrics/StubMetricsSystem.java | 15 ++++- .../OpenTelemetryMetricsSystemTest.java | 6 +- .../PrometheusMetricsSystemTest.java | 10 +-- plugin-api/build.gradle | 2 +- .../besu/plugin/services/MetricsSystem.java | 57 +++++++++++++++- .../services/metrics/LabelledGauge.java | 30 ++------- .../metrics/LabelledSuppliedMetric.java | 28 ++++++++ 20 files changed, 326 insertions(+), 153 deletions(-) create mode 100644 metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryLabelledSuppliedMetric.java create mode 100644 metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySuppliedCounter.java rename metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/{PrometheusGauge.java => PrometheusSuppliedValueCollector.java} (76%) create mode 100644 plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledSuppliedMetric.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 2efa312d263..28d0a874427 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,12 @@ # Changelog ## [Unreleased] -- Added isLabelsObserved to LabelledGauge in plugin-api. Default implementation returns false. ### Breaking Changes - Removed Retesteth rpc service and commands [#7833](https://github.com/hyperledger/besu/pull/7783) ### Upcoming Breaking Changes +- `MetricSystem::createLabelledGauge` is deprecated and will be removed in a future release, replace it with `MetricSystem::createLabelledSuppliedGauge` ### Additions and Improvements - Fine tune already seen txs tracker when a tx is removed from the pool [#7755](https://github.com/hyperledger/besu/pull/7755) @@ -16,6 +16,7 @@ - Add a method to get all the transaction in the pool, to the `TransactionPoolService`, to easily access the transaction pool content from plugins [#7813](https://github.com/hyperledger/besu/pull/7813) - Add a method to check if a metric category is enabled to the plugin API [#7832](https://github.com/hyperledger/besu/pull/7832) - Add account and state overrides to `eth_call` and `eth_estimateGas` [#7801](https://github.com/hyperledger/besu/pull/7801) +- Add a new metric collector for counters which get their value from suppliers [#7894](https://github.com/hyperledger/besu/pull/7894) ### Bug fixes - Fix registering new metric categories from plugins [#7825](https://github.com/hyperledger/besu/pull/7825) diff --git a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java index 9126bc8a2a7..f817dfb8a69 100644 --- a/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java +++ b/besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java @@ -1332,7 +1332,7 @@ private void validatePrivacyPluginOptions() { private void setReleaseMetrics() { besuComponent .getMetricsSystem() - .createLabelledGauge( + .createLabelledSuppliedGauge( StandardMetricCategory.PROCESS, "release", "Release information", "version") .labels(() -> 1, BesuInfo.version()); } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/MonitoredExecutors.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/MonitoredExecutors.java index bf2389c1a85..02514db1e7b 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/MonitoredExecutors.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/MonitoredExecutors.java @@ -153,13 +153,13 @@ private static T newMonitoredExecutor( "Current number of threads in the thread pool", executor::getPoolSize); - metricsSystem.createLongGauge( + metricsSystem.createCounter( BesuMetricCategory.EXECUTORS, metricName + "_completed_tasks_total", "Total number of tasks executed", executor::getCompletedTaskCount); - metricsSystem.createLongGauge( + metricsSystem.createCounter( BesuMetricCategory.EXECUTORS, metricName + "_submitted_tasks_total", "Total number of tasks executed", diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java index a2ae0455263..c8eed6e1f1f 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/manager/peertask/PeerTaskExecutor.java @@ -20,8 +20,8 @@ import org.hyperledger.besu.metrics.BesuMetricCategory; import org.hyperledger.besu.plugin.services.MetricsSystem; import org.hyperledger.besu.plugin.services.metrics.Counter; -import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; import java.util.Collection; @@ -43,7 +43,7 @@ public class PeerTaskExecutor { private final LabelledMetric timeoutCounter; private final LabelledMetric invalidResponseCounter; private final LabelledMetric internalExceptionCounter; - private final LabelledGauge inflightRequestGauge; + private final LabelledSuppliedMetric inflightRequestGauge; private final Map inflightRequestCountByClassName; public PeerTaskExecutor( @@ -77,7 +77,7 @@ public PeerTaskExecutor( "Counter of the number of internal exceptions occurred", "taskName"); inflightRequestGauge = - metricsSystem.createLabelledGauge( + metricsSystem.createLabelledSuppliedGauge( BesuMetricCategory.PEERS, "inflight_request_gauge", "Gauge of the number of inflight requests", diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPoolMetrics.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPoolMetrics.java index fac9b3174d0..6f116d3b88f 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPoolMetrics.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/TransactionPoolMetrics.java @@ -22,8 +22,8 @@ import org.hyperledger.besu.metrics.RunnableCounter; import org.hyperledger.besu.plugin.services.MetricsSystem; import org.hyperledger.besu.plugin.services.metrics.Counter; -import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import java.util.HashMap; import java.util.Map; @@ -47,10 +47,10 @@ public class TransactionPoolMetrics { private final LabelledMetric removedCounter; private final LabelledMetric rejectedCounter; private final LabelledMetric penalizedCounter; - private final LabelledGauge spaceUsed; - private final LabelledGauge transactionCount; - private final LabelledGauge transactionCountByType; - private final LabelledGauge uniqueSenderCount; + private final LabelledSuppliedMetric spaceUsed; + private final LabelledSuppliedMetric transactionCount; + private final LabelledSuppliedMetric transactionCountByType; + private final LabelledSuppliedMetric uniqueSenderCount; private final LabelledMetric expiredMessagesCounter; private final Map expiredMessagesRunnableCounters = new HashMap<>(); private final LabelledMetric alreadySeenTransactionsCounter; @@ -103,21 +103,21 @@ public TransactionPoolMetrics(final MetricsSystem metricsSystem) { "layer"); spaceUsed = - metricsSystem.createLabelledGauge( + metricsSystem.createLabelledSuppliedGauge( BesuMetricCategory.TRANSACTION_POOL, "space_used", "The amount of space used by the transactions in the layer", "layer"); transactionCount = - metricsSystem.createLabelledGauge( + metricsSystem.createLabelledSuppliedGauge( BesuMetricCategory.TRANSACTION_POOL, "number_of_transactions", "The number of transactions currently present in the layer", "layer"); transactionCountByType = - metricsSystem.createLabelledGauge( + metricsSystem.createLabelledSuppliedGauge( BesuMetricCategory.TRANSACTION_POOL, "number_of_transactions_by_type", "The number of transactions, of a specified type, currently present in the layer", @@ -125,7 +125,7 @@ public TransactionPoolMetrics(final MetricsSystem metricsSystem) { "type"); uniqueSenderCount = - metricsSystem.createLabelledGauge( + metricsSystem.createLabelledSuppliedGauge( BesuMetricCategory.TRANSACTION_POOL, "unique_senders", "The number of senders with at least one transaction currently present in the layer", diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpMetricsSystem.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpMetricsSystem.java index d3b6c4aadc5..68b5d52ffa3 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpMetricsSystem.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpMetricsSystem.java @@ -20,6 +20,7 @@ import org.hyperledger.besu.plugin.services.metrics.ExternalSummary; import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import org.hyperledger.besu.plugin.services.metrics.MetricCategory; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -41,7 +42,7 @@ public class NoOpMetricsSystem implements ObservableMetricsSystem { public static final Counter NO_OP_COUNTER = new NoOpCounter(); /** The constant NO_OP_GAUGE. */ - public static final LabelledGauge NO_OP_GAUGE = new NoOpValueCollector(); + public static final LabelledSuppliedMetric NO_OP_GAUGE = new NoOpValueCollector(); private static final OperationTimer.TimingContext NO_OP_TIMING_CONTEXT = () -> 0; @@ -65,16 +66,16 @@ public class NoOpMetricsSystem implements ObservableMetricsSystem { new LabelCountingNoOpMetric<>(1, NO_OP_OPERATION_TIMER); /** The constant NO_OP_LABELLED_1_GAUGE. */ - public static final LabelledGauge NO_OP_LABELLED_1_GAUGE = - new LabelledGaugeNoOpMetric(1, NO_OP_GAUGE); + public static final LabelledSuppliedMetric NO_OP_LABELLED_1_GAUGE = + new LabelledSuppliedNoOpMetric(1, NO_OP_GAUGE); /** The constant NO_OP_LABELLED_2_GAUGE. */ - public static final LabelledGauge NO_OP_LABELLED_2_GAUGE = - new LabelledGaugeNoOpMetric(2, NO_OP_GAUGE); + public static final LabelledSuppliedMetric NO_OP_LABELLED_2_GAUGE = + new LabelledSuppliedNoOpMetric(2, NO_OP_GAUGE); /** The constant NO_OP_LABELLED_3_GAUGE. */ - public static final LabelledGauge NO_OP_LABELLED_3_GAUGE = - new LabelledGaugeNoOpMetric(3, NO_OP_GAUGE); + public static final LabelledSuppliedMetric NO_OP_LABELLED_3_GAUGE = + new LabelledSuppliedNoOpMetric(3, NO_OP_GAUGE); /** Default constructor */ public NoOpMetricsSystem() {} @@ -159,12 +160,21 @@ public void createGuavaCacheCollector( final MetricCategory category, final String name, final Cache cache) {} @Override - public LabelledGauge createLabelledGauge( + public LabelledSuppliedMetric createLabelledSuppliedCounter( final MetricCategory category, final String name, final String help, final String... labelNames) { - return getLabelledGauge(labelNames.length); + return getLabelledSuppliedMetric(labelNames.length); + } + + @Override + public LabelledSuppliedMetric createLabelledSuppliedGauge( + final MetricCategory category, + final String name, + final String help, + final String... labelNames) { + return getLabelledSuppliedMetric(labelNames.length); } /** @@ -173,7 +183,7 @@ public LabelledGauge createLabelledGauge( * @param labelCount the label count * @return the labelled gauge */ - public static LabelledGauge getLabelledGauge(final int labelCount) { + public static LabelledSuppliedMetric getLabelledSuppliedMetric(final int labelCount) { switch (labelCount) { case 1: return NO_OP_LABELLED_1_GAUGE; @@ -182,7 +192,7 @@ public static LabelledGauge getLabelledGauge(final int labelCount) { case 3: return NO_OP_LABELLED_3_GAUGE; default: - return new LabelledGaugeNoOpMetric(labelCount, NO_OP_GAUGE); + return new LabelledSuppliedNoOpMetric(labelCount, NO_OP_GAUGE); } } @@ -237,8 +247,9 @@ public T labels(final String... labels) { } } - /** The Labelled gauge NoOp metric. */ - public static class LabelledGaugeNoOpMetric implements LabelledGauge { + /** The Labelled supplied NoOp metric. */ + @SuppressWarnings("removal") // remove when deprecated LabelledGauge is removed + public static class LabelledSuppliedNoOpMetric implements LabelledSuppliedMetric, LabelledGauge { /** The Label count. */ final int labelCount; @@ -251,13 +262,14 @@ public static class LabelledGaugeNoOpMetric implements LabelledGauge { * @param labelCount the label count * @param fakeMetric the fake metric */ - public LabelledGaugeNoOpMetric(final int labelCount, final LabelledGauge fakeMetric) { + public LabelledSuppliedNoOpMetric( + final int labelCount, final LabelledSuppliedMetric fakeMetric) { this.labelCount = labelCount; this.fakeMetric = fakeMetric; } /** The Fake metric. */ - final LabelledGauge fakeMetric; + final LabelledSuppliedMetric fakeMetric; @Override public void labels(final DoubleSupplier valueSupplier, final String... labelValues) { @@ -270,14 +282,5 @@ public void labels(final DoubleSupplier valueSupplier, final String... labelValu "The count of labels used must match the count of labels expected."); Preconditions.checkNotNull(valueSupplier, "No valueSupplier specified"); } - - @Override - public boolean isLabelsObserved(final String... labelValues) { - Preconditions.checkArgument( - labelValues.length == labelCount, - "The count of labels used must match the count of labels expected."); - final String labelValuesString = String.join(",", labelValues); - return labelValuesCache.contains(labelValuesString); - } } } diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpValueCollector.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpValueCollector.java index 6f36f10d2c7..3db34babe42 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpValueCollector.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/noop/NoOpValueCollector.java @@ -14,14 +14,14 @@ */ package org.hyperledger.besu.metrics.noop; -import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import java.util.ArrayList; import java.util.List; import java.util.function.DoubleSupplier; /** The NoOp value collector. */ -public class NoOpValueCollector implements LabelledGauge { +public class NoOpValueCollector implements LabelledSuppliedMetric { private final List labelValuesCreated = new ArrayList<>(); /** Default constructor */ @@ -36,10 +36,4 @@ public synchronized void labels(final DoubleSupplier valueSupplier, final String } labelValuesCreated.add(labelValuesString); } - - @Override - public boolean isLabelsObserved(final String... labelValues) { - final String labelValuesString = String.join(",", labelValues); - return labelValuesCreated.contains(labelValuesString); - } } diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryGauge.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryGauge.java index e1d785c68ec..4f250cbd92d 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryGauge.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryGauge.java @@ -16,23 +16,14 @@ import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; -import java.util.Arrays; import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.DoubleSupplier; -import com.google.common.base.Preconditions; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.api.metrics.Meter; -import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; /** The Open telemetry gauge. */ -public class OpenTelemetryGauge implements LabelledGauge { - private final List labelNames; - private final Map observationsMap = new ConcurrentHashMap<>(); - +@SuppressWarnings("removal") // remove when deprecated LabelledGauge is removed +public class OpenTelemetryGauge extends OpenTelemetryLabelledSuppliedMetric + implements LabelledGauge { /** * Instantiates a new Open telemetry gauge. * @@ -46,41 +37,8 @@ public OpenTelemetryGauge( final String help, final Meter meter, final List labelNames) { - this.labelNames = labelNames; + super(labelNames); meter.gaugeBuilder(metricName).setDescription(help).buildWithCallback(this::updater); } - - @Override - public void labels(final DoubleSupplier valueSupplier, final String... labelValues) { - Preconditions.checkArgument( - labelValues.length == labelNames.size(), - "label values and label names need the same number of elements"); - final Attributes labels = getLabels(labelValues); - if (observationsMap.putIfAbsent(labels, valueSupplier) != null) { - throw new IllegalStateException( - "Already registered a gauge with labels " + Arrays.toString(labelValues)); - } - } - - @Override - public boolean isLabelsObserved(final String... labelValues) { - Preconditions.checkArgument( - labelValues.length == labelNames.size(), - "label values and label names need the same number of elements"); - return observationsMap.containsKey(getLabels(labelValues)); - } - - private Attributes getLabels(final String... labelValues) { - final AttributesBuilder labelsBuilder = Attributes.builder(); - for (int i = 0; i < labelNames.size(); i++) { - labelsBuilder.put(labelNames.get(i), labelValues[i]); - } - return labelsBuilder.build(); - } - - private void updater(final ObservableDoubleMeasurement measurement) { - observationsMap.forEach( - (labels, valueSupplier) -> measurement.record(valueSupplier.getAsDouble(), labels)); - } } diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryLabelledSuppliedMetric.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryLabelledSuppliedMetric.java new file mode 100644 index 00000000000..db48ee9b312 --- /dev/null +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryLabelledSuppliedMetric.java @@ -0,0 +1,67 @@ +/* + * Copyright contributors to Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.metrics.opentelemetry; + +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.DoubleSupplier; + +import com.google.common.base.Preconditions; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.common.AttributesBuilder; +import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; + +abstract class OpenTelemetryLabelledSuppliedMetric implements LabelledSuppliedMetric { + private final List labelNames; + private final Map observationsMap = new ConcurrentHashMap<>(); + + public OpenTelemetryLabelledSuppliedMetric(final List labelNames) { + this.labelNames = labelNames; + } + + @Override + public void labels(final DoubleSupplier valueSupplier, final String... labelValues) { + Preconditions.checkArgument( + labelValues.length == labelNames.size(), + "label values and label names need the same number of elements"); + final Attributes labels = getLabels(labelValues); + if (observationsMap.putIfAbsent(labels, valueSupplier) != null) { + throw new IllegalStateException( + "Already registered a collector with label values " + Arrays.toString(labelValues)); + } + } + + private Attributes getLabels(final String... labelValues) { + final AttributesBuilder labelsBuilder = Attributes.builder(); + for (int i = 0; i < labelNames.size(); i++) { + labelsBuilder.put(labelNames.get(i), labelValues[i]); + } + return labelsBuilder.build(); + } + + /** + * Callback to record the supplied values + * + * @param measurement where to record the values + */ + protected void updater(final ObservableDoubleMeasurement measurement) { + observationsMap.forEach( + (labels, valueSupplier) -> measurement.record(valueSupplier.getAsDouble(), labels)); + } +} diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySuppliedCounter.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySuppliedCounter.java new file mode 100644 index 00000000000..7bfadcdc2d9 --- /dev/null +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySuppliedCounter.java @@ -0,0 +1,43 @@ +/* + * Copyright contributors to Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.metrics.opentelemetry; + +import java.util.List; + +import io.opentelemetry.api.metrics.Meter; + +/** The Open telemetry supplied counter. */ +public class OpenTelemetrySuppliedCounter extends OpenTelemetryLabelledSuppliedMetric { + /** + * Instantiates a new Open telemetry supplied counter. + * + * @param metricName the metric name + * @param help the help + * @param meter the meter + * @param labelNames the label names + */ + public OpenTelemetrySuppliedCounter( + final String metricName, + final String help, + final Meter meter, + final List labelNames) { + super(labelNames); + meter + .counterBuilder(metricName) + .setDescription(help) + .ofDoubles() + .buildWithCallback(this::updater); + } +} diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySystem.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySystem.java index a17c773813c..cc2174dff53 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySystem.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetrySystem.java @@ -21,8 +21,8 @@ import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; import org.hyperledger.besu.plugin.services.metrics.Counter; import org.hyperledger.besu.plugin.services.metrics.ExternalSummary; -import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import org.hyperledger.besu.plugin.services.metrics.MetricCategory; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -292,7 +292,21 @@ public void createGuavaCacheCollector( final MetricCategory category, final String name, final Cache cache) {} @Override - public LabelledGauge createLabelledGauge( + public LabelledSuppliedMetric createLabelledSuppliedCounter( + final MetricCategory category, + final String name, + final String help, + final String... labelNames) { + LOG.trace("Creating a labelled supplied counter {}", name); + if (isCategoryEnabled(category)) { + return new OpenTelemetrySuppliedCounter( + name, help, sdkMeterProvider.get(category.getName()), List.of(labelNames)); + } + return NoOpMetricsSystem.getLabelledSuppliedMetric(labelNames.length); + } + + @Override + public LabelledSuppliedMetric createLabelledSuppliedGauge( final MetricCategory category, final String name, final String help, @@ -302,7 +316,7 @@ public LabelledGauge createLabelledGauge( return new OpenTelemetryGauge( name, help, sdkMeterProvider.get(category.getName()), List.of(labelNames)); } - return NoOpMetricsSystem.getLabelledGauge(labelNames.length); + return NoOpMetricsSystem.getLabelledSuppliedMetric(labelNames.length); } @Override diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystem.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystem.java index b001eb0b3be..9263383b8f0 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystem.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystem.java @@ -19,8 +19,8 @@ import org.hyperledger.besu.metrics.StandardMetricCategory; import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; import org.hyperledger.besu.plugin.services.metrics.ExternalSummary; -import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import org.hyperledger.besu.plugin.services.metrics.MetricCategory; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -240,18 +240,37 @@ public void createGuavaCacheCollector( } @Override - public LabelledGauge createLabelledGauge( + public LabelledSuppliedMetric createLabelledSuppliedCounter( final MetricCategory category, final String name, final String help, final String... labelNames) { + return createLabelledSuppliedMetric(category, Collector.Type.COUNTER, name, help, labelNames); + } + + @Override + public LabelledSuppliedMetric createLabelledSuppliedGauge( + final MetricCategory category, + final String name, + final String help, + final String... labelNames) { + return createLabelledSuppliedMetric(category, Collector.Type.GAUGE, name, help, labelNames); + } + + private LabelledSuppliedMetric createLabelledSuppliedMetric( + final MetricCategory category, + final Collector.Type type, + final String name, + final String help, + final String... labelNames) { final String metricName = convertToPrometheusName(category, name); if (isCategoryEnabled(category)) { - final PrometheusGauge gauge = new PrometheusGauge(metricName, help, List.of(labelNames)); - registerCollector(category, gauge); - return gauge; + final PrometheusSuppliedValueCollector suppliedValueCollector = + new PrometheusSuppliedValueCollector(type, metricName, help, List.of(labelNames)); + registerCollector(category, suppliedValueCollector); + return suppliedValueCollector; } - return NoOpMetricsSystem.getLabelledGauge(labelNames.length); + return NoOpMetricsSystem.getLabelledSuppliedMetric(labelNames.length); } private void registerCollector(final MetricCategory category, final Collector collector) { diff --git a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusGauge.java b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusSuppliedValueCollector.java similarity index 76% rename from metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusGauge.java rename to metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusSuppliedValueCollector.java index b69e3f90626..47394ffed4d 100644 --- a/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusGauge.java +++ b/metrics/core/src/main/java/org/hyperledger/besu/metrics/prometheus/PrometheusSuppliedValueCollector.java @@ -1,5 +1,5 @@ /* - * Copyright ConsenSys AG. + * Copyright contributors to Besu. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at @@ -15,6 +15,7 @@ package org.hyperledger.besu.metrics.prometheus; import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import java.util.ArrayList; import java.util.List; @@ -24,22 +25,27 @@ import io.prometheus.client.Collector; -/** The Prometheus gauge. */ -public class PrometheusGauge extends Collector implements LabelledGauge { +/** The Prometheus supplied value collector. */ +@SuppressWarnings("removal") // remove when deprecated LabelledGauge is removed +public class PrometheusSuppliedValueCollector extends Collector + implements LabelledSuppliedMetric, LabelledGauge { + private final Type type; private final String metricName; private final String help; private final List labelNames; private final Map, DoubleSupplier> observationsMap = new ConcurrentHashMap<>(); /** - * Instantiates a new Prometheus gauge. + * Instantiates a new Prometheus supplied value collector. * + * @param type the type of the collector * @param metricName the metric name * @param help the help * @param labelNames the label names */ - public PrometheusGauge( - final String metricName, final String help, final List labelNames) { + public PrometheusSuppliedValueCollector( + final Type type, final String metricName, final String help, final List labelNames) { + this.type = type; this.metricName = metricName; this.help = help; this.labelNames = labelNames; @@ -55,12 +61,6 @@ public synchronized void labels(final DoubleSupplier valueSupplier, final String } } - @Override - public boolean isLabelsObserved(final String... labelValues) { - validateLabelsCardinality(labelValues); - return observationsMap.containsKey(List.of(labelValues)); - } - @Override public List collect() { final List samples = new ArrayList<>(); @@ -69,7 +69,7 @@ public List collect() { samples.add( new MetricFamilySamples.Sample( metricName, labelNames, labels, valueSupplier.getAsDouble()))); - return List.of(new MetricFamilySamples(metricName, Type.GAUGE, help, samples)); + return List.of(new MetricFamilySamples(metricName, type, help, samples)); } private void validateLabelsCardinality(final String... labelValues) { diff --git a/metrics/core/src/test-support/java/org/hyperledger/besu/metrics/StubMetricsSystem.java b/metrics/core/src/test-support/java/org/hyperledger/besu/metrics/StubMetricsSystem.java index 13f16ef0a0c..c10c62132c1 100644 --- a/metrics/core/src/test-support/java/org/hyperledger/besu/metrics/StubMetricsSystem.java +++ b/metrics/core/src/test-support/java/org/hyperledger/besu/metrics/StubMetricsSystem.java @@ -19,8 +19,8 @@ import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; import org.hyperledger.besu.plugin.services.metrics.Counter; import org.hyperledger.besu.plugin.services.metrics.ExternalSummary; -import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import org.hyperledger.besu.plugin.services.metrics.MetricCategory; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -50,12 +50,21 @@ public LabelledMetric createLabelledCounter( } @Override - public LabelledGauge createLabelledGauge( + public LabelledSuppliedMetric createLabelledSuppliedCounter( final MetricCategory category, final String name, final String help, final String... labelNames) { - return NoOpMetricsSystem.getLabelledGauge(labelNames.length); + return NoOpMetricsSystem.getLabelledSuppliedMetric(labelNames.length); + } + + @Override + public LabelledSuppliedMetric createLabelledSuppliedGauge( + final MetricCategory category, + final String name, + final String help, + final String... labelNames) { + return NoOpMetricsSystem.getLabelledSuppliedMetric(labelNames.length); } public long getCounterValue(final String name, final String... labels) { diff --git a/metrics/core/src/test/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryMetricsSystemTest.java b/metrics/core/src/test/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryMetricsSystemTest.java index f1bff33e555..8c6dd8261d5 100644 --- a/metrics/core/src/test/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryMetricsSystemTest.java +++ b/metrics/core/src/test/java/org/hyperledger/besu/metrics/opentelemetry/OpenTelemetryMetricsSystemTest.java @@ -31,8 +31,8 @@ import org.hyperledger.besu.metrics.prometheus.MetricsConfiguration; import org.hyperledger.besu.plugin.services.MetricsSystem; import org.hyperledger.besu.plugin.services.metrics.Counter; -import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; import java.util.Collections; @@ -231,8 +231,8 @@ public void shouldCreateObservationFromGauge() { @Test public void shouldCreateLabelledGauge() { - LabelledGauge labelledGauge = - metricsSystem.createLabelledGauge(RPC, "gaugeName", "help", "a", "b"); + LabelledSuppliedMetric labelledGauge = + metricsSystem.createLabelledSuppliedGauge(RPC, "gaugeName", "help", "a", "b"); labelledGauge.labels(() -> 1.0, "a1", "b1"); labelledGauge.labels(() -> 11.0, "a2", "b2"); labelledGauge.labels(() -> 21.0, "a3", "b3"); diff --git a/metrics/core/src/test/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystemTest.java b/metrics/core/src/test/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystemTest.java index 2ddf86d347b..ba875ab79b1 100644 --- a/metrics/core/src/test/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystemTest.java +++ b/metrics/core/src/test/java/org/hyperledger/besu/metrics/prometheus/PrometheusMetricsSystemTest.java @@ -33,8 +33,8 @@ import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; import org.hyperledger.besu.plugin.services.MetricsSystem; import org.hyperledger.besu.plugin.services.metrics.Counter; -import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; import java.util.Collections; @@ -129,8 +129,8 @@ public void shouldCreateSeparateObservationsForEachCounterLabelValue() { @Test public void shouldCreateSeparateObservationsForEachLabelledGaugeValue() { - final LabelledGauge gauge = - metricsSystem.createLabelledGauge(PEERS, "test", "test help", "a", "b", "c"); + final LabelledSuppliedMetric gauge = + metricsSystem.createLabelledSuppliedGauge(PEERS, "test", "test help", "a", "b", "c"); final double value1 = 1.0; final double value2 = 11.0; @@ -145,8 +145,8 @@ public void shouldCreateSeparateObservationsForEachLabelledGaugeValue() { @Test public void shouldNotUseSameLabelsTwiceOnSameGauge() { - final LabelledGauge gauge = - metricsSystem.createLabelledGauge(PEERS, "test", "test help", "a", "b", "c"); + final LabelledSuppliedMetric gauge = + metricsSystem.createLabelledSuppliedGauge(PEERS, "test", "test help", "a", "b", "c"); final double value1 = 1.0; gauge.labels(() -> value1, "a1", "b1", "c1"); diff --git a/plugin-api/build.gradle b/plugin-api/build.gradle index 2d3d5191df4..8242189db71 100644 --- a/plugin-api/build.gradle +++ b/plugin-api/build.gradle @@ -71,7 +71,7 @@ Calculated : ${currentHash} tasks.register('checkAPIChanges', FileStateChecker) { description = "Checks that the API for the Plugin-API project does not change without deliberate thought" files = sourceSets.main.allJava.files - knownHash = 'aYWbsgPoKTGDgq9d4QUBvQEaZYbKNJGMiBufzyKnusA=' + knownHash = 'ktVrmQXU7LMQi1ieb9OQ2vJNqZ0SVQ7Usauh1LMvUXY=' } check.dependsOn('checkAPIChanges') diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/MetricsSystem.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/MetricsSystem.java index 744bca4eb3e..f3d3f91d0a5 100644 --- a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/MetricsSystem.java +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/MetricsSystem.java @@ -18,6 +18,7 @@ import org.hyperledger.besu.plugin.services.metrics.ExternalSummary; import org.hyperledger.besu.plugin.services.metrics.LabelledGauge; import org.hyperledger.besu.plugin.services.metrics.LabelledMetric; +import org.hyperledger.besu.plugin.services.metrics.LabelledSuppliedMetric; import org.hyperledger.besu.plugin.services.metrics.MetricCategory; import org.hyperledger.besu.plugin.services.metrics.OperationTimer; @@ -45,6 +46,23 @@ default Counter createCounter( return createLabelledCounter(category, name, help, new String[0]).labels(); } + /** + * Creates a Counter that gets its value from the specified supplier. To be used when the value of + * the counter is calculated outside the metric system. + * + * @param category The {@link MetricCategory} this counter is assigned to. + * @param name A name for this metric. + * @param help A human readable description of the metric. + * @param valueSupplier The supplier of the value. + */ + default void createCounter( + final MetricCategory category, + final String name, + final String help, + final DoubleSupplier valueSupplier) { + createLabelledSuppliedCounter(category, name, help).labels(valueSupplier); + } + /** * Creates a Counter with assigned labels. * @@ -58,7 +76,42 @@ LabelledMetric createLabelledCounter( MetricCategory category, String name, String help, String... labelNames); /** - * Creates a Gauge with assigned labels. + * Creates a Counter with assigned labels, that gets its values from suppliers. To be used when + * the values of the counter are calculated outside the metric system. + * + * @param category The {@link MetricCategory} this counter is assigned to. + * @param name A name for this metric. + * @param help A human readable description of the metric. + * @param labelNames An array of labels to assign to the Counter. + * @return The created LabelledSupplierMetric instance. + */ + LabelledSuppliedMetric createLabelledSuppliedCounter( + MetricCategory category, String name, String help, String... labelNames); + + /** + * Creates a Gauge with assigned labels, that gets its values from suppliers. To be used when the + * values of the gauge are calculated outside the metric system. + * + * @param category The {@link MetricCategory} this gauge is assigned to. + * @param name A name for this metric. + * @param help A human readable description of the metric. + * @param labelNames An array of labels to assign to the Gauge. + * @return The created LabelledGauge instance. + * @deprecated Use {@link #createLabelledSuppliedGauge(MetricCategory, String, String, String...)} + */ + @Deprecated(forRemoval = true) + @SuppressWarnings("removal") // remove when deprecated LabelledGauge is removed + default LabelledGauge createLabelledGauge( + final MetricCategory category, + final String name, + final String help, + final String... labelNames) { + return (LabelledGauge) createLabelledSuppliedGauge(category, name, help, labelNames); + } + + /** + * Creates a Gauge with assigned labels, that gets its values from suppliers. To be used when the + * values of the gauge are calculated outside the metric system. * * @param category The {@link MetricCategory} this gauge is assigned to. * @param name A name for this metric. @@ -66,7 +119,7 @@ LabelledMetric createLabelledCounter( * @param labelNames An array of labels to assign to the Gauge. * @return The created LabelledGauge instance. */ - LabelledGauge createLabelledGauge( + LabelledSuppliedMetric createLabelledSuppliedGauge( MetricCategory category, String name, String help, String... labelNames); /** diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledGauge.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledGauge.java index 5357c6505ae..f94e5231840 100644 --- a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledGauge.java +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledGauge.java @@ -14,26 +14,10 @@ */ package org.hyperledger.besu.plugin.services.metrics; -import java.util.function.DoubleSupplier; - -/** The interface Labelled gauge. */ -public interface LabelledGauge { - /** - * Labels. - * - * @param valueSupplier the value supplier - * @param labelValues the label values - */ - void labels(final DoubleSupplier valueSupplier, final String... labelValues); - - /** - * Checks whether the supplied labelValues are already observed by this LabelledGauge - * - * @param labelValues The labelValues to check - * @return true if the supplied labelValues are already observed by this LabelledGauge, false - * otherwise - */ - default boolean isLabelsObserved(final String... labelValues) { - return false; - } -} +/** + * The interface Labelled gauge. + * + * @deprecated Use {@link LabelledSuppliedMetric} + */ +@Deprecated(forRemoval = true) +public interface LabelledGauge extends LabelledSuppliedMetric {} diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledSuppliedMetric.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledSuppliedMetric.java new file mode 100644 index 00000000000..755bd6787e3 --- /dev/null +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/metrics/LabelledSuppliedMetric.java @@ -0,0 +1,28 @@ +/* + * Copyright contributors to Besu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.plugin.services.metrics; + +import java.util.function.DoubleSupplier; + +/** The interface Labelled gauge. */ +public interface LabelledSuppliedMetric { + /** + * Labels. + * + * @param valueSupplier the value supplier + * @param labelValues the label values + */ + void labels(final DoubleSupplier valueSupplier, final String... labelValues); +}