Package org.bitcoinj.core
Class FullPrunedBlockChain
- java.lang.Object
-
- org.bitcoinj.core.AbstractBlockChain
-
- org.bitcoinj.core.FullPrunedBlockChain
-
public class FullPrunedBlockChain extends AbstractBlockChain
A FullPrunedBlockChain works in conjunction with a
FullPrunedBlockStoreto verify all the rules of the Bitcoin system, with the downside being a large cost in system resources. Fully verifying means all unspent transaction outputs are stored. Once a transaction output is spent and that spend is buried deep enough, the data related to it is deleted to ensure disk space usage doesn't grow forever. For this reason a pruning node cannot serve the full block chain to other clients, but it nevertheless provides the same security guarantees as Bitcoin Core does.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.bitcoinj.core.AbstractBlockChain
AbstractBlockChain.NewBlockType
-
-
Field Summary
Fields Modifier and Type Field Description protected FullPrunedBlockStoreblockStoreKeeps 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
Constructors Constructor Description FullPrunedBlockChain(Context context, java.util.List<Wallet> listeners, FullPrunedBlockStore blockStore)Constructs a block chain connected to the given list of wallets and a store.FullPrunedBlockChain(Context context, FullPrunedBlockStore blockStore)Constructs a block chain connected to the given store.FullPrunedBlockChain(Context context, Wallet wallet, FullPrunedBlockStore blockStore)Constructs a block chain connected to the given wallet and store.FullPrunedBlockChain(NetworkParameters params, java.util.List<Wallet> listeners, FullPrunedBlockStore blockStore)FullPrunedBlockChain(NetworkParameters params, FullPrunedBlockStore blockStore)FullPrunedBlockChain(NetworkParameters params, Wallet wallet, FullPrunedBlockStore blockStore)Constructs a block chain connected to the given wallet and store.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected StoredBlockaddToBlockStore(StoredBlock storedPrev, Block block)Adds/updates the givenBlockwith the block store.protected StoredBlockaddToBlockStore(StoredBlock storedPrev, Block header, 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)Used during reorgs to connect a block previously on a forkprotected voiddisconnectTransactions(StoredBlock oldBlock)This is broken for blocks that do not pass BIP30, so all BIP30-failing blocks which are allowed to fail BIP30 must be checkpointed.protected voiddoSetChainHead(StoredBlock chainHead)Called before setting chain head in memory.protected StoredBlockgetStoredBlockInCurrentScope(Sha256Hash hash)For a standard BlockChain, this should return blockStore.get(hash), for a FullPrunedBlockChain blockStore.getOnceUndoableStoredBlock(hash)protected voidnotSettingChainHead()Called 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.voidsetRunScripts(boolean value)Whether or not to run scripts whilst accepting blocks (i.e.protected booleanshouldVerifyTransactions()Whether or not we are maintaining a set of unspent outputs and are verifying all transactions.-
Methods inherited from class org.bitcoinj.core.AbstractBlockChain
add, 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 Detail
-
blockStore
protected final FullPrunedBlockStore blockStore
Keeps a map of block hashes to StoredBlocks.
-
-
Constructor Detail
-
FullPrunedBlockChain
public FullPrunedBlockChain(Context context, Wallet wallet, FullPrunedBlockStore blockStore) throws BlockStoreException
Constructs a block chain connected to the given wallet and store. To obtain aWalletyou can construct one from scratch, or you can deserialize a saved wallet from disk usingWallet.loadFromFile(File, WalletExtension...)- Throws:
BlockStoreException
-
FullPrunedBlockChain
public FullPrunedBlockChain(NetworkParameters params, Wallet wallet, FullPrunedBlockStore blockStore) throws BlockStoreException
Constructs a block chain connected to the given wallet and store. To obtain aWalletyou can construct one from scratch, or you can deserialize a saved wallet from disk usingWallet.loadFromFile(File, WalletExtension...)- Throws:
BlockStoreException
-
FullPrunedBlockChain
public FullPrunedBlockChain(Context context, FullPrunedBlockStore blockStore) throws BlockStoreException
Constructs a block chain connected to the given store.- Throws:
BlockStoreException
-
FullPrunedBlockChain
public FullPrunedBlockChain(NetworkParameters params, FullPrunedBlockStore blockStore) throws BlockStoreException
- Throws:
BlockStoreException
-
FullPrunedBlockChain
public FullPrunedBlockChain(Context context, java.util.List<Wallet> listeners, FullPrunedBlockStore blockStore) throws BlockStoreException
Constructs a block chain connected to the given list of wallets and a store.- Throws:
BlockStoreException
-
FullPrunedBlockChain
public FullPrunedBlockChain(NetworkParameters params, java.util.List<Wallet> listeners, FullPrunedBlockStore blockStore) throws BlockStoreException
- Throws:
BlockStoreException
-
-
Method Detail
-
addToBlockStore
protected StoredBlock addToBlockStore(StoredBlock storedPrev, Block header, 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.header- 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 block) 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.block- TheBlockto add/update.- Returns:
- the newly created
StoredBlock - Throws:
BlockStoreException- if a failure occurs while storing a blockVerificationException- if the block is invalid
-
rollbackBlockStore
protected void rollbackBlockStore(int height) throws BlockStoreExceptionDescription 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
-
setRunScripts
public void setRunScripts(boolean value)
Whether or not to run scripts whilst accepting blocks (i.e. checking signatures, for most transactions). If you're accepting data from an untrusted node, such as one found via the P2P network, this should be set to true (which is the default). If you're downloading a chain from a node you control, script execution is redundant because you know the connected node won't relay bad data to you. In that case it's safe to set this to false and obtain a significant speedup.
-
connectTransactions
protected TransactionOutputChanges connectTransactions(int height, Block block) throws VerificationException, BlockStoreException
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.
- Throws:
VerificationException- if an attempt was made to spend an already-spent output, or if a transaction incorrectly solved an output script.BlockStoreException- if the block store had an underlying error.
-
connectTransactions
protected TransactionOutputChanges connectTransactions(StoredBlock newBlock) throws VerificationException, BlockStoreException, PrunedException
Used during reorgs to connect a block previously on a fork- Specified by:
connectTransactionsin classAbstractBlockChain- Parameters:
newBlock- block to load- Returns:
- The full set of all changes made to the set of open transaction outputs.
- Throws:
VerificationException- if an attempt was made to spend an already-spent output, or if a transaction incorrectly solved an output script.BlockStoreException- if the block store had an underlying error or newBlock does not exist in the block store at all.PrunedException- if newBlock does not exist as aStoredUndoableBlockin the block store.
-
disconnectTransactions
protected void disconnectTransactions(StoredBlock oldBlock) throws PrunedException, BlockStoreException
This is broken for blocks that do not pass BIP30, so all BIP30-failing blocks which are allowed to fail BIP30 must be checkpointed.- Specified by:
disconnectTransactionsin classAbstractBlockChain- Parameters:
oldBlock- block to disconnect- Throws:
PrunedException- if block does not exist as aStoredUndoableBlockin the block store.BlockStoreException- if the block store had an underlying error or block does not exist in the block store at all.
-
doSetChainHead
protected void doSetChainHead(StoredBlock chainHead) throws BlockStoreException
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
protected void notSettingChainHead() throws BlockStoreExceptionDescription 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
protected StoredBlock getStoredBlockInCurrentScope(Sha256Hash hash) throws BlockStoreException
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
-
-