-
Notifications
You must be signed in to change notification settings - Fork 194
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
Conversation
@@ -255,7 +255,7 @@ type StorecoreStoreDeleteRecord struct { | |||
|
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
packages/store/package.json
Outdated
"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", |
There was a problem hiding this comment.
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:
- Move the
tablegen
scripts intoStore
(andworldgen
scripts intoWorld
), 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 egStore
without the rest of the framework). CLI can still expose atablegen
command by importing the scripts fromStore
(and aworldgen
command by importing fromWorld
) - 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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
packages/utils/src/v2/TableId.ts
Outdated
// 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(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch!
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
81edeee
to
ced5245
Compare
17a70f1
to
3f5bd43
Compare
bytes32
for tableIds, but switched touint256
in refactor(store): change tableId type from bytes32 to uint256 #416 becausebytes32
had a type overlap withstring
, which caused issues in the first version of theWorld
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).tableId
to beuint256
anymore, whileresourceSelector
inWorld
isbytes32
. AresourceSelector
is a superset of atableId
(also including systems), and converting frombytes32
touint256
whenever we use aresourceSelector
as atableId
is unnecessary overhead.