Blockchain Technology - Unit 2
Blockchain Technology - Unit 2
1. Define a Smart contract and discuss important characteristics of a smart contract like
secure, unstoppable , automatically executable and enforceable.
A smart contract is a self-executing contract with the terms of the agreement written into code
and deployed on a blockchain. It automatically enforces and executes the contract when
predefined conditions are met, without the need for intermediaries.
Key Characteristics:
1. Secure: Smart contracts are secure due to blockchain’s cryptographic techniques,
making the contract’s code immutable and resistant to tampering once deployed.
2. Unstoppable: Once deployed on the blockchain, smart contracts cannot be stopped,
altered, or reversed by any party, ensuring the execution is guaranteed.
3. Automatically Executable: Smart contracts automatically execute their terms when
conditions are met, without requiring human intervention or external enforcement.
4. Enforceable: The blockchain network ensures that the contract’s terms are enforced
automatically, reducing the chances of disputes or breaches.
2. What is Ricardian Contracts and explain its elements, with a neat diagram.
An Ethereum transaction consists of several key fields that define how value and data are
transferred on the blockchain. The most common fields in an Ethereum transaction are:
1. Nonce – A unique number assigned to each transaction from an account, preventing
replay attacks.
2. Gas Price – The amount of Ether (ETH) the sender is willing to pay per unit of gas.
3. Gas Limit – The maximum amount of gas the sender is willing to consume for the
transaction.
6. Data – Optional field for smart contract interactions or additional information.
7. V, R, S – Signature values used for transaction verification and sender authentication.
1. Human & Machine Readable – Can be understood by both people and software.
The Ethereum Virtual Machine (EVM) is a decentralized runtime environment responsible for
executing smart contracts on the Ethereum blockchain. It operates as a Turing-complete
machine, meaning it can perform any computation given enough resources. However, execution
is limited by gas fees to prevent infinite loops and denial-of-service attacks.
1. Stack-based execution – It processes instructions using a last-in, first-out (LIFO) stack.
2. Isolation and Security – The EVM is sandboxed, meaning it cannot directly interact
with external systems like networks or files.
3. 256-bit word size – It supports cryptographic functions like Keccak-256 hashing and
elliptic curve cryptography for security.
4. Exception handling – The EVM halts execution if an error occurs, such as insufficient
gas or invalid instructions.
In summary, the EVM enables secure and decentralized execution of smart contracts, making
Ethereum a leading platform for blockchain applications
6. Write short notes on : i)Ethereum Stack ii) Ether Cryptocurrency/token (ETC & ETH) iii)
Fork iv)Gas
i) Ethereum Stack
● An Ethereum client, such as Geth, which helps interact with the network.
● The web3.js library, which allows developers to communicate with Ethereum using the
Remote Procedure Call (RPC) interface.
Ethereum’s native cryptocurrency is Ether (ETH), used to pay for transactions and smart
contract execution.
● Ethereum Classic (ETC): Formed after a hard fork following the DAO hack, retaining
the original blockchain.
● Ethereum (ETH): The forked version that reversed the hack’s impact and continues as
the main Ethereum network.
iii) Fork
A fork occurs when the Ethereum protocol undergoes an upgrade, creating a divergence in the
blockchain.
1. A notable hard fork happened during the transition from Frontier (Ethereum’s first
version) to Homestead, improving network security and efficiency.
2. Hard Fork: A non-backward-compatible blockchain split, creating a new version (e.g.,
Ethereum vs. Ethereum Classic after the DAO hack).
3. Soft Fork: A backward-compatible upgrade where older nodes can still validate
transactions (e.g., Ethereum’s Constantinople Upgrade).
iv) Gas
Gas is a unit used to measure computational effort required for transactions and smart contracts
on Ethereum.
1. Every operation consumes gas, and users must pay gas fees to miners for execution.
2. If a transaction runs out of gas, it is rolled back, but gas fees are still charged for
computation.
3. If the transaction is successfully executed, then any remaining gas is “refunded” to the
transaction originator.
4. Gas Fee Calculation: Gas Used × Gas Price (in gwei) determines transaction fees;
complex smart contracts require more gas.
7. With a simple example program, explain the layout of a solidity program Explain
available precompiled contracts in Ethereum.
Eg.
contract SimpleStorage {
1. Elliptic Curve Public Key Recovery (ecrecover) – Recovers a public key from a
signature.
3. RIPEMD-160 Hash Function (ripemd160) – Produces a 160-bit hash, used in Bitcoin
addresses.
4. Identity Function (identity) – Returns the same input without modification, useful for
gas-efficient data transfers.
8. With the execution environment tuple, explain the execution environment in Ethereum
platform. With a neat generic model of an oracle and smart contract ecosystem
The execution environment in Ethereum defines the conditions under which smart contracts
and transactions are executed. It is represented by the execution environment tuple, which
includes essential parameters for managing contract execution.
1. Executing Account Address – The address of the account running the smart contract.
3. Gas Price – The price per unit of gas set by the sender for execution.
4. Input Data – The transaction or function call data provided to the contract.
5. Originating Address – The address that originally initiated the execution.
7. Executable Code – The smart contract code (in bytecode format) that needs to run.
8. Block Header – Contains details of the current block, such as timestamp and block
number.
9. Message Calls & Contract Creations – Tracks the number of nested function calls or
contract deployments.
10.System State & Remaining Gas – Maintains the current state of Ethereum and the
available gas.
Smart contracts on Ethereum cannot access real-world data directly. Oracles act as bridges
between the blockchain and external data sources.
Generic Model of Oracle-Smart Contract Ecosystem:
1. External Data Source → Real-world data (e.g., stock prices, weather, sports scores).
2. Oracle Service → Fetches and verifies data from external sources.
4. Smart Contract → Uses the data to trigger actions or execute transactions.
9. Explain the role and types of oracles in the smart contract paradigm
Smart contracts on Ethereum cannot access real-world data directly as they operate within an
isolated blockchain environment. Oracles act as intermediaries that fetch external data and
provide it to smart contracts, enabling them to interact with off-chain information.
Key Roles of Oracles:
1. Bridging On-Chain and Off-Chain Data – Oracles fetch real-world data (e.g., stock
prices, weather, sports scores) and make it available to smart contracts.
3. Ensuring Data Reliability – Decentralized oracles verify data from multiple sources to
prevent manipulation
○ Example: A DeFi smart contract getting ETH/USD price from a price feed.
○ Connect blockchain with real-world physical devices (IoT sensors, RFID, barcode
scanners).
○ Bring off-chain data into the blockchain for smart contract execution.
○ Send blockchain data to external systems (e.g., triggering actions in IoT devices).
○ Example: Chainlink, which aggregates price feeds from multiple exchanges for
accurate data.
11. Explain World state and Account state in Ethereum
Ethereum maintains a state database to track accounts and smart contracts. Two key
components of this state are World State and Account State.
1. World State
The world state represents the global state of the Ethereum blockchain at any given time. It is
a mapping of Ethereum addresses to their corresponding account states.
● Stored in a Merkle Patricia Trie, a data structure that efficiently organizes and verifies
account data.
● The root hash of this trie is stored in the block header to ensure security.
● Encoded using Recursive Length Prefix (RLP) for efficient storage and transmission.
👉
(Note ) Think of the world state as a big ledger that records the status of all accounts on
Ethereum.
2. Account State
Each account state holds information about an individual Ethereum account. It consists of four
main components:
1. Nonce – A counter that tracks the number of transactions sent from the account.
3. Storage Root – The root hash of a separate Merkle Patricia Trie storing the account’s
key-value storage (used for smart contracts).
4. Code Hash – A hash of the smart contract code, if the account is a contract account.
👉
(Note ) In simple terms, the account state keeps track of an account’s transaction history,
funds, and smart contract code (if applicable).
12. With suitable example, explain the following in Solidity i)Events ii)Inheritance v)Fall
back functions iii)Internal function call iv)External function call vi)Modifier functions
vii)Constructor functions.
i) Events
Events in Solidity allow smart contracts to communicate with external applications (such as
front-end interfaces) by logging data.
Example:
contract EventExample {
ii) Inheritance
Inheritance allows a contract to inherit properties and functions from another contract.
Example:
contract Parent {
}
contract Child is Parent {
Internal functions are accessible only within the contract or its derived contracts and cannot be
called externally.
Example:
contract InternalExample {
External functions can be called from outside the contract but not from within the same contract.
Example:
pragma solidity ^0.8.0;
contract ExternalExample {
externalFunction() must be called from another contract or off-chain application, not from within
ExternalExample.
v) Fallback Function
A fallback function is executed when a contract receives Ether without any function being called
or an unknown function is invoked.
Example:
contract FallbackExample {
Modifiers define reusable conditions that modify the behavior of functions, commonly used for
access control.
Example:
pragma solidity ^0.8.0;
contract ModifierExample {
address owner;
constructor() {
modifier onlyOwner() {
_;
A constructor is a special function that runs only once when the contract is deployed. It is
typically used to initialize contract variables.
Example:
contract ConstructorExample {
message = _message;
}
}
13. Explain internal, external and constant function types in Solidity. Show the syntax to
declare a function.
i) Internal Functions
Syntax:
// Function logic
Syntax:
These functions do not modify the blockchain state, making them gas-efficient.
A) View Functions
Syntax:
return address(this).balance;
B) Pure Functions
Syntax: