com.google.bitcoin.core
Class BlockChain

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

public class BlockChain
extends AbstractBlockChain

A BlockChain implements the simplified payment verification mode of the Bitcoin protocol. It is the right choice to use for programs that have limited resources as it won't verify transactions signatures or attempt to store all of the block chain. Really, this class should be called SPVBlockChain but for backwards compatibility it is not.


Nested Class Summary
 
Nested classes/interfaces inherited from class com.google.bitcoin.core.AbstractBlockChain
AbstractBlockChain.NewBlockType
 
Field Summary
protected  BlockStore blockStore
          Keeps a map of block hashes to StoredBlocks.
 
Fields inherited from class com.google.bitcoin.core.AbstractBlockChain
chainHead, FP_ESTIMATOR_ALPHA, FP_ESTIMATOR_BETA, lock, params
 
Constructor Summary
BlockChain(NetworkParameters params, BlockStore blockStore)
          Constructs a BlockChain that has no wallet at all.
BlockChain(NetworkParameters params, List<BlockChainListener> wallets, BlockStore blockStore)
          Constructs a BlockChain connected to the given list of listeners and a store.
BlockChain(NetworkParameters params, Wallet wallet, BlockStore blockStore)
          Constructs a BlockChain connected to the given wallet and store.
 
Method Summary
 boolean add(FilteredBlock block)
          Processes a received block and tries to add it to the chain.
protected  StoredBlock addToBlockStore(StoredBlock storedPrev, Block blockHeader)
          Adds/updates the given Block with the block store.
protected  StoredBlock addToBlockStore(StoredBlock storedPrev, Block blockHeader, TransactionOutputChanges txOutChanges)
          Adds/updates the given StoredBlock with the block store.
protected  TransactionOutputChanges connectTransactions(int height, Block block)
          Connect each transaction in block.transactions, verifying them as we go and removing spent outputs If an error is encountered in a transaction, no changes should be made to the underlying BlockStore.
protected  TransactionOutputChanges connectTransactions(StoredBlock newBlock)
          Load newBlock from BlockStore and connect its transactions, returning changes to the set of unspent transactions.
protected  void disconnectTransactions(StoredBlock block)
          Disconnect each transaction in the block (after reading it from the block store) Only called if(shouldVerifyTransactions())
protected  void doSetChainHead(StoredBlock chainHead)
          Called before setting chain head in memory.
protected  StoredBlock getStoredBlockInCurrentScope(Sha256Hash hash)
          For a standard BlockChain, this should return blockStore.get(hash), for a FullPrunedBlockChain blockStore.getOnceUndoableStoredBlock(hash)
protected  void notSettingChainHead()
          Called if we (possibly) previously called disconnectTransaction/connectTransactions, but will not be calling preSetChainHead as a block failed verification.
protected  boolean shouldVerifyTransactions()
          Whether or not we are maintaining a set of unspent outputs and are verifying all transactions.
 
Methods inherited from class com.google.bitcoin.core.AbstractBlockChain
add, addListener, addListener, addWallet, estimateBlockTime, getBestChainHeight, getBlockStore, getChainHead, getFalsePositiveRate, getHeightFuture, getOrphanRoot, isOrphan, removeListener, removeWallet, resetFalsePositiveEstimate, setChainHead, trackFilteredTransactions
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

blockStore

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

Constructor Detail

BlockChain

public BlockChain(NetworkParameters params,
                  Wallet wallet,
                  BlockStore blockStore)
           throws BlockStoreException

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 should use SPVBlockStore or you could also try a MemoryBlockStore if you want to hold all headers in RAM and don't care about disk serialization (this is rare).

Throws:
BlockStoreException

BlockChain

public BlockChain(NetworkParameters params,
                  BlockStore blockStore)
           throws BlockStoreException
Constructs a BlockChain that has no wallet at all. This is helpful when you don't actually care about sending and receiving coins but rather, just want to explore the network data structures.

Throws:
BlockStoreException

BlockChain

public BlockChain(NetworkParameters params,
                  List<BlockChainListener> wallets,
                  BlockStore blockStore)
           throws BlockStoreException
Constructs a BlockChain connected to the given list of listeners and a store.

Throws:
BlockStoreException
Method Detail

addToBlockStore

protected StoredBlock addToBlockStore(StoredBlock storedPrev,
                                      Block blockHeader,
                                      TransactionOutputChanges txOutChanges)
                               throws BlockStoreException,
                                      VerificationException
Description copied from class: AbstractBlockChain
Adds/updates the given StoredBlock with the block store. This version is used when the transactions have already been verified to properly spend txOutputChanges.

Specified by:
addToBlockStore in class AbstractBlockChain
Parameters:
storedPrev - The StoredBlock which immediately precedes block.
blockHeader - The StoredBlock to add/update.
txOutChanges - The total sum of all changes made by this block to the set of open transaction outputs (from a call to connectTransactions), if in fully verifying mode (null otherwise).
Returns:
the newly created StoredBlock
Throws:
BlockStoreException
VerificationException

addToBlockStore

protected StoredBlock addToBlockStore(StoredBlock storedPrev,
                                      Block blockHeader)
                               throws BlockStoreException,
                                      VerificationException
Description copied from class: AbstractBlockChain
Adds/updates the given Block with the block store. This version is used when the transactions have not been verified.

Specified by:
addToBlockStore in class AbstractBlockChain
Parameters:
storedPrev - The StoredBlock which immediately precedes block.
blockHeader - The Block to add/update.
Returns:
the newly created StoredBlock
Throws:
BlockStoreException
VerificationException

shouldVerifyTransactions

protected boolean shouldVerifyTransactions()
Description copied from class: AbstractBlockChain
Whether or not we are maintaining a set of unspent outputs and are verifying all transactions. Also indicates that all calls to add() should provide a block containing transactions

Specified by:
shouldVerifyTransactions in class AbstractBlockChain

connectTransactions

protected TransactionOutputChanges connectTransactions(int height,
                                                       Block block)
Description copied from class: AbstractBlockChain
Connect each transaction in block.transactions, verifying them as we go and removing spent outputs If an error is encountered in a transaction, no changes should be made to the underlying BlockStore. and a VerificationException should be thrown. Only called if(shouldVerifyTransactions())

Specified by:
connectTransactions in class AbstractBlockChain
Returns:
The full set of all changes made to the set of open transaction outputs.

connectTransactions

protected TransactionOutputChanges connectTransactions(StoredBlock newBlock)
Description copied from class: AbstractBlockChain
Load newBlock from BlockStore and connect its transactions, returning changes to the set of unspent transactions. If an error is encountered in a transaction, no changes should be made to the underlying BlockStore. Only called if(shouldVerifyTransactions())

Specified by:
connectTransactions in class AbstractBlockChain
Returns:
The full set of all changes made to the set of open transaction outputs.

disconnectTransactions

protected void disconnectTransactions(StoredBlock block)
Description copied from class: AbstractBlockChain
Disconnect each transaction in the block (after reading it from the block store) Only called if(shouldVerifyTransactions())

Specified by:
disconnectTransactions in class AbstractBlockChain

doSetChainHead

protected void doSetChainHead(StoredBlock chainHead)
                       throws BlockStoreException
Description copied from class: AbstractBlockChain
Called before setting chain head in memory. Should write the new head to block store and then commit any database transactions that were started by disconnectTransactions/connectTransactions.

Specified by:
doSetChainHead in class AbstractBlockChain
Throws:
BlockStoreException

notSettingChainHead

protected void notSettingChainHead()
                            throws BlockStoreException
Description copied from class: AbstractBlockChain
Called if we (possibly) previously called disconnectTransaction/connectTransactions, but will not be calling preSetChainHead as a block failed verification. Can be used to abort database transactions that were started by disconnectTransactions/connectTransactions.

Specified by:
notSettingChainHead in class AbstractBlockChain
Throws:
BlockStoreException

getStoredBlockInCurrentScope

protected StoredBlock getStoredBlockInCurrentScope(Sha256Hash hash)
                                            throws BlockStoreException
Description copied from class: AbstractBlockChain
For a standard BlockChain, this should return blockStore.get(hash), for a FullPrunedBlockChain blockStore.getOnceUndoableStoredBlock(hash)

Specified by:
getStoredBlockInCurrentScope in class AbstractBlockChain
Throws:
BlockStoreException

add

public boolean add(FilteredBlock block)
            throws VerificationException,
                   PrunedException
Description copied from class: AbstractBlockChain
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.

Overrides:
add in class AbstractBlockChain
Throws:
VerificationException
PrunedException


Copyright © 2014. All rights reserved.