Skip to content

Commit

Permalink
CheckpointManager, BuildCheckpoints: support checkpoints with 32 byte…
Browse files Browse the repository at this point in the history
… chain work

* `BuildCheckpoints` now always creates checkpoints with 32 byte chain work
* `CheckpointManager` knows how to read both 12 and 32 byte chain work

Also updates the bundled checkpoints to the new format.
  • Loading branch information
schildbach committed Jun 27, 2024
1 parent db329be commit 2332992
Show file tree
Hide file tree
Showing 5 changed files with 1,930 additions and 1,928 deletions.
16 changes: 9 additions & 7 deletions core/src/main/java/org/bitcoinj/core/CheckpointManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
* sign the hash of all bytes that follow the last signature.</p>
*
* <p>After the signatures come the number of checkpoints in the file. Then each checkpoint follows one per line in
* compact format (as written by {@link StoredBlock#serializeCompact(ByteBuffer)}) as a base64-encoded blob.</p>
* compact format (as written by {@link StoredBlock#serializeCompactV2(ByteBuffer)}) as a base64-encoded blob.</p>
*/
public class CheckpointManager {
private static final Logger log = LoggerFactory.getLogger(CheckpointManager.class);
Expand Down Expand Up @@ -129,15 +129,17 @@ private Sha256Hash readTextual(InputStream inputStream) throws IOException {
checkState(numCheckpoints > 0);
// Hash numCheckpoints in a way compatible to the binary format.
hasher.putBytes(ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(numCheckpoints).array());
final int size = StoredBlock.COMPACT_SERIALIZED_SIZE;
ByteBuffer buffer = ByteBuffer.allocate(size);
for (int i = 0; i < numCheckpoints; i++) {
byte[] bytes = BASE64.decode(reader.readLine());
hasher.putBytes(bytes);
((Buffer) buffer).position(0);
buffer.put(bytes);
((Buffer) buffer).position(0);
StoredBlock block = StoredBlock.deserializeCompact(buffer);
ByteBuffer buffer = ByteBuffer.wrap(bytes);
StoredBlock block;
if (bytes.length == StoredBlock.COMPACT_SERIALIZED_SIZE)
block = StoredBlock.deserializeCompact(buffer);
else if (bytes.length == StoredBlock.COMPACT_SERIALIZED_SIZE_V2)
block = StoredBlock.deserializeCompactV2(buffer);
else
throw new IllegalStateException("unexpected length of checkpoint: " + bytes.length);
checkpoints.put(block.getHeader().time(), block);
}
HashCode hash = hasher.hash();
Expand Down
Loading

0 comments on commit 2332992

Please sign in to comment.