Blockchain
Blockchain is a digital record book, also called a ledger, that is shared across many computers instead of being stored in one central place. Because everyone in the network holds a copy of the same data, no single authority like a bank or company controls it. Once information is written to the blockchain, it cannot be easily changed, which makes the system reliable and transparent. This shared ownership of data is what makes blockchain fundamentally different from traditional databases.
Why is it Called a βBlock-Chainβ?
The name blockchain comes from the way data is structured. Information is stored inside blocks, and these blocks are linked together in a sequential order, forming a chain. Each block contains some data, a unique cryptographic hash that identifies the block, and the hash of the previous block. Because every block depends on the previous one, altering a single block would break the entire chain. This linking mechanism is what makes blockchain tamper-resistant.
Where is Blockchain Used?
Blockchain technology is widely used in cryptocurrencies like Bitcoin and Ethereum, but its applications go far beyond digital money. It is used in digital payments, smart contracts, supply chain tracking, voting systems, and digital identity solutions. Any system that requires trust, transparency, and data integrity can benefit from blockchain.
What is a Smart Contract?
A smart contract is a computer program that lives on a blockchain and executes automatically when predefined conditions are met. Unlike traditional contracts that require intermediaries such as lawyers or banks, smart contracts enforce rules directly through code. Once deployed, the contract runs exactly as programmed and cannot be altered. In simple terms, if the conditions written in the code are satisfied, the contract executes itself without human intervention.
How Smart Contracts Work
The process begins with writing the contract rules in code. This code is then deployed to a blockchain such as Ethereum. When someone interacts with the contract, for example by sending a transaction, the contract checks whether the required conditions are met. If they are, the contract automatically executes the specified actions. Because the contract runs on the blockchain, its execution is transparent, deterministic, and irreversible.
Where Smart Contracts Are Used
Smart contracts are used in decentralized finance applications, NFT marketplaces, voting systems, insurance claim automation, and supply chain management. In this project, smart contracts are written using the Solidity programming language.
Introduction to Solidity
Solidity is a statically typed programming language designed specifically for writing smart contracts on Ethereum. It has a syntax similar to JavaScript and C++, making it easier for developers from traditional programming backgrounds to learn.
Basic Solidity Structure
Every Solidity contract starts with a license identifier and a compiler version declaration. The license provides legal clarity, while the version ensures the contract is compiled using a compatible Solidity compiler. The contract keyword is then used to define the smart contract, which is conceptually similar to a class in object-oriented programming.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract ProfileRegistry {
struct Profile {
bytes32 nameHash;
int32 luckyNumber;
}
mapping(address => Profile) private profiles;
event ProfileUpdated(
address indexed user,
bytes32 nameHash,
int32 luckyNumber,
uint256 timestamp
);
function updateProfile(
bytes32 _nameHash,
int32 _luckyNumber
) external {
require(_nameHash != bytes32(0), "Invalid name hash");
profiles[msg.sender] = Profile(
_nameHash,
_luckyNumber
);
emit ProfileUpdated(
msg.sender,
_nameHash,
_luckyNumber,
block.timestamp
);
}
function getProfile(address user)
external
view
returns (bytes32, int32)
{
Profile memory p = profiles[user];
return (p.nameHash, p.luckyNumber);
}
function verifyName(
address user,
string calldata name
) external view returns (bool) {
return profiles[user].nameHash == keccak256(abi.encodePacked(name));
}
function add(int256 a, int256 b)
external
pure
returns (int256)
{
return a + b;
}
}
ProfileRegistry Smart Contract Walkthrough
// SPDX-License-Identifier: MIT
- SPDX License Identifier
Declares the license of the contract
Required by Solidity compilers to avoid warnings
MIT means open-source and permissive
- Solidity Compiler Version
pragma solidity ^0.8.20;
Tells the compiler:
Use Solidity version 0.8.20 or higher
But not 0.9.0 or above
Solidity 0.8+ has:
Built-in overflow checks
Safer arithmetic
- Contract Declaration
contract ProfileRegistry {
Starts a smart contract
Think of it as a class in OOP
Everything inside belongs to this contract
- Struct Definition
struct Profile {
bytes32 nameHash;
int32 luckyNumber;
}
What is a struct?
A custom data type
Groups related data together
Fields explained:
bytes32 nameHash
Fixed-size 32-byte value
Stores keccak256(name)
Privacy-friendly: actual name is not stored
int32 luckyNumber
Signed 32-bit integer
Range: -2^31 to 2^31 - 1
π One Profile = one user's data
- Mapping Declaration
mapping(address => Profile) private profiles;
Meaning:
Maps a wallet address to a Profile struct
Breakdown:
address β userβs wallet address
Profile β their stored profile
private:
Cannot be accessed directly outside the contract
Still visible on blockchain, but not via Solidity getter
Think of it like:
profiles[0xABC...] = { nameHash, luckyNumber }
- Event Declaration
event ProfileUpdated(
address indexed user,
bytes32 nameHash,
int32 luckyNumber,
uint256 timestamp
);
What is an event?
Used for logging
Stored in transaction logs, not contract storage
Cheap to read from frontend
Parameters:
address indexed user
indexed allows fast filtering (very important for UIs)
bytes32 nameHash
int32 luckyNumber
uint256 timestamp
Block timestamp when update occurred
π Frontend apps listen to this event
- updateProfile Function (State-changing)
function updateProfile(
bytes32 _nameHash,
int32 _luckyNumber
) external {
Function type:
external
Can be called from outside the contract
Cheaper than public for calldata inputs
Parameters:
_nameHash β keccak256 hash of name
_luckyNumber β user's chosen number
7.1 Input Validation
require(_nameHash != bytes32(0), "Invalid name hash");
Prevents empty hash
bytes32(0) = all zeros
If condition fails:
Transaction reverts
Gas is refunded (except base cost)
Error message shown
7.2 Storing Profile Data
profiles[msg.sender] = Profile(
_nameHash,
_luckyNumber
);
Key concepts:
msg.sender = wallet calling the function
Creates a new Profile struct
Stores it in the mapping
Equivalent to:
profiles[msg.sender].nameHash = _nameHash;
profiles[msg.sender].luckyNumber = _luckyNumber;
7.3 Emitting Event
emit ProfileUpdated(
msg.sender,
_nameHash,
_luckyNumber,
block.timestamp
);
Emits the event to blockchain logs
block.timestamp
Current block time (in seconds since Unix epoch)
π Used by frontends / indexers (like The Graph)
- getProfile Function (Read-only)
emit ProfileUpdated(
msg.sender,
_nameHash,
_luckyNumber,
block.timestamp
);
Modifiers:
view
Reads blockchain state
Does not cost gas when called locally
Function Body
Profile memory p = profiles[user];
Fetches the profile from storage
Copies it into memory (temporary)
return (p.nameHash, p.luckyNumber);
Returns both values as a tuple
- verifyName Function (Hash Verification)
function verifyName(
address user,
string calldata name
) external view returns (bool) {
Why calldata?
Read-only input
Cheaper gas
Best for function parameters
Hash Comparison
return profiles[user].nameHash == keccak256(abi.encodePacked(name));
Step-by-step:
abi.encodePacked(name)
Converts string into bytes
keccak256(...)
Produces a bytes32 hash
Compare with stored hash
β
Returns true if names match
β false otherwise
- add Function (Pure Function)
Modifiers:
pure
Uses no blockchain data
No state access
No gas when called locally
Logic
return a + b;
Simple integer addition
Solidity 0.8+ automatically checks overflow
π Summary of Concepts Used
Concept
Where
Struct
Profile
Mapping
address => Profile
Event
ProfileUpdated
State change
updateProfile
Read-only
getProfile, verifyName
Hashing
keccak256
Privacy
Store hash instead of name
Pure function
add()
Solidity Function Types Explained
In Solidity, functions can be categorized based on how they interact with blockchain data. Pure functions do not read or modify any blockchain state and behave like calculators. View functions can read blockchain data but cannot modify it, making them ideal for data retrieval. Normal functions can modify the blockchain state, emit events, and write to storage, which means they always cost gas when executed in a transaction.
A simple way to remember this is: pure functions calculate, view functions read, and normal functions write.
What is Ganache?
Ganache is a local Ethereum blockchain simulator used for development and testing. It allows developers to deploy contracts, send transactions, test gas usage, and debug applications without spending real Ether. Ganache provides instant block mining, free test Ether, and complete control over the blockchain environment, making it ideal for learning and experimentation.
Ganache Installation:-
npm install -g ganache
Check Version:-
ganache --version
Run:-
ganache
Default:-
RPC: http://127.0.0.1:8545
Chain ID: 1337
What is Truffle?
Truffle is a development framework that simplifies the process of building Ethereum smart contracts. It provides a structured project setup, Solidity compilation, deployment scripts called migrations, testing utilities, and network management. Truffle removes the need to manually handle low-level details like ABI generation and contract deployment.
Installation:-
npm install -g truffle
Check version:-
truffle --version
Initiate Truffle:-
truffle init
This generates your first project Directory.
Deploy Contract:-
truffle migrate
Verify Deployment:-
truffle console
Truffle config.js:-
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545, // or 8545
network_id: "*"
}
},
compilers: {
solc: {
version: "0.8.20"
}
}
};
Truffle Nomenclature for migration/ folder:-
What is MetaMask?
MetaMask is a browser-based crypto wallet that acts as a bridge between users and decentralized applications. It allows users to manage private keys, store Ether and tokens, and sign blockchain transactions securely. MetaMask injects a Web3 provider into the browser, enabling frontend applications to communicate directly with the blockchain and identify users.
Prerequisites:-
JavaScript Basics
https://www.w3schools.com/js/
Chrome Extension Development (Intro)
https://developer.chrome.com/docs/extensions/
Basic Cryptography Concepts
https://www.cloudflare.com/learning/ssl/what-is-encryption/
Blockchain Basics
https://ethereum.org/en/developers/docs/intro-to-ethereum/
GitHub Repo:-
https://github.com/yuvraj1235/TDOC_Guardium_tutorial

Top comments (0)