You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* docs: stabilize Rex4 — promote unstable previews to current behavior
* address review: replace DIFFICULTY with PREVRANDAO, explain view annotation on system contracts
Copy file name to clipboardExpand all lines: docs/AGENTS.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -86,7 +86,7 @@ Use these exact forms consistently. Do not alternate between variants.
86
86
-**GitBook Markdown**: Use GitBook-flavored Markdown with custom blocks. See `/skill.md` for full syntax reference.
87
87
-**One sentence, one line**: Each sentence goes on its own line for better diffs.
88
88
-**Frontmatter**: Every page must have YAML frontmatter with at least `description` for SEO.
89
-
-**Headings**: Use `#` for the page title (H1), `##` for sections (H2), `###` for subsections (H3). One H1 per page. Use sentence case: "Gas forwarding rules" not "Gas Forwarding Rules." Capitalize only proper nouns (MegaETH, Ethereum, Rex3).
89
+
-**Headings**: Use `#` for the page title (H1), `##` for sections (H2), `###` for subsections (H3). One H1 per page. Use sentence case: "Gas forwarding rules" not "Gas Forwarding Rules." Capitalize only proper nouns (MegaETH, Ethereum, Rex4).
90
90
-**Serial commas**: Use serial (Oxford) commas in lists of three or more: "compute gas, storage gas, and detention gas."
Copy file name to clipboardExpand all lines: docs/dev/AGENTS.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,7 +81,7 @@ For the complete storage gas schedule, see the [formal specification](https://do
81
81
- Use `{% hint style="success" %}` for practical tips and best practices.
82
82
- Use tables for reference data (gas costs, resource limits, contract addresses).
83
83
- Use `<details>` for optional deep-dives that most developers can skip.
84
-
- Use `<details>` with a `<summary>` label like `"Rex4 (unstable): ..."` for upcoming behavior changes in unstable specs, mirroring the convention used in the EVM specification (maintained in the [mega-evm repo](https://github.com/megaeth-labs/mega-evm)). This keeps the main content focused on the current stable spec while giving developers early visibility into upcoming changes.
84
+
- Use `<details>` with a `<summary>` label like `"<SpecName> (unstable): ..."` for upcoming behavior changes in unstable specs, mirroring the convention used in the EVM specification (maintained in the [mega-evm repo](https://github.com/megaeth-labs/mega-evm)). This keeps the main content focused on the current stable spec while giving developers early visibility into upcoming changes. At present, all specs are stable.
Copy file name to clipboardExpand all lines: docs/dev/execution/overview.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ It is fully compatible with Ethereum smart contracts while introducing optimizat
10
10
## Overview
11
11
12
12
MegaEVM builds on established standards.
13
-
Its latest hardfork, _Rex3_, is based on [Optimism Isthmus](https://specs.optimism.io/protocol/isthmus/overview.html), which in turn is adapted from [Ethereum Prague](https://ethereum.org/roadmap/pectra/).
13
+
Its latest hardfork, _Rex4_, is based on [Optimism Isthmus](https://specs.optimism.io/protocol/isthmus/overview.html), which in turn is adapted from [Ethereum Prague](https://ethereum.org/roadmap/pectra/).
14
14
This means:
15
15
16
16
- All standard Solidity contracts work on MegaETH.
@@ -39,7 +39,7 @@ For the full gas model details, see [Gas Model](gas-model.md).
39
39
40
40
## Access to Volatile Data
41
41
42
-
Reading volatile data — `block.timestamp`, `block.number`, oracle storage, or the beneficiary account — caps the transaction's total compute gas to **20,000,000**.
42
+
Reading volatile data — `block.timestamp`, `block.number`, oracle storage, or the beneficiary account — triggers a detention cap of additional **20,000,000 compute gas** from the point of access.
43
43
This ensures transactions with external dependencies yield quickly and don't block parallel execution.
44
44
45
45
For the full list of triggers, best practices for structuring contracts around this cap, and Solidity examples, see [Volatile Data Access](volatile-data.md).
|[MegaAccessControl](#mega-access-control)|[`0x6342000000000000000000000000000000000004`](https://megaeth.blockscout.com/address/0x6342000000000000000000000000000000000004)| Opt out of volatile data access detection |
14
+
|[MegaLimitControl](#mega-limit-control)|[`0x6342000000000000000000000000000000000005`](https://megaeth.blockscout.com/address/0x6342000000000000000000000000000000000005)| Query remaining compute gas budget |
13
15
14
16
## High-Precision Timestamp
15
17
@@ -44,7 +46,7 @@ uint256 timestampSec = timestampUs / 1_000_000; // convert to second timestamp
44
46
**Outcome:**
45
47
46
48
- Returns the current timestamp in microseconds since Unix epoch.
47
-
- Reading the timestamp accesses [volatile data](volatile-data.md) and **caps the transaction's Compute Gas to 20M**.
49
+
- Reading the timestamp accesses [volatile data](volatile-data.md) and **triggers the 20M compute gas detention cap**.
48
50
Avoid reading it in transactions that perform heavy computation.
49
51
50
52
**Properties:**
@@ -121,22 +123,83 @@ If you need a widely-used contract deployed, reach out to the MegaETH team — i
// All subcalls from this point cannot access volatile data.
151
+
// Attempts to read block.timestamp, oracle storage, etc. revert immediately.
152
+
```
153
+
154
+
**Outcome:**
155
+
156
+
-`disableVolatileDataAccess()` — disables volatile data access for the caller's call frame and all descendant call frames.
157
+
-`enableVolatileDataAccess()` — re-enables access, but only if the restriction was set at the caller's own depth. If a parent frame disabled access, the call reverts with `DisabledByParent()`.
158
+
-`isVolatileDataAccessDisabled()` — returns `true` if volatile data access is currently disabled.
159
+
160
+
The restriction automatically ends when the call frame that called `disableVolatileDataAccess` returns.
161
+
No explicit cleanup is needed.
162
+
163
+
{% hint style="success" %}
164
+
Use MegaAccessControl when calling untrusted contracts to prevent them from silently triggering gas detention and tightening your gas budget.
165
+
{% endhint %}
166
+
167
+
**Common use cases:** DeFi protocols calling untrusted hooks, proxy contracts with user-supplied logic, batch execution of arbitrary calls.
168
+
169
+
## Mega Limit Control
170
+
171
+
Provides a runtime query for the effective remaining compute gas, accounting for both [gas detention](volatile-data.md) and per-call-frame resource budgets.
172
+
The standard `GAS` opcode does not reflect these MegaETH-specific constraints.
// Use 'remaining' to decide whether to attempt a costly sub-call
188
+
```
189
+
190
+
**Outcome:**
191
+
192
+
- Returns the effective remaining compute gas for the caller's call frame at the time of the call.
193
+
- The returned value accounts for both the detention cap (if triggered) and the per-call-frame compute gas budget — it is the minimum of the two.
194
+
- The value is a point-in-time snapshot that decreases as execution proceeds.
195
+
196
+
**Common use cases:** Gas-aware batching (loop until budget exhausted), deciding whether to attempt expensive sub-calls, on-chain gas budgeting.
136
197
137
198
## Related Pages
138
199
139
200
-[Volatile Data Access](volatile-data.md) — compute gas cap, best practices for reading volatile data
140
201
-[System Contracts (spec)](https://docs.megaeth.com/spec/system-contracts/overview) — formal specification of the system contract registry
141
202
-[Oracle (spec)](https://docs.megaeth.com/spec/system-contracts/oracle) — underlying oracle contract that powers the High-Precision Timestamp and other services
142
203
-[KeylessDeploy (spec)](https://docs.megaeth.com/spec/system-contracts/keyless-deploy) — keyless deployment sandbox and validation rules
204
+
-[MegaAccessControl (spec)](https://docs.megaeth.com/spec/system-contracts/mega-access-control) — volatile data access restriction mechanism
205
+
-[MegaLimitControl (spec)](https://docs.megaeth.com/spec/system-contracts/mega-limit-control) — remaining compute gas query
Copy file name to clipboardExpand all lines: docs/dev/execution/volatile-data.md
+17-37Lines changed: 17 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,39 +10,24 @@ This includes the current block metadata (block number, timestamp, coinbase), th
10
10
When a transaction reads volatile data, a dependency forms between it and other transactions that modify the same data.
11
11
This harms parallel execution performance — for example, reading `block.number` prevents the sequencer from producing the next block until the reading transaction finishes.
12
12
13
-
To mitigate this, MegaEVM imposes an **absolute cap of 20,000,000 compute gas** on the entire transaction once it accesses any volatile data source.
14
-
This is not 20M of _additional_ gas — it is a hard ceiling on total compute gas for the transaction.
15
-
If the transaction has already consumed more than 20M compute gas before the access, execution halts immediately.
Rex4 changes the cap from absolute to relative: accessing volatile data allows **20M more** compute gas from the point of access, regardless of how much was consumed before.
21
-
22
-
For example, a transaction that uses 15M compute gas before reading `block.timestamp` would still have 20M of compute gas remaining (effective limit = 15M + 20M = 35M).
23
-
Under the current absolute model, the same transaction would have only 5M remaining (absolute cap = 20M).
24
-
25
-
This change removes the penalty for accessing volatile data late in a transaction's execution.
26
-
Under relative detention, reading volatile data **as late as possible** becomes a valid optimization — see [Best Practices](#best-practices).
27
-
For the formal definition, see [Gas Detention](https://docs.megaeth.com/spec/megaevm/gas-detention).
28
-
29
-
</details>
13
+
To mitigate this, MegaEVM imposes a **detention cap of 20,000,000 compute gas** on the transaction once it accesses any volatile data source.
14
+
The cap is relative: accessing volatile data allows **20M more** compute gas from the point of access, regardless of how much was consumed before.
15
+
For example, a transaction that uses 15M compute gas before reading `block.timestamp` still has 20M of compute gas remaining (effective limit = 15M + 20M = 35M).
30
16
31
17
## What Triggers the Cap
32
18
33
19
### Block Environment Opcodes
34
20
35
-
Accessing any of these opcodes caps the transaction's total compute gas to **20,000,000**.
21
+
Accessing any of these opcodes triggers a **20,000,000 compute gas** detention cap from the point of access.
36
22
37
23
| Opcode | Description |
38
24
| ------------- | ------------------------- |
39
25
|`NUMBER`| Current block number |
40
26
|`TIMESTAMP`| Current block timestamp |
41
27
|`COINBASE`| Block beneficiary address |
42
-
|`DIFFICULTY`|Block difficulty |
28
+
|`PREVRANDAO`|Previous block randomness|
43
29
|`GASLIMIT`| Block gas limit |
44
30
|`BASEFEE`| Base fee per gas |
45
-
|`PREVRANDAO`| Previous block randomness |
46
31
|`BLOCKHASH`| Historical block hash |
47
32
|`BLOBBASEFEE`| Blob base fee |
48
33
|`BLOBHASH`| Blob hash lookup |
@@ -69,26 +54,17 @@ Reading oracle data via `SLOAD` from the oracle contract storage triggers the ca
69
54
70
55
### Shared Cap
71
56
72
-
All volatile data sources share the same 20,000,000 compute gas cap.
73
-
Accessing multiple sources (e.g., both `block.timestamp` and oracle storage) does not increase the cap to 40M — the same 20M ceiling applies.
57
+
All volatile data sources share the same 20,000,000 compute gas detention cap.
58
+
The first volatile read triggers the cap; subsequent reads of other volatile sources do not extend it.
74
59
75
60
## Best Practices
76
61
77
-
### Split volatile reads and heavy computation into separate transactions
62
+
### Read volatile data as late as possible
78
63
79
-
If your contract needs both volatile data and heavy computation, split the work across two transactions:
80
-
81
-
1. A lightweight transaction that reads volatile data and stores the result on-chain.
82
-
2. A separate transaction that reads the stored result and performs heavy computation — no cap applies because it never accesses volatile data.
83
-
84
-
<details>
85
-
<summary>Rex4 (unstable): Read volatile data as late as possible</summary>
86
-
87
-
Rex4 changes the cap from absolute to relative: accessing volatile data allows **20M more** compute gas from the point of access, regardless of how much was consumed before.
88
-
This means deferring the volatile data read to the end of the transaction maximizes the computation you can perform.
64
+
Because the detention cap is measured from the point of access, deferring the volatile data read to the end of the transaction maximizes the computation you can perform.
89
65
90
66
```solidity
91
-
// Good under Rex4: heavy computation first, volatile read last
67
+
// Good: heavy computation first, volatile read last
92
68
function processAndCheckTime(uint256[] calldata items) external {
93
69
for (uint i = 0; i < items.length; i++) {
94
70
processItem(items[i]);
@@ -99,7 +75,7 @@ function processAndCheckTime(uint256[] calldata items) external {
99
75
```
100
76
101
77
```solidity
102
-
// Bad under Rex4: reading volatile data first wastes the budget
78
+
// Bad: reading volatile data first wastes the budget
103
79
function processWithTimestamp(uint256[] calldata items) external {
104
80
uint256 currentTime = block.timestamp; // Cap starts immediately
105
81
for (uint i = 0; i < items.length; i++) {
@@ -108,10 +84,14 @@ function processWithTimestamp(uint256[] calldata items) external {
108
84
}
109
85
```
110
86
111
-
Under the current absolute cap, read order makes no difference — the 20M ceiling applies to total compute gas regardless.
112
87
For the formal definition, see [Gas Detention](https://docs.megaeth.com/spec/megaevm/gas-detention).
113
88
114
-
</details>
89
+
### Split volatile reads and heavy computation into separate transactions
90
+
91
+
If your contract needs both volatile data and more than 20M compute gas of heavy computation after the volatile read, split the work across two transactions:
92
+
93
+
1. A lightweight transaction that reads volatile data and stores the result on-chain.
94
+
2. A separate transaction that reads the stored result and performs heavy computation — no cap applies because it never accesses volatile data.
0 commit comments