com.github.mathiewz:java-blockchain

A simple implementation of blokchain using java socket to communicate

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.github.mathiewz
ArtifactId

ArtifactId

java-blockchain
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

com.github.mathiewz:java-blockchain
A simple implementation of blokchain using java socket to communicate
Project URL

Project URL

https://github.com/mathiewz/simple_blockchain
Source Code Management

Source Code Management

https://github.com/mathiewz/simple_blockchain

Download java-blockchain

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.mathiewz/java-blockchain/ -->
<dependency>
    <groupId>com.github.mathiewz</groupId>
    <artifactId>java-blockchain</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.mathiewz/java-blockchain/
implementation 'com.github.mathiewz:java-blockchain:0.0.1'
// https://jarcasting.com/artifacts/com.github.mathiewz/java-blockchain/
implementation ("com.github.mathiewz:java-blockchain:0.0.1")
'com.github.mathiewz:java-blockchain:jar:0.0.1'
<dependency org="com.github.mathiewz" name="java-blockchain" rev="0.0.1">
  <artifact name="java-blockchain" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.mathiewz', module='java-blockchain', version='0.0.1')
)
libraryDependencies += "com.github.mathiewz" % "java-blockchain" % "0.0.1"
[com.github.mathiewz/java-blockchain "0.0.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.apache.commons : commons-lang3 jar 3.6
org.slf4j : slf4j-log4j12 jar 1.7.25

Project Modules

There are no modules declared in this project.

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

Versions

Version
0.0.1