Skip to content

Commit

Permalink
deplayed forking
Browse files Browse the repository at this point in the history
  • Loading branch information
josojo committed Nov 2, 2023
1 parent 46f9e35 commit f15b4cb
Show file tree
Hide file tree
Showing 3 changed files with 327 additions and 130 deletions.
140 changes: 78 additions & 62 deletions contracts/ForkingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,10 @@ contract ForkingManager is IForkingManager, ForkableStructure {
// Fee that needs to be paid to initiate a fork
uint256 public arbitrationFee;

// Dispute contract and call to identify the dispute
// that will be used to initiate/justify the fork
struct DisputeData {
address disputeContract;
bytes disputeCall;
}

DisputeData public disputeData;

// Struct containing the addresses of the new implementations
struct NewImplementations {
address bridgeImplementation;
address zkEVMImplementation;
address forkonomicTokenImplementation;
address forkingManagerImplementation;
address globalExitRootImplementation;
address verifier;
uint64 forkID;
}

// Struct that holds an address pair used to store the new child contracts
struct AddressPair {
address one;
address two;
}

// Struct containing the addresses of the new instances
struct NewInstances {
AddressPair forkingManager;
AddressPair bridge;
AddressPair zkEVM;
AddressPair forkonomicToken;
AddressPair globalExitRoot;
}
// Counter for new proposals to fork
uint256 public proposalCounter = 0;
// mapping to store the fork proposal data
mapping(uint256 => ForkProposal) public forkProposals;

/// @inheritdoc IForkingManager
function initialize(
Expand All @@ -80,7 +50,8 @@ contract ForkingManager is IForkingManager, ForkableStructure {
address _parentContract,
address _globalExitRoot,
uint256 _arbitrationFee,
address _chainIdManager
address _chainIdManager,
ForkProposal[] calldata proposals
) external initializer {
zkEVM = _zkEVM;
bridge = _bridge;
Expand All @@ -90,24 +61,56 @@ contract ForkingManager is IForkingManager, ForkableStructure {
arbitrationFee = _arbitrationFee;
chainIdManager = _chainIdManager;
ForkableStructure.initialize(address(this), _parentContract);
for (uint i = 0; i < proposals.length; i++) {
forkProposals[i] = proposals[i];
}
proposalCounter = proposals.length;
}

/**
* @notice function to initiate and execute the fork
* @param _disputeData the dispute contract and call to identify the dispute
* @notice function to initiate and schedule the fork
* @param disputeData the dispute contract and call to identify the dispute
* @param newImplementations the addresses of the new implementations that will
* be used to create the second children of the contracts
* @param preparationTime is the duration until when the fork can be executed
* @return counter: A index of the fork proposal
*/
function initiateFork(
DisputeData memory _disputeData,
NewImplementations calldata newImplementations
) external onlyBeforeForking {
DisputeData memory disputeData,
NewImplementations calldata newImplementations,
uint256 preparationTime
) external onlyBeforeForking returns (uint256) {
// Charge the forking fee
IERC20(forkonomicToken).safeTransferFrom(
msg.sender,
address(this),
arbitrationFee
);
uint256 counter = proposalCounter;
// Store the dispute contract and call to identify the dispute
forkProposals[counter] = ForkProposal({
disputeData: disputeData,
proposedImplementations: newImplementations,
// solhint-disable-next-line not-rely-on-time
executionTime: block.timestamp + preparationTime
});
proposalCounter = counter + 1;
return counter;
}

/**
* @dev function that executes a fork proposal
* @param counter the counter that was given while creating the fork proposal
*/
function executeFork(uint256 counter) external onlyBeforeForking {
require(
forkProposals[counter].executionTime != 0 &&
// solhint-disable-next-line not-rely-on-time
forkProposals[counter].executionTime <= block.timestamp,
"ForkingManager: fork not ready"
);
NewImplementations memory newImplementations = forkProposals[counter]
.proposedImplementations;

// Create the children of each contract
NewInstances memory newInstances;
(
Expand Down Expand Up @@ -236,24 +239,40 @@ contract ForkingManager is IForkingManager, ForkableStructure {
);

//Initialize the forking manager contracts
IForkingManager(newInstances.forkingManager.one).initialize(
newInstances.zkEVM.one,
newInstances.bridge.one,
newInstances.forkonomicToken.one,
address(this),
newInstances.globalExitRoot.one,
arbitrationFee,
chainIdManager
);
IForkingManager(newInstances.forkingManager.two).initialize(
newInstances.zkEVM.two,
newInstances.zkEVM.two,
newInstances.forkonomicToken.two,
address(this),
newInstances.globalExitRoot.two,
arbitrationFee,
chainIdManager
);
{
ForkProposal[] memory proposals = new ForkProposal[](
proposalCounter - 1
);
{
uint256 skipAddition = 0;
for (uint i = 0; i < proposalCounter - 1; i++) {
if (i == counter) {
skipAddition = 1;
}
proposals[i] = forkProposals[i + skipAddition];
}
}
IForkingManager(newInstances.forkingManager.one).initialize(
newInstances.zkEVM.one,
newInstances.bridge.one,
newInstances.forkonomicToken.one,
address(this),
newInstances.globalExitRoot.one,
arbitrationFee,
chainIdManager,
proposals
);
IForkingManager(newInstances.forkingManager.two).initialize(
newInstances.zkEVM.two,
newInstances.zkEVM.two,
newInstances.forkonomicToken.two,
address(this),
newInstances.globalExitRoot.two,
arbitrationFee,
chainIdManager,
proposals
);
}

//Initialize the global exit root contracts
IForkableGlobalExitRoot(newInstances.globalExitRoot.one).initialize(
Expand All @@ -268,8 +287,5 @@ contract ForkingManager is IForkingManager, ForkableStructure {
newInstances.zkEVM.two,
newInstances.bridge.two
);

// Store the dispute contract and call to identify the dispute
disputeData = _disputeData;
}
}
43 changes: 42 additions & 1 deletion contracts/interfaces/IForkingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,54 @@ pragma solidity ^0.8.20;
import {IForkableStructure} from "./IForkableStructure.sol";

interface IForkingManager is IForkableStructure {
// Dispute contract and call to identify the dispute
// that will be used to initiate/justify the fork
struct DisputeData {
address disputeContract;
bytes disputeCall;
}

// Struct containing the addresses of the new implementations
struct NewImplementations {
address bridgeImplementation;
address zkEVMImplementation;
address forkonomicTokenImplementation;
address forkingManagerImplementation;
address globalExitRootImplementation;
address verifier;
uint64 forkID;
}

// Struct that holds an address pair used to store the new child contracts
struct AddressPair {
address one;
address two;
}

// Struct containing the addresses of the new instances
struct NewInstances {
AddressPair forkingManager;
AddressPair bridge;
AddressPair zkEVM;
AddressPair forkonomicToken;
AddressPair globalExitRoot;
}

// Struct containing the data for the paid fork
struct ForkProposal {
DisputeData disputeData;
NewImplementations proposedImplementations;
uint256 executionTime;
}

function initialize(
address _zkEVM,
address _bridge,
address _forkonomicToken,
address _parentContract,
address _globalExitRoot,
uint256 _arbitrationFee,
address _chainIdManager
address _chainIdManager,
ForkProposal[] memory proposals
) external;
}
Loading

0 comments on commit f15b4cb

Please sign in to comment.