0% found this document useful (0 votes)
13 views17 pages

Blockchain Technology - Unit 2

Uploaded by

smask510
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)
13 views17 pages

Blockchain Technology - Unit 2

Uploaded by

smask510
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/ 17

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.

A Ricardian Contract is a digital contract that is both human-readable and machine-readable. It


defines legal agreements in a structured format, making them verifiable and executable by
software.

Elements of a Ricardian Contract

1.​ Legal Agreement – A legally binding document.​

2.​ Readable Format – Understandable by both humans and machines.​

3.​ Digital Signature – Ensures authenticity and security.​

4.​ Cryptographic Hash – Provides a unique identifier for verification.


3. Explain the common fields of a transaction in Ethereum blockchain

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.​

4.​ To – The recipient’s Ethereum address.​

5.​ Value – The amount of ETH being sent in 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.

4. What is a Recardian contract? List its properties

A Ricardian Contract is a digital contract that is both human-readable and


machine-readable. It was introduced by Ian Grigg in 1996 and is widely used in blockchain and
financial systems. This contract ensures legal enforceability while being cryptographically
secured.

Properties of a Ricardian Contract

1.​ Human & Machine Readable – Can be understood by both people and software.​

2.​ Legal Binding – Represents a real-world legal agreement.​

3.​ Cryptographic Hash – Ensures integrity and uniqueness.​

4.​ Digital Signature – Provides authentication and security.​

5.​ Easily Verifiable – Can be checked by both parties involved.​

6.​ Immutable – Once deployed, it cannot be changed


5. Explain Ethereum Virtual Machine (EVM) with a suitable diagram.

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.

Key features of the EVM include:

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

The Ethereum stack consists of:

●​ The Ethereum blockchain, which operates on a peer-to-peer (P2P) network.​

●​ 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.

ii) Ether Cryptocurrency/Token (ETH & ETC)

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.

A Solidity program consists of several key components:

●​ Version declaration (pragma solidity) specifies the compiler version.​

●​ Contract declaration defines the smart contract.​

●​ State variables store contract data.​

●​ Functions define behavior, such as modifying or retrieving data.​

Eg.

pragma solidity ^0.8.0;

contract SimpleStorage {

uint storedData; // State variable to store data

function set(uint x) public {

storedData = x; // Function to update storedData

function get() public view returns (uint) {

return storedData; // Function to retrieve storedData

●​ The contract SimpleStorage contains a variable storedData.​

●​ The set() function updates the value of storedData.​

●​ The get() function returns the stored value.


Ethereum provides built-in precompiled contracts for efficient execution of common
cryptographic operations. These contracts are optimized at the Ethereum protocol level,
reducing gas costs. The four main precompiled contracts are:

1.​ Elliptic Curve Public Key Recovery (ecrecover) – Recovers a public key from a
signature.​

2.​ SHA-256 Hash Function (sha256) – Computes a 256-bit cryptographic hash.​

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.

Elements of the Execution Environment Tuple

1.​ Executing Account Address – The address of the account running the smart contract.​

2.​ Sender Address – The address that initiated the transaction.​

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.​

6.​ Transaction Value – The amount of Ether transferred in the transaction.​

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.

Oracle & Smart Contract Ecosystem

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.​

3.​ Oracle Contract → Relays verified data to a smart contract.​

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

Role of Oracles in Smart Contracts

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.​

2.​ Enhancing Smart Contract Functionality – Enables decentralized applications


(dApps) like DeFi, supply chain tracking, and insurance automation.​

3.​ Ensuring Data Reliability – Decentralized oracles verify data from multiple sources to
prevent manipulation

Types of Oracles in Blockchain

1.​ Software Oracles:​

○​ Fetch data from web APIs, databases, or online sources.​

○​ Example: A DeFi smart contract getting ETH/USD price from a price feed.​

2.​ Hardware Oracles:​

○​ Connect blockchain with real-world physical devices (IoT sensors, RFID, barcode
scanners).​

○​ Example: A supply chain contract tracking shipment temperature using IoT


sensors.​

3.​ Inbound Oracles:​

○​ Bring off-chain data into the blockchain for smart contract execution.​

○​ Example: A sports betting dApp retrieving match results from an API.​

4.​ Outbound Oracles:​

○​ Send blockchain data to external systems (e.g., triggering actions in IoT devices).​

○​ Example: A smart lock unlocking a door when an on-chain payment is verified.​

5.​ Decentralized Oracles:​

○​ Use multiple sources to prevent data manipulation and ensure trust.​

○​ 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.​

2.​ Balance – The amount of Ether (ETH) held by 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:

pragma solidity ^0.8.0;

contract EventExample {

event DataStored(uint data); // Declaring an event

function storeData(uint _data) public {

emit DataStored(_data); // Emitting the event

When storeData(100) is called, it logs DataStored(100), which can be captured by external


applications.

ii) Inheritance

Inheritance allows a contract to inherit properties and functions from another contract.

Example:

pragma solidity ^0.8.0;

contract Parent {

function greet() public pure returns (string memory) {

return "Hello from Parent!";

}
contract Child is Parent {

// Inherits greet() function from Parent

The Child contract can call greet() without redefining it.

iii) Internal Function Call

Internal functions are accessible only within the contract or its derived contracts and cannot be
called externally.

Example:

pragma solidity ^0.8.0;

contract InternalExample {

function _internalFunction() internal pure returns (string memory) {

return "This is an internal function";

function callInternalFunction() public pure returns (string memory) {

return _internalFunction(); // Internal function is called within the contract

_internalFunction() is called within callInternalFunction(), but cannot be accessed from outside


the contract.

iv) External Function Call

External functions can be called from outside the contract but not from within the same contract.

Example:
pragma solidity ^0.8.0;

contract ExternalExample {

function externalFunction() external pure returns (string memory) {

return "This is an external function";

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:

pragma solidity ^0.8.0;

contract FallbackExample {

event Received(address sender, uint amount);

fallback() external payable {

emit Received(msg.sender, msg.value);

vi) Modifier Functions

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() {

owner = msg.sender; // Assign contract deployer as owner

modifier onlyOwner() {

require(msg.sender == owner, "Not the owner");

_;

function restrictedFunction() public onlyOwner {

// Only the owner can execute this function

vii) Constructor Functions

A constructor is a special function that runs only once when the contract is deployed. It is
typically used to initialize contract variables.

Example:

pragma solidity ^0.8.0;

contract ConstructorExample {

string public message;

constructor(string memory _message) {

message = _message;

}
}

When deploying, if "Hello, Blockchain!" is passed, message is set to "Hello, Blockchain!".

13. Explain internal, external and constant function types in Solidity. Show the syntax to
declare a function.

In Solidity, functions can have different visibility types:

i) Internal Functions

●​ Accessible only within the contract and derived (inherited) contracts.​

●​ Cannot be called from outside the contract.

Syntax:

function _internalFunction() internal {

// Function logic

ii) External Functions

●​ Can only be called from outside the contract.​

●​ Cannot be called from within the same contract.​

Syntax:

function externalFunction() external {


// Function logic
}

iii) Constant (View & Pure) Functions

These functions do not modify the blockchain state, making them gas-efficient.
A) View Functions

●​ Can read blockchain state but cannot modify it.

Syntax:

function getBalance() public view returns (uint) {

return address(this).balance;

B) Pure Functions

●​ Cannot read or modify blockchain state.​

●​ Used for computations that do not depend on contract data.

Syntax:

function add(uint a, uint b) public pure returns (uint) {


return a + b;
}

(Just for Reference)

You might also like