Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make FuelChainState params configurable on deployment #204

Merged
merged 5 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mighty-pens-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fuel-bridge/solidity-contracts': minor
---

FuelChainState is now configurable in deployment scripts
2 changes: 0 additions & 2 deletions docker/l1-chain/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ RUN pnpm compile

# replace the fuel chain consts values and change contract code
ADD ./docker/l1-chain/.fuelChainConsts.env /l1chain/fuel-v2-contracts/.fuelChainConsts.env
ADD ./packages/solidity-contracts/scripts/replaceFuelChainConsts.ts /l1chain/fuel-v2-contracts/scripts/replaceFuelChainConsts.ts
ADD ./packages/solidity-contracts/contracts /l1chain/fuel-v2-contracts/contracts
ADD ./packages/solidity-contracts/deploy /l1chain/fuel-v2-contracts/deploy
ADD ./packages/solidity-contracts/scripts /l1chain/fuel-v2-contracts/scripts
ADD ./packages/solidity-contracts/protocol /l1chain/fuel-v2-contracts/protocol
RUN pnpm ts-node scripts/replaceFuelChainConsts.ts


# remove build dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ contract FuelChainState is Initializable, PausableUpgradeable, AccessControlUpgr
///////////////

/// @dev The commit proccess parameters
//TODO: update these values once block time and commit frequency are finalized
// days worth of commits
// NUM_COMMIT_SLOTS an arbitrary number of commits to store before starting to overwrite
uint256 public constant NUM_COMMIT_SLOTS = 240;
// Number of blocks per commit interval
// BLOCKS_PER_COMMIT_INTERVAL = (num of blocks per minute * target interval in minutes)
uint256 public constant BLOCKS_PER_COMMIT_INTERVAL = 10800;
// Time to fianlize in seconds
// TIME_TO_FINALIZE = target interval in minutes * 60
uint256 public constant TIME_TO_FINALIZE = 10800;
uint32 public constant COMMIT_COOLDOWN = uint32(TIME_TO_FINALIZE) * 8;
// BLOCKS_PER_COMMIT_INTERVAL = (num of blocks per minute (=60) * target interval in minutes)
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
uint256 public immutable BLOCKS_PER_COMMIT_INTERVAL;

// Time after which a commit becomes finalized
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
uint256 public immutable TIME_TO_FINALIZE;

/// Time before a slot can be overwritten
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
uint32 public immutable COMMIT_COOLDOWN;

/// @dev The admin related contract roles
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
Expand All @@ -50,6 +53,9 @@ contract FuelChainState is Initializable, PausableUpgradeable, AccessControlUpgr
// Errors //
////////////

error TimeToFinalizeTooLarge();
error CommitCooldownTooLarge();
error FinalizationIsGtCooldown();
error UnknownBlock();
error CannotRecommit();

Expand All @@ -65,8 +71,27 @@ contract FuelChainState is Initializable, PausableUpgradeable, AccessControlUpgr
/////////////////////////////

/// @notice Constructor disables initialization for the implementation contract
/// @dev assumes 1 block per second in the L2 chain
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
constructor(uint256 timeToFinalize, uint256 blocksPerCommitInterval, uint32 commitCooldown) {
if (timeToFinalize > commitCooldown) {
revert FinalizationIsGtCooldown();
}

uint256 circularBufferSizeInSeconds = NUM_COMMIT_SLOTS * blocksPerCommitInterval;

if (timeToFinalize > circularBufferSizeInSeconds) {
revert TimeToFinalizeTooLarge();
}

if (commitCooldown > circularBufferSizeInSeconds) {
revert CommitCooldownTooLarge();
}

TIME_TO_FINALIZE = timeToFinalize;
COMMIT_COOLDOWN = commitCooldown;
BLOCKS_PER_COMMIT_INTERVAL = blocksPerCommitInterval;

_disableInitializers();
}

Expand Down
11 changes: 10 additions & 1 deletion packages/solidity-contracts/deploy/hardhat/001.chain_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import type { DeployFunction } from 'hardhat-deploy/dist/types';

import { FuelChainState__factory as FuelChainState } from '../../typechain';

const BLOCKS_PER_COMMIT_INTERVAL = 30;
const TIME_TO_FINALIZE = 5;
BarneyChambers marked this conversation as resolved.
Show resolved Hide resolved
const COMMIT_COOLDOWN = TIME_TO_FINALIZE;

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const {
ethers,
Expand All @@ -13,6 +17,11 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {

const contract = await deployProxy(new FuelChainState(deployer), [], {
initializer: 'initialize',
constructorArgs: [
TIME_TO_FINALIZE,
BLOCKS_PER_COMMIT_INTERVAL,
COMMIT_COOLDOWN,
],
});
await contract.waitForDeployment();
const address = await contract.getAddress();
Expand All @@ -21,7 +30,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
console.log('Deployed FuelChainState at', address);
await save('FuelChainState', {
address,
abi: [],
abi: [...FuelChainState.abi],
implementation,
});

Expand Down
6 changes: 5 additions & 1 deletion packages/solidity-contracts/protocol/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export async function deployFuel(
const fuelChainState = await upgrades
.deployProxy(FuelChainState, [], {
initializer: 'initialize',
constructorArgs: [10800, 10800, 10800],
})
.then((tx) => tx.waitForDeployment())
.then((tx) => FuelChainState__factory.connect(tx as any, tx.runner));
Expand Down Expand Up @@ -218,8 +219,11 @@ export async function upgradeFuel(
);
await upgrades.forceImport(contracts.FuelChainState, FuelChainState, {
kind: 'uups',
constructorArgs: [10800, 10800, 10800],
} as any);
await upgrades.upgradeProxy(contracts.FuelChainState, FuelChainState, {
constructorArgs: [10800, 10800, 10800],
});
await upgrades.upgradeProxy(contracts.FuelChainState, FuelChainState);

// Upgrade message portal contract
const FuelMessagePortal = await ethers.getContractFactory(
Expand Down
46 changes: 0 additions & 46 deletions packages/solidity-contracts/scripts/replaceFuelChainConsts.ts

This file was deleted.

16 changes: 15 additions & 1 deletion packages/solidity-contracts/test/erc721Gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import type {
NFT,
} from '../typechain';

import {
BLOCKS_PER_COMMIT_INTERVAL,
TIME_TO_FINALIZE,
COMMIT_COOLDOWN,
} from './utils';
import { createBlock } from './utils/createBlock';

const { expect } = chai;
Expand Down Expand Up @@ -86,7 +91,16 @@ const fixture = deployments.createFixture(

const fuelChainState = (await ethers
.getContractFactory('FuelChainState', deployer)
.then(async (factory) => deployProxy(factory, [], proxyOptions))
.then(async (factory) =>
deployProxy(factory, [], {
...proxyOptions,
constructorArgs: [
TIME_TO_FINALIZE,
BLOCKS_PER_COMMIT_INTERVAL,
COMMIT_COOLDOWN,
],
})
)
.then((tx) => tx.waitForDeployment())) as FuelChainState;

const fuelMessagePortal = (await ethers
Expand Down
12 changes: 11 additions & 1 deletion packages/solidity-contracts/test/messagesIncomingV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { createBlock } from './utils/createBlock';
import type { TreeNode } from './utils/merkle';
import {
BLOCKS_PER_COMMIT_INTERVAL,
COMMIT_COOLDOWN,
TIME_TO_FINALIZE,
generateProof,
getLeafIndexKey,
Expand Down Expand Up @@ -229,7 +230,16 @@ describe('FuelMessagePortalV2 - Incoming messages', () => {

const fuelChainState = (await ethers
.getContractFactory('FuelChainState', deployer)
.then(async (factory) => deployProxy(factory, [], proxyOptions))
.then(async (factory) =>
deployProxy(factory, [], {
...proxyOptions,
constructorArgs: [
TIME_TO_FINALIZE,
BLOCKS_PER_COMMIT_INTERVAL,
COMMIT_COOLDOWN,
],
})
)
.then((tx) => tx.waitForDeployment())) as FuelChainState;

const deployment = await ethers
Expand Down
12 changes: 11 additions & 1 deletion packages/solidity-contracts/test/messagesIncomingV3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { createBlock } from './utils/createBlock';
import type { TreeNode } from './utils/merkle';
import {
BLOCKS_PER_COMMIT_INTERVAL,
COMMIT_COOLDOWN,
TIME_TO_FINALIZE,
generateProof,
getLeafIndexKey,
Expand Down Expand Up @@ -230,7 +231,16 @@ describe('FuelMessagePortalV3 - Incoming messages', () => {

const fuelChainState = (await ethers
.getContractFactory('FuelChainState', deployer)
.then(async (factory) => deployProxy(factory, [], proxyOptions))
.then(async (factory) =>
deployProxy(factory, [], {
...proxyOptions,
constructorArgs: [
TIME_TO_FINALIZE,
BLOCKS_PER_COMMIT_INTERVAL,
COMMIT_COOLDOWN,
],
})
)
.then((tx) => tx.waitForDeployment())) as FuelChainState;

const deployment = await ethers
Expand Down
16 changes: 15 additions & 1 deletion packages/solidity-contracts/test/messagesOutgoingV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import type {
FuelMessagePortalV2,
} from '../typechain';

import {
BLOCKS_PER_COMMIT_INTERVAL,
COMMIT_COOLDOWN,
TIME_TO_FINALIZE,
} from './utils';
import { addressToB256 } from './utils/addressConversion';

const { expect } = chai;
Expand Down Expand Up @@ -60,7 +65,16 @@ describe('FuelMessagesPortalV2 - Outgoing messages', async () => {

const fuelChainState = (await ethers
.getContractFactory('FuelChainState', deployer)
.then(async (factory) => deployProxy(factory, [], proxyOptions))
.then(async (factory) =>
deployProxy(factory, [], {
...proxyOptions,
constructorArgs: [
TIME_TO_FINALIZE,
BLOCKS_PER_COMMIT_INTERVAL,
COMMIT_COOLDOWN,
],
})
)
.then((tx) => tx.waitForDeployment())) as FuelChainState;

const fuelMessagePortalDeployment = await ethers
Expand Down
1 change: 1 addition & 0 deletions packages/solidity-contracts/test/utils/merkle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { computeMessageId } from '../../protocol/message';

// Contract constants
export const TIME_TO_FINALIZE = 10800;
export const COMMIT_COOLDOWN = 10800;
export const BLOCKS_PER_COMMIT_INTERVAL = 10800;

// TODO: should be importable from @fuel-ts/merkle
Expand Down
Loading