Skip to content

Commit a9e7606

Browse files
authored
docs: stabilize Rex4 (#27)
* docs: stabilize Rex4 — promote unstable previews to current behavior * address review: replace DIFFICULTY with PREVRANDAO, explain view annotation on system contracts
1 parent 398d483 commit a9e7606

5 files changed

Lines changed: 94 additions & 51 deletions

File tree

docs/AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Use these exact forms consistently. Do not alternate between variants.
8686
- **GitBook Markdown**: Use GitBook-flavored Markdown with custom blocks. See `/skill.md` for full syntax reference.
8787
- **One sentence, one line**: Each sentence goes on its own line for better diffs.
8888
- **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).
9090
- **Serial commas**: Use serial (Oxford) commas in lists of three or more: "compute gas, storage gas, and detention gas."
9191

9292
## Cross-Linking

docs/dev/AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ For the complete storage gas schedule, see the [formal specification](https://do
8181
- Use `{% hint style="success" %}` for practical tips and best practices.
8282
- Use tables for reference data (gas costs, resource limits, contract addresses).
8383
- 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.
8585

8686
## RPC Method Page Rules
8787

docs/dev/execution/overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ It is fully compatible with Ethereum smart contracts while introducing optimizat
1010
## Overview
1111

1212
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/).
1414
This means:
1515

1616
- All standard Solidity contracts work on MegaETH.
@@ -39,7 +39,7 @@ For the full gas model details, see [Gas Model](gas-model.md).
3939

4040
## Access to Volatile Data
4141

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.
4343
This ensures transactions with external dependencies yield quickly and don't block parallel execution.
4444

4545
For the full list of triggers, best practices for structuring contracts around this cap, and Solidity examples, see [Volatile Data Access](volatile-data.md).

docs/dev/execution/system-contracts.md

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ MegaETH provides system contracts that give transactions access to functionality
1010
| ----------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
1111
| [High-Precision Timestamp](#high-precision-timestamp) | [`0x6342000000000000000000000000000000000002`](https://megaeth.blockscout.com/address/0x6342000000000000000000000000000000000002) | Microsecond-resolution timestamps |
1212
| [KeylessDeploy](#keyless-deployment) | [`0x6342000000000000000000000000000000000003`](https://megaeth.blockscout.com/address/0x6342000000000000000000000000000000000003) | Deterministic cross-chain deployment (Nick's Method) |
13+
| [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 |
1315

1416
## High-Precision Timestamp
1517

@@ -44,7 +46,7 @@ uint256 timestampSec = timestampUs / 1_000_000; // convert to second timestamp
4446
**Outcome:**
4547

4648
- 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**.
4850
Avoid reading it in transactions that perform heavy computation.
4951

5052
**Properties:**
@@ -121,22 +123,83 @@ If you need a widely-used contract deployed, reach out to the MegaETH team — i
121123
| [CREATE2 Factory](https://github.com/Arachnid/deterministic-deployment-proxy) | [`0x4e59b44847b379578588920ca78fbf26c0b4956c`](https://megaeth.blockscout.com/address/0x4e59b44847b379578588920ca78fbf26c0b4956c) |
122124
| [EIP-1820 Registry](https://eips.ethereum.org/EIPS/eip-1820) | [`0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24`](https://megaeth.blockscout.com/address/0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24) |
123125

124-
<details>
125-
<summary>Rex4 preview — upcoming system contracts</summary>
126+
## Mega Access Control
126127

127-
The following system contracts are planned for the Rex4 hardfork.
128-
Addresses and interfaces are subject to change before release.
128+
Provides a proactive mechanism to prevent untrusted subcalls from accessing [volatile data](volatile-data.md).
129+
A contract can disable volatile data access for its entire call subtree before any untrusted code runs.
130+
Attempts to access volatile data while disabled revert immediately, preventing both the access and the [detention](volatile-data.md) side effect.
129131

130-
| Contract | Address | Purpose |
131-
| ----------------- | -------------------------------------------- | --------------------------------------------------------------------------- |
132-
| MegaAccessControl | `0x6342000000000000000000000000000000000004` | Opt out of volatile data access detection via `disableVolatileDataAccess()` |
133-
| MegaLimitControl | `0x6342000000000000000000000000000000000005` | Query remaining compute gas budget via `remainingComputeGas()` |
132+
**Address:** `0x6342000000000000000000000000000000000004`
134133

135-
</details>
134+
**Interface:**
135+
136+
Functions are declared `view` so they can be called from `view` contexts.
137+
The node intercepts these calls and tracks the restriction outside EVM storage, so no state modification occurs.
138+
139+
```solidity
140+
interface IMegaAccessControl {
141+
function disableVolatileDataAccess() external view;
142+
function enableVolatileDataAccess() external view;
143+
function isVolatileDataAccessDisabled() external view returns (bool disabled);
144+
}
145+
146+
IMegaAccessControl accessControl = IMegaAccessControl(
147+
0x6342000000000000000000000000000000000004
148+
);
149+
accessControl.disableVolatileDataAccess();
150+
// 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.
173+
174+
**Address:** `0x6342000000000000000000000000000000000005`
175+
176+
**Interface:**
177+
178+
```solidity
179+
interface IMegaLimitControl {
180+
function remainingComputeGas() external view returns (uint64 remaining);
181+
}
182+
183+
IMegaLimitControl limitControl = IMegaLimitControl(
184+
0x6342000000000000000000000000000000000005
185+
);
186+
uint64 remaining = limitControl.remainingComputeGas();
187+
// 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.
136197

137198
## Related Pages
138199

139200
- [Volatile Data Access](volatile-data.md) — compute gas cap, best practices for reading volatile data
140201
- [System Contracts (spec)](https://docs.megaeth.com/spec/system-contracts/overview) — formal specification of the system contract registry
141202
- [Oracle (spec)](https://docs.megaeth.com/spec/system-contracts/oracle) — underlying oracle contract that powers the High-Precision Timestamp and other services
142203
- [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

docs/dev/execution/volatile-data.md

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,24 @@ This includes the current block metadata (block number, timestamp, coinbase), th
1010
When a transaction reads volatile data, a dependency forms between it and other transactions that modify the same data.
1111
This harms parallel execution performance — for example, reading `block.number` prevents the sequencer from producing the next block until the reading transaction finishes.
1212

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.
16-
17-
<details>
18-
<summary>Rex4 (unstable): Relative detention</summary>
19-
20-
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).
3016

3117
## What Triggers the Cap
3218

3319
### Block Environment Opcodes
3420

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.
3622

3723
| Opcode | Description |
3824
| ------------- | ------------------------- |
3925
| `NUMBER` | Current block number |
4026
| `TIMESTAMP` | Current block timestamp |
4127
| `COINBASE` | Block beneficiary address |
42-
| `DIFFICULTY` | Block difficulty |
28+
| `PREVRANDAO` | Previous block randomness |
4329
| `GASLIMIT` | Block gas limit |
4430
| `BASEFEE` | Base fee per gas |
45-
| `PREVRANDAO` | Previous block randomness |
4631
| `BLOCKHASH` | Historical block hash |
4732
| `BLOBBASEFEE` | Blob base fee |
4833
| `BLOBHASH` | Blob hash lookup |
@@ -69,26 +54,17 @@ Reading oracle data via `SLOAD` from the oracle contract storage triggers the ca
6954

7055
### Shared Cap
7156

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.
7459

7560
## Best Practices
7661

77-
### Split volatile reads and heavy computation into separate transactions
62+
### Read volatile data as late as possible
7863

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.
8965

9066
```solidity
91-
// Good under Rex4: heavy computation first, volatile read last
67+
// Good: heavy computation first, volatile read last
9268
function processAndCheckTime(uint256[] calldata items) external {
9369
for (uint i = 0; i < items.length; i++) {
9470
processItem(items[i]);
@@ -99,7 +75,7 @@ function processAndCheckTime(uint256[] calldata items) external {
9975
```
10076

10177
```solidity
102-
// Bad under Rex4: reading volatile data first wastes the budget
78+
// Bad: reading volatile data first wastes the budget
10379
function processWithTimestamp(uint256[] calldata items) external {
10480
uint256 currentTime = block.timestamp; // Cap starts immediately
10581
for (uint i = 0; i < items.length; i++) {
@@ -108,10 +84,14 @@ function processWithTimestamp(uint256[] calldata items) external {
10884
}
10985
```
11086

111-
Under the current absolute cap, read order makes no difference — the 20M ceiling applies to total compute gas regardless.
11287
For the formal definition, see [Gas Detention](https://docs.megaeth.com/spec/megaevm/gas-detention).
11388

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.
11595

11696
## Related Pages
11797

0 commit comments

Comments
 (0)