Skip to content

A simple implementation of blokchain using java socket to communicate

License

Notifications You must be signed in to change notification settings

mathiewz/simple_blockchain

Repository files navigation

simple_blockchain Build Status

A simple implementation of blockchain using java socket to communicate The blocks can contain any Object implementing Serializable as data.

Communication

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.

Usage

create new node

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);

Get the data of a block

Block<MyDataObject> block = node.getBlockChain();
MyDataObject data = block.getData();

Get latest block

Block<MyDataObject> latest = node.getBlockChain();

Add new Block to the chain

MyDataObject data = new MyDataObject();
node.addBlock(data);

Iterate through whole block chain

All of the next cases iterate through the block sorted by creation date

orEach

for(Block<MyDataObject> block : node.getBlockChain()) {
        //Do some stuff
}

Iterator

Iterator<Block<MyDataObject>> itr = node.getBlockChain().iterator();
while (itr.hasNext()) {
    Block<MyDataObject> block = itr.next();
    //Do some stuff            
}

Java 8 Stream API

node.getBlockChain().stream().forEach(block -> {
   //Do somme stuff 
});

Check the validity of a block

Block<MyDataObject> block = node.getBlockChain();
block.isValid();

Check the validity of the chain

Block<MyDataObject> block = node.getBlockChain();
block.isWholeChainValid();

Javadoc

Javadoc can be find here

About

A simple implementation of blokchain using java socket to communicate

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages