無知

갈 길이 먼 공부 일기

기술 공부/블록체인

스마트 컨트랙트 (4-2) | 가스

moozii 2022. 4. 12. 23:40

@Diagram adapted from Ethereum EVM illustrated https://ethereum.org/static/c3638b26a1210d2c73a7ec2335c57351/302a4/gas-tx.png

 

 

1. 가스의 개념

1-1. 정의

- 가스란, 이더리움 블록체인 내 트랜잭션, 컨트랙트를 실행하는 데 필요한 계산 작업의 측정 단위

Gas refers to the unit that measures the amount of computational effort required to execute specific operations on the Ethereum network. Since each Ethereum transaction requires computational resources to execute, each transaction requires a fee. Gas refers to the fee required to conduct a transaction on Ethereum successfully.

@minimalism, Ethereum.org, 2022-03-10
https://ethereum.org/en/developers/docs/gas/#what-is-gas 

 

1-2. 크기

- 코드의 실행에 필요한 연산 작업량에 비례

- 비트코인의 transaction fee와 같이 트랜잭션 크기에 비례하지는 않음

 

1-3. 단위

Gas fees are paid in Ethereum's native currency, ether (ETH). Gas prices are denoted in gwei, which itself is a denomination of ETH - each gwei is equal to 0.000000001 ETH (10-9 ETH). For example, instead of saying that your gas costs 0.000000001 ether, you can say your gas costs 1 gwei. The word 'gwei' itself means 'giga-wei', and it is equal to 1,000,000,000 wei. Wei itself (named after Wei Dai, creator of b-money) is the smallest unit of ETH. 

@minimalism, Ethereum.org, 2022-03-10
https://ethereum.org/en/developers/docs/gas/#what-is-gas 

 

 

 

 

2. 가스의 구성 요소

2-1. 개요

- 가스 = 가스 비용 * 가스 가격

 

2-2. 가스 가격, Gas Price

- 가스 당 얼마의 이더리움을 지불할 것인지

- 트랜잭션을 만드는 sender가 결정

- 채굴업자가 가스 가격이 높은 트랜잭션을 우선해서 연산

- 가스 가격을 높이 제시할수록 트랜잭션 승인이 빠름

 

2-3. 가스 비용, Gas Cost

- 연산 작업에 드는 비용

- 덧셈 연산에는 3 가스 소요

- 트랜잭션 전송 연산에는 21000 가스 소요

- 황서깃헙 레포를 통해 확인 가능

 

2-4. 가스 한도, Gas Limit

- 해당 트랜잭션 내 사용할 최대 비용 설정량

- 가스 한도를 초과할 경우, 즉 트랜잭션 내 남은 가스가 없을 경우 실행 중지 및 작업 무효화

- 단, 가스 한도 초과로 무효화해도 이미 소비한 가스량은 지불됨. 잔여 가스량만 반환.

- 가스 한도를 설정해 스마트 컨트랙트 버그로 인한 과다 비용 지불을 방지

- 가스 한도는 실제 필요량보다 넉넉히 설정해야 트랜잭션 중단을 방지

 

2-5. Transaction Fee 계산

- Remaining Gas = Gas Limit - Gas Cost

- Refuned Ether = Remaining Gas * Gas Price

- Transaction Fee = Gas Price * Gas Cost Total Gas Used

 

 

 

 

3. London Upgrade 이후의 계산

Starting with the London network upgrade, every block has a base fee, the minimum price per unit of gas for inclusion in this block, calculated by the network based on demand for block space. As the base fee of the transaction fee is burnt, users are also expected to set a tip (priority fee) in their transactions. The tip compensates miners for executing and propagating user transactions in blocks and is expected to be set automatically by most wallets.

Calculating the total transaction fee works as follows: Gas units (limit) * (Base fee + Tip)

(...)

Before the London Upgrade, Ethereum had fixed-sized blocks. In times of high network demand, these blocks operated at total capacity. As a result, users often had to wait for high demand to reduce to get included in a block, which led to a poor user experience. The London Upgrade introduced variable-size blocks to Ethereum. Each block has a target size of 15 million gas, but the size of blocks will increase or decrease in accordance with network demand, up until the block limit of 30 million gas (2x the target block size). The protocol achieves an equilibrium block size of 15 million on average through the process of tâtonnement. This means if the block size is greater than the target block size, the protocol will increase the base fee for the following block. Similarly, the protocol will decrease the base fee if the block size is less than the target block size. The amount by which the base fee is adjusted is proportional to how far the current block size is from the target

Every block has a base fee which acts as a reserve price. To be eligible for inclusion in a block the offered price per gas must at least equal the base fee. The base fee is calculated independently of the current block and is instead determined by the blocks before it - making transaction fees more predictable for users. When the block is mined this base fee is "burned", removing it from circulation. The base fee is calculated by a formula that compares the size of the previous block (the amount of gas used for all the transactions) with the target size. The base fee will increase by a maximum of 12.5% per block if the target block size is exceeded. This exponential growth makes it economically non-viable for block size to remain high indefinitely. Relative to the pre-London gas auction market, this transaction-fee-mechanism change causes fee prediction to be more reliable.

(...)

One of the main benefits of the London upgrade is improving the user's experience when setting transaction fees. For wallets that support the upgrade, instead of explicitly stating how much you are willing to pay to get your transaction through, wallet providers will automatically set a recommended transaction fee (base fee + recommended priority fee) to reduce the amount of complexity burdened onto their users.

The implementation of EIP-1559 in the London Upgrade made the transaction fee mechanism more complex than the previous gas price auction, but it has the advantage of making gas fees more predictable, resulting in a more efficient transaction fee market. Users can submit transactions with a maxFeePerGas corresponding to how much they are willing to pay for the transaction to be executing, knowing that they will not pay more than the market price for gas (baseFeePerGas), and get any extra, minus their tip, refunded.

@minimalism, Ethereum.org, 2022-03-10
https://ethereum.org/en/developers/docs/gas/ 
 

Home | ethereum.org

Ethereum is a global, decentralized platform for money and new kinds of applications. On Ethereum, you can write code that controls money, and build applications accessible anywhere in the world.

ethereum.org