Skip to content

Commit

Permalink
Use prettier default print width
Browse files Browse the repository at this point in the history
  • Loading branch information
cristovaoth committed Jun 6, 2024
1 parent 3986828 commit 7ae874a
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 72 deletions.
1 change: 0 additions & 1 deletion packages/evm/.prettierrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ bracketSpacing: true
plugins:
- "@trivago/prettier-plugin-sort-imports"
- "prettier-plugin-solidity"
printWidth: 100
proseWrap: "always"
singleQuote: false
tabWidth: 2
Expand Down
106 changes: 84 additions & 22 deletions packages/evm/contracts/Enclave.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity >=0.8.26;

import { IEnclave, E3, IComputationModule, IExecutionModule } from "./interfaces/IEnclave.sol";
import {
IEnclave,
E3,
IComputationModule,
IExecutionModule
} from "./interfaces/IEnclave.sol";
import { ICyphernodeRegistry } from "./interfaces/ICyphernodeRegistry.sol";
import { IInputValidator } from "./interfaces/IInputValidator.sol";
import { IOutputVerifier } from "./interfaces/IOutputVerifier.sol";
Expand All @@ -28,10 +33,12 @@ contract Enclave is IEnclave, OwnableUpgradeable {
// This would reduce the governance overhead for Enclave.

// Mapping of allowed computation modules.
mapping(IComputationModule computationModule => bool allowed) public computationModules;
mapping(IComputationModule computationModule => bool allowed)
public computationModules;

// Mapping of allowed execution modules.
mapping(IExecutionModule executionModule => bool allowed) public executionModules;
mapping(IExecutionModule executionModule => bool allowed)
public executionModules;

// Mapping of E3s.
mapping(uint256 id => E3 e3) public e3s;
Expand Down Expand Up @@ -71,7 +78,11 @@ contract Enclave is IEnclave, OwnableUpgradeable {

/// @param _owner The owner of this contract
/// @param _maxDuration The maximum duration of a computation in seconds
constructor(address _owner, ICyphernodeRegistry _cyphernodeRegistry, uint256 _maxDuration) {
constructor(
address _owner,
ICyphernodeRegistry _cyphernodeRegistry,
uint256 _maxDuration
) {
initialize(_owner, _cyphernodeRegistry, _maxDuration);
}

Expand Down Expand Up @@ -110,25 +121,39 @@ contract Enclave is IEnclave, OwnableUpgradeable {
// TODO: should payment checks be somewhere else? Perhaps in the computation module or cyphernode registry?
require(msg.value > 0, PaymentRequired(msg.value));

require(threshold[1] >= threshold[0] && threshold[0] > 0, InvalidThreshold(threshold));
require(
threshold[1] >= threshold[0] && threshold[0] > 0,
InvalidThreshold(threshold)
);
// TODO: should 0 be a magic number for infinite duration?
require(duration > 0 && duration <= maxDuration, InvalidDuration(duration));
require(
duration > 0 && duration <= maxDuration,
InvalidDuration(duration)
);
require(
computationModules[computationModule],
ComputationModuleNotAllowed(computationModule)
);
require(executionModules[executionModule], ModuleNotEnabled(address(executionModule)));
require(
executionModules[executionModule],
ModuleNotEnabled(address(executionModule))
);

// TODO: should IDs be incremental or produced deterministic?
e3Id = nexte3Id;
nexte3Id++;

IInputValidator inputValidator = computationModule.validate(computationParams);
IInputValidator inputValidator = computationModule.validate(
computationParams
);
require(address(inputValidator) != address(0), InvalidComputation());

// TODO: validate that the requested computation can be performed by the given execution module.
IOutputVerifier outputVerifier = executionModule.validate(emParams);
require(address(outputVerifier) != address(0), InvalidExecutionModuleSetup());
require(
address(outputVerifier) != address(0),
InvalidExecutionModuleSetup()
);

e3 = E3({
threshold: threshold,
Expand All @@ -149,7 +174,13 @@ contract Enclave is IEnclave, OwnableUpgradeable {
);
// TODO: validate that the selected pool accepts both the computation and execution modules.

emit E3Requested(e3Id, e3s[e3Id], pools, computationModule, executionModule);
emit E3Requested(
e3Id,
e3s[e3Id],
pools,
computationModule,
executionModule
);
}

function activate(uint256 e3Id) external returns (bool success) {
Expand All @@ -158,7 +189,8 @@ contract Enclave is IEnclave, OwnableUpgradeable {
E3 memory e3 = getE3(e3Id);
require(e3.expiration == 0, E3AlreadyActivated(e3Id));

bytes memory committeePublicKey = cyphernodeRegistry.getCommitteePublicKey(e3Id);
bytes memory committeePublicKey = cyphernodeRegistry
.getCommitteePublicKey(e3Id);
// Note: This check feels weird
require(committeePublicKey.length > 0, CommitteeSelectionFailed());

Expand All @@ -171,13 +203,19 @@ contract Enclave is IEnclave, OwnableUpgradeable {
return true;
}

function publishInput(uint256 e3Id, bytes memory data) external returns (bool success) {
function publishInput(
uint256 e3Id,
bytes memory data
) external returns (bool success) {
E3 memory e3 = getE3(e3Id);

// Note: if we make 0 a no expiration, this has to be refactored
require(e3.expiration > 0, E3NotActivated(e3Id));
// TODO: should we have an input window, including both a start and end timestamp?
require(e3.expiration > block.timestamp, InputDeadlinePassed(e3Id, e3.expiration));
require(
e3.expiration > block.timestamp,
InputDeadlinePassed(e3Id, e3.expiration)
);
bytes memory input;
(input, success) = e3.inputValidator.validate(msg.sender, data);
require(success, InvalidInput());
Expand All @@ -190,10 +228,16 @@ contract Enclave is IEnclave, OwnableUpgradeable {
bytes memory data
) external returns (bool success) {
E3 memory e3 = getE3(e3Id);
require(e3.expiration <= block.timestamp, InputDeadlineNotPassed(e3Id, e3.expiration));
require(
e3.expiration <= block.timestamp,
InputDeadlineNotPassed(e3Id, e3.expiration)
);
// TODO: should the output verifier be able to change its mind?
//i.e. should we be able to call this multiple times?
require(e3.ciphertextOutput.length == 0, CiphertextOutputAlreadyPublished(e3Id));
require(
e3.ciphertextOutput.length == 0,
CiphertextOutputAlreadyPublished(e3Id)
);
bytes memory output;
(output, success) = e3.outputVerifier.verify(e3Id, data);
require(success, InvalidOutput());
Expand All @@ -207,8 +251,14 @@ contract Enclave is IEnclave, OwnableUpgradeable {
bytes memory data
) external returns (bool success) {
E3 memory e3 = getE3(e3Id);
require(e3.ciphertextOutput.length > 0, CiphertextOutputNotPublished(e3Id));
require(e3.plaintextOutput.length == 0, PlaintextOutputAlreadyPublished(e3Id));
require(
e3.ciphertextOutput.length > 0,
CiphertextOutputNotPublished(e3Id)
);
require(
e3.plaintextOutput.length == 0,
PlaintextOutputAlreadyPublished(e3Id)
);
bytes memory output;
(output, success) = e3.computationModule.verify(e3Id, data);
require(success, InvalidOutput());
Expand All @@ -223,7 +273,9 @@ contract Enclave is IEnclave, OwnableUpgradeable {
// //
////////////////////////////////////////////////////////////

function setMaxDuration(uint256 _maxDuration) public onlyOwner returns (bool success) {
function setMaxDuration(
uint256 _maxDuration
) public onlyOwner returns (bool success) {
maxDuration = _maxDuration;
success = true;
emit MaxDurationSet(_maxDuration);
Expand All @@ -233,7 +285,8 @@ contract Enclave is IEnclave, OwnableUpgradeable {
ICyphernodeRegistry _cyphernodeRegistry
) public onlyOwner returns (bool success) {
require(
address(_cyphernodeRegistry) != address(0) && _cyphernodeRegistry != cyphernodeRegistry,
address(_cyphernodeRegistry) != address(0) &&
_cyphernodeRegistry != cyphernodeRegistry,
InvalidCyphernodeRegistry(_cyphernodeRegistry)
);
cyphernodeRegistry = _cyphernodeRegistry;
Expand All @@ -256,7 +309,10 @@ contract Enclave is IEnclave, OwnableUpgradeable {
function enableExecutionModule(
IExecutionModule executionModule
) public onlyOwner returns (bool success) {
require(!executionModules[executionModule], ModuleAlreadyEnabled(address(executionModule)));
require(
!executionModules[executionModule],
ModuleAlreadyEnabled(address(executionModule))
);
executionModules[executionModule] = true;
success = true;
emit ExecutionModuleEnabled(executionModule);
Expand All @@ -277,7 +333,10 @@ contract Enclave is IEnclave, OwnableUpgradeable {
function disableExecutionModule(
IExecutionModule executionModule
) public onlyOwner returns (bool success) {
require(executionModules[executionModule], ModuleNotEnabled(address(executionModule)));
require(
executionModules[executionModule],
ModuleNotEnabled(address(executionModule))
);
delete executionModules[executionModule];
success = true;
emit ExecutionModuleDisabled(executionModule);
Expand All @@ -291,6 +350,9 @@ contract Enclave is IEnclave, OwnableUpgradeable {

function getE3(uint256 e3Id) public view returns (E3 memory e3) {
e3 = e3s[e3Id];
require(e3.computationModule != IComputationModule(address(0)), E3DoesNotExist(e3Id));
require(
e3.computationModule != IComputationModule(address(0)),
E3DoesNotExist(e3Id)
);
}
}
4 changes: 3 additions & 1 deletion packages/evm/contracts/interfaces/IComputationModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ interface IComputationModule {
/// @notice This function should be called by the Enclave contract to validate the computation parameters.
/// @param params ABI encoded computation parameters.
/// @return inputValidator The input validator to be used for the computation.
function validate(bytes calldata params) external returns (IInputValidator inputValidator);
function validate(
bytes calldata params
) external returns (IInputValidator inputValidator);

/// @notice This function should be called by the Enclave contract to verify the decrypted output of an E3.
/// @param e3Id ID of the E3.
Expand Down
10 changes: 8 additions & 2 deletions packages/evm/contracts/interfaces/ICyphernodeRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ interface ICyphernodeRegistry {
/// @param e3Id ID of the E3 for which the committee was selected.
/// @param pools Addresses of the pools of nodes from which the committee was selected.
/// @param threshold The M/N threshold for the committee.
event CommitteeRequested(uint256 indexed e3Id, address[] pools, uint32[2] threshold);
event CommitteeRequested(
uint256 indexed e3Id,
address[] pools,
uint32[2] threshold
);

/// @notice This event MUST be emitted when a committee is selected for an E3.
/// @param e3Id ID of the E3 for which the committee was selected.
Expand Down Expand Up @@ -50,5 +54,7 @@ interface ICyphernodeRegistry {
/// @dev This function MUST revert if the committee has not yet published a public key.
/// @param e3Id ID of the E3 for which to get the committee public key.
/// @return publicKey The public key of the committee.
function getCommitteePublicKey(uint256 e3Id) external view returns (bytes memory publicKey);
function getCommitteePublicKey(
uint256 e3Id
) external view returns (bytes memory publicKey);
}
20 changes: 16 additions & 4 deletions packages/evm/contracts/interfaces/IEnclave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ interface IEnclave {
/// @param e3Id ID of the E3.
/// @param expiration Timestamp when committee duties expire.
/// @param committeePublicKey Public key of the committee.
event E3Activated(uint256 e3Id, uint256 expiration, bytes committeePublicKey);
event E3Activated(
uint256 e3Id,
uint256 expiration,
bytes committeePublicKey
);

/// @notice This event MUST be emitted when an input to an Encrypted Execution Environment (E3) is
/// successfully published.
Expand All @@ -46,7 +50,10 @@ interface IEnclave {
/// is successfully published.
/// @param e3Id ID of the E3.
/// @param ciphertextOutput ABI encoded ciphertext output.
event CiphertextOutputPublished(uint256 indexed e3Id, bytes ciphertextOutput);
event CiphertextOutputPublished(
uint256 indexed e3Id,
bytes ciphertextOutput
);

/// @notice This event MUST be emitted any time the `maxDuration` is set.
/// @param maxDuration The maximum duration of a computation in seconds.
Expand Down Expand Up @@ -113,7 +120,10 @@ interface IEnclave {
/// @param e3Id ID of the E3.
/// @param data ABI encoded input data to publish.
/// @return success True if the input was successfully published.
function publishInput(uint256 e3Id, bytes calldata data) external returns (bool success);
function publishInput(
uint256 e3Id,
bytes calldata data
) external returns (bool success);

/// @notice This function should be called to publish output data for an Encrypted Execution Environment (E3).
/// @dev This function MUST emit the CiphertextOutputPublished event.
Expand Down Expand Up @@ -145,7 +155,9 @@ interface IEnclave {
/// @notice This function should be called to set the maximum duration of requested computations.
/// @param _maxDuration The maximum duration of a computation in seconds.
/// @return success True if the max duration was successfully set.
function setMaxDuration(uint256 _maxDuration) external returns (bool success);
function setMaxDuration(
uint256 _maxDuration
) external returns (bool success);

////////////////////////////////////////////////////////////
// //
Expand Down
4 changes: 3 additions & 1 deletion packages/evm/contracts/interfaces/IExecutionModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ import { IOutputVerifier } from "./IOutputVerifier.sol";
interface IExecutionModule {
/// @notice This function should be called by the Enclave contract to validate the execution module parameters.
/// @param params ABI encoded execution module parameters.
function validate(bytes calldata params) external returns (IOutputVerifier outputVerifier);
function validate(
bytes calldata params
) external returns (IOutputVerifier outputVerifier);
}
13 changes: 10 additions & 3 deletions packages/evm/contracts/registry/CyphernodeRegistryOwnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ contract CyphernodeRegistryOwnable is ICyphernodeRegistry, OwnableUpgradeable {
bytes memory publicKey
) external onlyOwner {
Committee storage committee = committees[e3Id];
require(keccak256(committee.publicKey) == keccak256(hex""), CommitteeAlreadyPublished());
require(
keccak256(committee.publicKey) == keccak256(hex""),
CommitteeAlreadyPublished()
);
committee.nodes = _nodes;
committee.merkleRoots = merkleRoots;
committee.publicKey = publicKey;
Expand All @@ -124,12 +127,16 @@ contract CyphernodeRegistryOwnable is ICyphernodeRegistry, OwnableUpgradeable {
// //
////////////////////////////////////////////////////////////

function getCommitteePublicKey(uint256 e3Id) external view returns (bytes memory publicKey) {
function getCommitteePublicKey(
uint256 e3Id
) external view returns (bytes memory publicKey) {
publicKey = committees[e3Id].publicKey;
require(publicKey.length > 0, NoPublicKeyPublished());
}

function getCommittee(uint256 e3Id) external view returns (Committee memory committee) {
function getCommittee(
uint256 e3Id
) external view returns (Committee memory committee) {
committee = committees[e3Id];
require(committees[e3Id].threshold.length > 0, CommitteeDoesNotExist());
}
Expand Down
9 changes: 7 additions & 2 deletions packages/evm/contracts/test/MockComputationModule.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity >=0.8.26;

import { IComputationModule, IInputValidator } from "../interfaces/IComputationModule.sol";
import {
IComputationModule,
IInputValidator
} from "../interfaces/IComputationModule.sol";

contract MockComputationModule is IComputationModule {
error invalidParams(bytes params);

function validate(bytes memory params) external pure returns (IInputValidator inputValidator) {
function validate(
bytes memory params
) external pure returns (IInputValidator inputValidator) {
require(params.length == 32, "invalid params");
// solhint-disable no-inline-assembly
assembly {
Expand Down
8 changes: 6 additions & 2 deletions packages/evm/contracts/test/MockCyphernodeRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ contract MockCyphernodeRegistry is ICyphernodeRegistry {
}
}

function getCommitteePublicKey(uint256 e3Id) external pure returns (bytes memory) {
function getCommitteePublicKey(
uint256 e3Id
) external pure returns (bytes memory) {
if (e3Id == type(uint256).max) {
return hex"";
} else {
Expand All @@ -38,7 +40,9 @@ contract MockCyphernodeRegistryEmptyKey is ICyphernodeRegistry {
}
}

function getCommitteePublicKey(uint256) external pure returns (bytes memory) {
function getCommitteePublicKey(
uint256
) external pure returns (bytes memory) {
return hex"";
}
}
Loading

0 comments on commit 7ae874a

Please sign in to comment.