Skip to content

Commit

Permalink
add slot derivation test
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanrikulu committed Nov 25, 2024
1 parent aa39c67 commit 73242b9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/TransparentVerifiableProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ contract TransparentVerifiableProxy is Proxy, Initializable {
// ### EVENTS
error ProxyDeniedOwnerAccess();

// // Modifier that allows only the owner to call certain functions
// modifier onlyOwner() {
// require(msg.sender == owner, "Caller is not the owner");
// modifier that allows only the creator address to call certain functions
// modifier onlyCreator() {
// require(msg.sender == creator, "Caller is not the creator address");
// _;
// }

Expand Down
72 changes: 72 additions & 0 deletions test/TransparentVerifiableProxy.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "forge-std/Test.sol";
import "../src/TransparentVerifiableProxy.sol";
import {SlotDerivation} from "@openzeppelin/contracts/utils/SlotDerivation.sol";
import {MockRegistry} from "../src/mock/MockRegistry.sol";

contract TransparentVerifiableProxyTest is Test {
using SlotDerivation for bytes32;

TransparentVerifiableProxy proxy;

address creator = address(0x1);
address owner = address(0x2);
address implementation = address(new MockRegistry());
uint256 salt = 12345;
bytes emptyData;

string internal constant _VERIFICATION_SLOT = "proxy.verifiable";
string internal constant _SALT = "salt";
string internal constant _OWNER = "owner";

function setUp() public {
proxy = new TransparentVerifiableProxy(creator);
}

function testInitialize() public {
// initialize the proxy
proxy.initialize(salt, owner, implementation, emptyData);

// check salt and owner values
assertEq(proxy.salt(), salt, "Salt mismatch");
assertEq(proxy.owner(), owner, "Owner mismatch");
}

function testSaltStorage() public {
// initialize the proxy
proxy.initialize(salt, owner, implementation, emptyData);

// compute the base slot
bytes32 baseSlot = SlotDerivation.erc7201Slot(_VERIFICATION_SLOT);

// use SlotDerivation to compute the salt slot
bytes32 saltSlot = baseSlot.deriveMapping(_SALT);

// directly manipulate the storage for the salt
uint256 newSalt = 54321;
vm.store(address(proxy), saltSlot, bytes32(newSalt));

// verify the updated salt
assertEq(proxy.salt(), newSalt, "Salt update failed");
}

function testOwnerStorage() public {
// initialize the proxy
proxy.initialize(salt, owner, implementation, emptyData);

// compute the base slot
bytes32 baseSlot = SlotDerivation.erc7201Slot(_VERIFICATION_SLOT);

// use SlotDerivation to compute the owner slot
bytes32 ownerSlot = baseSlot.deriveMapping(_OWNER);

// directly manipulate the storage for the owner
address newOwner = address(0x4);
vm.store(address(proxy), ownerSlot, bytes32(uint256(uint160(newOwner))));

// verify the updated owner
assertEq(proxy.owner(), newOwner, "Owner update failed");
}
}

0 comments on commit 73242b9

Please sign in to comment.