CS/블록체인응용

9. Ethereum Transactions and Smart Contract

호프 2023. 11. 29. 03:06

Overview

  • Data such as account balances aare not stored directly in the blocks of the Ethereum blockchain. Only the relevant hash values are stored directly in the blockchain.
  • Ethereum use the Account/Balance Model
    • Simplicity: more intuitive - smart contract keep track of states to perform different tasks
      • UTXO's stateless model force transactions to include state information so that complicates the design of the contracts
    • Efficiency: only needs to validate that sending account has enough balance

 

Ethereum Account

Ethereum Account

  • Account is made of a cryptographic pair of keys: public & private
    • help prove that a tx was actually signed by the sender
  • Private key is used to sign tx
  • User hold private keys - the funds are always on Ethereum's ledger

 

Two Types of Account

EOA (Externally-owned account)

  • controlled by anyone with the private keys
  • creating account costs nothing
  • can initiate transactions
  • tx btw EOA can only be ETH/token transfers
  • made up of a cryptographic pair of keys: public and private keys that control account

Contract Account

  • a smart contract deployed to the network, controlled by code
  • creating a contract has a cost because it's using network storage
  • can only send tx in response to receiving a tx
  • tx from EOA to contract account can trigger code which can execute different actions
  • don't have private keys. instead, controlled by the logic of the smart contract code
Both account types have the ability to
- recieve, hold, send ETH & tokens
- interact with deployed smart contracts

Ethereum Transactions

Transactions

  • cryptographically signed instructions from accounts.
    • account will initiate a tx to update the state of the Ethereum network.
    • the simplest tx = transferring ETH from one account to another
  • refers to an action initiated by an EOA
  • Tx which change the state of the EVM need to be boadcast to the whole network.
  • Any node can broadcast a request for a tx to be executed on the EVM
    • after this, validator will execute the tx and propagate the resulting state change to the rest of the network.

Information included in Tx

  • recipient: receivng address
    • if EOA: transfer value / if contract account: execute the contract code
  • signature: identifier of the sender
  • nonce: sequencially incrementing couter which indicates the tx # of the account !== nonce in PoW
  • value: amount of ETH to transfer (in Wei: 10^9 wei = 1 ETH)
  • data: optional field to include arbitrary data
  • gasLimit: maximum amount of gas units that can be consumed by the tx
  • maxPriorityFeePerGas: maximum amount of gas to be included as a tip to the validator
  • maxFeePerGas: maximum amount of gas willing to be paid for the tx
    • inclusive of baseFeePerGas and maxPriorityFeePerGas

 

Types of Transactions

Regular Transactions

  • Transaction from one account to another

Contract deployment transactions

  • Transaction without a 'to' address, where the data field is used for the contract code

Executioin of a contact

  • Transaction that interacts with a deployed smart contract
  • 'to' address is the smart contract address

 

Gas

Transactions cost gas to execute

  • simple transfer tx = require 21000 units of gas
  • Gas = Gas Fee * Gas amount = (baseFeePerGas + maxPriorityFeePerGas) * gas amount
    • base fee will be burned to manage supply for price (to avoid over-supply)
    • validator keeps the tip

Smart Contract

Smart Contract

  • computer programs stored on the blockchain
  • terms of an agreement into computer code that automatically executes when the contract terms are met.

 

Properties of Smart Contracts

Automatic execution

  • outcome is automatically executed when the conditiosn are met -> no need to wait for a human to execute
  • 👉 Smart contracts remove the need for trust.

Predictable outcomes

  • human can interpret a traditional contract in different ways -> lead to failure
  • Smart contracts remove the possibility of different interpretations
    • execute precisel based on the conditions written within the contract's code
  • given the same circumstances, the smart contract will produce the same result

Public record

  • Smart contract is useful for audits and tracking because they're on a public blockchain
  • anyone can instantly track asset transfers and other related information.

Privacy protection

  • Smart contract can protect your privacy
  • Ethereum is a pseudonymous network = tx are tied to a unique cryptographic address, not your identity

Visible terms

  • You can check what's in a smart contract before you sign it.
  • public transparency of the terms in the contract means that anyone can scrutinize it

 

Use cases of Smart contract

Smart contracts can do essentially anothing that other computer programs do.

  • stablecoins, NFT, currency exchange, decentralized gaming ...

 

Smart Contracts Language

  • Ethereum Smart contracts can be programmed using relatively developer-friendly languages

Solidity

  • Object-oriented, high-level language for implementing smart contracts
  • similar with C++
  • Statically typed (the type of a variable is known at compile time)
  • Supports: Inheritance, Libraries, Complex user-defined types

Vyper

  • similar with Python
  • Stroing typing
  • Small and understandable compiler code
  • Efficient bytecode generation
  • Has less features than Solidity with the aim of making contracts more secure and easier to audit.

Remix

  • Remix IDE allows developing, deploying, and administering smarts contracts