Skip to content

Commit

Permalink
Merge pull request #79 from zama-ai/refactor-test-helpers
Browse files Browse the repository at this point in the history
refactor: updates helpers for reencryption/encryption
  • Loading branch information
PacificYield authored Dec 10, 2024
2 parents d1ceec9 + 7d2426c commit 631d8c0
Show file tree
Hide file tree
Showing 39 changed files with 1,184 additions and 2,532 deletions.
6 changes: 0 additions & 6 deletions CustomProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ethers } from "ethers";
import { ProviderWrapper } from "hardhat/plugins";
import type { EIP1193Provider, RequestArguments } from "hardhat/types";

Expand All @@ -19,11 +18,6 @@ class CustomProvider extends ProviderWrapper implements Test {
}

async request(args: RequestArguments): ReturnType<EIP1193Provider["request"]> {
if (args.method === "eth_estimateGas") {
const estimatedGasLimit = BigInt((await this._wrappedProvider.request(args)) as bigint);
const increasedGasLimit = ethers.toBeHex((estimatedGasLimit * 120n) / 100n); // override estimated gasLimit by 120%, to avoid some edge case with ethermint gas estimation
return increasedGasLimit;
}
if (args.method === "evm_revert") {
const result = await this._wrappedProvider.request(args);
const blockNumberHex = (await this._wrappedProvider.request({ method: "eth_blockNumber" })) as string;
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ inherited contracts.
// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity ^0.8.24;
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
import { SepoliaZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
import { ConfidentialERC20 } from "fhevm-contracts/contracts/token/ERC20/ConfidentialERC20.sol";
contract MyERC20 is MockZamaFHEVMConfig, ConfidentialERC20 {
contract MyERC20 is SepoliaZamaFHEVMConfig, ConfidentialERC20 {
constructor() ConfidentialERC20("MyToken", "MYTOKEN") {
_unsafeMint(1000000, msg.sender);
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/governance/TestConfidentialERC20Votes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
pragma solidity ^0.8.24;

import { ConfidentialERC20Votes } from "../../governance/ConfidentialERC20Votes.sol";
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
import { SepoliaZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";

contract TestConfidentialERC20Votes is MockZamaFHEVMConfig, ConfidentialERC20Votes {
contract TestConfidentialERC20Votes is SepoliaZamaFHEVMConfig, ConfidentialERC20Votes {
constructor(
address owner_,
string memory name_,
Expand Down
6 changes: 3 additions & 3 deletions contracts/test/governance/TestConfidentialGovernorAlpha.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
pragma solidity ^0.8.24;

import { ConfidentialGovernorAlpha } from "../../governance/ConfidentialGovernorAlpha.sol";
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
import { MockZamaGatewayConfig } from "fhevm/config/ZamaGatewayConfig.sol";
import { SepoliaZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
import { SepoliaZamaGatewayConfig } from "fhevm/config/ZamaGatewayConfig.sol";

contract TestConfidentialGovernorAlpha is MockZamaFHEVMConfig, MockZamaGatewayConfig, ConfidentialGovernorAlpha {
contract TestConfidentialGovernorAlpha is SepoliaZamaFHEVMConfig, SepoliaZamaGatewayConfig, ConfidentialGovernorAlpha {
constructor(
address owner_,
address timelock_,
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/token/ERC20/TestConfidentialERC20Mintable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
pragma solidity ^0.8.24;

import { ConfidentialERC20Mintable } from "../../../token/ERC20/extensions/ConfidentialERC20Mintable.sol";
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
import { SepoliaZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";

contract TestConfidentialERC20Mintable is MockZamaFHEVMConfig, ConfidentialERC20Mintable {
contract TestConfidentialERC20Mintable is SepoliaZamaFHEVMConfig, ConfidentialERC20Mintable {
constructor(
string memory name_,
string memory symbol_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pragma solidity ^0.8.24;
import {
ConfidentialERC20WithErrorsMintable
} from "../../../token/ERC20/extensions/ConfidentialERC20WithErrorsMintable.sol";
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
import { SepoliaZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";

contract TestConfidentialERC20WithErrorsMintable is MockZamaFHEVMConfig, ConfidentialERC20WithErrorsMintable {
contract TestConfidentialERC20WithErrorsMintable is SepoliaZamaFHEVMConfig, ConfidentialERC20WithErrorsMintable {
constructor(
string memory name_,
string memory symbol_,
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/utils/TestEncryptedErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pragma solidity ^0.8.24;

import "fhevm/lib/TFHE.sol";
import { EncryptedErrors } from "../../utils/EncryptedErrors.sol";
import { MockZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";
import { SepoliaZamaFHEVMConfig } from "fhevm/config/ZamaFHEVMConfig.sol";

contract TestEncryptedErrors is MockZamaFHEVMConfig, EncryptedErrors {
contract TestEncryptedErrors is SepoliaZamaFHEVMConfig, EncryptedErrors {
constructor(uint8 totalNumberErrorCodes_) EncryptedErrors(totalNumberErrorCodes_) {
for (uint8 i; i <= totalNumberErrorCodes_; i++) {
/// @dev It is not possible to access the _errorCodeDefinitions since it is private.
Expand Down
68 changes: 6 additions & 62 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import "@nomicfoundation/hardhat-toolbox";
import "@openzeppelin/hardhat-upgrades";
import dotenv from "dotenv";
import * as fs from "fs-extra";
import "hardhat-ignore-warnings";
import type { HardhatUserConfig } from "hardhat/config";
import { extendProvider, task } from "hardhat/config";
import { HardhatUserConfig, extendProvider } from "hardhat/config";
import { task } from "hardhat/config";
import type { NetworkUserConfig } from "hardhat/types";
import { resolve } from "path";
import * as path from "path";
import "solidity-docgen";

import CustomProvider from "./CustomProvider";
// Adjust the import path as needed
import "./tasks/accounts";
import "./tasks/getEthereumAddress";
import "./tasks/taskDeploy";
import "./tasks/taskGatewayRelayer";
import "./tasks/taskTFHE";
import { setCodeMocked } from "./test/mockedSetup";

extendProvider(async (provider) => {
const newProvider = new CustomProvider(provider);
Expand All @@ -36,10 +28,7 @@ const dotenvConfigPath: string = process.env.DOTENV_CONFIG_PATH || "./.env";
dotenv.config({ path: resolve(__dirname, dotenvConfigPath) });

// Ensure that we have all the environment variables we need.
const mnemonic: string | undefined = process.env.MNEMONIC;
if (!mnemonic) {
throw new Error("Please set your MNEMONIC in a .env file");
}
const mnemonic: string = process.env.MNEMONIC!;

const chainIds = {
zama: 8009,
Expand Down Expand Up @@ -77,59 +66,14 @@ function getChainConfig(chain: keyof typeof chainIds): NetworkUserConfig {
task("coverage").setAction(async (taskArgs, hre, runSuper) => {
hre.config.networks.hardhat.allowUnlimitedContractSize = true;
hre.config.networks.hardhat.blockGasLimit = 1099511627775;

await runSuper(taskArgs);
});

function replaceImportStatement(filePath: string, oldImport: string, newImport: string): void {
try {
let fileContent = fs.readFileSync(filePath, "utf-8");
fileContent = fileContent.replace(oldImport, newImport);
fs.writeFileSync(filePath, fileContent, "utf-8");
} catch (error) {
console.error(`Error updating file: ${error}`);
}
}

task("test", async (_taskArgs, hre, runSuper) => {
// Run modified test task
if (hre.network.name === "hardhat") {
// in fhevm mode all this block is done when launching the node via `pnmp fhevm:start`
const privKeyGatewayDeployer = process.env.PRIVATE_KEY_GATEWAY_DEPLOYER;
const privKeyFhevmDeployer = process.env.PRIVATE_KEY_FHEVM_DEPLOYER;
await hre.run("task:computeGatewayAddress", { privateKey: privKeyGatewayDeployer });
await hre.run("task:computeACLAddress", { privateKey: privKeyFhevmDeployer });
await hre.run("task:computeTFHEExecutorAddress", { privateKey: privKeyFhevmDeployer });
await hre.run("task:computeKMSVerifierAddress", { privateKey: privKeyFhevmDeployer });
await hre.run("task:computeInputVerifierAddress", { privateKey: privKeyFhevmDeployer, useAddress: false });
await hre.run("task:computeFHEPaymentAddress", { privateKey: privKeyFhevmDeployer });
await hre.run("compile:specific", { contract: "contracts/" });
const sourceDir = path.resolve(__dirname, "node_modules/fhevm-core-contracts/");
const destinationDir = path.resolve(__dirname, "fhevmTemp/");
fs.copySync(sourceDir, destinationDir, { dereference: true });

const sourceDir2 = path.resolve("./node_modules/fhevm/gateway/GatewayContract.sol");
const destinationFilePath = path.join(destinationDir, "GatewayContract.sol");
fs.copySync(sourceDir2, destinationFilePath, { dereference: true });
const oldImport = `import "../lib/TFHE.sol";`;
const newImport = `import "fhevm/lib/TFHE.sol";`;
replaceImportStatement(destinationFilePath, oldImport, newImport);
const sourceDir3 = path.resolve("./node_modules/fhevm/gateway/IKMSVerifier.sol");
const destinationFilePath3 = path.join(destinationDir, "IKMSVerifier.sol");
fs.copySync(sourceDir3, destinationFilePath3, { dereference: true });

await hre.run("compile:specific", { contract: "fhevmTemp/" });
await hre.run("task:faucetToPrivate", { privateKey: privKeyFhevmDeployer });
await hre.run("task:deployACL", { privateKey: privKeyFhevmDeployer });
await hre.run("task:deployTFHEExecutor", { privateKey: privKeyFhevmDeployer });
await hre.run("task:deployKMSVerifier", { privateKey: privKeyFhevmDeployer });
await hre.run("task:deployInputVerifier", { privateKey: privKeyFhevmDeployer });
await hre.run("task:deployFHEPayment", { privateKey: privKeyFhevmDeployer });
await hre.run("task:addSigners", {
numSigners: process.env.NUM_KMS_SIGNERS!,
privateKey: privKeyFhevmDeployer,
useAddress: false,
});
await hre.run("task:launchFhevm", { skipGetCoin: false, useAddress: false });
await setCodeMocked(hre);
}
await runSuper();
});
Expand Down
22 changes: 0 additions & 22 deletions launch-fhevm.sh

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"eslint": "^9.15.0",
"eslint-config-prettier": "^9.1.0",
"ethers": "^6.13.4",
"fhevm": "0.6.0-1",
"fhevm-core-contracts": "0.1.0-2",
"fhevmjs": "^0.6.0-4",
"fhevm": "0.6.0",
"fhevm-core-contracts": "0.6.0",
"fhevmjs": "0.6.0",
"fs-extra": "^11.2.0",
"hardhat": "2.22.14",
"hardhat-gas-reporter": "^1.0.10",
Expand Down
Loading

0 comments on commit 631d8c0

Please sign in to comment.