-
Notifications
You must be signed in to change notification settings - Fork 189
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
feat(world, store): stop loading schema from storage, require schema as an argument #1174
Conversation
🦋 Changeset detectedLatest commit: b087930 The changes in this PR will be included in the next version bump. This PR includes changesets to release 26 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
We should probably wait for v2 networking to be merged and v1 networking to be removed to avoid double work in integrating this in the old stack. |
2bc7cc3
to
98b9586
Compare
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
98b9586
to
f433947
Compare
f433947
to
233c35c
Compare
233c35c
to
d3a7d26
Compare
@@ -261,13 +261,13 @@ | |||
"file": "test/Mixed.t.sol", | |||
"test": "testSetAndGet", | |||
"name": "set record in Mixed", | |||
"gasUsed": 110021 | |||
"gasUsed": 111113 |
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.
very interesting that the gas cost increased for this one (while decreasing for the other tests). @dk1a do you have an intuition around why?
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.
pretty sure it's cause registerSchema
is called in that test (before gas report), which warms the slot, so there's no gas reduction, but there is a new call to getSchema
which is very suboptimal (should be better after #1252 and SchemaType->staticLength)
c628440
to
f862270
Compare
f862270
to
5908a3d
Compare
2c68257
to
b806818
Compare
5908a3d
to
20e5702
Compare
return mapObject(testData, (records, table) => { | ||
if (!records) return undefined; | ||
const keyConfig = config.tables[table].keySchema; | ||
return records.map((record) => { | ||
const encodedKey = Object.entries(record.key).map(([keyName, keyValue]) => { | ||
const keyType = keyConfig[keyName as keyof typeof keyConfig] as StaticAbiType; | ||
return encodeAbiParameters([{ type: keyType }], [keyValue]); | ||
}); | ||
|
||
const encodedValue = encodePacked(Object.values(config.tables[table].schema), Object.values(record.value)); | ||
|
||
const encodedValueSchema = schemaToHex(abiTypesToSchema(Object.values(config.tables[table].schema))); | ||
|
||
return { | ||
key: encodedKey, | ||
value: encodedValue, | ||
valueSchema: encodedValueSchema, | ||
}; | ||
}); | ||
}) as EncodedData<typeof testData>; |
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.
this change is fixing a type issue, adding the valueSchema
to the encoded data and slightly refactoring because the nested functions became hard to read
packages/cli/src/commands/trace.ts
Outdated
const address = await WorldContract.getField( | ||
systemsTableId.toHex(), | ||
[systemSelector.toHex()], | ||
0, | ||
systemTableSchema | ||
); |
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.
getField
now requires the value schema to be passed in
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.
another approach that might be a little less error prone if we change this schema later and forget to update this is to fetch the schema from the chain before we get field
packages/store/src/StoreCore.sol
Outdated
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.
This file contains the most significant changes (reading the schema from args instead of storage and renaming schema
to valueSchema
). The interface changes are then propagated through all the interfaces (IStore
, World
, etc)
packages/store/ts/codegen/field.ts
Outdated
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.
passing the schema in all methods interacting with the store
This looks like a massive PR but it's mostly small refactors (passing the schema as arg to all methods interacting with the store). Added some PR comments to point out the relevant areas to make reviewing a bit easier. |
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.
Looks great! Mostly just a question about where we want to put the schema arg.
bytes32 table, | ||
bytes32[] calldata key, | ||
bytes calldata data, | ||
+ Schema valueSchema |
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.
Does the argument ordering here make sense? I usually order args by specificity and would kind of expect valueSchema
to be further ahead in the list of arguments (before or after table
perhaps).
Unless it's an optional arg, then it being at the end makes a bit more sense.
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 agree with ordering by specificity but would defer this refactor until after this current stack of PRs is merged since they touch lots of the same places in the code and changing it here would likely lead to massive merge conflicts in the upstream branches
@@ -1,5 +1,5 @@ | |||
{ | |||
"31337": { | |||
"address": "0x5FbDB2315678afecb367f032d93F642f64180aa3" | |||
"address": "0xA7c59f010700930003b33aB25a7a0679C860f29c" |
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.
wonder if this will muck with e2e tests or if this will get overwritten in CI after deploy
e2e/pnpm-lock.yaml
Outdated
settings: | ||
autoInstallPeers: true | ||
excludeLinksFromLockfile: false | ||
|
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.
8.6.8
packages/cli/src/commands/trace.ts
Outdated
const address = await WorldContract.getField( | ||
systemsTableId.toHex(), | ||
[systemSelector.toHex()], | ||
0, | ||
systemTableSchema | ||
); |
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.
another approach that might be a little less error prone if we change this schema later and forget to update this is to fetch the schema from the chain before we get field
@@ -90,7 +90,7 @@ library KeyEncoding { | |||
_keyTuple[4] = _boolToBytes32(k5); | |||
_keyTuple[5] = bytes32(uint256(uint8(k6))); | |||
|
|||
bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0); | |||
bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0, getSchema()); |
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.
not blocking, just thinking aloud: I don't know the gas implications but I wonder if it would make sense to provide an alternative code path for these cases where we can generate a byte offset rather than a schema index + schema definition.
We could potentially do this for getters even if we don't commit to the bytes offset events changes.
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.
yeah this seems kinda related to #1222 and SchemaType removal, as we are moving away from validation in StoreCore and towards dealing with byte blobs
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.
yeah could add this, but would defer the decision until after #1222
packages/cli/src/commands/trace.ts
Outdated
@@ -11,8 +11,10 @@ import { resolveWorldConfig, WorldConfig } from "@latticexyz/world"; | |||
import { IBaseWorld } from "@latticexyz/world/types/ethers-contracts/IBaseWorld"; | |||
import IBaseWorldData from "@latticexyz/world/abi/IBaseWorld.sol/IBaseWorld.json" assert { type: "json" }; | |||
import { getChainId, getExistingContracts } from "../utils"; | |||
import { schemaToHex } from "@latticexyz/protocol-parser"; |
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 this be before the local import?
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.
one day I'll add a linter/autofixer for this: #1277
Fixes #1131
Breaking change: All of Store's read and write methods now require the table's value schema to be passed in instead of reading it from storage