Skip to content
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

Correctly initialize the txpool as disabled on creation #6890

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 @@ -45,6 +45,7 @@
- Fix Shanghai/QBFT block import bug when syncing new nodes [#6765](https://github.com/hyperledger/besu/pull/6765)
- Fix to avoid broadcasting full blob txs, instead of only the tx announcement, to a subset of nodes [#6835](https://github.com/hyperledger/besu/pull/6835)
- Snap client fixes discovered during snap server testing [#6847](https://github.com/hyperledger/besu/pull/6847)
- Correctly initialize the txpool as disabled on creation [#6890](https://github.com/hyperledger/besu/pull/6890)

### Download Links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class TransactionPool implements BlockAddedObserver {
private static final Logger LOG = LoggerFactory.getLogger(TransactionPool.class);
private static final Logger LOG_FOR_REPLAY = LoggerFactory.getLogger("LOG_FOR_REPLAY");
private final Supplier<PendingTransactions> pendingTransactionsSupplier;
private volatile PendingTransactions pendingTransactions;
private volatile PendingTransactions pendingTransactions = new DisabledPendingTransactions();
private final ProtocolSchedule protocolSchedule;
private final ProtocolContext protocolContext;
private final EthContext ethContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration.Implementation.LAYERED;
import static org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration.Implementation.LEGACY;
import static org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule.DEFAULT_CHAIN_ID;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
Expand All @@ -27,6 +28,7 @@
import static org.mockito.Mockito.when;

import org.hyperledger.besu.config.StubGenesisConfigOptions;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
Expand Down Expand Up @@ -262,33 +264,38 @@ public void incomingTransactionMessageHandlersRegisteredIfNoInitialSync() {
"transaction messages handler should be enabled"));
}

@Test
public void txPoolStartsDisabledWhenInitialSyncPhaseIsRequired() {
// when using any of the initial syncs (SNAP, FAST, ...), txpool starts disabled
// and is enabled only after it is in sync
setupInitialSyncPhase(true);
assertThat(pool.isEnabled()).isFalse();
}

@Test
public void callingGetNextNonceForSenderOnDisabledTxPoolWorks() {
setupInitialSyncPhase(true);

assertThat(pool.getNextNonceForSender(Address.fromHexString("0x123abc"))).isEmpty();
}

@Test
public void txPoolStartsEnabledWhenFullSyncConfigured() {
// when using FULL sync, txpool starts enabled, because it could be
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad that you added this comment here, I was wondering why we enable txpool on full sync thinking of a node that is still syncing up to head when I first read the test name.

// that it is the first node on a new network and so, it is in sync with genesis
// and must accept txs. Otherwise, asap it find a sync target that is ahead, the txpool
// will be disabled, until in sync.
setupInitialSyncPhase(false);
assertThat(pool.isEnabled()).isTrue();
}

private void setupInitialSyncPhase(final boolean hasInitialSyncPhase) {
syncState = new SyncState(blockchain, ethPeers, hasInitialSyncPhase, Optional.empty());
setupInitialSyncPhase(syncState);
}

private void setupInitialSyncPhase(final SyncState syncState) {
pool =
TransactionPoolFactory.createTransactionPool(
schedule,
context,
ethContext,
TestClock.fixed(),
new TransactionPoolMetrics(new NoOpMetricsSystem()),
syncState,
ImmutableTransactionPoolConfiguration.builder()
.txPoolMaxSize(1)
.pendingTxRetentionPeriod(1)
.unstable(
ImmutableTransactionPoolConfiguration.Unstable.builder()
.txMessageKeepAliveSeconds(1)
.build())
.build(),
peerTransactionTracker,
transactionsMessageSender,
newPooledTransactionHashesMessageSender,
new BlobCache(),
MiningParameters.newDefault());
pool = createTransactionPool(LAYERED, syncState);

ethProtocolManager =
new EthProtocolManager(
Expand All @@ -312,8 +319,7 @@ private void setupInitialSyncPhase(final SyncState syncState) {
createLegacyTransactionPool_shouldUseBaseFeePendingTransactionsSorter_whenLondonEnabled() {
setupScheduleWith(new StubGenesisConfigOptions().londonBlock(0));

final TransactionPool pool =
createTransactionPool(TransactionPoolConfiguration.Implementation.LEGACY);
final TransactionPool pool = createAndEnableTransactionPool(LEGACY, syncState);

assertThat(pool.pendingTransactionsImplementation())
.isEqualTo(BaseFeePendingTransactionsSorter.class);
Expand All @@ -324,8 +330,7 @@ private void setupInitialSyncPhase(final SyncState syncState) {
createLegacyTransactionPool_shouldUseGasPricePendingTransactionsSorter_whenLondonNotEnabled() {
setupScheduleWith(new StubGenesisConfigOptions().berlinBlock(0));

final TransactionPool pool =
createTransactionPool(TransactionPoolConfiguration.Implementation.LEGACY);
final TransactionPool pool = createAndEnableTransactionPool(LEGACY, syncState);

assertThat(pool.pendingTransactionsImplementation())
.isEqualTo(GasPricePendingTransactionsSorter.class);
Expand All @@ -336,7 +341,7 @@ private void setupInitialSyncPhase(final SyncState syncState) {
createLayeredTransactionPool_shouldUseBaseFeePendingTransactionsSorter_whenLondonEnabled() {
setupScheduleWith(new StubGenesisConfigOptions().londonBlock(0));

final TransactionPool pool = createTransactionPool(LAYERED);
final TransactionPool pool = createAndEnableTransactionPool(LAYERED, syncState);

assertThat(pool.pendingTransactionsImplementation())
.isEqualTo(LayeredPendingTransactions.class);
Expand All @@ -349,7 +354,7 @@ private void setupInitialSyncPhase(final SyncState syncState) {
createLayeredTransactionPool_shouldUseGasPricePendingTransactionsSorter_whenLondonNotEnabled() {
setupScheduleWith(new StubGenesisConfigOptions().berlinBlock(0));

final TransactionPool pool = createTransactionPool(LAYERED);
final TransactionPool pool = createAndEnableTransactionPool(LAYERED, syncState);

assertThat(pool.pendingTransactionsImplementation())
.isEqualTo(LayeredPendingTransactions.class);
Expand Down Expand Up @@ -378,27 +383,33 @@ private void setupScheduleWith(final StubGenesisConfigOptions config) {
}

private TransactionPool createTransactionPool(
final TransactionPoolConfiguration.Implementation implementation) {
final TransactionPool txPool =
TransactionPoolFactory.createTransactionPool(
schedule,
protocolContext,
ethContext,
TestClock.fixed(),
new NoOpMetricsSystem(),
syncState,
ImmutableTransactionPoolConfiguration.builder()
.txPoolImplementation(implementation)
.txPoolMaxSize(1)
.pendingTxRetentionPeriod(1)
.unstable(
ImmutableTransactionPoolConfiguration.Unstable.builder()
.txMessageKeepAliveSeconds(1)
.build())
.build(),
new BlobCache(),
MiningParameters.newDefault());
final TransactionPoolConfiguration.Implementation implementation, final SyncState syncState) {
return TransactionPoolFactory.createTransactionPool(
schedule,
context,
ethContext,
TestClock.fixed(),
new TransactionPoolMetrics(new NoOpMetricsSystem()),
syncState,
ImmutableTransactionPoolConfiguration.builder()
.txPoolImplementation(implementation)
.txPoolMaxSize(1)
.pendingTxRetentionPeriod(1)
.unstable(
ImmutableTransactionPoolConfiguration.Unstable.builder()
.txMessageKeepAliveSeconds(1)
.build())
.build(),
peerTransactionTracker,
transactionsMessageSender,
newPooledTransactionHashesMessageSender,
new BlobCache(),
MiningParameters.newDefault());
}

private TransactionPool createAndEnableTransactionPool(
final TransactionPoolConfiguration.Implementation implementation, final SyncState syncState) {
final TransactionPool txPool = createTransactionPool(implementation, syncState);
txPool.setEnabled();
return txPool;
}
Expand Down
Loading