0% found this document useful (0 votes)
3 views7 pages

Blockchain in Vehicle Networks

The document presents a Java implementation of a blockchain system for vehicular networks, focusing on creating blocks that store data about vehicle incidents. It includes classes for Block, Car, and RSU (Roadside Unit), and demonstrates vehicle-to-vehicle (V2V) and vehicle-to-RSU (V2R) communication. The program simulates an event, updates the blockchain, and showcases how incident information is shared among vehicles and the RSU.

Uploaded by

priyacs104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Blockchain in Vehicle Networks

The document presents a Java implementation of a blockchain system for vehicular networks, focusing on creating blocks that store data about vehicle incidents. It includes classes for Block, Car, and RSU (Roadside Unit), and demonstrates vehicle-to-vehicle (V2V) and vehicle-to-RSU (V2R) communication. The program simulates an event, updates the blockchain, and showcases how incident information is shared among vehicles and the RSU.

Uploaded by

priyacs104
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Blockchain Implementation in Vehicular Networks

// Imported date class so as to get timeStamp


import [Link];

// Imported to make use of random numbers


import [Link].*;

// This is to use SHA-256 algorithm


import [Link];

// Making our Block


class Block
{

// This is the data of our block


private String data;
// This is hash of previous block
public String previousHash;
// This is hash of current block
public String hash;
// This indicates when a block last updated
private long timeStamp;

private double trueness;

// Initializing all our members of Block


public Block(String data, String previousHash, double trueness)
{
[Link] = data;
[Link] = previousHash;
[Link] = new Date().getTime();
[Link]=trueness;
// Hash of current block is calculated as the aggregate of all the
members present in that block
[Link] = current_block_hash_calculate();
}

public double getTruthValue()


{
return trueness;
}

public void setTruthValue(double val)


{
trueness=(trueness+val)/2;
}

// Calculatuing hash of current block


public String current_block_hash_calculate()
{
// Hash of current block is calculated as the aggregate of all the members
present in that block
String current_hash = Sha_256(previousHash + [Link](timeStamp) +
data);
return current_hash;
}
//Applying Sha256 algorithm to input string and getting the hash of that
block
public static String Sha_256(String input)
{
// Wraping it inside try-catch as it is comes under checked exception
try
{
// Taking the Instance of SHA-256
MessageDigest md = [Link]("SHA-256");

// Get our input in bytes


byte[] hash_in_bytes = [Link]([Link]("UTF-8"));

// Representing our hash in hexidecimal


StringBuilder hash_in_hex = new StringBuilder();

for (int i = 0; i < hash_in_bytes.length; i++)


{
// This make sure our input is in Hex form only
String hex = [Link](0xff & hash_in_bytes[i]);
hash_in_hex.append(hex);
}

return hash_in_hex.toString();
}

// catch block
catch(Exception e)
{
throw new RuntimeException(e);
}
}
}

// Making our class


class Car
{
private int Xcoordinate;
private int Ycoordinate;

Car(int x, int y)
{
Xcoordinate=x;
Ycoordinate=y;
}

public int getXcoordinate()


{
return Xcoordinate;
}

public int getYcoordinate()


{
return Ycoordinate;
}

public void setXcoordinate(int x)


{
Xcoordinate=x;
}

public void setYcoordinate(int y)


{
Ycoordinate=y;
}

class RSU
{
private int Xcoordinate;
private int Ycoordinate;
private String message;

RSU(int x, int y, String msg)


{
Xcoordinate=x;
Ycoordinate=y;
message=msg;
}

public int getXcoordinate()


{
return Xcoordinate;
}

public int getYcoordinate()


{
return Ycoordinate;
}

// Driver class
public class Main {

// Driver method
public static void main(String[] args) {

Random rdm = new Random();

double val=[Link]();

int xcord=[Link](101)+200;
int ycord=[Link](101)+200;

double tv1=[Link]();
double tv2=[Link]();
double tv3=[Link]();

String message;

// Making our 1st block


// Since it is our first block so its previous hash should be 0
Block GenesisBlock = new Block("This is our Genesis Block", "0",tv1);
//[Link]("Genesis Block Hash is : " + [Link]);
//[Link]("Hash of block previous to Genesis Block is : "
+null);

//[Link]();

// Making our 2nd block


// Since it is our 2nd block so its previous hash should be hash of genesis
block (Block 1)
Block Block_2 = new Block("This is the 2nd
Block",[Link],tv2);
//[Link]("Block 2 Hash is : " + Block_2.hash);
//[Link]("Hash of block previous to Block 2 (i,e Block 1)
is : " +[Link]);

//[Link]();

// Making our 3rd block


// Since it is our 3rd block so its previous hash should be hash of Block 2
Block Block_3 = new Block("This is the 3rd Block",Block_2.hash,tv3);
//[Link]("Block 3 Hash is : " + Block_3.hash);
//[Link]("Hash of block previous to Block 3 (i,e Block 2)
is : " +Block_2.hash);

Car c1=new Car(xcord,ycord);


Car c2=new Car(xcord-50,ycord);
Car c3=new Car(xcord-110,ycord);

[Link]();
[Link]("-----------------------------Initial Coordinates of
Cars-------------------------");
[Link]();

[Link]("Initial Coordinates of Car 1 are


("+[Link]()+","+[Link]()+")");
[Link]("Initial Coordinates of Car 2 are
("+[Link]()+","+[Link]()+")");
[Link]("Initial Coordinates of Car 3 are
("+[Link]()+","+[Link]()+")");

[Link]();
[Link]("-----------------------------HAPPENING OF
EVENT----------------------------------");
[Link]();
[Link]("EVENT HAPPENED IS: ");

if([Link]()>=0.5 && val>=0.5)


{
message="Accident happened at coordinate ("+xcord+","+ycord+") of Car
1";
[Link](message);
[Link](1);
}

else if([Link]()<0.5 && val>=0.5)


{
message="False Accident message send by Car 1";
[Link](message);
[Link](-1);
return;
}

else if([Link]()>=0.5 && val<0.5)


{
message="Traffic Jam happened at coordinate ("+xcord+","+ycord+") of
Car 1 ";
[Link](message);
[Link](1);
}

else
{
message="False Traffic Jam message send by Car 1";
[Link](message);
[Link](-1);
return;
}

[Link]();

RSU rsu=new RSU(xcord,ycord+50, message);

Car[]arrayofcar=new Car[4];

arrayofcar[1]=c1;
arrayofcar[2]=c2;
arrayofcar[3]=c3;

boolean visited[]=new boolean[4];


visited[1]=true;

[Link]();
[Link]("-----------------------------HAPPENING OF V2V
Communication----------------------");
[Link]();

V2Vcommunication(arrayofcar, visited, 1);

[Link]();
[Link]("-----------------------------HAPPENING OF V2R
Communication----------------------");
[Link]();

[Link]("Coordinates of RSU are ("+[Link]()


+","+[Link]()+")");
V2Rcommunication(arrayofcar,visited, rsu);

[Link]();
[Link]("-----------------------------Final Coordinates of
Cars---------------------------");
[Link]();

[Link]("Final Coordinates of Car 1 are ("+[Link]()


+","+[Link]()+") ###### UNCHANGED");
[Link]("Final Coordinates of Car 2 are ("+[Link]()
+","+[Link]()+") ###### CHANGED");
[Link]("Final Coordinates of Car 3 are ("+[Link]()
+","+[Link]()+") ###### CHANGED");
}

public static void V2Vcommunication(Car[]arrayofcar, boolean[]visited, int


carnumber)
{

if(carnumber==3)
return;

int q=carnumber;
for(;q<[Link]-1;q++)
{
int val1=[Link](arrayofcar[q].getXcoordinate()-
arrayofcar[q+1].getXcoordinate());
int val2=[Link](arrayofcar[q].getYcoordinate()-
arrayofcar[q+1].getYcoordinate());

double reach=[Link]((val1*val1)+(val2*val2));

if(visited[q+1]==false)
{

if(reach<=50)
{
visited[q+1]=true;
[Link]("Incident information passed from Car "+q+" to
Car "+(q+1));

int xcord=arrayofcar[q+1].getXcoordinate();
int ycord=arrayofcar[q+1].getYcoordinate();

V2Vcommunication(arrayofcar, visited, carnumber+1);

[Link]("Car "+(q+1)+" moves to Right from coordinate


("+xcord+","+ycord+") to coordinate ("+(xcord+25)+","+(ycord-25)+")");
arrayofcar[q+1].setXcoordinate(xcord+25);
arrayofcar[q+1].setYcoordinate(ycord-25);

break;
}

else
{
[Link]("Incident information cannot be passed from Car
"+q+ " to Car "+(q+1)+" because Car "+(q+1)+" is not in a range of Car "+q);
[Link]("No other Car is in reach of Car "+q);
return;
}

}
}

[Link]("No other Car is in reach of Car "+(q+2));

public static void V2Rcommunication(Car[]arrayofcar, boolean[]visited, RSU


rsu)
{
[Link]("Incident information passed from Car 1 to RSU ");

for(int q=2;q<[Link];q++)
{
int val1=[Link]([Link]()-
arrayofcar[q].getXcoordinate());
int val2=[Link]([Link]()-
arrayofcar[q].getYcoordinate());

double reach=[Link]((val1*val1)+(val2*val2));

if(reach<=150)
{
if(visited[q]==true)
[Link]("Car "+q+" is in reach of RSU but it already
gets V2V communication from Car 1");

else
{
[Link]("Incident information passed from RSU to Car
"+q);
int xcord=arrayofcar[q].getXcoordinate();
int ycord=arrayofcar[q].getYcoordinate();

[Link]("Car "+q+" moves to Left from coordinate


("+xcord+","+ycord+") to coordinate ("+(xcord+25)+","+(ycord+25)+")");
arrayofcar[q].setXcoordinate(xcord+25);
arrayofcar[q].setYcoordinate(ycord+25);

else
[Link]("Incident information cannot be passed from RSU to
Car "+q+" because Car "+q+" is not in a range of RSU");

[Link]("No other Car is in reach of RSU");


}

You might also like