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

feat(protocol-parser): add keySchema/valueSchema helpers #1443

Merged
merged 6 commits into from
Sep 12, 2023

Conversation

holic
Copy link
Member

@holic holic commented Sep 11, 2023

related to #1296

pulled out of #1354

@changeset-bot
Copy link

changeset-bot bot commented Sep 11, 2023

🦋 Changeset detected

Latest commit: f2be76b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 28 packages
Name Type
@latticexyz/store Major
@latticexyz/protocol-parser Major
@latticexyz/cli Major
@latticexyz/dev-tools Major
@latticexyz/react Major
@latticexyz/store-indexer Major
@latticexyz/store-sync Major
@latticexyz/world Major
@latticexyz/abi-ts Major
@latticexyz/block-logs-stream Major
@latticexyz/common Major
@latticexyz/config Major
create-mud Major
@latticexyz/ecs-browser Major
@latticexyz/gas-report Major
@latticexyz/network Major
@latticexyz/noise Major
@latticexyz/phaserx Major
@latticexyz/recs Major
@latticexyz/schema-type Major
@latticexyz/services Major
@latticexyz/solecs Major
solhint-config-mud Major
solhint-plugin-mud Major
@latticexyz/std-client Major
@latticexyz/std-contracts Major
@latticexyz/store-cache Major
@latticexyz/utils Major

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

Copy link
Member

@alvrs alvrs left a comment

Choose a reason for hiding this comment

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

Wondering if we should already remove / fully refactor all the now deprecated functions (decodeRecord, decodeKeyTuple, etc) or if it's too big of a change for this PR?

keySchema: TSchema,
data: readonly Hex[]
): SchemaToPrimitives<TSchema> {
// TODO: refactor and move all decodeKeyTuple logic into this method so we can delete decodeKeyTuple
Copy link
Member

Choose a reason for hiding this comment

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

is decodeKeyTuple still used somewhere else? If not, should we just move the logic in there in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

it's used by blockLogsToStorage which is getting refactored in the other PR

didn't wanna refactor everything quite yet to avoid a big rebase of the other PR, just wanted to add new methods and deprecate old ones for an easy first pass

Copy link
Member Author

Choose a reason for hiding this comment

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

updated the original issue to remind me to refactor this: #1296


it("can decode an out of bounds array", () => {
const schema = { staticFields: [], dynamicFields: ["uint32[]"] } as const;
const values = decodeRecord(schema, "0x0000000000000000000000000000000000000000000000000400000000000004");
Copy link
Member

Choose a reason for hiding this comment

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

The hex represents an encoded dynamic length field with total length 4 and length of the first array of 4, correct? Can you add more context for why the decoded array has one element with value 0? Somehow I would have expected either an empty because there is no data after the encoded data length, or an array with 4 elements that are all 0?

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 think I had added this test for proof that padSliceHex was doing the right thing internally when used within decodeRecord. I can't recall where exactly I was getting a the hex value but I copied it from some tests or logs that were failing without padSliceHex.

To explain what's going on:

The total length and first array length is the byte length (4 bytes in the case of uint32). Meaning that there's one item/value for the uint32[] field.

The record hex is "trimmed" to the right-most byte (end of the counter). On chain, any bytes after the right-most byte would be treated as zeros, so we want to have the same behavior in the client.

So if we were to read the same value on chain, we should get a value of [0] for this record (one uint32 value with unset bytes, i.e. 0). This test ensures that the same behavior happens in the client.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh makes sense, had forgotten that the length corresponds to bytes and assumed elements. Thanks for the explanation!

value.map(() => staticFieldType),
value
);
// TODO: we can remove conditional once this is fixed: https://github.com/wagmi-dev/viem/pull/1147
Copy link
Member

Choose a reason for hiding this comment

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

seems like it got merged already!

Copy link
Member Author

Choose a reason for hiding this comment

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

yep! we'll need to bump viem to get this change so gonna leave this here for now

packages/protocol-parser/src/encodeKey.ts Outdated Show resolved Hide resolved
@@ -0,0 +1,8 @@
import { Hex } from "viem";

export function padSliceHex(data: Hex, start: number, end?: number): Hex {
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 add a small typedoc comment with something like "slices the hex string while padding data out of bound data with 0; including start index, excluding end index"?

Copy link
Member Author

@holic holic Sep 11, 2023

Choose a reason for hiding this comment

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

done! also renamed to readHex because I didn't think padSliceHex was clear enough (felt more like it was named after the implementation rather than the intent)

@holic holic merged commit 5e71e1c into main Sep 12, 2023
@holic holic deleted the holic/protocol-parser-helpers branch September 12, 2023 09:19
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