From a775d2a5fe318240acf8bff4a70f5c7d8e20bfcb Mon Sep 17 00:00:00 2001 From: alvrs Date: Wed, 19 Jul 2023 12:17:40 +0100 Subject: [PATCH] refactor(world): integrate changes to registerTable and getValueSchema --- .../abi/CoreModule.sol/CoreModule.abi.json | 16 +- .../abi/CoreSystem.sol/CoreSystem.abi.json | 135 ++++------ .../abi/IBaseWorld.sol/IBaseWorld.abi.json | 139 +++++----- packages/world/abi/IStore.sol/IStore.abi.json | 77 +++--- .../world/abi/IStore.sol/IStoreData.abi.json | 2 +- .../world/abi/IStore.sol/IStoreRead.abi.json | 2 +- .../IStore.sol/IStoreRegistration.abi.json | 43 ++- .../IStoreErrors.sol/IStoreErrors.abi.json | 16 ++ .../IWorldRegistrationSystem.abi.json | 42 +-- .../abi/StoreRead.sol/StoreRead.abi.json | 18 +- .../StoreRegistrationSystem.abi.json | 41 +-- .../abi/TableData.sol/TableData.abi.json | 1 + packages/world/abi/World.sol/World.abi.json | 18 +- .../WorldRegistrationSystem.abi.json | 68 ++--- .../world/abi/src/IStore.sol/IStore.abi.json | 77 +++--- .../abi/src/IStore.sol/IStoreData.abi.json | 2 +- .../abi/src/IStore.sol/IStoreRead.abi.json | 2 +- .../IStore.sol/IStoreRegistration.abi.json | 43 ++- packages/world/src/World.sol | 2 +- .../interfaces/IWorldRegistrationSystem.sol | 11 +- .../world/src/modules/core/CoreModule.sol | 34 +-- .../StoreRegistrationSystem.sol | 37 +-- .../WorldRegistrationSystem.sol | 23 +- .../modules/core/tables/FunctionSelectors.sol | 92 +++---- .../src/modules/core/tables/ResourceType.sol | 58 ++-- .../src/modules/core/tables/SystemHooks.sol | 85 +++--- .../modules/core/tables/SystemRegistry.sol | 58 ++-- .../world/src/modules/core/tables/Systems.sol | 80 +++--- .../modules/keysintable/KeysInTableModule.sol | 20 +- .../keysintable/tables/KeysInTable.sol | 249 ++++++++++-------- .../keysintable/tables/UsedKeysIndex.sol | 81 +++--- .../keyswithvalue/KeysWithValueModule.sol | 16 +- .../keyswithvalue/tables/KeysWithValue.sol | 85 +++--- .../uniqueentity/UniqueEntityModule.sol | 7 +- .../uniqueentity/tables/UniqueEntity.sol | 59 ++--- .../world/src/tables/InstalledModules.sol | 69 +++-- packages/world/src/tables/NamespaceOwner.sol | 58 ++-- packages/world/src/tables/ResourceAccess.sol | 61 ++--- packages/world/test/AccessControl.t.sol | 4 +- packages/world/test/KeysInTableModule.t.sol | 77 ++++-- packages/world/test/KeysWithValueModule.t.sol | 9 +- packages/world/test/World.t.sol | 239 ++++++++++------- packages/world/test/WorldDynamicUpdate.t.sol | 36 +-- packages/world/test/query.t.sol | 122 ++++----- packages/world/test/tables/AddressArray.sol | 85 +++--- packages/world/test/tables/Bool.sol | 59 ++--- 46 files changed, 1226 insertions(+), 1332 deletions(-) create mode 100644 packages/world/abi/TableData.sol/TableData.abi.json diff --git a/packages/world/abi/CoreModule.sol/CoreModule.abi.json b/packages/world/abi/CoreModule.sol/CoreModule.abi.json index 1bb5e1625b9..7aca0b168b5 100644 --- a/packages/world/abi/CoreModule.sol/CoreModule.abi.json +++ b/packages/world/abi/CoreModule.sol/CoreModule.abi.json @@ -82,17 +82,17 @@ { "inputs": [ { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" + "internalType": "uint256", + "name": "expected", + "type": "uint256" }, { - "internalType": "string", - "name": "tableIdString", - "type": "string" + "internalType": "uint256", + "name": "received", + "type": "uint256" } ], - "name": "StoreCore_TableAlreadyExists", + "name": "StoreCore_InvalidKeyNamesLength", "type": "error" }, { @@ -108,7 +108,7 @@ "type": "string" } ], - "name": "StoreCore_TableNotFound", + "name": "StoreCore_TableAlreadyExists", "type": "error" }, { diff --git a/packages/world/abi/CoreSystem.sol/CoreSystem.abi.json b/packages/world/abi/CoreSystem.sol/CoreSystem.abi.json index a887a2245ec..acc95232769 100644 --- a/packages/world/abi/CoreSystem.sol/CoreSystem.abi.json +++ b/packages/world/abi/CoreSystem.sol/CoreSystem.abi.json @@ -150,25 +150,25 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, - { - "inputs": [], - "name": "StoreCore_NotDynamicField", - "type": "error" - }, { "inputs": [ { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" + "internalType": "uint256", + "name": "expected", + "type": "uint256" }, { - "internalType": "string", - "name": "tableIdString", - "type": "string" + "internalType": "uint256", + "name": "received", + "type": "uint256" } ], - "name": "StoreCore_TableAlreadyExists", + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, + { + "inputs": [], + "name": "StoreCore_NotDynamicField", "type": "error" }, { @@ -184,7 +184,7 @@ "type": "string" } ], - "name": "StoreCore_TableNotFound", + "name": "StoreCore_TableAlreadyExists", "type": "error" }, { @@ -429,29 +429,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "valueSchema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -541,13 +518,23 @@ }, { "internalType": "Schema", - "name": "valueSchema", + "name": "keySchema", "type": "bytes32" }, { "internalType": "Schema", - "name": "keySchema", + "name": "valueSchema", "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], "name": "registerTable", @@ -564,22 +551,32 @@ { "inputs": [ { - "internalType": "bytes16", - "name": "namespace", - "type": "bytes16" + "internalType": "bytes32", + "name": "tableId", + "type": "bytes32" }, { - "internalType": "bytes16", - "name": "name", - "type": "bytes16" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" }, { - "internalType": "contract IStoreHook", - "name": "hook", - "type": "address" + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], - "name": "registerTableHook", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -597,12 +594,12 @@ "type": "bytes16" }, { - "internalType": "address", - "name": "grantee", + "internalType": "contract IStoreHook", + "name": "hook", "type": "address" } ], - "name": "revokeAccess", + "name": "registerTableHook", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -620,40 +617,12 @@ "type": "bytes16" }, { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" + "internalType": "address", + "name": "grantee", + "type": "address" } ], - "name": "setMetadata", + "name": "revokeAccess", "outputs": [], "stateMutability": "nonpayable", "type": "function" diff --git a/packages/world/abi/IBaseWorld.sol/IBaseWorld.abi.json b/packages/world/abi/IBaseWorld.sol/IBaseWorld.abi.json index 29c6d356669..030d25492cb 100644 --- a/packages/world/abi/IBaseWorld.sol/IBaseWorld.abi.json +++ b/packages/world/abi/IBaseWorld.sol/IBaseWorld.abi.json @@ -129,6 +129,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -597,7 +613,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", @@ -913,29 +929,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -1025,13 +1018,23 @@ }, { "internalType": "Schema", - "name": "valueSchema", + "name": "keySchema", "type": "bytes32" }, { "internalType": "Schema", - "name": "keySchema", + "name": "valueSchema", "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], "name": "registerTable", @@ -1045,6 +1048,39 @@ "stateMutability": "nonpayable", "type": "function" }, + { + "inputs": [ + { + "internalType": "bytes32", + "name": "table", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "valueNames", + "type": "string[]" + } + ], + "name": "registerTable", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, { "inputs": [ { @@ -1162,57 +1198,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes16", - "name": "namespace", - "type": "bytes16" - }, - { - "internalType": "bytes16", - "name": "name", - "type": "bytes16" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { diff --git a/packages/world/abi/IStore.sol/IStore.abi.json b/packages/world/abi/IStore.sol/IStore.abi.json index 3dc7bc3b3a1..eef3bc4b5dd 100644 --- a/packages/world/abi/IStore.sol/IStore.abi.json +++ b/packages/world/abi/IStore.sol/IStore.abi.json @@ -47,6 +47,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -408,7 +424,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", @@ -493,17 +509,12 @@ "type": "bytes32" }, { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" + "internalType": "contract IStoreHook", + "name": "hook", + "type": "address" } ], - "name": "registerSchema", + "name": "registerStoreHook", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -516,12 +527,27 @@ "type": "bytes32" }, { - "internalType": "contract IStoreHook", - "name": "hook", - "type": "address" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "valueNames", + "type": "string[]" } ], - "name": "registerStoreHook", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -559,29 +585,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { diff --git a/packages/world/abi/IStore.sol/IStoreData.abi.json b/packages/world/abi/IStore.sol/IStoreData.abi.json index 8bdc4379fa0..f02203d5e9d 100644 --- a/packages/world/abi/IStore.sol/IStoreData.abi.json +++ b/packages/world/abi/IStore.sol/IStoreData.abi.json @@ -265,7 +265,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/IStore.sol/IStoreRead.abi.json b/packages/world/abi/IStore.sol/IStoreRead.abi.json index ca89eb4ba56..0f47144f3dd 100644 --- a/packages/world/abi/IStore.sol/IStoreRead.abi.json +++ b/packages/world/abi/IStore.sol/IStoreRead.abi.json @@ -167,7 +167,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/IStore.sol/IStoreRegistration.abi.json b/packages/world/abi/IStore.sol/IStoreRegistration.abi.json index 2bf01232236..f33165e2763 100644 --- a/packages/world/abi/IStore.sol/IStoreRegistration.abi.json +++ b/packages/world/abi/IStore.sol/IStoreRegistration.abi.json @@ -1,27 +1,4 @@ [ - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -48,17 +25,27 @@ "type": "bytes32" }, { - "internalType": "string", - "name": "tableName", - "type": "string" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" }, { "internalType": "string[]", - "name": "fieldNames", + "name": "valueNames", "type": "string[]" } ], - "name": "setMetadata", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" diff --git a/packages/world/abi/IStoreErrors.sol/IStoreErrors.abi.json b/packages/world/abi/IStoreErrors.sol/IStoreErrors.abi.json index d38ae0cc0d4..cf6d3753966 100644 --- a/packages/world/abi/IStoreErrors.sol/IStoreErrors.abi.json +++ b/packages/world/abi/IStoreErrors.sol/IStoreErrors.abi.json @@ -47,6 +47,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", diff --git a/packages/world/abi/IWorldRegistrationSystem.sol/IWorldRegistrationSystem.abi.json b/packages/world/abi/IWorldRegistrationSystem.sol/IWorldRegistrationSystem.abi.json index 5552356eb8c..6c5efc1fd56 100644 --- a/packages/world/abi/IWorldRegistrationSystem.sol/IWorldRegistrationSystem.abi.json +++ b/packages/world/abi/IWorldRegistrationSystem.sol/IWorldRegistrationSystem.abi.json @@ -174,13 +174,23 @@ }, { "internalType": "Schema", - "name": "valueSchema", + "name": "keySchema", "type": "bytes32" }, { "internalType": "Schema", - "name": "keySchema", + "name": "valueSchema", "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], "name": "registerTable", @@ -216,33 +226,5 @@ "outputs": [], "stateMutability": "nonpayable", "type": "function" - }, - { - "inputs": [ - { - "internalType": "bytes16", - "name": "namespace", - "type": "bytes16" - }, - { - "internalType": "bytes16", - "name": "name", - "type": "bytes16" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" } ] \ No newline at end of file diff --git a/packages/world/abi/StoreRead.sol/StoreRead.abi.json b/packages/world/abi/StoreRead.sol/StoreRead.abi.json index b05f278530b..93a0e17cf50 100644 --- a/packages/world/abi/StoreRead.sol/StoreRead.abi.json +++ b/packages/world/abi/StoreRead.sol/StoreRead.abi.json @@ -73,6 +73,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -278,7 +294,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/StoreRegistrationSystem.sol/StoreRegistrationSystem.abi.json b/packages/world/abi/StoreRegistrationSystem.sol/StoreRegistrationSystem.abi.json index 7815ac594ad..fb6658c07a1 100644 --- a/packages/world/abi/StoreRegistrationSystem.sol/StoreRegistrationSystem.abi.json +++ b/packages/world/abi/StoreRegistrationSystem.sol/StoreRegistrationSystem.abi.json @@ -15,29 +15,6 @@ "name": "SchemaLib_StaticTypeAfterDynamicType", "type": "error" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "valueSchema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -64,9 +41,19 @@ "type": "bytes32" }, { - "internalType": "string", - "name": "tableName", - "type": "string" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" }, { "internalType": "string[]", @@ -74,7 +61,7 @@ "type": "string[]" } ], - "name": "setMetadata", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" diff --git a/packages/world/abi/TableData.sol/TableData.abi.json b/packages/world/abi/TableData.sol/TableData.abi.json new file mode 100644 index 00000000000..0637a088a01 --- /dev/null +++ b/packages/world/abi/TableData.sol/TableData.abi.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/packages/world/abi/World.sol/World.abi.json b/packages/world/abi/World.sol/World.abi.json index aad1a60dadf..24c4c756dc8 100644 --- a/packages/world/abi/World.sol/World.abi.json +++ b/packages/world/abi/World.sol/World.abi.json @@ -171,6 +171,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -552,7 +568,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/WorldRegistrationSystem.sol/WorldRegistrationSystem.abi.json b/packages/world/abi/WorldRegistrationSystem.sol/WorldRegistrationSystem.abi.json index c0f14dc1354..7bf047f4f4f 100644 --- a/packages/world/abi/WorldRegistrationSystem.sol/WorldRegistrationSystem.abi.json +++ b/packages/world/abi/WorldRegistrationSystem.sol/WorldRegistrationSystem.abi.json @@ -150,25 +150,25 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, - { - "inputs": [], - "name": "StoreCore_NotDynamicField", - "type": "error" - }, { "inputs": [ { - "internalType": "bytes32", - "name": "tableId", - "type": "bytes32" + "internalType": "uint256", + "name": "expected", + "type": "uint256" }, { - "internalType": "string", - "name": "tableIdString", - "type": "string" + "internalType": "uint256", + "name": "received", + "type": "uint256" } ], - "name": "StoreCore_TableAlreadyExists", + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, + { + "inputs": [], + "name": "StoreCore_NotDynamicField", "type": "error" }, { @@ -184,7 +184,7 @@ "type": "string" } ], - "name": "StoreCore_TableNotFound", + "name": "StoreCore_TableAlreadyExists", "type": "error" }, { @@ -373,13 +373,23 @@ }, { "internalType": "Schema", - "name": "valueSchema", + "name": "keySchema", "type": "bytes32" }, { "internalType": "Schema", - "name": "keySchema", + "name": "valueSchema", "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "fieldNames", + "type": "string[]" } ], "name": "registerTable", @@ -415,33 +425,5 @@ "outputs": [], "stateMutability": "nonpayable", "type": "function" - }, - { - "inputs": [ - { - "internalType": "bytes16", - "name": "namespace", - "type": "bytes16" - }, - { - "internalType": "bytes16", - "name": "name", - "type": "bytes16" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" } ] \ No newline at end of file diff --git a/packages/world/abi/src/IStore.sol/IStore.abi.json b/packages/world/abi/src/IStore.sol/IStore.abi.json index 3dc7bc3b3a1..eef3bc4b5dd 100644 --- a/packages/world/abi/src/IStore.sol/IStore.abi.json +++ b/packages/world/abi/src/IStore.sol/IStore.abi.json @@ -47,6 +47,22 @@ "name": "StoreCore_InvalidFieldNamesLength", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "expected", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "received", + "type": "uint256" + } + ], + "name": "StoreCore_InvalidKeyNamesLength", + "type": "error" + }, { "inputs": [], "name": "StoreCore_NotDynamicField", @@ -408,7 +424,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", @@ -493,17 +509,12 @@ "type": "bytes32" }, { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" + "internalType": "contract IStoreHook", + "name": "hook", + "type": "address" } ], - "name": "registerSchema", + "name": "registerStoreHook", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -516,12 +527,27 @@ "type": "bytes32" }, { - "internalType": "contract IStoreHook", - "name": "hook", - "type": "address" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" + }, + { + "internalType": "string[]", + "name": "valueNames", + "type": "string[]" } ], - "name": "registerStoreHook", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" @@ -559,29 +585,6 @@ "stateMutability": "nonpayable", "type": "function" }, - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "string", - "name": "tableName", - "type": "string" - }, - { - "internalType": "string[]", - "name": "fieldNames", - "type": "string[]" - } - ], - "name": "setMetadata", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { diff --git a/packages/world/abi/src/IStore.sol/IStoreData.abi.json b/packages/world/abi/src/IStore.sol/IStoreData.abi.json index 8bdc4379fa0..f02203d5e9d 100644 --- a/packages/world/abi/src/IStore.sol/IStoreData.abi.json +++ b/packages/world/abi/src/IStore.sol/IStoreData.abi.json @@ -265,7 +265,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/src/IStore.sol/IStoreRead.abi.json b/packages/world/abi/src/IStore.sol/IStoreRead.abi.json index ca89eb4ba56..0f47144f3dd 100644 --- a/packages/world/abi/src/IStore.sol/IStoreRead.abi.json +++ b/packages/world/abi/src/IStore.sol/IStoreRead.abi.json @@ -167,7 +167,7 @@ "type": "bytes32" } ], - "name": "getSchema", + "name": "getValueSchema", "outputs": [ { "internalType": "Schema", diff --git a/packages/world/abi/src/IStore.sol/IStoreRegistration.abi.json b/packages/world/abi/src/IStore.sol/IStoreRegistration.abi.json index 2bf01232236..f33165e2763 100644 --- a/packages/world/abi/src/IStore.sol/IStoreRegistration.abi.json +++ b/packages/world/abi/src/IStore.sol/IStoreRegistration.abi.json @@ -1,27 +1,4 @@ [ - { - "inputs": [ - { - "internalType": "bytes32", - "name": "table", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "schema", - "type": "bytes32" - }, - { - "internalType": "Schema", - "name": "keySchema", - "type": "bytes32" - } - ], - "name": "registerSchema", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, { "inputs": [ { @@ -48,17 +25,27 @@ "type": "bytes32" }, { - "internalType": "string", - "name": "tableName", - "type": "string" + "internalType": "Schema", + "name": "keySchema", + "type": "bytes32" + }, + { + "internalType": "Schema", + "name": "valueSchema", + "type": "bytes32" + }, + { + "internalType": "string[]", + "name": "keyNames", + "type": "string[]" }, { "internalType": "string[]", - "name": "fieldNames", + "name": "valueNames", "type": "string[]" } ], - "name": "setMetadata", + "name": "registerTable", "outputs": [], "stateMutability": "nonpayable", "type": "function" diff --git a/packages/world/src/World.sol b/packages/world/src/World.sol index 1b24600f462..9ef8361508f 100644 --- a/packages/world/src/World.sol +++ b/packages/world/src/World.sol @@ -31,7 +31,7 @@ contract World is StoreRead, IStoreData, IWorldKernel { // Register internal NamespaceOwner table and give ownership of the root // namespace to msg.sender. This is done in the constructor instead of a // module, so that we can use it for access control checks in `installRootModule`. - NamespaceOwner.registerSchema(); + NamespaceOwner.register(); NamespaceOwner.set(ROOT_NAMESPACE, msg.sender); // Other internal tables are registered by the CoreModule to reduce World's bytecode size. diff --git a/packages/world/src/interfaces/IWorldRegistrationSystem.sol b/packages/world/src/interfaces/IWorldRegistrationSystem.sol index 2db43b87acf..63b7273234f 100644 --- a/packages/world/src/interfaces/IWorldRegistrationSystem.sol +++ b/packages/world/src/interfaces/IWorldRegistrationSystem.sol @@ -14,16 +14,11 @@ interface IWorldRegistrationSystem { function registerTable( bytes16 namespace, bytes16 name, + Schema keySchema, Schema valueSchema, - Schema keySchema - ) external returns (bytes32 resourceSelector); - - function setMetadata( - bytes16 namespace, - bytes16 name, - string calldata tableName, + string[] calldata keyNames, string[] calldata fieldNames - ) external; + ) external returns (bytes32 resourceSelector); function registerHook(bytes16 namespace, bytes16 name, address hook) external; diff --git a/packages/world/src/modules/core/CoreModule.sol b/packages/world/src/modules/core/CoreModule.sol index 3f9ceca6431..c6fe5b0875f 100644 --- a/packages/world/src/modules/core/CoreModule.sol +++ b/packages/world/src/modules/core/CoreModule.sol @@ -57,29 +57,15 @@ contract CoreModule is IModule, WorldContext { * Register core tables in the World */ function _registerCoreTables() internal { - NamespaceOwner.setMetadata(); + InstalledModules.register(); + ResourceAccess.register(); + Systems.register(); + FunctionSelectors.register(); + SystemHooks.register(); + SystemRegistry.register(); + ResourceType.register(); - InstalledModules.registerSchema(); - InstalledModules.setMetadata(); - - ResourceAccess.registerSchema(); - ResourceAccess.setMetadata(); ResourceAccess.set(ROOT_NAMESPACE, _msgSender(), true); - - Systems.registerSchema(); - Systems.setMetadata(); - - FunctionSelectors.registerSchema(); - FunctionSelectors.setMetadata(); - - SystemHooks.registerSchema(); - SystemHooks.setMetadata(); - - SystemRegistry.registerSchema(); - SystemRegistry.setMetadata(); - - ResourceType.registerSchema(); - ResourceType.setMetadata(); ResourceType.set(ROOT_NAMESPACE, Resource.NAMESPACE); } @@ -107,11 +93,10 @@ contract CoreModule is IModule, WorldContext { * Register function selectors for all CoreSystem functions in the World */ function _registerFunctionSelectors() internal { - bytes4[17] memory functionSelectors = [ + bytes4[15] memory functionSelectors = [ // --- WorldRegistrationSystem --- WorldRegistrationSystem.registerNamespace.selector, WorldRegistrationSystem.registerTable.selector, - WorldRegistrationSystem.setMetadata.selector, WorldRegistrationSystem.registerHook.selector, WorldRegistrationSystem.registerTableHook.selector, WorldRegistrationSystem.registerSystemHook.selector, @@ -119,8 +104,7 @@ contract CoreModule is IModule, WorldContext { WorldRegistrationSystem.registerFunctionSelector.selector, WorldRegistrationSystem.registerRootFunctionSelector.selector, // --- StoreRegistrationSystem --- - StoreRegistrationSystem.registerSchema.selector, - StoreRegistrationSystem.setMetadata.selector, + StoreRegistrationSystem.registerTable.selector, StoreRegistrationSystem.registerStoreHook.selector, // --- ModuleInstallationSystem --- ModuleInstallationSystem.installModule.selector, diff --git a/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol b/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol index 6d155c2175a..b60114fc425 100644 --- a/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol +++ b/packages/world/src/modules/core/implementations/StoreRegistrationSystem.sol @@ -23,11 +23,17 @@ contract StoreRegistrationSystem is IStoreRegistration, System { using ResourceSelector for bytes32; /** - * Register the given schema for the given table id. + * Register the given key schema, value schema, key names and value names for the given table id. * This overload exists to conform with the IStore interface. * Access is checked based on the namespace or name (encoded in the tableId). */ - function registerSchema(bytes32 tableId, Schema valueSchema, Schema keySchema) public virtual { + function registerTable( + bytes32 tableId, + Schema keySchema, + Schema valueSchema, + string[] calldata keyNames, + string[] calldata fieldNames + ) public virtual { (address systemAddress, ) = Systems.get(ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME)); // We can't call IBaseWorld(this).registerSchema directly because it would be handled like @@ -39,32 +45,9 @@ contract StoreRegistrationSystem is IStoreRegistration, System { WorldRegistrationSystem.registerTable.selector, tableId.getNamespace(), tableId.getName(), + keySchema, valueSchema, - keySchema - ), - delegate: true, - value: 0 - }); - } - - /** - * Register metadata (tableName, fieldNames) for 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 setMetadata(bytes32 tableId, string calldata tableName, string[] calldata fieldNames) public virtual { - (address systemAddress, ) = Systems.get(ResourceSelector.from(ROOT_NAMESPACE, CORE_SYSTEM_NAME)); - - // We can't call IBaseWorld(this).setMetadata directly because it would be handled like - // an external call, so msg.sender would be the address of the World contract - Call.withSender({ - msgSender: _msgSender(), - target: systemAddress, - funcSelectorAndArgs: abi.encodeWithSelector( - WorldRegistrationSystem.setMetadata.selector, - tableId.getNamespace(), - tableId.getName(), - tableName, + keyNames, fieldNames ), delegate: true, diff --git a/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol b/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol index 7f92199fb51..b87f34b2ae6 100644 --- a/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol +++ b/packages/world/src/modules/core/implementations/WorldRegistrationSystem.sol @@ -52,8 +52,10 @@ contract WorldRegistrationSystem is System, IWorldErrors { function registerTable( bytes16 namespace, bytes16 name, + Schema keySchema, Schema valueSchema, - Schema keySchema + string[] calldata keyNames, + string[] calldata fieldNames ) public virtual returns (bytes32 resourceSelector) { resourceSelector = ResourceSelector.from(namespace, name); @@ -74,24 +76,7 @@ contract WorldRegistrationSystem is System, IWorldErrors { ResourceType.set(resourceSelector, Resource.TABLE); // Register the table's schema - StoreCore.registerSchema(resourceSelector, valueSchema, keySchema); - } - - /** - * Register metadata (tableName, fieldNames) for the table at the given namespace and name. - * Requires the caller to own the namespace. - */ - function setMetadata( - bytes16 namespace, - bytes16 name, - string calldata tableName, - string[] calldata fieldNames - ) public virtual { - // Require caller to own the namespace - bytes32 tableId = AccessControl.requireOwnerOrSelf(namespace, name, _msgSender()); - - // Set the metadata - StoreCore.setMetadata(tableId, tableName, fieldNames); + StoreCore.registerTable(resourceSelector, keySchema, valueSchema, keyNames, fieldNames); } /** diff --git a/packages/world/src/modules/core/tables/FunctionSelectors.sol b/packages/world/src/modules/core/tables/FunctionSelectors.sol index 530fd6a5fd9..4e2a5bc7460 100644 --- a/packages/world/src/modules/core/tables/FunctionSelectors.sol +++ b/packages/world/src/modules/core/tables/FunctionSelectors.sol @@ -21,16 +21,7 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Funct bytes32 constant FunctionSelectorsTableId = _tableId; library FunctionSelectors { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](3); - _schema[0] = SchemaType.BYTES16; - _schema[1] = SchemaType.BYTES16; - _schema[2] = SchemaType.BYTES4; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.BYTES4; @@ -38,35 +29,38 @@ library FunctionSelectors { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](3); - _fieldNames[0] = "namespace"; - _fieldNames[1] = "name"; - _fieldNames[2] = "systemFunctionSelector"; - return ("FunctionSelectors", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](3); + _schema[0] = SchemaType.BYTES16; + _schema[1] = SchemaType.BYTES16; + _schema[2] = SchemaType.BYTES4; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "functionSelector"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](3); + fieldNames[0] = "namespace"; + fieldNames[1] = "name"; + fieldNames[2] = "systemFunctionSelector"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get namespace */ @@ -74,7 +68,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (Bytes.slice16(_blob, 0)); } @@ -83,7 +77,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (Bytes.slice16(_blob, 0)); } @@ -92,7 +86,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((namespace)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((namespace)), getValueSchema()); } /** Set namespace (using the specified store) */ @@ -100,7 +94,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((namespace)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((namespace)), getValueSchema()); } /** Get name */ @@ -108,7 +102,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (Bytes.slice16(_blob, 0)); } @@ -117,7 +111,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (Bytes.slice16(_blob, 0)); } @@ -126,7 +120,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((name)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((name)), getValueSchema()); } /** Set name (using the specified store) */ @@ -134,7 +128,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((name)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((name)), getValueSchema()); } /** Get systemFunctionSelector */ @@ -142,7 +136,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); return (Bytes.slice4(_blob, 0)); } @@ -154,7 +148,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); return (Bytes.slice4(_blob, 0)); } @@ -163,7 +157,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((systemFunctionSelector)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 2, abi.encodePacked((systemFunctionSelector)), getValueSchema()); } /** Set systemFunctionSelector (using the specified store) */ @@ -171,7 +165,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((systemFunctionSelector)), getSchema()); + _store.setField(_tableId, _keyTuple, 2, abi.encodePacked((systemFunctionSelector)), getValueSchema()); } /** Get the full data */ @@ -181,7 +175,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -193,7 +187,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -204,7 +198,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -220,7 +214,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Decode the tightly packed blob using this table's schema */ @@ -250,7 +244,7 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -258,6 +252,6 @@ library FunctionSelectors { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(functionSelector); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/core/tables/ResourceType.sol b/packages/world/src/modules/core/tables/ResourceType.sol index 1a850fc38bb..cd403d01d5c 100644 --- a/packages/world/src/modules/core/tables/ResourceType.sol +++ b/packages/world/src/modules/core/tables/ResourceType.sol @@ -24,48 +24,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Resou bytes32 constant ResourceTypeTableId = _tableId; library ResourceType { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT8; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.UINT8; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "resourceType"; - return ("ResourceType", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "resourceSelector"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "resourceType"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get resourceType */ @@ -73,7 +67,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return Resource(uint8(Bytes.slice1(_blob, 0))); } @@ -82,7 +76,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return Resource(uint8(Bytes.slice1(_blob, 0))); } @@ -91,7 +85,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), getValueSchema()); } /** Set resourceType (using the specified store) */ @@ -99,7 +93,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked(uint8(resourceType)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -118,7 +112,7 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -126,6 +120,6 @@ library ResourceType { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/core/tables/SystemHooks.sol b/packages/world/src/modules/core/tables/SystemHooks.sol index cdbe5e6c548..ba66bd3ed43 100644 --- a/packages/world/src/modules/core/tables/SystemHooks.sol +++ b/packages/world/src/modules/core/tables/SystemHooks.sol @@ -21,48 +21,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Syste bytes32 constant SystemHooksTableId = _tableId; library SystemHooks { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS_ARRAY; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.ADDRESS_ARRAY; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("SystemHooks", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "resourceSelector"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -70,7 +64,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -79,7 +73,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -88,7 +82,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Set value (using the specified store) */ @@ -96,7 +90,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Get the length of value */ @@ -104,7 +98,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); return _byteLength / 20; } @@ -113,7 +107,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); return _byteLength / 20; } @@ -122,7 +116,14 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 20, (_index + 1) * 20); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 20, + (_index + 1) * 20 + ); return (address(Bytes.slice20(_blob, 0))); } @@ -131,7 +132,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 20, (_index + 1) * 20); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getValueSchema(), _index * 20, (_index + 1) * 20); return (address(Bytes.slice20(_blob, 0))); } @@ -140,7 +141,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to value (using the specified store) */ @@ -148,7 +149,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from value */ @@ -156,7 +157,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getValueSchema()); } /** Pop an element from value (using the specified store) */ @@ -164,7 +165,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.popFromField(_tableId, _keyTuple, 0, 20, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 20, getValueSchema()); } /** Update an element of value at `_index` */ @@ -172,7 +173,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getValueSchema()); } /** Update an element of value (using the specified store) at `_index` */ @@ -180,7 +181,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -203,7 +204,7 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -211,6 +212,6 @@ library SystemHooks { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/core/tables/SystemRegistry.sol b/packages/world/src/modules/core/tables/SystemRegistry.sol index 6c90b48353d..5e329620967 100644 --- a/packages/world/src/modules/core/tables/SystemRegistry.sol +++ b/packages/world/src/modules/core/tables/SystemRegistry.sol @@ -21,48 +21,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Syste bytes32 constant SystemRegistryTableId = _tableId; library SystemRegistry { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.ADDRESS; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "resourceSelector"; - return ("SystemRegistry", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "system"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "resourceSelector"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get resourceSelector */ @@ -70,7 +64,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (Bytes.slice32(_blob, 0)); } @@ -79,7 +73,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (Bytes.slice32(_blob, 0)); } @@ -88,7 +82,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getValueSchema()); } /** Set resourceSelector (using the specified store) */ @@ -96,7 +90,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((resourceSelector)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -115,7 +109,7 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -123,6 +117,6 @@ library SystemRegistry { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(system))); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/core/tables/Systems.sol b/packages/world/src/modules/core/tables/Systems.sol index c8fbf75dbad..e39c40cee35 100644 --- a/packages/world/src/modules/core/tables/Systems.sol +++ b/packages/world/src/modules/core/tables/Systems.sol @@ -21,15 +21,7 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Syste bytes32 constant SystemsTableId = _tableId; library Systems { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](2); - _schema[0] = SchemaType.ADDRESS; - _schema[1] = SchemaType.BOOL; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); _schema[0] = SchemaType.BYTES32; @@ -37,34 +29,36 @@ library Systems { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](2); - _fieldNames[0] = "system"; - _fieldNames[1] = "publicAccess"; - return ("Systems", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](2); + _schema[0] = SchemaType.ADDRESS; + _schema[1] = SchemaType.BOOL; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "resourceSelector"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](2); + fieldNames[0] = "system"; + fieldNames[1] = "publicAccess"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get system */ @@ -72,7 +66,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -81,7 +75,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -90,7 +84,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), getValueSchema()); } /** Set system (using the specified store) */ @@ -98,7 +92,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((system)), getValueSchema()); } /** Get publicAccess */ @@ -106,7 +100,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -115,7 +109,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -124,7 +118,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), getValueSchema()); } /** Set publicAccess (using the specified store) */ @@ -132,7 +126,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((publicAccess)), getValueSchema()); } /** Get the full data */ @@ -140,7 +134,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -149,7 +143,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -160,7 +154,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -170,7 +164,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Decode the tightly packed blob using this table's schema */ @@ -196,7 +190,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -204,7 +198,7 @@ library Systems { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = resourceSelector; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/keysintable/KeysInTableModule.sol b/packages/world/src/modules/keysintable/KeysInTableModule.sol index e908d01a559..74abc6ad842 100644 --- a/packages/world/src/modules/keysintable/KeysInTableModule.sol +++ b/packages/world/src/modules/keysintable/KeysInTableModule.sol @@ -43,24 +43,8 @@ contract KeysInTableModule is IModule, WorldContext { if (ResourceType.get(KeysInTableTableId) == Resource.NONE) { // Register the tables - world.registerTable( - KeysInTableTableId.getNamespace(), - KeysInTableTableId.getName(), - KeysInTable.getSchema(), - KeysInTable.getKeySchema() - ); - world.registerTable( - UsedKeysIndexTableId.getNamespace(), - UsedKeysIndexTableId.getName(), - UsedKeysIndex.getSchema(), - UsedKeysIndex.getKeySchema() - ); - - // Register metadata for the tables - (string memory tableName1, string[] memory fieldNames1) = KeysInTable.getMetadata(); - world.setMetadata(KeysInTableTableId.getNamespace(), KeysInTableTableId.getName(), tableName1, fieldNames1); - (string memory tableName2, string[] memory fieldNames2) = UsedKeysIndex.getMetadata(); - world.setMetadata(UsedKeysIndexTableId.getNamespace(), UsedKeysIndexTableId.getName(), tableName2, fieldNames2); + KeysInTable.register(world); + UsedKeysIndex.register(world); // Grant the hook access to the tables world.grantAccess(KeysInTableTableId.getNamespace(), KeysInTableTableId.getName(), address(hook)); diff --git a/packages/world/src/modules/keysintable/tables/KeysInTable.sol b/packages/world/src/modules/keysintable/tables/KeysInTable.sol index fbfabc00171..257dbe2c491 100644 --- a/packages/world/src/modules/keysintable/tables/KeysInTable.sol +++ b/packages/world/src/modules/keysintable/tables/KeysInTable.sol @@ -29,8 +29,16 @@ struct KeysInTableData { } library KeysInTable { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.BYTES32; + + return SchemaLib.encode(_schema); + } + + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](5); _schema[0] = SchemaType.BYTES32_ARRAY; _schema[1] = SchemaType.BYTES32_ARRAY; @@ -41,44 +49,30 @@ library KeysInTable { return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; - - return SchemaLib.encode(_schema); - } - - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](5); - _fieldNames[0] = "keys0"; - _fieldNames[1] = "keys1"; - _fieldNames[2] = "keys2"; - _fieldNames[3] = "keys3"; - _fieldNames[4] = "keys4"; - return ("KeysInTable", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "sourceTable"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](5); + fieldNames[0] = "keys0"; + fieldNames[1] = "keys1"; + fieldNames[2] = "keys2"; + fieldNames[3] = "keys3"; + fieldNames[4] = "keys4"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get keys0 */ @@ -86,7 +80,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -95,7 +89,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -104,7 +98,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), getValueSchema()); } /** Set keys0 (using the specified store) */ @@ -112,7 +106,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keys0)), getValueSchema()); } /** Get the length of keys0 */ @@ -120,7 +114,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); return _byteLength / 32; } @@ -129,7 +123,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); return _byteLength / 32; } @@ -138,7 +132,14 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } @@ -147,7 +148,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getValueSchema(), _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } @@ -156,7 +157,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keys0 (using the specified store) */ @@ -164,7 +165,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keys0 */ @@ -172,7 +173,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getValueSchema()); } /** Pop an element from keys0 (using the specified store) */ @@ -180,7 +181,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 0, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 32, getValueSchema()); } /** Update an element of keys0 at `_index` */ @@ -188,7 +189,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Update an element of keys0 (using the specified store) at `_index` */ @@ -196,7 +197,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Get keys1 */ @@ -204,7 +205,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -213,7 +214,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -222,7 +223,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), getValueSchema()); } /** Set keys1 (using the specified store) */ @@ -230,7 +231,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, EncodeArray.encode((keys1)), getValueSchema()); } /** Get the length of keys1 */ @@ -238,7 +239,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 1, getValueSchema()); return _byteLength / 32; } @@ -247,7 +248,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 1, getValueSchema()); return _byteLength / 32; } @@ -256,7 +257,14 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 1, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } @@ -265,7 +273,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 1, getValueSchema(), _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } @@ -274,7 +282,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keys1 (using the specified store) */ @@ -282,7 +290,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 1, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keys1 */ @@ -290,7 +298,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 1, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 1, 32, getValueSchema()); } /** Pop an element from keys1 (using the specified store) */ @@ -298,7 +306,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 1, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 1, 32, getValueSchema()); } /** Update an element of keys1 at `_index` */ @@ -306,7 +314,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Update an element of keys1 (using the specified store) at `_index` */ @@ -314,7 +322,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 1, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Get keys2 */ @@ -322,7 +330,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 2, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -331,7 +339,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 2, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -340,7 +348,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), getValueSchema()); } /** Set keys2 (using the specified store) */ @@ -348,7 +356,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), getSchema()); + _store.setField(_tableId, _keyTuple, 2, EncodeArray.encode((keys2)), getValueSchema()); } /** Get the length of keys2 */ @@ -356,7 +364,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); return _byteLength / 32; } @@ -365,7 +373,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 2, getValueSchema()); return _byteLength / 32; } @@ -374,7 +382,14 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 2, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 2, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } @@ -383,7 +398,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 2, getValueSchema(), _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } @@ -392,7 +407,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keys2 (using the specified store) */ @@ -400,7 +415,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 2, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keys2 */ @@ -408,7 +423,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 2, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 2, 32, getValueSchema()); } /** Pop an element from keys2 (using the specified store) */ @@ -416,7 +431,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 2, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 2, 32, getValueSchema()); } /** Update an element of keys2 at `_index` */ @@ -424,7 +439,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Update an element of keys2 (using the specified store) at `_index` */ @@ -432,7 +447,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 2, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Get keys3 */ @@ -440,7 +455,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 3, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -449,7 +464,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 3, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -458,7 +473,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), getValueSchema()); } /** Set keys3 (using the specified store) */ @@ -466,7 +481,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), getSchema()); + _store.setField(_tableId, _keyTuple, 3, EncodeArray.encode((keys3)), getValueSchema()); } /** Get the length of keys3 */ @@ -474,7 +489,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); return _byteLength / 32; } @@ -483,7 +498,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 3, getValueSchema()); return _byteLength / 32; } @@ -492,7 +507,14 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 3, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 3, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } @@ -501,7 +523,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 3, getValueSchema(), _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } @@ -510,7 +532,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keys3 (using the specified store) */ @@ -518,7 +540,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 3, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keys3 */ @@ -526,7 +548,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 3, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 3, 32, getValueSchema()); } /** Pop an element from keys3 (using the specified store) */ @@ -534,7 +556,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 3, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 3, 32, getValueSchema()); } /** Update an element of keys3 at `_index` */ @@ -542,7 +564,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Update an element of keys3 (using the specified store) at `_index` */ @@ -550,7 +572,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 3, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Get keys4 */ @@ -558,7 +580,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 4, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -567,7 +589,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 4, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -576,7 +598,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), getValueSchema()); } /** Set keys4 (using the specified store) */ @@ -584,7 +606,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), getSchema()); + _store.setField(_tableId, _keyTuple, 4, EncodeArray.encode((keys4)), getValueSchema()); } /** Get the length of keys4 */ @@ -592,7 +614,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 4, getValueSchema()); return _byteLength / 32; } @@ -601,7 +623,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 4, getValueSchema()); return _byteLength / 32; } @@ -610,7 +632,14 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 4, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 4, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } @@ -619,7 +648,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 4, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 4, getValueSchema(), _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } @@ -628,7 +657,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keys4 (using the specified store) */ @@ -636,7 +665,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 4, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keys4 */ @@ -644,7 +673,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.popFromField(_tableId, _keyTuple, 4, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 4, 32, getValueSchema()); } /** Pop an element from keys4 (using the specified store) */ @@ -652,7 +681,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.popFromField(_tableId, _keyTuple, 4, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 4, 32, getValueSchema()); } /** Update an element of keys4 at `_index` */ @@ -660,7 +689,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Update an element of keys4 (using the specified store) at `_index` */ @@ -668,7 +697,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 4, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Get the full data */ @@ -676,7 +705,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -685,7 +714,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -703,7 +732,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -721,7 +750,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -805,7 +834,7 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -813,6 +842,6 @@ library KeysInTable { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = sourceTable; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol index 84cddae2793..aed5f7e8d00 100644 --- a/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol +++ b/packages/world/src/modules/keysintable/tables/UsedKeysIndex.sol @@ -21,15 +21,7 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("UsedK bytes32 constant UsedKeysIndexTableId = _tableId; library UsedKeysIndex { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](2); - _schema[0] = SchemaType.BOOL; - _schema[1] = SchemaType.UINT40; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](2); _schema[0] = SchemaType.BYTES32; @@ -38,34 +30,37 @@ library UsedKeysIndex { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](2); - _fieldNames[0] = "has"; - _fieldNames[1] = "index"; - return ("UsedKeysIndex", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](2); + _schema[0] = SchemaType.BOOL; + _schema[1] = SchemaType.UINT40; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](2); + keyNames[0] = "sourceTable"; + keyNames[1] = "keysHash"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](2); + fieldNames[0] = "has"; + fieldNames[1] = "index"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get has */ @@ -74,7 +69,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -84,7 +79,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -94,7 +89,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), getValueSchema()); } /** Set has (using the specified store) */ @@ -103,7 +98,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((has)), getValueSchema()); } /** Get index */ @@ -112,7 +107,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 1, getValueSchema()); return (uint40(Bytes.slice5(_blob, 0))); } @@ -122,7 +117,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 1, getValueSchema()); return (uint40(Bytes.slice5(_blob, 0))); } @@ -132,7 +127,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), getValueSchema()); } /** Set index (using the specified store) */ @@ -141,7 +136,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), getSchema()); + _store.setField(_tableId, _keyTuple, 1, abi.encodePacked((index)), getValueSchema()); } /** Get the full data */ @@ -150,7 +145,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -160,7 +155,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -172,7 +167,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -183,7 +178,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Decode the tightly packed blob using this table's schema */ @@ -211,7 +206,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -220,7 +215,7 @@ library UsedKeysIndex { _keyTuple[0] = sourceTable; _keyTuple[1] = keysHash; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol b/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol index f6af846bd5f..43c8ca34cb8 100644 --- a/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol +++ b/packages/world/src/modules/keyswithvalue/KeysWithValueModule.sol @@ -42,21 +42,7 @@ contract KeysWithValueModule is IModule, WorldContext { bytes32 targetTableSelector = getTargetTableSelector(MODULE_NAMESPACE, sourceTableId); // Register the target table - IBaseWorld(_world()).registerTable( - targetTableSelector.getNamespace(), - targetTableSelector.getName(), - KeysWithValue.getSchema(), - KeysWithValue.getKeySchema() - ); - - // Register metadata for the target table - (string memory tableName, string[] memory fieldNames) = KeysWithValue.getMetadata(); - IBaseWorld(_world()).setMetadata( - targetTableSelector.getNamespace(), - targetTableSelector.getName(), - tableName, - fieldNames - ); + KeysWithValue.register(IBaseWorld(_world()), targetTableSelector); // Grant the hook access to the target table IBaseWorld(_world()).grantAccess(targetTableSelector.getNamespace(), targetTableSelector.getName(), address(hook)); diff --git a/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol b/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol index ca318fe214c..7991cffe1db 100644 --- a/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol +++ b/packages/world/src/modules/keyswithvalue/tables/KeysWithValue.sol @@ -18,48 +18,42 @@ import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; library KeysWithValue { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32_ARRAY; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.BYTES32_ARRAY; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "keysWithValue"; - return ("KeysWithValue", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema(bytes32 _tableId) internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "valueHash"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store, bytes32 _tableId) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "keysWithValue"; } - /** Set the table's metadata */ - function setMetadata(bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register(bytes32 _tableId) internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store, bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store, bytes32 _tableId) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get keysWithValue */ @@ -67,7 +61,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -80,7 +74,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_bytes32()); } @@ -89,7 +83,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), getValueSchema()); } /** Set keysWithValue (using the specified store) */ @@ -97,7 +91,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((keysWithValue)), getValueSchema()); } /** Get the length of keysWithValue */ @@ -105,7 +99,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); return _byteLength / 32; } @@ -114,7 +108,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); return _byteLength / 32; } @@ -123,7 +117,14 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 32, + (_index + 1) * 32 + ); return (Bytes.slice32(_blob, 0)); } @@ -132,7 +133,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 32, (_index + 1) * 32); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getValueSchema(), _index * 32, (_index + 1) * 32); return (Bytes.slice32(_blob, 0)); } @@ -141,7 +142,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to keysWithValue (using the specified store) */ @@ -149,7 +150,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from keysWithValue */ @@ -157,7 +158,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 32, getValueSchema()); } /** Pop an element from keysWithValue (using the specified store) */ @@ -165,7 +166,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.popFromField(_tableId, _keyTuple, 0, 32, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 32, getValueSchema()); } /** Update an element of keysWithValue at `_index` */ @@ -173,7 +174,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Update an element of keysWithValue (using the specified store) at `_index` */ @@ -181,7 +182,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 32, abi.encodePacked((_element)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -204,7 +205,7 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -212,6 +213,6 @@ library KeysWithValue { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = valueHash; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol b/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol index d86a040b6ed..05ffefa7dcb 100644 --- a/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol +++ b/packages/world/src/modules/uniqueentity/UniqueEntityModule.sol @@ -5,6 +5,7 @@ import { IBaseWorld } from "../../interfaces/IBaseWorld.sol"; import { IModule } from "../../interfaces/IModule.sol"; import { WorldContext } from "../../WorldContext.sol"; +import { ResourceSelector } from "../../ResourceSelector.sol"; import { UniqueEntity } from "./tables/UniqueEntity.sol"; import { UniqueEntitySystem } from "./UniqueEntitySystem.sol"; @@ -28,13 +29,11 @@ contract UniqueEntityModule is IModule, WorldContext { IBaseWorld world = IBaseWorld(_world()); // Register table - world.registerTable(NAMESPACE, TABLE_NAME, UniqueEntity.getSchema(), UniqueEntity.getKeySchema()); - // Set table's metadata - (string memory tableName, string[] memory fieldNames) = UniqueEntity.getMetadata(); - world.setMetadata(NAMESPACE, TABLE_NAME, tableName, fieldNames); + UniqueEntity.register(world, ResourceSelector.from(NAMESPACE, TABLE_NAME)); // Register system world.registerSystem(NAMESPACE, SYSTEM_NAME, uniqueEntitySystem, true); + // Register system's functions world.registerFunctionSelector(NAMESPACE, SYSTEM_NAME, "getUniqueEntity", "()"); } diff --git a/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol b/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol index b346e586e92..9bc015bbc5a 100644 --- a/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol +++ b/packages/world/src/modules/uniqueentity/tables/UniqueEntity.sol @@ -18,54 +18,47 @@ import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; library UniqueEntity { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.UINT256; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](0); return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("UniqueEntity", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.UINT256; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema(bytes32 _tableId) internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store, bytes32 _tableId) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata(bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register(bytes32 _tableId) internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store, bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store, bytes32 _tableId) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ function get(bytes32 _tableId) internal view returns (uint256 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint256(Bytes.slice32(_blob, 0))); } @@ -73,7 +66,7 @@ library UniqueEntity { function get(IStore _store, bytes32 _tableId) internal view returns (uint256 value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (uint256(Bytes.slice32(_blob, 0))); } @@ -81,14 +74,14 @@ library UniqueEntity { function set(bytes32 _tableId, uint256 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Set value (using the specified store) */ function set(IStore _store, bytes32 _tableId, uint256 value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -105,13 +98,13 @@ library UniqueEntity { function deleteRecord(bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/tables/InstalledModules.sol b/packages/world/src/tables/InstalledModules.sol index 6595d0f7fac..5e2de4536c9 100644 --- a/packages/world/src/tables/InstalledModules.sol +++ b/packages/world/src/tables/InstalledModules.sol @@ -25,14 +25,7 @@ struct InstalledModulesData { } library InstalledModules { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](2); _schema[0] = SchemaType.BYTES16; @@ -41,33 +34,35 @@ library InstalledModules { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "moduleAddress"; - return ("InstalledModules", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.ADDRESS; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](2); + keyNames[0] = "moduleName"; + keyNames[1] = "argumentsHash"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "moduleAddress"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get moduleAddress */ @@ -76,7 +71,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -90,7 +85,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -100,7 +95,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getValueSchema()); } /** Set moduleAddress (using the specified store) */ @@ -109,7 +104,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((moduleAddress)), getValueSchema()); } /** Get the full data */ @@ -118,7 +113,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = StoreSwitch.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -132,7 +127,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getSchema()); + bytes memory _blob = _store.getRecord(_tableId, _keyTuple, getValueSchema()); return decode(_blob); } @@ -144,7 +139,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - StoreSwitch.setRecord(_tableId, _keyTuple, _data, getSchema()); + StoreSwitch.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using individual values (using the specified store) */ @@ -155,7 +150,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - _store.setRecord(_tableId, _keyTuple, _data, getSchema()); + _store.setRecord(_tableId, _keyTuple, _data, getValueSchema()); } /** Set the full data using the data struct */ @@ -194,7 +189,7 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -203,6 +198,6 @@ library InstalledModules { _keyTuple[0] = bytes32(moduleName); _keyTuple[1] = argumentsHash; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/tables/NamespaceOwner.sol b/packages/world/src/tables/NamespaceOwner.sol index 2f7dfea5fe5..4623a6f10bd 100644 --- a/packages/world/src/tables/NamespaceOwner.sol +++ b/packages/world/src/tables/NamespaceOwner.sol @@ -21,48 +21,42 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Names bytes32 constant NamespaceOwnerTableId = _tableId; library NamespaceOwner { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS; + _schema[0] = SchemaType.BYTES16; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES16; + _schema[0] = SchemaType.ADDRESS; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "owner"; - return ("NamespaceOwner", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "namespace"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "owner"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get owner */ @@ -70,7 +64,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -79,7 +73,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (address(Bytes.slice20(_blob, 0))); } @@ -88,7 +82,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), getValueSchema()); } /** Set owner (using the specified store) */ @@ -96,7 +90,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((owner)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -115,7 +109,7 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -123,6 +117,6 @@ library NamespaceOwner { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(namespace); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/src/tables/ResourceAccess.sol b/packages/world/src/tables/ResourceAccess.sol index 936f5e9f9a3..58ed2c3befa 100644 --- a/packages/world/src/tables/ResourceAccess.sol +++ b/packages/world/src/tables/ResourceAccess.sol @@ -21,14 +21,7 @@ bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("Resou bytes32 constant ResourceAccessTableId = _tableId; library ResourceAccess { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BOOL; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](2); _schema[0] = SchemaType.BYTES32; @@ -37,33 +30,35 @@ library ResourceAccess { return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "access"; - return ("ResourceAccess", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.BOOL; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema() internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](2); + keyNames[0] = "resourceSelector"; + keyNames[1] = "caller"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "access"; } - /** Set the table's metadata */ - function setMetadata() internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register() internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get access */ @@ -72,7 +67,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -82,7 +77,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -92,7 +87,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), getValueSchema()); } /** Set access (using the specified store) */ @@ -101,7 +96,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((access)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -122,7 +117,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -131,7 +126,7 @@ library ResourceAccess { _keyTuple[0] = resourceSelector; _keyTuple[1] = bytes32(uint256(uint160(caller))); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/test/AccessControl.t.sol b/packages/world/test/AccessControl.t.sol index ad7accb5f97..fcafb4f67db 100644 --- a/packages/world/test/AccessControl.t.sol +++ b/packages/world/test/AccessControl.t.sol @@ -17,8 +17,8 @@ contract AccessControlTest is Test, StoreReadWithStubs { address caller = address(0x01); function setUp() public { - ResourceAccess.registerSchema(); - NamespaceOwner.registerSchema(); + ResourceAccess.register(); + NamespaceOwner.register(); NamespaceOwner.set(namespace, address(this)); ResourceAccess.set(ResourceSelector.from(namespace, 0), address(this), true); diff --git a/packages/world/test/KeysInTableModule.t.sol b/packages/world/test/KeysInTableModule.t.sol index b330eaaa0c8..a1547985c10 100644 --- a/packages/world/test/KeysInTableModule.t.sol +++ b/packages/world/test/KeysInTableModule.t.sol @@ -34,7 +34,7 @@ contract KeysInTableModuleTest is Test, GasReporter { bytes32 key3 = keccak256("test3"); bytes32[] keyTuple3; - Schema tableSchema; + Schema tableValueSchema; Schema tableKeySchema; Schema singletonKeySchema; Schema compositeKeySchema; @@ -46,7 +46,7 @@ contract KeysInTableModuleTest is Test, GasReporter { uint256 val2 = 42; function setUp() public { - tableSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); + tableValueSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); tableKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); compositeKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32, SchemaType.BYTES32); @@ -65,9 +65,23 @@ contract KeysInTableModuleTest is Test, GasReporter { function _installKeysInTableModule() internal { // Register source table - tableId = world.registerTable(namespace, name, tableSchema, tableKeySchema); - singletonTableId = world.registerTable(namespace, singletonName, tableSchema, singletonKeySchema); - compositeTableId = world.registerTable(namespace, compositeName, tableSchema, compositeKeySchema); + tableId = world.registerTable(namespace, name, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); + singletonTableId = world.registerTable( + namespace, + singletonName, + singletonKeySchema, + tableValueSchema, + new string[](0), + new string[](1) + ); + compositeTableId = world.registerTable( + namespace, + compositeName, + compositeKeySchema, + tableValueSchema, + new string[](3), + new string[](1) + ); // Install the index module // TODO: add support for installing this via installModule @@ -86,7 +100,7 @@ contract KeysInTableModuleTest is Test, GasReporter { bytes32[] memory keyTuple = new bytes32[](0); - world.setRecord(namespace, singletonName, keyTuple, abi.encodePacked(val1), tableSchema); + world.setRecord(namespace, singletonName, keyTuple, abi.encodePacked(val1), tableValueSchema); // Get the list of keys in this target table bytes32[][] memory keysInTable = getKeysInTable(world, singletonTableId); @@ -103,7 +117,7 @@ contract KeysInTableModuleTest is Test, GasReporter { keyTuple[1] = "two"; keyTuple[2] = "three"; - world.setRecord(namespace, compositeName, keyTuple, abi.encodePacked(val1), tableSchema); + world.setRecord(namespace, compositeName, keyTuple, abi.encodePacked(val1), tableValueSchema); // Get the list of keys in this target table bytes32[][] memory keysInTable = getKeysInTable(world, compositeTableId); @@ -128,7 +142,7 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Set a value in the source table startGasReport("set a record on a table with keysInTableModule installed"); - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value), tableSchema); + world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value), tableValueSchema); endGasReport(); // Get the list of keys in this target table @@ -147,7 +161,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Set a value in the source table startGasReport("set a record on a table with keysInTableModule installed (first)"); - world.setRecord(namespace, name, keyTuple, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, name, keyTuple, abi.encodePacked(value1), tableValueSchema); endGasReport(); // Get the list of keys in the first target table @@ -159,7 +173,14 @@ contract KeysInTableModuleTest is Test, GasReporter { // Install the hook on the second table bytes16 sourceFile2 = bytes16("source2"); - bytes32 sourceTableId2 = world.registerTable(namespace, sourceFile2, tableSchema, tableKeySchema); + bytes32 sourceTableId2 = world.registerTable( + namespace, + sourceFile2, + tableValueSchema, + tableKeySchema, + new string[](1), + new string[](1) + ); world.installRootModule(keysInTableModule, abi.encode(sourceTableId2)); keyTuple = new bytes32[](1); @@ -167,7 +188,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Set a value in the source table startGasReport("set a record on a table with keysInTableModule installed (second)"); - world.setRecord(namespace, sourceFile2, keyTuple, abi.encodePacked(value2), tableSchema); + world.setRecord(namespace, sourceFile2, keyTuple, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the second target table @@ -187,7 +208,7 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Set a value in the source table - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table bytes32[][] memory keysInTable = getKeysInTable(world, tableId); @@ -197,7 +218,7 @@ contract KeysInTableModuleTest is Test, GasReporter { assertEq(keysInTable[0][0], key1, "2"); // Set another key with the same value - world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table keysInTable = getKeysInTable(world, tableId); @@ -209,7 +230,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Change the value of the first key startGasReport("change a record on a table with keysInTableModule installed"); - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value2), tableSchema); + world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -222,7 +243,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Delete the first key startGasReport("delete a record on a table with keysInTableModule installed"); - world.deleteRecord(namespace, name, keyTuple1, tableSchema); + world.deleteRecord(namespace, name, keyTuple1, tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -252,7 +273,7 @@ contract KeysInTableModuleTest is Test, GasReporter { keyTupleB[2] = "charlie"; // Set a value in the source table - world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table bytes32[][] memory keysInTable = getKeysInTable(world, compositeTableId); @@ -264,7 +285,7 @@ contract KeysInTableModuleTest is Test, GasReporter { } // Set another key with the same value - world.setRecord(namespace, compositeName, keyTupleB, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, compositeName, keyTupleB, abi.encodePacked(value1), tableValueSchema); // Get the list of keys in the target table keysInTable = getKeysInTable(world, compositeTableId); @@ -280,7 +301,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Change the value of the first key startGasReport("change a composite record on a table with keysInTableModule installed"); - world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value2), tableSchema); + world.setRecord(namespace, compositeName, keyTupleA, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -297,7 +318,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Delete the first key startGasReport("delete a composite record on a table with keysInTableModule installed"); - world.deleteRecord(namespace, compositeName, keyTupleA, tableSchema); + world.deleteRecord(namespace, compositeName, keyTupleA, tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -315,7 +336,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Set a value in the source table startGasReport("set a field on a table with keysInTableModule installed"); - world.setField(namespace, name, keyTuple1, 0, abi.encodePacked(value1), tableSchema); + world.setField(namespace, name, keyTuple1, 0, abi.encodePacked(value1), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -327,7 +348,7 @@ contract KeysInTableModuleTest is Test, GasReporter { // Change the value using setField startGasReport("change a field on a table with keysInTableModule installed"); - world.setField(namespace, name, keyTuple1, 0, abi.encodePacked(value2), tableSchema); + world.setField(namespace, name, keyTuple1, 0, abi.encodePacked(value2), tableValueSchema); endGasReport(); // Get the list of keys in the target table @@ -342,7 +363,7 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Set a value in the source table - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1), tableSchema); + world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value1), tableValueSchema); startGasReport("Get list of keys in a given table"); bytes32[][] memory keysInTable = getKeysInTable(world, tableId); @@ -353,7 +374,7 @@ contract KeysInTableModuleTest is Test, GasReporter { assertEq(keysInTable[0][0], key1); // Set another key with a different value - world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value2), tableSchema); + world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value2), tableValueSchema); // Get the list of keys in the target table keysInTable = getKeysInTable(world, tableId); @@ -368,14 +389,14 @@ contract KeysInTableModuleTest is Test, GasReporter { _installKeysInTableModule(); // Add 3 values - world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value), tableSchema); - world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value), tableSchema); - world.setRecord(namespace, name, keyTuple3, abi.encodePacked(value), tableSchema); + world.setRecord(namespace, name, keyTuple1, abi.encodePacked(value), tableValueSchema); + world.setRecord(namespace, name, keyTuple2, abi.encodePacked(value), tableValueSchema); + world.setRecord(namespace, name, keyTuple3, abi.encodePacked(value), tableValueSchema); // Remove 2, starting from the middle // This tests that KeysInTable correctly tracks swaps indexes - world.deleteRecord(namespace, name, keyTuple2, tableSchema); - world.deleteRecord(namespace, name, keyTuple3, tableSchema); + world.deleteRecord(namespace, name, keyTuple2, tableValueSchema); + world.deleteRecord(namespace, name, keyTuple3, tableValueSchema); // Get the list of keys in the target table bytes32[][] memory keysInTable = getKeysInTable(world, tableId); diff --git a/packages/world/test/KeysWithValueModule.t.sol b/packages/world/test/KeysWithValueModule.t.sol index 5ab148ca007..3f55d0b8d79 100644 --- a/packages/world/test/KeysWithValueModule.t.sol +++ b/packages/world/test/KeysWithValueModule.t.sol @@ -52,7 +52,14 @@ contract KeysWithValueModuleTest is Test, GasReporter { function _installKeysWithValueModule() internal { // Register source table - sourceTableId = world.registerTable(namespace, sourceName, sourceTableSchema, sourceTableKeySchema); + sourceTableId = world.registerTable( + namespace, + sourceName, + sourceTableSchema, + sourceTableKeySchema, + new string[](1), + new string[](1) + ); // Install the index module // TODO: add support for installing this via installModule diff --git a/packages/world/test/World.t.sol b/packages/world/test/World.t.sol index ac82db2502a..75498e8eeb3 100644 --- a/packages/world/test/World.t.sol +++ b/packages/world/test/World.t.sol @@ -12,6 +12,8 @@ import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol"; import { Schema } from "@latticexyz/store/src/Schema.sol"; import { SchemaEncodeHelper } from "@latticexyz/store/test/SchemaEncodeHelper.sol"; import { StoreMetadataData, StoreMetadata } from "@latticexyz/store/src/codegen/Tables.sol"; +import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; +import { StoreMetadataData, StoreMetadata, TableData, TableDataTableId } from "@latticexyz/store/src/codegen/Tables.sol"; import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol"; import { World } from "../src/World.sol"; @@ -19,7 +21,7 @@ import { System } from "../src/System.sol"; import { ResourceSelector } from "../src/ResourceSelector.sol"; import { ROOT_NAMESPACE, ROOT_NAME } from "../src/constants.sol"; -import { NamespaceOwner } from "../src/tables/NamespaceOwner.sol"; +import { NamespaceOwner, NamespaceOwnerTableId } from "../src/tables/NamespaceOwner.sol"; import { ResourceAccess } from "../src/tables/ResourceAccess.sol"; import { CoreModule } from "../src/modules/core/CoreModule.sol"; @@ -77,7 +79,7 @@ contract WorldTestSystem is System { function writeData(bytes16 namespace, bytes16 name, bool data) public { bytes32[] memory key = new bytes32[](0); bytes32 tableId = ResourceSelector.from(namespace, name); - Schema valueSchema = StoreSwitch.getSchema(tableId); + Schema valueSchema = StoreSwitch.getValueSchema(tableId); if (StoreSwitch.getStoreAddress() == address(this)) { StoreCore.setRecord(tableId, key, abi.encodePacked(data), valueSchema); @@ -191,19 +193,19 @@ contract WorldTest is Test, GasReporter { emit HelloWorld(); new World(); - bytes32[] memory schemaKey = new bytes32[](1); - schemaKey[0] = StoreCoreInternal.SCHEMA_TABLE; - bytes memory value = world.getRecord( - StoreCoreInternal.SCHEMA_TABLE, - schemaKey, - SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32) - ); + // Should have registered the table data table (fka schema table) + assertEq(TableData.getKeySchema(world, TableDataTableId), Schema.unwrap(TableData.getKeySchema())); + assertEq(TableData.getValueSchema(world, TableDataTableId), Schema.unwrap(TableData.getValueSchema())); + assertEq(TableData.getAbiEncodedKeyNames(world, TableDataTableId), abi.encode(TableData.getKeyNames())); + assertEq(TableData.getAbiEncodedValueNames(world, TableDataTableId), abi.encode(TableData.getFieldNames())); + + // Should have registered the namespace owner table + assertEq(TableData.getKeySchema(world, NamespaceOwnerTableId), Schema.unwrap(NamespaceOwner.getKeySchema())); + assertEq(TableData.getValueSchema(world, NamespaceOwnerTableId), Schema.unwrap(NamespaceOwner.getValueSchema())); + assertEq(TableData.getAbiEncodedKeyNames(world, NamespaceOwnerTableId), abi.encode(NamespaceOwner.getKeyNames())); assertEq( - value, - abi.encodePacked( - SchemaEncodeHelper.encode(SchemaType.BYTES32, SchemaType.BYTES32), - SchemaEncodeHelper.encode(SchemaType.BYTES32) - ) + TableData.getAbiEncodedValueNames(world, NamespaceOwnerTableId), + abi.encode(NamespaceOwner.getFieldNames()) ); } @@ -248,74 +250,52 @@ contract WorldTest is Test, GasReporter { } function testRegisterTable() public { - Schema schema = SchemaEncodeHelper.encode(SchemaType.BOOL, SchemaType.UINT256, SchemaType.STRING); + Schema valueSchema = SchemaEncodeHelper.encode(SchemaType.BOOL, SchemaType.UINT256, SchemaType.STRING); bytes16 namespace = "testNamespace"; - bytes16 table = "testTable"; + bytes16 tableName = "testTable"; + + string[] memory keyNames = new string[](1); + keyNames[0] = "key1"; + string[] memory valueNames = new string[](3); + valueNames[0] = "value1"; + valueNames[1] = "value2"; + valueNames[2] = "value3"; startGasReport("Register a new table in the namespace"); - bytes32 tableSelector = world.registerTable(namespace, table, schema, defaultKeySchema); + bytes32 tableSelector = world.registerTable( + namespace, + tableName, + defaultKeySchema, + valueSchema, + keyNames, + valueNames + ); endGasReport(); // Expect the namespace to be created and owned by the caller - assertEq(NamespaceOwner.get(world, namespace), address(this)); + assertEq(NamespaceOwner.get(world, namespace), address(this), "namespace should be created by caller"); // Expect the table to be registered + assertEq(world.getKeySchema(tableSelector).unwrap(), defaultKeySchema.unwrap(), "key schema should be registered"); + assertEq(world.getValueSchema(tableSelector).unwrap(), valueSchema.unwrap(), "value schema should be registered"); + + bytes memory loadedKeyNames = TableData.getAbiEncodedKeyNames(world, tableSelector); + assertEq(loadedKeyNames, abi.encode(keyNames), "key names should be registered"); - assertEq(world.getSchema(tableSelector).unwrap(), schema.unwrap(), "schema should be registered"); + bytes memory loadedValueNames = TableData.getAbiEncodedValueNames(world, tableSelector); + assertEq(loadedValueNames, abi.encode(valueNames), "value names should be registered"); // Expect an error when registering an existing table vm.expectRevert(abi.encodeWithSelector(IWorldErrors.ResourceExists.selector, tableSelector.toString())); - world.registerTable(namespace, table, schema, defaultKeySchema); + world.registerTable(namespace, tableName, defaultKeySchema, valueSchema, keyNames, valueNames); // Expect an error when registering a table in a namespace that is not owned by the caller _expectAccessDenied(address(0x01), namespace, ""); - world.registerTable(namespace, "otherTable", schema, defaultKeySchema); + world.registerTable(namespace, "otherTable", defaultKeySchema, valueSchema, keyNames, valueNames); // Expect the World to be allowed to call registerTable vm.prank(address(world)); - world.registerTable(namespace, "otherTable", schema, defaultKeySchema); - } - - function testSetMetadata() public { - string memory tableName = "testTable"; - bytes16 namespace = "testNamespace"; - bytes16 name = "tableName"; - bytes32 tableId = ResourceSelector.from(namespace, name); - - Schema schema = SchemaEncodeHelper.encode(SchemaType.UINT8, SchemaType.UINT8); - string[] memory fieldNames = new string[](2); - fieldNames[0] = "testField1"; - fieldNames[1] = "testField2"; - - // Expect an error when setting metadata on a resource that does not exist - vm.expectRevert(); - world.setMetadata("invalid", "invalid", tableName, fieldNames); - - // Register a table - world.registerTable(namespace, name, schema, defaultKeySchema); - - startGasReport("Set metadata"); - world.setMetadata(namespace, name, tableName, fieldNames); - endGasReport(); - - // Expect the metadata to be set - StoreMetadataData memory metadata = StoreMetadata.get(world, tableId); - assertEq(metadata.tableName, tableName); - assertEq(metadata.abiEncodedFieldNames, abi.encode(fieldNames)); - - // Expect it to be possible to change metadata - world.setMetadata(namespace, name, "newTableName", fieldNames); - metadata = StoreMetadata.get(world, tableId); - assertEq(metadata.tableName, "newTableName"); - assertEq(metadata.abiEncodedFieldNames, abi.encode(fieldNames)); - - // Expect an error when setting metadata on a route that is not owned by the caller - _expectAccessDenied(address(1), namespace, name); - world.setMetadata(namespace, name, tableName, fieldNames); - - // Expect the World to be allowed to set metadata - vm.prank(address(world)); - world.setMetadata(namespace, name, tableName, fieldNames); + world.registerTable(namespace, "otherTable", defaultKeySchema, valueSchema, keyNames, valueNames); } function testRegisterSystem() public { @@ -374,7 +354,14 @@ contract WorldTest is Test, GasReporter { function testDuplicateSelectors() public { // Register a new table - bytes32 resourceSelector = world.registerTable("namespace", "name", Bool.getSchema(), defaultKeySchema); + bytes32 resourceSelector = world.registerTable( + "namespace", + "name", + defaultKeySchema, + Bool.getValueSchema(), + new string[](1), + new string[](1) + ); // Deploy a new system System system = new System(); @@ -388,7 +375,14 @@ contract WorldTest is Test, GasReporter { // Expect an error when trying to register a table at the same selector vm.expectRevert(abi.encodeWithSelector(IWorldErrors.ResourceExists.selector, resourceSelector.toString())); - world.registerTable("namespace2", "name", Bool.getSchema(), defaultKeySchema); + world.registerTable( + "namespace2", + "name", + defaultKeySchema, + Bool.getValueSchema(), + new string[](1), + new string[](1) + ); } function testGrantAccess() public { @@ -401,7 +395,14 @@ contract WorldTest is Test, GasReporter { function testSetRecord() public { // Register a new table - bytes32 tableId = world.registerTable("testSetRecord", "testTable", Bool.getSchema(), defaultKeySchema); + bytes32 tableId = world.registerTable( + "testSetRecord", + "testTable", + defaultKeySchema, + Bool.getValueSchema(), + new string[](1), + new string[](1) + ); startGasReport("Write data to the table"); Bool.set(world, tableId, true); @@ -422,44 +423,58 @@ contract WorldTest is Test, GasReporter { function testSetField() public { bytes16 namespace = "testSetField"; bytes16 name = "testTable"; - Schema schema = Bool.getSchema(); + Schema valueSchema = Bool.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable(namespace, name, schema, defaultKeySchema); + bytes32 tableId = world.registerTable( + namespace, + name, + defaultKeySchema, + valueSchema, + new string[](1), + new string[](1) + ); startGasReport("Write data to a table field"); - world.setField(namespace, name, singletonKey, 0, abi.encodePacked(true), schema); + world.setField(namespace, name, singletonKey, 0, abi.encodePacked(true), valueSchema); endGasReport(); // Expect the data to be written assertTrue(Bool.get(world, tableId)); // Write data to the table via its tableId - world.setField(tableId, singletonKey, 0, abi.encodePacked(false), schema); + world.setField(tableId, singletonKey, 0, abi.encodePacked(false), valueSchema); // Expect the data to be written assertFalse(Bool.get(world, tableId)); // Expect an error when trying to write from an address that doesn't have access when calling via the namespace _expectAccessDenied(address(0x01), "testSetField", "testTable"); - world.setField("testSetField", "testTable", singletonKey, 0, abi.encodePacked(true), schema); + world.setField("testSetField", "testTable", singletonKey, 0, abi.encodePacked(true), valueSchema); // Expect an error when trying to write from an address that doesn't have access when calling via the tableId _expectAccessDenied(address(0x01), "testSetField", "testTable"); - world.setField(tableId, singletonKey, 0, abi.encodePacked(true), schema); + world.setField(tableId, singletonKey, 0, abi.encodePacked(true), valueSchema); // Expect the World to have access vm.prank(address(world)); - world.setField("testSetField", "testTable", singletonKey, 0, abi.encodePacked(true), schema); + world.setField("testSetField", "testTable", singletonKey, 0, abi.encodePacked(true), valueSchema); } function testPushToField() public { bytes16 namespace = "testPushToField"; bytes16 name = "testTable"; - Schema schema = AddressArray.getSchema(); + Schema valueSchema = AddressArray.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable(namespace, name, schema, defaultKeySchema); + bytes32 tableId = world.registerTable( + namespace, + name, + defaultKeySchema, + valueSchema, + new string[](1), + new string[](1) + ); // Create data address[] memory dataToPush = new address[](3); @@ -469,76 +484,83 @@ contract WorldTest is Test, GasReporter { bytes memory encodedData = EncodeArray.encode(dataToPush); startGasReport("Push data to the table"); - world.pushToField(namespace, name, keyTuple, 0, encodedData, schema); + world.pushToField(namespace, name, keyTuple, 0, encodedData, valueSchema); endGasReport(); // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), dataToPush); // Delete the data - world.deleteRecord(namespace, name, keyTuple, schema); + world.deleteRecord(namespace, name, keyTuple, valueSchema); // Push data to the table via direct access - world.pushToField(tableId, keyTuple, 0, encodedData, schema); + world.pushToField(tableId, keyTuple, 0, encodedData, valueSchema); // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), dataToPush); // Expect an error when trying to write from an address that doesn't have access (via namespace/name) _expectAccessDenied(address(0x01), namespace, name); - world.pushToField(namespace, name, keyTuple, 0, encodedData, schema); + world.pushToField(namespace, name, keyTuple, 0, encodedData, valueSchema); // Expect an error when trying to write from an address that doesn't have access (via tableId) _expectAccessDenied(address(0x01), namespace, name); - world.pushToField(tableId, keyTuple, 0, encodedData, schema); + world.pushToField(tableId, keyTuple, 0, encodedData, valueSchema); // Expect the World to have access vm.prank(address(world)); - world.pushToField(namespace, name, keyTuple, 0, encodedData, schema); + world.pushToField(namespace, name, keyTuple, 0, encodedData, valueSchema); } function testDeleteRecord() public { bytes16 namespace = "testDeleteRecord"; bytes16 name = "testTable"; - Schema schema = Bool.getSchema(); + Schema valueSchema = Bool.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable(namespace, name, schema, defaultKeySchema); + bytes32 tableId = world.registerTable( + namespace, + name, + defaultKeySchema, + valueSchema, + new string[](1), + new string[](1) + ); // Write data to the table via the namespace and expect it to be written - world.setRecord(namespace, name, singletonKey, abi.encodePacked(true), schema); + world.setRecord(namespace, name, singletonKey, abi.encodePacked(true), valueSchema); assertTrue(Bool.get(world, tableId)); startGasReport("Delete record"); - world.deleteRecord(namespace, name, singletonKey, schema); + world.deleteRecord(namespace, name, singletonKey, valueSchema); endGasReport(); // expect it to be deleted assertFalse(Bool.get(world, tableId)); // Write data to the table via the namespace and expect it to be written - world.setRecord("testDeleteRecord", "testTable", singletonKey, abi.encodePacked(true), schema); + world.setRecord("testDeleteRecord", "testTable", singletonKey, abi.encodePacked(true), valueSchema); assertTrue(Bool.get(world, tableId)); // Delete the record via the tableId and expect it to be deleted - world.deleteRecord(tableId, singletonKey, schema); + world.deleteRecord(tableId, singletonKey, valueSchema); assertFalse(Bool.get(world, tableId)); // Write data to the table via the namespace and expect it to be written - world.setRecord("testDeleteRecord", "testTable", singletonKey, abi.encodePacked(true), schema); + world.setRecord("testDeleteRecord", "testTable", singletonKey, abi.encodePacked(true), valueSchema); assertTrue(Bool.get(world, tableId)); // Expect an error when trying to delete from an address that doesn't have access when calling via the namespace _expectAccessDenied(address(0x01), "testDeleteRecord", "testTable"); - world.deleteRecord("testDeleteRecord", "testTable", singletonKey, schema); + world.deleteRecord("testDeleteRecord", "testTable", singletonKey, valueSchema); // Expect an error when trying to delete from an address that doesn't have access when calling via the tableId _expectAccessDenied(address(0x02), "testDeleteRecord", "testTable"); - world.deleteRecord(tableId, singletonKey, schema); + world.deleteRecord(tableId, singletonKey, valueSchema); // Expect the World to have access vm.prank(address(world)); - world.deleteRecord("testDeleteRecord", "testTable", singletonKey, schema); + world.deleteRecord("testDeleteRecord", "testTable", singletonKey, valueSchema); } function testCall() public { @@ -613,10 +635,17 @@ contract WorldTest is Test, GasReporter { } function testRegisterTableHook() public { - Schema schema = Bool.getSchema(); + Schema valueSchema = Bool.getValueSchema(); // Register a new table - bytes32 tableId = world.registerTable("", "testTable", Bool.getSchema(), defaultKeySchema); + bytes32 tableId = world.registerTable( + "", + "testTable", + defaultKeySchema, + valueSchema, + new string[](1), + new string[](1) + ); // Register a new hook IStoreHook tableHook = new WorldTestTableHook(); @@ -627,8 +656,8 @@ contract WorldTest is Test, GasReporter { // Expect the hook to be notified when a record is written vm.expectEmit(true, true, true, true); - emit HookCalled(abi.encode(tableId, singletonKey, value, schema)); - world.setRecord(tableId, singletonKey, value, schema); + emit HookCalled(abi.encode(tableId, singletonKey, value, valueSchema)); + world.setRecord(tableId, singletonKey, value, valueSchema); // TODO: add tests for other hook methods (onBeforeSetField, onAfterSetField, onDeleteRecord) // (See https://github.com/latticexyz/mud/issues/444) @@ -661,7 +690,14 @@ contract WorldTest is Test, GasReporter { function testWriteRootSystem() public { // Register a new table - bytes32 tableId = world.registerTable("namespace", "testTable", Bool.getSchema(), defaultKeySchema); + bytes32 tableId = world.registerTable( + "namespace", + "testTable", + defaultKeySchema, + Bool.getValueSchema(), + new string[](1), + new string[](1) + ); // Register a new system WorldTestSystem system = new WorldTestSystem(); @@ -680,7 +716,14 @@ contract WorldTest is Test, GasReporter { function testWriteAutonomousSystem() public { // Register a new table - bytes32 tableId = world.registerTable("namespace", "testTable", Bool.getSchema(), defaultKeySchema); + bytes32 tableId = world.registerTable( + "namespace", + "testTable", + defaultKeySchema, + Bool.getValueSchema(), + new string[](1), + new string[](1) + ); // Register a new system WorldTestSystem system = new WorldTestSystem(); diff --git a/packages/world/test/WorldDynamicUpdate.t.sol b/packages/world/test/WorldDynamicUpdate.t.sol index 5e4f0cd4114..809d704923f 100644 --- a/packages/world/test/WorldDynamicUpdate.t.sol +++ b/packages/world/test/WorldDynamicUpdate.t.sol @@ -50,7 +50,7 @@ contract UpdateInFieldTest is Test, GasReporter { keyTuple = new bytes32[](1); keyTuple[0] = key; singletonKey = new bytes32[](0); - Schema schema = AddressArray.getSchema(); + Schema valueSchema = AddressArray.getValueSchema(); // Initialize the data in setUp so that slots aren't warm in tests (to test cold update) @@ -58,7 +58,7 @@ contract UpdateInFieldTest is Test, GasReporter { bytes16 name = "testTable"; // Register a new table - tableId = world.registerTable(namespace, name, schema, defaultKeySchema); + tableId = world.registerTable(namespace, name, defaultKeySchema, valueSchema, new string[](1), new string[](1)); // Create data initData = new address[](3); @@ -67,7 +67,7 @@ contract UpdateInFieldTest is Test, GasReporter { initData[2] = address(bytes20(keccak256("another address"))); encodedData = EncodeArray.encode(initData); - world.setField(namespace, name, keyTuple, 0, encodedData, schema); + world.setField(namespace, name, keyTuple, 0, encodedData, valueSchema); } // Expect an error when trying to write from an address that doesn't have access @@ -85,7 +85,7 @@ contract UpdateInFieldTest is Test, GasReporter { function testPopFromField() public { bytes16 namespace = "DynamicUpdTest"; bytes16 name = "testTable"; - Schema schema = AddressArray.getSchema(); + Schema valueSchema = AddressArray.getValueSchema(); // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), initData); @@ -94,7 +94,7 @@ contract UpdateInFieldTest is Test, GasReporter { uint256 byteLengthToPop = 20; startGasReport("pop 1 address (cold)"); - world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop, schema); + world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop, valueSchema); endGasReport(); // Expect the data to be updated @@ -108,7 +108,7 @@ contract UpdateInFieldTest is Test, GasReporter { byteLengthToPop = 20; startGasReport("pop 1 address (warm)"); - world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop, schema); + world.popFromField(namespace, name, keyTuple, 0, byteLengthToPop, valueSchema); endGasReport(); // Expect the data to be updated @@ -119,10 +119,10 @@ contract UpdateInFieldTest is Test, GasReporter { } // Reset data - world.setField(namespace, name, keyTuple, 0, encodedData, schema); + world.setField(namespace, name, keyTuple, 0, encodedData, valueSchema); // Pop 2 items via direct access byteLengthToPop = 20 * 2; - world.popFromField(tableId, keyTuple, 0, byteLengthToPop, schema); + world.popFromField(tableId, keyTuple, 0, byteLengthToPop, valueSchema); // Expect the data to be updated loadedData = AddressArray.get(world, tableId, key); assertEq(loadedData.length, initData.length - 2); @@ -132,21 +132,21 @@ contract UpdateInFieldTest is Test, GasReporter { // Expect an error when trying to write from an address that doesn't have access (via namespace/name) _expectAccessDenied(address(0x01), namespace, name); - world.popFromField(namespace, name, keyTuple, 0, 20, schema); + world.popFromField(namespace, name, keyTuple, 0, 20, valueSchema); // Expect an error when trying to write from an address that doesn't have access (via tableId) _expectAccessDenied(address(0x01), namespace, name); - world.popFromField(tableId, keyTuple, 0, 20, schema); + world.popFromField(tableId, keyTuple, 0, 20, valueSchema); // Expect the World to have access vm.prank(address(world)); - world.popFromField(namespace, name, keyTuple, 0, 20, schema); + world.popFromField(namespace, name, keyTuple, 0, 20, valueSchema); } function testUpdateInField() public { bytes16 namespace = "DynamicUpdTest"; bytes16 name = "testTable"; - Schema schema = AddressArray.getSchema(); + Schema valueSchema = AddressArray.getValueSchema(); // Expect the data to be written assertEq(AddressArray.get(world, tableId, key), initData); @@ -156,11 +156,11 @@ contract UpdateInFieldTest is Test, GasReporter { dataForUpdate[0] = address(bytes20(keccak256("address for update"))); startGasReport("updateInField 1 item (cold)"); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); endGasReport(); startGasReport("updateInField 1 item (warm)"); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); endGasReport(); // Expect the data to be updated @@ -168,7 +168,7 @@ contract UpdateInFieldTest is Test, GasReporter { assertEq(AddressArray.get(world, tableId, key), initData); // Update index 1 via direct access - world.updateInField(tableId, keyTuple, 0, 20 * 1, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(tableId, keyTuple, 0, 20 * 1, EncodeArray.encode(dataForUpdate), valueSchema); // Expect the data to be updated initData[1] = dataForUpdate[0]; @@ -176,14 +176,14 @@ contract UpdateInFieldTest is Test, GasReporter { // Expect an error when trying to write from an address that doesn't have access (via namespace/name) _expectAccessDenied(address(0x01), namespace, name); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); // Expect an error when trying to write from an address that doesn't have access (via tableId) _expectAccessDenied(address(0x01), namespace, name); - world.updateInField(tableId, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(tableId, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); // Expect the World to have access vm.prank(address(world)); - world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), schema); + world.updateInField(namespace, name, keyTuple, 0, 0, EncodeArray.encode(dataForUpdate), valueSchema); } } diff --git a/packages/world/test/query.t.sol b/packages/world/test/query.t.sol index 73913ee7ea5..6a40efae567 100644 --- a/packages/world/test/query.t.sol +++ b/packages/world/test/query.t.sol @@ -29,7 +29,7 @@ contract QueryTest is Test, GasReporter { bytes16 name2 = bytes16("source2"); bytes16 name3 = bytes16("source3"); - Schema tableSchema; + Schema tableValueSchema; Schema tableKeySchema; bytes32 table1 = ResourceSelector.from(namespace, name1); bytes32 table2 = ResourceSelector.from(namespace, name2); @@ -44,7 +44,7 @@ contract QueryTest is Test, GasReporter { QueryFragment[] fragmentsHasNot; function setUp() public { - tableSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); + tableValueSchema = SchemaEncodeHelper.encode(SchemaType.UINT256); tableKeySchema = SchemaEncodeHelper.encode(SchemaType.BYTES32); world = IBaseWorld(address(new World())); world.installRootModule(new CoreModule(), new bytes(0)); @@ -62,9 +62,9 @@ contract QueryTest is Test, GasReporter { function _installKeysInTableModule() internal { // Register source table - table1 = world.registerTable(namespace, name1, tableSchema, tableKeySchema); - table2 = world.registerTable(namespace, name2, tableSchema, tableKeySchema); - table3 = world.registerTable(namespace, name3, tableSchema, tableKeySchema); + table1 = world.registerTable(namespace, name1, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); + table2 = world.registerTable(namespace, name2, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); + table3 = world.registerTable(namespace, name3, tableKeySchema, tableValueSchema, new string[](1), new string[](1)); // Install the index module // TODO: add support for installing this via installModule @@ -86,9 +86,9 @@ contract QueryTest is Test, GasReporter { function testHasQuery() public { _installKeysInTableModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key1, abi.encode(0), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key1, abi.encode(0), tableValueSchema); // Query should return all keys in table1 QueryFragment[] memory fragments = new QueryFragment[](1); @@ -107,9 +107,9 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(2), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); // Query should return all keys in table1 with value 1 QueryFragment[] memory fragments = new QueryFragment[](1); fragments[0] = QueryFragment(QueryType.HasValue, table1, abi.encode(1)); @@ -125,12 +125,12 @@ contract QueryTest is Test, GasReporter { function testCombinedHasQuery() public { _installKeysInTableModule(); - world.setRecord(namespace, name1, key1, abi.encode(2), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name3, key1, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name3, key1, abi.encode(1), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -149,12 +149,12 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(2), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(2), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name3, key1, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name3, key1, abi.encode(1), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -172,13 +172,13 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key4, abi.encode(2), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key4, abi.encode(2), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -196,13 +196,13 @@ contract QueryTest is Test, GasReporter { function testCombinedHasNotQuery() public { _installKeysInTableModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key4, abi.encode(2), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key4, abi.encode(2), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -220,13 +220,13 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key4, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key4, abi.encode(1), tableValueSchema); // Query should return all entities that have table1 and table2 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -244,16 +244,16 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key1, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key2, abi.encode(2), tableSchema); - world.setRecord(namespace, name2, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name2, key4, abi.encode(1), tableSchema); - world.setRecord(namespace, name3, key2, abi.encode(1), tableSchema); - world.setRecord(namespace, name3, key3, abi.encode(1), tableSchema); - world.setRecord(namespace, name3, key4, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key1, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key2, abi.encode(2), tableValueSchema); + world.setRecord(namespace, name2, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name2, key4, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name3, key2, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name3, key3, abi.encode(1), tableValueSchema); + world.setRecord(namespace, name3, key4, abi.encode(1), tableValueSchema); // Query should return all entities that have table2 and not table1 QueryFragment[] memory fragments = new QueryFragment[](3); @@ -272,9 +272,9 @@ contract QueryTest is Test, GasReporter { _installKeysInTableModule(); _installKeysWithValueModule(); - world.setRecord(namespace, name1, key1, abi.encode(4), tableSchema); - world.setRecord(namespace, name1, key2, abi.encode(5), tableSchema); - world.setRecord(namespace, name1, key3, abi.encode(6), tableSchema); + world.setRecord(namespace, name1, key1, abi.encode(4), tableValueSchema); + world.setRecord(namespace, name1, key2, abi.encode(5), tableValueSchema); + world.setRecord(namespace, name1, key3, abi.encode(6), tableValueSchema); // Query should return all entities with table1 except value 6 QueryFragment[] memory fragments = new QueryFragment[](2); @@ -295,9 +295,9 @@ contract QueryTest is Test, GasReporter { for (uint256 i; i < 100; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); - world.setRecord(namespace, name1, key, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key, abi.encode(1), tableValueSchema); } - world.setRecord(namespace, name2, key1, abi.encode(0), tableSchema); + world.setRecord(namespace, name2, key1, abi.encode(0), tableValueSchema); // Query should return all keys in table1 QueryFragment[] memory fragments = new QueryFragment[](1); @@ -316,9 +316,9 @@ contract QueryTest is Test, GasReporter { for (uint256 i; i < 1000; i++) { bytes32[] memory key = new bytes32[](1); key[0] = bytes32(i); - world.setRecord(namespace, name1, key, abi.encode(1), tableSchema); + world.setRecord(namespace, name1, key, abi.encode(1), tableValueSchema); } - world.setRecord(namespace, name2, key1, abi.encode(0), tableSchema); + world.setRecord(namespace, name2, key1, abi.encode(0), tableValueSchema); // Query should return all keys in table1 QueryFragment[] memory fragments = new QueryFragment[](1); diff --git a/packages/world/test/tables/AddressArray.sol b/packages/world/test/tables/AddressArray.sol index 8b0009b8987..66cfe12243f 100644 --- a/packages/world/test/tables/AddressArray.sol +++ b/packages/world/test/tables/AddressArray.sol @@ -18,48 +18,42 @@ import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; library AddressArray { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { + /** Get the table's key schema */ + function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.ADDRESS_ARRAY; + _schema[0] = SchemaType.BYTES32; return SchemaLib.encode(_schema); } - function getKeySchema() internal pure returns (Schema) { + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BYTES32; + _schema[0] = SchemaType.ADDRESS_ARRAY; return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("AddressArray", _fieldNames); - } - - /** Register the table's schema */ - function registerSchema(bytes32 _tableId) internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](1); + keyNames[0] = "key"; } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store, bytes32 _tableId) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata(bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register(bytes32 _tableId) internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store, bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store, bytes32 _tableId) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ @@ -67,7 +61,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -76,7 +70,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_address()); } @@ -85,7 +79,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Set value (using the specified store) */ @@ -93,7 +87,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)), getValueSchema()); } /** Get the length of value */ @@ -101,7 +95,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); return _byteLength / 20; } @@ -110,7 +104,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema()); + uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getValueSchema()); return _byteLength / 20; } @@ -119,7 +113,14 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 20, (_index + 1) * 20); + bytes memory _blob = StoreSwitch.getFieldSlice( + _tableId, + _keyTuple, + 0, + getValueSchema(), + _index * 20, + (_index + 1) * 20 + ); return (address(Bytes.slice20(_blob, 0))); } @@ -128,7 +129,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 20, (_index + 1) * 20); + bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getValueSchema(), _index * 20, (_index + 1) * 20); return (address(Bytes.slice20(_blob, 0))); } @@ -137,7 +138,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Push an element to value (using the specified store) */ @@ -145,7 +146,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getSchema()); + _store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)), getValueSchema()); } /** Pop an element from value */ @@ -153,7 +154,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getSchema()); + StoreSwitch.popFromField(_tableId, _keyTuple, 0, 20, getValueSchema()); } /** Pop an element from value (using the specified store) */ @@ -161,7 +162,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.popFromField(_tableId, _keyTuple, 0, 20, getSchema()); + _store.popFromField(_tableId, _keyTuple, 0, 20, getValueSchema()); } /** Update an element of value at `_index` */ @@ -169,7 +170,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getSchema()); + StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getValueSchema()); } /** Update an element of value (using the specified store) at `_index` */ @@ -177,7 +178,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getSchema()); + _store.updateInField(_tableId, _keyTuple, 0, _index * 20, abi.encodePacked((_element)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -200,7 +201,7 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ @@ -208,6 +209,6 @@ library AddressArray { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = key; - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } } diff --git a/packages/world/test/tables/Bool.sol b/packages/world/test/tables/Bool.sol index 376ad851a35..efabcf7288f 100644 --- a/packages/world/test/tables/Bool.sol +++ b/packages/world/test/tables/Bool.sol @@ -18,54 +18,47 @@ import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol"; import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol"; library Bool { - /** Get the table's schema */ - function getSchema() internal pure returns (Schema) { - SchemaType[] memory _schema = new SchemaType[](1); - _schema[0] = SchemaType.BOOL; - - return SchemaLib.encode(_schema); - } - + /** Get the table's key schema */ function getKeySchema() internal pure returns (Schema) { SchemaType[] memory _schema = new SchemaType[](0); return SchemaLib.encode(_schema); } - /** Get the table's metadata */ - function getMetadata() internal pure returns (string memory, string[] memory) { - string[] memory _fieldNames = new string[](1); - _fieldNames[0] = "value"; - return ("Bool", _fieldNames); + /** Get the table's value schema */ + function getValueSchema() internal pure returns (Schema) { + SchemaType[] memory _schema = new SchemaType[](1); + _schema[0] = SchemaType.BOOL; + + return SchemaLib.encode(_schema); } - /** Register the table's schema */ - function registerSchema(bytes32 _tableId) internal { - StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's key names */ + function getKeyNames() internal pure returns (string[] memory keyNames) { + keyNames = new string[](0); } - /** Register the table's schema (using the specified store) */ - function registerSchema(IStore _store, bytes32 _tableId) internal { - _store.registerSchema(_tableId, getSchema(), getKeySchema()); + /** Get the table's field names */ + function getFieldNames() internal pure returns (string[] memory fieldNames) { + fieldNames = new string[](1); + fieldNames[0] = "value"; } - /** Set the table's metadata */ - function setMetadata(bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names */ + function register(bytes32 _tableId) internal { + StoreSwitch.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Set the table's metadata (using the specified store) */ - function setMetadata(IStore _store, bytes32 _tableId) internal { - (string memory _tableName, string[] memory _fieldNames) = getMetadata(); - _store.setMetadata(_tableId, _tableName, _fieldNames); + /** Register the table's key schema, value schema, key names and value names (using the specified store) */ + function register(IStore _store, bytes32 _tableId) internal { + _store.registerTable(_tableId, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } /** Get value */ function get(bytes32 _tableId) internal view returns (bool value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -73,7 +66,7 @@ library Bool { function get(IStore _store, bytes32 _tableId) internal view returns (bool value) { bytes32[] memory _keyTuple = new bytes32[](0); - bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getSchema()); + bytes memory _blob = _store.getField(_tableId, _keyTuple, 0, getValueSchema()); return (_toBool(uint8(Bytes.slice1(_blob, 0)))); } @@ -81,14 +74,14 @@ library Bool { function set(bytes32 _tableId, bool value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + StoreSwitch.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Set value (using the specified store) */ function set(IStore _store, bytes32 _tableId, bool value) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getSchema()); + _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), getValueSchema()); } /** Tightly pack full data using this table's schema */ @@ -105,14 +98,14 @@ library Bool { function deleteRecord(bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); - StoreSwitch.deleteRecord(_tableId, _keyTuple, getSchema()); + StoreSwitch.deleteRecord(_tableId, _keyTuple, getValueSchema()); } /* Delete all data for given keys (using the specified store) */ function deleteRecord(IStore _store, bytes32 _tableId) internal { bytes32[] memory _keyTuple = new bytes32[](0); - _store.deleteRecord(_tableId, _keyTuple, getSchema()); + _store.deleteRecord(_tableId, _keyTuple, getValueSchema()); } }