BlockChain Technology Unit - 2
BlockChain Technology Unit - 2
Unit-2
1. Consensus Algorithms
These are mechanisms that allow blockchain nodes to agree on the state of the network, ensuring that
transactions are valid and trust is maintained.
a. Proof of Work (PoW):
• Definition: PoW requires participants (miners) to solve complex mathematical puzzles (hashing
problems) to validate transactions and create new blocks.
• How it works:
o Miners compete to solve a cryptographic puzzle.
o The first miner to solve the puzzle gets to add the block to the blockchain and is rewarded.
o The puzzle involves finding a hash that is less than a target value by repeatedly hashing
block data.
o This system is energy-intensive and slow but is highly secure.
• Example: Bitcoin uses PoW.
• Drawbacks: High energy consumption, low transaction speed.
b. Proof of Stake (PoS):
• Definition: PoS assigns block validation rights to nodes in proportion to their holdings of the
network's cryptocurrency, eliminating the need for energy-intensive mining.
• How it works:
o Validators are chosen to create new blocks based on their stake (amount of cryptocurrency
they hold).
o Validators lock up their stake, which they may lose if they attempt to validate fraudulent
transactions (slashing).
o Less energy is consumed as compared to PoW.
• Example: Ethereum 2.0 is transitioning from PoW to PoS.
• Drawbacks: Potential for centralization, as wealthier nodes have more power.
c. Practical Byzantine Fault Tolerance (PBFT):
• Definition: PBFT is a consensus mechanism designed for distributed systems that ensures
consensus even if some nodes are faulty or malicious.
• How it works:
o The network requires a majority of honest nodes (at least ⅔) to agree on the transaction’s
validity.
o Nodes exchange messages to reach a consensus, tolerating up to ⅓ faulty nodes.
o Faster than PoW or PoS, as it doesn't rely on solving puzzles or staking.
• Example: Hyperledger Fabric, Tendermint.
• Drawbacks: Doesn't scale well with large numbers of nodes.
d. Delegated Proof of Stake (DPoS):
• Definition: DPoS is a variation of PoS where stakeholders elect a small number of delegates
(witnesses) to validate blocks on their behalf.
• How it works:
o Stakeholders vote for delegates by pooling their tokens.
o The elected delegates produce and validate blocks.
o Delegates can be voted out if they fail to perform efficiently.
• Example: EOS, Tron.
• Drawbacks: Risk of centralization, as the power is concentrated in the hands of a few elected
validators.
2. Smart Contracts
Definition:
• A smart contract is a self-executing contract where the terms of the agreement (i.e., conditions,
rules, and penalties) are directly written in the code.
• It runs on blockchain networks like Ethereum, ensuring that once the contract conditions are met,
the agreed-upon actions are executed automatically without the need for intermediaries such as
lawyers, banks, or escrow services.
Key Characteristics:
1. Self-Executing: Once the conditions in the contract are met, the contract automatically enforces its
terms.
2. Immutable: Once deployed, the code of a smart contract cannot be altered, ensuring transparency
and security. However, upgradeable patterns exist, such as proxy contracts, which allow for certain
changes.
3. Trustless: The need for trust between parties is eliminated. The code enforces the contract’s rules.
4. Transparent: Since smart contracts are stored on a public blockchain, anyone can audit the
contract to verify its rules and execution.
5. Autonomous: Smart contracts function without any third-party interference, reducing the need for
human intervention once deployed.
Benefits of Smart Contracts:
• Cost Efficiency: By removing the need for intermediaries like lawyers or financial institutions,
transaction costs are reduced.
• Speed: Smart contracts execute in real time as soon as conditions are met, without delays caused
by paperwork or middlemen.
• Security: The contracts are encrypted and distributed across a decentralized blockchain network,
making them tamper-resistant and highly secure.
• Precision: Since smart contracts are based on code, they execute exactly as written, eliminating
human error or misinterpretation of terms.
Components of Smart Contracts:
1. Parties Involved: These are the individuals or entities entering into the contract.
2. Contractual Clauses: The terms of the agreement, written in the form of logical code (e.g., “If party
A sends X amount of tokens, then party B will send Y product”).
3. Triggering Events: Conditions that must be met to trigger the contract’s execution (e.g., date,
payment received, product delivered).
4. Outcomes: The contract executes its agreed actions (e.g., transfer of funds or assets) based on
predefined outcomes.
Example of a Smart Contract:
• Escrow in a Sale: Suppose two parties want to perform an online transaction:
o Step 1: The buyer places cryptocurrency into a smart contract.
o Step 2: The smart contract holds the funds in escrow.
o Step 3: The seller ships the goods.
o Step 4: Upon delivery confirmation (e.g., a tracking system), the smart contract releases the
funds to the seller.
o Step 5: If there’s a dispute, the smart contract might execute a predefined resolution
mechanism, like a refund.
solidity
Copy code
pragma solidity ^0.8.0;
contract SimpleBank {
mapping(address => uint) private balances;
3. Solidity Programming
Solidity is a high-level, statically typed programming language designed specifically for creating smart
contracts that run on the Ethereum Virtual Machine (EVM). Solidity is similar in syntax to JavaScript,
Python, and C++, and is primarily used for writing decentralized applications (DApps) on blockchain
platforms like Ethereum.
Why Solidity?
• Ethereum-Compatible: Solidity is designed to compile into bytecode that can be run by the
Ethereum Virtual Machine.
• Smart Contract Focused: It enables developers to define complex agreements that automatically
execute based on the rules written in the contract, ensuring decentralization, transparency, and
immutability.
• Widely Used: It is the most popular language for smart contract development, supported by a rich
ecosystem of tools, documentation, and developer communities.
1. Contracts:
o Solidity contracts are collections of code (functions) and data (state) that reside on the
Ethereum blockchain. Contracts are the primary building blocks of DApps.
o Example:
solidity
Copy code
pragma solidity ^0.8.0;
contract SimpleContract {
// Contract code goes here
}
2. State Variables:
o These are permanently stored on the blockchain and represent the contract’s data (like class
variables in OOP languages).
o Example:
solidity
Copy code
uint public count; // State variable
3. Functions:
o Functions are actions that can be performed by or on the contract. Functions can be public,
private, or internal.
o Example:
solidity
Copy code
function increment() public {
count += 1;
}
o View Functions: These do not modify the state and only read data from the blockchain.
solidity
Copy code
function getCount() public view returns (uint) {
return count;
}
4. Modifiers:
o Modifiers are used to change the behavior of functions. They often check for conditions
before allowing a function to execute (like access control).
o Example:
solidity
Copy code
modifier onlyOwner() {
require(msg.sender == owner, "Not the contract owner");
_;
}
5. Events:
o Events are used to communicate with off-chain applications by emitting logs. Events allow
data to be stored in a way that can be easily read externally (by dApps or other services).
o Example:
solidity
Copy code
event Incremented(uint newCount);