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(store,world): replace isStore with storeAddress #1061

Merged
merged 27 commits into from
Jul 18, 2023

Conversation

dk1a
Copy link
Contributor

@dk1a dk1a commented Jun 20, 2023

fixes #940
the previous approach used the absence of isStore to indicate that the contract isn't the Store
I change it so all store consumers need storeAddress method, and its absence is an error use a storage variable to detect storeAddress
Moved MudV2Test (otherwise circular dependency) and took the opportunity to rename it to MudTest

@changeset-bot
Copy link

changeset-bot bot commented Jun 20, 2023

🦋 Changeset detected

Latest commit: fe49822

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

This PR includes changesets to release 26 packages
Name Type
@latticexyz/std-contracts Major
@latticexyz/store Major
@latticexyz/world Major
@latticexyz/cli Major
@latticexyz/network Major
@latticexyz/react Major
@latticexyz/std-client Major
@latticexyz/store-cache Major
@latticexyz/store-sync Major
@latticexyz/dev-tools Major
@latticexyz/ecs-browser Major
@latticexyz/block-logs-stream Major
@latticexyz/common Major
@latticexyz/config Major
create-mud Major
@latticexyz/gas-report 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/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

@holic holic left a comment

Choose a reason for hiding this comment

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

looks like this upped the gas everywhere?

packages/store/src/StoreSwitch.sol Outdated Show resolved Hide resolved
@dk1a
Copy link
Contributor Author

dk1a commented Jun 20, 2023

looks like this upped the gas everywhere?

pretty much. It increases StoreSwitch calls by ~300 gas (which means any table interactions); relatively it's not big, but affects everything
I don't try to optimize it here, that'd be a separate PR. I'd want to compare using a method to using storage, and maybe rely on a magic return value instead of passing msg.sender arg.
But the best way to do this would probably be some combo of code data location and transient storage, neither of which are out yet

@holic
Copy link
Member

holic commented Jun 20, 2023

is this to help us with the "what context am I running in" issue where tests don't work well with the previous approach?

@dk1a
Copy link
Contributor Author

dk1a commented Jun 21, 2023

is this to help us with the "what context am I running in" issue where tests don't work well with the previous approach?

yep

@@ -1,11 +1,12 @@
// SPDX-License-Identifier: MIT
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think Consumer is clearer than Context (which makes me think it's something World inherits, not systems), and it matches StoreConsumer

Copy link
Member

Choose a reason for hiding this comment

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

Context was leaning on ERC2771Context where this pattern originates

Copy link
Member

Choose a reason for hiding this comment

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

I feel like Context still makes sense for this?

Copy link
Member

Choose a reason for hiding this comment

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

yeah i see how Consumer would also make sense but I'd prefer to keep Context to draw the analogy to ERC2771Context

// Check whether this contract implements the IStore interface
try IStore(address(this)).isStore() {
success = true;
} catch {
success = false;
try IStoreConsumer(address(this)).storeAddress(msg.sender) returns (address _storeAddress) {
return _storeAddress;
} catch (bytes memory lowLevelData) {
// catch and rename the error, otherwise it's usually "EvmError: Revert" which is very unhelpful
revert StoreSwitch_MissingOrInvalidStoreAddressFunction(lowLevelData);
Copy link
Contributor Author

@dk1a dk1a Jun 21, 2023

Choose a reason for hiding this comment

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

an alternative to enforcing IStoreConsumer would be to fall back on msg.sender if it's absent. That might help with gas too.
But I like this explicit approach since it gives a nice error instead of confusing behavior down the line, making things hopefully easy to debug (or answer questions about on mud-help)
I was thinking of exploring the alternative in a followup though, along with other gas optimizations

@dk1a dk1a marked this pull request as ready for review June 21, 2023 16:18
@dk1a dk1a requested a review from alvrs as a code owner June 21, 2023 16:18
* Default Store consumers consider their caller to be the Store, thus delegating access control to the Store.
* The Store contract considers itself to be the Store (so StoreRead has a different storeAddress definition).
* Tests/scripts may override this value for convenience.
* WARNING: World Systems rely on storeAddress being msg.sender for access control!
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if we should put this warning here - the Store package should be independent of the World package, and the warning itself is not detailed enough to be helpful without the full context. Maybe we should a more detailed explanation in the docs and link it somewhere in the world package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added a comment to WorldConsumer instead
also agree with docs, should it be in this PR or separate?

@dk1a
Copy link
Contributor Author

dk1a commented Jul 17, 2023

storage slot is mostly better (see #1158), rewrote this PR to use storage instead of a method

@dk1a dk1a requested a review from alvrs July 17, 2023 14:30
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.

This is great! Just two minor proposed changes for comments. Also let's add a changeset to describe the change.

packages/store/src/StoreSwitch.sol Outdated Show resolved Hide resolved
packages/store/src/StoreCore.sol Outdated Show resolved Hide resolved
@dk1a dk1a merged commit a7b30c7 into main Jul 18, 2023
@dk1a dk1a deleted the dk1a/refactor-is-store branch July 18, 2023 10:27
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.

Allow tables to be used in tests without providing explicit store argument
3 participants