Skip to content

Commit

Permalink
fix for txpool inSync listener to honor initial sync state (hyperledg…
Browse files Browse the repository at this point in the history
…er#6696)

Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
garyschulte authored Mar 7, 2024
1 parent 854d35b commit 6109275
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,19 @@ public void onInitialSyncRestart() {
syncState.subscribeInSync(
isInSync -> {
if (isInSync != transactionPool.isEnabled()) {
if (isInSync) {
if (isInSync && syncState.isInitialSyncPhaseDone()) {
LOG.info("Node is in sync, enabling transaction handling");
enableTransactionHandling(
transactionTracker,
transactionPool,
transactionsMessageHandler,
pooledTransactionsMessageHandler);
} else {
LOG.info("Node out of sync, disabling transaction handling");
disableTransactionHandling(
transactionPool, transactionsMessageHandler, pooledTransactionsMessageHandler);
if (transactionPool.isEnabled()) {
LOG.info("Node out of sync, disabling transaction handling");
disableTransactionHandling(
transactionPool, transactionsMessageHandler, pooledTransactionsMessageHandler);
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -34,6 +35,7 @@
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.MiningParameters;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Synchronizer;
import org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.EthMessages;
Expand Down Expand Up @@ -129,6 +131,37 @@ public void notRegisteredToBlockAddedEventBeforeInitialSyncIsDone() {
assertThat(pool.isEnabled()).isFalse();
}

@Test
public void assertPoolDisabledIfChainInSyncWithoutInitialSync() {
SyncState syncSpy = spy(new SyncState(blockchain, ethPeers, true, Optional.empty()));
ArgumentCaptor<Synchronizer.InSyncListener> chainSyncCaptor =
ArgumentCaptor.forClass(Synchronizer.InSyncListener.class);

setupInitialSyncPhase(syncSpy);
// verify that we are registered to the sync state
verify(syncSpy).subscribeInSync(chainSyncCaptor.capture());
// Retrieve the captured InSyncListener
Synchronizer.InSyncListener chainSyncListener = chainSyncCaptor.getValue();

// mock chain being in sync:
chainSyncListener.onInSyncStatusChange(true);

// assert pool is disabled if chain in sync and initial sync not done
assertThat(pool.isEnabled()).isFalse();

// mock initial sync done (avoid triggering initial sync listener)
when(syncSpy.isInitialSyncPhaseDone()).thenReturn(true);

// assert pool is enabled when chain in sync and initial sync done
chainSyncListener.onInSyncStatusChange(true);
assertThat(pool.isEnabled()).isTrue();

// assert pool is re-disabled when initial sync is incomplete but chain reaches head:
when(syncSpy.isInitialSyncPhaseDone()).thenCallRealMethod();
chainSyncListener.onInSyncStatusChange(false);
assertThat(pool.isEnabled()).isFalse();
}

@Test
public void registeredToBlockAddedEventAfterInitialSyncIsDone() {
setupInitialSyncPhase(true);
Expand Down Expand Up @@ -231,7 +264,10 @@ public void incomingTransactionMessageHandlersRegisteredIfNoInitialSync() {

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,
Expand Down

0 comments on commit 6109275

Please sign in to comment.