Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
### Additions and Improvements
- Dispatch snap server request processing (GET_ACCOUNT_RANGE, GET_STORAGE_RANGE, GET_BYTECODES, GET_TRIE_NODES, GET_BLOCK_ACCESS_LISTS) off the Netty event loop to prevent heavy trie/DB work from blocking ETH protocol message handling [#10083](https://github.com/besu-eth/besu/pull/10083)
- Add DiscV5 discovery metrics (`discv5_live_nodes_current`, `discv5_total_nodes_current`) to track node counts in the routing table [#9692](https://github.com/besu-eth/besu/issues/9692)
- Add `txpool_contentFrom` JSON-RPC method [#10111](https://github.com/besu-eth/besu/pull/10111)

## 26.3.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public enum RpcMethod {
TX_POOL_BESU_TRANSACTIONS("txpool_besuTransactions"),
TX_POOL_BESU_PENDING_TRANSACTIONS("txpool_besuPendingTransactions"),
TX_POOL_STATUS("txpool_status"),
TX_POOL_CONTENT_FROM("txpool_contentFrom"),
WEB3_CLIENT_VERSION("web3_clientVersion"),
WEB3_SHA3("web3_sha3"),
PLUGINS_RELOAD_CONFIG("plugins_reloadPluginConfig"),
Expand Down
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;
}
Comment thread
fab-10 marked this conversation as resolved.

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));
Comment thread
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);
}
}
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() {
Comment thread
fab-10 marked this conversation as resolved.
Comment thread
fab-10 marked this conversation as resolved.
Comment thread
fab-10 marked this conversation as resolved.
Comment thread
fab-10 marked this conversation as resolved.
return queued;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TxPoolBesuPendingTransactions;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TxPoolBesuStatistics;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TxPoolBesuTransactions;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TxPoolContentFrom;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.TxPoolStatus;
import org.hyperledger.besu.ethereum.eth.transactions.TransactionPool;

Expand All @@ -43,6 +44,7 @@ protected Map<String, JsonRpcMethod> create() {
new TxPoolBesuTransactions(transactionPool),
new TxPoolBesuPendingTransactions(transactionPool),
new TxPoolBesuStatistics(transactionPool),
new TxPoolStatus(transactionPool));
new TxPoolStatus(transactionPool),
new TxPoolContentFrom(transactionPool));
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public Collection<PendingTransaction> getPendingTransactions() {
return List.of();
}

@Override
public SenderPendingTransactionsData getPendingTransactionsFor(final Address sender) {
return SenderPendingTransactionsData.empty(sender);
}

@Override
public long subscribePendingTransactions(final PendingTransactionAddedListener listener) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ TransactionAddedResult addTransaction(

Collection<PendingTransaction> getPendingTransactions();

/**
* Returns all pending transactions for the given sender, sorted by nonce in ascending order.
*
* @param sender the sender address
* @return transactions for the sender sorted by nonce ascending, or an empty list if none exist
*/
SenderPendingTransactionsData getPendingTransactionsFor(Address sender);

long subscribePendingTransactions(PendingTransactionAddedListener listener);

void unsubscribePendingTransactions(long id);
Expand Down
Loading
Loading