mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
EIPs: update code explain eip-1559.md
This commit is contained in:
parent
64ee02849e
commit
25c65c196c
1 changed files with 48 additions and 11 deletions
|
|
@ -50,11 +50,10 @@ EIP-1559에서는 이전 블록의 사이즈가 목표 블록 사이즈보다
|
||||||
|
|
||||||
## Code
|
## Code
|
||||||
|
|
||||||
```python
|
### python implementation
|
||||||
from typing import Union, Dict, Sequence, List, Tuple, Literal
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from abc import ABC, abstractmethod
|
|
||||||
|
|
||||||
|
legacy, 2930, 1559등 트랜잭션 별 dataclass를 정의합니다.
|
||||||
|
```python
|
||||||
@dataclass
|
@dataclass
|
||||||
class TransactionLegacy:
|
class TransactionLegacy:
|
||||||
signer_nonce: int = 0
|
signer_nonce: int = 0
|
||||||
|
|
@ -149,19 +148,46 @@ class Account:
|
||||||
balance: int = 0
|
balance: int = 0
|
||||||
storage_root: int = 0
|
storage_root: int = 0
|
||||||
code_hash: int = 0
|
code_hash: int = 0
|
||||||
|
```
|
||||||
|
|
||||||
|
초기 base fee, fork block number(London) 등 base fee 계산에 필요한 상수값을 정의합니다.
|
||||||
|
|
||||||
|
```python
|
||||||
INITIAL_BASE_FEE = 1000000000
|
INITIAL_BASE_FEE = 1000000000
|
||||||
INITIAL_FORK_BLOCK_NUMBER = 10 # TBD
|
INITIAL_FORK_BLOCK_NUMBER = 10 # TBD
|
||||||
BASE_FEE_MAX_CHANGE_DENOMINATOR = 8
|
BASE_FEE_MAX_CHANGE_DENOMINATOR = 8
|
||||||
ELASTICITY_MULTIPLIER = 2
|
ELASTICITY_MULTIPLIER = 2
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
아래 코드는 eip 1559 이후 이더리움 블록의 유효성을 검증하는 핵심 로직을 구현한 것입니다. 주요 기능은 다음과 같습니다:
|
||||||
|
|
||||||
|
1. 부모 블록의 가스 한도와 목표량 계산
|
||||||
|
- 부모 블록의 가스 한도와 가스 목표량을 계산합니다.
|
||||||
|
- 포크 블록의 경우, 가스 목표량이 과도하게 줄어드는 것을 방지하기 위해 별도로 처리합니다.
|
||||||
|
|
||||||
|
2. 블록의 가스 한도 검증
|
||||||
|
- 블록의 실제 가스 사용량이 블록의 가스 한도 내에 있는지 확인합니다.
|
||||||
|
- 블록의 가스 한도가 부모 블록에 비해 너무 많이 증가/감소하지 않았는지 확인합니다.
|
||||||
|
- 블록의 가스 한도가 최소 가스 한도 이상인지 검증합니다.
|
||||||
|
|
||||||
|
3. 블록의 base fee 검증
|
||||||
|
- 부모 블록의 base fee와 가스 사용량을 기반으로 예상되는 base fee를 계산합니다.
|
||||||
|
- 블록의 실제 base fee가 예상 값과 일치하는지 확인합니다.
|
||||||
|
|
||||||
|
4. 트랜잭션 실행 및 가스 회계 수행
|
||||||
|
- 트랜잭션 서명과 체인 ID를 먼저 검증한 후 트랜잭션을 정규화합니다.
|
||||||
|
- 트랜잭션 송신자가 연결된 가치와 가스 비용을 커버할 수 있는 이더 잔액이 있는지 확인합니다.
|
||||||
|
- 사용자가 최소한 base fee를 지불할 의사가 있는지 확인합니다.
|
||||||
|
- 트랜잭션 파라미터에 불가능할 정도로 큰 숫자가 포함되지 않도록 합니다.
|
||||||
|
- 트랜잭션을 실행하고, 실효 가스 가격(base fee + priority fee)을 송신자 잔액에서 차감하며, 사용되지 않은 가스를 환불합니다.
|
||||||
|
- priority fee를 채굴자 계정에 적립합니다.
|
||||||
|
|
||||||
|
5. 총 가스 사용량 검증
|
||||||
|
- 블록의 총 가스 사용량이 모든 트랜잭션의 가스 사용량 합계와 일치하는지 확인합니다.
|
||||||
|
|
||||||
|
```python
|
||||||
class World(ABC):
|
class World(ABC):
|
||||||
# EIP-1559에서 제안된 baseFee 계산 방식과 블록 검증 로직 구현
|
|
||||||
# geth, consensus/misc/eip1559/eip1559.go
|
|
||||||
# VerifyEIP1559Header: EIP-1559이후 변경된 헤더 속성을 검증
|
|
||||||
# CalcBaseFee: header의 basefee를 계산
|
|
||||||
# geth, consensus/misc/gaslimit.go
|
|
||||||
# VerifyGaslimit은 header gas limit관 parentgas limit의 증가/감소관계를 확인한다.
|
|
||||||
def validate_block(self, block: Block) -> None:
|
def validate_block(self, block: Block) -> None:
|
||||||
parent_gas_target = self.parent(block).gas_limit // ELASTICITY_MULTIPLIER
|
parent_gas_target = self.parent(block).gas_limit // ELASTICITY_MULTIPLIER
|
||||||
parent_gas_limit = self.parent(block).gas_limit
|
parent_gas_limit = self.parent(block).gas_limit
|
||||||
|
|
@ -271,8 +297,11 @@ class World(ABC):
|
||||||
# 따라서 블록에 대한 유효성 검사를 수행할 때는 블록 헤더에 있는 상태 루트와 실제 계정의 상태를 비교하여 일치하는지 확인해야 함
|
# 따라서 블록에 대한 유효성 검사를 수행할 때는 블록 헤더에 있는 상태 루트와 실제 계정의 상태를 비교하여 일치하는지 확인해야 함
|
||||||
# TODO: validate the rest of the block
|
# TODO: validate the rest of the block
|
||||||
# 나머지 블록 검증
|
# 나머지 블록 검증
|
||||||
|
```
|
||||||
|
|
||||||
# 트랜잭션 정규화
|
여러 표준의 트랜잭션을 정규화합니다.
|
||||||
|
|
||||||
|
```python
|
||||||
def normalize_transaction(self, transaction: Transaction, signer_address: int) -> NormalizedTransaction:
|
def normalize_transaction(self, transaction: Transaction, signer_address: int) -> NormalizedTransaction:
|
||||||
# legacy transactions
|
# legacy transactions
|
||||||
if isinstance(transaction, TransactionLegacy):
|
if isinstance(transaction, TransactionLegacy):
|
||||||
|
|
@ -337,6 +366,10 @@ class World(ABC):
|
||||||
def account(self, address: int) -> Account: pass
|
def account(self, address: int) -> Account: pass
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### geth implementation
|
||||||
|
|
||||||
|
VerifyEIP1559Header 함수는 gas limit, basefee를 확인해서 EIP-1559이후 변경된 헤더 속성을 검증합니다.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// VerifyEIP1559Header verifies some header attributes which were changed in EIP-1559,
|
// VerifyEIP1559Header verifies some header attributes which were changed in EIP-1559,
|
||||||
// - gas limit check
|
// - gas limit check
|
||||||
|
|
@ -366,7 +399,11 @@ func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Heade
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
CalcBaseFee는 header의 basefee를 계산합니다.
|
||||||
|
|
||||||
|
```go
|
||||||
// CalcBaseFee calculates the basefee of the header.
|
// CalcBaseFee calculates the basefee of the header.
|
||||||
// CalcBaseFee는 header의 basefee를 계산한다.
|
// CalcBaseFee는 header의 basefee를 계산한다.
|
||||||
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
|
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue