fix unit tests

Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
pull/6920/head
Karim TAAM 1 year ago
parent 657a78e62b
commit 6d4a4bb582
  1. 20
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/worldstate/WorldStateStorageCoordinator.java
  2. 11
      ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/SnapWorldDownloadState.java
  3. 31
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/checkpointsync/CheckPointSyncChainDownloaderTest.java
  4. 73
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastDownloaderFactoryTest.java
  5. 1
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncActionsTest.java
  6. 1
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncChainDownloaderTest.java
  7. 147
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/FastSyncDownloaderTest.java
  8. 2
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/worldstate/FastWorldStateDownloaderTest.java
  9. 7
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fastsync/worldstate/LoadLocalDataStepTest.java

@ -26,7 +26,12 @@ import java.util.function.Function;
import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32; import org.apache.tuweni.bytes.Bytes32;
public record WorldStateStorageCoordinator(WorldStateKeyValueStorage worldStateKeyValueStorage) { public class WorldStateStorageCoordinator {
private final WorldStateKeyValueStorage worldStateKeyValueStorage;
public WorldStateStorageCoordinator(final WorldStateKeyValueStorage worldStateKeyValueStorage) {
this.worldStateKeyValueStorage = worldStateKeyValueStorage;
}
public DataStorageFormat getDataStorageFormat() { public DataStorageFormat getDataStorageFormat() {
return worldStateKeyValueStorage.getDataStorageFormat(); return worldStateKeyValueStorage.getDataStorageFormat();
@ -63,6 +68,15 @@ public record WorldStateStorageCoordinator(WorldStateKeyValueStorage worldStateK
return (STRATEGY) worldStateKeyValueStorage; return (STRATEGY) worldStateKeyValueStorage;
} }
public boolean isMatchingFlatMode(final FlatDbMode flatDbMode) {
if (getDataStorageFormat().equals(DataStorageFormat.BONSAI)) {
final BonsaiWorldStateKeyValueStorage bonsaiWorldStateStorageStrategy =
(BonsaiWorldStateKeyValueStorage) worldStateKeyValueStorage();
return bonsaiWorldStateStorageStrategy.getFlatDbMode().equals(flatDbMode);
}
return false;
}
public void applyOnMatchingFlatMode( public void applyOnMatchingFlatMode(
final FlatDbMode flatDbMode, final Consumer<BonsaiWorldStateKeyValueStorage> onStrategy) { final FlatDbMode flatDbMode, final Consumer<BonsaiWorldStateKeyValueStorage> onStrategy) {
applyOnMatchingStrategy( applyOnMatchingStrategy(
@ -134,4 +148,8 @@ public record WorldStateStorageCoordinator(WorldStateKeyValueStorage worldStateK
public void clear() { public void clear() {
worldStateKeyValueStorage.clear(); worldStateKeyValueStorage.clear();
} }
public WorldStateKeyValueStorage worldStateKeyValueStorage() {
return worldStateKeyValueStorage;
}
} }

@ -194,15 +194,10 @@ public class SnapWorldDownloadState extends WorldDownloadState<SnapDataRequest>
blockObserverId.ifPresent(blockchain::removeObserver); blockObserverId.ifPresent(blockchain::removeObserver);
// If the flat database healing process is not in progress and the flat database mode is // If the flat database healing process is not in progress and the flat database mode is
// FULL // FULL
if (!snapSyncState.isHealFlatDatabaseInProgress()) { if (!snapSyncState.isHealFlatDatabaseInProgress()
worldStateStorageCoordinator.applyOnMatchingFlatMode( && worldStateStorageCoordinator.isMatchingFlatMode(FlatDbMode.FULL)) {
FlatDbMode.FULL, startFlatDatabaseHeal(header);
bonsaiWorldStateStorageStrategy -> {
// Start the flat database healing process
startFlatDatabaseHeal(header);
});
} }
// If the flat database healing process is in progress or the flat database mode is not FULL // If the flat database healing process is in progress or the flat database mode is not FULL
else { else {
final WorldStateKeyValueStorage.Updater updater = worldStateStorageCoordinator.updater(); final WorldStateKeyValueStorage.Updater updater = worldStateStorageCoordinator.updater();

@ -20,6 +20,8 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain; import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil; import org.hyperledger.besu.ethereum.core.BlockchainSetupUtil;
@ -35,6 +37,7 @@ import org.hyperledger.besu.ethereum.eth.sync.fastsync.FastSyncState;
import org.hyperledger.besu.ethereum.eth.sync.fastsync.checkpoint.Checkpoint; import org.hyperledger.besu.ethereum.eth.sync.fastsync.checkpoint.Checkpoint;
import org.hyperledger.besu.ethereum.eth.sync.fastsync.checkpoint.ImmutableCheckpoint; import org.hyperledger.besu.ethereum.eth.sync.fastsync.checkpoint.ImmutableCheckpoint;
import org.hyperledger.besu.ethereum.eth.sync.state.SyncState; import org.hyperledger.besu.ethereum.eth.sync.state.SyncState;
import org.hyperledger.besu.ethereum.forest.storage.ForestWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat; import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator; import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator;
@ -53,9 +56,6 @@ import org.junit.jupiter.params.provider.ArgumentsSource;
public class CheckPointSyncChainDownloaderTest { public class CheckPointSyncChainDownloaderTest {
private final WorldStateStorageCoordinator worldStateStorageCoordinator =
mock(WorldStateStorageCoordinator.class);
protected ProtocolSchedule protocolSchedule; protected ProtocolSchedule protocolSchedule;
protected EthProtocolManager ethProtocolManager; protected EthProtocolManager ethProtocolManager;
protected EthContext ethContext; protected EthContext ethContext;
@ -67,6 +67,8 @@ public class CheckPointSyncChainDownloaderTest {
protected Blockchain otherBlockchain; protected Blockchain otherBlockchain;
private Checkpoint checkpoint; private Checkpoint checkpoint;
private WorldStateStorageCoordinator worldStateStorageCoordinator;
static class CheckPointSyncChainDownloaderTestArguments implements ArgumentsProvider { static class CheckPointSyncChainDownloaderTestArguments implements ArgumentsProvider {
@Override @Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext context) { public Stream<? extends Arguments> provideArguments(final ExtensionContext context) {
@ -75,11 +77,26 @@ public class CheckPointSyncChainDownloaderTest {
} }
} }
public void setup(final DataStorageFormat storageFormat) { public void setup(final DataStorageFormat dataStorageFormat) {
when(worldStateStorageCoordinator.isWorldStateAvailable(any(), any())).thenReturn(true); final WorldStateKeyValueStorage worldStateKeyValueStorage;
final BlockchainSetupUtil localBlockchainSetup = BlockchainSetupUtil.forTesting(storageFormat); if (dataStorageFormat.equals(DataStorageFormat.BONSAI)) {
worldStateKeyValueStorage = mock(BonsaiWorldStateKeyValueStorage.class);
when(((BonsaiWorldStateKeyValueStorage) worldStateKeyValueStorage)
.isWorldStateAvailable(any(), any()))
.thenReturn(true);
} else {
worldStateKeyValueStorage = mock(ForestWorldStateKeyValueStorage.class);
when(((ForestWorldStateKeyValueStorage) worldStateKeyValueStorage)
.isWorldStateAvailable(any()))
.thenReturn(true);
}
when(worldStateKeyValueStorage.getDataStorageFormat()).thenReturn(dataStorageFormat);
worldStateStorageCoordinator = new WorldStateStorageCoordinator(worldStateKeyValueStorage);
final BlockchainSetupUtil localBlockchainSetup =
BlockchainSetupUtil.forTesting(dataStorageFormat);
localBlockchain = localBlockchainSetup.getBlockchain(); localBlockchain = localBlockchainSetup.getBlockchain();
otherBlockchainSetup = BlockchainSetupUtil.forTesting(storageFormat); otherBlockchainSetup = BlockchainSetupUtil.forTesting(dataStorageFormat);
otherBlockchain = otherBlockchainSetup.getBlockchain(); otherBlockchain = otherBlockchainSetup.getBlockchain();
protocolSchedule = localBlockchainSetup.getProtocolSchedule(); protocolSchedule = localBlockchainSetup.getProtocolSchedule();
protocolContext = localBlockchainSetup.getProtocolContext(); protocolContext = localBlockchainSetup.getProtocolContext();

@ -23,6 +23,8 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import org.hyperledger.besu.ethereum.ProtocolContext; import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain; import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.eth.manager.EthContext; import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.sync.PivotBlockSelector; import org.hyperledger.besu.ethereum.eth.sync.PivotBlockSelector;
@ -30,7 +32,9 @@ import org.hyperledger.besu.ethereum.eth.sync.SyncMode;
import org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration; import org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration;
import org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.FastDownloaderFactory; import org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.FastDownloaderFactory;
import org.hyperledger.besu.ethereum.eth.sync.state.SyncState; import org.hyperledger.besu.ethereum.eth.sync.state.SyncState;
import org.hyperledger.besu.ethereum.forest.storage.ForestWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator; import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator;
import org.hyperledger.besu.plugin.services.MetricsSystem; import org.hyperledger.besu.plugin.services.MetricsSystem;
@ -40,10 +44,16 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.Clock; import java.time.Clock;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.junit.jupiter.MockitoSettings;
@ -59,15 +69,36 @@ public class FastDownloaderFactoryTest {
@Mock private ProtocolContext protocolContext; @Mock private ProtocolContext protocolContext;
@Mock private MetricsSystem metricsSystem; @Mock private MetricsSystem metricsSystem;
@Mock private EthContext ethContext; @Mock private EthContext ethContext;
@Mock private WorldStateStorageCoordinator worldStateStorageCoordinator;
@Mock private SyncState syncState; @Mock private SyncState syncState;
@Mock private Clock clock; @Mock private Clock clock;
@Mock private Path dataDirectory; @Mock private Path dataDirectory;
@Mock private PivotBlockSelector pivotBlockSelector; @Mock private PivotBlockSelector pivotBlockSelector;
private WorldStateKeyValueStorage worldStateKeyValueStorage;
private WorldStateStorageCoordinator worldStateStorageCoordinator;
@SuppressWarnings("unchecked") static class FastDownloaderFactoryTestArguments implements ArgumentsProvider {
@Test @Override
public void shouldThrowIfSyncModeChangedWhileFastSyncIncomplete() { public Stream<? extends Arguments> provideArguments(final ExtensionContext context) {
return Stream.of(
Arguments.of(DataStorageFormat.BONSAI), Arguments.of(DataStorageFormat.FOREST));
}
}
public void setup(final DataStorageFormat dataStorageFormat) {
if (dataStorageFormat.equals(DataStorageFormat.BONSAI)) {
worldStateKeyValueStorage = mock(BonsaiWorldStateKeyValueStorage.class);
} else {
worldStateKeyValueStorage = mock(ForestWorldStateKeyValueStorage.class);
}
when(worldStateKeyValueStorage.getDataStorageFormat()).thenReturn(dataStorageFormat);
worldStateStorageCoordinator = new WorldStateStorageCoordinator(worldStateKeyValueStorage);
}
@ParameterizedTest
@ArgumentsSource(FastDownloaderFactoryTestArguments.class)
public void shouldThrowIfSyncModeChangedWhileFastSyncIncomplete(
final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
initDataDirectory(true); initDataDirectory(true);
when(syncConfig.getSyncMode()).thenReturn(SyncMode.FULL); when(syncConfig.getSyncMode()).thenReturn(SyncMode.FULL);
@ -88,8 +119,11 @@ public class FastDownloaderFactoryTest {
} }
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
@Test @ParameterizedTest
public void shouldNotThrowIfSyncModeChangedWhileFastSyncComplete() { @ArgumentsSource(FastDownloaderFactoryTestArguments.class)
public void shouldNotThrowIfSyncModeChangedWhileFastSyncComplete(
final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
initDataDirectory(false); initDataDirectory(false);
when(syncConfig.getSyncMode()).thenReturn(SyncMode.FULL); when(syncConfig.getSyncMode()).thenReturn(SyncMode.FULL);
@ -109,8 +143,11 @@ public class FastDownloaderFactoryTest {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @ParameterizedTest
public void shouldNotThrowWhenFastSyncModeRequested() throws NoSuchFieldException { @ArgumentsSource(FastDownloaderFactoryTestArguments.class)
public void shouldNotThrowWhenFastSyncModeRequested(final DataStorageFormat dataStorageFormat)
throws NoSuchFieldException {
setup(dataStorageFormat);
initDataDirectory(false); initDataDirectory(false);
final MutableBlockchain mutableBlockchain = mock(MutableBlockchain.class); final MutableBlockchain mutableBlockchain = mock(MutableBlockchain.class);
@ -133,8 +170,12 @@ public class FastDownloaderFactoryTest {
verify(mutableBlockchain).getChainHeadBlockNumber(); verify(mutableBlockchain).getChainHeadBlockNumber();
} }
@Test @ParameterizedTest
public void shouldClearWorldStateDuringFastSyncWhenStateQueDirectoryExists() throws IOException { @ArgumentsSource(FastDownloaderFactoryTestArguments.class)
public void shouldClearWorldStateDuringFastSyncWhenStateQueDirectoryExists(
final DataStorageFormat dataStorageFormat) throws IOException {
Assumptions.assumeTrue(dataStorageFormat == DataStorageFormat.FOREST);
setup(dataStorageFormat);
when(syncConfig.getSyncMode()).thenReturn(SyncMode.FAST); when(syncConfig.getSyncMode()).thenReturn(SyncMode.FAST);
final MutableBlockchain mutableBlockchain = mock(MutableBlockchain.class); final MutableBlockchain mutableBlockchain = mock(MutableBlockchain.class);
when(mutableBlockchain.getChainHeadBlockNumber()).thenReturn(0L); when(mutableBlockchain.getChainHeadBlockNumber()).thenReturn(0L);
@ -160,12 +201,16 @@ public class FastDownloaderFactoryTest {
syncState, syncState,
clock); clock);
verify(worldStateStorageCoordinator).clear(); verify(worldStateKeyValueStorage).clear();
assertThat(Files.exists(stateQueueDir)).isFalse(); assertThat(Files.exists(stateQueueDir)).isFalse();
} }
@Test @ParameterizedTest
public void shouldCrashWhenStateQueueIsNotDirectory() throws IOException { @ArgumentsSource(FastDownloaderFactoryTestArguments.class)
public void shouldCrashWhenStateQueueIsNotDirectory(final DataStorageFormat dataStorageFormat)
throws IOException {
Assumptions.assumeTrue(dataStorageFormat == DataStorageFormat.FOREST);
setup(dataStorageFormat);
when(syncConfig.getSyncMode()).thenReturn(SyncMode.FAST); when(syncConfig.getSyncMode()).thenReturn(SyncMode.FAST);
final MutableBlockchain mutableBlockchain = mock(MutableBlockchain.class); final MutableBlockchain mutableBlockchain = mock(MutableBlockchain.class);
when(mutableBlockchain.getChainHeadBlockNumber()).thenReturn(0L); when(mutableBlockchain.getChainHeadBlockNumber()).thenReturn(0L);

@ -86,6 +86,7 @@ public class FastSyncActionsTest {
} }
public void setUp(final DataStorageFormat storageFormat) { public void setUp(final DataStorageFormat storageFormat) {
when(worldStateStorageCoordinator.getDataStorageFormat()).thenReturn(storageFormat);
blockchainSetupUtil = BlockchainSetupUtil.forTesting(storageFormat); blockchainSetupUtil = BlockchainSetupUtil.forTesting(storageFormat);
blockchainSetupUtil.importAllBlocks(); blockchainSetupUtil.importAllBlocks();
blockchain = blockchainSetupUtil.getBlockchain(); blockchain = blockchainSetupUtil.getBlockchain();

@ -74,6 +74,7 @@ public class FastSyncChainDownloaderTest {
} }
public void setup(final DataStorageFormat storageFormat) { public void setup(final DataStorageFormat storageFormat) {
when(worldStateStorageCoordinator.getDataStorageFormat()).thenReturn(storageFormat);
when(worldStateStorageCoordinator.isWorldStateAvailable(any(), any())).thenReturn(true); when(worldStateStorageCoordinator.isWorldStateAvailable(any(), any())).thenReturn(true);
final BlockchainSetupUtil localBlockchainSetup = BlockchainSetupUtil.forTesting(storageFormat); final BlockchainSetupUtil localBlockchainSetup = BlockchainSetupUtil.forTesting(storageFormat);
localBlockchain = localBlockchainSetup.getBlockchain(); localBlockchain = localBlockchainSetup.getBlockchain();

@ -26,6 +26,8 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import org.hyperledger.besu.ethereum.WorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture; import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.eth.sync.ChainDownloader; import org.hyperledger.besu.ethereum.eth.sync.ChainDownloader;
@ -34,6 +36,7 @@ import org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.FastWorldState
import org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.NodeDataRequest; import org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.NodeDataRequest;
import org.hyperledger.besu.ethereum.eth.sync.worldstate.StalledDownloadException; import org.hyperledger.besu.ethereum.eth.sync.worldstate.StalledDownloadException;
import org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldStateDownloader; import org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldStateDownloader;
import org.hyperledger.besu.ethereum.forest.storage.ForestWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat; import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator; import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator;
import org.hyperledger.besu.services.tasks.TaskCollection; import org.hyperledger.besu.services.tasks.TaskCollection;
@ -44,19 +47,20 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
public class FastSyncDownloaderTest { public class FastSyncDownloaderTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private final FastSyncActions fastSyncActions = mock(FastSyncActions.class); private final FastSyncActions fastSyncActions = mock(FastSyncActions.class);
private final WorldStateStorageCoordinator worldStateStorageCoordinator =
mock(WorldStateStorageCoordinator.class);
private final WorldStateDownloader worldStateDownloader = mock(FastWorldStateDownloader.class); private final WorldStateDownloader worldStateDownloader = mock(FastWorldStateDownloader.class);
private final FastSyncStateStorage storage = mock(FastSyncStateStorage.class); private final FastSyncStateStorage storage = mock(FastSyncStateStorage.class);
@ -66,25 +70,47 @@ public class FastSyncDownloaderTest {
private final ChainDownloader chainDownloader = mock(ChainDownloader.class); private final ChainDownloader chainDownloader = mock(ChainDownloader.class);
private final Path fastSyncDataDirectory = null; private final Path fastSyncDataDirectory = null;
private WorldStateStorageCoordinator worldStateStorageCoordinator;
private FastSyncDownloader<NodeDataRequest> downloader;
static class FastSyncDownloaderTestArguments implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext context) {
return Stream.of(
Arguments.of(DataStorageFormat.BONSAI), Arguments.of(DataStorageFormat.FOREST));
}
}
private final FastSyncDownloader<NodeDataRequest> downloader = public void setup(final DataStorageFormat dataStorageFormat) {
new FastSyncDownloader<>( final WorldStateKeyValueStorage worldStateKeyValueStorage;
fastSyncActions, if (dataStorageFormat.equals(DataStorageFormat.BONSAI)) {
worldStateStorageCoordinator, worldStateKeyValueStorage = mock(BonsaiWorldStateKeyValueStorage.class);
worldStateDownloader, when(((BonsaiWorldStateKeyValueStorage) worldStateKeyValueStorage)
storage, .isWorldStateAvailable(any(), any()))
taskCollection, .thenReturn(true);
fastSyncDataDirectory, } else {
FastSyncState.EMPTY_SYNC_STATE); worldStateKeyValueStorage = mock(ForestWorldStateKeyValueStorage.class);
when(((ForestWorldStateKeyValueStorage) worldStateKeyValueStorage)
@BeforeEach .isWorldStateAvailable(any()))
public void setup() { .thenReturn(true);
when(worldStateStorageCoordinator.getDataStorageFormat()).thenReturn(DataStorageFormat.FOREST); }
when(worldStateStorageCoordinator.isWorldStateAvailable(any(), any())).thenReturn(true); when(worldStateKeyValueStorage.getDataStorageFormat()).thenReturn(dataStorageFormat);
worldStateStorageCoordinator = new WorldStateStorageCoordinator(worldStateKeyValueStorage);
downloader =
new FastSyncDownloader<>(
fastSyncActions,
worldStateStorageCoordinator,
worldStateDownloader,
storage,
taskCollection,
fastSyncDataDirectory,
FastSyncState.EMPTY_SYNC_STATE);
} }
@Test @ParameterizedTest
public void shouldCompleteFastSyncSuccessfully() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldCompleteFastSyncSuccessfully(final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final FastSyncState selectPivotBlockState = new FastSyncState(50); final FastSyncState selectPivotBlockState = new FastSyncState(50);
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader(); final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader();
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState(pivotBlockHeader); final FastSyncState downloadPivotBlockHeaderState = new FastSyncState(pivotBlockHeader);
@ -112,8 +138,10 @@ public class FastSyncDownloaderTest {
assertThat(result).isCompletedWithValue(downloadPivotBlockHeaderState); assertThat(result).isCompletedWithValue(downloadPivotBlockHeaderState);
} }
@Test @ParameterizedTest
public void shouldResumeFastSync() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldResumeFastSync(final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader(); final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader();
final FastSyncState fastSyncState = new FastSyncState(pivotBlockHeader); final FastSyncState fastSyncState = new FastSyncState(pivotBlockHeader);
final CompletableFuture<FastSyncState> complete = completedFuture(fastSyncState); final CompletableFuture<FastSyncState> complete = completedFuture(fastSyncState);
@ -148,8 +176,10 @@ public class FastSyncDownloaderTest {
assertThat(result).isCompletedWithValue(fastSyncState); assertThat(result).isCompletedWithValue(fastSyncState);
} }
@Test @ParameterizedTest
public void shouldAbortIfSelectPivotBlockFails() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldAbortIfSelectPivotBlockFails(final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
when(fastSyncActions.selectPivotBlock(FastSyncState.EMPTY_SYNC_STATE)) when(fastSyncActions.selectPivotBlock(FastSyncState.EMPTY_SYNC_STATE))
.thenThrow(new FastSyncException(FastSyncError.UNEXPECTED_ERROR)); .thenThrow(new FastSyncException(FastSyncError.UNEXPECTED_ERROR));
@ -161,8 +191,10 @@ public class FastSyncDownloaderTest {
verifyNoMoreInteractions(fastSyncActions); verifyNoMoreInteractions(fastSyncActions);
} }
@Test @ParameterizedTest
public void shouldAbortIfWorldStateDownloadFails() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldAbortIfWorldStateDownloadFails(final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final CompletableFuture<Void> worldStateFuture = new CompletableFuture<>(); final CompletableFuture<Void> worldStateFuture = new CompletableFuture<>();
final CompletableFuture<Void> chainFuture = new CompletableFuture<>(); final CompletableFuture<Void> chainFuture = new CompletableFuture<>();
final FastSyncState selectPivotBlockState = new FastSyncState(50); final FastSyncState selectPivotBlockState = new FastSyncState(50);
@ -199,8 +231,10 @@ public class FastSyncDownloaderTest {
assertThat(chainFuture).isCancelled(); assertThat(chainFuture).isCancelled();
} }
@Test @ParameterizedTest
public void shouldAbortIfChainDownloadFails() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldAbortIfChainDownloadFails(final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final CompletableFuture<Void> chainFuture = new CompletableFuture<>(); final CompletableFuture<Void> chainFuture = new CompletableFuture<>();
final CompletableFuture<Void> worldStateFuture = new CompletableFuture<>(); final CompletableFuture<Void> worldStateFuture = new CompletableFuture<>();
final FastSyncState selectPivotBlockState = new FastSyncState(50); final FastSyncState selectPivotBlockState = new FastSyncState(50);
@ -235,8 +269,10 @@ public class FastSyncDownloaderTest {
assertThat(worldStateFuture).isCancelled(); assertThat(worldStateFuture).isCancelled();
} }
@Test @ParameterizedTest
public void shouldAbortIfStopped() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldAbortIfStopped(final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final FastSyncState selectPivotBlockState = new FastSyncState(50); final FastSyncState selectPivotBlockState = new FastSyncState(50);
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader(); final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader();
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState(pivotBlockHeader); final FastSyncState downloadPivotBlockHeaderState = new FastSyncState(pivotBlockHeader);
@ -269,8 +305,11 @@ public class FastSyncDownloaderTest {
verifyNoMoreInteractions(fastSyncActions, worldStateDownloader, storage); verifyNoMoreInteractions(fastSyncActions, worldStateDownloader, storage);
} }
@Test @ParameterizedTest
public void shouldNotConsiderFastSyncCompleteIfOnlyWorldStateDownloadIsComplete() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldNotConsiderFastSyncCompleteIfOnlyWorldStateDownloadIsComplete(
final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final CompletableFuture<Void> chainFuture = new CompletableFuture<>(); final CompletableFuture<Void> chainFuture = new CompletableFuture<>();
final CompletableFuture<Void> worldStateFuture = new CompletableFuture<>(); final CompletableFuture<Void> worldStateFuture = new CompletableFuture<>();
final FastSyncState selectPivotBlockState = new FastSyncState(50); final FastSyncState selectPivotBlockState = new FastSyncState(50);
@ -304,8 +343,11 @@ public class FastSyncDownloaderTest {
assertThat(result).isNotDone(); assertThat(result).isNotDone();
} }
@Test @ParameterizedTest
public void shouldNotConsiderFastSyncCompleteIfOnlyChainDownloadIsComplete() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldNotConsiderFastSyncCompleteIfOnlyChainDownloadIsComplete(
final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final CompletableFuture<Void> chainFuture = new CompletableFuture<>(); final CompletableFuture<Void> chainFuture = new CompletableFuture<>();
final CompletableFuture<Void> worldStateFuture = new CompletableFuture<>(); final CompletableFuture<Void> worldStateFuture = new CompletableFuture<>();
final FastSyncState selectPivotBlockState = new FastSyncState(50); final FastSyncState selectPivotBlockState = new FastSyncState(50);
@ -340,8 +382,11 @@ public class FastSyncDownloaderTest {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @ParameterizedTest
public void shouldResetFastSyncStateAndRestartProcessIfWorldStateIsUnavailable() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldResetFastSyncStateAndRestartProcessIfWorldStateIsUnavailable(
final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final CompletableFuture<Void> firstWorldStateFuture = new CompletableFuture<>(); final CompletableFuture<Void> firstWorldStateFuture = new CompletableFuture<>();
final CompletableFuture<Void> secondWorldStateFuture = new CompletableFuture<>(); final CompletableFuture<Void> secondWorldStateFuture = new CompletableFuture<>();
final CompletableFuture<Void> chainFuture = new CompletableFuture<>(); final CompletableFuture<Void> chainFuture = new CompletableFuture<>();
@ -411,8 +456,11 @@ public class FastSyncDownloaderTest {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @ParameterizedTest
public void shouldResetFastSyncStateAndRestartProcessIfANonFastSyncExceptionOccurs() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldResetFastSyncStateAndRestartProcessIfANonFastSyncExceptionOccurs(
final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final CompletableFuture<Void> firstWorldStateFuture = new CompletableFuture<>(); final CompletableFuture<Void> firstWorldStateFuture = new CompletableFuture<>();
final CompletableFuture<Void> secondWorldStateFuture = new CompletableFuture<>(); final CompletableFuture<Void> secondWorldStateFuture = new CompletableFuture<>();
final CompletableFuture<Void> chainFuture = new CompletableFuture<>(); final CompletableFuture<Void> chainFuture = new CompletableFuture<>();
@ -485,14 +533,20 @@ public class FastSyncDownloaderTest {
assertThat(result).isCompletedWithValue(secondDownloadPivotBlockHeaderState); assertThat(result).isCompletedWithValue(secondDownloadPivotBlockHeaderState);
} }
@Test @ParameterizedTest
public void shouldNotHaveTrailingPeerRequirementsBeforePivotBlockSelected() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldNotHaveTrailingPeerRequirementsBeforePivotBlockSelected(
final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
downloader.start(); downloader.start();
Assertions.assertThat(downloader.calculateTrailingPeerRequirements()).isEmpty(); Assertions.assertThat(downloader.calculateTrailingPeerRequirements()).isEmpty();
} }
@Test @ParameterizedTest
public void shouldNotAllowPeersBeforePivotBlockOnceSelected() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldNotAllowPeersBeforePivotBlockOnceSelected(
final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final FastSyncState selectPivotBlockState = new FastSyncState(50); final FastSyncState selectPivotBlockState = new FastSyncState(50);
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader(); final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader();
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState(pivotBlockHeader); final FastSyncState downloadPivotBlockHeaderState = new FastSyncState(pivotBlockHeader);
@ -513,8 +567,11 @@ public class FastSyncDownloaderTest {
.contains(new TrailingPeerRequirements(50, 0)); .contains(new TrailingPeerRequirements(50, 0));
} }
@Test @ParameterizedTest
public void shouldNotHaveTrailingPeerRequirementsAfterDownloadCompletes() { @ArgumentsSource(FastSyncDownloaderTestArguments.class)
public void shouldNotHaveTrailingPeerRequirementsAfterDownloadCompletes(
final DataStorageFormat dataStorageFormat) {
setup(dataStorageFormat);
final FastSyncState selectPivotBlockState = new FastSyncState(50); final FastSyncState selectPivotBlockState = new FastSyncState(50);
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader(); final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader();
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState(pivotBlockHeader); final FastSyncState downloadPivotBlockHeaderState = new FastSyncState(pivotBlockHeader);

@ -218,7 +218,7 @@ class FastWorldStateDownloaderTest {
final WorldStateDownloader downloader = final WorldStateDownloader downloader =
createDownloader( createDownloader(
ethProtocolManager.ethContext(), ethProtocolManager.ethContext(),
worldStateArchive.getWorldStateStorage().worldStateKeyValueStorage(), worldStateArchive.getWorldStateStorage(),
taskCollection); taskCollection);
final FastSyncState fastSyncState = new FastSyncState(header); final FastSyncState fastSyncState = new FastSyncState(header);

@ -23,6 +23,7 @@ import static org.mockito.Mockito.when;
import org.hyperledger.besu.datatypes.Hash; import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage; import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage;
import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat;
import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator; import org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator;
import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem; import org.hyperledger.besu.metrics.noop.NoOpMetricsSystem;
import org.hyperledger.besu.services.pipeline.Pipe; import org.hyperledger.besu.services.pipeline.Pipe;
@ -32,6 +33,7 @@ import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -54,6 +56,11 @@ public class LoadLocalDataStepTest {
new LoadLocalDataStep( new LoadLocalDataStep(
new WorldStateStorageCoordinator(worldStateKeyValueStorage), new NoOpMetricsSystem()); new WorldStateStorageCoordinator(worldStateKeyValueStorage), new NoOpMetricsSystem());
@BeforeEach
public void setup() {
when(worldStateKeyValueStorage.getDataStorageFormat()).thenReturn(DataStorageFormat.BONSAI);
}
@Test @Test
public void shouldReturnStreamWithUnchangedTaskWhenDataNotPresent() { public void shouldReturnStreamWithUnchangedTaskWhenDataNotPresent() {
final Stream<Task<NodeDataRequest>> output = final Stream<Task<NodeDataRequest>> output =

Loading…
Cancel
Save