Intro to solidity

arsh_the_coder

ASHDEEP SINGH

Posted on June 30, 2024

Intro to solidity

The block header includes several pieces of data:

  • Previous Block Hash: The hash of the previous block in the chain.
  • Merkle Root: A hash representing all the transactions in the block.
  • Timestamp: The current time when the block is mined.
  • Difficulty Target: A value that determines the difficulty of the cryptographic puzzle.
  • Nonce: A variable number that miners change to find a valid hash.

Nonce - number only used once

This is a number used for setting the difficulty of the hash of current block upto the current blockchain's threshold.
A miner is a participant in a blockchain network who uses computational power to solve complex cryptographic puzzles. In return for their efforts, miners are rewarded with newly minted cryptocurrency coins (e.g., bitcoins) and transaction fees.
Since blockchain is decentralised, every miner has the data available enough to generate hash but it should be done by finding the right nonce.
Miners change the nonce value and hash the block header repeatedly.
Each hash is compared to the difficulty target.

Process in a nutshell
A miner collects transactions and forms a block.
They create a block header with all necessary information, including a nonce initially set to zero.
The miner hashes the block header.
If the resulting hash is below the difficulty target, the block is valid, and the miner broadcasts it to the network.
If the hash is not below the target, the miner increments the nonce and tries again.
This process repeats until a valid hash is found.

Nodes
They are just computers actively working on the blockchain network.
Types of nodes (ChatGPT)

  1. Full Nodes

Definition:

Full nodes store the entire blockchain history and validate all transactions and blocks according to the network's consensus rules.
Enter fullscreen mode Exit fullscreen mode

Characteristics:

Complete Ledger: Keeps a copy of the entire blockchain.
Validation: Verifies and validates all transactions and blocks.
Network Backbone: Provides data to other nodes and helps maintain the network's integrity.
Enter fullscreen mode Exit fullscreen mode

Examples:

Bitcoin Core in the Bitcoin network.
Geth (Go Ethereum) in the Ethereum network.
Enter fullscreen mode Exit fullscreen mode
  1. Light Nodes (Lightweight Nodes)

Definition:

Light nodes, also known as lightweight or simplified payment verification (SPV) nodes, store only a portion of the blockchain and rely on full nodes for transaction and block validation.
Enter fullscreen mode Exit fullscreen mode

Characteristics:

Partial Ledger: Stores only block headers or relevant parts of the blockchain.
Dependence on Full Nodes: Requests data from full nodes to verify transactions.
Resource Efficiency: Requires less storage and computational power.
Enter fullscreen mode Exit fullscreen mode

Examples:

Mobile wallets or light clients for Bitcoin and Ethereum.
Enter fullscreen mode Exit fullscreen mode
  1. Mining Nodes

Definition:

Mining nodes participate in the mining process by solving cryptographic puzzles to create new blocks and add them to the blockchain.
Enter fullscreen mode Exit fullscreen mode

Characteristics:

Proof of Work (PoW): Commonly found in PoW networks like Bitcoin.
High Computational Power: Uses significant computational resources to solve puzzles.
Block Creation: Competes to find the correct nonce and create new blocks.
Enter fullscreen mode Exit fullscreen mode

Examples:

ASIC miners for Bitcoin.
GPU miners for Ethereum (prior to Ethereum 2.0).
Enter fullscreen mode Exit fullscreen mode
  1. Staking Nodes (Validators)

Definition:

Staking nodes, or validators, are used in Proof of Stake (PoS) networks. They validate transactions and create new blocks based on the amount of cryptocurrency they hold and "stake" as collateral.
Enter fullscreen mode Exit fullscreen mode

Characteristics:

Proof of Stake (PoS): Participates in block validation and creation based on staked cryptocurrency.
Energy Efficiency: More energy-efficient than PoW mining.
Rewards: Earns rewards through staking rather than mining.
Enter fullscreen mode Exit fullscreen mode

Examples:

Validators in Ethereum 2.0.
Validators in Cardano and Tezos networks.
Enter fullscreen mode Exit fullscreen mode
  1. Masternodes

Definition:

Masternodes perform specialized functions beyond simple transaction validation, such as facilitating instant transactions, privacy features, and governance in certain blockchain networks.
Enter fullscreen mode Exit fullscreen mode

Characteristics:

Collateral Requirement: Requires a significant amount of cryptocurrency to operate.
Specialized Functions: Provides additional services like instant transactions and privacy.
Governance: Often participates in network governance and decision-making.
Enter fullscreen mode Exit fullscreen mode

Examples:

Masternodes in the Dash network.
Masternodes in the PIVX network.
Enter fullscreen mode Exit fullscreen mode
  1. Archival Nodes

Definition:

Archival nodes store the entire history of the blockchain, including all states and transactions. They provide historical data and are essential for developers and researchers.
Enter fullscreen mode Exit fullscreen mode

Characteristics:

Complete Storage: Keeps all historical data and states.
Resource Intensive: Requires significant storage capacity.
Data Availability: Provides comprehensive historical data for analysis.
Enter fullscreen mode Exit fullscreen mode

Examples:

Full archival nodes in Ethereum (maintaining all historical states).
Enter fullscreen mode Exit fullscreen mode
  1. Relay Nodes

Definition:

Relay nodes facilitate communication between nodes in geographically dispersed regions, helping to propagate transactions and blocks more efficiently.
Enter fullscreen mode Exit fullscreen mode

Characteristics:

Network Bridging: Acts as intermediaries to reduce latency and improve connectivity.
Propagation: Helps in faster dissemination of blocks and transactions.
Enter fullscreen mode Exit fullscreen mode

Examples:

Relay nodes in Bitcoin and Ethereum networks.
Enter fullscreen mode Exit fullscreen mode
  1. Oracles

Definition:

Oracles are specialized nodes that provide external data to smart contracts on the blockchain, allowing them to interact with real-world events.
Enter fullscreen mode Exit fullscreen mode

Characteristics:

Data Providers: Supplies real-world data to smart contracts.
Trust and Security: Ensures the accuracy and reliability of external data.
Enter fullscreen mode Exit fullscreen mode

Examples:

Chainlink oracles.
Oraclize (now Provable) oracles.
Enter fullscreen mode Exit fullscreen mode

Geth
It is used to make your PC a node
Depending upon it's installation it makes your PC into a node
After that, you can connect your PC to the mainnet, or even start mining if you would like to.
**
Smart contracts**
These are just programs stored on blockchain.
Assume that there is a transaction which should only happen when a logic if satisfied. Where shall we be coding that logic, the answer is smart contract.
Some features
Smart contracts are immutable as they get stored on blockchain
They have their own accounts where it can store cryptocurrency
No human intervention is required for cryptocurrency transfer or receiving

Solidity
It is an OOPs language for implementing smart contracts for the etherium blockchain
High-level statically typed programming language
Case sensitive
With solidity we can create contracts for voting, crowdfunding, blind auctions & multi-signature wallets.
**
SOLIDITY compilation process**
There is a .sol file
Solidity compiler takes the file and gives two files in output, the ABI and Byte code
Byte code is deployed on blockchain
ABI is used to interact with the smart contract's variables and functions
Bytecode is public in readable form

SPDX
To maintain trust in blockchain world, the codes can be protected using license.
Just add a comment containing "SPDX-License-Identifier:
to each source file
(Refer link for more : https://etherscan.io/contract-license-types )

Pragma
It specifies compiler version that the source file should use.
It is primarily of 3 types and their meaning

pragma solidity 0.8.0 Exact specified version required

pragma solidity ^0.8.0 Version should be specified one or newer that is backward compatible but not 0.9.0

pragma solidity >=0.5.0 <0.9.0 Any version from 0.5.0 upto but not including 0.9.0

Variables
These are of two types
1. State variable
Permanently stored in contract storage
Cost (gas expensive)
Reading of such variable is free but writing to it is costly.

2. local variable
Declared inside functions and are kept on the stack not on storage
Don't cost gas

Functions
When we declare a public state variable a getter function is automatically created.
We use some keywords which we describe to function type based on what the function is expected to do. Two popular types are : View & Pure

1. View :
Allows reading data from blockchain state but doesn't modify it
Suitable for functions that need to fetch and return data stored in the contract.

*2. Pure : *
It neither reads nor modifies the blockchain state.
Suitable for utility functions that perform calculations or operations based only on the provided input parameters.

# Good to know
The difficulty of a blockchain increases periodically.
Contract doesn't have to be public
Bytecode is immutable

💖 💪 🙅 🚩
arsh_the_coder
ASHDEEP SINGH

Posted on June 30, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related