Package org.bitcoinj.core
Class BlockChain
java.lang.Object
org.bitcoinj.core.AbstractBlockChain
org.bitcoinj.core.BlockChain
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 org.bitcoinj.core.AbstractBlockChain
AbstractBlockChain.NewBlockType -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final BlockStoreKeeps a map of block hashes to StoredBlocks.Fields inherited from class org.bitcoinj.core.AbstractBlockChain
chainHead, FP_ESTIMATOR_ALPHA, FP_ESTIMATOR_BETA, lock, params -
Constructor Summary
ConstructorsConstructorDescriptionBlockChain(Context params, List<? extends Wallet> wallets, BlockStore blockStore) Constructs a BlockChain connected to the given list of listeners and a store.BlockChain(Context context, BlockStore blockStore) Constructs a BlockChain that has no wallet at all.BlockChain(Context context, Wallet wallet, BlockStore blockStore) Constructs a BlockChain connected to the given wallet and store.BlockChain(NetworkParameters params, List<? extends Wallet> wallets, BlockStore blockStore) BlockChain(NetworkParameters params, BlockStore blockStore) BlockChain(NetworkParameters params, Wallet wallet, BlockStore blockStore) -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(FilteredBlock block) Processes a received block and tries to add it to the chain.protected StoredBlockaddToBlockStore(StoredBlock storedPrev, Block blockHeader) Adds/updates the givenBlockwith the block store.protected StoredBlockaddToBlockStore(StoredBlock storedPrev, Block blockHeader, TransactionOutputChanges txOutChanges) Adds/updates the givenStoredBlockwith the block store.protected TransactionOutputChangesconnectTransactions(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 TransactionOutputChangesconnectTransactions(StoredBlock newBlock) Load newBlock from BlockStore and connect its transactions, returning changes to the set of unspent transactions.protected voidDisconnect each transaction in the block (after reading it from the block store) Only called if(shouldVerifyTransactions())protected voiddoSetChainHead(StoredBlock chainHead) Called before setting chain head in memory.protected StoredBlockFor a standard BlockChain, this should return blockStore.get(hash), for a FullPrunedBlockChain blockStore.getOnceUndoableStoredBlock(hash)protected voidCalled if we (possibly) previously called disconnectTransaction/connectTransactions, but will not be calling preSetChainHead as a block failed verification.protected voidrollbackBlockStore(int height) Rollback the block store to a given height.protected booleanWhether or not we are maintaining a set of unspent outputs and are verifying all transactions.Methods inherited from class org.bitcoinj.core.AbstractBlockChain
add, addNewBestBlockListener, addNewBestBlockListener, addReorganizeListener, addReorganizeListener, addTransactionReceivedListener, addTransactionReceivedListener, addWallet, drainOrphanBlocks, estimateBlockTime, getBestChainHeight, getBlockStore, getChainHead, getFalsePositiveRate, getHeightFuture, getOrphanRoot, getVersionTally, isOrphan, removeNewBestBlockListener, removeReorganizeListener, removeTransactionReceivedListener, removeWallet, resetFalsePositiveEstimate, setChainHead, trackFilteredTransactions
-
Field Details
-
blockStore
Keeps a map of block hashes to StoredBlocks.
-
-
Constructor Details
-
BlockChain
Constructs a BlockChain connected to the given wallet and store. To obtain a
Walletyou can construct one from scratch, or you can deserialize a saved wallet from disk usingWallet.loadFromFile(File, WalletExtension...)For the store, you should use
SPVBlockStoreor you could also try aMemoryBlockStoreif 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, Wallet wallet, BlockStore blockStore) throws BlockStoreException - Throws:
BlockStoreException
-
BlockChain
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
- Throws:
BlockStoreException
-
BlockChain
public BlockChain(Context params, List<? extends Wallet> wallets, BlockStore blockStore) throws BlockStoreException Constructs a BlockChain connected to the given list of listeners and a store.- Throws:
BlockStoreException
-
BlockChain
public BlockChain(NetworkParameters params, List<? extends Wallet> wallets, BlockStore blockStore) throws BlockStoreException - Throws:
BlockStoreException
-
-
Method Details
-
addToBlockStore
protected StoredBlock addToBlockStore(StoredBlock storedPrev, Block blockHeader, TransactionOutputChanges txOutChanges) throws BlockStoreException, VerificationException Description copied from class:AbstractBlockChainAdds/updates the givenStoredBlockwith the block store. This version is used when the transactions have already been verified to properly spend txOutputChanges.- Specified by:
addToBlockStorein classAbstractBlockChain- Parameters:
storedPrev- TheStoredBlockwhich immediately precedes block.blockHeader- TheStoredBlockto 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- if a failure occurs while storing a blockVerificationException- if the block is invalid
-
addToBlockStore
protected StoredBlock addToBlockStore(StoredBlock storedPrev, Block blockHeader) throws BlockStoreException, VerificationException Description copied from class:AbstractBlockChainAdds/updates the givenBlockwith the block store. This version is used when the transactions have not been verified.- Specified by:
addToBlockStorein classAbstractBlockChain- Parameters:
storedPrev- TheStoredBlockwhich immediately precedes block.blockHeader- TheBlockto add/update.- Returns:
- the newly created
StoredBlock - Throws:
BlockStoreException- if a failure occurs while storing a blockVerificationException- if the block is invalid
-
rollbackBlockStore
Description copied from class:AbstractBlockChainRollback the block store to a given height. This is currently only supported byBlockChaininstances.- Specified by:
rollbackBlockStorein classAbstractBlockChain- Parameters:
height- height to roll back to- Throws:
BlockStoreException- if the operation fails or is unsupported.
-
shouldVerifyTransactions
protected boolean shouldVerifyTransactions()Description copied from class:AbstractBlockChainWhether 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:
shouldVerifyTransactionsin classAbstractBlockChain- Returns:
- true if we are verifying all transactions
-
connectTransactions
Description copied from class:AbstractBlockChainConnect 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:
connectTransactionsin classAbstractBlockChain- Parameters:
height- block height to attach atblock- block to connect- Returns:
- The full set of all changes made to the set of open transaction outputs.
-
connectTransactions
Description copied from class:AbstractBlockChainLoad 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:
connectTransactionsin classAbstractBlockChain- Parameters:
newBlock- block to load- Returns:
- The full set of all changes made to the set of open transaction outputs.
-
disconnectTransactions
Description copied from class:AbstractBlockChainDisconnect each transaction in the block (after reading it from the block store) Only called if(shouldVerifyTransactions())- Specified by:
disconnectTransactionsin classAbstractBlockChain- Parameters:
block- block to disconnect
-
doSetChainHead
Description copied from class:AbstractBlockChainCalled 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:
doSetChainHeadin classAbstractBlockChain- Parameters:
chainHead- chain head to set- Throws:
BlockStoreException- if a failure occurs while storing a block
-
notSettingChainHead
Description copied from class:AbstractBlockChainCalled 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:
notSettingChainHeadin classAbstractBlockChain- Throws:
BlockStoreException- if a failure occurs while storing a block
-
getStoredBlockInCurrentScope
Description copied from class:AbstractBlockChainFor a standard BlockChain, this should return blockStore.get(hash), for a FullPrunedBlockChain blockStore.getOnceUndoableStoredBlock(hash)- Specified by:
getStoredBlockInCurrentScopein classAbstractBlockChain- Parameters:
hash- hash of block to fetch- Returns:
- block with matching hash
- Throws:
BlockStoreException- if a failure occurs while storing a block
-
add
Description copied from class:AbstractBlockChainProcesses 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:
addin classAbstractBlockChain- Parameters:
block- received block- Returns:
- true if block can be connected, false if block is valid but can't be connected
- Throws:
VerificationException- if invalid blockPrunedException- a reorg that is too-long for our stored block data has occurred
-