public abstract class DatabaseFullPrunedBlockStore extends java.lang.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:
Field Name | Type (generic) |
---|---|
name | string |
value | binary |
Field Name | Type (generic) |
---|---|
hash | binary |
chainwork | binary |
height | integer |
header | binary |
wasundoable | boolean |
Field Name | Type (generic) |
---|---|
hash | binary |
height | integer |
txoutchanges | binary |
transactions | binary |
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 java.util.List<java.sql.Connection> |
allConnections |
protected StoredBlock |
chainHeadBlock |
protected Sha256Hash |
chainHeadHash |
protected java.lang.ThreadLocal<java.sql.Connection> |
conn |
protected java.lang.String |
connectionURL |
protected int |
fullStoreDepth |
protected NetworkParameters |
params |
protected java.lang.String |
password |
protected java.lang.String |
schemaName |
protected java.lang.String |
username |
protected StoredBlock |
verifiedChainHeadBlock |
protected Sha256Hash |
verifiedChainHeadHash |
Constructor and Description |
---|
DatabaseFullPrunedBlockStore(NetworkParameters params,
java.lang.String connectionURL,
int fullStoreDepth,
java.lang.String username,
java.lang.String password,
java.lang.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.
|
java.math.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 java.lang.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 java.util.List<java.lang.String> |
getCompatibilitySQL()
Get the SQL statements to check if the database is compatible.
|
protected abstract java.util.List<java.lang.String> |
getCreateIndexesSQL()
Get the SQL statements that create the indexes (DDL).
|
protected abstract java.util.List<java.lang.String> |
getCreateSchemeSQL()
Get the SQL statements that create the schema (DDL).
|
protected abstract java.util.List<java.lang.String> |
getCreateTablesSQL()
Get the SQL statements that create the tables (DDL).
|
protected abstract java.lang.String |
getDatabaseDriverClass()
Get the database driver class,
|
protected java.lang.String |
getDeleteOpenoutputsSQL()
Get the SQL to delete a openoutputs record.
|
protected java.lang.String |
getDeleteUndoableBlocksSQL()
Get the SQL to delete a undoableblocks record.
|
protected java.util.List<java.lang.String> |
getDropTablesSQL()
Get the SQL to drop all the tables (DDL).
|
protected abstract java.lang.String |
getDuplicateKeyErrorCode()
Get the database specific error code that indicated a duplicate key error when inserting a record.
|
protected java.lang.String |
getInsertHeadersSQL()
Get the SQL to insert a headers record.
|
protected java.lang.String |
getInsertOpenoutputsSQL()
Get the SQL to insert a openoutputs record.
|
protected java.lang.String |
getInsertSettingsSQL()
Get the SQL to insert a settings record.
|
protected java.lang.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.
|
java.util.List<UTXO> |
getOpenTransactionOutputs(java.util.List<ECKey> keys)
Get the list of
UTXO 's for given keys. |
NetworkParameters |
getParams()
Get the
NetworkParameters of this store. |
protected java.lang.String |
getSelectHeadersDumpSQL()
Get the SQL to select the headers dump fields for sizing/statistics.
|
protected java.lang.String |
getSelectHeadersSQL()
Get the SQL to select a headers record.
|
protected java.lang.String |
getSelectOpenoutputsCountSQL()
Get the SQL to select count of openoutputs.
|
protected java.lang.String |
getSelectopenoutputsDumpSQL()
Get the SQL to select the openoutouts dump fields for sizing/statistics.
|
protected java.lang.String |
getSelectOpenoutputsSQL()
Get the SQL to select a openoutputs record.
|
protected java.lang.String |
getSelectSettingsDumpSQL()
Get the SQL to select the setting dump fields for sizing/statistics.
|
protected java.lang.String |
getSelectSettingsSQL()
Get the SQL to select a setting value.
|
protected java.lang.String |
getSelectUndoableblocksDumpSQL()
Get the SQL to select the undoableblocks dump fields for sizing/statistics.
|
protected java.lang.String |
getSelectUndoableBlocksSQL()
Get the SQL to select an undoableblocks record.
|
protected java.lang.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 java.lang.String |
getTransactionOutputSelectSQL()
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 java.lang.String |
getUpdateHeadersSQL()
Get the SQL to update a headers record.
|
protected java.lang.String |
getUpdateSettingsSLQ()
Get the SQL to update a setting value.
|
protected java.lang.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 java.lang.ThreadLocal<java.sql.Connection> conn
protected java.util.List<java.sql.Connection> allConnections
protected java.lang.String connectionURL
protected int fullStoreDepth
protected java.lang.String username
protected java.lang.String password
protected java.lang.String schemaName
public DatabaseFullPrunedBlockStore(NetworkParameters params, java.lang.String connectionURL, int fullStoreDepth, @Nullable java.lang.String username, @Nullable java.lang.String password, @Nullable java.lang.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 java.lang.String getDatabaseDriverClass()
i.e org.postgresql.Driver.
protected abstract java.util.List<java.lang.String> getCreateSchemeSQL()
protected abstract java.util.List<java.lang.String> getCreateTablesSQL()
protected abstract java.util.List<java.lang.String> getCreateIndexesSQL()
protected abstract java.lang.String getDuplicateKeyErrorCode()
This is the code returned by SQLException.getSQLState()
protected java.lang.String getBalanceSelectSQL()
protected java.lang.String getTablesExistSQL()
protected java.util.List<java.lang.String> getCompatibilitySQL()
protected java.lang.String getTransactionOutputSelectSQL()
protected java.util.List<java.lang.String> getDropTablesSQL()
protected java.lang.String getSelectSettingsSQL()
protected java.lang.String getInsertSettingsSQL()
protected java.lang.String getUpdateSettingsSLQ()
protected java.lang.String getSelectHeadersSQL()
protected java.lang.String getInsertHeadersSQL()
protected java.lang.String getUpdateHeadersSQL()
protected java.lang.String getSelectUndoableBlocksSQL()
protected java.lang.String getInsertUndoableBlocksSQL()
protected java.lang.String getUpdateUndoableBlocksSQL()
protected java.lang.String getDeleteUndoableBlocksSQL()
protected java.lang.String getSelectOpenoutputsSQL()
protected java.lang.String getSelectOpenoutputsCountSQL()
protected java.lang.String getInsertOpenoutputsSQL()
protected java.lang.String getDeleteOpenoutputsSQL()
protected java.lang.String getSelectSettingsDumpSQL()
protected java.lang.String getSelectHeadersDumpSQL()
protected java.lang.String getSelectUndoableblocksDumpSQL()
protected java.lang.String getSelectopenoutputsDumpSQL()
protected final 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()
BlockStore
close
in interface BlockStore
protected void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable) throws java.sql.SQLException
java.sql.SQLException
public void put(StoredBlock storedBlock) throws BlockStoreException
BlockStore
put
in interface BlockStore
BlockStoreException
public void put(StoredBlock storedBlock, StoredUndoableBlock undoableBlock) throws BlockStoreException
FullPrunedBlockStore
Saves 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 FullPrunedBlockStore
BlockStoreException
- 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
BlockStoreException
public StoredBlock get(Sha256Hash hash) throws BlockStoreException
BlockStore
get
in interface BlockStore
BlockStoreException
public StoredBlock getOnceUndoableStoredBlock(Sha256Hash hash) throws BlockStoreException
FullPrunedBlockStore
getOnceUndoableStoredBlock
in interface FullPrunedBlockStore
BlockStoreException
public StoredUndoableBlock getUndoBlock(Sha256Hash hash) throws BlockStoreException
FullPrunedBlockStore
StoredUndoableBlock
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
StoredBlock
s have a StoredUndoableBlock
copy stored as well.getUndoBlock
in interface FullPrunedBlockStore
BlockStoreException
public StoredBlock getChainHead() throws BlockStoreException
BlockStore
StoredBlock
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 BlockStore
BlockStoreException
public void setChainHead(StoredBlock chainHead) throws BlockStoreException
BlockStore
StoredBlock
that represents the top of the chain of greatest total work.setChainHead
in interface BlockStore
BlockStoreException
public StoredBlock getVerifiedChainHead() throws BlockStoreException
FullPrunedBlockStore
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.getVerifiedChainHead
in interface FullPrunedBlockStore
BlockStoreException
public void setVerifiedChainHead(StoredBlock chainHead) throws BlockStoreException
FullPrunedBlockStore
StoredBlock
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 FullPrunedBlockStore
BlockStoreException
public UTXO getTransactionOutput(Sha256Hash hash, long index) throws BlockStoreException
FullPrunedBlockStore
UTXO
with the given hash and index, or null if none is foundgetTransactionOutput
in interface FullPrunedBlockStore
BlockStoreException
public void addUnspentTransactionOutput(UTXO out) throws BlockStoreException
FullPrunedBlockStore
UTXO
to the list of unspent TransactionOutputsaddUnspentTransactionOutput
in interface FullPrunedBlockStore
BlockStoreException
public void removeUnspentTransactionOutput(UTXO out) throws BlockStoreException
FullPrunedBlockStore
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.removeUnspentTransactionOutput
in interface FullPrunedBlockStore
BlockStoreException
- if there is an underlying storage issue, or out was not in the list.public void beginDatabaseBatchWrite() throws BlockStoreException
FullPrunedBlockStore
Begins/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 FullPrunedBlockStore
BlockStoreException
public void commitDatabaseBatchWrite() throws BlockStoreException
commitDatabaseBatchWrite
in interface FullPrunedBlockStore
BlockStoreException
public void abortDatabaseBatchWrite() throws BlockStoreException
abortDatabaseBatchWrite
in interface FullPrunedBlockStore
BlockStoreException
public boolean hasUnspentOutputs(Sha256Hash hash, int numOutputs) throws BlockStoreException
FullPrunedBlockStore
hasUnspentOutputs
in interface FullPrunedBlockStore
numOutputs
- the number of outputs the given transaction hasBlockStoreException
public NetworkParameters getParams()
BlockStore
NetworkParameters
of this store.getParams
in interface UTXOProvider
getParams
in interface BlockStore
public int getChainHeadHeight() throws UTXOProviderException
UTXOProvider
getChainHeadHeight
in interface UTXOProvider
UTXOProviderException
- If there is an error.public 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 java.math.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 java.util.List<UTXO> getOpenTransactionOutputs(java.util.List<ECKey> keys) throws UTXOProviderException
UTXOProvider
UTXO
's for given keys.getOpenTransactionOutputs
in interface UTXOProvider
keys
- List of keys.UTXOProviderException
- If there is an error.public void dumpSizes() throws java.sql.SQLException, BlockStoreException
java.sql.SQLException
BlockStoreException