public class SPVBlockStore extends java.lang.Object implements BlockStore
Modifier and Type | Field and 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 and Description |
---|
SPVBlockStore(NetworkParameters params,
java.io.File file)
Creates and initializes an SPV block store that can hold
DEFAULT_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.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
void |
close()
Closes the store.
|
StoredBlock |
get(Sha256Hash hash)
Returns the StoredBlock given a hash.
|
StoredBlock |
getChainHead()
Returns the
StoredBlock 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.
|
NetworkParameters |
getParams()
Get the
NetworkParameters of this store. |
void |
put(StoredBlock block)
Saves the given block header+extra data.
|
void |
setChainHead(StoredBlock chainHead)
Sets the
StoredBlock that represents the top of the chain of greatest total work. |
protected final java.util.concurrent.locks.ReentrantLock lock
public static final int DEFAULT_CAPACITY
public static final java.lang.String HEADER_MAGIC
protected volatile java.nio.MappedByteBuffer buffer
protected final NetworkParameters params
protected java.util.LinkedHashMap<Sha256Hash,StoredBlock> blockCache
protected java.util.LinkedHashMap<Sha256Hash,java.lang.Object> notFoundCache
protected java.nio.channels.FileLock fileLock
protected java.io.RandomAccessFile randomAccessFile
protected StoredBlock lastChainHead
protected static final int RECORD_SIZE
protected static final int FILE_PROLOGUE_BYTES
public SPVBlockStore(NetworkParameters params, java.io.File file) throws BlockStoreException
DEFAULT_CAPACITY
block headers. Will create the
given file if it's missing. This operation will block on disk.file
- file to use for the block storeBlockStoreException
- if something goes wrongpublic SPVBlockStore(NetworkParameters params, java.io.File file, int capacity, boolean grow) throws BlockStoreException
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 capacityBlockStoreException
- if something goes wrongpublic static final int getFileSize(int capacity)
public void put(StoredBlock block) throws BlockStoreException
BlockStore
put
in interface BlockStore
BlockStoreException
@Nullable public StoredBlock get(Sha256Hash hash) throws BlockStoreException
BlockStore
get
in interface BlockStore
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 void close() throws BlockStoreException
BlockStore
close
in interface BlockStore
BlockStoreException
public NetworkParameters getParams()
BlockStore
NetworkParameters
of this store.getParams
in interface BlockStore
public void clear() throws java.lang.Exception
java.lang.Exception