|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +pragma solidity ^0.8; |
| 3 | + |
| 4 | +import {GPv2AllowListAuthentication} from "src/contracts/GPv2AllowListAuthentication.sol"; |
| 5 | + |
| 6 | +import {Helper} from "./Helper.sol"; |
| 7 | + |
| 8 | +interface IEIP173Proxy { |
| 9 | + function owner() external view returns (address); |
| 10 | +} |
| 11 | + |
| 12 | +// ref: https://github.com/wighawag/hardhat-deploy/blob/e0ffcf9e7dc92b246e832c4d175f9dbd8b6df14d/solc_0.8/proxy/EIP173Proxy.sol |
| 13 | +bytes32 constant EIP173_IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; |
| 14 | + |
| 15 | +contract DeploymentTest is Helper(false) { |
| 16 | + event Metadata(string, bytes); |
| 17 | + |
| 18 | + function test__same_built_and_deployed_bytecode_metadata__authenticator() external { |
| 19 | + _assertBuiltAndDeployedMetadataCoincide(address(allowListImpl), "GPv2AllowListAuthentication"); |
| 20 | + } |
| 21 | + |
| 22 | + function test__same_built_and_deployed_bytecode_metadata__settlement() external { |
| 23 | + _assertBuiltAndDeployedMetadataCoincide(address(settlement), "GPv2Settlement"); |
| 24 | + } |
| 25 | + |
| 26 | + function test__same_built_and_deployed_bytecode_metadata__vault_relayer() external { |
| 27 | + _assertBuiltAndDeployedMetadataCoincide(address(vaultRelayer), "GPv2VaultRelayer"); |
| 28 | + } |
| 29 | + |
| 30 | + function test__determininstic_addresses__authenticator__proxy() external view { |
| 31 | + assertEq( |
| 32 | + _computeCreate2Addr( |
| 33 | + abi.encodePacked( |
| 34 | + vm.getCode("EIP173Proxy"), |
| 35 | + abi.encode( |
| 36 | + _implementationAddress(address(allowList)), |
| 37 | + owner, |
| 38 | + abi.encodeCall(GPv2AllowListAuthentication.initializeManager, (owner)) |
| 39 | + ) |
| 40 | + ) |
| 41 | + ), |
| 42 | + address(authenticator), |
| 43 | + "authenticator address not as expected" |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + function test__determininstic_addresses__authenticator__implementation() external view { |
| 48 | + assertEq( |
| 49 | + _computeCreate2Addr(vm.getCode("GPv2AllowListAuthentication")), |
| 50 | + _implementationAddress(address(allowList)), |
| 51 | + "authenticator impl address not as expected" |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + function test__determininstic_addresses__settlement() external view { |
| 56 | + assertEq( |
| 57 | + _computeCreate2Addr( |
| 58 | + abi.encodePacked(vm.getCode("GPv2Settlement"), abi.encode(address(authenticator), address(vault))) |
| 59 | + ), |
| 60 | + address(settlement), |
| 61 | + "settlement address not as expected" |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + function test__authorization__authenticator_has_dedicated_owner() external view { |
| 66 | + assertEq(IEIP173Proxy(address(allowList)).owner(), owner, "owner not as expected"); |
| 67 | + } |
| 68 | + |
| 69 | + function test__authorization__authenticator_has_dedicated_manager() external view { |
| 70 | + assertEq(allowList.manager(), owner, "manager not as expected"); |
| 71 | + } |
| 72 | + |
| 73 | + function _assertBuiltAndDeployedMetadataCoincide(address addr, string memory artifactName) internal { |
| 74 | + bytes memory deployedCode = vm.getDeployedCode(artifactName); |
| 75 | + assertEq( |
| 76 | + keccak256(_getMetadata(string(abi.encodePacked("deployed ", artifactName)), addr.code)), |
| 77 | + keccak256(_getMetadata(artifactName, deployedCode)), |
| 78 | + "metadata doesnt match" |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + function _getMetadata(string memory hint, bytes memory bytecode) internal returns (bytes memory metadata) { |
| 83 | + assembly ("memory-safe") { |
| 84 | + // the last two bytes encode the size of the cbor encoded metadata |
| 85 | + let bytecodeSize := mload(bytecode) |
| 86 | + let bytecodeStart := add(bytecode, 0x20) |
| 87 | + let cborSizeOffset := add(bytecodeStart, sub(bytecodeSize, 0x20)) |
| 88 | + let cborSize := and(mload(cborSizeOffset), 0xffff) |
| 89 | + |
| 90 | + // copy the metadata out |
| 91 | + metadata := mload(0x40) |
| 92 | + let metadataSize := add(cborSize, 0x02) |
| 93 | + mstore(metadata, metadataSize) |
| 94 | + let metadataOffset := add(bytecodeStart, sub(bytecodeSize, metadataSize)) |
| 95 | + mcopy(add(metadata, 0x20), metadataOffset, metadataSize) |
| 96 | + |
| 97 | + // update free memory ptr |
| 98 | + mstore(0x40, add(metadata, add(metadataSize, 0x20))) |
| 99 | + } |
| 100 | + emit Metadata(hint, metadata); |
| 101 | + } |
| 102 | + |
| 103 | + function _computeCreate2Addr(bytes memory initCode) internal view returns (address) { |
| 104 | + return vm.computeCreate2Address(SALT, hashInitCode(initCode), deployer); |
| 105 | + } |
| 106 | + |
| 107 | + function _implementationAddress(address proxy) internal view returns (address) { |
| 108 | + return address(uint160(uint256(vm.load(proxy, EIP173_IMPLEMENTATION_SLOT)))); |
| 109 | + } |
| 110 | +} |
0 commit comments