티스토리 뷰
빨간색 글씨는 추후 연구 후에 update 예정
EVM(Ethereum Virtual machine)은 스마트 컨트랙트의 Byte Code를 실행하는 32Byte Stack 기반의 실행 환경으로 최대 크기는 1024Byte이다. 이더리움의 각 노드는 EVM을 포함하고 있으며, EVM을 통해 컨트랙트의 Byte Code를 Op코드로 변환 후 내부에서 실행한다.
[그림1] Op code Example
<Op code operation>
0s: Stop and Arithmetic Operations
0x00 STOP Halts execution
0x01 ADD Addition operation
0x02 MUL Multiplication operation
0x03 SUB Subtraction operation
0x04 DIV Integer division operation
0x05 SDIV Signed integer
0x06 MOD Modulo
0x07 SMOD Signed modulo
0x08 ADDMOD Modulo
0x09 MULMOD Modulo
0x0a EXP Exponential operation
0x0b SIGNEXTEND Extend length of two's complement signed integer
10s: Comparison & Bitwise Logic Operations
0x10 LT Lesser-than comparison
0x11 GT Greater-than comparison
0x12 SLT Signed less-than comparison
0x13 SGT Signed greater-than comparison
0x14 EQ Equality comparison
0x15 ISZERO Simple not operator
0x16 AND Bitwise AND operation
0x17 OR Bitwise OR operation
0x18 XOR Bitwise XOR operation
0x19 NOT Bitwise NOT operation
0x1a BYTE Retrieve single byte from word
20s: SHA3
0x20 SHA3 Compute Keccak-256 hash
30s: Environmental Information
0x30 ADDRESS Get address of currently executing account
0x31 BALANCE Get balance of the given account
0x32 ORIGIN Get execution origination address
0x33 CALLER Get caller address. This is the address of the account that is directly responsible for this execution
0x34 CALLVALUE Get deposited value by the instruction/transaction responsible for this execution
0x35 CALLDATALOAD Get input data of current environment
0x36 CALLDATASIZE Get size of input data in current environment
0x37 CALLDATACOPY Copy input data in current environment to memory This pertains to the input data passed with the message call instruction or transaction
0x38 CODESIZE Get size of code running in current environment
0x39 CODECOPY Copy code running in current environment to memory
0x3a GASPRICE Get price of gas in current environment
0x3b EXTCODESIZE Get size of an account's code
0x3c EXTCODECOPY Copy an account's code to memory
40s: Block Information
0x40 BLOCKHASH Get the hash of one of the 256 most recent complete blocks
0x41 COINBASE Get the block's beneficiary address
0x42 TIMESTAMP Get the block's timestamp
0x43 NUMBER Get the block's number
0x44 DIFFICULTY Get the block's difficulty
0x45 GASLIMIT Get the block's gas limit
50s Stack, Memory, Storage and Flow Operations
0x50 POP Remove item from stack
0x51 MLOAD Load word from memory
0x52 MSTORE Save word to memory
0x53 MSTORE8 Save byte to memory
0x54 SLOAD Load word from storage
0x55 SSTORE Save word to storage
0x56 JUMP Alter the program counter
0x57 JUMPI Conditionally alter the program counter
0x58 PC Get the value of the program counter prior to the increment
0x59 MSIZE Get the size of active memory in bytes
0x5a GAS Get the amount of available gas, including the corresponding reduction
0x5b JUMPDEST Mark a valid destination for jumps
60s & 70s: Push Operations
0x60 PUSH1 Place 1 byte item on stack
0x61 PUSH2 Place 2-byte item on stack
…
0x7f PUSH32 Place 32-byte (full word) item on stack
80s: Duplication Operations
0x80 DUP1 Duplicate 1st stack item
0x81 DUP2 Duplicate 2nd stack item
…
0x8f DUP16 Duplicate 16th stack item
90s: Exchange Operations
0x90 SWAP1 Exchange 1st and 2nd stack items
0x91 SWAP2 Exchange 1st and 3rd stack items
… …
0x9f SWAP16 Exchange 1st and 17th stack items
a0s: Logging Operations
0xa0 LOG0 Append log record with no topics
0xa1 LOG1 Append log record with one topic
… …
0xa4 LOG4 Append log record with four topics
f0s: System operations
0xf0 CREATE Create a new account with associated code
0xf1 CALL Message-call into an account
0xf2 CALLCODE Message-call into this account with alternative account's code
0xf3 RETURN Halt execution returning output data
0xf4 DELEGATECALL Message-call into this account with an alternative account's code, but persisting the current values for `sender` and `value`
Halt Execution, Mark for deletion
0xff SELFDESTRUCT Halt execution and register account for later deletion
reference: https://ethereum.stackexchange.com/questions/119/what-opcodes-are-available-for-the-ethereum-evm
EVM은 Op코드의 program counter를 0부터 증가시키면서 연산을 수행한다. 최종적으로 error가 나거나 STOP, RETURN명령어 또는 실행이 완료될 때까지 Op코드를 계속 실행하면서 Gas를 소비한다. 만약 Gas가 충부하지 않거나, 잘못된 명령어이거나, 스택의 항목이 1024를 넘어 overflow가 발생하는 등의 예외 상황이 발생하면 실행이 중단되고 모늗 상태변화 내용은 취소된다.(Q. 아마 이미 소비된 Gas는 돌려받지 못하는 것으로 알고 있다.)
EVM의 특징들(수정 필요)
1. 32Byte word size를 지원
2. 메모리 크기가 가변적이고 스택의 크기에 제한이 없다.
3. 반복 호출 횟수(call depth)를 1024로 제한하여 함수 반복 호출을 통한 공격으로 성능이 저하되거나 보안 문제가 발생하는 것을 방지하였다.
4. 메모리의 크기가 가변적이고 스택의 크기에 제한이없다.(Q스택이 무제한으로 늘어나도 EVM의 크기랑 무관한 것인가? 이 글의 맨앞에서 EVM의 최대 크기는 1024Byte라고 하지 않았는가? - core ethereum progamming책 P.111)
5. Stack 기반 가상 머신이다. 이는 EVM 명령어를 저장하는 데 필요한 저장 공간을 최대한 줄이기 위해서 이다.(일반 컴퓨터는 레지스터 기반 머신이다.)
[그림2] STACK기반 가상머신의 저장공간에서의 장점
Stack 영역
- 32Byte size로 저장
- 스택의 최대 크기는 1024개로 제한
- 내부 함수 호출도 마치 일반적인 연산처럼 EVM stack을 사용
- 다른 컨트랙트의 method를 호출할 때 메시지 콜을 위한 스택을 사용(메시지 콜 크기도 1024로 제한)
Call Data 영역 (Zero Byte => 4Gas // Non-Zero Byte => 68Gas)
- ethereum에 transaction을 요청했을 때 전송되는 데이터들이 기록되는 저장 공간(어느 경우인지 코드를 통해 다시 이해)
Log영역 (32Byte당 96Gas)
- 스마트 컨트랙트가 실행될 때 부가적인 정보를 기록하기 위한 공간
- Log영역의 데이터를 읽으려면 web3.js를 이용하여 DApp을 개발해야 한다.
Storage 영역 (32Byte당 20,000Gas)
- Blockchain에 영구적으로 기록하기 위한 저장 공간이다.
- Key(32Byte)/Value(32Byte)를 mapping하기 위한 구조이다.
Memory 영역 (32Byte당 1Gas)
- 함수를 호충하거나 메모리 연산을 수행할 때 임시로 사용
- 메시지 호출이 발생할 때마다 초기화된 메모리 영역이 컨트랙트에 제공된다.
- 읽기: 32Byte단위
- 쓰기: 1Byte or 32Byte 모두 가능