Monad 백서

Monad

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 12쪽

900 gas for overwriting an existing one do not apply. ### Cost examples Costs for consecutive operations on one account within a single transaction: | Sequence | Before MIP-8 | With MIP-8 | | - | - | - | | `SLOAD` a slot, first access to its page | 8100 | 8100 | | `SLOAD` another slot in the same page | 8100 | 100 | | `SSTORE` a fresh slot, first access to its page | 28,100 | 27,900 | | `SSTORE` a fresh slot in a page already written | 28,100 | 17,100 | | `SSTORE` over a non-zero slot, first access to its page | 11,000 | 10,900 | | `SSTORE` over a non-zero slot in a page already written | 11,000 | 100 | An [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) access list entry warms the whole page containing the listed key, and `eth_createAccessList` deduplicates storage keys by page. <Note> These changes are activated in the [`MONAD_TEN`](/developer-essentials/changelog#revisions) revision, defined in [MIP-8](https://mips.monad.xyz/MIPs/MIP-8). See [Releases](/developer-essentials/changelog/releases) for per-network activation timestamps. </Note> ## Precompiles A few [precompiles](/developer-essentials/precompiles) have been repriced to accurately reflect their relative costs in execution. | Precompile | Address | Ethereum | Monad | Multiplier | | - | - | - | - | - | | `ecRecover` | `0x01` | 3000 | 6000 | 2 | | `ecAdd` | `0x06` | 150 | 300 | 2 | | `ecMul` | `0x07` | 6000 | 30,000 | 5 | | `ecPairing` | `0x08` | 45,000 + 34,000 per point | 225,000 + 170,000 per point | 5 | | `blake2f` | `0x09` | $\text{rounds} * 1$ | $\text{rounds} * 2$ | 2 | | `point eval` | `0x0a` | 50,000 | 200,000 | 4 | A point is a 192-byte pair of `G1` and `G2` elements, as in [EIP-1108](https://eips.ethereum.org/EIPS/eip-1108). ## Memory expansion Memory expansion is priced linearly, and the memory a transaction can use is capped at 8 MB (8,388,608 bytes). | Item | Ethereum | Monad | | - | - | - | | Expansion cost | $3w + w^2 / 512$ | $w / 2$ | | Memory limit | Bounded by the gas limit | 8 MB per transaction | where $w$ is the memory size in 32-byte words. Expanding all the way to the 8 MB cap costs 131,072 gas. Memory is counted cumulatively across call frames: the memory available to a child call is 8 MB minus the memory already used by the current call and its parents. Memory returns to the pool once a call returns. Exceeding the limit halts the call frame exceptionally, consuming all the gas that frame was given and reverting its state changes. From the caller's perspective this is indistinguishable from an ordinary out-of-gas. <Note> These changes are activated in the [`MONAD_NINE`](/developer-essentials/changelog/releases#v0-13-0-monad_nine) revision. </Note> # Precompiles Source: https://docs.monad.xyz/developer-essentials/precompiles How to use Monad's precompiled contracts Precompiles are contracts at predefined addresses that provide cryptographic and utility functions implemented natively rather than in EVM bytecode. They are callable like any other contract via `CALL` or `STATICCALL`. Monad supports all Ethereum precompiles as of the Fusaka fork (`0x01` to `0x11`), plus three additional precompiles: * **`0x0100`** - [P256 signature verification](#p256-signature-verification) ([EIP-7951](https://eips.ethereum.org/EIPS/eip-7951)) * **`0x1000`** - [Staking precompile](#staking-precompile) * **`0x1001`** - [Reserve balance precompile](#reserve-balance-precompile) ## Ethereum precompiles ### Cryptographic and hashing | Address | Name | Gas | Description | | - | - | - | - | | `0x01` | `ecRecover` | `6000` | ECDSA public key recovery | | `0x02` | `sha256` | `60 + 12 * word_size` | SHA-256 hash function | | `0x03` | `ripemd160` | `600 + 120 * word_size` | RIPEMD-160 hash function | | `0x09` | `blake2f` | `rounds * 2` | BLAKE2 compression function F | ### Arithmetic and utility | Address | Name | Gas | Description | | - | - | - | - | | `0x04` | `identity` | `15 + 3 * word_size` | Returns the input unchanged | | `0x05` | `modexp` | [see evm.codes](https://www.evm.codes/precompiled) | Arbitrary-precision modular exponentiation | ### Elliptic curve alt\_bn128 | Address | Name | Gas | Description | | - | - | - | - | | `0x06` | `ecAdd` | `300` | Point addition on alt\_bn128 | | `0x07` | `ecMul` | `30,000` | Scalar multiplication on alt\_bn128 | | `0x08` | `ecPairing` | `225,000` | Bilinear pairing check on alt\_bn128 | ### KZG commitment | Address | Name | Gas | Description | | - | - | - | - | | `0x0a` | `point_eval` | `200,000` | KZG commitment verification ([EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#point-evaluation-precompile)) | ### BLS12-381 These precompiles provide operations on the BLS12-381 curve per [EIP-2537](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2537.md). | Address | Name | Gas | Description | | - | - | - | - | | `0x0b` | `bls12_g1_add` | `375` | Point addition in G1 | | `0x0c` | `bls12_g1_msm` | [see EIP-2537](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2537.md) | Multi-scalar multiplication in G1 | | `0x0d` | `bls12_g2_add` | `600` | Point addition in G2 | | `0x0e` | `bls12_g2_msm` | [see EIP-2537](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2537.md) | Multi-scalar multiplication in G2 | | `0x0f` | `bls12_pairing_check` | [see EIP-2537](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2537.md) | Pairing check over (G1, G2) point pairs | | `0x10` | `bls12_map_fp_to_g1` | `5500` | Map base field element to G1 point | | `0x11` | `bls12_map_fp2_to_g2` | `23800` | Map extension field element to G2 point | ## P256 signature verification Address `0x0100` verifies signatures on the `secp256r1` (P256) elliptic curve per [EIP-7951](https://eips.ethereum.org/EIPS/eip-7951). <Note> EIP-7951 supersedes [RIP-7212](https://github.com/ethereum/RIPs/blob/master/RIPS/rip-7212.md). If you are migrating from a chain that uses RIP-7212 at `0x100`, note that the address and interface are identical — only the EIP designation has changed. </Note> The P256 curve is used by WebAuthn, Apple Secure Enclave, Android Keystore, and hardware security modules. With this precompile, you can verify passkey signatures on-chain, enabling biometric authentication flows for smart accounts without relying on off-chain signature verification. ### Input and output The precompile accepts exactly **160 bytes**: | Offset | Size | Parameter | Description | | - | - | - | - | | 0 | 32 bytes | `hash` | Message hash | | 32 | 32 bytes | `r` | Signature component r | | 64 | 32 bytes | `s` | Signature component s | | 96 | 32 bytes | `qx` | Public key x-coordinate | | 128 | 32 bytes | `qy` | Public key y-coordinate | All values are big-endian encoded as 256-bit unsigned integers. **Returns:** * `0x0000...0001` (32 bytes) on a valid signature * Empty bytes on an invalid signature or malformed input **Gas cost:** 6900 ### Solidity example ```solidity theme={null} address constant P256_VERIFY = address(0x0100); function verifyP256Signature( bytes32 hash, uint256 r, uint256 s, uint256 qx, uint256 qy ) internal view returns (bool) { (bool success, bytes memory result) = P256_VERIFY.staticcall( abi.encodePacked(hash, r, s, qx, qy) ); return success && result.length == 32 && abi.decode(result, (uint256)) == 1; } ``` For passkey infrastructure and embedded wallet providers on Monad, see [Embedded Wallets](/tooling-and-infra/wallet-infra/embedded-wallets). ## Staking precompile Address `0x1000` provides the interface for validator delegation, undelegation, reward claims, and staking queries. Gas costs vary by function. * [Staking overview](/reference/staking/overview) — key concepts, common actions, and constraints * [Staking API reference](/reference/staking/api) — full method signatures, parameters, gas costs, events, and ABI ## Reserve balance precompile Address `0x1001` exposes a single method, `dippedIntoReserve()` (selector `0x3a61584e`, gas cost `100`), which returns a `bool` indicating whether the current execution state is in [reserve balance](/developer-essentials/reserve-balance) violation. It is specified in [MIP-4](https://mips.monad.xyz/MIPs/MIP-4). `dippedIntoReserve()` must be invoked via `CALL`. Invoking it via `STATICCALL` - or via `DELEGATECALL` or `CALLCODE` - reverts. Although it reads state and returns a value, it is intentionally not a `view` function, so that a Solidity call site compiles to `CALL` rather than `STATICCALL`. ## Gas pricing differences A few precompiles (`0x01`, `0x06`, `0x07`, `0x08`, `0x09`, `0x0a`) are repriced relative to Ethereum to reflect their relative costs in Monad's execution environment. The gas values in the tables above reflect Monad's pricing. See [Opcode pricing](/developer-essentials/opcode-pricing#precompiles) for a comparison. ## Source code See the [precompile implementation](https://github.com/category-labs/monad/blob/main/category/execution/ethereum/precompiles.cpp). # Reserve Balance Source: https://docs.monad.xyz/developer-essentials/reserve-balance ## Introduction The **Reserve Balance** mechanism is a set of light constraints - at consensus time on which transactions can be **included**, and at execution time on which transactions **don't revert** - which allow Monad to simultaneously support [asynchronous execution](/monad-arch/consensus/asynchronous-execution) and [EIP-7702](/developer-essentials/eip-7702). The Reserve Balance mechanism is designed to preserve safety under asynchronous execution without interfering with normal usage patterns. **Most users and developers need not worry about the Reserve Balance constraints**, however we provide the details here for those encountering corner cases. ## Summary Asynchronous execution means that nodes achieve consensus on a block proposal prior to executing the transactions in that block. Execution is required to be completed in the next `k` (delay factor) blocks. (Currently `k=3`. *NOTE*: In the context of discussing [block states](/monad-arch/consensus/block-states) and [asynchronous execution](/monad-arch/consensus/asynchronous-execution), letter `D` is usually used to denote the same parameter.) Because consensus operates on a `k`-block delayed view of the global state, it is necessary to adjust the consensus and execution rules slightly to allow consensus to safely build and validate blocks that include only transactions whose gas costs can be paid for. Monad introduces the **Reserve Balance** mechanism to allow consensus and execution to collaborate across a multi-block lag to ensure that all EOAs must have enough MON in their account to pay for gas for any transaction included in the blockchain. <Info title="Inflight transaction"> Throughout this document, an **inflight transaction** refers to a transaction that has been included in a block less than `k` blocks ago. </Info> Here is a very brief summary of the rules: * From the perspective of a particular EOA, MON spent from that EOA in the course of a transaction is partitioned into two parts: **gas spend** and **value spend**. * In the case where the EOA was the sender: * **gas spend** is `gas_price * gas_limit`; * **value spend** is the `value` parameter on that transaction. * In the case where the EOA wasn't the sender (where they previously delegated via EIP-7702 and some other EOA submitted a transaction which called this EOA): * **gas spend** is 0; * **value spend** is whatever MON is sent out during the course of executing this EOA's code. * Let `user_reserve_balance = 10 MON` * *Execution time*: during execution, transactions revert due to **value spend** when that account's ending balance (before refunds) dips below `user_reserve_balance`, except in certain circumstances described below. * *Consensus time*: For each account, consensus has a budget for the **gas spend** for all inflight transactions; this budget is `user_reserve_balance` (or the account's balance from the lagged execution state, whichever is lower). The budget is further reduced if the first inflight transaction earned the exception mentioned above, by that transaction's **value spend**. When performing block validity checks for block `n`, consensus checks that the budget is not exceeded. <Info> See also the formal definition in the [Monad Initial Spec](https://category-labs.github.io/category-research/monad-initial-spec-proposal.pdf) proposal from Category Labs. </Info> ## Parameters | Parameter | Value | | - | - | | `user_reserve_balance` | 10 MON | ## Why is reserve balance needed? Monad has asynchronous execution: consensus is allowed to progress with building and validating blocks without waiting for execution to catch up. Specifically, proposing and validating consensus block `n` only requires knowledge of the state obtained after applying block `n-k`. While asynchronous execution has performance benefits, it introduces a novel challenge: how is consensus supposed to know the validity of a block if it does not have the latest state? Let’s illustrate this challenge with an example (for our examples, we will use `k = 3`): Consensus is validating block 4, which contains a transaction `t` from Alice with the relevant fields as: ``` sender=Alice, to=Bob, value=100, gas=1 ``` Consensus only has the state that was obtained by executing block 1: ``` block=1, balances={Alice: 110} ``` If consensus simply accepts block 4 as valid because Alice appears to have enough balance, it risks a safety failure. For instance, Alice may have already spent her balance in transaction `t’` in block 2. This creates a denial-of-service (DoS) vector, as Alice could cause consensus to include many transactions for free. ## First attempt at a solution One idea is for the consensus client to statically inspect transactions in blocks 2 and later, checking if Alice has spent any value in her transactions. This would let consensus reject block 4 as invalid if any transaction before `t` (such as `t'`) in blocks 2, 3, or 4 originates from Alice and spends some value or gas. While this is a fine solution on the face of it, it suffers from two shortcomings: 1. Suppose, as part of smart contract execution in blocks 2 or 3, Alice received a lot of currency. She would have had enough balance to pay for transaction `t` despite `t'` existing, if only we had the latest state. So, rejecting transactions based solely on static checks is overly restrictive. 2. It is not only restrictive, it is also not safe with EIP-7702. With EIP-7702, Alice could have her account delegated to a smart contract, which can transfer out currency from Alice’s account in a way that is not statically inspectable by consensus. Concretely in our example, Alice does not need to send a transaction like `t'` from her account in order to spend currency from her account, if her account is delegated. A spend could potentially be triggered by a transaction submitted by anyone else. So our static check would not succeed and it may be unsafe to accept block 4 as valid even if we don’t see any other transaction from Alice in blocks 2, 3 and 4. ## Reserve balance as the solution ### Simple version Intuitively, the core idea of reserve balance is as follows: if consensus and execution agree ahead of time that, for each EOA, execution will prevent the ending balance from dropping below a certain pre-determined threshold known to consensus (`user_reserve_balance`), up to the sender's **gas spend** allowance, then consensus can safely include a series of transactions whose running **gas spend** stays below `user_reserve_balance`, without knowing the latest state and without being vulnerable to the DoS vector described above. This concept can be generalized as follows: * *Execution time*: after execution (before gas refunds), a reserve-balance check is applied to the ending balance. For a non-sender account, the ending balance must not be lower than `min(balance at transaction start, user_reserve_balance)`. For the sender, the ending balance may be lower by at most the transaction's **gas spend** (unless the emptying exception below applies). Excessive intermediate debits during execution are allowed as long as the ending balance is sufficient. * *Consensus time*: For each account, consensus has a budget for the **gas spend** for all inflight transactions; this budget is `user_reserve_balance` (or the account's balance from the lagged execution state, whichever is lower). When performing block validity checks for block `n`, consensus checks that the budget is not exceeded. In Monad, `user_reserve_balance` is currently set to `10 MON` for each EOA. The above rule is sufficient to ensure all transactions included in consensus can be paid for, thus solving our problem. However, it has a drawback, which is that EOAs can't spend all of their MON, and EOAs with balances below `user_reserve_balance` won't be able to send any successful transactions. For instance, the following behaviors might be desired, but are currently blocked by the above rule (with `user_reserve_balance` set to `10 MON`): * Alice has a balance of 5 MON and wants to send 4.99 MON to Bob (plus pay 0.01 MON in gas) * Alice has a balance of 20 MON and wants to swap 18 MON into a memecoin (plus pay 0.01 MON in gas) To address this, we add some additional conditions under which transactions are allowed. ### Addressing the drawback First let's define an "emptying transaction": <Info title="Emptying Transaction"> A transaction is an **"emptying transaction"** iff * the sender is undelegated. * the sender has not sent any other transaction within the past `k` blocks. * nobody has sent a delegation or undelegation request for the sender within the past `k` blocks, including in this transaction. Such transactions are eligible for the emptying exception (see rules below). </Info> Notice that if a user account is **not** EIP-7702-delegated, then consensus can simply inspect transactions statically in order to estimate the lowest a user’s balance can possibly go. (This is because an undelegated user’s account can only be debited due to value transfers and gas fees specified in the transaction data). Therefore, we add the following exception to the reversion rule: 3. *Execution policy*: for each **undelegated** account sender: * *if* a transaction is an emptying transaction * *then* allow that transaction to proceed anyway (an "emptying transaction"). 4. *Consensus policy*: for each **undelegated** account sender: * *if* a transaction is an emptying transaction * *then* statically inspect that transaction's total MON needs (i.e. `gas_bid * gas_limit + value`), and take into account the fact that execution will still allow this transaction through. This means that for any subsequent transactions in the next `k` blocks, the reserve balance that consensus is working with will be lower by `value`. This rule lets execution allow consistently-undelegated accounts to dip below the reserve balance once every `k` blocks. Since `k` blocks is 1.2 seconds, this policy should allow most small accounts to still interact with the blockchain normally. Note that an account is not eligible for the exception criteria if there was recent (within `k` blocks) undelegation request for the account even if it was already undelegated at that time. See [Full specification](#full-specification) for details. The additional policy allows both of the examples mentioned at the end of the previous section, as long as they are the first transaction sent by the sender in `k` blocks. ## Discussion ### EIP-7702-delegated accounts If an EOA is not EIP-7702-delegated, then the reserve balance is rarely invasive, even if the EOA's balance is very low, because most transactions are the first transaction in `k=3` blocks sent by that EOA. But what if the EOA *is* EIP-7702-delegated? What is the impact? * The only transactions that will revert are ones in which the balance of an EIP-7702-delegated EOA
프로젝트로 돌아가기