CheckpointManager: expose a static method to get the checkpoint stream

This commit is contained in:
Mike Hearn 2015-07-09 00:36:18 +02:00
parent 95bfa40630
commit 49ac4ac2ad
2 changed files with 19 additions and 6 deletions

View file

@ -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 {

View file

@ -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;