Skip to content

Commit

Permalink
add unit test stubs for remaining core functions
Browse files Browse the repository at this point in the history
  • Loading branch information
auryn-macmillan committed Jun 3, 2024
1 parent 27c1b58 commit 9bc9340
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
28 changes: 15 additions & 13 deletions packages/evm/contracts/Enclave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -137,46 +137,48 @@ contract Enclave is IEnclave, OwnableUpgradeable {
}

function activate(uint256 e3Id) external returns (bool success) {
E3 storage e3 = e3s[e3Id];
E3 memory e3 = getE3(e3Id);
require(e3.expiration == 0, E3AlreadyActivated(e3Id));
e3.expiration = block.timestamp + maxDuration; // TODO: this should be based on the duration requested, not the current max duration.

e3.committeePublicKey = cypherNodeRegistry.getCommitteePublicKey(e3Id);
success = e3.committeePublicKey.length > 0;
bytes memory committeePublicKey = cypherNodeRegistry.getCommitteePublicKey(e3Id);
success = committeePublicKey.length > 0;
require(success, CommitteeSelectionFailed());
e3s[e3Id].committeePublicKey = committeePublicKey;

emit E3Activated(e3Id, e3.expiration, e3.committeePublicKey);
}

function publishInput(uint256 e3Id, bytes memory data) external returns (bool success) {
E3 storage e3 = e3s[e3Id];
require(e3.expiration > block.timestamp, InputDeadlinePassed(e3Id, e3.expiration));
E3 memory e3 = getE3(e3Id);
require(e3.expiration > block.timestamp, InputDeadlinePassed(e3Id, e3.expiration)); // TODO: should we have an input window, including both a start and end timestamp?
bytes memory input;
(input, success) = e3.inputValidator.validate(msg.sender, data);
require(success, InvalidInput());

// TODO: do we need to store or accumulate the inputs? Probably yes.
emit InputPublished(e3Id, input);
}

function publishOutput(uint256 e3Id, bytes memory data) external returns (bool success) {
E3 storage e3 = e3s[e3Id];
function publishComputationOutput(uint256 e3Id, bytes memory data) external returns (bool success) {
E3 memory e3 = getE3(e3Id);
require(e3.expiration <= block.timestamp, InputDeadlineNotPassed(e3Id, e3.expiration));
require(e3.ciphertextOutput.length == 0, CiphertextOutputAlreadyPublished(e3Id));
require(e3.ciphertextOutput.length == 0, CiphertextOutputAlreadyPublished(e3Id)); // TODO: should the output verifier be able to change its mind? i.e. should we be able to call this multiple times?
bytes memory output;
(output, success) = e3.outputVerifier.verify(e3Id, data);
require(success, InvalidOutput());
e3.ciphertextOutput = output;
e3s[e3Id].ciphertextOutput = output;

emit CiphertextOutputPublished(e3Id, output);
}

function publishDecryptedOutput(uint256 e3Id, bytes memory data) external returns (bool success) {
E3 storage e3 = e3s[e3Id];
function publishDecryptionOutput(uint256 e3Id, 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));
bytes memory output;
(output, success) = e3.computationModule.verify(e3Id, data);
e3.plaintextOutput = output;
require(success, InvalidOutput());
e3s[e3Id].plaintextOutput = output;

emit PlaintextOutputPublished(e3Id, output);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/evm/contracts/interfaces/IEnclave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ interface IEnclave {
/// @param e3Id ID of the E3.
/// @param data ABI encoded output data to verify.
/// @return success True if the output was successfully published.
function publishOutput(uint256 e3Id, bytes memory data) external returns (bool success);
function publishComputationOutput(uint256 e3Id, bytes memory data) external returns (bool success);

/// @notice This function should be called to decrypt the output of an Encrypted Execution Environment (E3).
/// @dev This function MUST revert if the output has not been published.
/// @dev This function MUST emit the PlaintextOutputPublished event.
/// @param e3Id ID of the E3.
/// @param data ABI encoded output data to decrypt.
/// @return success True if the output was successfully decrypted.
function publishDecryptedOutput(uint256 e3Id, bytes memory data) external returns (bool success);
function publishDecryptionOutput(uint256 e3Id, bytes memory data) external returns (bool success);

////////////////////////////////////////////////////////////
// //
Expand Down
41 changes: 41 additions & 0 deletions packages/evm/test/encalve/Enclave.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,45 @@ describe("Enclave", function () {
.withArgs(0, e3, 1, this.requestParams.computationModule, this.requestParams.executionModule);
});
});

describe("activate()", function () {
it("reverts if E3 does not exist");
it("reverts if E3 has already been activated");
it("reverts if cypherNodeRegistry does not return a public key");
it("sets committeePublicKey correctly");
it("returns true if E3 is activated successfully");
it("emits E3Activated event");
});

describe("publishInput()", function () {
it("reverts if E3 does not exist");
it("reverts if E3 has not been activated");
it("reverts if outside of input window");
it("reverts if input is not valid");
it("sets ciphertextInput correctly");
it("returns true if input is published successfully");
it("emits InputPublished event");
});

describe("publishComputationOutput()", function () {
it("reverts if E3 does not exist");
it("reverts if E3 has not been activated");
it("reverts if input deadline has not passed");
it("reverts if output has already been published");
it("reverts if output is not valid");
it("sets ciphertextOutput correctly");
it("returns true if output is published successfully");
it("emits CiphertextOutputPublished event");
});

describe("publishExecutionOutput()", function () {
it("reverts if E3 does not exist");
it("reverts if E3 has not been activated");
it("reverts if ciphertextOutput has not been published");
it("reverts if plaintextOutput has already been published");
it("reverts if output is not valid");
it("sets plaintextOutput correctly");
it("returns true if output is published successfully");
it("emits PlaintextOutputPublished event");
});
});

0 comments on commit 9bc9340

Please sign in to comment.