mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-10 09:20:04 +01:00
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:
parent
8aace05d3a
commit
369445d1e8
4 changed files with 19 additions and 8 deletions
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue