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>
blockCache
protected java.nio.MappedByteBuffer
buffer
static int
DEFAULT_CAPACITY
The default number of headers that will be stored in the ring buffer.protected static int
FILE_PROLOGUE_BYTES
protected java.nio.channels.FileLock
fileLock
static java.lang.String
HEADER_MAGIC
protected StoredBlock
lastChainHead
protected java.util.concurrent.locks.ReentrantLock
lock
protected java.util.LinkedHashMap<Sha256Hash,java.lang.Object>
notFoundCache
protected NetworkParameters
params
protected java.io.RandomAccessFile
randomAccessFile
protected static int
RECORD_SIZE
-
Constructor Summary
Constructors Constructor Description SPVBlockStore(NetworkParameters params, java.io.File file)
Creates and initializes an SPV block store that can holdDEFAULT_CAPACITY
block 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 void
clear()
void
close()
Closes the store.StoredBlock
get(Sha256Hash hash)
Returns the StoredBlock given a hash.StoredBlock
getChainHead()
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 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_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, 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 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: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
@Nullable public StoredBlock get(Sha256Hash hash) throws BlockStoreException
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
public StoredBlock getChainHead() throws BlockStoreException
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
public void setChainHead(StoredBlock chainHead) throws BlockStoreException
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
public void close() throws BlockStoreException
Description copied from interface:BlockStore
Closes the store.- Specified by:
close
in interfaceBlockStore
- Throws:
BlockStoreException
-
clear
public void clear() throws java.lang.Exception
- Throws:
java.lang.Exception
-
-