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.
Where it lands in Jayverse
- Verex: audit for unbounded loops before they become a DoS vector. Settlement and order processing must not scale gas linearly with participant count; replace any array iteration without a cap with pull-based claims where users pay their own gas.
- Verex: run a two-version gas report in CI. Compare a storage-write-heavy path against a calldata/event-heavy path, and break down which opcodes dominate, to catch an underpriced operation before it ships.
- CI: track per-opcode gas regression for Verex and Bridge contracts on every PR. A single underpriced operation is a DoS vector regardless of intent, so the check belongs in CI, not a one-time review.
Key expressions
| Expression | 뜻 · 쓰이는 자리 |
|---|---|
| DoS vector | 서비스 거부 공격의 통로(취약점) · 잘못 책정된 비용이 공격 수단이 될 때. "even one underpriced operation becomes a DoS vector" |
| repriced | 가격(비용)이 재산정된 · 과거 책정이 잘못돼 나중에 다시 조정될 때. "have been repriced several times in the past" |
| shape the incentives around | ~을 둘러싼 유인 구조를 설계하다 · 비용 정책이 행동을 유도하는 방식. "shaping the incentives around state growth" |
| cold / warm access | 최초 접근(cold)과 재접근(warm)을 구분하는 것 · 캐시 지역성을 요금에 반영하는 개념. "charges differently for a first (cold) access versus" |
| fold into | ~에 녹여 넣다·포함시키다 · 세부 요소까지 가격 모델에 반영할 때. "even cache locality being folded into the pricing model" |
| stall ... for pennies | 푼돈으로 (시스템을) 멈춰 세우다 · 아주 적은 비용으로 큰 피해를 입히는 공격. "an attacker can stall a node for pennies" |
| priced out | 비용 부담으로 밀려나다·못 쓰게 되다 · 가격이 너무 비싸 정상 사용자가 배제될 때. "honest users get priced out" |
| EIP-2929 | 이더리움 개선제안 2929번(콜드/웜 접근에 따라 가스비를 다르게 책정) · 캐시 지역성을 가스 모델에 반영한 표준. "EIP-2929, which charges differently for a first (cold) access" |
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/.