public class StoredBlock
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
static int |
COMPACT_SERIALIZED_SIZE
Number of bytes serialized by
serializeCompact(ByteBuffer) |
static int |
COMPACT_SERIALIZED_SIZE_V2
Number of bytes serialized by
serializeCompactV2(ByteBuffer) |
Constructor and Description |
---|
StoredBlock(Block header,
java.math.BigInteger chainWork,
int height)
Create a StoredBlock from a (header-only)
Block , chain work value, and block height |
Modifier and Type | Method and Description |
---|---|
StoredBlock |
build(Block block)
Creates a new StoredBlock, calculating the additional fields by adding to the values in this block.
|
static StoredBlock |
deserializeCompact(NetworkParameters params,
java.nio.ByteBuffer buffer)
Deserializes the stored block from a custom packed format.
|
static StoredBlock |
deserializeCompactV2(NetworkParameters params,
java.nio.ByteBuffer buffer)
Deserializes the stored block from a custom packed format.
|
boolean |
equals(java.lang.Object o) |
java.math.BigInteger |
getChainWork()
The total sum of work done in this block, and all the blocks below it in the chain.
|
Block |
getHeader()
The block header this object wraps.
|
int |
getHeight()
Position in the chain for this block.
|
StoredBlock |
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(java.nio.ByteBuffer buffer)
Serializes the stored block to a custom packed format.
|
void |
serializeCompactV2(java.nio.ByteBuffer buffer)
Serializes the stored block to a custom packed format.
|
java.lang.String |
toString() |
public static final int COMPACT_SERIALIZED_SIZE
serializeCompact(ByteBuffer)
public static final int COMPACT_SERIALIZED_SIZE_V2
serializeCompactV2(ByteBuffer)
public StoredBlock(Block header, java.math.BigInteger chainWork, int height)
Block
, chain work value, and block heightheader
- A Block object with only a header (no transactions should be included)chainWork
- Calculated chainWork for this blockheight
- block height for this blockpublic Block getHeader()
public java.math.BigInteger getChainWork()
public int getHeight()
public boolean moreWorkThan(StoredBlock other)
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public StoredBlock build(Block block) throws VerificationException
VerificationException
public StoredBlock getPrev(BlockStore store) throws BlockStoreException
store.get(this.getHeader().getPrevBlockHash())
.BlockStoreException
public void serializeCompact(java.nio.ByteBuffer buffer)
serializeCompactV2(ByteBuffer)
.buffer
- buffer to write topublic void serializeCompactV2(java.nio.ByteBuffer buffer)
buffer
- buffer to write topublic static StoredBlock deserializeCompact(NetworkParameters params, java.nio.ByteBuffer buffer) throws ProtocolException
deserializeCompactV2(NetworkParameters, ByteBuffer)
.buffer
- data to deserializeProtocolException
public static StoredBlock deserializeCompactV2(NetworkParameters params, java.nio.ByteBuffer buffer) throws ProtocolException
buffer
- data to deserializeProtocolException
public java.lang.String toString()
toString
in class java.lang.Object