Skip to content

Commit

Permalink
feat(world): align methods with store to require schema to be passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Aug 15, 2023
1 parent abc3c1a commit 097d836
Show file tree
Hide file tree
Showing 27 changed files with 449 additions and 368 deletions.
63 changes: 39 additions & 24 deletions packages/world/src/World.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { StoreRead } from "@latticexyz/store/src/StoreRead.sol";
import { IStoreData } from "@latticexyz/store/src/IStore.sol";
import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
import { Bytes } from "@latticexyz/store/src/Bytes.sol";
import { Schema } from "@latticexyz/store/src/Schema.sol";

import { System } from "./System.sol";
import { ResourceSelector } from "./ResourceSelector.sol";
Expand Down Expand Up @@ -68,12 +69,18 @@ contract World is StoreRead, IStoreData, IWorldKernel {
* Write a record in the table at the given namespace and name.
* Requires the caller to have access to the namespace or name.
*/
function setRecord(bytes16 namespace, bytes16 name, bytes32[] calldata key, bytes calldata data) public virtual {
function setRecord(
bytes16 namespace,
bytes16 name,
bytes32[] calldata key,
bytes calldata data,
Schema valueSchema
) public virtual {
// Require access to the namespace or name
bytes32 resourceSelector = AccessControl.requireAccess(namespace, name, msg.sender);

// Set the record
StoreCore.setRecord(resourceSelector, key, data);
StoreCore.setRecord(resourceSelector, key, data, valueSchema);
}

/**
Expand All @@ -85,13 +92,14 @@ contract World is StoreRead, IStoreData, IWorldKernel {
bytes16 name,
bytes32[] calldata key,
uint8 schemaIndex,
bytes calldata data
bytes calldata data,
Schema valueSchema
) public virtual {
// Require access to namespace or name
bytes32 resourceSelector = AccessControl.requireAccess(namespace, name, msg.sender);

// Set the field
StoreCore.setField(resourceSelector, key, schemaIndex, data);
StoreCore.setField(resourceSelector, key, schemaIndex, data, valueSchema);
}

/**
Expand All @@ -103,13 +111,14 @@ contract World is StoreRead, IStoreData, IWorldKernel {
bytes16 name,
bytes32[] calldata key,
uint8 schemaIndex,
bytes calldata dataToPush
bytes calldata dataToPush,
Schema valueSchema
) public virtual {
// Require access to namespace or name
bytes32 resourceSelector = AccessControl.requireAccess(namespace, name, msg.sender);

// Push to the field
StoreCore.pushToField(resourceSelector, key, schemaIndex, dataToPush);
StoreCore.pushToField(resourceSelector, key, schemaIndex, dataToPush, valueSchema);
}

/**
Expand All @@ -121,13 +130,14 @@ contract World is StoreRead, IStoreData, IWorldKernel {
bytes16 name,
bytes32[] calldata key,
uint8 schemaIndex,
uint256 byteLengthToPop
uint256 byteLengthToPop,
Schema valueSchema
) public virtual {
// Require access to namespace or name
bytes32 resourceSelector = AccessControl.requireAccess(namespace, name, msg.sender);

// Push to the field
StoreCore.popFromField(resourceSelector, key, schemaIndex, byteLengthToPop);
StoreCore.popFromField(resourceSelector, key, schemaIndex, byteLengthToPop, valueSchema);
}

/**
Expand All @@ -140,25 +150,26 @@ contract World is StoreRead, IStoreData, IWorldKernel {
bytes32[] calldata key,
uint8 schemaIndex,
uint256 startByteIndex,
bytes calldata dataToSet
bytes calldata dataToSet,
Schema valueSchema
) public virtual {
// Require access to namespace or name
bytes32 resourceSelector = AccessControl.requireAccess(namespace, name, msg.sender);

// Update data in the field
StoreCore.updateInField(resourceSelector, key, schemaIndex, startByteIndex, dataToSet);
StoreCore.updateInField(resourceSelector, key, schemaIndex, startByteIndex, dataToSet, valueSchema);
}

/**
* Delete a record in the table at the given namespace and name.
* Requires the caller to have access to the namespace or name.
*/
function deleteRecord(bytes16 namespace, bytes16 name, bytes32[] calldata key) public virtual {
function deleteRecord(bytes16 namespace, bytes16 name, bytes32[] calldata key, Schema valueSchema) public virtual {
// Require access to namespace or name
bytes32 resourceSelector = AccessControl.requireAccess(namespace, name, msg.sender);

// Delete the record
StoreCore.deleteRecord(resourceSelector, key);
StoreCore.deleteRecord(resourceSelector, key, valueSchema);
}

/************************************************************************
Expand All @@ -172,8 +183,8 @@ contract World is StoreRead, IStoreData, IWorldKernel {
* This overload exists to conform with the `IStore` interface.
* Access is checked based on the namespace or name (encoded in the tableId).
*/
function setRecord(bytes32 tableId, bytes32[] calldata key, bytes calldata data) public virtual {
setRecord(tableId.getNamespace(), tableId.getName(), key, data);
function setRecord(bytes32 tableId, bytes32[] calldata key, bytes calldata data, Schema valueSchema) public virtual {
setRecord(tableId.getNamespace(), tableId.getName(), key, data, valueSchema);
}

/**
Expand All @@ -185,9 +196,10 @@ contract World is StoreRead, IStoreData, IWorldKernel {
bytes32 tableId,
bytes32[] calldata key,
uint8 schemaIndex,
bytes calldata data
bytes calldata data,
Schema valueSchema
) public virtual override {
setField(tableId.getNamespace(), tableId.getName(), key, schemaIndex, data);
setField(tableId.getNamespace(), tableId.getName(), key, schemaIndex, data, valueSchema);
}

/**
Expand All @@ -199,9 +211,10 @@ contract World is StoreRead, IStoreData, IWorldKernel {
bytes32 tableId,
bytes32[] calldata key,
uint8 schemaIndex,
bytes calldata dataToPush
bytes calldata dataToPush,
Schema valueSchema
) public override {
pushToField(tableId.getNamespace(), tableId.getName(), key, schemaIndex, dataToPush);
pushToField(tableId.getNamespace(), tableId.getName(), key, schemaIndex, dataToPush, valueSchema);
}

/**
Expand All @@ -213,9 +226,10 @@ contract World is StoreRead, IStoreData, IWorldKernel {
bytes32 tableId,
bytes32[] calldata key,
uint8 schemaIndex,
uint256 byteLengthToPop
uint256 byteLengthToPop,
Schema valueSchema
) public override {
popFromField(tableId.getNamespace(), tableId.getName(), key, schemaIndex, byteLengthToPop);
popFromField(tableId.getNamespace(), tableId.getName(), key, schemaIndex, byteLengthToPop, valueSchema);
}

/**
Expand All @@ -228,18 +242,19 @@ contract World is StoreRead, IStoreData, IWorldKernel {
bytes32[] calldata key,
uint8 schemaIndex,
uint256 startByteIndex,
bytes calldata dataToSet
bytes calldata dataToSet,
Schema valueSchema
) public virtual {
updateInField(tableId.getNamespace(), tableId.getName(), key, schemaIndex, startByteIndex, dataToSet);
updateInField(tableId.getNamespace(), tableId.getName(), key, schemaIndex, startByteIndex, dataToSet, valueSchema);
}

/**
* Delete a record in the table at the given tableId.
* This overload exists to conform with the `IStore` interface.
* Access is checked based on the namespace or name (encoded in the tableId).
*/
function deleteRecord(bytes32 tableId, bytes32[] calldata key) public virtual override {
deleteRecord(tableId.getNamespace(), tableId.getName(), key);
function deleteRecord(bytes32 tableId, bytes32[] calldata key, Schema valueSchema) public virtual override {
deleteRecord(tableId.getNamespace(), tableId.getName(), key, valueSchema);
}

/************************************************************************
Expand Down
10 changes: 9 additions & 1 deletion packages/world/src/interfaces/IWorldEphemeral.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import { Schema } from "@latticexyz/store/src/Schema.sol";

/**
* Necessary in addition to `IStoreEphemeral`
* because 2 functions with the same name need 2 separate interfaces to allow referencing their selector
Expand All @@ -10,5 +12,11 @@ interface IWorldEphemeral {
* Emit the ephemeral event without modifying storage at the given namespace and name.
* Requires the caller to have access to the namespace or name.
*/
function emitEphemeralRecord(bytes16 namespace, bytes16 name, bytes32[] calldata key, bytes calldata data) external;
function emitEphemeralRecord(
bytes16 namespace,
bytes16 name,
bytes32[] calldata key,
bytes calldata data,
Schema valueSchema
) external;
}
23 changes: 17 additions & 6 deletions packages/world/src/interfaces/IWorldKernel.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

import { Schema } from "@latticexyz/store/src/Schema.sol";
import { IWorldErrors } from "./IWorldErrors.sol";
import { IModule } from "./IModule.sol";

Expand All @@ -14,7 +15,13 @@ interface IWorldData {
* Write a record in the table at the given namespace and name.
* Requires the caller to have access to the namespace or name.
*/
function setRecord(bytes16 namespace, bytes16 name, bytes32[] calldata key, bytes calldata data) external;
function setRecord(
bytes16 namespace,
bytes16 name,
bytes32[] calldata key,
bytes calldata data,
Schema valueSchema
) external;

/**
* Write a field in the table at the given namespace and name.
Expand All @@ -25,7 +32,8 @@ interface IWorldData {
bytes16 name,
bytes32[] calldata key,
uint8 schemaIndex,
bytes calldata data
bytes calldata data,
Schema valueSchema
) external;

/**
Expand All @@ -37,7 +45,8 @@ interface IWorldData {
bytes16 name,
bytes32[] calldata key,
uint8 schemaIndex,
bytes calldata dataToPush
bytes calldata dataToPush,
Schema valueSchema
) external;

/**
Expand All @@ -49,7 +58,8 @@ interface IWorldData {
bytes16 name,
bytes32[] calldata key,
uint8 schemaIndex,
uint256 byteLengthToPop
uint256 byteLengthToPop,
Schema valueSchema
) external;

/**
Expand All @@ -62,14 +72,15 @@ interface IWorldData {
bytes32[] calldata key,
uint8 schemaIndex,
uint256 startByteIndex,
bytes calldata dataToSet
bytes calldata dataToSet,
Schema valueSchema
) external;

/**
* Delete a record in the table at the given namespace and name.
* Requires the caller to have access to the namespace or name.
*/
function deleteRecord(bytes16 namespace, bytes16 name, bytes32[] calldata key) external;
function deleteRecord(bytes16 namespace, bytes16 name, bytes32[] calldata key, Schema valueSchema) external;
}

interface IWorldModuleInstallation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity >=0.8.0;

import { IStoreEphemeral } from "@latticexyz/store/src/IStore.sol";
import { Schema } from "@latticexyz/store/src/Schema.sol";
import { IModule } from "../../../interfaces/IModule.sol";
import { IWorldEphemeral } from "../../../interfaces/IWorldEphemeral.sol";
import { System } from "../../../System.sol";
Expand All @@ -23,21 +24,27 @@ contract EphemeralRecordSystem is IStoreEphemeral, IWorldEphemeral, System {
bytes16 namespace,
bytes16 name,
bytes32[] calldata key,
bytes calldata data
bytes calldata data,
Schema valueSchema
) public virtual {
// Require access to the namespace or name
bytes32 resourceSelector = AccessControl.requireAccess(namespace, name, msg.sender);

// Set the record
StoreCore.emitEphemeralRecord(resourceSelector, key, data);
StoreCore.emitEphemeralRecord(resourceSelector, key, data, valueSchema);
}

/**
* Emit the ephemeral event without modifying storage at the given tableId.
* This overload exists to conform with the `IStore` interface.
* Access is checked based on the namespace or name (encoded in the tableId).
*/
function emitEphemeralRecord(bytes32 tableId, bytes32[] calldata key, bytes calldata data) public virtual {
emitEphemeralRecord(tableId.getNamespace(), tableId.getName(), key, data);
function emitEphemeralRecord(
bytes32 tableId,
bytes32[] calldata key,
bytes calldata data,
Schema valueSchema
) public virtual {
emitEphemeralRecord(tableId.getNamespace(), tableId.getName(), key, data, valueSchema);
}
}
Loading

0 comments on commit 097d836

Please sign in to comment.