me.edwards.des
Class Node

java.lang.Object
  extended by me.edwards.des.Node

public class Node
extends java.lang.Object

A Miner Node is the most complex autonomous unit that operates on the DES Network. This class handles all of the higher-level processes for a Miner Node, including processing of packets received by the Node, generation of new Blocks, handling of broadcasted Ballots, and validation of data generated by other Nodes.

When this Node is placed on a network with additional Nodes running the same software, they are able to "mine" ballots broadcast by Election Applications using the Decentralized Election System. This is the step in the process that ensures the validity of each individual's vote and causes them to be immutably and permanently inserted into the public BlockChain, preventing corruption in a democratic voting system.

Created on: Oct 16, 2015 at 9:35:54 PM

Author:
Matthew Edwards

Field Summary
protected  java.util.ArrayList<Ballot> ballots
          This Node's private list of all known, unmined ballots.
static int BLOCK_THRESHOLD
          Threshold number of Ballots required to begin mining a Block.
static int BLOCK_TIME
          Minimum amount of time (in seconds) to begin mining a Block
protected  BlockChain blockChain
          This Node's private version of the BlockChain.
private  java.lang.Thread blockGen
          This thread is used to attempt generation of new Blocks (mine a proof of work for a new Block).
protected  java.lang.String blockGenHash
          Hash of the parent of the Block currently being generated by the Node.
private  java.lang.Thread blockGenTimer
          This thread initiates Block generation if the time threshold has been reached.
static int BUFFER_SIZE
          Default Packet Buffer Size
private  java.util.ArrayList<java.lang.String> dataRequests
          A list of the hashes of all the data objects requested by this Node.
protected  boolean demo
          Demonstration mode flag used to indicate if this Node is currently in demonstration mode.
private  java.lang.Thread handshake
          This thread accepts handshakes from new, previously unknown peers on the network.
protected  java.net.InetAddress ip
          This Node's IP Address as an InetAddress instance.
protected  java.util.logging.Logger logger
          Logger instance to be used by this Node.
protected  java.lang.String name
          This Node's human readable name.
protected  java.util.ArrayList<java.lang.String> peerList
          List of initial peers to be contacted during the bootstrapping process.
protected  java.util.ArrayList<Connection> peers
          List of connections containing all of this Node's current peers.
protected  int port
          This Node's current listening socket port.
protected  boolean running
          Running flag used to indicate if this Node is presently accepting and sending packets over connections on the network.
protected  java.net.ServerSocket socket
          This Node's current listening socket as a ServerSocket instance.
static Version VERSION
          Node's software version.
 
Constructor Summary
Node()
           
  Packet TypeProcess CategoryDescription of ProcessPing Connection Status Sends a Pong response to the Ping sender, incrementing the payload by one. Pong Connection Status Notifies the corresponding connection that a response has been received from the peer Node. Version Handshake Validates the version of the sender Node and sends a VerAck.
Method Summary
 void addDataRequest(java.lang.String hash)
          Adds a hash to the data requests list.
 Connection connect(java.net.InetAddress address, int port)
          Connects this Node to a peer Node via the specified IP Address and port.
 void generateBlock()
          Attempts to generate a new Block from a limited number of known, validated Ballots.
 Connection getConnection(java.lang.String hostname)
          Returns the connection with the specified name from the current list of peers of this Node.
 java.util.ArrayList<Connection> getPeers()
          Returns a list of this Node's known peers.
 boolean isRunning()
          Returns the running status of this Node.
 void parse(byte[] data, Connection connection)
          Parses a packet received by a connection to this Node into a Packet Type and processes the received data in the packet.

 void removeConnection(Connection c)
          Disconnects the specified peer and removes its connection from the list of this Node's peers.
 void sendToAll(Packet p)
          Sends the specified packet to all known peers.
 void start()
          Initializes the Node's networking processes and threads, starting the Node on a specific IP Address and Port (specified by the Launcher).
 void stop()
          Stops the Node's running processes, including the Handshake Thread (preventing other Nodes from connecting to this Node) and the Block Generation Thread (stopping the generation of any new Blocks or Blocks currently being generated).
 void stopBlockGeneration()
          Stops any current Block generation by this Node.
private static boolean validateVersion(Version v)
          Checks if the version of another Node is compatible with the version of this Node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static final Version VERSION
Node's software version. Used in version authentication during the handshake process.


BUFFER_SIZE

public static final int BUFFER_SIZE
Default Packet Buffer Size

See Also:
Constant Field Values

BLOCK_THRESHOLD

public static final int BLOCK_THRESHOLD
Threshold number of Ballots required to begin mining a Block. This number is also the maximum number of Ballots allowed in a Block, configurable to each indiviual Node.

See Also:
Constant Field Values

BLOCK_TIME

public static final int BLOCK_TIME
Minimum amount of time (in seconds) to begin mining a Block

See Also:
Constant Field Values

logger

protected java.util.logging.Logger logger
Logger instance to be used by this Node. Initialized during launching process.


peerList

protected java.util.ArrayList<java.lang.String> peerList
List of initial peers to be contacted during the bootstrapping process.


blockChain

protected BlockChain blockChain
This Node's private version of the BlockChain. Used for Block validation and storage.


blockGenHash

protected java.lang.String blockGenHash
Hash of the parent of the Block currently being generated by the Node.


peers

protected java.util.ArrayList<Connection> peers
List of connections containing all of this Node's current peers.


ballots

protected java.util.ArrayList<Ballot> ballots
This Node's private list of all known, unmined ballots. Ballots inserted into new Blocks are copied from this list as new Blocks are generated.


ip

protected java.net.InetAddress ip
This Node's IP Address as an InetAddress instance.


socket

protected java.net.ServerSocket socket
This Node's current listening socket as a ServerSocket instance.


port

protected int port
This Node's current listening socket port. Used for manual connections.


name

protected java.lang.String name
This Node's human readable name.


demo

protected boolean demo
Demonstration mode flag used to indicate if this Node is currently in demonstration mode. This will disable and change some functionality.


running

protected boolean running
Running flag used to indicate if this Node is presently accepting and sending packets over connections on the network. True if this Node is 'on' (able to communicate with other Nodes), False if this Node is 'off' (unable to communicate with other Nodes).


dataRequests

private java.util.ArrayList<java.lang.String> dataRequests
A list of the hashes of all the data objects requested by this Node. This prevents unsolicited data from being accepted by the Node.


handshake

private java.lang.Thread handshake
This thread accepts handshakes from new, previously unknown peers on the network.


blockGenTimer

private java.lang.Thread blockGenTimer
This thread initiates Block generation if the time threshold has been reached.


blockGen

private java.lang.Thread blockGen
This thread is used to attempt generation of new Blocks (mine a proof of work for a new Block). As this process is very time and processor intensive, only one Block can be generated at any given time. This thread is interrupted if a Block is received and has the same parent as the Block currently being generated.

Constructor Detail

Node

public Node()
Method Detail

start

public void start()
Initializes the Node's networking processes and threads, starting the Node on a specific IP Address and Port (specified by the Launcher). This method creates and starts the Handshake Thread, allowing other Nodes to connect to this Node. This method will also go through the "Peer List" and attempt to connect to any initial peers, allowing the bootstrapping process to take place. After this method is completed, the running flag should be set to True.


stop

public void stop()
Stops the Node's running processes, including the Handshake Thread (preventing other Nodes from connecting to this Node) and the Block Generation Thread (stopping the generation of any new Blocks or Blocks currently being generated). This method also closes all networking sockets currently open. After this method is completed, the running flag should be set to False.


parse

public void parse(byte[] data,
                  Connection connection)
Parses a packet received by a connection to this Node into a Packet Type and processes the received data in the packet.

Packet Type Process Category Description of Process
Ping Connection Status Sends a Pong response to the Ping sender, incrementing the payload by one.
Pong Connection Status Notifies the corresponding connection that a response has been received from the peer Node.
Version Handshake Validates the version of the sender Node and sends a VerAck. Updates the connection status and requests Ballot, Block, and Address Cache data from the sender Node. Reception completes the Handshake for the Node requesting the connection initially.
VerAck Handshake Updates the connection status and requests Ballot, Block, and Address Cache data from the sender Node. Reception completes the Handshake for the Node that did not request the connection initially.
GetAddr Bootstrapping Sends the requesting Node a copy of all of this Node's current peers.
Addr Bootstrapping Parses address cache information and attempts to connect to previously unknown peers.
Inv Inventory Transfer Sends requests using a GetData packet to the advertising Node for data objects that this Node does not possess.
NotFound Inventory Transfer Notifies the Node that a requested resources was not found.
GetData Inventory Transfer Sends requested data objects to the requesting Node using Ballot data packets or Block data packets.
Ballot Inventory Transfer Validates Ballot and adds it to this Node's list of known Ballots. If this Ballot was not requested, or is invalid, it is discarded. A valid Ballot is propagated to all known peer Nodes with Inventory Packets.
Block Inventory Transfer Validates Block and adds it to this Node's BlockChain. If this Block was not requested, or is invalid, it is discarded. A valid Ballot is propagated to all known peer Nodes with Inventory Packets.
GetBlocks Inventory Transfer Sends Inventory packet containing all Blocks after the Block specified in the GetBlocks request.

Parameters:
data - Packet data received by the connection as a byte array.
connection - Connection object through which the packet was received.
See Also:
Packet, PacketAddr, PacketBallot, PacketBlock, PacketGetAddr, PacketGetBlocks, PacketGetData, PacketInv, PacketNotFound, PacketPing, PacketPong, PacketVerack, PacketVersion, Connection

connect

public Connection connect(java.net.InetAddress address,
                          int port)
Connects this Node to a peer Node via the specified IP Address and port.

Parameters:
address - IP Address of the peer Node
port - Port of the peer Node
Returns:
Returns a connection object if the connection to the peer was successful. If the connection was unsuccessful, null is returned.

removeConnection

public void removeConnection(Connection c)
Disconnects the specified peer and removes its connection from the list of this Node's peers.

Parameters:
c - Connection of the peer to remove
See Also:
Connection

getConnection

public Connection getConnection(java.lang.String hostname)
Returns the connection with the specified name from the current list of peers of this Node.

Parameters:
hostname - Name of this connection in the format "/ipaddress:port", or the human readable name of the peer.
Returns:
Connection with the corresponding host name specified if one exists, otherwise returns null.
See Also:
Connection, Connection.getHostName()

sendToAll

public void sendToAll(Packet p)
Sends the specified packet to all known peers.

Parameters:
p - Packet to send to all known peers.
See Also:
Packet, Connection

isRunning

public boolean isRunning()
Returns the running status of this Node. (The running flag)

Returns:
True if this Node is currently running, False otherwise.

getPeers

public java.util.ArrayList<Connection> getPeers()
Returns a list of this Node's known peers.

Returns:
An ArrayList containing all of this Node's known peers.

generateBlock

public void generateBlock()
Attempts to generate a new Block from a limited number of known, validated Ballots. If a Block is generated successfully, it is broadcast to all peer Nodes. If the Block cannot be generated before a valid Block with the same parent is received, the Block Generation Thread initialized by this method is interrupted and stopped.

This method only allows one Block to be generated at any given time, so this method will return if called while a Block is currently being generated. Instead, the Stop Block Generation method must be called first, and then this method.


stopBlockGeneration

public void stopBlockGeneration()
Stops any current Block generation by this Node. Interrupts the Block Generation Thread and resets the Block Generation Hash variable.


addDataRequest

public void addDataRequest(java.lang.String hash)
Adds a hash to the data requests list.

Parameters:
hash - Hash to add to the data requests list

validateVersion

private static boolean validateVersion(Version v)
Checks if the version of another Node is compatible with the version of this Node.

Parameters:
v - Version to compare with this Node's version.
Returns:
True if the two versions are compatible, False otherwise.