-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement txpool_status RPC method
#10002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...rc/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/TxPoolStatus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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.ethereum.api.jsonrpc.internal.methods; | ||
|
|
||
| import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionPoolStatusResult; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.PendingTransactions; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; | ||
|
|
||
| public class TxPoolStatus implements JsonRpcMethod { | ||
|
|
||
| private final TransactionPool transactionPool; | ||
|
|
||
| public TxPoolStatus(final TransactionPool transactionPool) { | ||
| this.transactionPool = transactionPool; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return RpcMethod.TX_POOL_STATUS.getMethodName(); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { | ||
| return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), status()); | ||
| } | ||
|
|
||
| private TransactionPoolStatusResult status() { | ||
| final PendingTransactions.Status status = transactionPool.getStatus(); | ||
| return new TransactionPoolStatusResult(status.pendingCount(), status.queuedCount()); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
...g/hyperledger/besu/ethereum/api/jsonrpc/internal/results/TransactionPoolStatusResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * 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.ethereum.api.jsonrpc.internal.results; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonGetter; | ||
|
|
||
| public class TransactionPoolStatusResult { | ||
|
|
||
| private final long pending; | ||
| private final long queued; | ||
|
|
||
| public TransactionPoolStatusResult(final long pending, final long queued) { | ||
| this.pending = pending; | ||
| this.queued = queued; | ||
| } | ||
|
|
||
| @JsonGetter | ||
| public String getPending() { | ||
| return Quantity.create(pending); | ||
| } | ||
|
|
||
| @JsonGetter | ||
| public String getQueued() { | ||
| return Quantity.create(queued); | ||
| } | ||
|
fab-10 marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...est/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/TxPoolStatusTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * 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.ethereum.api.jsonrpc.internal.methods; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| 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.response.JsonRpcSuccessResponse; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionPoolStatusResult; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.PendingTransactions; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| public class TxPoolStatusTest { | ||
|
|
||
| @Mock private TransactionPool transactionPool; | ||
| private TxPoolStatus method; | ||
| private static final String JSON_RPC_VERSION = "2.0"; | ||
| private static final String TXPOOL_STATUS_METHOD = "txpool_status"; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| method = new TxPoolStatus(transactionPool); | ||
| } | ||
|
|
||
| @Test | ||
| public void returnsCorrectMethodName() { | ||
| assertThat(method.getName()).isEqualTo(TXPOOL_STATUS_METHOD); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnZeroCountsWhenPoolIsEmpty() { | ||
| when(transactionPool.getStatus()).thenReturn(new PendingTransactions.Status(0, 0)); | ||
|
|
||
| final JsonRpcRequestContext request = buildRequest(); | ||
| final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); | ||
| final TransactionPoolStatusResult result = (TransactionPoolStatusResult) response.getResult(); | ||
|
|
||
| assertThat(result.getPending()).isEqualTo("0x0"); | ||
| assertThat(result.getQueued()).isEqualTo("0x0"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnCorrectCountsWithPendingAndQueuedTransactions() { | ||
| when(transactionPool.getStatus()).thenReturn(new PendingTransactions.Status(10, 7)); | ||
|
|
||
| final JsonRpcRequestContext request = buildRequest(); | ||
| final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); | ||
| final TransactionPoolStatusResult result = (TransactionPoolStatusResult) response.getResult(); | ||
|
|
||
| assertThat(result.getPending()).isEqualTo("0xa"); | ||
| assertThat(result.getQueued()).isEqualTo("0x7"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnCorrectCountsWithOnlyPendingTransactions() { | ||
| when(transactionPool.getStatus()).thenReturn(new PendingTransactions.Status(5, 0)); | ||
|
|
||
| final JsonRpcRequestContext request = buildRequest(); | ||
| final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); | ||
| final TransactionPoolStatusResult result = (TransactionPoolStatusResult) response.getResult(); | ||
|
|
||
| assertThat(result.getPending()).isEqualTo("0x5"); | ||
| assertThat(result.getQueued()).isEqualTo("0x0"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnCorrectCountsWithOnlyQueuedTransactions() { | ||
| when(transactionPool.getStatus()).thenReturn(new PendingTransactions.Status(0, 3)); | ||
|
|
||
| final JsonRpcRequestContext request = buildRequest(); | ||
| final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); | ||
| final TransactionPoolStatusResult result = (TransactionPoolStatusResult) response.getResult(); | ||
|
|
||
| assertThat(result.getPending()).isEqualTo("0x0"); | ||
| assertThat(result.getQueued()).isEqualTo("0x3"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnHexEncodedLargeValues() { | ||
| when(transactionPool.getStatus()).thenReturn(new PendingTransactions.Status(256, 4096)); | ||
|
|
||
| final JsonRpcRequestContext request = buildRequest(); | ||
| final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); | ||
| final TransactionPoolStatusResult result = (TransactionPoolStatusResult) response.getResult(); | ||
|
|
||
| assertThat(result.getPending()).isEqualTo("0x100"); | ||
| assertThat(result.getQueued()).isEqualTo("0x1000"); | ||
| } | ||
|
|
||
| private JsonRpcRequestContext buildRequest() { | ||
| return new JsonRpcRequestContext( | ||
| new JsonRpcRequest(JSON_RPC_VERSION, TXPOOL_STATUS_METHOD, new Object[] {})); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.