Blockchain Lab Manual
Blockchain Lab Manual
DEPARTMENT OF
COMPUTER ENGINEERING
LABORATORY MANUAL
Semester-VII
Subject In - Charge
PROF. ARCHANA MATE
Practical List
Merkle trees, also known as Binary hash trees, are a prevalent sort of data structure in
computer science.
In bitcoin and other crypto currencies, they're used to encrypt blockchain data more
efficiently and securely.
It's a mathematical data structure made up of hashes of various data blocks that summarize all
the transactions in a block.
It also enables quick and secure content verification across big datasets and verifies the
consistency and content of the data.
Merkle Tree Structure
A hash function maps any type of arbitrary data of any length to a fixed-size output. It is
commonly used in cryptography since it is a cryptographic function.
They are efficient and are well-known for one property: they are irreversible. It's a one-way
function that's only meant to work in one direction.
Some of the Hash families available are Message Direct (MD), Secure Hash Function (SHF),
and RIPE Message Direct (RIPEMD).
Now, take an example, if you use the SHA256 hash algorithm and pass 101Blockchains as input,
you will get the following output.
fbffd63a60374a31aa9811cbc80b577e23925a5874e86a17f712bab874f33ac9
In conclusion, these are the following key properties of the hash function:
Deterministic
Pre-Image Resistant
Computationally Efficient
Collision Resistant
A Merkle tree totals all transactions in a block and generates a digital fingerprint of the entire set
of operations, allowing the user to verify whether it includes a transaction in the block.
Merkle trees are made by hashing pairs of nodes repeatedly until only one hash remains; this
hash is known as the Merkle Root or the Root Hash.
They're built from the bottom, using Transaction IDs, which are hashes of individual
transactions.
Each non-leaf node is a hash of its previous hash, and every leaf node is a hash of
transactional data.
Now, look at a little example of a Merkle Tree in Blockchain to help you understand the
concept.
Consider the following scenario: A, B, C, and D are four transactions, all executed on the same
block. Each transaction is then hashed, leaving you with:
Hash A
Hash B
Hash C
Hash D
Hash AB
and
Hash CD
And therefore, your Merkle Root is formed by combining these two hashes: Hash ABCD.
In reality, a Merkle Tree is much more complicated (especially when each transaction ID is 64
characters long). Still, this example helps you have a good overview of how the algorithms work
and why they are so effective.
Validate the data's integrity: It can be used to validate the data's integrity effectively.
Takes little disk space: Compared to other data structures, the Merkle tree takes up very little
disk space.
Tiny information across networks: Merkle trees can be broken down into small pieces of data
for verification.
Efficient Verification: The data format is efficient, and verifying the data's integrity takes
only a few moments.
Think of a blockchain without Merkle Trees to get a sense of how vital they are for blockchain
technology. Let’s have one of Bitcoin because its use of Merkle Trees is essential for the
cryptocurrency and easier to grasp.
If Bitcoin didn't include Merkle Trees, per se, every node on the network would have to retain
a complete copy of every single Bitcoin transaction ever made. One can imagine how much
information that would be.
To confirm that there were no modifications, a computer used for validation would need a lot
of computing power to compare ledgers.
Merkle Trees are a solution to this issue. They hash records in accounting, thereby separating the
proof of data from the data itself.
Proving that giving tiny amounts of information across the network is all that is required for a
transaction to be valid.
Furthermore, it enables you to demonstrate that both ledger variations are identical in terms of
nominal computer power and network bandwidth.
EXPERIMENT NO: 2
Name of the Student:
Roll No.
Date of Practical Performed: Staff Signature with Date
& Marks
Theory:
Remix IDE
Remix IDE, is a no-setup tool with a GUI for developing smart contracts. Used by experts and
beginners alike, Remix will get you going in double time. Remix plays well with other tools and
allows for a simple deployment process to the chain of your choice. Remix is famous for our
visual debugger. Remix is the place everyone comes to learn Ethereum.
1.1 — The first step is setting up the Remix IDE. Remix IDE is a browser-based development
environment for Smart Contracts. The IDE provides several plugins and compilers for different
solidity versions.
To launch the Remix IDE, visit http://remix.ethereum.org; you will see the page below, which
Notice that in the image above, you can see “Featured Plugins” because the Remix IDE is built
with a pluggable architecture, which means that all the operations are performed using plugins.
1.2 — The next thing to do is to enable the Solidity compiler plugins, as shown below.
Enable Plugins
1c — After enabling the plugin, click on the “Solidity compiler” button, and select the compiler
Compiler Configuration
It is noteworthy to mention that compiler versions in Solidity are tricky to work with, and the
advice is that you should try not to work with the most recent version. Interestingly, you will find
some legacy projects still running on older solidity versions, and this is okay. In summary, try to
stick with the version you see in a particular tutorial if you are a beginner.
In the image above, you will see that we are working with compiler version 0.8.1, which might
not be the recent version. Also, you can set the compiler version in the Solidity source file by
using the “pragma” line at the beginning of the file. The Pragma line is just a way of telling the
Now that everything is all set let’s write our first smart contract in the next step.
Like any other programming language, the first step is creating a project file for the language.
Here, you will create a file titled, “MyFirstSmartContract.sol”. The “sol” file extension stands for
Solidity, just like “py” is for Python.
See the images below to create this file in the Remix IDE.
& Marks
Theory:
Solidity:
Solidity is a brand-new programming language created by the Ethereum which is the second-
largest market of cryptocurrency by capitalization, released in the year 2015 led by Christian
Reitwiessner. Some key features of solidity are listed below:
Ethereum
Ethereum is a decentralized open-source platform based on blockchain domain, used to run smart
contracts i.e. applications that execute the program exactly as it was programmed without the
possibility of any fraud, interference from a third party, censorship, or downtime. It serves a
platform for nearly 2,60,000 different cryptocurrencies. Ether is a cryptocurrency generated by
ethereum miners, used to reward for the computations performed to secure the blockchain.
// Solidity program to
// demonstrate how to
// write a smart contract
pragma solidity >= 0.4.16 < 0.7.0;
// Defining a contract
contract Test
{
// Defining function to
// print the sum of
// state variables
function get(
) public view returns (uint) {
return sum;
}
}
Output
EXPERIMENT NO: 4
Name of the Student:
Roll No.
There are several Ethereum node clients. A popular one is go-ethereum, a client written in Go.
Download it and install it.
Ethereum, as other cryptocurrencies do, has different versions of the blockchain with different
parameters. There are essentially two blockchains: the main official blockchain and a test
blockchain. The main blockchain never undoes operations once they are confirmed. Since some
operations require money, the main blockchain is not ideal for testing. The test blockchain, on
the other hand, is much less strict about forks and changes. It is also simpler to mine "Ether",
Ethereum's currency.
We could use the test network for our example here. However, running a client node for any of
the public networks is problematic for one reason: to be able to start doing transactions, the client
must first verify all previous transactions in the blockchain. That means that bootstrapping a new
client node takes quite a bit of time. Fortunately, there is an alternative: we can create a new,
pristine private Ethereum blockchain to run our tests. To do so, run go-ethereum using the
following command line:
The geth command can also be used to interact with a running client. Launch an interactive
console connected to the running client:
/geth attach
ipc:/var/folders/ts/7xznj_p13xb7_5th3w6yjmjm0000gn/T/ethereum_dev_mode/geth.ipc
The IPC file mentioned in the command can be found in the output from running the node in our
first step. Look for the line that reads:
personal.newAccount()
After hitting ENTER a prompt will appear requesting a passphrase. This is the passphrase that
will be used to perform any operations using this account. You can think of this as the passphrase
required to decrypt the private-key used to sign Ethereum transactions. Do not leave the prompt
empty, choose a simple passphrase for testing instead. A new Ethereum address will be returned
by the function. If at any point you forget this address, you can list accounts by
inspecting personal.listAccounts (it's a variable, not a function, so don't add () at the end).
Now it's time to add some Ether to our new account. Ether is required to perform operations in
the Ethereum blockchain, so it is necessary to perform this step. Ether can be gathered in two
ways: by receiving it from another account or by mining it. Since this is a private network, we
will need to mine it. Don't worry, the private network is by default configured to be able to mine
Ether easily. Let's do it:
Now wait a few seconds (or minutes depending on your hardware) and then confirm you have
some Ether in your account:
To simplify the process of compiling and deploying contracts, we will use truffle. Truffle is a
development framework for Ethereum, simplifying many common tasks. Install it:
Before using truffle to deploy contracts, it is necessary to "unlock" our account in our Ethereum
node client. Unlocking is the process of decrypting the private-key and holding it in memory
using the passphrase used to create it. This allows any client libraries (such as Truffle)
connecting to the node to make operations on behalf of the unlocked account. Go to
the geth console and type:
Now switch to the solidity directory of our sample application. Edit the truffle.js file and set your
newly created address as the from key. Then run:
truffle migrate
The migrate command compiles and deploys the contracts to the Ethereum network on behalf of
the account set in truffle.js. As a result you will get the address of the newly deployed contract.
Take note of it.
Ethereum wallets are convenient interfaces for users to interact with the Ethereum network.
Sending and receiving Ether, deploying contracts or making calls to them are all operations
usually supported by wallets. Mist is the official Ethereum wallet. Download it and install it.
Once installed, we will need to tell Mist to connect to our private network rather than the public
main or test networks. To do this, run Mist from the command line like so:
The IPC file is the same file used by the geth console and can be gathered from the geth output
logs.
Many contracts live in the Ethereum network. Wallets need to know a contract's address and
interface before being able to interact with them. Let's tell Mist about our Login contract. Go
to Contracts -> Watch Contract (top right, then bottom left).
Complete the fields as follows:
Name: Login
Contract Address:
JSON Interface: the abi from Login.json. For convenience it is pasted below. Copy and
paste it in Mist.
As a test, now try to send some Ether to the contract: Contracts -> Login -> Transfer
Ether & Tokens. Send 1 Ether or any other amount less than your balance. You will need to
provide the passphrase for your account.
7. Deploy the backend.
npm install
node app.js
You may use any other simple static HTTP server such as Python's Simple HTTP Server. If you
do so, make sure to serve the app in port 9080. This is important due to CORS.
Open your browser at http://localhost:9080. Now attempt to login by putting your Ethereum
address in the input field. A challenge text will be generated. Go to the Mist (Ethereum Wallet)
and go to the Login contract. To the right you will see "WRITE TO CONTRACT". Select
the login function and paste the challenge in the text fill that appears there. Then click
on Execute. Input your passphrase and send the transaction.
Now switch back to the login page. After a few seconds the login will be completed and a
welcome message will appear. Voilà!
This example shows how a typical Ethereum user can use his existing Ethereum account to login
to any third party website supporting Ethereum. And all of this is done without a central server.
Although authentication is not performed by the owner of the website, there is no central
authority validating the user: it is the Ethereum network that does so.
EXPERIMENT NO: 5
Name of the Student:
Roll No.
What is Geth?
Geth(Go Ethereum) may refer to a Golang implementation of Ethereum blockchain in the form
of a standalone client software. Geth is a command line interface for running Ethereum nodes
implemented in Go Language. Using Geth you can join Ethereum network, transfer ether
between accounts or even mine ethers.
Geth Ethereum is the command-line interface for implementing an Ethereum node in Google’s
Go programming language. Geth serves as a node in the blockchain that helps the user to mine
Ether and creates software that runs on Ethereum Virtual Machine. Geth helps to mine real ether,
create contracts, make transactions, transfer funds between addresses, etc. Geth is supported on
almost all operating systems like Linux, macOS, and Windows.
Features of Go-Ethereum:
● As the name suggests, Geth only deals with Ethereum.
● The software has CPU mining but requires GPU mining software to produce ETH
● There are no fees when you are in solo mining mode. Costs from mining in a pool vary
significantly.
Output:
EXPERIMENT NO: 6
Name of the Student:
Roll No.
Date of Practical Performed: Staff Signature with Date
& Marks
Ganache
Ganache is a private Ethereum blockchain environment that allows to you emulate the Ethereum
blockchain so that you can interact with smart contracts in your own private blockchain.
Ganache forms part of the Truffle Suite, a set of developer tools that allows users to recreate
blockchain environments locally and test smart contracts. Smart contract execution on the
Ethereum blockchain is very different from other types of software.
Features of Ganache:
● Displays blockchain log output
● Provides advanced mining control
● Built-in block explorer
● Ethereum blockchain environment
● Ganache has a desktop application as well as a command-line tool
● When you open the installation URL given above, it automatically detects your
machine’s OS and directs you to the appropriate binary installation.
Conclusion:
Hence studied and implemented Ganache.
Output:
EXPERIMENT NO: 7
Roll No.
& Marks
Theory:
Hyperledger Fabric is a permissioned distributed ledger framework that enables the development
of enterprise-grade applications and solutions.
To ensure it is easy to integrate and develop, the design is kept versatile and modular. Clearly,
this also means that it can fit into different use cases and provide a unique way to solve
consensus within a network without the need to sacrifice performance or privacy.
At the core, the first thing that makes Hyperledger Fabric so useful is its key characteristics,
including permissioned nature, security, and high-performance. The code for Hyperledger Fabric
is written in Go, Java, JavaScript, Python, REST, and so on. Another version called Hyperledger
Fabric 2.0 is also out and available for everyone to use.
Hyperledger Fabric is an open-source platform for building distributed ledger solutions, with a
modular architecture that delivers high degrees of confidentiality, flexibility, resiliency, and
scalability. This enables solutions developed with fabric to be adapted for any industry. This is a
private and confidential blockchain framework managed by the Linux Foundation.
Here when clients submit the transaction proposal through the Fabric SDK, this proposal is sent
to all Endorsing Peers. These endorsing peers check the transaction verifies and executes and
generate the Read and Write set as output. Now, this response is again sent to the client. The
client collects all responses from all endorsing peers and send them to Order. Now, ordered sees
all transactions and orders them in ascending order and form a block. Now, this block is sent to
all committers which checks the transaction and add a new block in their own copy of the ledger.
Since you are aware of basic terms and workflow, let’s see blockchain’s main components.
Later, we will see the transaction flow in much more detail.
Orderer
Membership Service Provider (MSP)
The membership service provider (MSP), is a component that defines the rules in which,
identities are validated, authenticated, and allowed access to a network. The MSP manages user
IDs and authenticates clients who want to join the network. This includes providing credentials
for these clients to propose transactions. The MSP makes use of a Certificate Authority, which is
a pluggable interface that verifies and revokes user certificates upon confirmed identity. The
default interface used for the MSP is the Fabric-CA API. However, organizations can implement
an External Certificate Authority of their choice. As a result, a single Hyperledger Fabric
network can be controlled by multiple MSPs, where each organization brings its own favorite.
There are two types of MSPs.
Local MSP: It defines users (Clients) and nodes (peers orderers). It defines who has
administrative or participatory rights at that level.
Channel MSP: It defines administrative and participatory rights at the channel level.
Client
Clients are applications that act on behalf of a person to propose transactions on the network.
The client uses a Fabric SDK in order to communicate with the network. The client
communicates with the SDK in order to Read or Write the data in a Fabric blockchain and an in-
state DB. Even the client is issued with a certificate from the CA authority in order to make sure
that a valid client has initiated the transaction over the network.
Nodes
A “Node” is only a logical function in the sense that multiple nodes of different types can run on
the same physical server. What counts is how nodes are grouped in “trust domains” and
associated with logical entities that control them.
Client: A client that submits an actual transaction-invocation to the endorsers, and broadcasts
transaction-proposals to ordering service. In short, clients communicate with both peers and the
ordering service
Peer: A node that commits transactions and maintains the state and a copy of the ledger. A peer
receives ordered state updates in the form of blocks from the ordering service and maintains the
state and the ledger. Besides, peers can have a special endorser role. The special function of an
endorsing peer occurs with respect to a particular chaincode and consists in endorsing a
transaction before it is committed.
Types of Peers
Endorsing Peer: Endorsing peers is a special type of committing peers who have an additional
role to endorse a transaction. They endorse the transaction request which comes from the client.
Each endorsing peer possesses the copy of smart contract installed and a ledger. The main
function of Endorser is to simulate the transaction. It is executed based on the smart contract on
the personal copy of the ledger and generates the Read/Write sets which are sent to Client.
Though during simulation, the transaction is not committed to the ledger.
Committing Peer: Peers who commit the block which is received from the Ordering service, in
their own copy of the blockchain. This block contains the list of transactions where committing
peer to validate each transaction and mark it as either valid or invalid and commits to the block.
All transaction either valid or invalid are all committed to blockchain for future audit purpose.
Anchor Peer: As Fabric network can extend across multiple organization, we need some peers
to have communication across an organization. Not all peers can do this, but these are special
peers who are only authorized to do so which are nothing but Anchor peer. The anchor peers are
defined in Channel configuration.
Leading Peer: Leader peers are those who communicate or disseminate messages from Ordering
service to other peers in the same organization. These peers use Gossip protocol to make sure
that every peer receives the message. Leading peers cannot communicate across an organization.
If any Leading peer is not responding or is out of network, then we can select a leading peer from
available peer based on voting or randomly choose one.
Channels
A fabric network can have multiple channels. Channels allow organizations to utilize the same
network while maintaining separation between multiple blockchains. Only members(peers) of
the channels are allowed to see the transaction created by any member in a channel. In other
words, channels partition the network in order to allow transaction visibility for stakeholders
only. Only the members of the channel are involved in consensus, while other members of the
network do not see the transactions on the channel. The peer can maintain multiple ledgers. And
peer can be connected to multiple channels.
The configuration of the channel is maintained by configtx.yaml file. Using this file we generate
channel.tx file and then create a channel using it. Chaincode is installed on all participating peers
in a channel, whereas chaincode is instantiated on a channel. A channel contains all the
configurations of communication between peers. It holds the list of peers along with who are
endorsing, anchor, leader peers. When a client communicates with the network using SDK, the
SDK first gets a list of all endorsing peers to which the transaction request needs to send. Using
this list the SDK sends transaction requests to peers. Peers that do participate in multiple
channels simulate and commit transactions to different ledgers. Orderers are also a part of
channels.
Private and Permissioned– Rather than an open, permissionless system that allows unknown
identities to participate in the network, the members of a Hyperledger Fabric network enroll
through an MSP (Membership Service Provider), which is completely trusted.
Privacy– Unlike other networks, where privacy is not a top concern, this blockchain project
supports networks where privacy (using channels) is a crucial operational requirement.
Moreover, it offers the ability to create channels, allowing a group of participants to create a
separate ledger of transactions. It provides data sharing on a need-to-know basis by leveraging
private data ‘collections.’
EXPERIMENT NO: 8
Name of the Student:
Roll No.
What is Corda?
Corda is distributed ledger software for recording and processing shared data such as contracts,
designed to implement the vision contained in this document. Corda supports smart contracts,
matching the definition of Clack, Bakshi, Braine 4; our smart contract is an agreement whose
execution is both automatable by computer code working with human input and control, and
whose rights and obligations, as expressed in legal prose, are legally enforceable.
Features of Corda
Corda’s design was initially driven by the needs of regulated financial institutions but turns out
to be far more broadly applicable. It is heavily inspired by blockchain systems, but without the
design choices that make traditional blockchains inappropriate for the execution of real-world
business transactions. Our fundamental building block is a “state object”, representing a specific
instance of a specific agreement, which may be thought of as representing a real-world contract
or section of a contract. We use the terms ‘agreement’ and ‘contract’ interchangeably in this
paper. This stands in contrast to systems where the data over which participants must reach
consensus is the state of an entire ledger or the state of an entire virtual machine. Corda provides
three main tools to achieve global distributed consensus:
• Smart contract logic which specifies constraints that ensure state transitions are valid
according to pre-agreed rules, described in contract code as part of CorDapps. • Uniqueness and
timestamping services known as notary pools to order transactions temporally and eliminate
conflicts.
• A unique component called the flow framework which simplifies the process of writing
complex multi-step protocols between multiple mutually distrusting parties across the internet
What Makes Corda Blockchain Framework Different?
Privacy: Privacy is a critical focus for any distributed ledger technology system. It is because
your data is bound to be distributed across multiple nodes and servers belonging to different
business entities. In R3’s Corda, the only parties who have access to the details of a transaction
are those involved in the transaction and those who need to assure themselves of transaction
provenance.
It means two or more participants can transact with one another, sharing only necessary
information between them. This is in stark contrast to public blockchain or few private
blockchain frameworks - broadcasting the transaction or its details across the entire network. The
privacy feature of the Corda blockchain framework is what enterprises and businesses would
want. Like other comrade frameworks, Corda does not include any gossip protocols which
broadcasts all transactions to the network.
Identity: Identification of different parties in the DLT system over a permissioned blockchain
becomes a core criterion to build a closed network of the system among known participants. In
R3 Corda, parties have assurance over the identity of members in the blockchain network.
Identity is a very important feature for a global decentralized ledger where you are unaware of
other participants. It is made possible with KYC requirements of all participants across the
network and the core identity framework which enables Corda to assign a single user profile to
any legal entity, be it an organization or an individual.
Consensus: Consensus is a technique through which organizations over a distributed and
decentralized network come on to an agreement over the transactions happening between them.
It is an important concept to identify malpractices and maintain the integrity of any blockchain
network, no matter if its public or permissioned.
Transactions in Corda are confirmed through a process of consensus using a variety of
algorithms, including Byzantine Fault Tolerant algorithms. Just like any other blockchain, the
unique features a Corda network can support multiple different consensus pools using different
algorithms enabling its clients with a pluggable consensus model based on their requirement.
Contracts: Smart contracts and program files embedding the business logic, rules validation are
part of any business being run among different organizations over a blockchain-based distributed
system. In R3’s Corda, transactions are processed by having each participant execute the same
code deterministically to verify the proposed updates to the ledger.
Just like Ethereum, the languages you can use are high-level and productive, like Java and
Kotlin, rather than obscure ones like Solidity. The validation function of the CorDapp (Corda
Distributed Applications) contract code only needs the validation chain of each associated
transaction.
No Block, But Chain: Corda’s functionality relies on the UTXO input/output model, which is
very similar to the transaction system used in traditional blockchains such as Bitcoin. However,
the storage and verification do not end up into a chain of blocks like other enterprise blockchain
frameworks, e.g., Hyperledger Fabric that bundles up a set of transactions between ‘n’ number of
participants over the channel into a block-based on different criteria. Corda links transaction in a
cryptographically linked (chained) chain to the transactions it depends on and not to a previous
block of some other set of transactions.
Conclusion:
Successfully concluded a study on Corda.