@ -50,9 +50,6 @@ import org.junit.Test;
public class FastSyncDownloaderTest {
private static final CompletableFuture < FastSyncState > COMPLETE =
completedFuture ( FastSyncState . EMPTY_SYNC_STATE ) ;
@SuppressWarnings ( "unchecked" )
private final FastSyncActions fastSyncActions = mock ( FastSyncActions . class ) ;
@ -88,7 +85,6 @@ public class FastSyncDownloaderTest {
final FastSyncState selectPivotBlockState = new FastSyncState ( 50 ) ;
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture ( ) . number ( 50 ) . buildHeader ( ) ;
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState ( pivotBlockHeader ) ;
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn ( completedFuture ( selectPivotBlockState ) ) ;
when ( fastSyncActions . downloadPivotBlockHeader ( selectPivotBlockState ) )
@ -102,7 +98,6 @@ public class FastSyncDownloaderTest {
final CompletableFuture < FastSyncState > result = downloader . start ( ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( selectPivotBlockState ) ;
verify ( storage ) . storeState ( downloadPivotBlockHeaderState ) ;
@ -119,7 +114,6 @@ public class FastSyncDownloaderTest {
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture ( ) . number ( 50 ) . buildHeader ( ) ;
final FastSyncState fastSyncState = new FastSyncState ( pivotBlockHeader ) ;
final CompletableFuture < FastSyncState > complete = completedFuture ( fastSyncState ) ;
when ( fastSyncActions . waitForSuitablePeers ( fastSyncState ) ) . thenReturn ( complete ) ;
when ( fastSyncActions . selectPivotBlock ( fastSyncState ) ) . thenReturn ( complete ) ;
when ( fastSyncActions . downloadPivotBlockHeader ( fastSyncState ) ) . thenReturn ( complete ) ;
when ( fastSyncActions . createChainDownloader ( fastSyncState ) ) . thenReturn ( chainDownloader ) ;
@ -140,7 +134,6 @@ public class FastSyncDownloaderTest {
final CompletableFuture < FastSyncState > result = resumedDownloader . start ( ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( fastSyncState ) ;
verify ( fastSyncActions ) . selectPivotBlock ( fastSyncState ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( fastSyncState ) ;
verify ( storage ) . storeState ( fastSyncState ) ;
@ -152,23 +145,8 @@ public class FastSyncDownloaderTest {
assertThat ( result ) . isCompletedWithValue ( fastSyncState ) ;
}
@Test
public void shouldAbortIfWaitForSuitablePeersFails ( ) {
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn (
CompletableFuture . failedFuture ( new FastSyncException ( FastSyncError . UNEXPECTED_ERROR ) ) ) ;
final CompletableFuture < FastSyncState > result = downloader . start ( ) ;
assertCompletedExceptionally ( result , FastSyncError . UNEXPECTED_ERROR ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verifyNoMoreInteractions ( fastSyncActions ) ;
}
@Test
public void shouldAbortIfSelectPivotBlockFails ( ) {
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenThrow ( new FastSyncException ( FastSyncError . UNEXPECTED_ERROR ) ) ;
@ -176,7 +154,6 @@ public class FastSyncDownloaderTest {
assertCompletedExceptionally ( result , FastSyncError . UNEXPECTED_ERROR ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verifyNoMoreInteractions ( fastSyncActions ) ;
}
@ -188,7 +165,7 @@ public class FastSyncDownloaderTest {
final FastSyncState selectPivotBlockState = new FastSyncState ( 50 ) ;
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture ( ) . number ( 50 ) . buildHeader ( ) ;
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState ( pivotBlockHeader ) ;
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn ( completedFuture ( selectPivotBlockState ) ) ;
when ( fastSyncActions . downloadPivotBlockHeader ( selectPivotBlockState ) )
@ -202,7 +179,6 @@ public class FastSyncDownloaderTest {
final CompletableFuture < FastSyncState > result = downloader . start ( ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( selectPivotBlockState ) ;
verify ( storage ) . storeState ( downloadPivotBlockHeaderState ) ;
@ -227,7 +203,7 @@ public class FastSyncDownloaderTest {
final FastSyncState selectPivotBlockState = new FastSyncState ( 50 ) ;
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture ( ) . number ( 50 ) . buildHeader ( ) ;
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState ( pivotBlockHeader ) ;
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn ( completedFuture ( selectPivotBlockState ) ) ;
when ( fastSyncActions . downloadPivotBlockHeader ( selectPivotBlockState ) )
@ -241,7 +217,6 @@ public class FastSyncDownloaderTest {
final CompletableFuture < FastSyncState > result = downloader . start ( ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( selectPivotBlockState ) ;
verify ( fastSyncActions ) . createChainDownloader ( downloadPivotBlockHeaderState ) ;
@ -262,7 +237,7 @@ public class FastSyncDownloaderTest {
final FastSyncState selectPivotBlockState = new FastSyncState ( 50 ) ;
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture ( ) . number ( 50 ) . buildHeader ( ) ;
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState ( pivotBlockHeader ) ;
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn ( completedFuture ( selectPivotBlockState ) ) ;
doAnswer (
@ -284,7 +259,6 @@ public class FastSyncDownloaderTest {
Throwable thrown = catchThrowable ( ( ) - > result . get ( ) ) ;
assertThat ( thrown ) . hasCauseExactlyInstanceOf ( CancellationException . class ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( selectPivotBlockState ) ;
verify ( storage ) . storeState ( downloadPivotBlockHeaderState ) ;
@ -299,7 +273,7 @@ public class FastSyncDownloaderTest {
final FastSyncState selectPivotBlockState = new FastSyncState ( 50 ) ;
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture ( ) . number ( 50 ) . buildHeader ( ) ;
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState ( pivotBlockHeader ) ;
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn ( completedFuture ( selectPivotBlockState ) ) ;
when ( fastSyncActions . downloadPivotBlockHeader ( selectPivotBlockState ) )
@ -313,7 +287,6 @@ public class FastSyncDownloaderTest {
final CompletableFuture < FastSyncState > result = downloader . start ( ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( selectPivotBlockState ) ;
verify ( fastSyncActions ) . createChainDownloader ( downloadPivotBlockHeaderState ) ;
@ -335,7 +308,7 @@ public class FastSyncDownloaderTest {
final FastSyncState selectPivotBlockState = new FastSyncState ( 50 ) ;
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture ( ) . number ( 50 ) . buildHeader ( ) ;
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState ( pivotBlockHeader ) ;
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn ( completedFuture ( selectPivotBlockState ) ) ;
when ( fastSyncActions . downloadPivotBlockHeader ( selectPivotBlockState ) )
@ -349,7 +322,6 @@ public class FastSyncDownloaderTest {
final CompletableFuture < FastSyncState > result = downloader . start ( ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( selectPivotBlockState ) ;
verify ( fastSyncActions ) . createChainDownloader ( downloadPivotBlockHeaderState ) ;
@ -379,8 +351,8 @@ public class FastSyncDownloaderTest {
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState ( pivotBlockHeader ) ;
final FastSyncState secondDownloadPivotBlockHeaderState =
new FastSyncState ( secondPivotBlockHeader ) ;
// First attempt
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn (
completedFuture ( selectPivotBlockState ) , completedFuture ( secondSelectPivotBlockState ) ) ;
@ -406,7 +378,6 @@ public class FastSyncDownloaderTest {
final CompletableFuture < FastSyncState > result = downloader . start ( ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( selectPivotBlockState ) ;
verify ( storage ) . storeState ( downloadPivotBlockHeaderState ) ;
@ -423,7 +394,6 @@ public class FastSyncDownloaderTest {
// A real chain downloader would cause the chainFuture to complete when cancel is called.
chainFuture . completeExceptionally ( new CancellationException ( ) ) ;
verify ( fastSyncActions , times ( 2 ) ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions , times ( 2 ) ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( secondSelectPivotBlockState ) ;
verify ( storage ) . storeState ( secondDownloadPivotBlockHeaderState ) ;
@ -452,8 +422,8 @@ public class FastSyncDownloaderTest {
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState ( pivotBlockHeader ) ;
final FastSyncState secondDownloadPivotBlockHeaderState =
new FastSyncState ( secondPivotBlockHeader ) ;
// First attempt
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn (
completedFuture ( selectPivotBlockState ) , completedFuture ( secondSelectPivotBlockState ) ) ;
@ -481,7 +451,6 @@ public class FastSyncDownloaderTest {
final CompletableFuture < FastSyncState > result = downloader . start ( ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( selectPivotBlockState ) ;
verify ( storage ) . storeState ( downloadPivotBlockHeaderState ) ;
@ -500,7 +469,6 @@ public class FastSyncDownloaderTest {
chainFuture . completeExceptionally ( new CancellationException ( ) ) ;
verify ( fastSyncActions ) . scheduleFutureTask ( any ( ) , any ( ) ) ;
verify ( fastSyncActions , times ( 2 ) ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions , times ( 2 ) ) . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) ;
verify ( fastSyncActions ) . downloadPivotBlockHeader ( secondSelectPivotBlockState ) ;
verify ( storage ) . storeState ( secondDownloadPivotBlockHeaderState ) ;
@ -516,12 +484,7 @@ public class FastSyncDownloaderTest {
@Test
public void shouldNotHaveTrailingPeerRequirementsBeforePivotBlockSelected ( ) {
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn ( new CompletableFuture < > ( ) ) ;
downloader . start ( ) ;
verify ( fastSyncActions ) . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ;
Assertions . assertThat ( downloader . calculateTrailingPeerRequirements ( ) ) . isEmpty ( ) ;
}
@ -530,7 +493,7 @@ public class FastSyncDownloaderTest {
final FastSyncState selectPivotBlockState = new FastSyncState ( 50 ) ;
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture ( ) . number ( 50 ) . buildHeader ( ) ;
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState ( pivotBlockHeader ) ;
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn ( completedFuture ( selectPivotBlockState ) ) ;
when ( fastSyncActions . downloadPivotBlockHeader ( selectPivotBlockState ) )
@ -552,7 +515,7 @@ public class FastSyncDownloaderTest {
final FastSyncState selectPivotBlockState = new FastSyncState ( 50 ) ;
final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture ( ) . number ( 50 ) . buildHeader ( ) ;
final FastSyncState downloadPivotBlockHeaderState = new FastSyncState ( pivotBlockHeader ) ;
when ( fastSyncActions . waitForSuitablePeers ( FastSyncState . EMPTY_SYNC_STATE ) ) . thenReturn ( COMPLETE ) ;
when ( fastSyncActions . selectPivotBlock ( FastSyncState . EMPTY_SYNC_STATE ) )
. thenReturn ( completedFuture ( selectPivotBlockState ) ) ;
when ( fastSyncActions . downloadPivotBlockHeader ( selectPivotBlockState ) )