0% found this document useful (0 votes)
5 views

Practical Task Verifying Transaction Integrity Using Merkle

This document outlines a practical task for participants to verify transaction integrity using Merkle trees in a blockchain system. It includes steps for setting up the environment, generating transactions, constructing the Merkle tree, and verifying transaction integrity through Merkle proofs. Participants are expected to submit a report detailing their experience, including transaction data, Merkle tree construction, and reflections on the importance of Merkle trees in blockchain integrity.
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)
5 views

Practical Task Verifying Transaction Integrity Using Merkle

This document outlines a practical task for participants to verify transaction integrity using Merkle trees in a blockchain system. It includes steps for setting up the environment, generating transactions, constructing the Merkle tree, and verifying transaction integrity through Merkle proofs. Participants are expected to submit a report detailing their experience, including transaction data, Merkle tree construction, and reflections on the importance of Merkle trees in blockchain integrity.
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/ 4

Practical Task: Verifying Transaction

Integrity Using Merkle Trees in a


Blockchain System
Description
This task will guide participants through the process of verifying transaction integrity using
Merkle trees in a blockchain system. By the end of this task, participants will have a
practical understanding of how Merkle trees are used to ensure the integrity of
transactions in a blockchain.

Step 1: Setting up the Environment

1. Provide participants with a pre-configured blockchain network consisting of nodes,


each representing a participant in the network.
2. Include a client interface for submitting transactions and a visual representation of
the blockchain's structure.

Step 2: Generating Transactions

1. Ask participants to generate a set of random transactions.


2. Each transaction should contain:
a. Some data (e.g., amount, sender, receiver)
b. A unique identifier (e.g., transaction ID)

Step 3: Constructing the Merkle Tree

1. Guide participants through the process of constructing a Merkle tree using the
generated transactions.
2. Explain the concept of a Merkle tree and how it allows for efficient verification of
transaction integrity.
Step 4: Verifying Transaction Integrity

1. Divide participants into pairs:


a. One person acts as a verifier.
b. The other person acts as a prover.
2. Instruct provers to select a transaction from their generated set and provide the
verifier with the transaction data and its unique identifier.

Step 5: Generating Merkle Proofs

1. Once the verifier receives the transaction data, they should request the Merkle
proof from the prover.
2. The prover must generate the necessary Merkle proof for the transaction by
providing the path from the transaction leaf to the Merkle root.

Step 6: Verifying the Merkle Proof

1. The verifier should use the provided Merkle proof to verify the integrity of the
transaction.
2. Guide participants through the verification process, emphasizing how the Merkle
proof ensures that the transaction data has not been tampered with.

Step 7: Rotating Roles

1. After each verification, rotate the roles of the participants, allowing everyone to act
as both verifier and prover.
2. Repeat steps 4 to 6 until all participants have had the opportunity to verify a
transaction's integrity.

Final Outcome
Participants should submit a report summarizing their experience in verifying transaction
integrity using Merkle trees. The report should include:

1. A description of the generated transactions and their unique identifiers.


2. Details of the Merkle tree construction process.
3. Records of the transactions verified by the participant, including:
a. The transaction data
b. Unique identifier
c. Merkle proof
4. Reflection on the importance of Merkle trees in ensuring the integrity of
transactions in a blockchain system.

Additionally, participants should submit any code or scripts they developed during the
task, if applicable.

import hashlib

class MerkleTree: def init(self, transactions): self.transactions = transactions self.tree =


self.build_tree(transactions)

def build_tree(self, transactions):


tree = []
current_level = [self.hash_transaction(tx) for tx in transactions]
tree.append(current_level)

while len(current_level) > 1:


next_level = []
for i in range(0, len(current_level), 2):
left = current_level[i]
right = current_level[i + 1] if i + 1 < len(current_level)
else current_level[i]
combined = self.hash_pair(left, right)
next_level.append(combined)
current_level = next_level
tree.append(current_level)

return tree

def hash_transaction(self, transaction):


return hashlib.sha256(transaction.encode()).hexdigest()

def hash_pair(self, left, right):


return hashlib.sha256((left + right).encode()).hexdigest()

def get_root(self):
return self.tree[-1][0] if self.tree else None
def get_proof(self, transaction):
proof = []
index = self.transactions.index(transaction)
for level in self.tree[:-1]:
pair_index = index ^ 1
if pair_index < len(level):
proof.append(level[pair_index])
index //= 2
return proof

def verify_proof(self, transaction, proof, root):


current_hash = self.hash_transaction(transaction)
for sibling in proof:
if current_hash < sibling:
current_hash = self.hash_pair(current_hash, sibling)
else:
current_hash = self.hash_pair(sibling, current_hash)
return current_hash == root

Example Usage
transactions = ["tx1", "tx2", "tx3", "tx4"] merkle_tree = MerkleTree(transactions)

Prover
transaction = "tx1" proof = merkle_tree.get_proof(transaction) root = merkle_tree.get_root()

Verifier
is_valid = merkle_tree.verify_proof(transaction, proof, root) print(f"Transaction is valid:
{is_valid}")

You might also like