StoredBlock: make method deserializeCompact() not use MessageSerializer

Its usecase is limited to fixed formats, currently binary checkpoints
and `.spv` block stores.
This commit is contained in:
Andreas Schildbach 2023-05-03 12:15:14 +02:00
parent 8aace05d3a
commit 369445d1e8
4 changed files with 19 additions and 8 deletions

View file

@ -145,7 +145,7 @@ public class CheckpointManager {
for (int i = 0; i < numCheckpoints; i++) {
if (dis.read(buffer.array(), 0, size) < size)
throw new IOException("Incomplete read whilst loading checkpoints.");
StoredBlock block = StoredBlock.deserializeCompact(params.getDefaultSerializer(), buffer);
StoredBlock block = StoredBlock.deserializeCompact(buffer);
((Buffer) buffer).position(0);
checkpoints.put(block.getHeader().time(), block);
}
@ -183,7 +183,7 @@ public class CheckpointManager {
((Buffer) buffer).position(0);
buffer.put(bytes);
((Buffer) buffer).position(0);
StoredBlock block = StoredBlock.deserializeCompact(params.getDefaultSerializer(), buffer);
StoredBlock block = StoredBlock.deserializeCompact(buffer);
checkpoints.put(block.getHeader().time(), block);
}
HashCode hash = hasher.hash();

View file

@ -19,6 +19,7 @@ package org.bitcoinj.core;
import org.bitcoinj.base.internal.ByteUtils;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.BlockStoreException;
import org.bitcoinj.store.SPVBlockStore;
import java.math.BigInteger;
import java.nio.ByteBuffer;
@ -140,15 +141,27 @@ public class StoredBlock {
buffer.put(bytes, 0, Block.HEADER_SIZE); // Trim the trailing 00 byte (zero transactions).
}
/** De-serializes the stored block from a custom packed format. Used by {@link CheckpointManager}. */
public static StoredBlock deserializeCompact(MessageSerializer serializer, ByteBuffer buffer) throws ProtocolException {
/**
* Deserializes the stored block from a custom packed format. Used by {@link CheckpointManager} and
* {@link SPVBlockStore}.
*
* @param buffer data to deserialize
* @return deserialized stored block
*/
public static StoredBlock deserializeCompact(ByteBuffer buffer) throws ProtocolException {
byte[] chainWorkBytes = new byte[StoredBlock.CHAIN_WORK_BYTES];
buffer.get(chainWorkBytes);
BigInteger chainWork = ByteUtils.bytesToBigInteger(chainWorkBytes);
int height = buffer.getInt(); // +4 bytes
byte[] header = new byte[Block.HEADER_SIZE + 1]; // Extra byte for the 00 transactions length.
buffer.get(header, 0, Block.HEADER_SIZE);
return new StoredBlock(serializer.makeBlock(ByteBuffer.wrap(header)), chainWork, height);
return new StoredBlock(Block.read(ByteBuffer.wrap(header)), chainWork, height);
}
/** @deprecated use {@link #deserializeCompact(ByteBuffer)} */
@Deprecated
public static StoredBlock deserializeCompact(MessageSerializer serializer, ByteBuffer buffer) throws ProtocolException {
return deserializeCompact(buffer);
}
@Override

View file

@ -246,7 +246,7 @@ public class SPVBlockStore implements BlockStore {
buffer.get(scratch);
if (Arrays.equals(scratch, targetHashBytes)) {
// Found the target.
StoredBlock storedBlock = StoredBlock.deserializeCompact(params.getDefaultSerializer(), buffer);
StoredBlock storedBlock = StoredBlock.deserializeCompact(buffer);
blockCache.put(hash, storedBlock);
return storedBlock;
}

View file

@ -56,8 +56,6 @@ public class CheckpointManagerTest {
@Test
public void canReadTextualStream() throws IOException {
expect(params.getId()).andReturn("org/bitcoinj/core/checkpointmanagertest/validTextualFormat");
expect(params.getSerializer()).andReturn(
new BitcoinSerializer(params, ProtocolVersion.CURRENT.intValue()));
replay(params);
new CheckpointManager(params, null);
}