06 Smart Contract
06 Smart Contract
Zhejiang University
Credits: Campbell R. Harvey, Ashwin Ramachandran, Brent Xu, Anastasia Mavridou, Aron Laszka
Background: Ethereum
Ethereum
• Ethereum node
• Ethereum
• Transactions
• Smart Contracts
• Accounts:
• Two Kinds:
• Contract Accounts
• Wallets:
• Types
• Hold funds
• Contain code
• Gas used
Gastransfer
ETH Used byisTxn: Actual
21,000 gas amount of gas used to execute the
also 21,000
Gas Price: Amount of ETH a user is prepared to pay for each unit of gas.
The user chose to pay 8 Gwei for every gas unit, which is considered a
Eth Gas Station
Miner
• Voting systems
• Cryptocurrencies
• Gaming
• Lottery
• …
EVM: Ethereum Virtual Machine
• The runtime bytecode, on the other hand, is just the bytecode that
ends up sitting on the blockchain. This does not include the bytecode
needed to initialize the contract and place it on the blockchain.
Bytecode vs. Runtime Bytecode
• https://ethervm.io/decompile
Deploy a Contract on Ethereum Network
https://medium.com/coinmonks/transactions-in-ethereum-e85a73068f74
https://medium.com/coinmonks/transactions-in-ethereum-e85a73068f74
https://medium.com/coinmonks/transactions-in-ethereum-e85a73068f74
https://medium.com/coinmonks/transactions-in-ethereum-e85a73068f74
Deploy Smart Contracts
• In the transaction, the to is left empty (‘0x0’ is shown).
• The default Gas Limit (gas) is 90,000 gas. If you do not specify the gas, you
will encounter “out of gas” as it takes more than 90,000 gas for processing
this transaction. Therefore we specify 200,000 gas for this transaction.
• It turns out the transaction processing only takes 112,213 gas. The remain
is returned to transaction sender.
https://medium.com/coinmonks/transactions-in-ethereum-e85a73068f74
Execute a Function on a Deployed Contract
Function Selectors: which function to call
https://dasp.co/
Methods of calling functions
• Call is like:
Fallback function
The DAO Attack
• Looping until:
• Out of gas
https://etherscan.io/address/0xe82719202e5965Cf5D9B6673B7503a3b92DE20be#code
Overflow
Background
What’s the problem
_feeSmt = 8fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
value = 7000000000000000000000000000000000000000000000000000000000000001
__feeSmt + value = 0
attacker
Short Address Attack
Overview
https://ericrafaloff.com/analyzing-the-erc20-short-address-attack/
https://ericrafaloff.com/analyzing-the-erc20-short-address-attack/
First try
https://ericrafaloff.com/analyzing-the-erc20-short-address-attack/
Second try
512 = 2<<8
https://ericrafaloff.com/analyzing-the-erc20-short-address-attack/