#
Blockchain Basics
The blockchain, a concept constructed with modern-day protocols, networking, and cryptography, provides a variety of computational building blocks that open a whole new world of human interaction.
#
Architecture
Despite any grandiose claims from any particular blockchain platform, all blockchains are fundamentally the same from an architecture perspective. Any given blockchain is physically constructed of the same types of resources, consisting of a set of independently operated nodes talking to each other via an agreed-upon protocol to expose an append-only database.
A blockchain database is not organized by tables. Instead, it is organized by blocks that are linked cryptographically.
A hash function is a mathematical function that converts a numerical input value into another compressed numerical value. The input to the hash function is of arbitrary length but output is always of fixed length.
What does a hash look like? This is a very simple example in python:
import sha3
import json
k = sha3.keccak_256()
tx = {
'from': '0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf',
'to': '0x8C33DE6CbC21394350F8B4f3b1a7dC9e6f2d61Ed',
'value': 50000,
'nonce': 0,
'gas': 90000,
'gasPrice': 50000000000,
}
k.update(json.dumps(tx).encode('utf-8'))
print(k.hexdigest())
4e6428ce9f7e25aa033e46d4f3899e87adebd0e77d9fbb2e690c081b8837a902
#
Ethereum
Ethereum is a blockchain with a computer embedded in it, it has a single machine called Ethereum Virtual Machine (EVM), whose state must be agreed upon by everyone on the network,
Changing the state of the EVM can only be done via a transaction initiated by an account.
Although the examples in this workshop will be using the Polygon (Testnet) blockchain, an overwhelming number of blockchains, apart from obvisouly Ethereum, such as Avalanche, Polygon, BSC, and Harmony, use the same underlying state machine architecture to keep track of their database. In other words, the concepts introduced in this article may also be applied to other blockchains that are based on the Ethereum Virtual Machine (EVM).
#
EVM
EVM is a stack-based machine, which means that the results of computations are always stored at the top of the stack. The program counter keeps track of which program to execute next, and the stack is always instantiated whenever there is a contract call. As the stack is not persistent, it terminates at the end of the contract call. EVM is a deterministic machine, meaning that it always produces the same output for the same input, and its state can be replicated across all nodes on the Ethereum network.
#
Mainnet / Testnet
A Testnet is an Ethereum blockchain that uses identical technology and software as the “Mainnet” Ethereum blockchain. However, whereas the Mainnet network is used for “actual” transactions with “value”, Testnets are used for testing smart contracts and decentralized applications (“DApps”)
#
Nodes
An Ethereum node is simply any computer running the software needed to connect with the Ethereum network. Nodes connect with one another to send information back and forth to validate transactions and store data about the state of the blockchain.
#
Etherscan
Etherscan is a website through which you can view and analyse all assets, transactions and balances on the Ethereum network. You can also check gas fees, interact with smart contracts, find block data and search for airdrops with complete ease. In essence, it's like a search engine for the Ethereum network.
#
Accounts
Ethereum has two types of accounts, contract accounts, and externally owned account(EOA), the whole essence of an account is to allow us to initialize transaction that requests a change in the state of EVM
- Externally owned accounts (EOA) are controlled by anyone with a private and public key
- Contract accounts are accounts controlled by code deployed on the Ethereum blockchain.
An Account has the ability to hold, send and receive ether (the native token of Ethereum) and can interact with deployed smart contracts (a program that runs on the Ethereum blockchain)
An Ethereum address is your identity on the blockchain. It looks like this “0xd5e099c71b797516c10ed0f0d895f429c2781142”, Ethereum address is public and can be shared, but the private key must always be kept secret. Ethereum addresses are generated using a private key.
The following is the process of how an Ethereum address is generated:
- Generate a random private key of (64 (hex) characters / 256 bits / 32 bytes)
0xf4a2b939592564feb35ab10a8e04f6f2fe0943579fb3c9c33505298978b74893
- A Public key is derived from the private key (128 (hex) characters / 512 bits / 64 bytes) using Elliptic Curve Digital Signature Algorithm (ECDSA)
0x04345f1a86ebf24a6dbeff80f6a2a574d46efaa3ad3988de94aa68b695f09db9ddca37439f99548da0a1fe4acf4721a945a599a5d789c18a06b20349e803fdbbe3
- Then Keccak-256 hash function is applied on (128 characters / 64 bytes) public key, which gives out a (64 characters / 32 bytes) hash string, the last 40 characters / 20 bytes when prefixed with 0x is the Ethereum address.
0xd5e099c71b797516c10ed0f0d895f429c2781142
In an EVM address, there are 40 hexadecimal characters, prefaced by a 0x. Hexadecimal characters contain the numbers 0 to 9 and the letters a to f, meaning there are 16 possible character variations in any EVM public address.
With 16 different character types and 40 characters in total, we can obtain 16^{40} possible public address combinations. This is what that number looks like:
1,461,501,637,330,902,918,203,684,832,716,283,019,655,932,542,976
#
Transaction
The transaction is an action initiated by an external own account, which tries to change the state of the EVM, When these transactions are initiated, they are broadcasted to the whole network.
What does a transaction look like?
"tx": {
"nonce": "0x0",
"maxFeePerGas": "0x1234",
"maxPriorityFeePerGas": "0x1234",
"gas": "0x55555",
"to": "0x07a565b7ed7d7a678680a4c162885bedbb695fe0",
"value": "0x1234",
"input": "0xabcd",
"v": "0x26",
"r": "0x223a7c9bcf5531c99be5ea7082183816eb20cfe0bbc322e97cc5c7f71ab8b20e",
"s": "0x2aadee6b34b45bb15bc42d9c09de4a6754e7000908da72d48cc7704971491663",
"hash": "0xeba2df809e7a612a0a0d444ccfa5c839624bdc00dd29e3340d46df3870f8a30e"
}
A submitted transaction includes the following information:
- from – the address of the sender, that will be signing the transaction. This will be an externally-owned account as contract accounts cannot send transactions.
- recipient – the receiving address (if an externally-owned account, the transaction will transfer value. If a contract account, the transaction will execute the contract code)
- nonce - a sequentially incrementing counter which indicates the transaction number from the account
- signature – the identifier of the sender. This is generated when the sender's private key signs the transaction and confirms the sender has authorized this transaction
- value – amount of ETH to transfer from sender to recipient (denominated in WEI, where 1ETH equals 1e+18wei)
- data – optional field to include arbitrary data
- gasLimit – the maximum amount of gas units that can be consumed by the transaction. The EVM(opens in a new tab)↗ specifies the units of gas required by each computational step
- maxPriorityFeePerGas - the maximum price of the consumed gas to be included as a tip to the validator maxFeePerGas - the maximum fee per unit of gas willing to be paid for the transaction (inclusive of baseFeePerGas and maxPriorityFeePerGas)
On Ethereum there are a few different types of transactions:
- Regular transactions: a transaction from one account to another.
- Contract deployment transactions: a transaction without a 'to' address, where the data field is used for the contract code.
- Execution of a contract: a transaction that interacts with a deployed smart contract. In this case, 'to' address is the smart contract address.
#
Transaction life cycle
Once the transaction has been submitted the following happens:
- A transaction hash is cryptographically generated: 0x97d99bc7729211111a21b12c933c949d4f31684f1d6954ff477d0477538ff017
- The transaction is then broadcasted to the network and added to a transaction pool consisting of all other pending network transactions.
- A validator must pick your transaction and include it in a block in order to verify the transaction and consider it "successful".
- As time passes the block containing your transaction will be upgraded to "justified" then "finalized". These upgrades make it much more certain that your transaction was successful and will never be altered. Once a block is "finalized" it could only ever be changed by a network level attack that would cost many billions of dollars.