Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: uint256 tableId -> bytes32 tableId #613

Merged
merged 8 commits into from
Apr 12, 2023
Merged

Conversation

alvrs
Copy link
Member

@alvrs alvrs commented Apr 12, 2023

  • We originally used bytes32 for tableIds, but switched to uint256 in refactor(store): change tableId type from bytes32 to uint256 #416 because bytes32 had a type overlap with string, which caused issues in the first version of the World contract (where we used strings for routes instead of the current namespace/name approach, see feat(world): simplify access control to namespaces instead of routes #467 for context on this change).
  • Now that we don't use strings for routes in the World anymore, there is no reason for tableId to be uint256 anymore, while resourceSelector in World is bytes32. A resourceSelector is a superset of a tableId (also including systems), and converting from bytes32 to uint256 whenever we use a resourceSelector as a tableId is unnecessary overhead.

@alvrs alvrs marked this pull request as ready for review April 12, 2023 14:44
@alvrs alvrs requested a review from a user April 12, 2023 14:44
@alvrs alvrs requested a review from holic as a code owner April 12, 2023 14:44
@@ -255,7 +255,7 @@ type StorecoreStoreDeleteRecord struct {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @authcall - I edited this file manually when changing all the references of uint256 table to bytes32 table, but the correct approach would be to regenerate this file. What's the script to do so?

@@ -8,7 +8,7 @@ export default storeConfig({
Callbacks: "bytes24[]",
StoreMetadata: {
primaryKeys: {
tableId: "uint256",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @authcall / @holic: does any networking code on MODE/client hardcode the schema of the StoreMetadata table or is it inferred dynamically?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in client, StoreMetadata is inferred since its a proper table

"tablegen": "../cli/dist/mud.js tablegen",
"test": "yarn codegen && forge test",
"build": "yarn codegen && rimraf out && forge build && yarn dist && yarn types && tsup",
"build": "rimraf out && forge build && yarn dist && yarn types && tsup",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using tablegen here caused an error in the CI, because ../cli/dist/mud.js does not exist yet while store is being built (CLI can't be built first because it depends on World's ABI, which depends on Store). There are (at least) two approaches to solve this:

  1. Move the tablegen scripts into Store (and worldgen scripts into World), so these packages can use them locally (and consumers of these packages don't have to install the MUD CLI if they only want to use eg Store without the rest of the framework). CLI can still expose a tablegen command by importing the scripts from Store (and a worldgen command by importing from World)
  2. Check in the World/Store ABIs, so CLI can be built independently from building World/Store (this alone doesn't fix the circular dependency though)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this has come up a bunch, mind opening an issue for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 35 to 41
// assumes tableId is a 32-byte hex string, otherwise it left-pads with zeros (for numbers)
// this is scary, since zero padding is different depending on the type (bytes types vs number types)
// TODO: fix this after we move tableIds to bytes32 instead of uint256
// TODO: check if this is still relevant
const tableIdBytes = new Uint8Array(32);
tableIdBytes.set(tableId.slice().reverse());
tableIdBytes.reverse();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@holic can you help me figure out how to change this logic to make it work with tableIds that are bytes32? Do we assume tableId to be uint256 anywhere else in the client code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just change this to

const tableIdBytes = new Uint8Array(32);
tableIdBytes.set(tableId);

which ensures right-zero padding. But if ethers is already passing bytes32 values as zero padded hex, then we won't even need this.

(before it would reverse, then right-pad, then reverse again, so that the result is left-padded)

We could also use the viem pad helpers for this to make it more clear what's going on.

@@ -45,7 +46,7 @@ export function registerSchema(world: Contract, table: TableId, rawSchema?: stri
// TODO: populate from ECS cache before fetching from RPC

console.log("fetching schema for table", { table: table.toString(), world: world.address });
const schema = world.getSchema(table).then((rawSchema: string) => {
const schema = (world as IStore).getSchema(table.toHexString()).then((rawSchema: string) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we make the world argument an IStore here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that first but it requires to change it all the way upstream too, so I waited for now bc it's mostly unrelated to this PR. Just thought having type hints where we call the World methods is useful

@alvrs alvrs force-pushed the alvrs/renamefileselector branch from 81edeee to ced5245 Compare April 12, 2023 18:40
@alvrs alvrs force-pushed the alvrs/tableidbytes32 branch from 17a70f1 to 3f5bd43 Compare April 12, 2023 18:44
Base automatically changed from alvrs/renamefileselector to main April 12, 2023 19:25
@holic holic merged commit 26195b8 into main Apr 12, 2023
@holic holic deleted the alvrs/tableidbytes32 branch April 12, 2023 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants