mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-01-31 07:12:17 +00:00
Add a convenience method to CheckpointManager and example of how to use in PingService.
This commit is contained in:
parent
7f17766b47
commit
96cd35f139
@ -16,12 +16,13 @@
|
||||
|
||||
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.LoggerFactory;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.DigestInputStream;
|
||||
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
|
||||
* 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());
|
||||
// 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) {
|
||||
try {
|
||||
Block genesis = params.genesisBlock.cloneAsHeader();
|
||||
@ -122,4 +123,21 @@ public class CheckpointManager {
|
||||
public Sha256Hash getDataHash() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ import com.google.common.util.concurrent.FutureCallback;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
|
||||
@ -81,11 +82,19 @@ public class PingService {
|
||||
}
|
||||
final Wallet wallet = w;
|
||||
// Fetch the first key in the wallet (should be the only key).
|
||||
ECKey key = wallet.keychain.get(0);
|
||||
// Load the block chain, if there is one stored locally.
|
||||
ECKey key = wallet.getKeys().iterator().next();
|
||||
// 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");
|
||||
File file = new File(filePrefix + ".spvchain");
|
||||
boolean chainExistedAlready = file.exists();
|
||||
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
|
||||
System.out.println("Connecting ...");
|
||||
chain = new BlockChain(params, wallet, blockStore);
|
||||
|
Loading…
Reference in New Issue
Block a user