Package org.bitcoinj.store
Class SPVBlockStore
- java.lang.Object
-
- org.bitcoinj.store.SPVBlockStore
-
- All Implemented Interfaces:
BlockStore
public class SPVBlockStore extends java.lang.Object implements 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
Fields Modifier and Type Field Description protected java.util.LinkedHashMap<Sha256Hash,StoredBlock>blockCacheprotected java.nio.MappedByteBufferbufferstatic intDEFAULT_CAPACITYThe default number of headers that will be stored in the ring buffer.protected static intFILE_PROLOGUE_BYTESprotected java.nio.channels.FileLockfileLockstatic java.lang.StringHEADER_MAGICprotected StoredBlocklastChainHeadprotected java.util.concurrent.locks.ReentrantLocklockprotected java.util.LinkedHashMap<Sha256Hash,java.lang.Object>notFoundCacheprotected NetworkParametersparamsprotected java.io.RandomAccessFilerandomAccessFileprotected static intRECORD_SIZE
-
Constructor Summary
Constructors Constructor Description SPVBlockStore(NetworkParameters params, java.io.File file)Creates and initializes an SPV block store that can holdDEFAULT_CAPACITYblock headers.SPVBlockStore(NetworkParameters params, java.io.File file, int capacity, boolean grow)Creates and initializes an SPV block store that can hold a given amount of blocks.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()voidclose()Closes the store.StoredBlockget(Sha256Hash hash)Returns the StoredBlock given a hash.StoredBlockgetChainHead()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.NetworkParametersgetParams()Get theNetworkParametersof this store.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 Detail
-
lock
protected final java.util.concurrent.locks.ReentrantLock lock
-
DEFAULT_CAPACITY
public static final int DEFAULT_CAPACITY
The default number of headers that will be stored in the ring buffer.- See Also:
- Constant Field Values
-
HEADER_MAGIC
public static final java.lang.String HEADER_MAGIC
- See Also:
- Constant Field Values
-
buffer
protected volatile java.nio.MappedByteBuffer buffer
-
params
protected final NetworkParameters params
-
blockCache
protected java.util.LinkedHashMap<Sha256Hash,StoredBlock> blockCache
-
notFoundCache
protected java.util.LinkedHashMap<Sha256Hash,java.lang.Object> notFoundCache
-
fileLock
protected java.nio.channels.FileLock fileLock
-
randomAccessFile
protected java.io.RandomAccessFile randomAccessFile
-
lastChainHead
protected StoredBlock lastChainHead
-
RECORD_SIZE
protected static final int RECORD_SIZE
- See Also:
- Constant Field Values
-
FILE_PROLOGUE_BYTES
protected static final int FILE_PROLOGUE_BYTES
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SPVBlockStore
public SPVBlockStore(NetworkParameters params, java.io.File file) throws BlockStoreException
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, java.io.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- wether or not to migrate an existing block store of different capacity- Throws:
BlockStoreException- if something goes wrong
-
-
Method Detail
-
getFileSize
public static final int getFileSize(int capacity)
Returns the size in bytes of the file that is used to store the chain with the current parameters.
-
put
public void put(StoredBlock block) throws BlockStoreException
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
@Nullable public StoredBlock get(Sha256Hash hash) throws BlockStoreException
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
public StoredBlock getChainHead() throws BlockStoreException
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
public void setChainHead(StoredBlock chainHead) throws BlockStoreException
Description copied from interface:BlockStoreSets theStoredBlockthat represents the top of the chain of greatest total work.- Specified by:
setChainHeadin interfaceBlockStore- Throws:
BlockStoreException
-
close
public void close() throws BlockStoreExceptionDescription copied from interface:BlockStoreCloses the store.- Specified by:
closein interfaceBlockStore- Throws:
BlockStoreException
-
getParams
public NetworkParameters getParams()
Description copied from interface:BlockStoreGet theNetworkParametersof this store.- Specified by:
getParamsin interfaceBlockStore- Returns:
- The network params.
-
clear
public void clear() throws java.lang.Exception- Throws:
java.lang.Exception
-
-