com.google.bitcoin.store
Class SPVBlockStore

java.lang.Object
  extended by com.google.bitcoin.store.SPVBlockStore
All Implemented Interfaces:
BlockStore

public class SPVBlockStore
extends 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
protected  LinkedHashMap<Sha256Hash,StoredBlock> blockCache
           
protected  MappedByteBuffer buffer
           
static int DEFAULT_NUM_HEADERS
          The default number of headers that will be stored in the ring buffer.
protected static int FILE_PROLOGUE_BYTES
           
protected  FileLock fileLock
           
static String HEADER_MAGIC
           
protected  StoredBlock lastChainHead
           
protected  ReentrantLock lock
           
protected  LinkedHashMap<Sha256Hash,Object> notFoundCache
           
protected static Object notFoundMarker
           
protected  int numHeaders
           
protected  NetworkParameters params
           
protected  RandomAccessFile randomAccessFile
           
protected static int RECORD_SIZE
           
 
Constructor Summary
SPVBlockStore(NetworkParameters params, File file)
          Creates and initializes an SPV block store.
 
Method Summary
 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.
 int getFileSize()
          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 the StoredBlock that represents the top of the chain of greatest total work.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_NUM_HEADERS

public static final int DEFAULT_NUM_HEADERS
The default number of headers that will be stored in the ring buffer.

See Also:
Constant Field Values

HEADER_MAGIC

public static final String HEADER_MAGIC
See Also:
Constant Field Values

buffer

protected volatile MappedByteBuffer buffer

numHeaders

protected int numHeaders

params

protected NetworkParameters params

lock

protected ReentrantLock lock

blockCache

protected LinkedHashMap<Sha256Hash,StoredBlock> blockCache

notFoundMarker

protected static final Object notFoundMarker

notFoundCache

protected LinkedHashMap<Sha256Hash,Object> notFoundCache

fileLock

protected FileLock fileLock

randomAccessFile

protected 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,
                     File file)
              throws BlockStoreException
Creates and initializes an SPV block store. Will create the given file if it's missing. This operation will block on disk.

Throws:
BlockStoreException
Method Detail

getFileSize

public int getFileSize()
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 interface BlockStore
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 interface BlockStore
Throws:
BlockStoreException

getChainHead

public StoredBlock getChainHead()
                         throws BlockStoreException
Description copied from interface: BlockStore
Returns the 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.

Specified by:
getChainHead in interface BlockStore
Throws:
BlockStoreException

setChainHead

public void setChainHead(StoredBlock chainHead)
                  throws BlockStoreException
Description copied from interface: BlockStore
Sets the StoredBlock that represents the top of the chain of greatest total work.

Specified by:
setChainHead in interface BlockStore
Throws:
BlockStoreException

close

public void close()
           throws BlockStoreException
Description copied from interface: BlockStore
Closes the store.

Specified by:
close in interface BlockStore
Throws:
BlockStoreException


Copyright © 2014. All rights reserved.