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(cli,world): add user defined salt in WorldFactory.deployWorld() #2219

Merged
merged 23 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions packages/world/src/IWorldFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@ interface IWorldFactory {
*/
event WorldDeployed(address indexed newContract);

/**
* @notice Returns the total count of deployed World contracts per account.
* @param account The account.
* @return The total number of World contracts deployed by this factory per account.
*/
function worldCounts(address account) external view returns (uint256);

/**
* @notice Deploys a new World contract.
* @dev The deployment of the World contract will result in the `WorldDeployed` event being emitted.
* @return worldAddress The address of the newly deployed World contract.
*/
function deployWorld() external returns (address worldAddress);
function deployWorld(bytes memory _salt) external returns (address worldAddress);
}
9 changes: 4 additions & 5 deletions packages/world/src/WorldFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ contract WorldFactory is IWorldFactory {
/// @notice Address of the init module to be set in the World instances.
IModule public immutable initModule;

/// @notice Counters to keep track of the number of World instances deployed per address.
mapping(address creator => uint256 worldCount) public worldCounts;

/// @param _initModule The address of the init module.
constructor(IModule _initModule) {
initModule = _initModule;
Expand All @@ -28,12 +25,14 @@ contract WorldFactory is IWorldFactory {
/**
* @notice Deploys a new World instance, installs the InitModule and transfers ownership to the caller.
* @dev Uses the Create2 for deterministic deployment.
* @param _salt User defined salt for deterministic world addresses across chains
* @return worldAddress The address of the newly deployed World contract.
*/
function deployWorld() public returns (address worldAddress) {
function deployWorld(bytes memory _salt) public returns (address worldAddress) {
Copy link
Member

Choose a reason for hiding this comment

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

small API thing: because Solidity supports calling functions with named parameters now, it might make sense to call this salt and internally we can call the combined one _salt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

// Deploy a new World and increase the WorldCount
bytes memory bytecode = type(World).creationCode;
uint256 salt = uint256(keccak256(abi.encode(msg.sender, worldCounts[msg.sender]++)));
uint256 salt = uint256(keccak256(abi.encode(msg.sender, _salt)));

worldAddress = Create2.deploy(bytecode, salt);
IBaseWorld world = IBaseWorld(worldAddress);

Expand Down
40 changes: 21 additions & 19 deletions packages/world/test/Factories.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@ contract FactoriesTest is Test, GasReporter {
startGasReport("deploy contract via Create2");
create2Factory.deployContract(combinedBytes, uint256(0));
endGasReport();

// Confirm worldFactory was deployed correctly
IWorldFactory worldFactory = IWorldFactory(calculatedAddress);
assertEq(uint256(worldFactory.worldCounts(address(0))), uint256(0));
}

function testWorldFactory(address account) public {
function testWorldFactory(address account, uint256 salt1, uint256 salt2) public {
vm.assume(salt1 != salt2);
vm.startPrank(account);

// Deploy WorldFactory with current InitModule
InitModule initModule = createInitModule();
address worldFactoryAddress = address(new WorldFactory(initModule));
IWorldFactory worldFactory = IWorldFactory(worldFactoryAddress);

// Address we expect for World
// User defined bytes for create2
bytes memory _salt1 = abi.encode(salt1);

// Address we expect for first World
address calculatedAddress = calculateAddress(
worldFactoryAddress,
keccak256(abi.encode(account, 0)),
keccak256(abi.encode(account, _salt1)),
type(World).creationCode
);

Expand All @@ -77,27 +77,28 @@ contract FactoriesTest is Test, GasReporter {
vm.expectEmit(true, false, false, false);
emit WorldDeployed(calculatedAddress);
startGasReport("deploy world via WorldFactory");
worldFactory.deployWorld();
worldFactory.deployWorld(_salt1);
endGasReport();

// Set the store address manually
StoreSwitch.setStoreAddress(calculatedAddress);

// Confirm accountCount (which is salt) has incremented
assertEq(uint256(worldFactory.worldCounts(account)), uint256(1));

// Confirm correct Core is installed
assertTrue(InstalledModules.get(address(initModule), keccak256(new bytes(0))));

// Confirm the msg.sender is owner of the root namespace of the new world
assertEq(NamespaceOwner.get(ROOT_NAMESPACE_ID), account);

// Deploy another world
// Deploy a second world

// Address we expect for World
// User defined bytes for create2
// unchecked for the fuzzing test
bytes memory _salt2 = abi.encode(salt2);

// Address we expect for second World
calculatedAddress = calculateAddress(
worldFactoryAddress,
keccak256(abi.encode(account, 1)),
keccak256(abi.encode(account, _salt2)),
type(World).creationCode
);

Expand All @@ -108,10 +109,7 @@ contract FactoriesTest is Test, GasReporter {
// Check for WorldDeployed event from Factory
vm.expectEmit(true, false, false, false);
emit WorldDeployed(calculatedAddress);
worldFactory.deployWorld();

// Confirm accountCount (which is salt) has incremented
assertEq(uint256(worldFactory.worldCounts(account)), uint256(2));
worldFactory.deployWorld(_salt2);

// Set the store address manually
StoreSwitch.setStoreAddress(calculatedAddress);
Expand All @@ -121,9 +119,13 @@ contract FactoriesTest is Test, GasReporter {

// Confirm the msg.sender is owner of the root namespace of the new world
assertEq(NamespaceOwner.get(ROOT_NAMESPACE_ID), account);

// Expect revert when deploying world with same bytes salt as already deployed world
vm.expectRevert();
worldFactory.deployWorld(_salt1);
}

function testWorldFactoryGas() public {
testWorldFactory(address(this));
testWorldFactory(address(this), 0, 1);
}
}
Loading