Class CheckpointManager
- java.lang.Object
-
- org.bitcoinj.core.CheckpointManager
-
public class CheckpointManager extends java.lang.Object
Vends hard-coded
StoredBlock
s for blocks throughout the chain. Checkpoints serve two purposes:- They act as a safety mechanism against huge re-orgs that could rewrite large chunks of history, thus constraining the block chain to be a consensus mechanism only for recent parts of the timeline.
- They allow synchronization to the head of the chain for new wallets/users much faster than syncing all headers from the genesis block.
Checkpoints are used by the SPV
BlockChain
to initialize freshSPVBlockStore
s. They are not used by fully validating mode, which instead has a different concept of checkpoints that are used to hard-code the validity of blocks that violate BIP30 (duplicate coinbase transactions). Those "checkpoints" can be found in NetworkParameters.Checkpoints are read from a text file, one value per line. It consists of the magic string "TXT CHECKPOINTS 1", followed by the number of signatures to read. The value may not be larger than 256. If the number of signatures is larger than zero, each 65 byte ECDSA secp256k1 signature then follows. The signatures sign the hash of all bytes that follow the last signature.
After the signatures come the number of checkpoints in the file. Then each checkpoint follows one per line in compact format (as written by
StoredBlock.serializeCompactV2(ByteBuffer)
) as a base64-encoded blob.
-
-
Field Summary
Fields Modifier and Type Field Description static com.google.common.io.BaseEncoding
BASE64
protected java.util.TreeMap<java.time.Instant,StoredBlock>
checkpoints
protected Sha256Hash
dataHash
protected NetworkParameters
params
-
Constructor Summary
Constructors Constructor Description CheckpointManager(NetworkParameters params)
Loads the default checkpoints bundled with bitcoinjCheckpointManager(NetworkParameters params, java.io.InputStream inputStream)
Loads the checkpoints from the given stream
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
checkpoint(NetworkParameters params, java.io.InputStream checkpoints, BlockStore store, long timeSecs)
Deprecated.static void
checkpoint(NetworkParameters params, java.io.InputStream checkpoints, BlockStore store, java.time.Instant time)
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.StoredBlock
getCheckpointBefore(long timeSecs)
Deprecated.StoredBlock
getCheckpointBefore(java.time.Instant time)
Returns aStoredBlock
representing the last checkpoint before the given time, for example, normally you would want to know the checkpoint before the earliest wallet birthday.Sha256Hash
getDataHash()
Returns a hash of the concatenated checkpoint data.int
numCheckpoints()
Returns the number of checkpoints that were loaded.static java.io.InputStream
openStream(NetworkParameters params)
Returns a checkpoints stream pointing to inside the bitcoinj JAR
-
-
-
Field Detail
-
checkpoints
protected final java.util.TreeMap<java.time.Instant,StoredBlock> checkpoints
-
params
protected final NetworkParameters params
-
dataHash
protected final Sha256Hash dataHash
-
BASE64
public static final com.google.common.io.BaseEncoding BASE64
-
-
Constructor Detail
-
CheckpointManager
public CheckpointManager(NetworkParameters params) throws java.io.IOException
Loads the default checkpoints bundled with bitcoinj- Throws:
java.io.IOException
-
CheckpointManager
public CheckpointManager(NetworkParameters params, @Nullable java.io.InputStream inputStream) throws java.io.IOException
Loads the checkpoints from the given stream- Throws:
java.io.IOException
-
-
Method Detail
-
openStream
public static java.io.InputStream openStream(NetworkParameters params)
Returns a checkpoints stream pointing to inside the bitcoinj JAR
-
getCheckpointBefore
public StoredBlock getCheckpointBefore(java.time.Instant time)
Returns aStoredBlock
representing the last checkpoint before the given time, for example, normally you would want to know the checkpoint before the earliest wallet birthday.
-
getCheckpointBefore
@Deprecated public StoredBlock getCheckpointBefore(long timeSecs)
Deprecated.
-
numCheckpoints
public int numCheckpoints()
Returns the number of checkpoints that were loaded.
-
getDataHash
public Sha256Hash getDataHash()
Returns a hash of the concatenated checkpoint data.
-
checkpoint
public static void checkpoint(NetworkParameters params, java.io.InputStream checkpoints, BlockStore store, java.time.Instant time) throws java.io.IOException, BlockStoreException
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.
Note that time is adjusted backwards by a week to account for possible clock drift in the block headers.
- Throws:
java.io.IOException
BlockStoreException
-
checkpoint
@Deprecated public static void checkpoint(NetworkParameters params, java.io.InputStream checkpoints, BlockStore store, long timeSecs) throws java.io.IOException, BlockStoreException
Deprecated.- Throws:
java.io.IOException
BlockStoreException
-
-