Package org.bitcoinj.core
Class StoredBlock
- java.lang.Object
-
- org.bitcoinj.core.StoredBlock
-
public class StoredBlock extends java.lang.ObjectWraps aBlockobject 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
BlockStorewhich saves them to memory or disk.
-
-
Field Summary
Fields Modifier and Type Field Description static intCOMPACT_SERIALIZED_SIZE
-
Constructor Summary
Constructors Constructor Description StoredBlock(Block header, java.math.BigInteger chainWork, int height)Create a StoredBlock from a (header-only)Block, chain work value, and block height
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StoredBlockbuild(Block block)Creates a new StoredBlock, calculating the additional fields by adding to the values in this block.static StoredBlockdeserializeCompact(NetworkParameters params, java.nio.ByteBuffer buffer)De-serializes the stored block from a custom packed format.booleanequals(java.lang.Object o)java.math.BigIntegergetChainWork()The total sum of work done in this block, and all the blocks below it in the chain.BlockgetHeader()The block header this object wraps.intgetHeight()Position in the chain for this block.StoredBlockgetPrev(BlockStore store)Given a block store, looks up the previous block in this chain.inthashCode()booleanmoreWorkThan(StoredBlock other)Returns true if this objects chainWork is higher than the others.voidserializeCompact(java.nio.ByteBuffer buffer)Serializes the stored block to a custom packed format.java.lang.StringtoString()
-
-
-
Field Detail
-
COMPACT_SERIALIZED_SIZE
public static final int COMPACT_SERIALIZED_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
StoredBlock
public StoredBlock(Block header, java.math.BigInteger chainWork, int height)
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 Detail
-
getHeader
public Block getHeader()
The block header this object wraps. The referenced block object must not have any transactions in it.
-
getChainWork
public java.math.BigInteger 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
public boolean moreWorkThan(StoredBlock other)
Returns true if this objects chainWork is higher than the others.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
build
public StoredBlock build(Block block) throws VerificationException
Creates a new StoredBlock, calculating the additional fields by adding to the values in this block.- Throws:
VerificationException
-
getPrev
public StoredBlock getPrev(BlockStore store) throws BlockStoreException
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
public void serializeCompact(java.nio.ByteBuffer buffer)
Serializes the stored block to a custom packed format. Used byCheckpointManager.
-
deserializeCompact
public static StoredBlock deserializeCompact(NetworkParameters params, java.nio.ByteBuffer buffer) throws ProtocolException
De-serializes the stored block from a custom packed format. Used byCheckpointManager.- Throws:
ProtocolException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-