Skip to content

Commit

Permalink
refactor(world): change tableId type from bytes32 to uint256
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Feb 17, 2023
1 parent 91be40c commit e3b5be7
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 80 deletions.
36 changes: 20 additions & 16 deletions packages/world/src/World.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ contract World is Store {
string calldata baseRoute,
string calldata tableRoute,
Schema schema
) public returns (bytes32 tableRouteId) {
) public returns (uint256 tableRouteId) {
// Register table route
tableRouteId = registerRoute(baseRoute, tableRoute);
tableRouteId = uint256(registerRoute(baseRoute, tableRoute));

// StoreCore handles checking for existence
StoreCore.registerSchema(tableRouteId, schema);
Expand All @@ -90,9 +90,10 @@ contract World is Store {
* This overload exists to conform to the Store interface,
* but it requires the caller to register a route using `registerRoute` first
*/
function registerSchema(bytes32 tableId, Schema schema) public override {
function registerSchema(uint256 tableId, Schema schema) public override {
// Require caller to own the given tableId
if (OwnerTable.get(tableId) != msg.sender) revert RouteAccessDenied(RouteTable.get(tableId), msg.sender);
if (OwnerTable.get(bytes32(tableId)) != msg.sender)
revert RouteAccessDenied(RouteTable.get(bytes32(tableId)), msg.sender);

// Register the schema
StoreCore.registerSchema(tableId, schema);
Expand All @@ -102,16 +103,17 @@ contract World is Store {
* Register a hook for a given table route
*/
function registerTableHook(string calldata tableRoute, IStoreHook hook) public {
registerStoreHook(keccak256(bytes(tableRoute)), hook);
registerStoreHook(uint256(keccak256(bytes(tableRoute))), hook);
}

/**
* Register a hook for a given table route id
* This overload exists to conform with the `IStore` interface.
*/
function registerStoreHook(bytes32 tableId, IStoreHook hook) public override {
function registerStoreHook(uint256 tableId, IStoreHook hook) public override {
// Require caller to own the given tableId
if (OwnerTable.get(tableId) != msg.sender) revert RouteAccessDenied(RouteTable.get(tableId), msg.sender);
if (OwnerTable.get(bytes32(tableId)) != msg.sender)
revert RouteAccessDenied(RouteTable.get(bytes32(tableId)), msg.sender);

// Register the hook
StoreCore.registerStoreHook(tableId, hook);
Expand Down Expand Up @@ -201,7 +203,7 @@ contract World is Store {
if (!_isRoute(subRoute)) revert RouteInvalid(subRoute);

// Construct the table route id by concatenating accessRoute and tableRoute
bytes32 tableRouteId = keccak256(abi.encodePacked(accessRoute, subRoute));
uint256 tableRouteId = uint256(keccak256(abi.encodePacked(accessRoute, subRoute)));

// Set the record
StoreCore.setRecord(tableRouteId, key, data);
Expand All @@ -212,12 +214,12 @@ contract World is Store {
* This overload exists to conform with the `IStore` interface.
*/
function setRecord(
bytes32 tableRouteId,
uint256 tableRouteId,
bytes32[] calldata key,
bytes calldata data
) public {
// Check access based on the tableRoute
if (!_hasAccess(tableRouteId, msg.sender))
if (!_hasAccess(bytes32(tableRouteId), msg.sender))
revert RouteAccessDenied(RouteTable.get(bytes32(tableRouteId)), msg.sender);

// Set the record
Expand All @@ -243,7 +245,7 @@ contract World is Store {
if (!_isRoute(subRoute)) revert RouteInvalid(subRoute);

// Construct the table route id by concatenating accessRoute and tableRoute
bytes32 tableRouteId = keccak256(abi.encodePacked(accessRoute, subRoute));
uint256 tableRouteId = uint256(keccak256(abi.encodePacked(accessRoute, subRoute)));

// Set the field
StoreCore.setField(tableRouteId, key, schemaIndex, data);
Expand All @@ -254,13 +256,14 @@ contract World is Store {
* This overload exists to conform with the `IStore` interface.
*/
function setField(
bytes32 tableRouteId,
uint256 tableRouteId,
bytes32[] calldata key,
uint8 schemaIndex,
bytes calldata data
) public override {
// Check access based on the tableRoute
if (!_hasAccess(tableRouteId, msg.sender)) revert RouteAccessDenied(RouteTable.get(tableRouteId), msg.sender);
if (!_hasAccess(bytes32(tableRouteId), msg.sender))
revert RouteAccessDenied(RouteTable.get(bytes32(tableRouteId)), msg.sender);

// Set the field
StoreCore.setField(tableRouteId, key, schemaIndex, data);
Expand All @@ -283,7 +286,7 @@ contract World is Store {
if (!_isRoute(subRoute)) revert RouteInvalid(subRoute);

// Construct the table route id by concatenating accessRoute and tableRoute
bytes32 tableRouteId = keccak256(abi.encodePacked(accessRoute, subRoute));
uint256 tableRouteId = uint256(keccak256(abi.encodePacked(accessRoute, subRoute)));

// Delete the record
StoreCore.deleteRecord(tableRouteId, key);
Expand All @@ -293,9 +296,10 @@ contract World is Store {
* Delete a record in a table based on specific access rights.
* This overload exists to conform with the `IStore` interface.
*/
function deleteRecord(bytes32 tableRouteId, bytes32[] calldata key) public override {
function deleteRecord(uint256 tableRouteId, bytes32[] calldata key) public override {
// Check access based on the tableRoute
if (!_hasAccess(tableRouteId, msg.sender)) revert RouteAccessDenied(RouteTable.get(tableRouteId), msg.sender);
if (!_hasAccess(bytes32(tableRouteId), msg.sender))
revert RouteAccessDenied(RouteTable.get(bytes32(tableRouteId)), msg.sender);

// Delete the record
StoreCore.deleteRecord(tableRouteId, key);
Expand Down
12 changes: 6 additions & 6 deletions packages/world/src/schemas/Address.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ struct AddressSchema {
library AddressSchemaLib {
/** Get the table's schema */
function getSchema() internal pure returns (Schema schema) {
schema = SchemaLib.encode(SchemaType.Address);
schema = SchemaLib.encode(SchemaType.ADDRESS);
}

/** Register the table's schema */
function registerSchema(bytes32 tableId) internal {
function registerSchema(uint256 tableId) internal {
StoreSwitch.registerSchema(tableId, getSchema());
}

function registerSchema(bytes32 tableId, IStore store) internal {
function registerSchema(uint256 tableId, IStore store) internal {
store.registerSchema(tableId, getSchema());
}

/** Set the table's data */
function set(
bytes32 tableId,
uint256 tableId,
bytes32 key,
address addr
) internal {
Expand All @@ -45,15 +45,15 @@ library AddressSchemaLib {
}

/** Get the table's data */
function get(bytes32 tableId, bytes32 key) internal view returns (address) {
function get(uint256 tableId, bytes32 key) internal view returns (address) {
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = key;
bytes memory blob = StoreSwitch.getRecord(tableId, keyTuple);
return address(bytes20(blob));
}

function get(
bytes32 tableId,
uint256 tableId,
IStore store,
bytes32 key
) internal view returns (address) {
Expand Down
14 changes: 7 additions & 7 deletions packages/world/src/schemas/AddressArray.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ struct AddressArraySchema {
library AddressArraySchemaLib {
/** Get the table's schema */
function getSchema() internal pure returns (Schema schema) {
schema = SchemaLib.encode(SchemaType.AddressArray);
schema = SchemaLib.encode(SchemaType.ADDRESS_ARRAY);
}

/** Register the table's schema */
function registerSchema(bytes32 tableId) internal {
function registerSchema(uint256 tableId) internal {
StoreSwitch.registerSchema(tableId, getSchema());
}

function registerSchema(bytes32 tableId, IStore store) internal {
function registerSchema(uint256 tableId, IStore store) internal {
store.registerSchema(tableId, getSchema());
}

/** Set the table's data */
function set(
bytes32 tableId,
uint256 tableId,
bytes32 key,
address[] memory addresses
) internal {
Expand All @@ -52,7 +52,7 @@ library AddressArraySchemaLib {
* TODO: this is super inefficient right now, need to add support for pushing to arrays to the store core library to avoid reading/writing the entire array
*/
function push(
bytes32 tableId,
uint256 tableId,
bytes32 key,
address addr
) internal {
Expand All @@ -63,15 +63,15 @@ library AddressArraySchemaLib {
}

/** Get the table's data */
function get(bytes32 tableId, bytes32 key) internal view returns (address[] memory addresses) {
function get(uint256 tableId, bytes32 key) internal view returns (address[] memory addresses) {
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = key;
bytes memory blob = StoreSwitch.getRecord(tableId, keyTuple);
return decode(blob);
}

function get(
bytes32 tableId,
uint256 tableId,
IStore store,
bytes32 key
) internal view returns (address[] memory addresses) {
Expand Down
14 changes: 7 additions & 7 deletions packages/world/src/schemas/AddressToBytes32.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ struct AddressToBytes32Schema {
library AddressToBytes32SchemaLib {
/** Get the table's schema */
function getSchema() internal pure returns (Schema schema) {
schema = SchemaLib.encode(SchemaType.Bytes32);
schema = SchemaLib.encode(SchemaType.BYTES32);
}

/** Register the table's schema */
function registerSchema(bytes32 tableId) internal {
function registerSchema(uint256 tableId) internal {
StoreSwitch.registerSchema(tableId, getSchema());
}

function registerSchema(bytes32 tableId, IStore store) internal {
function registerSchema(uint256 tableId, IStore store) internal {
store.registerSchema(tableId, getSchema());
}

/** Set the table's data */
function set(
bytes32 tableId,
uint256 tableId,
address key,
bytes32 value
) internal {
Expand All @@ -45,15 +45,15 @@ library AddressToBytes32SchemaLib {
}

/** Get the table's data */
function get(bytes32 tableId, address key) internal view returns (bytes32) {
function get(uint256 tableId, address key) internal view returns (bytes32) {
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = bytes20(key);
bytes memory blob = StoreSwitch.getRecord(tableId, keyTuple);
return bytes32(blob);
}

function get(
bytes32 tableId,
uint256 tableId,
IStore store,
address key
) internal view returns (bytes32) {
Expand All @@ -63,7 +63,7 @@ library AddressToBytes32SchemaLib {
return bytes32(blob);
}

function has(bytes32 tableId, address key) internal view returns (bool) {
function has(uint256 tableId, address key) internal view returns (bool) {
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = bytes20(key);
return bytes32(StoreSwitch.getRecord(tableId, keyTuple)) != 0;
Expand Down
14 changes: 7 additions & 7 deletions packages/world/src/schemas/Bool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ struct BoolSchema {
library BoolSchemaLib {
/** Get the table's schema */
function getSchema() internal pure returns (Schema schema) {
schema = SchemaLib.encode(SchemaType.Bool);
schema = SchemaLib.encode(SchemaType.BOOL);
}

/** Register the table's schema */
function registerSchema(bytes32 tableId) internal {
function registerSchema(uint256 tableId) internal {
StoreSwitch.registerSchema(tableId, getSchema());
}

function registerSchema(bytes32 tableId, IStore store) internal {
function registerSchema(uint256 tableId, IStore store) internal {
store.registerSchema(tableId, getSchema());
}

/** Set the table's data */
function set(
bytes32 tableId,
uint256 tableId,
bytes32 key,
bool value
) internal {
Expand All @@ -46,7 +46,7 @@ library BoolSchemaLib {
}

function set(
bytes32 tableId,
uint256 tableId,
IStore store,
bytes32 key,
bool value
Expand All @@ -57,15 +57,15 @@ library BoolSchemaLib {
}

/** Get the table's data */
function get(bytes32 tableId, bytes32 key) internal view returns (bool) {
function get(uint256 tableId, bytes32 key) internal view returns (bool) {
bytes32[] memory keyTuple = new bytes32[](1);
keyTuple[0] = key;
bytes memory blob = StoreSwitch.getRecord(tableId, keyTuple);
return SliceLib.fromBytes(blob).toBool();
}

function get(
bytes32 tableId,
uint256 tableId,
IStore store,
bytes32 key
) internal view returns (bool) {
Expand Down
14 changes: 7 additions & 7 deletions packages/world/src/schemas/RouteAccess.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ struct RouteAccessSchema {
library RouteAccessSchemaLib {
/** Get the table's schema */
function getSchema() internal pure returns (Schema schema) {
schema = SchemaLib.encode(SchemaType.Bool);
schema = SchemaLib.encode(SchemaType.BOOL);
}

/** Register the table's schema */
function registerSchema(bytes32 tableId) internal {
function registerSchema(uint256 tableId) internal {
StoreSwitch.registerSchema(tableId, getSchema());
}

function registerSchema(bytes32 tableId, IStore store) internal {
function registerSchema(uint256 tableId, IStore store) internal {
store.registerSchema(tableId, getSchema());
}

/** Set the table's data */
function set(
bytes32 tableId,
uint256 tableId,
bytes32 routeId,
address caller,
bool access
Expand All @@ -49,7 +49,7 @@ library RouteAccessSchemaLib {

/** Get the table's data */
function get(
bytes32 tableId,
uint256 tableId,
bytes32 routeId,
address caller
) internal view returns (bool) {
Expand All @@ -61,7 +61,7 @@ library RouteAccessSchemaLib {
}

function get(
bytes32 tableId,
uint256 tableId,
IStore store,
bytes32 routeId,
address caller
Expand All @@ -74,7 +74,7 @@ library RouteAccessSchemaLib {
}

function deleteRecord(
bytes32 tableId,
uint256 tableId,
bytes32 routeId,
address caller
) internal {
Expand Down
Loading

0 comments on commit e3b5be7

Please sign in to comment.