Skip to content

Commit 8adbb1c

Browse files
feat(contracts): make portal chainId dynamic (#926)
Make portal.chainId() dynamic, rather than immutable. This makes portal contracts fork resistant. task: https://app.asana.com/0/1206208509925075/1207153043060444
1 parent 27b002a commit 8adbb1c

File tree

7 files changed

+148
-79
lines changed

7 files changed

+148
-79
lines changed

contracts/.gas-snapshot

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,45 +62,45 @@ OmniAVS_canRegister_Test:test_canRegister_notOperator() (gas: 119994)
6262
OmniAVS_initialize_Test:test_initialize_defaultParams_succeeds() (gas: 3588147)
6363
OmniAVS_syncWithOmni_Test:test_depositBeaconEth_succeeds() (gas: 630918)
6464
OmniAVS_syncWithOmni_Test:test_unsupportedStrategyDeposit_succeeds() (gas: 1532940)
65-
OmniPortal_admin_Test:test_pause() (gas: 63671684)
65+
OmniPortal_admin_Test:test_pause() (gas: 63672412)
6666
OmniPortal_admin_Test:test_setFeeOracle() (gas: 34843)
6767
OmniPortal_exec_Test:test_execSys_forwardsRevert() (gas: 23177)
68-
OmniPortal_exec_Test:test_exec_aheadOffset_reverts() (gas: 28337)
69-
OmniPortal_exec_Test:test_exec_behindOffset_reverts() (gas: 113108)
70-
OmniPortal_exec_Test:test_exec_errorBytes() (gas: 71926429)
71-
OmniPortal_exec_Test:test_exec_wrongChainId_reverts() (gas: 25871)
72-
OmniPortal_exec_Test:test_exec_xmsgRevert_succeeds() (gas: 70458)
73-
OmniPortal_exec_Test:test_exec_xmsg_succeeds() (gas: 128462)
68+
OmniPortal_exec_Test:test_exec_aheadOffset_reverts() (gas: 28744)
69+
OmniPortal_exec_Test:test_exec_behindOffset_reverts() (gas: 113928)
70+
OmniPortal_exec_Test:test_exec_errorBytes() (gas: 71928464)
71+
OmniPortal_exec_Test:test_exec_wrongChainId_reverts() (gas: 26265)
72+
OmniPortal_exec_Test:test_exec_xmsgRevert_succeeds() (gas: 70865)
73+
OmniPortal_exec_Test:test_exec_xmsg_succeeds() (gas: 128869)
7474
OmniPortal_feeFor_Test:test_feeFor_defaultGasLimit_succeeds() (gas: 36245)
7575
OmniPortal_feeFor_Test:test_feeFor_succeeds() (gas: 32735)
76-
OmniPortal_xcall_Test:test_xcall_defaultGasLimit_succeeds() (gas: 90037)
77-
OmniPortal_xcall_Test:test_xcall_explicitGasLimit_succeeds() (gas: 90428)
76+
OmniPortal_xcall_Test:test_xcall_defaultGasLimit_succeeds() (gas: 90036)
77+
OmniPortal_xcall_Test:test_xcall_explicitGasLimit_succeeds() (gas: 90427)
7878
OmniPortal_xcall_Test:test_xcall_gasLimitTooHigh_reverts() (gas: 56065)
7979
OmniPortal_xcall_Test:test_xcall_gasLimitTooLow_reverts() (gas: 56252)
8080
OmniPortal_xcall_Test:test_xcall_insufficientFee_reverts() (gas: 54233)
81-
OmniPortal_xcall_Test:test_xcall_sameChain_reverts() (gas: 56107)
82-
OmniPortal_xsubmit_Test:test_xsubmit_addValidatorSet_succeeds() (gas: 64353169)
83-
OmniPortal_xsubmit_Test:test_xsubmit_duplicateValidator_reverts() (gas: 63339395)
84-
OmniPortal_xsubmit_Test:test_xsubmit_invalidAttestationRoot_reverts() (gas: 63384584)
85-
OmniPortal_xsubmit_Test:test_xsubmit_invalidMsgs_reverts() (gas: 63371586)
86-
OmniPortal_xsubmit_Test:test_xsubmit_noQuorum_reverts() (gas: 63346188)
87-
OmniPortal_xsubmit_Test:test_xsubmit_noXmsgs_reverts() (gas: 63321487)
88-
OmniPortal_xsubmit_Test:test_xsubmit_notNewValSet_succeeds() (gas: 64325269)
89-
OmniPortal_xsubmit_Test:test_xsubmit_oldValSet_reverts() (gas: 64900648)
90-
OmniPortal_xsubmit_Test:test_xsubmit_uknownValSetId_reverts() (gas: 63329683)
91-
OmniPortal_xsubmit_Test:test_xsubmit_wrongChainId_reverts() (gas: 63426938)
92-
OmniPortal_xsubmit_Test:test_xsubmit_wrongStreamOffset_reverts() (gas: 63418435)
93-
OmniPortal_xsubmit_Test:test_xsubmit_xblock1_chainB_succeeds() (gas: 63602153)
94-
OmniPortal_xsubmit_Test:test_xsubmit_xblock1_succeeds() (gas: 63602244)
95-
OmniPortal_xsubmit_Test:test_xsubmit_xblock2_chainB_succeeds() (gas: 64253495)
96-
OmniPortal_xsubmit_Test:test_xsubmit_xblock2_succeeds() (gas: 64255100)
97-
OmniPortal_xsubmit_gas_Test:test_singleExec() (gas: 63905784)
98-
OmniPortal_xsubmit_gas_Test:test_xsubmi_addValidator_succeeds() (gas: 63520585)
99-
OmniPortal_xsubmit_gas_Test:test_xsubmit_guzzle10_succeeds() (gas: 64568160)
100-
OmniPortal_xsubmit_gas_Test:test_xsubmit_guzzle1_succeeds() (gas: 63536390)
101-
OmniPortal_xsubmit_gas_Test:test_xsubmit_guzzle25_succeeds() (gas: 66288089)
102-
OmniPortal_xsubmit_gas_Test:test_xsubmit_guzzle50_succeeds() (gas: 69155339)
103-
OmniPortal_xsubmit_gas_Test:test_xsubmit_guzzle5_succeeds() (gas: 63995004)
81+
OmniPortal_xcall_Test:test_xcall_sameChain_reverts() (gas: 56102)
82+
OmniPortal_xsubmit_Test:test_xsubmit_addValidatorSet_succeeds() (gas: 64352562)
83+
OmniPortal_xsubmit_Test:test_xsubmit_duplicateValidator_reverts() (gas: 63338241)
84+
OmniPortal_xsubmit_Test:test_xsubmit_invalidAttestationRoot_reverts() (gas: 63383054)
85+
OmniPortal_xsubmit_Test:test_xsubmit_invalidMsgs_reverts() (gas: 63370432)
86+
OmniPortal_xsubmit_Test:test_xsubmit_noQuorum_reverts() (gas: 63345033)
87+
OmniPortal_xsubmit_Test:test_xsubmit_noXmsgs_reverts() (gas: 63320333)
88+
OmniPortal_xsubmit_Test:test_xsubmit_notNewValSet_succeeds() (gas: 64324662)
89+
OmniPortal_xsubmit_Test:test_xsubmit_oldValSet_reverts() (gas: 64898899)
90+
OmniPortal_xsubmit_Test:test_xsubmit_uknownValSetId_reverts() (gas: 63328529)
91+
OmniPortal_xsubmit_Test:test_xsubmit_wrongChainId_reverts() (gas: 63414782)
92+
OmniPortal_xsubmit_Test:test_xsubmit_wrongStreamOffset_reverts() (gas: 63417280)
93+
OmniPortal_xsubmit_Test:test_xsubmit_xblock1_chainB_succeeds() (gas: 63601167)
94+
OmniPortal_xsubmit_Test:test_xsubmit_xblock1_succeeds() (gas: 63601258)
95+
OmniPortal_xsubmit_Test:test_xsubmit_xblock2_chainB_succeeds() (gas: 64252884)
96+
OmniPortal_xsubmit_Test:test_xsubmit_xblock2_succeeds() (gas: 64252957)
97+
OmniPortal_xsubmit_gas_Test:test_singleExec() (gas: 63906962)
98+
OmniPortal_xsubmit_gas_Test:test_xsubmi_addValidator_succeeds() (gas: 63520786)
99+
OmniPortal_xsubmit_gas_Test:test_xsubmit_guzzle10_succeeds() (gas: 64567019)
100+
OmniPortal_xsubmit_gas_Test:test_xsubmit_guzzle1_succeeds() (gas: 63535258)
101+
OmniPortal_xsubmit_gas_Test:test_xsubmit_guzzle25_succeeds() (gas: 66286933)
102+
OmniPortal_xsubmit_gas_Test:test_xsubmit_guzzle50_succeeds() (gas: 69154158)
103+
OmniPortal_xsubmit_gas_Test:test_xsubmit_guzzle5_succeeds() (gas: 63993868)
104104
OmniStake_Test:test_deposit_aboveMaxUint64_reverts() (gas: 17671)
105105
OmniStake_Test:test_deposit_below1Ether_reverts() (gas: 17460)
106106
OmniStake_Test:test_deposit_invalidLength_reverts() (gas: 18042)

contracts/src/protocol/OmniPortal.sol

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@ contract OmniPortal is
2525
OmniPortalConstants,
2626
OmniPortalStorage
2727
{
28-
/**
29-
* @notice Chain ID of the chain to which this portal is deployed
30-
*/
31-
uint64 public immutable chainId;
32-
3328
/**
3429
* @notice Construct the OmniPortal contract
3530
*/
3631
constructor() {
3732
_disableInitializers();
38-
chainId = uint64(block.chainid);
3933
}
4034

4135
/**
@@ -82,6 +76,10 @@ contract OmniPortal is
8276
inXStreamBlockHeight[omniCChainID_] = valSetId;
8377
}
8478

79+
function chainId() public view returns (uint64) {
80+
return uint64(block.chainid);
81+
}
82+
8583
//////////////////////////////////////////////////////////////////////////////
8684
// Outbound xcall functions //
8785
//////////////////////////////////////////////////////////////////////////////
@@ -142,7 +140,7 @@ contract OmniPortal is
142140
require(msg.value >= feeFor(destChainId, data, gasLimit), "OmniPortal: insufficient fee");
143141
require(gasLimit <= xmsgMaxGasLimit, "OmniPortal: gasLimit too high");
144142
require(gasLimit >= xmsgMinGasLimit, "OmniPortal: gasLimit too low");
145-
require(destChainId != chainId, "OmniPortal: no same-chain xcall");
143+
require(destChainId != chainId(), "OmniPortal: no same-chain xcall");
146144
require(destChainId != _BROADCAST_CHAIN_ID, "OmniPortal: no broadcast xcall");
147145
require(to != _VIRTUAL_PORTAL_ADDRESS, "OmniPortal: no portal xcall");
148146

@@ -152,10 +150,10 @@ contract OmniPortal is
152150
}
153151

154152
/**
155-
* @notice Returns true if `chainId` is supported destination chain.
153+
* @notice Returns true if `destChainId` is supported destination chain.
156154
*/
157155
function isSupportedChain(uint64 destChainId) public view returns (bool) {
158-
return destChainId != chainId
156+
return destChainId != chainId()
159157
&& XRegistryBase(xregistry).has(destChainId, XRegistryNames.OmniPortal, Predeploys.PortalRegistry);
160158
}
161159

@@ -245,7 +243,7 @@ contract OmniPortal is
245243
*/
246244
function _exec(XTypes.Msg calldata xmsg_) internal {
247245
require(
248-
xmsg_.destChainId == chainId || xmsg_.destChainId == _BROADCAST_CHAIN_ID, "OmniPortal: wrong destChainId"
246+
xmsg_.destChainId == chainId() || xmsg_.destChainId == _BROADCAST_CHAIN_ID, "OmniPortal: wrong destChainId"
249247
);
250248
require(xmsg_.streamOffset == inXStreamOffset[xmsg_.sourceChainId] + 1, "OmniPortal: wrong streamOffset");
251249

contracts/test/portal/OmniPortal_admin.t.sol

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ contract OmniPortal_admin_Test is Base {
3434
assertFalse(portal.paused());
3535

3636
// xcall with default gas
37+
vm.chainId(thisChainId);
3738
portal.xcall{ value: 1 ether }(chainAId, address(1234), abi.encodeWithSignature("test()"));
3839

3940
// xcall with specified gas
41+
vm.chainId(thisChainId);
4042
portal.xcall{ value: 1 ether }(chainAId, address(1234), abi.encodeWithSignature("test()"), 50_000);
4143

4244
// xsubmit
43-
XTypes.Submission memory xsub1 = readXSubmission("xblock1", portal.chainId());
45+
XTypes.Submission memory xsub1 = readXSubmission({ name: "xblock1", destChainId: thisChainId });
46+
vm.chainId(thisChainId);
4447
portal.xsubmit(xsub1);
4548

4649
// only owner can pause
@@ -54,12 +57,15 @@ contract OmniPortal_admin_Test is Base {
5457

5558
// when paused, cannot xcall and xsubmit
5659
vm.expectRevert("Pausable: paused");
60+
vm.chainId(thisChainId);
5761
portal.xcall(chainAId, address(1234), abi.encodeWithSignature("test()"));
5862

5963
vm.expectRevert("Pausable: paused");
64+
vm.chainId(thisChainId);
6065
portal.xcall(chainAId, address(1234), abi.encodeWithSignature("test()"), 50_000);
6166

6267
vm.expectRevert("Pausable: paused");
68+
vm.chainId(thisChainId);
6369
portal.xsubmit(xsub1);
6470
}
6571
}

contracts/test/portal/OmniPortal_exec.t.sol

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ contract OmniPortal_exec_Test is Base {
2323
vm.prank(relayer);
2424
vm.expectCall(xmsg.to, xmsg.data);
2525
vm.recordLogs();
26+
vm.chainId(xmsg.destChainId);
2627
portal.exec(xmsg);
2728

2829
assertEq(counter.count(), count + 1);
@@ -38,6 +39,7 @@ contract OmniPortal_exec_Test is Base {
3839
vm.prank(relayer);
3940
vm.expectCall(xmsg.to, xmsg.data);
4041
vm.recordLogs();
42+
vm.chainId(xmsg.destChainId);
4143
portal.exec(xmsg);
4244

4345
assertEq(portal.inXStreamOffset(xmsg.sourceChainId), xmsg.streamOffset);
@@ -48,9 +50,11 @@ contract OmniPortal_exec_Test is Base {
4850
function test_exec_wrongChainId_reverts() public {
4951
XTypes.Msg memory xmsg = _inbound_increment(1);
5052

51-
xmsg.destChainId = xmsg.destChainId + 1; // intentionally wrong chainId
53+
uint64 destChainId = xmsg.destChainId;
54+
xmsg.destChainId = destChainId + 1; // intentionally wrong chainId
5255

5356
vm.expectRevert("OmniPortal: wrong destChainId");
57+
vm.chainId(destChainId);
5458
portal.exec(xmsg);
5559
}
5660

@@ -61,16 +65,19 @@ contract OmniPortal_exec_Test is Base {
6165
xmsg.streamOffset = xmsg.streamOffset + 1; // intentionally ahead of offset
6266

6367
vm.expectRevert("OmniPortal: wrong streamOffset");
68+
vm.chainId(xmsg.destChainId);
6469
portal.exec(xmsg);
6570
}
6671

6772
/// @dev Test that exec of an XMsg behind the current offset reverts
6873
function test_exec_behindOffset_reverts() public {
6974
XTypes.Msg memory xmsg = _inbound_increment(1);
7075

76+
vm.chainId(xmsg.destChainId);
7177
portal.exec(xmsg); // execute, to increment offset
7278

7379
vm.expectRevert("OmniPortal: wrong streamOffset");
80+
vm.chainId(xmsg.destChainId);
7481
portal.exec(xmsg);
7582
}
7683

@@ -87,6 +94,7 @@ contract OmniPortal_exec_Test is Base {
8794
});
8895

8996
vm.recordLogs();
97+
vm.chainId(xmsg.destChainId);
9098
portal.exec(xmsg);
9199
Vm.Log[] memory logs = vm.getRecordedLogs();
92100
assertEq(logs.length, 1);
@@ -104,6 +112,7 @@ contract OmniPortal_exec_Test is Base {
104112
});
105113

106114
vm.recordLogs();
115+
vm.chainId(xmsg.destChainId);
107116
portal.exec(xmsg);
108117
logs = vm.getRecordedLogs();
109118
assertEq(logs.length, 1);
@@ -121,6 +130,7 @@ contract OmniPortal_exec_Test is Base {
121130
});
122131

123132
vm.recordLogs();
133+
vm.chainId(xmsg.destChainId);
124134
portal.exec(xmsg);
125135
logs = vm.getRecordedLogs();
126136
assertEq(logs.length, 1);
@@ -138,6 +148,7 @@ contract OmniPortal_exec_Test is Base {
138148
});
139149

140150
vm.recordLogs();
151+
vm.chainId(xmsg.destChainId);
141152
portal.exec(xmsg);
142153
logs = vm.getRecordedLogs();
143154
assertEq(logs.length, 1);
@@ -155,6 +166,7 @@ contract OmniPortal_exec_Test is Base {
155166
});
156167

157168
vm.recordLogs();
169+
vm.chainId(xmsg.destChainId);
158170
portal.exec(xmsg);
159171
logs = vm.getRecordedLogs();
160172
assertEq(logs.length, 1);

0 commit comments

Comments
 (0)