Skip to content

Commit

Permalink
test: add deploymentHistory test
Browse files Browse the repository at this point in the history
  • Loading branch information
cucupac committed Oct 25, 2023
1 parent 21b6a70 commit 14c34d7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
17 changes: 13 additions & 4 deletions src/PredictiveDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract PredictiveDeployer is Initializable, UUPSUpgradeable, Ownable {

// Factory Storage
mapping(address => mapping(bytes32 => uint256)) public userNonces;
mapping(address => address[]) public deploymentHistory;
mapping(address => address[]) internal _deploymentHistory;

// Events
event Deploy(address indexed principal, address indexed child, bytes32 hashedBytecode, uint256 nonce);
Expand Down Expand Up @@ -98,6 +98,15 @@ contract PredictiveDeployer is Initializable, UUPSUpgradeable, Ownable {
return keccak256(_bytecode);
}

/**
* @dev Returns a list of the provided principal's previously deployed child contracts.
* @param _principal The address of the account that signed the message hash.
* @return deploymentHistory A list of the provided principal's previously deployed child contracts.
*/
function getDeploymentHistory(address _principal) public view returns (address[] memory) {
return _deploymentHistory[_principal];
}

/**
* @dev Deploys arbitrary child contracts at predictable addresses, derived from account signatures.
* @param _principal The address of the account that signed the message hash.
Expand Down Expand Up @@ -130,12 +139,14 @@ contract PredictiveDeployer is Initializable, UUPSUpgradeable, Ownable {
}

// Update deployment history
deploymentHistory[_principal].push(child);
_deploymentHistory[_principal].push(child);

emit Deploy(_principal, child, hashedByteCode, currentNonce);
return child;
}

receive() external payable { } // solhint-disable-line no-empty-blocks

/* ****************************************************************************
**
** Admin Functions
Expand All @@ -158,6 +169,4 @@ contract PredictiveDeployer is Initializable, UUPSUpgradeable, Ownable {

SafeTransferLib.safeTransfer(ERC20(_token), msg.sender, balance);
}

receive() external payable { } // solhint-disable-line no-empty-blocks
}
1 change: 1 addition & 0 deletions src/interfaces/IPredictiveDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.21;

interface IPredictiveDeployer {
function userNonces(address _principal, bytes32 _hashedBytecode) external returns (uint256);
function getDeploymentHistory(address _principal) external returns (address[] memory);
function getTransactionHash(address _principal, bytes memory _bytecode, uint256 _nonce)
external
returns (bytes32);
Expand Down
45 changes: 42 additions & 3 deletions test/PredictiveDeployer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ pragma solidity ^0.8.21;
import { VmSafe } from "forge-std/Vm.sol";
import { PredictiveDeployer } from "../src/PredictiveDeployer.sol";
import { ERC1967Proxy } from "../src/dependencies/proxy/ERC1967Proxy.sol";
import { IPredictiveDeployer } from "../src/interfaces/IPredictiveDeployer.sol";
import { TestSetup } from "./common/TestSetup.t.sol";
import { AddressLib } from "./helpers/libraries/AddressLib.t.sol";
import { DeploymentHelper } from "./helpers/DeploymentHelper.t.sol";
import { IPredictiveDeployer } from "../src/interfaces/IPredictiveDeployer.sol";
import { CONTRACT_DEPLOYER } from "./common/Constants.t.sol";

contract PredictiveDeployerTest is DeploymentHelper, TestSetup {
/* solhint-disable func-name-mixedcase */

using AddressLib for address[];

function testFuzz_GetAddress(uint256 pkNum, address sender) public {
// Setup
VmSafe.Wallet memory wallet = vm.createWallet(uint256(keccak256(abi.encodePacked(uint256(pkNum)))));
Expand Down Expand Up @@ -67,12 +70,10 @@ contract PredictiveDeployerTest is DeploymentHelper, TestSetup {

// Act
address actualChild = IPredictiveDeployer(address(proxy)).deploy(wallet.addr, signature, bytecodeChildA);
uint256 postDeployNonce = IPredictiveDeployer(address(proxy)).userNonces(wallet.addr, hashedBytecodeChildA);
vm.stopPrank();

// Assertions
assertEq(actualChild, expectedChild);
assertEq(postDeployNonce, preDeployNonce + 1);
}

function testFuzz_DeployTwice(uint256 pkNum) public {
Expand Down Expand Up @@ -111,6 +112,44 @@ contract PredictiveDeployerTest is DeploymentHelper, TestSetup {
assertEq(setOneChildB, setTwoChildB);
}

function testFuzz_DeployNonceUpdate(uint256 pkNum) public {
// Setup
VmSafe.Wallet memory wallet = vm.createWallet(uint256(keccak256(abi.encodePacked(uint256(pkNum)))));
uint256 preDeployNonce = IPredictiveDeployer(address(proxy)).userNonces(wallet.addr, hashedBytecodeChildA);

// Act
deployChild(address(proxy), wallet, bytecodeChildA);
uint256 postDeployNonce = IPredictiveDeployer(address(proxy)).userNonces(wallet.addr, hashedBytecodeChildA);

// Assertions
assertEq(postDeployNonce, preDeployNonce + 1);
}

function testFuzz_DeployHistoryUpdate(uint256 pkNum) public {
// Setup
VmSafe.Wallet memory wallet = vm.createWallet(uint256(keccak256(abi.encodePacked(uint256(pkNum)))));

address[] memory deploymentHistory = IPredictiveDeployer(address(proxy)).getDeploymentHistory(wallet.addr);

// Pre-act assertions
assertEq(deploymentHistory.length, 0);

// Act
address child1 = deployChild(address(proxy), wallet, bytecodeChildA);
deploymentHistory = IPredictiveDeployer(address(proxy)).getDeploymentHistory(wallet.addr);

// Assertions
assertEq(deploymentHistory.length, 1);
assertTrue(deploymentHistory.includes(child1));

address child2 = deployChild(address(proxy), wallet, bytecodeChildA);
deploymentHistory = IPredictiveDeployer(address(proxy)).getDeploymentHistory(wallet.addr);

// Assertions
assertEq(deploymentHistory.length, 2);
assertTrue(deploymentHistory.includes(child2));
}

function testFuzz_CannotDeployReplay(uint256 pkNum) public {
// Setup
VmSafe.Wallet memory wallet = vm.createWallet(uint256(keccak256(abi.encodePacked(uint256(pkNum)))));
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions test/helpers/libraries/AddressLib.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

library AddressLib {
function includes(address[] memory _array, address _address) internal pure returns (bool) {
for (uint256 i; i < _array.length; i++) {
if (_array[i] == _address) {
return true;
}
}
return false;
}
}

0 comments on commit 14c34d7

Please sign in to comment.