Package org.bitcoinj.store
Class SPVBlockStore
java.lang.Object
org.bitcoinj.store.SPVBlockStore
- All Implemented Interfaces:
BlockStore
An SPVBlockStore holds a limited number of block headers in a memory mapped ring buffer. With such a store, you
may not be able to process very deep re-orgs and could be disconnected from the chain (requiring a replay),
but as they are virtually unheard of this is not a significant risk.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected LinkedHashMap<Sha256Hash,StoredBlock> protected MappedByteBufferstatic final intThe default number of headers that will be stored in the ring buffer.protected static final intprotected FileLockstatic final StringDeprecated.protected StoredBlockprotected final ReentrantLockprotected LinkedHashMap<Sha256Hash,Object> protected final NetworkParametersprotected RandomAccessFile -
Constructor Summary
ConstructorsConstructorDescriptionSPVBlockStore(NetworkParameters params, File file) Creates and initializes an SPV block store that can holdDEFAULT_CAPACITYblock headers.SPVBlockStore(NetworkParameters params, File file, int capacity, boolean grow) Creates and initializes an SPV block store that can hold a given amount of blocks. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()voidclose()Closes the store.get(Sha256Hash hash) Returns the StoredBlock given a hash.Returns theStoredBlockthat represents the top of the chain of greatest total work.static intgetFileSize(int capacity) Returns the size in bytes of the file that is used to store the chain with the current parameters.voidput(StoredBlock block) Saves the given block header+extra data.voidsetChainHead(StoredBlock chainHead) Sets theStoredBlockthat represents the top of the chain of greatest total work.
-
Field Details
-
lock
-
DEFAULT_CAPACITY
public static final int DEFAULT_CAPACITYThe default number of headers that will be stored in the ring buffer.- See Also:
-
HEADER_MAGIC
Deprecated.- See Also:
-
buffer
-
params
-
blockCache
-
notFoundCache
-
fileLock
-
randomAccessFile
-
lastChainHead
-
FILE_PROLOGUE_BYTES
protected static final int FILE_PROLOGUE_BYTES- See Also:
-
-
Constructor Details
-
SPVBlockStore
Creates and initializes an SPV block store that can holdDEFAULT_CAPACITYblock headers. Will create the given file if it's missing. This operation will block on disk.- Parameters:
file- file to use for the block store- Throws:
BlockStoreException- if something goes wrong
-
SPVBlockStore
public SPVBlockStore(NetworkParameters params, File file, int capacity, boolean grow) throws BlockStoreException Creates and initializes an SPV block store that can hold a given amount of blocks. Will create the given file if it's missing. This operation will block on disk.- Parameters:
file- file to use for the block storecapacity- custom capacity in number of block headersgrow- whether or not to migrate an existing block store of different capacity- Throws:
BlockStoreException- if something goes wrong
-
-
Method Details
-
getFileSize
public static int getFileSize(int capacity) Returns the size in bytes of the file that is used to store the chain with the current parameters. -
put
Description copied from interface:BlockStoreSaves the given block header+extra data. The key isn't specified explicitly as it can be calculated from the StoredBlock directly. Can throw if there is a problem with the underlying storage layer such as running out of disk space.- Specified by:
putin interfaceBlockStore- Throws:
BlockStoreException
-
get
Description copied from interface:BlockStoreReturns the StoredBlock given a hash. The returned values block.getHash() method will be equal to the parameter. If no such block is found, returns null.- Specified by:
getin interfaceBlockStore- Throws:
BlockStoreException
-
getChainHead
Description copied from interface:BlockStoreReturns theStoredBlockthat represents the top of the chain of greatest total work. Note that this can be arbitrarily expensive, you probably should useAbstractBlockChain.getChainHead()or perhapsAbstractBlockChain.getBestChainHeight()which will run in constant time and not take any heavyweight locks.- Specified by:
getChainHeadin interfaceBlockStore- Throws:
BlockStoreException
-
setChainHead
Description copied from interface:BlockStoreSets theStoredBlockthat represents the top of the chain of greatest total work.- Specified by:
setChainHeadin interfaceBlockStore- Throws:
BlockStoreException
-
close
Description copied from interface:BlockStoreCloses the store.- Specified by:
closein interfaceBlockStore- Throws:
BlockStoreException
-
clear
- Throws:
Exception
-