Skip to content

Commit

Permalink
feat(store,world): replace ephemeral tables with offchain tables (#…
Browse files Browse the repository at this point in the history
…1558)

Co-authored-by: Kevin Ingersoll <kingersoll@gmail.com>
Co-authored-by: dk1a <dk1a@protonmail.com>
  • Loading branch information
3 people committed Sep 21, 2023
1 parent 5e723b9 commit bfcb293
Show file tree
Hide file tree
Showing 82 changed files with 1,474 additions and 1,489 deletions.
20 changes: 20 additions & 0 deletions .changeset/small-chicken-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"@latticexyz/block-logs-stream": patch
"@latticexyz/cli": patch
"@latticexyz/common": major
"@latticexyz/dev-tools": patch
"@latticexyz/store-sync": patch
"@latticexyz/store": major
"create-mud": minor
---

What used to be known as `ephemeral` table is now called `offchain` table.
The previous `ephemeral` tables only supported an `emitEphemeral` method, which emitted a `StoreSetEphemeralRecord` event.

Now `offchain` tables support all regular table methods, except partial operations on dynamic fields (`push`, `pop`, `update`).
Unlike regular tables they don't store data on-chain but emit the same events as regular tables (`StoreSetRecord`, `StoreSpliceStaticData`, `StoreDeleteRecord`), so their data can be indexed by offchain indexers/clients.

```diff
- EphemeralTable.emitEphemeral(value);
+ OffchainTable.set(value);
```
20 changes: 3 additions & 17 deletions docs/pages/store/advanced-features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ uint256 value = CounterSingleton.get();

Additional documentation on Table library generation can be found in the [`tablegen`](/store/config#store-config--tablegen-tool) section.

### Ephemeral tables
### Offchain tables

Tables with the `ephemeral` property do not write to on-chain storage.
Tables with the `offchainOnly` property do not write to on-chain storage.
Instead, they simply emit events when records are set.
They are useful for sending data to connected clients without the gas costs associated with a storage write.

Expand All @@ -52,26 +52,12 @@ export default mudConfig({
amount: "uint32",
receiver: "bytes32",
},
ephemeral: true,
offchainOnly: true,
},
},
});
```

This will slightly change the generated code for the table. The `emitEphemeral` function is the main entrypoint:

```solidity
import { TradeExecuted, TradeExecutedData } from "./codegen/Tables.sol";
// Emitting an ephemeral record
TradeExecuted.emitEphemeral("0x1234", TradeExecutedData({
amount: 10,
receiver: "0x5678",
}));
```

`get`, `set`, and `delete` functions are not generated for ephemeral tables.

### Storage hooks

It is possible to register hooks on tables, allowing additional logic to be executed when records or fields are updated. Use cases include: creating indices on another table (like the `ownerOf` mapping of the ERC-721 spec as an example), emitting events on a different contract, or simply additional access-control and checks by reverting in the hook.
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/world/internals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Except `NamespaceOwner` is initialized in World's constructor, because it's need
Internal systems are in `core` module's [implementations](https://github.com/latticexyz/mud/tree/main/packages/world/src/modules/core/implementations) folder, because they're installed by `CoreModule`.

- `AccessManagementSystem` - grants/revokes access to/from resources
- `EphemeralRecordSystem` - has `emitEphemeralRecord` for ephemeral tables
- `BalanceTransferSystem` - handles balance transfers between namespaces and from namespaces to addresses
- `ModuleInstallationSystem` - installation of (non-root) modules in the World
- `StoreRegistrationSystem` - _its methods should not be used with the World framework_. Surfaces the APIs necessary to register Tables on-chain, but lacks namespaces used by World for better permission checks
- `WorldRegistrationSystem` - surfaces the APIs necessary to register Systems, Tables, and Namespaces on-chain
56 changes: 28 additions & 28 deletions e2e/packages/contracts/src/codegen/tables/Multi.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions e2e/packages/contracts/src/codegen/tables/Number.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 22 additions & 22 deletions e2e/packages/contracts/src/codegen/tables/NumberList.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions e2e/packages/contracts/src/codegen/tables/Vector.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/minimal/packages/contracts/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default mudConfig({
valueSchema: {
value: "string",
},
ephemeral: true,
offchainOnly: true,
},
Inventory: {
keySchema: {
Expand Down
Loading

0 comments on commit bfcb293

Please sign in to comment.