|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.google.bitcoin.core.BlockChain
public class BlockChain
A BlockChain holds a series of Block
objects, links them together, and knows how to verify that the
chain follows the rules of the NetworkParameters
for this chain.
A BlockChain requires a Wallet
to receive transactions that it finds during the initial download. However,
if you don't care about this, you can just pass in an empty wallet and nothing bad will happen.
A newly constructed BlockChain is empty. To fill it up, use a Peer
object to download the chain from the
network.
Notes
The 'chain' can actually be a tree although in normal operation it can be thought of as a simple list. In such a situation there are multiple stories of the economy competing to become the one true consensus. This can happen naturally when two miners solve a block within a few seconds of each other, or it can happen when the chain is under attack.
A reference to the head block of every chain is stored. If you can reach the genesis block by repeatedly walking through the prevBlock pointers, then we say this is a full chain. If you cannot reach the genesis block we say it is an orphan chain.
Orphan chains can occur when blocks are solved and received during the initial block chain download, or if we connect to a peer that doesn't send us blocks in order.
Field Summary | |
---|---|
protected BlockStore |
blockStore
Keeps a map of block hashes to StoredBlocks. |
protected StoredBlock |
chainHead
Tracks the top of the best known chain. |
protected NetworkParameters |
params
|
protected Wallet |
wallet
|
Constructor Summary | |
---|---|
BlockChain(NetworkParameters params,
Wallet wallet,
BlockStore blockStore)
Constructs a BlockChain connected to the given wallet and store. |
Method Summary | |
---|---|
boolean |
add(Block block)
Processes a received block and tries to add it to the chain. |
StoredBlock |
getChainHead()
Returns the block at the head of the current best chain. |
Block |
getUnconnectedBlock()
Returns the most recent unconnected block or null if there are none. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected BlockStore blockStore
protected StoredBlock chainHead
Following this one down to the genesis block produces the story of the economy from the creation of BitCoin until the present day. The chain head can change if a new set of blocks is received that results in a chain of greater work than the one obtained by following this one down. In that case a reorganize is triggered, potentially invalidating transactions in our wallet.
protected final NetworkParameters params
protected final Wallet wallet
Constructor Detail |
---|
public BlockChain(NetworkParameters params, Wallet wallet, BlockStore blockStore)
Wallet
you can construct
one from scratch, or you can deserialize a saved wallet from disk using Wallet.loadFromFile(java.io.File)
For the store you can use a MemoryBlockStore
if you don't care about saving the downloaded data, or a
BoundedOverheadBlockStore
if you'd like to ensure fast startup the next time you run the program.
Method Detail |
---|
public boolean add(Block block) throws VerificationException, ScriptException
VerificationException
ScriptException
public StoredBlock getChainHead()
public Block getUnconnectedBlock()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |