me.edwards.des.block
Class Block

java.lang.Object
  extended by me.edwards.des.block.Block

public class Block
extends java.lang.Object

Data Structure containing information for a Block. The Block is the main unit of storage for the system. Blocks are generated and propagated through Nodes and store a payload of Ballots.

These Ballots are tied to the Block via a Merkle Root, preventing any Ballot in the payload from being changed. The Block also contains the hash of the Block before it in the BlockChain, which allows for backwards-linking in the BlockChain. The Block contains a timestamp and target for mining by a Node.

Every Block is secured using a Proof of Work, preventing changes after the Block is generated. Any change (even a single bit) in a Ballot or Block attribute will change the overall hash of the Block, which will be invalid unless the nonce is changed. The nonce can only be changed to a value which makes the hash valid by re-mining the Block and regenerating a new Proof of Work.

Created on: Nov 1, 2015 at 3:09:17 PM

Author:
Matthew Edwards

Field Summary
private  java.util.ArrayList<Ballot> ballots
           
private  byte[] headerBytes
           
static int MAXIMUM_TARGET
          Maximum Target value for DES
private  java.lang.String merkleRootHash
           
private  byte[] myBytes
           
private  java.lang.String myHash
           
private  int nonce
           
private  java.lang.String prevBlockHash
           
private  int target
           
private  int time
           
private  boolean valid
           
private  int version
           
private  int VERSION
           
 
Constructor Summary
Block(byte[] binary)
          Initializes Block from binary data as a byte array.
Block(java.lang.String prevBlockHash, int target, java.util.ArrayList<Ballot> ballots)
          Creates new Block using the hash from its parent Block, a target (in short-format), and a list of ballots.
 
Method Summary
private  void genBytes()
          Populates headerBytes with this Block's data.
 void genProof()
          Generates a Proof of Work for this block and validates the Block's contents.
 java.util.ArrayList<Ballot> getBallots()
          Returns a list of the Ballots contained by this Block.
 byte[] getBytes()
          Returns the Block in binary format as a byte array.
static double getDifficulty(int target)
          Returns the difficulty of the specified target.

The difficulty is calculated by DIFFICULTY = MAXIMUM_TARGET / SPECIFIED_TARGET

This method uses exponent reduction to more accurately calculate the difficulty of a target.
 java.lang.String getHash()
          Returns the hash of the header for this Block.
private  java.lang.String getMerkleRoot(int depth, int position)
          Generates the Merkle Root of the list of Ballots contained in this Block.
 java.lang.String getPrevHash()
          Returns the hash for this Block's parent.
 int getTarget()
          Returns the target (in Short-Format) of this Block.
static int getTarget(java.math.BigInteger target)
          Returns the specified target in Short-Format.
static java.math.BigInteger getTarget(int target)
          Returns the specified target as a BigInteger.
 int getTime()
          Returns the time when this Block was mined.
 java.lang.String toString()
           
 boolean validate()
          Validates this Block's data and resets the valid flag.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MAXIMUM_TARGET

public static final int MAXIMUM_TARGET
Maximum Target value for DES


VERSION

private final int VERSION
See Also:
Constant Field Values

version

private int version

prevBlockHash

private java.lang.String prevBlockHash

merkleRootHash

private java.lang.String merkleRootHash

time

private int time

target

private int target

nonce

private int nonce

ballots

private java.util.ArrayList<Ballot> ballots

headerBytes

private byte[] headerBytes

myBytes

private byte[] myBytes

myHash

private java.lang.String myHash

valid

private boolean valid
Constructor Detail

Block

public Block(java.lang.String prevBlockHash,
             int target,
             java.util.ArrayList<Ballot> ballots)
Creates new Block using the hash from its parent Block, a target (in short-format), and a list of ballots. The list of ballots must contain at least one element (cannot be empty).

Parameters:
prevBlockHash - 256-bit hash of the previous block in the chain
target - Short-Format target for hashes
ballots - A list of votes to be included in this block

Block

public Block(byte[] binary)
Initializes Block from binary data as a byte array. This constructor is used by Nodes and Blocks to load Blocks.

Parameters:
binary - Byte array representing this Block
Method Detail

getHash

public java.lang.String getHash()
Returns the hash of the header for this Block.

Returns:
32-digit hexadecimal hash of this Block's header

getPrevHash

public java.lang.String getPrevHash()
Returns the hash for this Block's parent.

Returns:
32-digit hexadecimal hash of this Block's parent
See Also:
getHash()

genProof

public void genProof()
              throws java.lang.InterruptedException
Generates a Proof of Work for this block and validates the Block's contents. If the block is already valid, this method will return.

Throws:
java.lang.InterruptedException - Thrown if the Thread is interrupted by Node.stopBlockGeneration().

getMerkleRoot

private java.lang.String getMerkleRoot(int depth,
                                       int position)
Generates the Merkle Root of the list of Ballots contained in this Block. The initial call to this method should be getMerkleRoot(0, 0); and it will recurse to get the root of the entire list.

Parameters:
depth - Depth of the Merkle Tree on this recursive step
position - Index of the Ballot composing the first child of the Merkle Tree node returned by this method
Returns:
32-digit hexadecimal Merkle Root of the two leaves at the given depth and position

genBytes

private void genBytes()
Populates headerBytes with this Block's data. This method is used by genProof() and Block(byte[]).


getBytes

public byte[] getBytes()
Returns the Block in binary format as a byte array.

Returns:
Byte array representing this Block

getBallots

public java.util.ArrayList<Ballot> getBallots()
Returns a list of the Ballots contained by this Block.

Returns:
ArrayList containing the Ballots in the Block

getTime

public int getTime()
Returns the time when this Block was mined.

Returns:
Unix Timestamp adjusted to minutes (instead of milliseconds)

getTarget

public int getTarget()
Returns the target (in Short-Format) of this Block.

Returns:
Short-Format target

validate

public boolean validate()
Validates this Block's data and resets the valid flag.

Returns:
True if this Block is valid, False otherwise
See Also:
HashUtil.validateProof(byte[], int, int)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getTarget

public static java.math.BigInteger getTarget(int target)
Returns the specified target as a BigInteger.

Parameters:
target - Short-Format target
Returns:
Returns a BigInteger represented by the compressed Short-Format target

getTarget

public static int getTarget(java.math.BigInteger target)
Returns the specified target in Short-Format.

Parameters:
target - BigInteger target
Returns:
Returns the Short-Format target representing the expanded BigInteger target

getDifficulty

public static double getDifficulty(int target)
Returns the difficulty of the specified target.

The difficulty is calculated by DIFFICULTY = MAXIMUM_TARGET / SPECIFIED_TARGET

This method uses exponent reduction to more accurately calculate the difficulty of a target.

Parameters:
target - Short-Format target
Returns:
Difficult of the target