Skip to content

Commit

Permalink
feat: made more contracts upgradable, fixed state variable init, nonc…
Browse files Browse the repository at this point in the history
…e arithmetics in deploy scripts
  • Loading branch information
jaybuidl committed Sep 26, 2023
1 parent 487fc5b commit 4467927
Show file tree
Hide file tree
Showing 16 changed files with 251 additions and 105 deletions.
7 changes: 2 additions & 5 deletions contracts/deploy/00-home-chain-arbitrable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import disputeTemplate from "../test/fixtures/DisputeTemplate.simple.json";
import { HomeChains, isSkipped } from "./utils";
import { deployUpgradable } from "./utils/deployUpgradable";

const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { deployments, getNamedAccounts, getChainId } = hre;
Expand All @@ -17,11 +18,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
"0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003"; // General court, 3 jurors
const weth = await deployments.get("WETH");

const disputeTemplateRegistry = await deploy("DisputeTemplateRegistry", {
from: deployer,
args: [],
log: true,
});
const disputeTemplateRegistry = await deployUpgradable(hre, deployer, "DisputeTemplateRegistry", [deployer]);

await deploy("ArbitrableExample", {
from: deployer,
Expand Down
14 changes: 5 additions & 9 deletions contracts/deploy/00-home-chain-arbitration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
randomizerByChain.set(HomeChains[HomeChains[chainId]], randomizerMock.address);
}

await deploy("PolicyRegistry", {
from: deployer,
args: [deployer],
log: true,
});
await deployUpgradable(hre, deployer, "PolicyRegistry", [deployer]);

const randomizer = randomizerByChain.get(Number(await getChainId())) ?? AddressZero;
const rng = await deployUpgradable(hre, deployer, "RandomizerRNG", [randomizer, deployer]);
Expand All @@ -66,8 +62,8 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
let klerosCoreAddress = await deployments.getOrNull("KlerosCore").then((deployment) => deployment?.address);
if (!klerosCoreAddress) {
const nonce = await ethers.provider.getTransactionCount(deployer);
klerosCoreAddress = getContractAddress(deployer, nonce + 3); // Deploying an upgradeable version of SortitionModule requires 2 transactions instead of 1 (implementation then proxy)
console.log("calculated future KlerosCore address for nonce %d: %s", nonce, klerosCoreAddress);
klerosCoreAddress = getContractAddress(deployer, nonce + 3); // deployed on the 4th tx (nonce+3): SortitionModule Impl tx, SortitionModule Proxy tx, KlerosCore Impl tx, KlerosCore Proxy tx
console.log("calculated future KlerosCore address for nonce %d: %s", nonce + 3, klerosCoreAddress);
}

const sortitionModule = await deployUpgradable(hre, deployer, "SortitionModule", [
Expand All @@ -77,7 +73,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
1800, // maxFreezingTime
rng.address,
RNG_LOOKAHEAD,
]);
]); // nonce (implementation), nonce+1 (proxy)

const pnk = pnkByChain.get(chainId) ?? AddressZero;
const dai = daiByChain.get(chainId) ?? AddressZero;
Expand All @@ -95,7 +91,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
[0, 0, 0, 10], // evidencePeriod, commitPeriod, votePeriod, appealPeriod
ethers.utils.hexlify(5), // Extra data for sortition module will return the default value of K
sortitionModule.address,
]);
]); // nonce+2 (implementation), nonce+3 (proxy)

// execute DisputeKitClassic.changeCore() only if necessary
const currentCore = await hre.ethers.getContractAt("DisputeKitClassic", disputeKit.address).then((dk) => dk.core());
Expand Down
24 changes: 14 additions & 10 deletions contracts/deploy/01-foreign-gateway-on-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import getContractAddress from "./utils/getContractAddress";
import { KlerosCore__factory } from "../typechain-types";
import { ForeignChains, HardhatChain, isSkipped } from "./utils";
import { ForeignChains, isSkipped } from "./utils";
import { deployUpgradable } from "./utils/deployUpgradable";

const deployForeignGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const { ethers, deployments, getNamedAccounts, getChainId, config } = hre;
Expand All @@ -24,7 +25,7 @@ const deployForeignGateway: DeployFunction = async (hre: HardhatRuntimeEnvironme
// TODO: use deterministic deployments
const homeChainProvider = new ethers.providers.JsonRpcProvider(homeNetworks[ForeignChains[chainId]].url);
let nonce = await homeChainProvider.getTransactionCount(deployer);
nonce += 2; // HomeGatewayToEthereum deploy tx will the third tx after this on its home network, so we add two to the current nonce.
nonce += 1; // HomeGatewayToEthereum Proxy deploy tx will be the 2nd tx after this on its home network, so we add 1 to the current nonce.
const homeGatewayAddress = getContractAddress(deployer, nonce);
console.log("Calculated future HomeGatewayToEthereum address for nonce %d: %s", nonce, homeGatewayAddress);

Expand All @@ -33,21 +34,24 @@ const deployForeignGateway: DeployFunction = async (hre: HardhatRuntimeEnvironme

const homeChainId = (await homeChainProvider.getNetwork()).chainId;
const homeChainIdAsBytes32 = hexZeroPad(hexlify(homeChainId), 32);
await deploy("ForeignGatewayOnEthereum", {
from: deployer,
contract: "ForeignGateway",
args: [deployer, veaOutbox.address, homeChainIdAsBytes32, homeGatewayAddress],
gasLimit: 4000000,
log: true,
});
await deployUpgradable(
hre,
deployer,
"ForeignGatewayOnEthereum",
[deployer, veaOutbox.address, homeChainIdAsBytes32, homeGatewayAddress],
{
contract: "ForeignGateway",
gasLimit: 4000000,
}
);

// TODO: disable the gateway until fully initialized with the correct fees OR allow disputeCreators to add funds again if necessary.
const coreDeployment = await hre.companionNetworks.home.deployments.get("KlerosCore");
const core = await KlerosCore__factory.connect(coreDeployment.address, homeChainProvider);
// TODO: set up the correct fees for the FORKING_COURT
const courtId = await core.GENERAL_COURT();
const fee = (await core.courts(courtId)).feeForJuror;
await execute("ForeignGatewayOnGnosis", { from: deployer, log: true }, "changeCourtJurorFee", courtId, fee);
await execute("ForeignGatewayOnEthereum", { from: deployer, log: true }, "changeCourtJurorFee", courtId, fee);
// TODO: set up the correct fees for the lower courts
};

Expand Down
25 changes: 14 additions & 11 deletions contracts/deploy/01-foreign-gateway-on-gnosis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DeployFunction } from "hardhat-deploy/types";
import getContractAddress from "./utils/getContractAddress";
import { KlerosCore__factory } from "../typechain-types";
import { ForeignChains, isSkipped } from "./utils";
import { deployUpgradable } from "./utils/deployUpgradable";

const ONE_GWEI = parseUnits("1", "gwei");

Expand All @@ -26,9 +27,8 @@ const deployForeignGateway: DeployFunction = async (hre: HardhatRuntimeEnvironme
// Hack to predict the deployment address on the home chain.
// TODO: use deterministic deployments
const homeChainProvider = new ethers.providers.JsonRpcProvider(homeNetworks[ForeignChains[chainId]].url);
const nonce = await homeChainProvider.getTransactionCount(deployer);

// FIXME: this computed address is wrong for deploys to testnets, okay on Hardhat
let nonce = await homeChainProvider.getTransactionCount(deployer);
nonce += 1; // HomeGatewayToEthereum Proxy deploy tx will be the 2nd tx after this on its home network, so we add 1 to the current nonce.
const homeGatewayAddress = getContractAddress(deployer, nonce); // HomeGateway deploy tx will be the next tx home network
console.log("Calculated future HomeGatewayToEthereum address for nonce %d: %s", nonce, homeGatewayAddress);

Expand All @@ -37,14 +37,17 @@ const deployForeignGateway: DeployFunction = async (hre: HardhatRuntimeEnvironme

const homeChainId = (await homeChainProvider.getNetwork()).chainId;
const homeChainIdAsBytes32 = hexZeroPad(hexlify(homeChainId), 32);
await deploy("ForeignGatewayOnGnosis", {
from: deployer,
contract: "ForeignGateway",
args: [deployer, veaOutbox.address, homeChainIdAsBytes32, homeGatewayAddress],
log: true,
maxFeePerGas: ONE_GWEI,
maxPriorityFeePerGas: ONE_GWEI,
});
await deployUpgradable(
hre,
deployer,
"ForeignGatewayOnGnosis",
[deployer, veaOutbox.address, homeChainIdAsBytes32, homeGatewayAddress],
{
contract: "ForeignGateway",
maxFeePerGas: ONE_GWEI,
maxPriorityFeePerGas: ONE_GWEI,
}
);

// TODO: disable the gateway until fully initialized with the correct fees OR allow disputeCreators to add funds again if necessary.
const coreDeployment = await hre.companionNetworks.home.deployments.get("KlerosCore");
Expand Down
14 changes: 8 additions & 6 deletions contracts/deploy/02-home-gateway-to-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import { ethers } from "hardhat";
import { HardhatChain, HomeChains, isSkipped } from "./utils";
import { deployUpgradable } from "./utils/deployUpgradable";

// TODO: use deterministic deployments

Expand All @@ -22,19 +23,20 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
const foreignChainName = await hre.companionNetworks.foreignGoerli.deployments.getNetworkName();
console.log("Using ForeignGateway %s on chainId %s (%s)", foreignGateway.address, foreignChainId, foreignChainName);

await deploy("HomeGatewayToEthereum", {
from: deployer,
contract: "HomeGateway",
args: [
await deployUpgradable(
hre,
deployer,
"HomeGatewayToEthereum",
[
deployer,
klerosCore.address,
veaInbox.address,
foreignChainId,
foreignGateway.address,
ethers.constants.AddressZero, // feeToken is ETH
],
log: true,
}); // nonce+0
{ contract: "HomeGateway" }
); // nonce+0
};

deployHomeGateway.tags = ["HomeGatewayToEthereum"];
Expand Down
14 changes: 8 additions & 6 deletions contracts/deploy/02-home-gateway-to-gnosis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";
import { HardhatChain, HomeChains, isSkipped } from "./utils";
import { deployUpgradable } from "./utils/deployUpgradable";

// TODO: use deterministic deployments

Expand All @@ -22,12 +23,13 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
const foreignChainName = await hre.companionNetworks.foreignChiado.deployments.getNetworkName();
console.log("Using ForeignGateway %s on chainId %s (%s)", foreignGateway.address, foreignChainId, foreignChainName);

await deploy("HomeGatewayToGnosis", {
from: deployer,
contract: "HomeGateway",
args: [deployer, klerosCore.address, veaInbox.address, foreignChainId, foreignGateway.address, dai.address],
log: true,
}); // nonce+0
await deployUpgradable(
hre,
deployer,
"HomeGatewayToGnosis",
[deployer, klerosCore.address, veaInbox.address, foreignChainId, foreignGateway.address, dai.address],
{ contract: "HomeGateway" }
); // nonce+0
};

deployHomeGateway.tags = ["HomeGatewayToGnosis"];
Expand Down
48 changes: 27 additions & 21 deletions contracts/deploy/03-vea-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import getContractAddress from "./utils/getContractAddress";
import { KlerosCore__factory } from "../typechain-types";
import disputeTemplate from "../test/fixtures/DisputeTemplate.simple.json";
import { HardhatChain, isSkipped } from "./utils";
import { deployUpgradable } from "./utils/deployUpgradable";

// TODO: use deterministic deployments

Expand All @@ -23,33 +24,42 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
log: true,
});

const nonce = await ethers.provider.getTransactionCount(deployer);
const homeGatewayAddress = getContractAddress(deployer, nonce + 1);
let nonce = await ethers.provider.getTransactionCount(deployer);
nonce += 3; // deployed on the 4th tx (nonce+3): SortitionModule Impl tx, SortitionModule Proxy tx, KlerosCore Impl tx, KlerosCore Proxy tx
const homeGatewayAddress = getContractAddress(deployer, nonce);
console.log("Calculated future HomeGatewayToEthereum address for nonce %d: %s", nonce, homeGatewayAddress);

const homeChainIdAsBytes32 = hexZeroPad(hexlify(HardhatChain.HARDHAT), 32);
const foreignGateway = await deploy("ForeignGatewayOnEthereum", {
from: deployer,
contract: "ForeignGateway",
args: [deployer, vea.address, homeChainIdAsBytes32, homeGatewayAddress],
gasLimit: 4000000,
log: true,
}); // nonce+0
const foreignGateway = await deployUpgradable(
hre,
deployer,
"ForeignGatewayOnEthereum",
[deployer, vea.address, homeChainIdAsBytes32, homeGatewayAddress],
{
contract: "ForeignGateway",
gasLimit: 4000000,
}
); // nonce (implementation), nonce+1 (proxy)
console.log("foreignGateway.address: ", foreignGateway.address);

await deploy("HomeGatewayToEthereum", {
from: deployer,
contract: "HomeGateway",
args: [
await deployUpgradable(
hre,
deployer,
"HomeGatewayToEthereum",
[
deployer,
klerosCore.address,
vea.address,
HardhatChain.HARDHAT,
foreignGateway.address,
ethers.constants.AddressZero, // feeToken
],
gasLimit: 4000000,
log: true,
}); // nonce+1
{
contract: "HomeGateway",
gasLimit: 4000000,
log: true,
}
); // nonce+2 (implementation), nonce+3 (proxy)

// TODO: disable the gateway until fully initialized with the correct fees OR allow disputeCreators to add funds again if necessary.
const signer = (await hre.ethers.getSigners())[0];
Expand All @@ -60,11 +70,7 @@ const deployHomeGateway: DeployFunction = async (hre: HardhatRuntimeEnvironment)
await execute("ForeignGatewayOnEthereum", { from: deployer, log: true }, "changeCourtJurorFee", courtId, fee);
// TODO: set up the correct fees for the lower courts

const disputeTemplateRegistry = await deploy("DisputeTemplateRegistry", {
from: deployer,
args: [],
log: true,
});
const disputeTemplateRegistry = await deployUpgradable(hre, deployer, "DisputeTemplateRegistry", [deployer]);

// TODO: debug why this extraData fails but "0x00" works
// const extraData =
Expand Down
6 changes: 4 additions & 2 deletions contracts/deploy/utils/deployUpgradable.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { DeployResult } from "hardhat-deploy/types";
import { DeployResult, DeployOptions } from "hardhat-deploy/types";
import { HardhatRuntimeEnvironment } from "hardhat/types";

export function deployUpgradable(
hre: HardhatRuntimeEnvironment,
deployer: string,
contract: string,
params: any[]
params: any[],
deployOptions?: Omit<DeployOptions, "from">
): Promise<DeployResult> {
const { deploy } = hre.deployments;
return deploy(contract, {
Expand All @@ -27,5 +28,6 @@ export function deployUpgradable(
},
},
log: true,
...deployOptions,
});
}
57 changes: 55 additions & 2 deletions contracts/src/arbitration/DisputeTemplateRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,70 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

import "../proxy/UUPSProxiable.sol";
import "../proxy/Initializable.sol";
import "./interfaces/IDisputeTemplateRegistry.sol";

/// @title Dispute Template Registry
/// @dev A contract to maintain a registry of dispute templates.
contract DisputeTemplateRegistry is IDisputeTemplateRegistry {
uint256 public templates;
contract DisputeTemplateRegistry is IDisputeTemplateRegistry, UUPSProxiable, Initializable {
// ************************************* //
// * Storage * //
// ************************************* //

address public governor; // The address that can withdraw funds.
uint256 public templates; // The number of templates.

// ************************************* //
// * Function Modifiers * //
// ************************************* //

modifier onlyByGovernor() {
require(governor == msg.sender, "Governor only");
_;
}

// ************************************* //
// * Constructor * //
// ************************************* //

/// @dev Constructor, initializing the implementation to reduce attack surface.
constructor() {
_disableInitializers();
}

/// @dev Initializer
/// @param _governor Governor of the contract.
function initialize(address _governor) external reinitializer(1) {
governor = _governor;
}

// ************************ //
// * Governance * //
// ************************ //

/**
* @dev Access Control to perform implementation upgrades (UUPS Proxiable)
* @dev Only the governor can perform upgrades (`onlyByGovernor`)
*/
function _authorizeUpgrade(address) internal view override onlyByGovernor {
// NOP
}

/// @dev Changes the governor of the contract.
/// @param _governor The new governor.
function changeGovernor(address _governor) external onlyByGovernor {
governor = _governor;
}

// ************************************* //
// * State Modifiers * //
// ************************************* //

/// @dev Registers a new dispute template.
/// @param _templateTag The tag of the template (optional).
/// @param _templateData The data of the template.
/// @param _templateDataMappings The data mappings of the template.
function setDisputeTemplate(
string memory _templateTag,
string memory _templateData,
Expand Down
Loading

0 comments on commit 4467927

Please sign in to comment.