Skip to content

Commit

Permalink
SPVBlockStore: rewind buffer directly before accesses to magic header
Browse files Browse the repository at this point in the history
This is meant to make sure the buffer position is always right.
  • Loading branch information
schildbach committed Jul 14, 2024
1 parent 1582c02 commit 24ace02
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/java/org/bitcoinj/store/SPVBlockStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ else if (fileLength < randomAccessFile.length())
// Check or initialize the header bytes to ensure we don't try to open some random file.
if (exists) {
byte[] currentHeader = new byte[4];
((Buffer) buffer).rewind();
buffer.get(currentHeader);
if (!Arrays.equals(currentHeader, MAGIC_HEADER))
throw new BlockStoreException("Magic header expected, got: " + ByteUtils.formatHex(currentHeader));
Expand All @@ -179,6 +180,7 @@ else if (fileLength < randomAccessFile.length())
}

private void initNewStore(Block genesisBlock) throws Exception {
((Buffer) buffer).rewind();
buffer.put(MAGIC_HEADER);
// Insert the genesis block.
lock.lock();
Expand Down Expand Up @@ -353,7 +355,6 @@ public void clear() throws Exception {
buffer.put((byte)0);
}
// Initialize store again
((Buffer) buffer).position(0);
initNewStore(params.getGenesisBlock());
} finally { lock.unlock(); }
}
Expand Down

0 comments on commit 24ace02

Please sign in to comment.