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(store,world): more granularity for onchain hooks #1399

Merged
merged 18 commits into from
Sep 7, 2023
Merged

Conversation

alvrs
Copy link
Member

@alvrs alvrs commented Sep 5, 2023

Fixes #1159 (that issue also includes more context on this approach vs different approaches)

TODOs

  • Split onSetRecord into onBeforeSetRecord and onAfterSetRecord
  • Split onDeleteRecord into onBeforeDeleteRecord and onAfterDeleteRecord
  • Add a bitmap to the store hooks and system hooks: One bit for each operation, so we can skip unnecessary calls.
  • Rename EnableHooks to EnableStoreHooks and each element from beforeX to onBeforeX etc

TODOs in follow ups

  • Add ability to remove hooks (by filtering the array). Since it's possible to upgrade systems now it seems useful to be able to remove hooks and register new ones.
  • Consider renaming all mentions of "Hook" to "Hooks" (since now that individual functions can be enabled and disabled that makes more sense)

TODOs depending on #1354

  • Remove onBeforeSetField and onAfterSetField
  • Add onBeforeSpliceRecord and onAfterSpliceRecord

@changeset-bot
Copy link

changeset-bot bot commented Sep 5, 2023

🦋 Changeset detected

Latest commit: 5464530

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/world Major
@latticexyz/cli Major
@latticexyz/dev-tools Major
@latticexyz/react Major
@latticexyz/store-indexer Major
@latticexyz/store-sync 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/protocol-parser 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

pragma solidity >=0.8.0;

// 20 bytes address, 1 byte bitmap of enabled hooks
type Hook is bytes21;
Copy link
Member

Choose a reason for hiding this comment

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

SmYd

*/
function encode(address hookAddress, uint8 encodedHooks) internal pure returns (Hook) {
// Move the address to the leftmost 20 bytes and the bitmap to the rightmost byte
return Hook.wrap(bytes21(bytes20(hookAddress)) | bytes21(uint168(encodedHooks)));
Copy link
Member

Choose a reason for hiding this comment

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

ooc is this more gas efficient than the 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.

I think it shouldn't make a difference, wdyt @dk1a? Could try it out (either here or in a follow up gas optimization pass)

onAfterSetField: false,
onBeforeDeleteRecord: true,
onAfterDeleteRecord: false
})
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this would also work

EnabledStoreHooks({
  onBeforeSetRecord: true,
  onBeforeSetField: true,
  onBeforeDeleteRecord: true
})

(only toggling on the ones we want and letting the others default, but not sure if solc will complain)

Copy link
Member

Choose a reason for hiding this comment

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

I also wonder if we should just use a unit8 here instead of the struct so that the ABI (and presumably abi-encoded calldata) is a bit more compact vs (bool,bool,bool,bool,...)

Copy link
Member Author

Choose a reason for hiding this comment

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

unfortunately solc does complain :(

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 also wonder if we should just use a unit8 here

yeah i think you're right here - we just have to document how to use the helper libraries to encode the bitmap, but it would be much cheaper to encode it in eg. the module the hook is registered and only pass the uint8 between contracts.

holic
holic previously approved these changes Sep 6, 2023
@alvrs alvrs merged commit c4d5eb4 into main Sep 7, 2023
@alvrs alvrs deleted the alvrs/hooks branch September 7, 2023 17:32
@github-actions github-actions bot mentioned this pull request Sep 7, 2023
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.

Consider registering individual hook methods (instead of all-or-nothing)
2 participants