Skip to content

Commit

Permalink
forge fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jtguibas committed Oct 11, 2023
1 parent fef13c1 commit 0130b08
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 176 deletions.
19 changes: 4 additions & 15 deletions contracts/script/deploy/FunctionGateway.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

contract DeployFunctionGateway is BaseScript {
function run() external broadcaster {
console.log(
"Deploying FunctionGateway contract on chain %s",
Strings.toString(block.chainid)
);
console.log("Deploying FunctionGateway contract on chain %s", Strings.toString(block.chainid));

address TIMELOCK = envAddress("TIMELOCK", block.chainid);
address GUARDIAN = envAddress("GUARDIAN", block.chainid);
Expand All @@ -23,23 +20,15 @@ contract DeployFunctionGateway is BaseScript {
FunctionGateway gatewayImpl = new FunctionGateway{salt: CREATE2_SALT}();
FunctionGateway gateway;
if (!UPGRADE) {
gateway = FunctionGateway(
address(new Proxy{salt: CREATE2_SALT}(address(gatewayImpl), ""))
);
gateway = FunctionGateway(address(new Proxy{salt: CREATE2_SALT}(address(gatewayImpl), "")));
gateway.initialize(TIMELOCK, GUARDIAN);
} else {
gateway = FunctionGateway(
envAddress("FUNCTION_GATEWAY", block.chainid)
);
gateway = FunctionGateway(envAddress("FUNCTION_GATEWAY", block.chainid));
gateway.upgradeTo(address(gatewayImpl));
}

// Write address
writeEnvAddress(DEPLOYMENT_FILE, "FUNCTION_GATEWAY", address(gateway));
writeEnvAddress(
DEPLOYMENT_FILE,
"FUNCTION_GATEWAY_IMPL",
address(gatewayImpl)
);
writeEnvAddress(DEPLOYMENT_FILE, "FUNCTION_GATEWAY_IMPL", address(gatewayImpl));
}
}
102 changes: 22 additions & 80 deletions contracts/src/FunctionGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import {FunctionRegistry} from "./FunctionRegistry.sol";
import {TimelockedUpgradeable} from "./upgrades/TimelockedUpgradeable.sol";
import {IFeeVault} from "./payments/interfaces/IFeeVault.sol";

contract FunctionGateway is
IFunctionGateway,
FunctionRegistry,
TimelockedUpgradeable
{
contract FunctionGateway is IFunctionGateway, FunctionRegistry, TimelockedUpgradeable {
/// @dev The address of the fee vault.
address public feeVault;

Expand All @@ -37,10 +33,7 @@ contract FunctionGateway is
/// @dev Initializes the contract.
/// @param _timelock The address of the timelock contract.
/// @param _guardian The address of the guardian.
function initialize(
address _timelock,
address _guardian
) external initializer {
function initialize(address _timelock, address _guardian) external initializer {
isCallback = false;
__TimelockedUpgradeable_init(_timelock, _guardian);
}
Expand All @@ -64,13 +57,7 @@ contract FunctionGateway is
bytes32 contextHash = keccak256(_context);
address callbackAddress = msg.sender;
bytes32 requestHash = _requestHash(
nonce,
_functionId,
inputHash,
contextHash,
callbackAddress,
_callbackSelector,
_callbackGasLimit
nonce, _functionId, inputHash, contextHash, callbackAddress, _callbackSelector, _callbackGasLimit
);

// Increment the nonce.
Expand All @@ -79,13 +66,7 @@ contract FunctionGateway is
// Store the callback hash.
requests[nonce] = requestHash;
emit RequestCallback(
nonce,
_functionId,
_input,
_context,
callbackAddress,
_callbackSelector,
_callbackGasLimit
nonce, _functionId, _input, _context, callbackAddress, _callbackSelector, _callbackGasLimit
);

// Send the fee to the vault.
Expand All @@ -100,19 +81,12 @@ contract FunctionGateway is
/// @param _input The function input.
/// @param _address The address of the callback contract.
/// @param _data The data for the callback function.
function requestCall(
bytes32 _functionId,
bytes memory _input,
address _address,
bytes memory _data
) external payable {
function requestCall(bytes32 _functionId, bytes memory _input, address _address, bytes memory _data)
external
payable
{
// Emit event.
emit RequestCall(
_functionId,
_input,
_address,
_data
);
emit RequestCall(_functionId, _input, _address, _data);

// Send the fee to the vault.
IFeeVault(feeVault).depositNative{value: msg.value}(_address);
Expand All @@ -122,14 +96,9 @@ contract FunctionGateway is
/// this function reverts.
/// @param _functionId The function identifier.
/// @param _input The function input.
function verifiedCall(
bytes32 _functionId,
bytes memory _input
) external view returns (bytes memory) {
function verifiedCall(bytes32 _functionId, bytes memory _input) external view returns (bytes memory) {
bytes32 inputHash = sha256(_input);
if (
verifiedFunctionId == _functionId && verifiedInputHash == inputHash
) {
if (verifiedFunctionId == _functionId && verifiedInputHash == inputHash) {
return verifiedOutput;
} else {
revert InvalidCall(_functionId, _input);
Expand Down Expand Up @@ -160,13 +129,7 @@ contract FunctionGateway is
// Reconstruct the callback hash.
bytes32 contextHash = keccak256(_context);
bytes32 requestHash = _requestHash(
_nonce,
_functionId,
_inputHash,
contextHash,
_callbackAddress,
_callbackSelector,
_callbackGasLimit
_nonce, _functionId, _inputHash, contextHash, _callbackAddress, _callbackSelector, _callbackGasLimit
);

// Assert that the callback hash is unfilfilled.
Expand All @@ -185,9 +148,7 @@ contract FunctionGateway is

// Execute the callback.
isCallback = true;
(bool status, ) = _callbackAddress.call(
abi.encodeWithSelector(_callbackSelector, _output, _context)
);
(bool status,) = _callbackAddress.call(abi.encodeWithSelector(_callbackSelector, _output, _context));
isCallback = false;

// If the callback failed, revert.
Expand Down Expand Up @@ -227,7 +188,7 @@ contract FunctionGateway is
verifiedOutput = _output;

// Execute the callback.
(bool status, ) = _callbackAddress.call(_callbackData);
(bool status,) = _callbackAddress.call(_callbackData);
if (!status) {
revert CallFailed(_callbackAddress, _callbackData);
}
Expand Down Expand Up @@ -257,41 +218,22 @@ contract FunctionGateway is
bytes4 _callbackSelector,
uint32 _callbackGasLimit
) internal pure returns (bytes32) {
return
keccak256(
abi.encodePacked(
_nonce,
_functionId,
_inputHash,
_contextHash,
_callbackAddress,
_callbackSelector,
_callbackGasLimit
)
);
return keccak256(
abi.encodePacked(
_nonce, _functionId, _inputHash, _contextHash, _callbackAddress, _callbackSelector, _callbackGasLimit
)
);
}

/// @dev Verifies a proof with respect to a function identifier, input hash, and output hash.
/// @param _functionId The function identifier.
/// @param _inputHash The hash of the function input.
/// @param _outputHash The hash of the function output.
/// @param _proof The function proof.
function _verify(
bytes32 _functionId,
bytes32 _inputHash,
bytes32 _outputHash,
bytes memory _proof
) internal {
function _verify(bytes32 _functionId, bytes32 _inputHash, bytes32 _outputHash, bytes memory _proof) internal {
address verifier = verifiers[_functionId];
if (
!IFunctionVerifier(verifier).verify(_inputHash, _outputHash, _proof)
) {
revert InvalidProof(
address(verifier),
_inputHash,
_outputHash,
_proof
);
if (!IFunctionVerifier(verifier).verify(_inputHash, _outputHash, _proof)) {
revert InvalidProof(address(verifier), _inputHash, _outputHash, _proof);
}
}

Expand Down
43 changes: 13 additions & 30 deletions contracts/src/FunctionRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ contract FunctionRegistry is IFunctionRegistry {
/// @notice Registers a function, using a pre-deployed verifier.
/// @param _verifier The address of the verifier.
/// @param _name The name of the function to be registered.
function registerFunction(
address _verifier,
string memory _name
) external returns (bytes32 functionId) {
function registerFunction(address _verifier, string memory _name) external returns (bytes32 functionId) {
functionId = getFunctionId(msg.sender, _name);
if (address(verifiers[functionId]) != address(0)) {
revert FunctionAlreadyRegistered(functionId); // should call update instead
Expand All @@ -33,10 +30,10 @@ contract FunctionRegistry is IFunctionRegistry {
/// @notice Registers a function, using CREATE2 to deploy the verifier.
/// @param _bytecode The bytecode of the verifier.
/// @param _name The name of the function to be registered.
function deployAndRegisterFunction(
bytes memory _bytecode,
string memory _name
) external returns (bytes32 functionId, address verifier) {
function deployAndRegisterFunction(bytes memory _bytecode, string memory _name)
external
returns (bytes32 functionId, address verifier)
{
functionId = getFunctionId(msg.sender, _name);
if (address(verifiers[functionId]) != address(0)) {
revert FunctionAlreadyRegistered(functionId); // should call update instead
Expand All @@ -52,10 +49,7 @@ contract FunctionRegistry is IFunctionRegistry {
/// @notice Updates the function, using a pre-deployed verifier.
/// @param _verifier The address of the verifier.
/// @param _name The name of the function to be updated.
function updateFunction(
address _verifier,
string memory _name
) external returns (bytes32 functionId) {
function updateFunction(address _verifier, string memory _name) external returns (bytes32 functionId) {
functionId = getFunctionId(msg.sender, _name);
if (msg.sender != verifierOwners[functionId]) {
revert NotFunctionOwner(msg.sender, verifierOwners[functionId]);
Expand All @@ -71,10 +65,10 @@ contract FunctionRegistry is IFunctionRegistry {
/// @notice Updates the function, using CREATE2 to deploy the new verifier.
/// @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
) external returns (bytes32 functionId, address verifier) {
function deployAndUpdateFunction(bytes memory _bytecode, string memory _name)
external
returns (bytes32 functionId, address verifier)
{
functionId = getFunctionId(msg.sender, _name);
if (msg.sender != verifierOwners[functionId]) {
revert NotFunctionOwner(msg.sender, verifierOwners[functionId]);
Expand All @@ -88,26 +82,15 @@ contract FunctionRegistry is IFunctionRegistry {
/// @notice Returns the functionId for a given owner and function name.
/// @param _owner The owner of the function (sender of registerFunction).
/// @param _name The name of the function.
function getFunctionId(
address _owner,
string memory _name
) public pure returns (bytes32 functionId) {
function getFunctionId(address _owner, string memory _name) public pure returns (bytes32 functionId) {
functionId = keccak256(abi.encode(_owner, _name));
}

function _deploy(
bytes memory _bytecode,
bytes32 _salt
) internal returns (address deployedAddr) {
function _deploy(bytes memory _bytecode, bytes32 _salt) internal returns (address deployedAddr) {
if (_bytecode.length == 0) revert EmptyBytecode();

assembly {
deployedAddr := create2(
0,
add(_bytecode, 32),
mload(_bytecode),
_salt
)
deployedAddr := create2(0, add(_bytecode, 32), mload(_bytecode), _salt)
}
if (deployedAddr == address(0)) revert FailedDeploy();

Expand Down
47 changes: 9 additions & 38 deletions contracts/src/interfaces/IFunctionGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,17 @@ interface IFunctionGatewayEvents {
bytes4 callbackSelector,
uint32 callbackGasLimit
);
event RequestCall(
bytes32 indexed functionId,
bytes input,
address callbackAddress,
bytes callbackData
);
event RequestFulfilled(
uint32 indexed nonce,
bytes32 indexed functionId,
bytes32 inputHash,
bytes32 outputHash
);
event Call(
bytes32 indexed functionId,
bytes32 inputHash,
bytes32 outputHash
);
event RequestCall(bytes32 indexed functionId, bytes input, address callbackAddress, bytes callbackData);
event RequestFulfilled(uint32 indexed nonce, bytes32 indexed functionId, bytes32 inputHash, bytes32 outputHash);
event Call(bytes32 indexed functionId, bytes32 inputHash, bytes32 outputHash);
}

interface IFunctionGatewayErrors {
error InvalidRequest(
uint32 nonce,
bytes32 expectedRequestHash,
bytes32 requestHash
);
error InvalidRequest(uint32 nonce, bytes32 expectedRequestHash, bytes32 requestHash);
error CallbackFailed(bytes4 callbackSelector, bytes output, bytes context);
error InvalidCall(bytes32 functionId, bytes input);
error CallFailed(address callbackAddress, bytes callbackData);
error InvalidProof(
address verifier,
bytes32 inputHash,
bytes32 outputHash,
bytes proof
);
error InvalidProof(address verifier, bytes32 inputHash, bytes32 outputHash, bytes proof);
}

interface IFunctionGateway is IFunctionGatewayEvents, IFunctionGatewayErrors {
Expand All @@ -56,15 +33,9 @@ interface IFunctionGateway is IFunctionGatewayEvents, IFunctionGatewayErrors {
uint32 _callbackGasLimit
) external payable returns (bytes32);

function requestCall(
bytes32 _functionId,
bytes memory _input,
address _callbackAddress,
bytes memory _callbackData
) external payable;
function requestCall(bytes32 _functionId, bytes memory _input, address _callbackAddress, bytes memory _callbackData)
external
payable;

function verifiedCall(
bytes32 _functionId,
bytes memory _input
) external view returns (bytes memory);
function verifiedCall(bytes32 _functionId, bytes memory _input) external view returns (bytes memory);
}
4 changes: 4 additions & 0 deletions contracts/src/mocks/MockFunctionGateway.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

contract MockFunctionGateway {}
Loading

0 comments on commit 0130b08

Please sign in to comment.