Gas Accounting Design TODO
Concept
Gas is a mechanism that quantifies compute, state storage, and bandwidth consumption into a single accounting unit and charges for it. Every opcode's gas value needs to be proportional to its actual resource consumption; even one underpriced operation becomes a DoS vector (this is why Ethereum's state-access and storage costs have been repriced several times in the past). Writes that grow state are made expensive, while operations that free state are given a refund, shaping the incentives around state growth. EIP-2929, which charges differently for a first (cold) access versus a repeat (warm) access to the same slot or address, is an example of even cache locality being folded into the pricing model. In the end, the block gas limit is an upper bound that caps a block's worst-case execution time so nodes can keep up.
If the cost model diverges from actual resource consumption, an attacker can stall a node for pennies, and if it's overpriced instead, honest users get priced out.
Code & Formula
# 가스 회계 설계 — opcode별 비용을 합산하고, cold/warm 접근 차등 과금(EIP-2929)과 블록 가스 한도를 시뮬레이션한다.
BASE_COST = {"ADD": 3, "MUL": 5, "SLOAD": 100, "SSTORE_SET": 20000}
COLD_SURCHARGE = 2000 # 슬롯 첫 접근(cold)에 추가로 붙는 과금
BLOCK_GAS_LIMIT = 30000
def run_tx(ops):
gas_used = 0
warm_slots = set() # 트랜잭션 안에서 이미 접근한 슬롯은 이후 warm
trace = []
for op, *arg in ops:
cost = BASE_COST[op]
if op in ("SLOAD", "SSTORE_SET") and arg:
slot = arg[0]
if slot not in warm_slots:
cost += COLD_SURCHARGE # cold 접근: 자원 소비가 더 크다고 보고 과금
warm_slots.add(slot)
gas_used += cost
trace.append((op, arg, cost, gas_used))
return gas_used, trace
# 슬롯 x 를 두 번 읽는 트랜잭션: 첫 접근은 cold, 두 번째는 warm(캐시 지역성을 요금에 반영)
ops = [("SLOAD", "x"), ("ADD",), ("SLOAD", "x"), ("SSTORE_SET", "y"), ("MUL",)]
gas_used, trace = run_tx(ops)
for op, arg, cost, cum in trace:
print(f" {op}{arg or ''}: cost={cost:>6} 누적={cum}")
print(f"\n총 가스: {gas_used}, 블록 한도: {BLOCK_GAS_LIMIT}, 한도 내: {gas_used <= BLOCK_GAS_LIMIT}")
# DoS 저항 사고 실험: 이 트랜잭션을 블록 하나에 최대 몇 번 담을 수 있는가
max_repeats = BLOCK_GAS_LIMIT // gas_used
print(f"이 트랜잭션을 블록 하나에 최대 {max_repeats}번 담을 수 있음 (가스가 실행량을 캡핑)")
docs/code/algorithms/algorithms-26.py
Exercise
Implement the same logic in two versions — one centered on storage writes, one centered on calldata and events — pull a gas report for each, and break down which opcodes dominate the cost.
Practical Connection
Verex's settlement and order processing need to finish within the block gas limit no matter how many participants there are, so avoiding unbounded array iteration and shifting cost to users via a pull-based claim design is necessary.
If you study this on a given day, add a note link and a ✅ to this line in the source curriculum (docs/knowledge/dev-100-curriculum.md) and this spot will lead straight to the note body. You can also write directly on this page — but regenerating overwrites it, so it's safer to keep anything you want to save as markdown under docs/algorithms/.