com.google.bitcoin.core
Class BlockChain

java.lang.Object
  extended by com.google.bitcoin.core.BlockChain

public class BlockChain
extends Object

A BlockChain holds a series of Block objects, links them together, and knows how to verify that the chain follows the rules of the NetworkParameters for this chain.

A BlockChain requires a Wallet to receive transactions that it finds during the initial download. However, if you don't care about this, you can just pass in an empty wallet and nothing bad will happen.

A newly constructed BlockChain is empty. To fill it up, use a Peer object to download the chain from the network.

Notes

The 'chain' can actually be a tree although in normal operation it can be thought of as a simple list. In such a situation there are multiple stories of the economy competing to become the one true consensus. This can happen naturally when two miners solve a block within a few seconds of each other, or it can happen when the chain is under attack.

A reference to the head block of every chain is stored. If you can reach the genesis block by repeatedly walking through the prevBlock pointers, then we say this is a full chain. If you cannot reach the genesis block we say it is an orphan chain.

Orphan chains can occur when blocks are solved and received during the initial block chain download, or if we connect to a peer that doesn't send us blocks in order.


Field Summary
protected  BlockStore blockStore
          Keeps a map of block hashes to StoredBlocks.
protected  StoredBlock chainHead
          Tracks the top of the best known chain.
protected  NetworkParameters params
           
protected  Wallet wallet
           
 
Constructor Summary
BlockChain(NetworkParameters params, Wallet wallet, BlockStore blockStore)
          Constructs a BlockChain connected to the given wallet and store.
 
Method Summary
 boolean add(Block block)
          Processes a received block and tries to add it to the chain.
 StoredBlock getChainHead()
          Returns the block at the head of the current best chain.
 Block getUnconnectedBlock()
          Returns the most recent unconnected block or null if there are none.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

blockStore

protected BlockStore blockStore
Keeps a map of block hashes to StoredBlocks.


chainHead

protected StoredBlock chainHead
Tracks the top of the best known chain.

Following this one down to the genesis block produces the story of the economy from the creation of BitCoin until the present day. The chain head can change if a new set of blocks is received that results in a chain of greater work than the one obtained by following this one down. In that case a reorganize is triggered, potentially invalidating transactions in our wallet.


params

protected final NetworkParameters params

wallet

protected final Wallet wallet
Constructor Detail

BlockChain

public BlockChain(NetworkParameters params,
                  Wallet wallet,
                  BlockStore blockStore)
Constructs a BlockChain connected to the given wallet and store. To obtain a Wallet you can construct one from scratch, or you can deserialize a saved wallet from disk using Wallet.loadFromFile(java.io.File)

For the store you can use a MemoryBlockStore if you don't care about saving the downloaded data, or a BoundedOverheadBlockStore if you'd like to ensure fast startup the next time you run the program.

Method Detail

add

public boolean add(Block block)
            throws VerificationException,
                   ScriptException
Processes a received block and tries to add it to the chain. If there's something wrong with the block an exception is thrown. If the block is OK but cannot be connected to the chain at this time, returns false. If the block can be connected to the chain, returns true.

Throws:
VerificationException
ScriptException

getChainHead

public StoredBlock getChainHead()
Returns the block at the head of the current best chain. This is the block which represents the greatest amount of cumulative work done.


getUnconnectedBlock

public Block getUnconnectedBlock()
Returns the most recent unconnected block or null if there are none. This will all have to change.



Copyright © 2011. All Rights Reserved.