0% found this document useful (0 votes)
2 views3 pages

Practical No 5

The document outlines the creation of a block in a blockchain, detailing its structure, creation process, and importance in maintaining integrity, security, and decentralization. It includes a Java code implementation for a Block class that encapsulates the properties and methods necessary for block creation and mining. The conclusion confirms the successful development of a block creation program.

Uploaded by

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

Practical No 5

The document outlines the creation of a block in a blockchain, detailing its structure, creation process, and importance in maintaining integrity, security, and decentralization. It includes a Java code implementation for a Block class that encapsulates the properties and methods necessary for block creation and mining. The conclusion confirms the successful development of a block creation program.

Uploaded by

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

Practical No -05

AIM- Write a program for Creation of Block


Theory
Blocks are data structures within the blockchain database, where transaction data in a cryptocurrency
blockchain are permanently recorded. A block records some or all of the most recent transactions not yet
validated by the network. Once the data are validated, the block is closed. Then, a new block is created for new
transactions to be entered into and validated. Blocks are created when miners or block validators successfully
validate the encrypted information in the blockheader, which prompts the creation of a new block.
A block is a fundamental component of a blockchain. It acts as a container for data, specifically transaction data
in the case of cryptocurrencies like Bitcoin. Each block is part of a larger, immutable, distributed digital ledger
— the blockchain — which ensures security, transparency, and trust in decentralized systems.
Structure of a Block:
A typical block contains the following elements:
Index – The position of the block in the chain.
Timestamp – When the block was created.
Data – The transactions or information being stored.
Previous Hash – A hash value linking to the previous block, creating the chain.
Hash – A cryptographic fingerprint of the block's content.
Nonce – A variable used in the proof-of-work algorithm to find a valid hash.
Block Creation Process:
Blocks are created through a process known as mining (in Proof-of-Work systems) or validation (in other
consensus mechanisms like Proof-of-Stake). Here’s how it works:
Transaction Pool: Unconfirmed transactions are collected in a pool (also called the memory pool).
Block Assembly: A miner selects a group of valid transactions and packages them into a block.
Hash Calculation: The miner tries to find a valid hash for the block by adjusting the nonce. This is done to meet
a difficulty target — usually a hash that starts with a specific number of zeroes.
Proof of Work: This process requires computational effort and is called proof of work. It's hard to do but easy to
verify.
Block Validation: Once a valid hash is found, the block is broadcast to the network.
Block Finalization: Other nodes verify the hash, and if it's valid, the block is added to the blockchain. A new
block is then started to continue recording new transactions.
Importance of Blocks in Blockchain:
Integrity: Each block’s hash depends on its content and the previous hash, making it tamper-evident.
Security: Altering any data in a block changes its hash, breaking the chain unless all subsequent blocks are
recalculated.
Decentralization: Every node in the network keeps a copy of the blockchain, and consensus protocols ensure
agreement on the correct state. Real-World Application:
In cryptocurrencies, blocks hold transaction data, but in other blockchain applications, they may store smart
contracts, healthcare records, supply chain logs, or any type of secure data.

CODE
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

class Block {
private int index;
private long timestamp;
private String previousHash;
private String hash;
private String data;
private int nonce;

public Block(int index, String previousHash, String data) {


this.index = index;
this.timestamp = new Date().getTime();
this.previousHash = previousHash;
this.data = data;
this.nonce = 0;
this.hash = calculateHash();
}

public String calculateHash() {


try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
String input = index + Long.toString(timestamp) + previousHash + data + nonce;
byte[] hashBytes = digest.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte hashByte : hashBytes) {
String hex = Integer.toHexString(0xff & hashByte);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}

public void mineBlock(int difficulty) {


String target = new String(new char[difficulty]).replace('\0', '0');
while (!hash.substring(0, difficulty).equals(target)) {
nonce++;
hash = calculateHash();
}
System.out.println("Block mined: " + hash);
}

public int getIndex() {


return index;
}

public long getTimestamp() {


return timestamp;
}
public String getPreviousHash() {
return previousHash;
}

public String getHash() {


return hash;
}

public String getData() {


return data;
}
}

public class Main {


public static void main(String[] args) {
Block block = new Block(1,
"0000000000000000000000000000000000000000000000000000000000000000", "Hello Blockchain");
System.out.println("Mining block...");
block.mineBlock(4); // Adjust difficulty as needed

System.out.println("Block index: " + block.getIndex());


System.out.println("Timestamp: " + block.getTimestamp());
System.out.println("Previous Hash: " + block.getPreviousHash());
System.out.println("Hash: " + block.getHash());
System.out.println("Data: " + block.getData());
}
}

OUTPUT

CONCLUSION- Hence, successfully wrote a program to create a block.

You might also like