Module 5 Miss
Module 5 Miss
Ethereum
Major upgrades of Ethereum
Ethereum Blockchain
• Ethereum, just like any other blockchain, can be visualized as a transaction-based
state machine.
• This definition is mentioned in the Ethereum yellow paper written by Dr. Gavin
Wood.
• The core idea is that in the Ethereum blockchain, a genesis state is transformed
into a final state by executing transactions incrementally.
• The final transformation is then accepted as the absolute undisputed version of the
state.
Ethereum state transition function
• The state transition is achieved using what's called the Ethereum state transition function,
which works as follows:
1. Confirm the transaction validity by checking the syntax, signature validity, and nonce.
2. The transaction fee is calculated, and the sending address is resolved using the signature.
Furthermore, the sender's account balance is checked and subtracted accordingly, and the
nonce is incremented. An error is returned if the account balance is insufficient.
3. Provide enough ETH (the gas price) to cover the cost of the transaction. This is charged per
byte and is incrementally proportional to the size of the transaction. In this step, the actual
transfer of value occurs. The flow is from the sender's account to the receiver’s account.
The account is created automatically if the destination account specified in the transaction
does not exist yet. Moreover, if the destination account is a contract, then the contract code is
executed. This also depends on the amount of gas available. If enough gas is available, then
the contract code will be executed fully; otherwise, it will run up to the point where it runs out
of gas.
Accounts
4. In cases of transaction failure due to insufficient account balance or gas, all state
changes are rolled back except for the fee payment, which is paid to the miners.
5. Finally, the remainder (if any) of the fee is sent back to the sender as change and
the fee is paid to the miners accordingly. At this point, the function returns the
resulting state, which is also stored on the blockchain.
Types of accounts
• A transaction in Ethereum is a digitally signed data packet using a private key that
contains the instructions that, when completed, either result in a message call or
contract creation.
• Transactions can be divided into two types based on the output they produce:
• Message call transactions: This transaction simply produces a message call that
is used to pass messages from one CA to another.
• Contract creation transactions: As the name suggests, these transactions result
in the creation of a new CA. This means that when this transaction is executed
successfully, it creates an account with the associated code.
Transactions and messages
• Field inside the transaction:
• Nonce: The nonce is a number that is incremented by one every time a transaction
is sent by the sender. It must be equal to the number of transactions sent and is
used as a unique identifier for the transaction. A nonce value can only be used
once. This is used for replay protection on the network.
• Gas price: The gas price field represents the amount of Wei required to execute
the transaction. In other words, this is the amount of Wei we are willing to pay for
this transaction. This is charged per unit of gas for all computation costs incurred
as a result of the execution of this transaction.
• Gas limit: The gas limit field contains the value that represents the maximum
amount of gas that can be consumed to execute the transaction.
• To: As the name suggests, the To field is a value that represents the address of the
recipient of the transaction. This is a 20 byte value.
Transactions and messages
• Value: Value represents the total number of Wei to be transferred to the recipient
• Signature: The signature is composed of three fields, namely V, R, and S. These
values represent the digital signature (R, S) and some information that can be used
to recover the public key (V).
• Init: The Init field is used only in transactions that are intended to create
contracts, that is, contract creation transactions. This represents a byte array of
unlimited length that specifies the EVM code to be used in the account
initialization process. The code contained in this field is executed only once when
the account is created for the first time, it (init) gets destroyed immediately after
that. Init also returns another code section called the body, which persists and runs
in response to message calls that the CA may receive.
• Data: If the transaction is a message call, then the Data field is used instead of
init, and represents the input data of the message call. It is also unlimited in size
and is organized as a byte array.
Transactions and messages
• A transaction is a tuple of the fields mentioned earlier, which is then included in a
transaction trie (a modified Merkle-Patricia tree (MPT)) composed of the
transactions to be included.
• Finally, the root node of the transaction trie is hashed using a Keccak 256-bit
algorithm and is included in the block header along with a list of transactions in
the block
• A block is a data structure that contains batches of transactions.
• Transactions can be found in either transaction pools or blocks.
• In transaction pools, they wait for verification by a node, and in blocks, they are
added after successful verification.
• When a mining node starts its operation of verifying blocks, it starts with the
highest-paying transactions in the transaction pool and executes them one by one.
• When the gas limit is reached, or no more transactions are left to be processed in
the transaction pool, the mining starts.
Transactions and messages
• In this process, the block is repeatedly hashed until a valid nonce is found, such
that once hashed with the block, it results in a value less than the difficulty target.
• Once the block is successfully mined, it will be broadcasted immediately to the
network, claiming success, and will be verified and accepted by the network.
The Ethereum Virtual Machine (EVM)
• The Ethereum Virtual Machine (EVM) is a core piece of Ethereum that helps
power the blockchain and smart contracts. It is vital in assisting Ethereum to
achieve user adoption and decentralization.
• The Ethereum Virtual Machine (EVM) is the computation engine for Ethereum
that manages the state of the blockchain and enables smart contract functionality.
• EVM is contained within the client software
• The EVM participates in block creation and transaction execution. In block
creation, the EVM sets standards for managing the state from block to block.
These states are stored in a Merkle Patricia Trie
• The EVM is a simple stack-based execution machine that runs bytecode
instructions to transform the system state from one state to another.
• The code that runs on the EVM does not have access to any external resources
such as a network or filesystem.
• This results in increased security, deterministic execution, and allows untrusted
code (code that can be run by anyone) to be executed on Ethereum blockchain.
The Ethereum Virtual Machine (EVM)
• There are three main types of storage available for contracts and the EVM:
• Memory: The first type is called memory or volatile memory, which is a word
addressed byte array.
• Storage: The other type is called storage, which is a key-value store and is
permanently persisted on the blockchain
• Stack: EVM is a stack-based machine, and thus performs all computations in a
data area called the stack.
The Ethereum Virtual Machine (EVM)
• Diagram shows an EVM stack on the left side showing that elements are pushed
and popped from the stack.
• It also shows that a program counter is maintained, and is incremented with
instructions being read from the main memory.
• The main memory gets the program code from the virtual ROM/storage via the
CODECOPY instruction
Execution environment
• There are some key elements that are required by the execution environment to
execute the code.
• These are listed as follows:
• The system state.
• The remaining gas for execution.
• The address of the account that owns the executing code.
• The address of the sender of the transaction. This is the originating address of this
execution (it can be different from the sender).
• The gas price of the transaction that initiated the execution.
• Input data or transaction data depending on the type of executing agent. This is a
byte array; in the case of a message call, if the execution agent is a transaction,
then the transaction data is included as input data.
Execution environment
• The iterator function mentioned earlier performs various vital functions that are
used to set the next state of the machine and eventually the world state. These
functions include the following:
• It fetches the next instruction from a byte array where the machine code is stored
in the execution environment.
• It adds/removes ( PUSH/POP ) items from the stack accordingly.
• Gas is reduced according to the gas cost of the instructions/opcodes. It increments
the Program Counter (PC).
• The EVM is also able to halt in normal conditions if STOP , SUICIDE , or
RETURN opcodes are encountered during the execution cycle.
Blocks and blockchain
• Ethereum blocks consist of various elements, which are described as
follows:
• The block header
• The transactions list
• The list of headers of ommers or uncles
Blocks and blockchain
• The transaction list is simply a list of all transactions included in the block.
• Also, the list of headers of uncles is also included in the block.
• Block header: Block headers are the most critical and detailed components of an Ethereum block.
The header contains various elements, which are described in detail here:
• Parent hash: This is the Keccak 256-bit hash of the parent (previous) block's header.
• Ommers hash: This is the Keccak 256-bit hash of the list of ommers (or uncles) blocks included
in the block.
• The beneficiary: The beneficiary field contains the 160-bit address of the recipient that will
receive the mining reward once the block is successfully mined.
• State root: The state root field contains the Keccak 256-bit hash of the root node of the state trie.
It is calculated once all transactions have been processed and finalized.
• Transactions root: The transaction root is the Keccak 256-bit hash of the root node of the
transaction trie. The transaction trie represents the list of transactions included in the block.
Blocks and blockchain
• Receipts root: The receipts root is the Keccak 256-bit hash of the root node of the
transaction receipt trie. This trie is composed of receipts of all transactions included in
the block. Transaction receipts are generated after each transaction is processed and
contain useful post transaction information.
• Logs bloom: The logs bloom is a bloom filter that is composed of the logger address
and log topics from the log entry of each transaction receipt of the included transaction
list in the block.
• Difficulty: The difficulty level of the current block.
• Number: The total number of all previous blocks; the genesis block is block zero.
• Gas limit: This field contains the value that represents the limit set on the gas
consumption per block.
• Gas used: This field contains the total gas consumed by the transactions included in the
block.
Blocks and blockchain
• Timestamp: The timestamp is the epoch Unix time of the time of
block initialization.
• Extra data: The extra data field can be used to store arbitrary
data related to the block. Only up to 32 bytes are allowed in this
field.
• Mixhash: The mixhash field contains a 256-bit hash that, once
combined with the nonce, is used to prove that adequate
computational effort (Proof of Work, or PoW) has been spent in
order to create this block.
• Nonce: Nonce is a 64-bit hash (a number) that is used to prove, in
combination with the mixhash field, that adequate computational
effort (PoW) has been spent in order to create this block.
Blocks and blockchain
• The genesis block
The block validation mechanism
• Conversely, if the transaction that has an appropriate fee paid is included in the
block by miners but has too many complex operations to perform, it can result in
an out-of-gas exception if the gas cost is not enough.
• In this case, the transaction will fail but will still be made part of the block, and
the transaction originator will not get any refund.
• Transaction costs can be estimated using the following formula:
• Total cost = gasUsed * gasPrice
• Here, gasUsed is the total gas that is supposed to be used by the transaction during
the execution, and gasPrice is specified by the transaction originator as an
incentive to the miners to include the transaction in the next block.
Fee schedule
• Gas is charged in three scenarios as a prerequisite to the execution of
an operation:
• The computation of an operation
• For contract creation or message calls
• An increase in the use of memory
Tutorial
• Etherscan
• Difference between bitcoin and Ethereum