Extract constants to top of BuildCheckpoints. This makes it easier to switch between networks.

This commit is contained in:
Andreas Schildbach 2014-03-05 15:34:05 +01:00 committed by Mike Hearn
parent 71ee048d1e
commit 0ff1ce73a7

View File

@ -8,6 +8,7 @@ import com.google.bitcoin.utils.BriefLogFormatter;
import com.google.bitcoin.utils.Threading;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.InetAddress;
@ -24,18 +25,21 @@ import static com.google.common.base.Preconditions.checkState;
* to a file which is then signed with your key.
*/
public class BuildCheckpoints {
private static final NetworkParameters PARAMS = MainNetParams.get();
private static final File CHECKPOINTS_FILE = new File("checkpoints");
public static void main(String[] args) throws Exception {
BriefLogFormatter.init();
final NetworkParameters params = MainNetParams.get();
// Sorted map of UNIX time of block to StoredBlock object.
final TreeMap<Integer, StoredBlock> checkpoints = new TreeMap<Integer, StoredBlock>();
// Configure bitcoinj to fetch only headers, not save them to disk, connect to a local fully synced/validated
// node and to save block headers that are on interval boundaries, as long as they are <1 month old.
final BlockStore store = new MemoryBlockStore(params);
final BlockChain chain = new BlockChain(params, store);
final PeerGroup peerGroup = new PeerGroup(params, chain);
final BlockStore store = new MemoryBlockStore(PARAMS);
final BlockChain chain = new BlockChain(PARAMS, store);
final PeerGroup peerGroup = new PeerGroup(PARAMS, chain);
peerGroup.addAddress(InetAddress.getLocalHost());
long now = new Date().getTime() / 1000;
peerGroup.setFastCatchupTimeSecs(now);
@ -46,7 +50,7 @@ public class BuildCheckpoints {
@Override
public void notifyNewBestBlock(StoredBlock block) throws VerificationException {
int height = block.getHeight();
if (height % params.getInterval() == 0 && block.getHeader().getTimeSeconds() <= oneMonthAgo) {
if (height % PARAMS.getInterval() == 0 && block.getHeader().getTimeSeconds() <= oneMonthAgo) {
System.out.println(String.format("Checkpointing block %s at height %d",
block.getHeader().getHash(), block.getHeight()));
checkpoints.put(height, block);
@ -60,7 +64,7 @@ public class BuildCheckpoints {
checkState(checkpoints.size() > 0);
// Write checkpoint data out.
final FileOutputStream fileOutputStream = new FileOutputStream("checkpoints", false);
final FileOutputStream fileOutputStream = new FileOutputStream(CHECKPOINTS_FILE, false);
MessageDigest digest = MessageDigest.getInstance("SHA-256");
final DigestOutputStream digestOutputStream = new DigestOutputStream(fileOutputStream, digest);
digestOutputStream.on(false);
@ -85,10 +89,12 @@ public class BuildCheckpoints {
store.close();
// Sanity check the created file.
CheckpointManager manager = new CheckpointManager(params, new FileInputStream("checkpoints"));
CheckpointManager manager = new CheckpointManager(PARAMS, new FileInputStream(CHECKPOINTS_FILE));
checkState(manager.numCheckpoints() == checkpoints.size());
StoredBlock test = manager.getCheckpointBefore(1348310800); // Just after block 200,000
checkState(test.getHeight() == 199584);
checkState(test.getHeader().getHashAsString().equals("000000000000002e00a243fe9aa49c78f573091d17372c2ae0ae5e0f24f55b52"));
System.out.println("Checkpoints written to '" + CHECKPOINTS_FILE.getCanonicalPath() + "'.");
}
}