public abstract class DatabaseFullPrunedBlockStore extends Object implements FullPrunedBlockStore
A generic full pruned block store for a relational database. This generic class requires certain table structures for the block store.
The following are the tables and field names/types that are assumed:-
setting table
| Field Name | Type (generic) |
|---|---|
| name | string |
| value | binary |
headers table
| Field Name | Type (generic) |
|---|---|
| hash | binary |
| chainwork | binary |
| height | integer |
| header | binary |
| wasundoable | boolean |
undoableblocks table
| Field Name | Type (generic) |
|---|---|
| hash | binary |
| height | integer |
| txoutchanges | binary |
| transactions | binary |
openoutputs table
| Field Name | Type (generic) |
|---|---|
| hash | binary |
| index | integer |
| height | integer |
| value | integer |
| scriptbytes | binary |
| toaddress | string |
| addresstargetable | integer |
| coinbase | boolean |
| Modifier and Type | Field and Description |
|---|---|
protected List<Connection> |
allConnections |
protected StoredBlock |
chainHeadBlock |
protected Sha256Hash |
chainHeadHash |
protected ThreadLocal<Connection> |
conn |
protected String |
connectionURL |
protected int |
fullStoreDepth |
protected NetworkParameters |
params |
protected String |
password |
protected String |
schemaName |
protected String |
username |
protected StoredBlock |
verifiedChainHeadBlock |
protected Sha256Hash |
verifiedChainHeadHash |
| Constructor and Description |
|---|
DatabaseFullPrunedBlockStore(NetworkParameters params,
String connectionURL,
int fullStoreDepth,
String username,
String password,
String schemaName)
Create a new DatabaseFullPrunedBlockStore, using the full connection URL instead of a hostname and password,
and optionally allowing a schema to be specified.
|
| Modifier and Type | Method and Description |
|---|---|
void |
abortDatabaseBatchWrite() |
void |
addUnspentTransactionOutput(UTXO out)
Adds a
UTXO to the list of unspent TransactionOutputs |
void |
beginDatabaseBatchWrite()
Begins/Commits/Aborts a database transaction.
|
BigInteger |
calculateBalanceForAddress(Address address)
Calculate the balance for a coinbase, to-address, or p2sh address.
|
void |
close()
Closes the store.
|
void |
commitDatabaseBatchWrite() |
void |
deleteStore()
Deletes the store by deleting the tables within the database.
|
void |
dumpSizes()
Dumps information about the size of actual data in the database to standard output
The only truly useless data counted is printed in the form "N in id indexes"
This does not take database indexes into account.
|
StoredBlock |
get(Sha256Hash hash)
Returns the StoredBlock given a hash.
|
StoredBlock |
get(Sha256Hash hash,
boolean wasUndoableOnly) |
protected String |
getBalanceSelectSQL()
Get the SQL to select the total balance for a given address.
|
StoredBlock |
getChainHead()
Returns the
StoredBlock that represents the top of the chain of greatest total work. |
int |
getChainHeadHeight()
Get the height of the chain head.
|
protected List<String> |
getCompatibilitySQL()
Get the SQL statements to check if the database is compatible.
|
protected abstract List<String> |
getCreateIndexesSQL()
Get the SQL statements that create the indexes (DDL).
|
protected abstract List<String> |
getCreateSchemeSQL()
Get the SQL statements that create the schema (DDL).
|
protected abstract List<String> |
getCreateTablesSQL()
Get the SQL statements that create the tables (DDL).
|
protected abstract String |
getDatabaseDriverClass()
Get the database driver class,
|
protected String |
getDeleteOpenoutputsSQL()
Get the SQL to delete a openoutputs record.
|
protected String |
getDeleteUndoableBlocksSQL()
Get the SQL to delete a undoableblocks record.
|
protected List<String> |
getDropTablesSQL()
Get the SQL to drop all the tables (DDL).
|
protected abstract String |
getDuplicateKeyErrorCode()
Get the database specific error code that indicated a duplicate key error when inserting a record.
|
protected String |
getInsertHeadersSQL()
Get the SQL to insert a headers record.
|
protected String |
getInsertOpenoutputsSQL()
Get the SQL to insert a openoutputs record.
|
protected String |
getInsertSettingsSQL()
Get the SQL to insert a settings record.
|
protected String |
getInsertUndoableBlocksSQL()
Get the SQL to insert a undoableblocks record.
|
StoredBlock |
getOnceUndoableStoredBlock(Sha256Hash hash)
Returns the StoredBlock that was added as a StoredUndoableBlock given a hash.
|
List<UTXO> |
getOpenTransactionOutputs(List<Address> addresses)
Get the list of
UTXO's for a given address. |
NetworkParameters |
getParams()
Get the
NetworkParameters of this store. |
protected String |
getSelectHeadersDumpSQL()
Get the SQL to select the headers dump fields for sizing/statistics.
|
protected String |
getSelectHeadersSQL()
Get the SQL to select a headers record.
|
protected String |
getSelectOpenoutputsCountSQL()
Get the SQL to select count of openoutputs.
|
protected String |
getSelectopenoutputsDumpSQL()
Get the SQL to select the openoutouts dump fields for sizing/statistics.
|
protected String |
getSelectOpenoutputsSQL()
Get the SQL to select a openoutputs record.
|
protected String |
getSelectSettingsDumpSQL()
Get the SQL to select the setting dump fields for sizing/statistics.
|
protected String |
getSelectSettingsSQL()
Get the SQL to select a setting value.
|
protected String |
getSelectUndoableblocksDumpSQL()
Get the SQL to select the undoableblocks dump fields for sizing/statistics.
|
protected String |
getSelectUndoableBlocksSQL()
Get the SQL to select an undoableblocks record.
|
protected String |
getTablesExistSQL()
Get the SQL statement that checks if tables exist.
|
UTXO |
getTransactionOutput(Sha256Hash hash,
long index)
Gets a
UTXO with the given hash and index, or null if none is found |
protected String |
getTrasactionOutputSelectSQL()
Get the SQL to select the transaction outputs for a given address.
|
StoredUndoableBlock |
getUndoBlock(Sha256Hash hash)
Returns a
StoredUndoableBlock whose block.getHash() method will be equal to the parameter. |
protected String |
getUpdateHeadersSQL()
Get the SQL to update a headers record.
|
protected String |
getUpdateSettingsSLQ()
Get the SQL to update a setting value.
|
protected String |
getUpdateUndoableBlocksSQL()
Get the SQL to update a undoableblocks record.
|
StoredBlock |
getVerifiedChainHead()
Returns the
StoredBlock that represents the top of the chain of greatest total work that has
been fully verified and the point in the chain at which the unspent transaction output set in this
store represents. |
boolean |
hasUnspentOutputs(Sha256Hash hash,
int numOutputs)
True if this store has any unspent outputs from a transaction with a hash equal to the first parameter
|
protected void |
maybeConnect()
If there isn't a connection on the
ThreadLocal then create and store it. |
void |
put(StoredBlock storedBlock)
Saves the given block header+extra data.
|
void |
put(StoredBlock storedBlock,
StoredUndoableBlock undoableBlock)
Saves the given
StoredUndoableBlock and StoredBlock. |
protected void |
putUpdateStoredBlock(StoredBlock storedBlock,
boolean wasUndoable) |
void |
removeUnspentTransactionOutput(UTXO out)
Removes a
UTXO from the list of unspent TransactionOutputs
Note that the coinbase of the genesis block should NEVER be spendable and thus never in the list. |
void |
resetStore()
Resets the store by deleting the contents of the tables and reinitialising them.
|
void |
setChainHead(StoredBlock chainHead)
Sets the
StoredBlock that represents the top of the chain of greatest total work. |
void |
setVerifiedChainHead(StoredBlock chainHead)
Sets the
StoredBlock that represents the top of the chain of greatest total work that has been
fully verified. |
protected Sha256Hash chainHeadHash
protected StoredBlock chainHeadBlock
protected Sha256Hash verifiedChainHeadHash
protected StoredBlock verifiedChainHeadBlock
protected NetworkParameters params
protected ThreadLocal<Connection> conn
protected List<Connection> allConnections
protected String connectionURL
protected int fullStoreDepth
protected String username
protected String password
protected String schemaName
public DatabaseFullPrunedBlockStore(NetworkParameters params, String connectionURL, int fullStoreDepth, @Nullable String username, @Nullable String password, @Nullable String schemaName) throws BlockStoreException
Create a new DatabaseFullPrunedBlockStore, using the full connection URL instead of a hostname and password, and optionally allowing a schema to be specified.
params - A copy of the NetworkParameters used.connectionURL - The jdbc url to connect to the database.fullStoreDepth - The number of blocks of history stored in full (something like 1000 is pretty safe).username - The database username.password - The password to the database.schemaName - The name of the schema to put the tables in. May be null if no schema is being used.BlockStoreException - If there is a failure to connect and/or initialise the database.protected abstract String getDatabaseDriverClass()
i.e org.postgresql.Driver.
protected abstract List<String> getCreateSchemeSQL()
protected abstract List<String> getCreateTablesSQL()
protected abstract List<String> getCreateIndexesSQL()
protected abstract String getDuplicateKeyErrorCode()
This is the code returned by SQLException.getSQLState()
protected String getBalanceSelectSQL()
protected String getTablesExistSQL()
protected List<String> getCompatibilitySQL()
protected String getTrasactionOutputSelectSQL()
protected List<String> getDropTablesSQL()
protected String getSelectSettingsSQL()
protected String getInsertSettingsSQL()
protected String getUpdateSettingsSLQ()
protected String getSelectHeadersSQL()
protected String getInsertHeadersSQL()
protected String getUpdateHeadersSQL()
protected String getSelectUndoableBlocksSQL()
protected String getInsertUndoableBlocksSQL()
protected String getUpdateUndoableBlocksSQL()
protected String getDeleteUndoableBlocksSQL()
protected String getSelectOpenoutputsSQL()
protected String getSelectOpenoutputsCountSQL()
protected String getInsertOpenoutputsSQL()
protected String getDeleteOpenoutputsSQL()
protected String getSelectSettingsDumpSQL()
protected String getSelectHeadersDumpSQL()
protected String getSelectUndoableblocksDumpSQL()
protected String getSelectopenoutputsDumpSQL()
protected void maybeConnect()
throws BlockStoreException
If there isn't a connection on the ThreadLocal then create and store it.
This will also automatically set up the schema if it does not exist within the DB.
BlockStoreException - if successful connection to the DB couldn't be made.public void close()
BlockStoreclose in interface BlockStoreprotected void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable) throws SQLException
SQLExceptionpublic void put(StoredBlock storedBlock) throws BlockStoreException
BlockStoreput in interface BlockStoreBlockStoreExceptionpublic void put(StoredBlock storedBlock, StoredUndoableBlock undoableBlock) throws BlockStoreException
FullPrunedBlockStoreSaves the given StoredUndoableBlock and StoredBlock. Calculates keys from the StoredBlock
Though not required for proper function of a FullPrunedBlockStore, any user of a FullPrunedBlockStore should ensure that a StoredUndoableBlock for each block up to the fully verified chain head has been added to this block store using this function (not put(StoredBlock)), so that the ability to perform reorgs is maintained.
put in interface FullPrunedBlockStoreBlockStoreException - if there is a problem with the underlying storage layer, such as running out of disk space.public StoredBlock get(Sha256Hash hash, boolean wasUndoableOnly) throws BlockStoreException
BlockStoreExceptionpublic StoredBlock get(Sha256Hash hash) throws BlockStoreException
BlockStoreget in interface BlockStoreBlockStoreExceptionpublic StoredBlock getOnceUndoableStoredBlock(Sha256Hash hash) throws BlockStoreException
FullPrunedBlockStoregetOnceUndoableStoredBlock in interface FullPrunedBlockStoreBlockStoreExceptionpublic StoredUndoableBlock getUndoBlock(Sha256Hash hash) throws BlockStoreException
FullPrunedBlockStoreStoredUndoableBlock whose block.getHash() method will be equal to the parameter. If no such
block is found, returns null. Note that this may return null more often than get(Sha256Hash hash) as not all
StoredBlocks have a StoredUndoableBlock copy stored as well.getUndoBlock in interface FullPrunedBlockStoreBlockStoreExceptionpublic StoredBlock getChainHead() throws BlockStoreException
BlockStoreStoredBlock that represents the top of the chain of greatest total work. Note that this
can be arbitrarily expensive, you probably should use AbstractBlockChain.getChainHead()
or perhaps AbstractBlockChain.getBestChainHeight() which will run in constant time and
not take any heavyweight locks.getChainHead in interface BlockStoreBlockStoreExceptionpublic void setChainHead(StoredBlock chainHead) throws BlockStoreException
BlockStoreStoredBlock that represents the top of the chain of greatest total work.setChainHead in interface BlockStoreBlockStoreExceptionpublic StoredBlock getVerifiedChainHead() throws BlockStoreException
FullPrunedBlockStoreStoredBlock that represents the top of the chain of greatest total work that has
been fully verified and the point in the chain at which the unspent transaction output set in this
store represents.getVerifiedChainHead in interface FullPrunedBlockStoreBlockStoreExceptionpublic void setVerifiedChainHead(StoredBlock chainHead) throws BlockStoreException
FullPrunedBlockStoreStoredBlock that represents the top of the chain of greatest total work that has been
fully verified. It should generally be set after a batch of updates to the transaction unspent output set,
before a call to commitDatabaseBatchWrite.
If chainHead has a greater height than the non-verified chain head (ie that set with
BlockStore.setChainHead(org.bitcoinj.core.StoredBlock)) the non-verified chain head should be set to the one set here.
In this way a class using a FullPrunedBlockStore only in full-verification mode can ignore the regular
BlockStore functions implemented as a part of a FullPrunedBlockStore.setVerifiedChainHead in interface FullPrunedBlockStoreBlockStoreExceptionpublic UTXO getTransactionOutput(Sha256Hash hash, long index) throws BlockStoreException
FullPrunedBlockStoreUTXO with the given hash and index, or null if none is foundgetTransactionOutput in interface FullPrunedBlockStoreBlockStoreExceptionpublic void addUnspentTransactionOutput(UTXO out) throws BlockStoreException
FullPrunedBlockStoreUTXO to the list of unspent TransactionOutputsaddUnspentTransactionOutput in interface FullPrunedBlockStoreBlockStoreExceptionpublic void removeUnspentTransactionOutput(UTXO out) throws BlockStoreException
FullPrunedBlockStoreUTXO from the list of unspent TransactionOutputs
Note that the coinbase of the genesis block should NEVER be spendable and thus never in the list.removeUnspentTransactionOutput in interface FullPrunedBlockStoreBlockStoreException - if there is an underlying storage issue, or out was not in the list.public void beginDatabaseBatchWrite()
throws BlockStoreException
FullPrunedBlockStoreBegins/Commits/Aborts a database transaction.
If abortDatabaseBatchWrite() is called by the same thread that called beginDatabaseBatchWrite(), any data writes between this call and abortDatabaseBatchWrite() made by the same thread should be discarded.
Furthermore, any data written after a call to beginDatabaseBatchWrite() should not be readable by any other threads until commitDatabaseBatchWrite() has been called by this thread. Multiple calls to beginDatabaseBatchWrite() in any given thread should be ignored and treated as one call.
beginDatabaseBatchWrite in interface FullPrunedBlockStoreBlockStoreExceptionpublic void commitDatabaseBatchWrite()
throws BlockStoreException
commitDatabaseBatchWrite in interface FullPrunedBlockStoreBlockStoreExceptionpublic void abortDatabaseBatchWrite()
throws BlockStoreException
abortDatabaseBatchWrite in interface FullPrunedBlockStoreBlockStoreExceptionpublic boolean hasUnspentOutputs(Sha256Hash hash, int numOutputs) throws BlockStoreException
FullPrunedBlockStorehasUnspentOutputs in interface FullPrunedBlockStorenumOutputs - the number of outputs the given transaction hasBlockStoreExceptionpublic NetworkParameters getParams()
BlockStoreNetworkParameters of this store.getParams in interface UTXOProvidergetParams in interface BlockStorepublic int getChainHeadHeight()
throws UTXOProviderException
UTXOProvidergetChainHeadHeight in interface UTXOProviderUTXOProviderExceptionpublic void resetStore()
throws BlockStoreException
BlockStoreException - If the tables couldn't be cleared and initialised.public void deleteStore()
throws BlockStoreException
BlockStoreException - If tables couldn't be deleted.public BigInteger calculateBalanceForAddress(Address address) throws BlockStoreException
The balance getBalanceSelectSQL() returns
the balance (summed) as an number, then use calculateClientSide=false
The balance getBalanceSelectSQL() returns
the all the openoutputs as stored in the DB (binary), then use calculateClientSide=true
address - The address to calculate the balance ofBlockStoreException - If there is an error getting the balance.public List<UTXO> getOpenTransactionOutputs(List<Address> addresses) throws UTXOProviderException
UTXOProviderUTXO's for a given address.getOpenTransactionOutputs in interface UTXOProvideraddresses - List of address.UTXOProviderException - If there is an error.public void dumpSizes()
throws SQLException,
BlockStoreException
SQLExceptionBlockStoreExceptionCopyright © 2015. All rights reserved.