com.google.bitcoin.core
Class StoredBlock

java.lang.Object
  extended by com.google.bitcoin.core.StoredBlock
All Implemented Interfaces:
Serializable

public class StoredBlock
extends Object
implements Serializable

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.

See Also:
Serialized Form

Constructor Summary
StoredBlock(Block header, BigInteger chainWork, int height)
           
 
Method Summary
 StoredBlock build(Block block)
          Creates a new StoredBlock, calculating the additional fields by adding to the values in this block.
 boolean equals(Object other)
           
 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.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StoredBlock

public StoredBlock(Block header,
                   BigInteger chainWork,
                   int height)
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 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(Object other)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class 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 doing store.get(this.getHeader().getPrevBlockHash()).

Returns:
the previous block in the chain or null if it was not found in the store.
Throws:
BlockStoreException

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2011. All Rights Reserved.