Package org.bitcoinj.core
Class StoredBlock
java.lang.Object
org.bitcoinj.core.StoredBlock
Wraps a
Block
object with extra data that can be derived from the block chain but is slow or inconvenient to
calculate. By storing it alongside the block header we reduce the amount of work required significantly.
Recalculation is slow because the fields are cumulative - to find the chainWork you have to iterate over every
block in the chain back to the genesis block, which involves lots of seeking/loading etc. So we just keep a
running total: it's a disk space vs cpu/io tradeoff.
StoredBlocks are put inside a BlockStore
which saves them to memory or disk.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Number of bytes serialized byserializeCompact(ByteBuffer)
static final int
Number of bytes serialized byserializeCompactV2(ByteBuffer)
-
Constructor Summary
ConstructorDescriptionStoredBlock
(Block header, BigInteger chainWork, int height) Create a StoredBlock from a (header-only)Block
, chain work value, and block height -
Method Summary
Modifier and TypeMethodDescriptionCreates a new StoredBlock, calculating the additional fields by adding to the values in this block.static StoredBlock
deserializeCompact
(ByteBuffer buffer) Deserializes the stored block from a custom packed format.static StoredBlock
deserializeCompact
(MessageSerializer serializer, ByteBuffer buffer) Deprecated.static StoredBlock
deserializeCompactV2
(ByteBuffer buffer) Deserializes the stored block from a custom packed format.boolean
The total sum of work done in this block, and all the blocks below it in the chain.The block header this object wraps.int
Position in the chain for this block.getPrev
(BlockStore store) Given a block store, looks up the previous block in this chain.int
hashCode()
boolean
moreWorkThan
(StoredBlock other) Returns true if this objects chainWork is higher than the others.void
serializeCompact
(ByteBuffer buffer) Serializes the stored block to a custom packed format.void
serializeCompactV2
(ByteBuffer buffer) Serializes the stored block to a custom packed format.toString()
-
Field Details
-
COMPACT_SERIALIZED_SIZE
public static final int COMPACT_SERIALIZED_SIZENumber of bytes serialized byserializeCompact(ByteBuffer)
- See Also:
-
COMPACT_SERIALIZED_SIZE_V2
public static final int COMPACT_SERIALIZED_SIZE_V2Number of bytes serialized byserializeCompactV2(ByteBuffer)
- See Also:
-
-
Constructor Details
-
StoredBlock
Create a StoredBlock from a (header-only)Block
, chain work value, and block height- Parameters:
header
- A Block object with only a header (no transactions should be included)chainWork
- Calculated chainWork for this blockheight
- block height for this block
-
-
Method Details
-
getHeader
The block header this object wraps. The referenced block object must not have any transactions in it. -
getChainWork
The total sum of work done in this block, and all the blocks below it in the chain. Work is a measure of how many tries are needed to solve a block. If the target is set to cover 10% of the total hash value space, then the work represented by a block is 10. -
getHeight
public int getHeight()Position in the chain for this block. The genesis block has a height of zero. -
moreWorkThan
Returns true if this objects chainWork is higher than the others. -
equals
-
hashCode
public int hashCode() -
build
Creates a new StoredBlock, calculating the additional fields by adding to the values in this block.- Throws:
VerificationException
-
getPrev
Given a block store, looks up the previous block in this chain. Convenience method for doingstore.get(this.getHeader().getPrevBlockHash())
.- Returns:
- the previous block in the chain or null if it was not found in the store.
- Throws:
BlockStoreException
-
serializeCompact
Serializes the stored block to a custom packed format. Used internally. As of June 22, 2024, it takes 12 unsigned bytes to store the chain work value, so developers should useserializeCompactV2(ByteBuffer)
.- Parameters:
buffer
- buffer to write to
-
serializeCompactV2
Serializes the stored block to a custom packed format. Used internally.- Parameters:
buffer
- buffer to write to
-
deserializeCompact
Deserializes the stored block from a custom packed format. Used internally. As of June 22, 2024, it takes 12 unsigned bytes to store the chain work value, so developers should usedeserializeCompactV2(ByteBuffer)
.- Parameters:
buffer
- data to deserialize- Returns:
- deserialized stored block
- Throws:
ProtocolException
-
deserializeCompactV2
Deserializes the stored block from a custom packed format. Used internally.- Parameters:
buffer
- data to deserialize- Returns:
- deserialized stored block
- Throws:
ProtocolException
-
deserializeCompact
@Deprecated public static StoredBlock deserializeCompact(MessageSerializer serializer, ByteBuffer buffer) throws ProtocolException Deprecated.- Throws:
ProtocolException
-
toString
-
deserializeCompact(ByteBuffer)