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(world-modules): add puppet module #1793

Merged
merged 13 commits into from
Nov 1, 2023
Merged

feat(world-modules): add puppet module #1793

merged 13 commits into from
Nov 1, 2023

Conversation

alvrs
Copy link
Member

@alvrs alvrs commented Oct 18, 2023

No description provided.

@changeset-bot
Copy link

changeset-bot bot commented Oct 18, 2023

🦋 Changeset detected

Latest commit: 4af069d

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

This PR includes changesets to release 30 packages
Name Type
@latticexyz/world-modules Major
@latticexyz/cli Major
@latticexyz/abi-ts Major
@latticexyz/block-logs-stream Major
@latticexyz/common Major
@latticexyz/config Major
create-mud Major
@latticexyz/dev-tools Major
@latticexyz/ecs-browser Major
@latticexyz/faucet Major
@latticexyz/gas-report Major
@latticexyz/network Major
@latticexyz/noise Major
@latticexyz/phaserx Major
@latticexyz/protocol-parser Major
@latticexyz/react 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/store-indexer Major
@latticexyz/store-sync Major
@latticexyz/store Major
@latticexyz/utils Major
@latticexyz/world 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

bytes32 eventSignature,
bytes32 topic1,
bytes32 topic2,
bytes32 topic3,
Copy link
Member

Choose a reason for hiding this comment

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

are these essentially the indexed args?

Copy link
Member Author

@alvrs alvrs Oct 18, 2023

Choose a reason for hiding this comment

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

yes exactly


```solidity
import { PuppetModule } from "@latticexyz/world-modules/src/modules/puppet/PuppetModule.sol";
import { createPuppet } from "@latticexyz/world-modules/src/modules/puppet/createPuppet.sol";
Copy link
Member

Choose a reason for hiding this comment

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

Although the puppet terminology is fun, I wonder if we should just call this a proxy 🙈

Copy link
Member Author

Choose a reason for hiding this comment

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

Thought about that too but I'm afraid proxy has too many preconceptions around the proxy being the authority (ie proxy holds the state, proxy switches its implementation, etc), whereas here the "proxy" is just the "puppet" of the World

Copy link
Member

Choose a reason for hiding this comment

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

true! proxy is quite overloaded

holic
holic previously approved these changes Oct 31, 2023
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 good! Only real concern is what seems to be a 1:1 mapping between puppets and systems. Wondering about one to many, but also don't need to explode the scope of this right now.

import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";
import { Systems } from "@latticexyz/world/src/codegen/tables/Systems.sol";

contract Puppet {
Copy link
Member

Choose a reason for hiding this comment

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

pretty great to see how lean this is!

contract PuppetMaster {
error PuppetMaster_NoPuppet(address systemAddress, ResourceId systemId);

function puppet() internal view returns (Puppet) {
Copy link
Member

Choose a reason for hiding this comment

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

does this mean there's only a 1:1 mapping between a system and a puppet?

if I want multiple tokens (e.g. ERC721) on my world, how would I go about that?

Copy link
Member Author

Choose a reason for hiding this comment

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

does this mean there's only a 1:1 mapping between a system and a puppet

For now yes. Agree 1:N would be nicer but makes it much more complex, so I'd think about that later (either as a change to the puppet module, or with a new module)

if I want multiple tokens (e.g. ERC721) on my world, how would I go about that?

You'll have to register different systems (see #1844). Thought a lot about this too, but I feel like this approach is the simplest (one namespace per token, with a separate system and tables in that namespace). Otherwise everything gets more complicated (if it's just one system you'd need to pass a param to the system, which means the interface doesn't match anymore with the puppet, access management becomes more complex, etc)

*/
function createPuppet(IBaseWorld world, ResourceId systemId) returns (address puppet) {
puppet = abi.decode(
world.call(PUPPET_FACTORY, abi.encodeCall(PuppetFactorySystem.createPuppet, (systemId))),
Copy link
Member

Choose a reason for hiding this comment

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

ooc why don't we use SystemSwitch here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree should probably use system switch here

Copy link
Member Author

Choose a reason for hiding this comment

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

update: turns out using system switch here pushes ERC20Module over the bytecode limit, so reverting for now (meaning you can't register a puppet from a root system for now). We should find a better implementation for SystemSwitch that doesn't require inlining the libraries, maybe by deploying StoreCore as a public library.

Copy link
Member

@holic holic Nov 1, 2023

Choose a reason for hiding this comment

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

we should prob add a follow up issue to come back to 1) this specific instance and 2) the problem overall

StoreCore as a public lib seems bad for gas because it'd be a delegatecall to an external contract right? And iirc foundry doesn't have good support for linked libs

or do you mean public as in not-internal?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah i mean public as in not-internal, which means delegatecall to an external contract

.changeset/happy-pants-try.md Outdated Show resolved Hide resolved
dk1a
dk1a previously approved these changes Nov 1, 2023
Copy link
Contributor

@dk1a dk1a left a comment

Choose a reason for hiding this comment

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

Just some formatting nits

packages/world-modules/src/modules/puppet/PuppetModule.sol Outdated Show resolved Hide resolved
* This library is functionally equivalent with the AccessControl library from world,
* but uses StoreSwitch instead of always reading from own storage.
*/
library AccessControlLib {
Copy link
Contributor

Choose a reason for hiding this comment

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

Though I understand why it's in world-modules, would it maybe be better to have a single AccessControl lib with hasAccess/_hasAccess etc, to have all the logic in 1 place and let it mirror the table methods?

Copy link
Member Author

Choose a reason for hiding this comment

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

agree! unfortunately can't touch the code in world while it's being audited but should note this down for later

Copy link
Member Author

Choose a reason for hiding this comment

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

holic
holic previously approved these changes Nov 1, 2023
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.

agree with @dk1a's comment around centralizing AccessControlLib but otherwise looks great

function puppet() internal view returns (Puppet) {
ResourceId systemId = SystemRegistry.getSystemId(address(this));
address puppetAddress = PuppetRegistry.get(PUPPET_TABLE_ID, systemId);
if (puppetAddress == address(0)) revert PuppetMaster_NoPuppet(address(this), systemId);
Copy link
Member

Choose a reason for hiding this comment

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

does address(this) still work in the context of a delegatecall?

Copy link
Member Author

@alvrs alvrs Nov 1, 2023

Choose a reason for hiding this comment

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

There could only be a single puppet for root systems (because they all have the world address). There would also have to be a namespace delegation in the root namespace, which feels like a bad idea. So all in all i'd say you shouldn't use puppets with root systems.

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.

3 participants