3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 15:22:16 +00:00

Add a convenience method to CheckpointManager and example of how to use in PingService.

This commit is contained in:
Mike Hearn 2013-03-04 17:42:19 +01:00 committed by Mike Hearn
parent 7f17766b47
commit 96cd35f139
2 changed files with 34 additions and 7 deletions

View File

@ -16,12 +16,13 @@
package com.google.bitcoin.core; package com.google.bitcoin.core;
import com.google.bitcoin.store.BlockStore;
import com.google.bitcoin.store.BlockStoreException;
import com.google.bitcoin.store.FullPrunedBlockStore;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.DataInputStream; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.security.DigestInputStream; import java.security.DigestInputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
@ -98,10 +99,10 @@ public class CheckpointManager {
* Returns a {@link StoredBlock} representing the last checkpoint before the given time, for example, normally * Returns a {@link StoredBlock} representing the last checkpoint before the given time, for example, normally
* you would want to know the checkpoint before the earliest wallet birthday. * you would want to know the checkpoint before the earliest wallet birthday.
*/ */
public StoredBlock getCheckpointBefore(int time) { public StoredBlock getCheckpointBefore(long time) {
checkArgument(time > params.genesisBlock.getTimeSeconds()); checkArgument(time > params.genesisBlock.getTimeSeconds());
// This is thread safe because the map never changes after creation. // This is thread safe because the map never changes after creation.
Map.Entry<Long, StoredBlock> entry = checkpoints.floorEntry((long) time); Map.Entry<Long, StoredBlock> entry = checkpoints.floorEntry(time);
if (entry == null) { if (entry == null) {
try { try {
Block genesis = params.genesisBlock.cloneAsHeader(); Block genesis = params.genesisBlock.cloneAsHeader();
@ -122,4 +123,21 @@ public class CheckpointManager {
public Sha256Hash getDataHash() { public Sha256Hash getDataHash() {
return dataHash; return dataHash;
} }
/**
* Convenience method that creates a CheckpointManager, loads the given data, gets the checkpoint for the given
* time, then inserts it into the store and sets that to be the chain head. Useful when you have just created
* a new store from scratch and want to use configure it all in one go.
*/
public static void checkpoint(NetworkParameters params, InputStream checkpoints, BlockStore store, long time)
throws IOException, BlockStoreException {
checkNotNull(params);
checkNotNull(store);
checkArgument(!(store instanceof FullPrunedBlockStore), "You cannot use checkpointing with a full store.");
BufferedInputStream stream = new BufferedInputStream(checkpoints);
CheckpointManager manager = new CheckpointManager(params, stream);
StoredBlock checkpoint = manager.getCheckpointBefore(time);
store.put(checkpoint);
store.setChainHead(checkpoint);
}
} }

View File

@ -26,6 +26,7 @@ import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
@ -81,11 +82,19 @@ public class PingService {
} }
final Wallet wallet = w; final Wallet wallet = w;
// Fetch the first key in the wallet (should be the only key). // Fetch the first key in the wallet (should be the only key).
ECKey key = wallet.keychain.get(0); ECKey key = wallet.getKeys().iterator().next();
// Load the block chain, if there is one stored locally. // Load the block chain, if there is one stored locally. If it's going to be freshly created, checkpoint it.
System.out.println("Reading block store from disk"); System.out.println("Reading block store from disk");
File file = new File(filePrefix + ".spvchain"); File file = new File(filePrefix + ".spvchain");
boolean chainExistedAlready = file.exists();
blockStore = new SPVBlockStore(params, file); blockStore = new SPVBlockStore(params, file);
if (!chainExistedAlready) {
File checkpointsFile = new File("checkpoints");
if (checkpointsFile.exists()) {
FileInputStream stream = new FileInputStream(checkpointsFile);
CheckpointManager.checkpoint(params, stream, blockStore, key.getCreationTimeSeconds());
}
}
// Connect to the localhost node. One minute timeout since we won't try any other peers // Connect to the localhost node. One minute timeout since we won't try any other peers
System.out.println("Connecting ..."); System.out.println("Connecting ...");
chain = new BlockChain(params, wallet, blockStore); chain = new BlockChain(params, wallet, blockStore);