Skip to content

Commit

Permalink
Rework L2ChainInfo to explicitly get chain id (since originNetwork is…
Browse files Browse the repository at this point in the history
… different) and manage previous forks
  • Loading branch information
edmundedgar committed Nov 23, 2023
1 parent b1bd67d commit d35a4d1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion contracts/L1GlobalRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract L1GlobalRouter {

// TODO: Can we put the disputeData in ForkingManager in a bytes32?
// Fork results: 0 for the genesis, 1 for yes, 2 for no
bytes memory data = abi.encode(forkonomicToken, arbitrationFee, isL1, disputeContract, disputeContent, forkResult);
bytes memory data = abi.encode(uint64(123), forkonomicToken, arbitrationFee, isL1, disputeContract, disputeContent, forkResult);

IPolygonZkEVMBridge(_bridge).bridgeMessage(
uint32(1),
Expand Down
36 changes: 15 additions & 21 deletions contracts/L2ChainInfo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ contract L2ChainInfo is IBridgeMessageReceiver{
// These should be fixed addresses that never change
address public l2bridge;
address public l1globalRouter;
uint32 public constant L1_NETWORK_ID= 0;
uint32 public constant L1_NETWORK_ID = 0;

uint256 internal chainId;
address internal forkonomicToken;
uint256 internal forkFee;
// uint256 internal totalSupply;
struct ChainInfo{
address forkonomicToken;
uint256 forkFee;
}
mapping(uint256 => ChainInfo) public chainInfo;

// Questions are stored by isL2->forker->id
// The forker is assumed to have created a unique id within itself for the dispute it's forking over
Expand All @@ -38,45 +39,38 @@ contract L2ChainInfo is IBridgeMessageReceiver{
}

modifier isUpToDate {
require(block.chainid == chainId, "Chain ID must be up-to-date");
require(chainInfo[block.chainid].forkonomicToken != address(0), "Current chain must be known");
_;
}

modifier isNotUpToDate {
require(block.chainid != chainId, "Chain ID must be changed");
require(chainInfo[block.chainid].forkonomicToken == address(0), "Current chain must be unknown");
_;
}

function getForkonomicToken() external view isUpToDate returns(address) {
return forkonomicToken;
}

function getChainID() external view isUpToDate returns(uint256) {
return chainId;
return chainInfo[block.chainid].forkonomicToken;
}

function getForkFee() external view isUpToDate returns(uint256) {
return forkFee;
return chainInfo[block.chainid].forkFee;
}

function getForkQuestionResult(bool isL1, address forker, bytes32 questionId) external view isUpToDate returns(bytes32) {
return forkQuestionResults[isL1][forker][questionId];
}

function onMessageReceived(address _originAddress, uint32 _originNetwork, bytes memory _data) external payable isNotUpToDate {
// Get a message from a contract we trust on L1 reporting to us the details of a chain.
// It should only send us the current chain (normally) or a previous chain that's a parent of ours.
function onMessageReceived(address _originAddress, uint32 _originNetwork, bytes memory _data) external payable {

require(msg.sender == l2bridge, "not the expected bridge");
require(_originAddress == l1globalRouter, "only l1globalRouter can call us");
require(_originNetwork == L1_NETWORK_ID, "wrong origin network");

bool isL1;
address forker;
bytes32 questionId;
bytes32 result;

(forkonomicToken, forkFee, isL1, forker, questionId, result) = abi.decode(_data, (address, uint256, bool, address, bytes32, bytes32));
(uint64 chainId, address forkonomicToken, uint256 forkFee, bool isL1, address forker, bytes32 questionId, bytes32 result) = abi.decode(_data, (uint64, address, uint256, bool, address, bytes32, bytes32));

chainId = block.chainid;
chainInfo[uint256(chainId)] = ChainInfo(forkonomicToken, forkFee);

questionToChainID[isL1][forker][questionId] = chainId;
forkQuestionResults[isL1][forker][questionId] = result;
Expand Down
17 changes: 9 additions & 8 deletions test/AdjudicationIntegration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ contract AdjudicationIntegrationTest is Test {
address internal l1ForkingManagerF2 = address(0x2abe13);
address internal l1TokenF2 = address(0x2abe14);

uint32 internal l1chainId = 0;
uint64 internal l2chainIdInit = 1;

uint256 internal forkingFee = 5000; // Should ultimately come from l1 forkingmanager

Expand All @@ -109,8 +109,9 @@ contract AdjudicationIntegrationTest is Test {
// Triggers:
// l2ChainInfo.onMessageReceived(l1GlobalRouter, l1chainId, fakeMessageData);
// In reality this would originate on L1.
bytes memory fakeMessageData = abi.encode(address(l1ForkingManager), uint256(forkingFee), false, address(l2forkArbitrator), bytes32(0x0), bytes32(0x0));
l2Bridge.fakeClaimMessage(address(l1GlobalRouter), uint32(l1chainId), address(l2ChainInfo), fakeMessageData, uint256(0));
vm.chainId(l2chainIdInit);
bytes memory fakeMessageData = abi.encode(l2chainIdInit, address(l1ForkingManager), uint256(forkingFee), false, address(l2forkArbitrator), bytes32(0x0), bytes32(0x0));
l2Bridge.fakeClaimMessage(address(l1GlobalRouter), uint32(0), address(l2ChainInfo), fakeMessageData, uint256(0));

l1realityEth = new ForkableRealityETH_ERC20();
l1realityEth.init(tokenMock, address(0), bytes32(0));
Expand Down Expand Up @@ -365,8 +366,8 @@ contract AdjudicationIntegrationTest is Test {
vm.chainId(newChainId1);

// TODO: Adjust the forkingFee as the total supply has changed a bit
bytes memory fakeMessageData = abi.encode(address(l1ForkingManagerF1), uint256(forkingFee), false, address(l2forkArbitrator), removalQuestionId, bytes32(uint256(1)));
l2Bridge.fakeClaimMessage(address(l1GlobalRouter), uint32(l1chainId), address(l2ChainInfo), fakeMessageData, uint256(0));
bytes memory fakeMessageData = abi.encode(uint64(newChainId1), address(l1ForkingManagerF1), uint256(forkingFee), false, address(l2forkArbitrator), removalQuestionId, bytes32(uint256(1)));
l2Bridge.fakeClaimMessage(address(l1GlobalRouter), uint32(0), address(l2ChainInfo), fakeMessageData, uint256(0));

assertTrue(l2realityEth.isPendingArbitration(removalQuestionId));
l2forkArbitrator.handleCompletedFork(removalQuestionId, lastHistoryHash, lastAnswer, lastAnswerer);
Expand Down Expand Up @@ -408,8 +409,8 @@ contract AdjudicationIntegrationTest is Test {
vm.chainId(newChainId1);

// TODO: Adjust the forkingFee as the total supply has changed a bit
bytes memory fakeMessageData = abi.encode(address(l1ForkingManagerF1), uint256(forkingFee), false, address(l2forkArbitrator), removalQuestionId, bytes32(uint256(0)));
l2Bridge.fakeClaimMessage(address(l1GlobalRouter), uint32(l1chainId), address(l2ChainInfo), fakeMessageData, uint256(0));
bytes memory fakeMessageData = abi.encode(uint64(newChainId1), address(l1ForkingManagerF1), uint256(forkingFee), false, address(l2forkArbitrator), removalQuestionId, bytes32(uint256(0)));
l2Bridge.fakeClaimMessage(address(l1GlobalRouter), uint32(0), address(l2ChainInfo), fakeMessageData, uint256(0));

assertTrue(l2realityEth.isPendingArbitration(removalQuestionId));
l2forkArbitrator.handleCompletedFork(removalQuestionId, lastHistoryHash, lastAnswer, lastAnswerer);
Expand Down Expand Up @@ -459,7 +460,7 @@ contract AdjudicationIntegrationTest is Test {
assertEq(address(l2forkArbitrator).balance, 0);
payable(address(l2Bridge)).transfer(1000000); // Fund it so it can fund the L2ForkArbitrator
bytes memory fakeMessageData = abi.encode(removalQuestionId);
l2Bridge.fakeClaimMessage(address(l1GlobalForkRequester), uint32(l1chainId), address(l2forkArbitrator), fakeMessageData, forkFee);
l2Bridge.fakeClaimMessage(address(l1GlobalForkRequester), uint32(0), address(l2forkArbitrator), fakeMessageData, forkFee);
assertEq(address(l2forkArbitrator).balance, forkFee);

assertFalse(l2forkArbitrator.isForkInProgress(), "Not in forking state");
Expand Down

0 comments on commit d35a4d1

Please sign in to comment.