Class DownloadProgressTracker

java.lang.Object
org.bitcoinj.core.listeners.DownloadProgressTracker
All Implemented Interfaces:
BlockchainDownloadEventListener, BlocksDownloadedEventListener, ChainDownloadStartedEventListener

public class DownloadProgressTracker extends Object implements BlockchainDownloadEventListener

An implementation of BlockchainDownloadEventListener that listens to chain download events and tracks progress as a percentage. The default implementation prints progress to stdout, but you can subclass it and override the progress method to update a GUI instead.

  • Constructor Details

    • DownloadProgressTracker

      public DownloadProgressTracker()
  • Method Details

    • onChainDownloadStarted

      public void onChainDownloadStarted(Peer peer, int blocksLeft)
      Description copied from interface: ChainDownloadStartedEventListener
      Called when a download is started with the initial number of blocks to be downloaded.
      Specified by:
      onChainDownloadStarted in interface ChainDownloadStartedEventListener
      Parameters:
      peer - the peer receiving the block
      blocksLeft - the number of blocks left to download
    • onBlocksDownloaded

      public void onBlocksDownloaded(Peer peer, Block block, @Nullable FilteredBlock filteredBlock, int blocksLeft)
      Description copied from interface: BlocksDownloadedEventListener

      Called on a Peer thread when a block is received.

      The block may be a Block object that contains transactions, a Block object that is only a header when fast catchup is being used. If set, filteredBlock can be used to retrieve the list of associated transactions.

      Specified by:
      onBlocksDownloaded in interface BlocksDownloadedEventListener
      Parameters:
      peer - the peer receiving the block
      block - the downloaded block
      filteredBlock - if non-null, the object that wraps the block header passed as the block param.
      blocksLeft - the number of blocks left to download
    • progress

      protected void progress(double pct, int blocksSoFar, Instant time)
      Called when download progress is made.
      Parameters:
      pct - the percentage of chain downloaded, estimated
      time - the time of the last block downloaded
    • startDownload

      protected void startDownload(int blocks)
      Called when download is initiated.
      Parameters:
      blocks - the number of blocks to download, estimated
    • doneDownload

      protected void doneDownload()
      Called when we are done downloading the block chain.
    • await

      public void await() throws InterruptedException
      Wait for the chain to be downloaded.
      Throws:
      InterruptedException
    • getFuture

      public ListenableCompletableFuture<Long> getFuture()
      Returns a listenable future that completes with the height of the best chain (as reported by the peer) once chain download seems to be finished.