-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add txpool_contentFrom JSON-RPC method #10111
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
96 changes: 96 additions & 0 deletions
96
...in/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/TxPoolContentFrom.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,96 @@ | ||
| /* | ||
| * 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.datatypes.Address; | ||
| 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.exception.InvalidJsonRpcParameters; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.JsonRpcParameter; | ||
| 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.response.RpcErrorType; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionPendingResult; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionPoolContentFromResult; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.SenderPendingTransactionsData; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.SequencedMap; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class TxPoolContentFrom implements JsonRpcMethod { | ||
|
|
||
| private final TransactionPool transactionPool; | ||
|
|
||
| public TxPoolContentFrom(final TransactionPool transactionPool) { | ||
| this.transactionPool = transactionPool; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return RpcMethod.TX_POOL_CONTENT_FROM.getMethodName(); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { | ||
| try { | ||
| final Address sender = requestContext.getRequiredParameter(0, Address.class); | ||
|
|
||
| return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), contentFrom(sender)); | ||
| } catch (JsonRpcParameter.JsonRpcParameterException e) { | ||
| throw new InvalidJsonRpcParameters( | ||
| "Invalid address parameter (index 0)", RpcErrorType.INVALID_ADDRESS_PARAMS, e); | ||
| } | ||
| } | ||
|
|
||
| private TransactionPoolContentFromResult contentFrom(final Address sender) { | ||
| final SenderPendingTransactionsData pendingTransactionsData = | ||
| transactionPool.getPendingTransactionsFor(sender); | ||
| final List<PendingTransaction> pendingTransactions = | ||
| pendingTransactionsData.pendingTransactions(); | ||
| long expectedNonce = pendingTransactionsData.nonce(); | ||
| int idx = 0; | ||
| while (idx < pendingTransactions.size() | ||
| && expectedNonce == pendingTransactions.get(idx).getNonce()) { | ||
| ++expectedNonce; | ||
| ++idx; | ||
| } | ||
|
|
||
| final SequencedMap<String, TransactionPendingResult> pendingByNonce = | ||
| pendingTransactions.subList(0, idx).stream() | ||
| .map(PendingTransaction::getTransaction) | ||
| .collect( | ||
| Collectors.toMap( | ||
| tx -> Long.toString(tx.getNonce()), | ||
| TransactionPendingResult::new, | ||
| (a, b) -> a, | ||
| LinkedHashMap::new)); | ||
|
fab-10 marked this conversation as resolved.
|
||
|
|
||
| final SequencedMap<String, TransactionPendingResult> queuedByNonce = | ||
| pendingTransactions.subList(idx, pendingTransactions.size()).stream() | ||
| .map(PendingTransaction::getTransaction) | ||
| .collect( | ||
| Collectors.toMap( | ||
| tx -> Long.toString(tx.getNonce()), | ||
| TransactionPendingResult::new, | ||
| (a, b) -> a, | ||
| LinkedHashMap::new)); | ||
|
|
||
| return new TransactionPoolContentFromResult(pendingByNonce, queuedByNonce); | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
...erledger/besu/ethereum/api/jsonrpc/internal/results/TransactionPoolContentFromResult.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,42 @@ | ||
| /* | ||
| * 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 java.util.SequencedMap; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonGetter; | ||
|
|
||
| public class TransactionPoolContentFromResult { | ||
|
|
||
| private final SequencedMap<String, TransactionPendingResult> pending; | ||
| private final SequencedMap<String, TransactionPendingResult> queued; | ||
|
|
||
| public TransactionPoolContentFromResult( | ||
| final SequencedMap<String, TransactionPendingResult> pending, | ||
| final SequencedMap<String, TransactionPendingResult> queued) { | ||
| this.pending = pending; | ||
| this.queued = queued; | ||
| } | ||
|
|
||
| @JsonGetter(value = "pending") | ||
| public SequencedMap<String, TransactionPendingResult> getPending() { | ||
| return pending; | ||
| } | ||
|
|
||
| @JsonGetter(value = "queued") | ||
| public SequencedMap<String, TransactionPendingResult> getQueued() { | ||
|
fab-10 marked this conversation as resolved.
fab-10 marked this conversation as resolved.
fab-10 marked this conversation as resolved.
fab-10 marked this conversation as resolved.
|
||
| return queued; | ||
| } | ||
| } | ||
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
171 changes: 171 additions & 0 deletions
171
...ava/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/TxPoolContentFromTest.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,171 @@ | ||
| /* | ||
| * 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.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.Mockito.lenient; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import org.hyperledger.besu.crypto.KeyPair; | ||
| import org.hyperledger.besu.crypto.SignatureAlgorithmFactory; | ||
| 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.exception.InvalidJsonRpcParameters; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; | ||
| import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionPoolContentFromResult; | ||
| import org.hyperledger.besu.ethereum.core.Transaction; | ||
| import org.hyperledger.besu.ethereum.core.TransactionTestFixture; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.PendingTransaction; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.SenderPendingTransactionsData; | ||
| import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| 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 TxPoolContentFromTest { | ||
|
|
||
| @Mock private TransactionPool transactionPool; | ||
|
|
||
| private TxPoolContentFrom method; | ||
|
|
||
| private static final String JSON_RPC_VERSION = "2.0"; | ||
| private static final String METHOD_NAME = "txpool_contentFrom"; | ||
| private static final Address SENDER = | ||
| Address.fromHexString("0x1234567890123456789012345678901234567890"); | ||
| private static final KeyPair KEY_PAIR = SignatureAlgorithmFactory.getInstance().generateKeyPair(); | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| method = new TxPoolContentFrom(transactionPool); | ||
| } | ||
|
|
||
| @Test | ||
| public void returnsCorrectMethodName() { | ||
| assertThat(method.getName()).isEqualTo(METHOD_NAME); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnEmptyResultForSenderWithNoTransactions() { | ||
| when(transactionPool.getPendingTransactionsFor(SENDER)) | ||
| .thenReturn(SenderPendingTransactionsData.empty(SENDER)); | ||
|
|
||
| final TransactionPoolContentFromResult result = invokeMethod(); | ||
|
|
||
| assertThat(result.getPending()).isEmpty(); | ||
| assertThat(result.getQueued()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnAllTransactionsAsPendingWhenAllAreConsecutive() { | ||
| // Nonce = 0, txs at nonces 0, 1, 2 → all pending, none queued | ||
| final PendingTransaction tx0 = pendingTx(0); | ||
| final PendingTransaction tx1 = pendingTx(1); | ||
| final PendingTransaction tx2 = pendingTx(2); | ||
|
|
||
| when(transactionPool.getPendingTransactionsFor(SENDER)) | ||
| .thenReturn(new SenderPendingTransactionsData(SENDER, 0L, List.of(tx0, tx1, tx2))); | ||
|
|
||
| final TransactionPoolContentFromResult result = invokeMethod(); | ||
|
|
||
| assertThat(result.getPending()).containsOnlyKeys("0", "1", "2"); | ||
| assertThat(result.getQueued()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturnAllTransactionsAsQueuedWhenGapExistsAtStart() { | ||
| // Nonce = 0, but first tx has nonce 2 → all queued, none pending | ||
| final PendingTransaction tx2 = pendingTx(2); | ||
| final PendingTransaction tx3 = pendingTx(3); | ||
|
|
||
| when(transactionPool.getPendingTransactionsFor(SENDER)) | ||
| .thenReturn(new SenderPendingTransactionsData(SENDER, 0L, List.of(tx2, tx3))); | ||
|
|
||
| final TransactionPoolContentFromResult result = invokeMethod(); | ||
|
|
||
| assertThat(result.getPending()).isEmpty(); | ||
| assertThat(result.getQueued()).containsOnlyKeys("2", "3"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldSplitTransactionsIntoPendingAndQueued() { | ||
| // Nonce = 0, txs at nonces 0, 1, 3, 4 → pending: [0,1], queued: [3,4] | ||
| final PendingTransaction tx0 = pendingTx(0); | ||
| final PendingTransaction tx1 = pendingTx(1); | ||
| final PendingTransaction tx3 = pendingTx(3); | ||
| final PendingTransaction tx4 = pendingTx(4); | ||
|
|
||
| when(transactionPool.getPendingTransactionsFor(SENDER)) | ||
| .thenReturn(new SenderPendingTransactionsData(SENDER, 0L, List.of(tx0, tx1, tx3, tx4))); | ||
|
|
||
| final TransactionPoolContentFromResult result = invokeMethod(); | ||
|
|
||
| assertThat(result.getPending()).containsOnlyKeys("0", "1"); | ||
| assertThat(result.getQueued()).containsOnlyKeys("3", "4"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldHandleMidNonceAccountState() { | ||
| // Account has mined nonces 0-4; pool has nonces 5, 6, 8 → pending: [5,6], queued: [8] | ||
| final PendingTransaction tx5 = pendingTx(5); | ||
| final PendingTransaction tx6 = pendingTx(6); | ||
| final PendingTransaction tx8 = pendingTx(8); | ||
|
|
||
| when(transactionPool.getPendingTransactionsFor(SENDER)) | ||
| .thenReturn(new SenderPendingTransactionsData(SENDER, 5L, List.of(tx5, tx6, tx8))); | ||
|
|
||
| final TransactionPoolContentFromResult result = invokeMethod(); | ||
|
|
||
| assertThat(result.getPending()).containsOnlyKeys("5", "6"); | ||
| assertThat(result.getQueued()).containsOnlyKeys("8"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldThrowInvalidJsonRpcParametersWhenAddressParamIsMissing() { | ||
| final JsonRpcRequestContext request = | ||
| new JsonRpcRequestContext( | ||
| new JsonRpcRequest(JSON_RPC_VERSION, METHOD_NAME, new Object[] {})); | ||
|
|
||
| assertThatThrownBy(() -> method.response(request)).isInstanceOf(InvalidJsonRpcParameters.class); | ||
| } | ||
|
|
||
| private TransactionPoolContentFromResult invokeMethod() { | ||
| final JsonRpcSuccessResponse response = | ||
| (JsonRpcSuccessResponse) method.response(buildRequest(SENDER)); | ||
| return (TransactionPoolContentFromResult) response.getResult(); | ||
| } | ||
|
|
||
| private JsonRpcRequestContext buildRequest(final Address sender) { | ||
| return new JsonRpcRequestContext( | ||
| new JsonRpcRequest(JSON_RPC_VERSION, METHOD_NAME, new Object[] {sender.toString()})); | ||
| } | ||
|
|
||
| private PendingTransaction pendingTx(final long nonce) { | ||
| final Transaction tx = | ||
| new TransactionTestFixture().sender(SENDER).nonce(nonce).createTransaction(KEY_PAIR); | ||
| final PendingTransaction pendingTransaction = mock(PendingTransaction.class); | ||
| lenient().when(pendingTransaction.getNonce()).thenReturn(nonce); | ||
| when(pendingTransaction.getTransaction()).thenReturn(tx); | ||
| return pendingTransaction; | ||
| } | ||
| } |
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.