A simple implementation of blockchain using java socket to communicate The blocks can contain any Object implementing Serializable as data.
All the connections are peer to peer connection using java sockets. Each connection to another node aware of the blockchain will create a new Thread to both JVM connected.
To create a new Node, there are two possibilities :
// Create a new chain and is ready for new connections on port 8080
int listeningPort = 8080;
MyDataObject data = new MyDataObject();
Node<MyDataObject> node = new Node<>(listeningPort, new Block<>(data));
OR
//Connect to 192.168.0.1:8080, get the current chain and is ready for new connections on port 8080
int listeningPort = 8080;
String remoteHost = "192.168.0.1";
int remotePort = 8080;
Node<MyDataObject> node = new Node<>(listeningPort, remoteHost, remotePort);
Block<MyDataObject> block = node.getBlockChain();
MyDataObject data = block.getData();
Block<MyDataObject> latest = node.getBlockChain();
MyDataObject data = new MyDataObject();
node.addBlock(data);
All of the next cases iterate through the block sorted by creation date
for(Block<MyDataObject> block : node.getBlockChain()) {
//Do some stuff
}
Iterator<Block<MyDataObject>> itr = node.getBlockChain().iterator();
while (itr.hasNext()) {
Block<MyDataObject> block = itr.next();
//Do some stuff
}
node.getBlockChain().stream().forEach(block -> {
//Do somme stuff
});
Block<MyDataObject> block = node.getBlockChain();
block.isValid();
Block<MyDataObject> block = node.getBlockChain();
block.isWholeChainValid();
Javadoc can be find here