com.google.bitcoin.core
Class Block

java.lang.Object
  extended by com.google.bitcoin.core.Message
      extended by com.google.bitcoin.core.Block
All Implemented Interfaces:
Serializable

public class Block
extends Message

A block is the foundation of the BitCoin system. It records a set of Transactions together with some data that links it into a place in the global block chain, and proves that a difficult calculation was done over its contents. See the BitCoin technical paper for more detail on blocks.

To get a block, you can either build one from the raw bytes you can get from another implementation, or request one specifically using Peer.getBlock(byte[]), or grab one from a downloaded BlockChain.

See Also:
Serialized Form

Field Summary
static int HEADER_SIZE
          How many bytes are required to represent a block header.
 
Fields inherited from class com.google.bitcoin.core.Message
bytes, cursor, MAX_SIZE, offset, params, protocolVersion
 
Constructor Summary
Block(NetworkParameters params, byte[] payloadBytes)
          Constructs a block object from the BitCoin wire format.
 
Method Summary
 Block cloneAsHeader()
          Returns a copy of the block, but without any transactions.
 boolean equals(Object o)
           
 long getDifficultyTarget()
          Returns the difficulty of the proof of work that this block should meet encoded in compact form.
 BigInteger getDifficultyTargetAsInteger()
          Returns the difficulty target as a 256 bit value that can be compared to a SHA-256 hash.
 byte[] getHash()
          Returns the hash of the block (which for a valid, solved block should be below the target).
 String getHashAsString()
          Returns the hash of the block (which for a valid, solved block should be below the target) in the form seen on the block explorer.
 byte[] getMerkleRoot()
          Returns the merkle root in big endian form, calculating it from transactions if necessary.
 long getNonce()
          Returns the nonce, an arbitrary value that exists only to make the hash of the block header fall below the difficulty target.
 byte[] getPrevBlockHash()
          Returns the hash of the previous block in the chain, as defined by the block header.
 long getTime()
          Returns the time at which the block was solved and broadcast, according to the clock of the solving node.
 long getVersion()
          Returns the version of the block data structure as defined by the BitCoin protocol.
 BigInteger getWork()
          Returns the work represented by this block.
 int hashCode()
           
 String toString()
          Returns a multi-line string containing a description of the contents of the block.
 void verify()
          Checks the block data to ensure it follows the rules laid out in the network parameters.
 
Methods inherited from class com.google.bitcoin.core.Message
bitcoinSerialize
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

HEADER_SIZE

public static final int HEADER_SIZE
How many bytes are required to represent a block header.

See Also:
Constant Field Values
Constructor Detail

Block

public Block(NetworkParameters params,
             byte[] payloadBytes)
      throws ProtocolException
Constructs a block object from the BitCoin wire format.

Throws:
ProtocolException
Method Detail

getHashAsString

public String getHashAsString()
Returns the hash of the block (which for a valid, solved block should be below the target) in the form seen on the block explorer. If you call this on block 1 in the production chain, you will get "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048".


getHash

public byte[] getHash()
Returns the hash of the block (which for a valid, solved block should be below the target). Big endian.


getWork

public BigInteger getWork()
                   throws VerificationException
Returns the work represented by this block.

Work is defined as the number of tries needed to solve a block in the average case. Consider a difficulty target that covers 5% of all possible hash values. Then the work of the block will be 20. As the target gets lower, the amount of work goes up.

Throws:
VerificationException

cloneAsHeader

public Block cloneAsHeader()
Returns a copy of the block, but without any transactions.


toString

public String toString()
Returns a multi-line string containing a description of the contents of the block. Use for debugging purposes only.

Overrides:
toString in class Object

getDifficultyTargetAsInteger

public BigInteger getDifficultyTargetAsInteger()
                                        throws VerificationException
Returns the difficulty target as a 256 bit value that can be compared to a SHA-256 hash. Inside a block the target is represented using a compact form. If this form decodes to a value that is out of bounds, an exception is thrown.

Throws:
VerificationException

verify

public void verify()
            throws VerificationException
Checks the block data to ensure it follows the rules laid out in the network parameters. Specifically, throws an exception if the proof of work is invalid, if the timestamp is too far from what it should be, or if the transactions don't hash to the value in the merkle root field. This is not everything that is required for a block to be valid, only what is checkable independent of the chain.

Throws:
VerificationException

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

getMerkleRoot

public byte[] getMerkleRoot()
Returns the merkle root in big endian form, calculating it from transactions if necessary.


getVersion

public long getVersion()
Returns the version of the block data structure as defined by the BitCoin protocol.


getPrevBlockHash

public byte[] getPrevBlockHash()
Returns the hash of the previous block in the chain, as defined by the block header.


getTime

public long getTime()
Returns the time at which the block was solved and broadcast, according to the clock of the solving node.


getDifficultyTarget

public long getDifficultyTarget()
Returns the difficulty of the proof of work that this block should meet encoded in compact form. The BlockChain verifies that this is not too easy by looking at the length of the chain when the block is added. To find the actual value the hash should be compared against, use getDifficultyTargetBI.


getNonce

public long getNonce()
Returns the nonce, an arbitrary value that exists only to make the hash of the block header fall below the difficulty target.



Copyright © 2011. All Rights Reserved.