From 3646d78c2ff7c5451318f63dd5f6d8fd5a6962b2 Mon Sep 17 00:00:00 2001 From: Matt Whitehead Date: Tue, 30 Jan 2024 11:31:22 +0000 Subject: [PATCH] Implement "pending" for qbft_getValidatorsByBlockNumber (#6436) * Implement 'pending' for qbft_getValidatorsByBlockNumber Signed-off-by: Matthew Whitehead * Update change log Signed-off-by: Matthew Whitehead * Update consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetValidatorsByBlockNumber.java Co-authored-by: Jason Frame Signed-off-by: Matt Whitehead * Refactoring Signed-off-by: Matthew Whitehead --------- Signed-off-by: Matthew Whitehead Signed-off-by: Matt Whitehead Co-authored-by: Jason Frame --- CHANGELOG.md | 1 + .../QbftGetValidatorsByBlockNumber.java | 9 ++++++ .../QbftGetValidatorsByBlockNumberTest.java | 30 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39aec2f15f..e37654c305 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ - Upgrade Mockito [#6397](https://github.com/hyperledger/besu/pull/6397) - Upgrade `tech.pegasys.discovery:discovery` [#6414](https://github.com/hyperledger/besu/pull/6414) - Options to tune the max allowed time that can be spent selecting transactions during block creation are now stable [#6423](https://github.com/hyperledger/besu/pull/6423) +- Support for "pending" in `qbft_getValidatorsByBlockNumber` [#6436](https://github.com/hyperledger/besu/pull/6436) ### Bug fixes - INTERNAL_ERROR from `eth_estimateGas` JSON/RPC calls [#6344](https://github.com/hyperledger/besu/issues/6344) diff --git a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetValidatorsByBlockNumber.java b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetValidatorsByBlockNumber.java index 6210d97bbb..804ecc6b5a 100644 --- a/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetValidatorsByBlockNumber.java +++ b/consensus/qbft/src/main/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetValidatorsByBlockNumber.java @@ -53,6 +53,15 @@ public class QbftGetValidatorsByBlockNumber extends AbstractBlockParameterMethod return request.getRequiredParameter(0, BlockParameter.class); } + @Override + protected Object pendingResult(final JsonRpcRequestContext request) { + final BlockHeader blockHeader = getBlockchainQueries().headBlockHeader(); + LOG.trace("Received RPC rpcName={} block={}", getName(), blockHeader.getNumber()); + return validatorProvider.getValidatorsAfterBlock(blockHeader).stream() + .map(Address::toString) + .collect(Collectors.toList()); + } + @Override protected Object resultByBlockNumber( final JsonRpcRequestContext request, final long blockNumber) { diff --git a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetValidatorsByBlockNumberTest.java b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetValidatorsByBlockNumberTest.java index b2e1c2b3d4..a0235723bf 100644 --- a/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetValidatorsByBlockNumberTest.java +++ b/consensus/qbft/src/test/java/org/hyperledger/besu/consensus/qbft/jsonrpc/methods/QbftGetValidatorsByBlockNumberTest.java @@ -23,6 +23,7 @@ import org.hyperledger.besu.datatypes.Address; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameter; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; import org.hyperledger.besu.ethereum.core.BlockHeader; @@ -73,4 +74,33 @@ public class QbftGetValidatorsByBlockNumberTest { Object result = method.resultByBlockNumber(request, 12); assertThat(result).isEqualTo(expectedOutput); } + + @Test + public void shouldReturnListOfValidatorsFromLatestBlock() { + request = + new JsonRpcRequestContext( + new JsonRpcRequest("2.0", "qbft_getValidatorsByBlockNumber", new String[] {"latest"})); + when(blockchainQueries.headBlockNumber()).thenReturn(12L); + when(blockchainQueries.getBlockHeaderByNumber(12)).thenReturn(Optional.of(blockHeader)); + final List
addresses = Collections.singletonList(Address.ID); + final List expectedOutput = Collections.singletonList(Address.ID.toString()); + when(validatorProvider.getValidatorsForBlock(any())).thenReturn(addresses); + Object result = method.response(request); + assertThat(result).isInstanceOf(JsonRpcSuccessResponse.class); + assertThat(((JsonRpcSuccessResponse) result).getResult()).isEqualTo(expectedOutput); + } + + @Test + public void shouldReturnListOfValidatorsFromPendingBlock() { + request = + new JsonRpcRequestContext( + new JsonRpcRequest("2.0", "qbft_getValidatorsByBlockNumber", new String[] {"pending"})); + when(blockchainQueries.headBlockHeader()).thenReturn(blockHeader); + final List
addresses = Collections.singletonList(Address.ID); + final List expectedOutput = Collections.singletonList(Address.ID.toString()); + when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(addresses); + Object result = method.response(request); + assertThat(result).isInstanceOf(JsonRpcSuccessResponse.class); + assertThat(((JsonRpcSuccessResponse) result).getResult()).isEqualTo(expectedOutput); + } }