Skip to content

Commit 51527d3

Browse files
authored
test(gateway-contracts): include upgrade test for enhanced storage layout case
* test(gateway-contracts): include upgrade test for enhanced storage layout case * chore(gateway-contracts): remove some comments * refactor(gateway-contracts): rename contract upgrade examples * test(gateway-contracts): apply suggested improvements on upgrade tests
1 parent c63d21a commit 51527d3

10 files changed

Lines changed: 360 additions & 308 deletions

gateway-contracts/contracts/examples/CiphertextCommitsUpgradedExample.sol renamed to gateway-contracts/contracts/examples/CiphertextCommitsV2Example.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ pragma solidity ^0.8.24;
44

55
import "../CiphertextCommits.sol";
66

7-
contract CiphertextCommitsUpgradedExample is CiphertextCommits {
7+
contract CiphertextCommitsV2Example is CiphertextCommits {
88
/// @notice Name of the contract
99
string private constant CONTRACT_NAME = "CiphertextCommits";
1010

1111
/// @notice Version of the contract
12-
uint256 private constant MAJOR_VERSION = 0;
13-
uint256 private constant MINOR_VERSION = 2;
12+
uint256 private constant MAJOR_VERSION = 1000;
13+
uint256 private constant MINOR_VERSION = 0;
1414
uint256 private constant PATCH_VERSION = 0;
1515

1616
/// @notice Getter for the name and version of the contract

gateway-contracts/contracts/examples/DecryptionUpgradedExample.sol renamed to gateway-contracts/contracts/examples/DecryptionV2Example.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ pragma solidity ^0.8.24;
44

55
import "../Decryption.sol";
66

7-
contract DecryptionUpgradedExample is Decryption {
7+
contract DecryptionV2Example is Decryption {
88
/// @notice Name of the contract
99
string private constant CONTRACT_NAME = "Decryption";
1010

1111
/// @notice Version of the contract
12-
uint256 private constant MAJOR_VERSION = 0;
13-
uint256 private constant MINOR_VERSION = 2;
12+
uint256 private constant MAJOR_VERSION = 1000;
13+
uint256 private constant MINOR_VERSION = 0;
1414
uint256 private constant PATCH_VERSION = 0;
1515

1616
/// @notice Getter for the name and version of the contract

gateway-contracts/contracts/examples/GatewayConfigUpgradedExample2.sol

Lines changed: 0 additions & 32 deletions
This file was deleted.

gateway-contracts/contracts/examples/GatewayConfigUpgradedExample.sol renamed to gateway-contracts/contracts/examples/GatewayConfigV2Example.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ pragma solidity ^0.8.24;
44

55
import "../GatewayConfig.sol";
66

7-
contract GatewayConfigUpgradedExample is GatewayConfig {
7+
contract GatewayConfigV2Example is GatewayConfig {
88
/// @notice Name of the contract
99
string private constant CONTRACT_NAME = "GatewayConfig";
1010

1111
/// @notice Version of the contract
12-
uint256 private constant MAJOR_VERSION = 0;
13-
uint256 private constant MINOR_VERSION = 2;
12+
uint256 private constant MAJOR_VERSION = 1000;
13+
uint256 private constant MINOR_VERSION = 0;
1414
uint256 private constant PATCH_VERSION = 0;
1515

1616
/// @notice Getter for the name and version of the contract
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// SPDX-License-Identifier: BSD-3-Clause-Clear
2+
pragma solidity ^0.8.24;
3+
4+
import { Ownable2StepUpgradeable } from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
5+
import { UUPSUpgradeable } from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
6+
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
7+
import "../shared/Pausable.sol";
8+
9+
contract GatewayConfigV3Example is Ownable2StepUpgradeable, UUPSUpgradeable, Pausable {
10+
/// @notice Name of the contract
11+
string private constant CONTRACT_NAME = "GatewayConfig";
12+
13+
/// @notice Version of the contract
14+
uint256 private constant MAJOR_VERSION = 1001;
15+
uint256 private constant MINOR_VERSION = 0;
16+
uint256 private constant PATCH_VERSION = 0;
17+
18+
struct ProtocolMetadataV2 {
19+
string name;
20+
string website;
21+
string newField;
22+
}
23+
24+
/// @custom:storage-location erc7201:fhevm_gateway.storage.GatewayConfig
25+
struct GatewayConfigStorage {
26+
address pauser;
27+
mapping(address kmsTxSenderAddress => bool isKmsTxSender) _isKmsTxSender;
28+
mapping(address kmsSignerAddress => bool isKmsSigner) _isKmsSigner;
29+
mapping(address coprocessorTxSenderAddress => bool isCoprocessorTxSender) _isCoprocessorTxSender;
30+
mapping(address coprocessorSignerAddress => bool isCoprocessorSigner) _isCoprocessorSigner;
31+
mapping(uint256 chainId => bool isRegistered) _isHostChainRegistered;
32+
ProtocolMetadata protocolMetadata; // deprecated, use protocolMetadataV2 instead
33+
mapping(address kmsTxSenderAddress => KmsNode kmsNode) kmsNodes;
34+
address[] kmsTxSenderAddresses;
35+
address[] kmsSignerAddresses;
36+
uint256 mpcThreshold;
37+
uint256 publicDecryptionThreshold;
38+
uint256 userDecryptionThreshold;
39+
mapping(address coprocessorTxSenderAddress => Coprocessor coprocessor) coprocessors;
40+
address[] coprocessorTxSenderAddresses;
41+
address[] coprocessorSignerAddresses;
42+
HostChain[] hostChains;
43+
// New state variables added in the upgraded version
44+
ProtocolMetadataV2 protocolMetadataV2;
45+
}
46+
47+
bytes32 private constant GATEWAY_CONFIG_STORAGE_LOCATION =
48+
0x86d3070a8993f6b209bee6185186d38a07fce8bbd97c750d934451b72f35b400;
49+
50+
/// @custom:oz-upgrades-unsafe-allow constructor
51+
constructor() {
52+
_disableInitializers();
53+
}
54+
55+
/// @custom:oz-upgrades-validate-as-initializer
56+
function initialize(string calldata newField) public virtual reinitializer(3) {
57+
__Ownable_init(owner());
58+
__Pausable_init();
59+
60+
// Execute the migration logic to set the new field in the protocol metadata
61+
_migrate(newField);
62+
}
63+
64+
function getProtocolMetadata() external view virtual returns (ProtocolMetadataV2 memory) {
65+
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
66+
return $.protocolMetadataV2;
67+
}
68+
69+
/// @notice Getter for the name and version of the contract
70+
/// @return string representing the name and the version of the contract
71+
function getVersion() external pure virtual returns (string memory) {
72+
return
73+
string(
74+
abi.encodePacked(
75+
CONTRACT_NAME,
76+
" v",
77+
Strings.toString(MAJOR_VERSION),
78+
".",
79+
Strings.toString(MINOR_VERSION),
80+
".",
81+
Strings.toString(PATCH_VERSION)
82+
)
83+
);
84+
}
85+
86+
function _migrate(string calldata newField) private {
87+
GatewayConfigStorage storage $ = _getGatewayConfigStorage();
88+
$.protocolMetadataV2 = ProtocolMetadataV2({
89+
name: $.protocolMetadata.name,
90+
website: $.protocolMetadata.website,
91+
newField: newField
92+
});
93+
}
94+
95+
// solhint-disable-next-line no-empty-blocks
96+
function _authorizeUpgrade(address _newImplementation) internal virtual override {}
97+
98+
function _getGatewayConfigStorage() internal pure returns (GatewayConfigStorage storage $) {
99+
// solhint-disable-next-line no-inline-assembly
100+
assembly {
101+
$.slot := GATEWAY_CONFIG_STORAGE_LOCATION
102+
}
103+
}
104+
}

gateway-contracts/contracts/examples/InputVerificationUpgradedExample.sol renamed to gateway-contracts/contracts/examples/InputVerificationV2Example.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ pragma solidity ^0.8.24;
44

55
import "../InputVerification.sol";
66

7-
contract InputVerificationUpgradedExample is InputVerification {
7+
contract InputVerificationV2Example is InputVerification {
88
/// @notice Name of the contract
99
string private constant CONTRACT_NAME = "InputVerification";
1010

1111
/// @notice Version of the contract
12-
uint256 private constant MAJOR_VERSION = 0;
13-
uint256 private constant MINOR_VERSION = 2;
12+
uint256 private constant MAJOR_VERSION = 1000;
13+
uint256 private constant MINOR_VERSION = 0;
1414
uint256 private constant PATCH_VERSION = 0;
1515

1616
/// @notice Getter for the name and version of the contract

gateway-contracts/contracts/examples/KmsManagementUpgradedExample.sol renamed to gateway-contracts/contracts/examples/KmsManagementV2Example.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ pragma solidity ^0.8.24;
44

55
import "../KmsManagement.sol";
66

7-
contract KmsManagementUpgradedExample is KmsManagement {
7+
contract KmsManagementV2Example is KmsManagement {
88
/// @notice Name of the contract
99
string private constant CONTRACT_NAME = "KmsManagement";
1010

1111
/// @notice Version of the contract
12-
uint256 private constant MAJOR_VERSION = 0;
13-
uint256 private constant MINOR_VERSION = 2;
12+
uint256 private constant MAJOR_VERSION = 1000;
13+
uint256 private constant MINOR_VERSION = 0;
1414
uint256 private constant PATCH_VERSION = 0;
1515

1616
/// @notice Getter for the name and version of the contract

gateway-contracts/contracts/examples/MultichainAclUpgradedExample.sol renamed to gateway-contracts/contracts/examples/MultichainAclV2Example.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ pragma solidity ^0.8.24;
44

55
import "../MultichainAcl.sol";
66

7-
contract MultichainAclUpgradedExample is MultichainAcl {
7+
contract MultichainAclV2Example is MultichainAcl {
88
/// @notice Name of the contract
99
string private constant CONTRACT_NAME = "MultichainAcl";
1010

1111
/// @notice Version of the contract
12-
uint256 private constant MAJOR_VERSION = 0;
13-
uint256 private constant MINOR_VERSION = 2;
12+
uint256 private constant MAJOR_VERSION = 1000;
13+
uint256 private constant MINOR_VERSION = 0;
1414
uint256 private constant PATCH_VERSION = 0;
1515

1616
/// @notice Getter for the name and version of the contract

0 commit comments

Comments
 (0)