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

Improve upgrade initializer call #1010

Merged
merged 2 commits into from
Nov 16, 2023
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
12 changes: 5 additions & 7 deletions contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ contract Gateway is IGateway, IInitializable {
revert InvalidCodeHash();
}

// Verify that the code in the implementation contract was not changed
// after the upgrade initiated on BridgeHub parachain.
// As a sanity check, ensure that the codehash of implementation contract
// matches the codehash in the upgrade proposal
if (params.impl.codehash != params.implCodeHash) {
revert InvalidCodeHash();
}
Expand All @@ -384,11 +384,9 @@ contract Gateway is IGateway, IInitializable {

// Apply the initialization function of the implementation only if params were provided
if (params.initParams.length > 0) {
(bool success,) =
params.impl.delegatecall(abi.encodeWithSelector(this.initialize.selector, params.initParams));
if (!success) {
revert InitializationFailed();
}
(bool success, bytes memory returndata) =
params.impl.delegatecall(abi.encodeCall(IInitializable.initialize, params.initParams));
Call.verifyResult(success, returndata);
}

emit Upgraded(params.impl);
Expand Down
10 changes: 5 additions & 5 deletions contracts/src/GatewayProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pragma solidity 0.8.22;

import {ERC1967} from "./utils/ERC1967.sol";
import {Call} from "./utils/Call.sol";
import {IInitializable} from "./interfaces/IInitializable.sol";

contract GatewayProxy is IInitializable {
Expand All @@ -14,13 +15,12 @@ contract GatewayProxy is IInitializable {
ERC1967.store(implementation);
// Initialize storage by calling the implementation's `initialize(bytes)` function
// using `delegatecall`.
(bool success,) = implementation.delegatecall(abi.encodeCall(IInitializable.initialize, params));
if (!success) {
revert InitializationFailed();
}
(bool success, bytes memory returndata) =
implementation.delegatecall(abi.encodeCall(IInitializable.initialize, params));
Call.verifyResult(success, returndata);
}

// Prevent fallback() from calling `initialize(bytes)` on the implementation contract
// Prevent fallback() from calling `IInitializable.initialize(bytes)` on the implementation contract
function initialize(bytes calldata) external pure {
revert Unauthorized();
}
Expand Down
4 changes: 1 addition & 3 deletions contracts/src/interfaces/IInitializable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
pragma solidity 0.8.22;

/**
* @title Initialization of gateway contracts
* @title Initialization of gateway logic contracts
*/
interface IInitializable {
error InitializationFailed();

function initialize(bytes calldata data) external;
}
2 changes: 1 addition & 1 deletion contracts/test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ contract GatewayTest is Test {
initParams: abi.encode(666)
});

vm.expectRevert(IInitializable.InitializationFailed.selector);
vm.expectRevert("initialize failed");
GatewayMock(address(gateway)).upgradePublic(abi.encode(params));
}

Expand Down