Skip to content

Commit

Permalink
feat: rename table to tableId (#1484)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Ingersoll <[email protected]>
  • Loading branch information
alvrs and holic authored Sep 15, 2023
1 parent 07dd6f3 commit 6573e38
Show file tree
Hide file tree
Showing 23 changed files with 814 additions and 776 deletions.
38 changes: 38 additions & 0 deletions .changeset/metal-hounds-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
"@latticexyz/block-logs-stream": patch
"@latticexyz/store-sync": patch
"@latticexyz/store": patch
---

Renamed all occurrences of `table` where it is used as "table ID" to `tableId`.
This is only a breaking change for consumers who manually decode `Store` events, but not for consumers who use the MUD libraries.

```diff
event StoreSetRecord(
- bytes32 table,
+ bytes32 tableId,
bytes32[] key,
bytes data
);

event StoreSetField(
- bytes32 table,
+ bytes32 tableId,
bytes32[] key,
uint8 fieldIndex,
bytes data
);

event StoreDeleteRecord(
- bytes32 table,
+ bytes32 tableId,
bytes32[] key
);

event StoreEphemeralRecord(
- bytes32 table,
+ bytes32 tableId,
bytes32[] key,
bytes data
);
```
26 changes: 13 additions & 13 deletions docs/pages/store/advanced-features.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,28 @@ It is possible to register hooks on tables, allowing additional logic to be exec
This is an example of a Mirror hook which mirrors a table into another one:

```solidity
uint256 constant indexerTableId = uint256(keccak256("indexer.table"));
bytes32 constant indexerTableId = bytes32("indexer.table");
contract MirrorSubscriber is IStoreHook {
uint256 _table;
bytes32 _tableId;
constructor(uint256 table, Schema keySchema, Schema valueSchema) {
constructor(bytes32 tableId, Schema keySchema, Schema valueSchema) {
IStore(msg.sender).registerSchema(indexerTableId, valueSchema, keySchema);
_table = table;
_tableId = tableId;
}
function onSetRecord(uint256 table, bytes32[] memory key, bytes memory data) public {
if (_table != table) revert("invalid table");
function onSetRecord(bytes32 tableId, bytes32[] memory key, bytes memory data) public {
if (_tableId != tableId) revert("invalid tableId");
StoreSwitch.setRecord(indexerTableId, key, data);
}
function onSetField(uint256 table, bytes32[] memory key, uint8 schemaIndex, bytes memory data) public {
if (_table != table) revert("invalid table");
function onSetField(bytes32 tableId, bytes32[] memory key, uint8 schemaIndex, bytes memory data) public {
if (_tableId != tableId) revert("invalid tableId");
StoreSwitch.setField(indexerTableId, key, schemaIndex, data);
}
function onDeleteRecord(uint256 table, bytes32[] memory key) public {
if (_table != table) revert("invalid table");
function onDeleteRecord(bytes32 tableId, bytes32[] memory key) public {
if (_tableId != tableId) revert("invalid tableId");
StoreSwitch.deleteRecord(indexerTableId, key);
}
}
Expand All @@ -109,11 +109,11 @@ contract MirrorSubscriber is IStoreHook {
Registering the hook can be done using the low-level Store API:

```solidity
uint256 table = keccak256("table");
bytes32 tableId = bytes32("table");
Schema valueSchema = SchemaLib.encode(SchemaType.UINT256, SchemaType.UINT256);
Schema keySchema = SchemaLib.encode(SchemaType.UINT256);
MirrorSubscriber subscriber = new MirrorSubscriber(table, keySchema, valueSchema);
StoreCore.registerStoreHook(table, subscriber);
MirrorSubscriber subscriber = new MirrorSubscriber(tableId, keySchema, valueSchema);
StoreCore.registerStoreHook(tableId, subscriber);
```

### Accessing Store from a `CALL` or `DELEGATECALL` transparently
Expand Down
8 changes: 4 additions & 4 deletions packages/block-logs-stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ latestBlockNumber$
publicClient,
address,
events: parseAbi([
"event StoreDeleteRecord(bytes32 table, bytes32[] key)",
"event StoreSetField(bytes32 table, bytes32[] key, uint8 schemaIndex, bytes data)",
"event StoreSetRecord(bytes32 table, bytes32[] key, bytes data)",
"event StoreEphemeralRecord(bytes32 table, bytes32[] key, bytes data)",
"event StoreDeleteRecord(bytes32 tableId, bytes32[] key)",
"event StoreSetField(bytes32 tableId, bytes32[] key, uint8 schemaIndex, bytes data)",
"event StoreSetRecord(bytes32 tableId, bytes32[] key, bytes data)",
"event StoreEphemeralRecord(bytes32 tableId, bytes32[] key, bytes data)",
]),
}),
mergeMap(({ logs }) => from(groupLogsByBlockNumber(logs)))
Expand Down
8 changes: 4 additions & 4 deletions packages/store-sync/src/blockLogsToStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("blockLogsToStorage", () => {
logIndex: 53,
removed: false,
args: {
table: "0x6d756473746f726500000000000000005461626c657300000000000000000000",
tableId: "0x6d756473746f726500000000000000005461626c657300000000000000000000",
key: ["0x00000000000000000000000000000000496e76656e746f727900000000000000"],
data: "0x0004010004000000000000000000000000000000000000000000000000000000001c030061030300000000000000000000000000000000000000000000000000000401000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000001600000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000056f776e657200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000046974656d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b6974656d56617269616e740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006616d6f756e740000000000000000000000000000000000000000000000000000",
},
Expand All @@ -76,7 +76,7 @@ describe("blockLogsToStorage", () => {
logIndex: 88,
removed: false,
args: {
table: "0x00000000000000000000000000000000496e76656e746f727900000000000000",
tableId: "0x00000000000000000000000000000000496e76656e746f727900000000000000",
key: [
"0x000000000000000000000000796eb990a3f9c431c69149c7a168b91596d87f60",
"0x0000000000000000000000000000000000000000000000000000000000000001",
Expand Down Expand Up @@ -116,7 +116,7 @@ describe("blockLogsToStorage", () => {
"0x0000000000000000000000000000000000000000000000000000000000000001",
],
"schemaIndex": 0,
"table": "0x00000000000000000000000000000000496e76656e746f727900000000000000",
"tableId": "0x00000000000000000000000000000000496e76656e746f727900000000000000",
},
"blockHash": "0x03e962e7402b2ab295b92feac342a132111dd14b0d1fd4d4a0456fdc77981577",
"blockNumber": 5448n,
Expand Down Expand Up @@ -170,7 +170,7 @@ describe("blockLogsToStorage", () => {
"0x0000000000000000000000000000000000000000000000000000000000000001",
],
"schemaIndex": 0,
"table": "0x00000000000000000000000000000000496e76656e746f727900000000000000",
"tableId": "0x00000000000000000000000000000000496e76656e746f727900000000000000",
},
"blockHash": "0x03e962e7402b2ab295b92feac342a132111dd14b0d1fd4d4a0456fdc77981577",
"blockNumber": 5448n,
Expand Down
10 changes: 5 additions & 5 deletions packages/store-sync/src/blockLogsToStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function blockLogsToStorage<TConfig extends StoreConfig = StoreConfig>({
.map((log) => {
try {
if (log.eventName !== "StoreSetRecord") return;
if (log.args.table !== schemasTableId) return;
if (log.args.tableId !== schemasTableId) return;

// TODO: refactor encode/decode to use Record<string, SchemaAbiType> schemas
// TODO: refactor to decode key with protocol-parser utils
Expand Down Expand Up @@ -87,8 +87,8 @@ export function blockLogsToStorage<TConfig extends StoreConfig = StoreConfig>({
block.logs.map((log) =>
JSON.stringify({
address: getAddress(log.address),
tableId: log.args.table,
...hexToTableId(log.args.table),
tableId: log.args.tableId,
...hexToTableId(log.args.tableId),
})
)
)
Expand All @@ -106,9 +106,9 @@ export function blockLogsToStorage<TConfig extends StoreConfig = StoreConfig>({
const operations = block.logs
.map((log): StorageOperation<TConfig> | undefined => {
try {
const table = tables[`${getAddress(log.address)}:${log.args.table}`];
const table = tables[`${getAddress(log.address)}:${log.args.tableId}`];
if (!table) {
debug("no table found for event, skipping", hexToTableId(log.args.table), log);
debug("no table found for event, skipping", hexToTableId(log.args.tableId), log);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/postgres/postgresStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("postgresStorage", async () => {
logIndex: 54,
removed: false,
args: {
table: "0x000000000000000000000000000000005265736f757263655479706500000000",
tableId: "0x000000000000000000000000000000005265736f757263655479706500000000",
key: ["0x00000000000000000000000000000000496e76656e746f727900000000000000"],
schemaIndex: 0,
data: "0x02",
Expand All @@ -64,7 +64,7 @@ describe("postgresStorage", async () => {
logIndex: 55,
removed: false,
args: {
table: "0x6d756473746f726500000000000000005461626c657300000000000000000000",
tableId: "0x6d756473746f726500000000000000005461626c657300000000000000000000",
key: ["0x00000000000000000000000000000000496e76656e746f727900000000000000"],
data: "0x0004010004000000000000000000000000000000000000000000000000000000001c030061030300000000000000000000000000000000000000000000000000000401000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000001600000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000056f776e657200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000046974656d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b6974656d56617269616e740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006616d6f756e740000000000000000000000000000000000000000000000000000",
},
Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/sqlite/sqliteStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("sqliteStorage", async () => {
logIndex: 54,
removed: false,
args: {
table: "0x000000000000000000000000000000005265736f757263655479706500000000",
tableId: "0x000000000000000000000000000000005265736f757263655479706500000000",
key: ["0x00000000000000000000000000000000496e76656e746f727900000000000000"],
schemaIndex: 0,
data: "0x02",
Expand All @@ -70,7 +70,7 @@ describe("sqliteStorage", async () => {
logIndex: 55,
removed: false,
args: {
table: "0x6d756473746f726500000000000000005461626c657300000000000000000000",
tableId: "0x6d756473746f726500000000000000005461626c657300000000000000000000",
key: ["0x00000000000000000000000000000000496e76656e746f727900000000000000"],
data: "0x0004010004000000000000000000000000000000000000000000000000000000001c030061030300000000000000000000000000000000000000000000000000000401000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a000000001600000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000056f776e657200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000046974656d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b6974656d56617269616e740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000006616d6f756e740000000000000000000000000000000000000000000000000000",
},
Expand Down
42 changes: 21 additions & 21 deletions packages/store/src/IStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@ import { Schema } from "./Schema.sol";
import { IStoreHook } from "./IStoreHook.sol";

interface IStoreRead {
function getFieldLayout(bytes32 table) external view returns (FieldLayout fieldLayout);
function getFieldLayout(bytes32 tableId) external view returns (FieldLayout fieldLayout);

function getValueSchema(bytes32 table) external view returns (Schema valueSchema);
function getValueSchema(bytes32 tableId) external view returns (Schema valueSchema);

function getKeySchema(bytes32 table) external view returns (Schema keySchema);
function getKeySchema(bytes32 tableId) external view returns (Schema keySchema);

// Get full record (including full array)
function getRecord(
bytes32 table,
bytes32 tableId,
bytes32[] calldata key,
FieldLayout fieldLayout
) external view returns (bytes memory data);

// Get partial data at schema index
function getField(
bytes32 table,
bytes32 tableId,
bytes32[] calldata key,
uint8 schemaIndex,
FieldLayout fieldLayout
) external view returns (bytes memory data);

// Get field length at schema index
function getFieldLength(
bytes32 table,
bytes32 tableId,
bytes32[] memory key,
uint8 schemaIndex,
FieldLayout fieldLayout
) external view returns (uint256);

// Get start:end slice of the field at schema index
function getFieldSlice(
bytes32 table,
bytes32 tableId,
bytes32[] memory key,
uint8 schemaIndex,
FieldLayout fieldLayout,
Expand All @@ -48,16 +48,16 @@ interface IStoreRead {
}

interface IStoreWrite {
event StoreSetRecord(bytes32 table, bytes32[] key, bytes data);
event StoreSetField(bytes32 table, bytes32[] key, uint8 schemaIndex, bytes data);
event StoreDeleteRecord(bytes32 table, bytes32[] key);
event StoreSetRecord(bytes32 tableId, bytes32[] key, bytes data);
event StoreSetField(bytes32 tableId, bytes32[] key, uint8 schemaIndex, bytes data);
event StoreDeleteRecord(bytes32 tableId, bytes32[] key);

// Set full record (including full dynamic data)
function setRecord(bytes32 table, bytes32[] calldata key, bytes calldata data, FieldLayout fieldLayout) external;
function setRecord(bytes32 tableId, bytes32[] calldata key, bytes calldata data, FieldLayout fieldLayout) external;

// Set partial data at schema index
function setField(
bytes32 table,
bytes32 tableId,
bytes32[] calldata key,
uint8 schemaIndex,
bytes calldata data,
Expand All @@ -66,7 +66,7 @@ interface IStoreWrite {

// Push encoded items to the dynamic field at schema index
function pushToField(
bytes32 table,
bytes32 tableId,
bytes32[] calldata key,
uint8 schemaIndex,
bytes calldata dataToPush,
Expand All @@ -75,7 +75,7 @@ interface IStoreWrite {

// Pop byte length from the dynamic field at schema index
function popFromField(
bytes32 table,
bytes32 tableId,
bytes32[] calldata key,
uint8 schemaIndex,
uint256 byteLengthToPop,
Expand All @@ -84,7 +84,7 @@ interface IStoreWrite {

// Change encoded items within the dynamic field at schema index
function updateInField(
bytes32 table,
bytes32 tableId,
bytes32[] calldata key,
uint8 schemaIndex,
uint256 startByteIndex,
Expand All @@ -93,15 +93,15 @@ interface IStoreWrite {
) external;

// Set full record (including full dynamic data)
function deleteRecord(bytes32 table, bytes32[] memory key, FieldLayout fieldLayout) external;
function deleteRecord(bytes32 tableId, bytes32[] memory key, FieldLayout fieldLayout) external;
}

interface IStoreEphemeral {
event StoreEphemeralRecord(bytes32 table, bytes32[] key, bytes data);
event StoreEphemeralRecord(bytes32 tableId, bytes32[] key, bytes data);

// Emit the ephemeral event without modifying storage
function emitEphemeralRecord(
bytes32 table,
bytes32 tableId,
bytes32[] calldata key,
bytes calldata data,
FieldLayout fieldLayout
Expand All @@ -124,7 +124,7 @@ interface IStoreData is IStoreRead, IStoreWrite {
*/
interface IStoreRegistration {
function registerTable(
bytes32 table,
bytes32 tableId,
FieldLayout fieldLayout,
Schema keySchema,
Schema valueSchema,
Expand All @@ -133,10 +133,10 @@ interface IStoreRegistration {
) external;

// Register hook to be called when a record or field is set or deleted
function registerStoreHook(bytes32 table, IStoreHook hookAddress, uint8 enabledHooksBitmap) external;
function registerStoreHook(bytes32 tableId, IStoreHook hookAddress, uint8 enabledHooksBitmap) external;

// Unregister a hook for the given tableId
function unregisterStoreHook(bytes32 table, IStoreHook hookAddress) external;
function unregisterStoreHook(bytes32 tableId, IStoreHook hookAddress) external;
}

interface IStore is IStoreData, IStoreRegistration, IStoreEphemeral, IStoreErrors {}
17 changes: 11 additions & 6 deletions packages/store/src/IStoreHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,32 @@ bytes4 constant STORE_HOOK_INTERFACE_ID = IStoreHook.onBeforeSetRecord.selector
ERC165_INTERFACE_ID;

interface IStoreHook is IERC165 {
function onBeforeSetRecord(bytes32 table, bytes32[] memory key, bytes memory data, FieldLayout fieldLayout) external;
function onBeforeSetRecord(
bytes32 tableId,
bytes32[] memory key,
bytes memory data,
FieldLayout fieldLayout
) external;

function onAfterSetRecord(bytes32 table, bytes32[] memory key, bytes memory data, FieldLayout fieldLayout) external;
function onAfterSetRecord(bytes32 tableId, bytes32[] memory key, bytes memory data, FieldLayout fieldLayout) external;

function onBeforeSetField(
bytes32 table,
bytes32 tableId,
bytes32[] memory key,
uint8 schemaIndex,
bytes memory data,
FieldLayout fieldLayout
) external;

function onAfterSetField(
bytes32 table,
bytes32 tableId,
bytes32[] memory key,
uint8 schemaIndex,
bytes memory data,
FieldLayout fieldLayout
) external;

function onBeforeDeleteRecord(bytes32 table, bytes32[] memory key, FieldLayout fieldLayout) external;
function onBeforeDeleteRecord(bytes32 tableId, bytes32[] memory key, FieldLayout fieldLayout) external;

function onAfterDeleteRecord(bytes32 table, bytes32[] memory key, FieldLayout fieldLayout) external;
function onAfterDeleteRecord(bytes32 tableId, bytes32[] memory key, FieldLayout fieldLayout) external;
}
Loading

0 comments on commit 6573e38

Please sign in to comment.