22SS02IT094
Pari Dhaduk
Subject code:-SSCS3021
Subject Name:- Blockchain Technology
Title: Developing a Simple Blockchain Prototype using Java/C Language
Objective:
The aim of this assignment is to introduce the concept of blockchain technology to beginners by
developing a simple blockchain system using either Java or C language. This project will help
students understand how blockchain works, including blocks, cryptography, and the concept of
immutability.
Learning Outcomes:
By completing this project, students will:
1. Gain a basic understanding of blockchain technology.
2. Learn how to create and link blocks in a chain.
3. Implement hash functions for block identification.
4. Understand proof of work (PoW) as a consensus mechanism.
5. Develop coding skills in Java or C with a focus on object-oriented programming (Java) or
structured programming (C).
Part 1: Introduction to Blockchain
Before beginning with the code, provide a brief overview of blockchain technology:
- Blockchain is a decentralized and distributed digital ledger that stores data in blocks. Each
block contains a cryptographic hash of the previous block, a timestamp, and transaction data.
- Key concepts:
- Blocks: Data units that are linked together.
22SS02IT094
Pari Dhaduk
- Hashing: The process of generating a fixed-length code from data.
- Proof of Work (PoW): A consensus mechanism to secure the network.
Part 2: Project Structure
1. Project Title: "Simple Blockchain in Java/C"
2. Prerequisites:
- Basic knowledge of programming in Java or C.
- Understanding of basic cryptographic hash functions (e.g., SHA-256).
Part 3: Project Steps
Step 1: Setting up the Environment
- For Java: Use any IDE such as Eclipse, IntelliJ, or VSCode.
- For C: Use an IDE like Code::Blocks or GCC compiler.
- Install necessary libraries (if applicable), for example, for hashing in C, include the OpenSSL
library.
Step 2: Design the Block Structure
The block is the basic unit of a blockchain. The structure of a block will include:
- Block Number: The position of the block in the chain.
- Timestamp: The creation time of the block.
- Transaction Data: Any data (e.g., a string) that represents a transaction.
- Previous Hash: The hash of the previous block, which links the chain.
- Current Hash: The hash of the current block.
BLOCK CODE:-
import java.nio.charset.StandardCharsets; import
java.security.MessageDigest; import
java.util.Date;
22SS02IT094
Pari Dhaduk
class Block { public int index;
public long timestamp; public
String data; public String
previousHash; public String
currentHash;
// Block constructor
public Block(int index, String data, String previousHash) {
this.index = index;
this.timestamp = new Date().getTime(); this.data
= data;
this.previousHash = previousHash; this.currentHash
= calculateHash();
}
// Calculate the SHA-256 hash of the block public
String calculateHash() {
String input = index + Long.toString(timestamp) + data + previousHash; return
applySha256(input);
}
// Applies SHA-256 to the input string and returns the hash
public static String applySha256(String input) { try {
MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash =
digest.digest(input.getBytes(StandardCharsets.UTF_8)); StringBuilder hexString = new
StringBuilder(); for (byte b : hash) {
22SS02IT094
Pari Dhaduk
String hex = Integer.toHexString(0xff & b); if
(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Step 3: Implement the Blockchain
The blockchain is a series of blocks linked together. Implement a chain that will add new blocks
and verify that the chain remains intact by checking the hashes. Step 4: Proof of Work
(PoW)
Add a simple Proof of Work algorithm that requires solving a hash puzzle (e.g., finding a hash
with a certain number of leading zeroes).
Step 5: Test the Blockchain
Write test cases to ensure that:
- New blocks can be added.
- The blockchain validates correctly.
- The immutability of the blockchain is maintained.
BLOCKCHAIN CODE:-
import java.util.ArrayList;
class Blockchain { public
ArrayList<Block> chain;
22SS02IT094
Pari Dhaduk
// Constructor initializes blockchain with the Genesis block
public Blockchain() { chain = new ArrayList<>();
chain.add(generateGenesisBlock());
}
// Genesis block (first block) public Block
generateGenesisBlock() { return new Block(0,
"Genesis Block", "0");
}
// Get the latest block in the chain
public Block getLatestBlock() { return
chain.get(chain.size() - 1);
}
// Add a new block to the chain public
void addBlock(Block newBlock) {
newBlock.previousHash = getLatestBlock().currentHash;
newBlock.currentHash = newBlock.calculateHash(); chain.add(newBlock);
}
// Validate the blockchain by checking the integrity of hashes public boolean isChainValid()
{ for (int i = 1; i < chain.size(); i++) {
Block currentBlock = chain.get(i);
Block previousBlock = chain.get(i - 1);
22SS02IT094
Pari Dhaduk
// Check current block's hash
if (!currentBlock.currentHash.equals(currentBlock.calculateHash())) {
return false;
}
// Check previous block's hash
if (!currentBlock.previousHash.equals(previousBlock.currentHash)) {
return false;
} }
return true;
}
}
MAIN CODE:-
public class Main {
public static void main(String[] args) {
Blockchain myBlockchain = new Blockchain();
// Adding blocks
System.out.println("Mining block 1..."); myBlockchain.addBlock(new
Block(1, "Block 1 Data", myBlockchain.getLatestBlock().currentHash));
System.out.println("Mining block 2..."); myBlockchain.addBlock(new
Block(2, "Block 2 Data", myBlockchain.getLatestBlock().currentHash));
System.out.println("Is blockchain valid? " + myBlockchain.isChainValid());
// Print the blockchain
for (Block block : myBlockchain.chain) {
System.out.println("Block #" + block.index);
22SS02IT094
Pari Dhaduk
System.out.println("Data: " + block.data);
System.out.println("Hash: " + block.currentHash);
System.out.println("Previous Hash: " + block.previousHash);
System.out.println();
}
}
}
OUTPUT :-
Part 4: Conclusion
This project involved the development of a straightforward blockchain prototype
using Java or C. The primary goal was to create a functioning model that
encapsulates the fundamental principles of blockchain technology.
Block Creation: Each block serves as a unit of data storage, encapsulating
essential information such as data, timestamps, and cryptographic hashes to
establish connections between blocks.
22SS02IT094
Pari Dhaduk
Data Integrity: By employing hash functions, we ensured that each block's
content remains intact and unaltered, fostering trust in the system's accuracy and
reliability.
Mining Simulation: A basic mechanism was integrated to represent the process of
block generation, illustrating how computational tasks can contribute to network
security.
Validation Mechanism: A validation function was established to verify the
integrity of the entire blockchain, ensuring that all data remains consistent and
trustworthy.
Through this exercise, we gained hands-on experience with programming
concepts while gaining insights into the foundational aspects of blockchain
technology, preparing us for more complex