From 49ac4ac2adb77cc00e5e8eeb80d599ed258941a1 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Thu, 9 Jul 2015 00:36:18 +0200 Subject: [PATCH] CheckpointManager: expose a static method to get the checkpoint stream --- .../org/bitcoinj/core/CheckpointManager.java | 16 +++++++++++++++- .../java/org/bitcoinj/kits/WalletAppKit.java | 9 ++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/CheckpointManager.java b/core/src/main/java/org/bitcoinj/core/CheckpointManager.java index f099045e9..30d011f93 100644 --- a/core/src/main/java/org/bitcoinj/core/CheckpointManager.java +++ b/core/src/main/java/org/bitcoinj/core/CheckpointManager.java @@ -28,6 +28,7 @@ import com.google.common.io.BaseEncoding; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.*; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataInputStream; @@ -82,8 +83,16 @@ public class CheckpointManager { public static final BaseEncoding BASE64 = BaseEncoding.base64().omitPadding(); - public CheckpointManager(NetworkParameters params, InputStream inputStream) throws IOException { + /** Loads the default checkpoints bundled with bitcoinj */ + public CheckpointManager(Context context) throws IOException { + this(context.getParams(), null); + } + + /** Loads the checkpoints from the given stream */ + public CheckpointManager(NetworkParameters params, @Nullable InputStream inputStream) throws IOException { this.params = checkNotNull(params); + if (inputStream == null) + inputStream = openStream(params); checkNotNull(inputStream); inputStream = new BufferedInputStream(inputStream); inputStream.mark(1); @@ -97,6 +106,11 @@ public class CheckpointManager { throw new IOException("Unsupported format."); } + /** Returns a checkpoints stream pointing to inside the bitcoinj JAR */ + public static InputStream openStream(NetworkParameters params) { + return CheckpointManager.class.getResourceAsStream("/" + params.getId() + ".checkpoints"); + } + private Sha256Hash readBinary(InputStream inputStream) throws IOException { DataInputStream dis = null; try { diff --git a/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java b/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java index 3b89413eb..7fb67032a 100644 --- a/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java +++ b/core/src/main/java/org/bitcoinj/kits/WalletAppKit.java @@ -101,11 +101,6 @@ public class WalletAppKit extends AbstractIdleService { this.params = checkNotNull(context.getParams()); this.directory = checkNotNull(directory); this.filePrefix = checkNotNull(filePrefix); - if (!Utils.isAndroidRuntime()) { - InputStream stream = WalletAppKit.class.getResourceAsStream("/" + params.getId() + ".checkpoints"); - if (stream != null) - setCheckpoints(stream); - } } /** Will only connect to the given addresses. Cannot be called after startup. */ @@ -280,6 +275,10 @@ public class WalletAppKit extends AbstractIdleService { // Initiate Bitcoin network objects (block store, blockchain and peer group) vStore = provideBlockStore(chainFile); if (!chainFileExists || restoreFromSeed != null) { + if (checkpoints == null && !Utils.isAndroidRuntime()) { + checkpoints = CheckpointManager.openStream(params); + } + if (checkpoints != null) { // Initialize the chain file with a checkpoint to speed up first-run sync. long time;