Study Notes on Ethereum Virtual Machine (EVM) - The Basics

Kumar Abhishek's avatar Kumar Abhishek 6 min read

Ethereum Accounts §

  • There are two types of Ethereum accounts:
    • External Accounts
      • Users
      • Address = public key
    • Contract Accounts
      • Smart Contracts (code)
      • Address generated when the contract is created (it is derived from the creator address and the number of transactions sent from that address, the so-called “nonce”).
  • EVM treats both types of accounts equally.
  • Every account has a persistent key-value store mapping 256-bit words to 256-bit words called storage.
  • Every account has a balance in Ether (in “Wei” to be exact, 1 ether is 10**18 wei) which can be modified by sending transactions that include Ether.

Ethereum Transactions §

  • Message that is sent from one account to another account (which might be the same or empty).
  • It can include binary data (payload) and Ether.
  • If the target account contains code, that code is executed and the payload is provided as input data.
  • If the target account is not set (the transaction does not have a recipient or the recipient is set to null), the transaction creates a new contract.
    • the address of that contract is not the zero address, but an address derived from the sender and its number of transactions sent (the “nonce”).
    • The payload of such a contract creation transaction is taken to be EVM bytecode and executed.
    • The output data of this execution is permanently stored as the code of the contract.
    • This means that in order to create a contract, you do not send the actual code of the contract, but in fact code that returns that code when executed.
    • ⚠ While a contract is being created, its code is still empty. Because of that, you should not call back into the contract under construction until its constructor has finished executing.

Gas §

  • Gas price is set by the creator of the transaction.
    • They have to pay gas_price * gas upfront from the sending account.
  • Any remaining gas after execution is refunded back to the sender account.
  • If all gas is used up before the transaction is complete, an out-of-gas exception is triggered and all modifications to the state are reverted.

Storage, Memory, and the Stack §

#background: #FDF6E3 #stroke: #33322E #direction: down [EVM [Non-Volatile [<frame> ROM | code] [<frame> RAM | storage] ]--[Volatile [<database> stack] [<frame> ROM | args] [<frame> RAM | memory] ] ]EVMNon-VolatileROM
														</g>
													</g>
													<g transform="translate(0, 31)" font-family="Helvetica" font-size="12pt" font-weight="normal" font-style="normal" data-name="ROM">
														<g transform="translate(8, 8)" fill="#33322E" text-align="left" data-name="ROM">
															<text x="0.0" y="13.5" stroke="none" data-name="ROM">code</text>
															
														</g>
													</g>
												</g>
												<g data-name="RAM">
													<g fill="#eee8d5" stroke="#33322E" data-name="RAM">
														<rect x="111.0" y="0.0" height="62.0" width="76.0" data-name="RAM"></rect>
														<path d="M111.0 31.0 L156.3 31.0 L171.8 15.5 L171.8 0.0" fill="none" data-name="RAM"></path>
													</g>
													<g transform="translate(111, 0)" font-family="Helvetica" font-size="12pt" font-weight="normal" font-style="normal" data-name="RAM">
														<g transform="translate(8, 8)" fill="#33322E" text-align="left" data-name="RAM">
															<text x="0.0" y="13.5" stroke="none" data-name="RAM">RAM</text>
															
														</g>
													</g>
													<g transform="translate(111, 31)" font-family="Helvetica" font-size="12pt" font-weight="normal" font-style="normal" data-name="RAM">
														<g transform="translate(8, 8)" fill="#33322E" text-align="left" data-name="RAM">
															<text x="0.0" y="13.5" stroke="none" data-name="RAM">storage</text>
															
														</g>
													</g>
												</g>
											</g>
										</g>
									</g>
								</g>
								<g data-name="Volatile">
									<g fill="#fdf6e3" stroke="#33322E" data-name="Volatile">
										<rect x="0.0" y="181.0" height="141.0" width="343.5" data-name="Volatile"></rect>
									</g>
									<g transform="translate(0, 181)" font-family="Helvetica" font-size="12pt" font-weight="bold" font-style="normal" data-name="Volatile">
										<g transform="translate(8, 8)" fill="#33322E" text-align="center" data-name="Volatile">
											<text x="163.8" y="13.5" stroke="none" text-anchor="middle" data-name="Volatile">Volatile</text>
											<g transform="translate(20, 20)" data-name="Volatile">
												<g data-name="ROM">
													<g fill="#eee8d5" stroke="#33322E" data-name="ROM">
														<rect x="0.0" y="0.0" height="62.0" width="70.5" data-name="ROM"></rect>
														<path d="M0.0 31.0 L47.3 31.0 L62.8 15.5 L62.8 0.0" fill="none" data-name="ROM"></path>
													</g>
													<g transform="translate(0, 0)" font-family="Helvetica" font-size="12pt" font-weight="normal" font-style="normal" data-name="ROM">
														<g transform="translate(8, 8)" fill="#33322E" text-align="left" data-name="ROM">
															<text x="0.0" y="13.5" stroke="none" data-name="ROM">ROM</text>
															
														</g>
													</g>
													<g transform="translate(0, 31)" font-family="Helvetica" font-size="12pt" font-weight="normal" font-style="normal" data-name="ROM">
														<g transform="translate(8, 8)" fill="#33322E" text-align="left" data-name="ROM">
															<text x="0.0" y="13.5" stroke="none" data-name="ROM">args</text>
															
														</g>
													</g>
												</g>
												<g data-name="RAM">
													<g fill="#eee8d5" stroke="#33322E" data-name="RAM">
														<rect x="111.0" y="0.0" height="62.0" width="79.0" data-name="RAM"></rect>
														<path d="M111.0 31.0 L156.3 31.0 L171.8 15.5 L171.8 0.0" fill="none" data-name="RAM"></path>
													</g>
													<g transform="translate(111, 0)" font-family="Helvetica" font-size="12pt" font-weight="normal" font-style="normal" data-name="RAM">
														<g transform="translate(8, 8)" fill="#33322E" text-align="left" data-name="RAM">
															<text x="0.0" y="13.5" stroke="none" data-name="RAM">RAM</text>
															
														</g>
													</g>
													<g transform="translate(111, 31)" font-family="Helvetica" font-size="12pt" font-weight="normal" font-style="normal" data-name="RAM">
														<g transform="translate(8, 8)" fill="#33322E" text-align="left" data-name="RAM">
															<text x="0.0" y="13.5" stroke="none" data-name="RAM">memory</text>
															
														</g>
													</g>
												</g>
												<g data-name="stack">
													<g fill="#eee8d5" stroke="#33322E" data-name="stack">
														<rect x="230.0" y="16.0" height="35.0" width="58.0" stroke="none" data-name="stack"></rect>
														<path d="M230.0 16.0 L230.0 47.0" fill="none" data-name="stack"></path>
														<path d="M288.0 16.0 L288.0 47.0" fill="none" data-name="stack"></path>
														<ellipse cx="258.5" cy="16.0" rx="29.0" ry="6.0" data-name="stack"></ellipse>
														<path d="M287.5 47.0 L287.5 47.3 L287.4 47.6 L287.2 47.9 L286.9 48.2 L286.6 48.5 L286.2 48.8 L285.8 49.1 L285.2 49.3 L284.6 49.6 L284.0 49.9 L283.2 50.1 L282.5 50.4 L281.6 50.6 L280.7 50.9 L279.8 51.1 L278.7 51.3 L277.7 51.5 L276.6 51.7 L275.4 51.9 L274.2 52.0 L273.0 52.2 L271.7 52.3 L270.4 52.5 L269.1 52.6 L267.7 52.7 L266.4 52.8 L265.0 52.8 L263.5 52.9 L262.1 53.0 L260.7 53.0 L259.2 53.0 L257.8 53.0 L256.3 53.0 L254.9 53.0 L253.5 52.9 L252.0 52.8 L250.6 52.8 L249.3 52.7 L247.9 52.6 L246.6 52.5 L245.3 52.3 L244.0 52.2 L242.8 52.0 L241.6 51.9 L240.4 51.7 L239.3 51.5 L238.3 51.3 L237.2 51.1 L236.3 50.9 L235.4 50.6 L234.5 50.4 L233.8 50.1 L233.0 49.9 L232.4 49.6 L231.8 49.3 L231.2 49.1 L230.8 48.8 L230.4 48.5 L230.1 48.2 L229.8 47.9 L229.6 47.6 L229.5 47.3 L229.5 47.0" data-name="stack"></path>
													</g>
													<g transform="translate(230, 20)" font-family="Helvetica" font-size="12pt" font-weight="bold" font-style="normal" data-name="stack">
														<g transform="translate(8, 8)" fill="#33322E" text-align="center" data-name="stack">
															<text x="21.0" y="13.5" stroke="none" text-anchor="middle" data-name="stack">stack</text>
															
														</g>
													</g>
												</g>
											</g>
										</g>
									</g>
								</g>
							</g>
						</g>
					</g>
				</g>
			</g>
		</g>
	</g>
</g>

  • Storage
    • Non-Volatile: persistent between function calls & transactions.
    • It’s a key-value store that maps 256-bit words to 256-bit words.
    • not possible to enumerate storage from within a contract.
    • is comparatively costly to read, and even more to initialize and modify storage.
    • Because of this cost, you should minimize what you store in persistent storage.
    • Store data like derived calculations, caching, and aggregates outside of the contract.
    • A contract can neither read nor write to any storage apart from its own.
  • Memory
    • Volatile: fresh instance for each message call.
    • it is linear and can be addressed at the byte level.
    • reads are limited to a width of 256 bits.
    • writes can be either 8 bits or 256 bits wide.
    • Memory is expanded by a word (256-bit) when accessing (either reading or writing) a previously untouched memory word (i.e. any offset within a word).
    • At the time of expansion, the cost in gas must be paid.
    • Memory is more costly the larger it grows (it scales quadratically).
  • Stack
    • all computations are performed on the stack.
    • maximum size of 1024 elements and contains words of 256 bits.
    • It is possible to copy one of the topmost sixteen elements to the top of the stack or swap the topmost element with one of the sixteen elements below it.
    • All other operations take the topmost two (or one, or more, depending on the operation) elements from the stack and push the result onto the stack.
    • it is possible to move stack elements to storage or memory to get deeper access to the stack.

Message Calls §

  • Contracts can call other contracts or send Ether to non-contract accounts by the means of message calls.
  • Message calls are like transactions, in fact, every transaction consists of a top-level message call which in turn can create further message calls.
  • Calls are limited to a depth of 1024, which means that for more complex operations, loops should be preferred over recursive calls.
  • Furthermore, only 63/64th of the gas can be forwarded in a message call, which causes a depth limit of a little less than one thousand in practice.

Delegatecall / Callcode and Libraries §

  • The delegatecall is a special variant of a message call.
  • code at the target address is executed in the context of the calling contract and msg.sender and msg.value do not change their values.
  • This means that a contract can dynamically load code from a different address at runtime.
  • Storage, current address, and balance still refer to the calling contract, only the code is taken from the called address.
  • This makes it possible to implement the “library” feature in Solidity.

Logs §

  • Specially indexed data structure that maps all the way up to the block level.
  • This feature called logs is used by Solidity to implement events.
  • Contracts cannot access log data after it has been created.
  • But they can be efficiently accessed from outside the blockchain.
  • Since some part of the log data is stored in bloom filters, it is possible to search for this data in an efficient and cryptographically secure way, so even “light clients” can still find these logs.

Create §

  • Contracts can even create other contracts using a special opcode (i.e. they do not simply call the zero address as a transaction would).
  • The only difference between these create calls and the normal message calls is that the payload data is executed, and the result stored as code, and the caller/creator receives the address of the new contract on the stack.

Deactivate and Self-destruct §

  • The only way to remove code from the blockchain is when a contract at that address performs the selfdestruct operation.
  • Remaining Ether stored at that address is sent to a designated target and then the storage and code is removed from the state.
  • It is potentially dangerous as if someone sends Ether to removed contracts, the Ether is forever lost.
  • If you want to deactivate your contracts, you should instead disable them by changing some internal state which causes all functions to revert. This makes it impossible to use the contract, as it returns Ether immediately.
All Notes Suggest Edit