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
Modifier and TypeFieldDescriptionprotected LinkedHashMap<Sha256Hash,
StoredBlock> protected MappedByteBuffer
static final int
The default number of headers that will be stored in the ring buffer.protected static final int
protected FileLock
static final String
Deprecated.protected StoredBlock
protected final ReentrantLock
protected LinkedHashMap<Sha256Hash,
Object> protected final NetworkParameters
protected RandomAccessFile
-
Constructor Summary
ConstructorDescriptionSPVBlockStore
(NetworkParameters params, File file) Creates and initializes an SPV block store that can holdDEFAULT_CAPACITY
block 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 TypeMethodDescriptionvoid
clear()
void
close()
Closes the store.get
(Sha256Hash hash) Returns the StoredBlock given a hash.Returns theStoredBlock
that represents the top of the chain of greatest total work.static int
getFileSize
(int capacity) Returns the size in bytes of the file that is used to store the chain with the current parameters.void
put
(StoredBlock block) Saves the given block header+extra data.void
setChainHead
(StoredBlock chainHead) Sets theStoredBlock
that 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_CAPACITY
block 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:BlockStore
Saves 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:
put
in interfaceBlockStore
- Throws:
BlockStoreException
-
get
Description copied from interface:BlockStore
Returns 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:
get
in interfaceBlockStore
- Throws:
BlockStoreException
-
getChainHead
Description copied from interface:BlockStore
Returns theStoredBlock
that 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:
getChainHead
in interfaceBlockStore
- Throws:
BlockStoreException
-
setChainHead
Description copied from interface:BlockStore
Sets theStoredBlock
that represents the top of the chain of greatest total work.- Specified by:
setChainHead
in interfaceBlockStore
- Throws:
BlockStoreException
-
close
Description copied from interface:BlockStore
Closes the store.- Specified by:
close
in interfaceBlockStore
- Throws:
BlockStoreException
-
clear
- Throws:
Exception
-