Skip to content

Commit

Permalink
refactor: FunctionRegistry to use salt instead of name
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstam committed Nov 7, 2023
1 parent 57a61da commit 862ba62
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
38 changes: 19 additions & 19 deletions contracts/src/FunctionRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ pragma solidity ^0.8.16;
import {IFunctionRegistry} from "./interfaces/IFunctionRegistry.sol";

abstract contract FunctionRegistry is IFunctionRegistry {
/// @dev Maps function identifiers to their corresponding verifiers.
/// @notice Maps function IDs to their corresponding verifiers.
mapping(bytes32 => address) public verifiers;

/// @dev Maps function identifiers to their corresponding owners.
/// @notice Maps function IDs to their corresponding owners.
mapping(bytes32 => address) public verifierOwners;

/// @notice Registers a function, using a pre-deployed verifier.
/// @param _owner The owner of the function.
/// @param _verifier The address of the verifier.
/// @param _name The name of the function to be registered.
function registerFunction(address _owner, address _verifier, string memory _name)
/// @param _salt The salt to use for calculatingthe function ID.
function registerFunction(address _owner, address _verifier, bytes32 _salt)
external
returns (bytes32 functionId)
{
functionId = getFunctionId(_owner, _name);
functionId = getFunctionId(_owner, _salt);
if (address(verifiers[functionId]) != address(0)) {
revert FunctionAlreadyRegistered(functionId); // should call update instead
}
Expand All @@ -28,18 +28,18 @@ abstract contract FunctionRegistry is IFunctionRegistry {
verifierOwners[functionId] = _owner;
verifiers[functionId] = _verifier;

emit FunctionRegistered(functionId, _verifier, _name, _owner);
emit FunctionRegistered(functionId, _verifier, _salt, _owner);
}

/// @notice Registers a function, using CREATE2 to deploy the verifier.
/// @param _owner The owner of the function.
/// @param _bytecode The bytecode of the verifier.
/// @param _name The name of the function to be registered.
function deployAndRegisterFunction(address _owner, bytes memory _bytecode, string memory _name)
/// @param _salt The salt to use for calculatingthe function ID.
function deployAndRegisterFunction(address _owner, bytes memory _bytecode, bytes32 _salt)
external
returns (bytes32 functionId, address verifier)
{
functionId = getFunctionId(_owner, _name);
functionId = getFunctionId(_owner, _salt);
if (address(verifiers[functionId]) != address(0)) {
revert FunctionAlreadyRegistered(functionId); // should call update instead
}
Expand All @@ -48,18 +48,18 @@ abstract contract FunctionRegistry is IFunctionRegistry {
verifier = _deploy(_bytecode, functionId);
verifiers[functionId] = verifier;

emit FunctionRegistered(functionId, verifier, _name, _owner);
emit FunctionRegistered(functionId, verifier, _salt, _owner);
}

/// @notice Updates the function, using a pre-deployed verifier.
/// @dev Only the owner of the function can update it.
/// @param _verifier The address of the verifier.
/// @param _name The name of the function to be updated.
function updateFunction(address _verifier, string memory _name)
/// @param _salt The salt that was used when registering this function ID.
function updateFunction(address _verifier, bytes32 _salt)
external
returns (bytes32 functionId)
{
functionId = getFunctionId(msg.sender, _name);
functionId = getFunctionId(msg.sender, _salt);
if (msg.sender != verifierOwners[functionId]) {
revert NotFunctionOwner(msg.sender, verifierOwners[functionId]);
}
Expand All @@ -77,12 +77,12 @@ abstract contract FunctionRegistry is IFunctionRegistry {
/// @notice Updates the function, using CREATE2 to deploy the new verifier.
/// @dev Only the owner of the function can update it.
/// @param _bytecode The bytecode of the verifier.
/// @param _name The name of the function to be updated.
function deployAndUpdateFunction(bytes memory _bytecode, string memory _name)
/// @param _salt The salt that was used when registering this function ID.
function deployAndUpdateFunction(bytes memory _bytecode, bytes32 _salt)
external
returns (bytes32 functionId, address verifier)
{
functionId = getFunctionId(msg.sender, _name);
functionId = getFunctionId(msg.sender, _salt);
if (msg.sender != verifierOwners[functionId]) {
revert NotFunctionOwner(msg.sender, verifierOwners[functionId]);
}
Expand All @@ -94,13 +94,13 @@ abstract contract FunctionRegistry is IFunctionRegistry {

/// @notice Returns the functionId for a given owner and function name.
/// @param _owner The owner of the function.
/// @param _name The name of the function.
function getFunctionId(address _owner, string memory _name)
/// @param _salt The name of the function.
function getFunctionId(address _owner, bytes32 _salt)
public
pure
returns (bytes32 functionId)
{
functionId = keccak256(abi.encode(_owner, _name));
functionId = keccak256(abi.encode(_owner, _salt));
}

function _deploy(bytes memory _bytecode, bytes32 _salt)
Expand Down
14 changes: 6 additions & 8 deletions contracts/src/interfaces/IFunctionRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.5.0;

interface IFunctionRegistryEvents {
event FunctionRegistered(
bytes32 indexed functionId, address verifier, string name, address owner
bytes32 indexed functionId, address verifier, bytes32 salt, address owner
);
event FunctionVerifierUpdated(bytes32 indexed functionId, address verifier);
event Deployed(
Expand All @@ -23,19 +23,17 @@ interface IFunctionRegistryErrors {
interface IFunctionRegistry is IFunctionRegistryEvents, IFunctionRegistryErrors {
function verifiers(bytes32 functionId) external view returns (address verifier);
function verifierOwners(bytes32 functionId) external view returns (address owner);
function registerFunction(address owner, address verifier, string memory name)
function registerFunction(address owner, address verifier, bytes32 salt)
external
returns (bytes32 functionId);
function deployAndRegisterFunction(address owner, bytes memory bytecode, string memory name)
function deployAndRegisterFunction(address owner, bytes memory bytecode, bytes32 salt)
external
returns (bytes32 functionId, address verifier);
function updateFunction(address verifier, string memory name)
external
returns (bytes32 functionId);
function deployAndUpdateFunction(bytes memory bytecode, string memory _name)
function updateFunction(address verifier, bytes32 salt) external returns (bytes32 functionId);
function deployAndUpdateFunction(bytes memory bytecode, bytes32 salt)
external
returns (bytes32 functionId, address verifier);
function getFunctionId(address owner, string memory name)
function getFunctionId(address owner, bytes32 salt)
external
pure
returns (bytes32 functionId);
Expand Down

0 comments on commit 862ba62

Please sign in to comment.