NOTE: This is a draft.
在上篇 An Overview of Ethereum 中,只是粗略的去了解了一下 ethereum 中的一
些基础概念。而要想深入 Ethereum,必然少不了 EVM。EVM 就是 Ethereum 的灵魂。
EVM
EVM execution context:
- The Code: smart contract byte code which is immutable, it is stored on-chain
and referenced using a contract address - The Stack: call stack, an empty stack is initialized for each EVM contract
execution - The Memory: contract memory, a clean memory is initialized for each EVM
contract execution - The Storage: contract storage which is persisted across executions, it’s
stored on-chain and is referenced via a contract address and its storage slot. - The Call Data: the input data for a transaction
- The Return Data: the data returned from a contract function call
Tips: zero function signature selector: wfjizxua()
, it’s signature is0x00000000
The main difference between DELEGATECALL
and CALL
is which storage the
contract access. CALL
will access the contract’s storage which contains the
function, DELEGATECALL
will access the contract’s storage which call this
function.
JUMP
: jump to the location of the top of the stack valueJUMPI
: if the top of the stack value is 0 , jump to the location of the second
of the stack value, otherwise no jumpJUMPDEST
: mark a location as a valid jump target. If the target location do
not contain a JUMPDEST opcode, the execution will fail.
OPCODES
contract creation
push1 80
push1 40
a sign for contract creation0x60806040
opcode: push1 -> 60
evm operation base on 32 bytes
why 80 and 40, this is hex,
0x80
is 128 in decimal, one hex stands for 4 bits,
32 bytes = 256 bits = 64 hex string = 0x40
in memory:
80 stands for start point of the memory pointer, max length 32 bytes0x00
- 0x40
reserver for hash compute0x40
- 0x80
storing the memory pointer
more details:0x00
- 0x3f
(64 bytes): scratch space, can be used between statements i.e.
within inline assembly and for hashing methods.0x40
- 0x5f
(32 bytes): free memory pointer, free memory pointer, currently
allocated memory size, start location of free memory, 0x80
initially0x60
- 0x7f
(32 bytes): zero slot, is used as an initial value for dynamic
memory arrays and should never be written to.
so free memory pointer assambly is:
1 | PUSH1 80 |
The hex code is : 0x6080604052
function selector
In EVM OPCODE:
function signature (4 bytes) + function arguments ( each is 32 bytes)
function signature:
keccak256(“function-name(arg1-type, arg2-type, …)”) and get the first 4 bytes,
8 hex string.
function selector, compare with the function signature, with the function entry
program counter(PC), if eq, then jump to the location which pc point to.
1 | PUSH1 0x0 |
step 1: free memory pointer
1 | PUSH1 80 |
step 2: calldata check
1 | PUSH1 04 |
step 3: function selector
1 | // extract first four bytes of calldata(function signature hash) |
function wrapper
function entry point
function body
the content of a function
Metahash
associate with swarm storing the source code.