From b961a953bbd7aa384490a332da477d0812fd8232 Mon Sep 17 00:00:00 2001 From: Vadim Yavorsky Date: Wed, 20 Nov 2024 16:44:54 +0000 Subject: [PATCH 1/9] Fix bug in initialize function --- contracts/test/TestContractManager.sol | 14 ++++++++++++-- migrations/upgradeMainnet.ts | 9 ++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/contracts/test/TestContractManager.sol b/contracts/test/TestContractManager.sol index aca6a39bf..6ce06bcb4 100644 --- a/contracts/test/TestContractManager.sol +++ b/contracts/test/TestContractManager.sol @@ -64,7 +64,17 @@ contract ContractManager is IContractManagerTester { /** * @dev Returns the contract address for a given contractName. */ - function getContract(string memory contractName) external view override returns (address) { - return contracts[keccak256(abi.encodePacked(contractName))]; + function getContract( + string memory name + ) + public + view + override + returns (address contractAddress) + { + contractAddress = contracts[keccak256(abi.encodePacked(name))]; + if (contractAddress == address(0)) { + revert(name.strConcat(" contract has not been found")); + } } } diff --git a/migrations/upgradeMainnet.ts b/migrations/upgradeMainnet.ts index 226646d10..94386d2d0 100644 --- a/migrations/upgradeMainnet.ts +++ b/migrations/upgradeMainnet.ts @@ -109,17 +109,20 @@ class ImaMainnetUpgrader extends Upgrader { console.log(`Address of ${contractName} is set to ${contractAddress}`); } catch { // getContract failed because the contract is not set - const contractAddress = await this.instance.getContract(contractName); + const contract = await this.instance.getContract(contractName); this.transactions.push(Transaction.from( { to: await contractManager.getAddress(), data: contractManager.interface.encodeFunctionData( "setContractsAddress", - [contractAddress] + [ + contractName, + await contract.getAddress() + ] ) } )) - console.log(`Set ${contractName} address to ${contractAddress}`); + console.log(`Set ${contractName} address to ${await contract.getAddress()}`); } } }; From e0803b4d17d917dff081657b259df386fbad2e36 Mon Sep 17 00:00:00 2001 From: Vadim Yavorsky Date: Wed, 20 Nov 2024 16:47:48 +0000 Subject: [PATCH 2/9] Fix bug --- contracts/test/TestContractManager.sol | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/contracts/test/TestContractManager.sol b/contracts/test/TestContractManager.sol index 6ce06bcb4..036b7b6c3 100644 --- a/contracts/test/TestContractManager.sol +++ b/contracts/test/TestContractManager.sol @@ -73,8 +73,6 @@ contract ContractManager is IContractManagerTester { returns (address contractAddress) { contractAddress = contracts[keccak256(abi.encodePacked(name))]; - if (contractAddress == address(0)) { - revert(name.strConcat(" contract has not been found")); - } + require(contractAddress != address(0), "Contract has not been found"); } } From 9e2e14c31ea093d5f19bb85f7a86139bf4942c90 Mon Sep 17 00:00:00 2001 From: Vadim Yavorsky Date: Wed, 20 Nov 2024 17:04:38 +0000 Subject: [PATCH 3/9] Fix tests --- .../deploy/mainnet/messageProxyForMainnet.ts | 7 ++--- .../skale-manager-utils/contractManager.ts | 26 ++++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/test/utils/deploy/mainnet/messageProxyForMainnet.ts b/test/utils/deploy/mainnet/messageProxyForMainnet.ts index e169c9312..a14a6b6a3 100644 --- a/test/utils/deploy/mainnet/messageProxyForMainnet.ts +++ b/test/utils/deploy/mainnet/messageProxyForMainnet.ts @@ -7,11 +7,12 @@ export async function deployMessageProxyForMainnet( contractManager: ContractManager ) { const factory = await ethers.getContractFactory(name); - if (await contractManager.getContract(name) !== "0x0000000000000000000000000000000000000000") { - return factory.attach(await contractManager.getContract(name)) as MessageProxyForMainnet; - } else { + try { + await contractManager.getContract(name); + } catch { const instance = await upgrades.deployProxy(factory, [await contractManager.getAddress()]) as unknown as MessageProxyForMainnet; await contractManager.setContractsAddress(name, instance); return instance; } + return factory.attach(await contractManager.getContract(name)) as MessageProxyForMainnet; } diff --git a/test/utils/skale-manager-utils/contractManager.ts b/test/utils/skale-manager-utils/contractManager.ts index 6c21724a2..5a71dd03e 100644 --- a/test/utils/skale-manager-utils/contractManager.ts +++ b/test/utils/skale-manager-utils/contractManager.ts @@ -13,31 +13,43 @@ export async function deployContractManager(contractManagerAddress: string) { if (contractManagerAddress === "0x0000000000000000000000000000000000000000") { instance = await contractManagerFactory.deploy() as ContractManager; } else { - instance = await contractManagerFactory.attach(contractManagerAddress) as ContractManager; + instance = contractManagerFactory.attach(contractManagerAddress) as ContractManager; } - if (await instance.getContract("KeyStorage") === "0x0000000000000000000000000000000000000000") { + try { + await instance.getContract("KeyStorage"); + } catch { const keyStorageInstance = await (await ethers.getContractFactory("KeyStorageMock")).deploy() as KeyStorageMock; await instance.setContractsAddress("KeyStorage", keyStorageInstance); } - if (await instance.getContract(nameNodes) === "0x0000000000000000000000000000000000000000") { + try { + await instance.getContract(nameNodes); + } catch { const nodesInstance = await (await ethers.getContractFactory(nameNodes)).deploy() as Nodes; await instance.setContractsAddress(nameNodes, nodesInstance); } - if (await instance.getContract(nameSchains) === "0x0000000000000000000000000000000000000000") { + try { + await instance.getContract(nameSchains); + } catch { const schainsInstance = await (await ethers.getContractFactory(nameSchains)).deploy() as Schains; await schainsInstance.addContractManager(instance); await instance.setContractsAddress(nameSchains, schainsInstance); } - if (await instance.getContract(nameSchainsInternal) === "0x0000000000000000000000000000000000000000") { + try { + await instance.getContract(nameSchainsInternal); + } catch { const schainsInternalInstance = await (await ethers.getContractFactory(nameSchainsInternal)).deploy() as SchainsInternal; await schainsInternalInstance.addContractManager(instance); await instance.setContractsAddress(nameSchainsInternal, schainsInternalInstance); } - if (await instance.getContract(nameSkaleVerifier) === "0x0000000000000000000000000000000000000000") { + try { + await instance.getContract(nameSkaleVerifier); + } catch { const skaleVerifierInstance = await (await ethers.getContractFactory(nameSkaleVerifier)).deploy() as SkaleVerifierMock; await instance.setContractsAddress("SkaleVerifier", skaleVerifierInstance); } - if (await instance.getContract(nameWallets) === "0x0000000000000000000000000000000000000000") { + try { + await instance.getContract(nameWallets); + } catch { const walletsInstance = await (await ethers.getContractFactory(nameWallets)).deploy() as Wallets; await walletsInstance.addContractManager(instance); await instance.setContractsAddress(nameWallets, walletsInstance); From 69c4a9949832a3c0f8c5e5e6efc3da204293c5e2 Mon Sep 17 00:00:00 2001 From: Vadim Yavorsky Date: Wed, 20 Nov 2024 17:06:35 +0000 Subject: [PATCH 4/9] Fix test in CommunityPool --- test/utils/deploy/test/messageProxyForMainnetTester.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/utils/deploy/test/messageProxyForMainnetTester.ts b/test/utils/deploy/test/messageProxyForMainnetTester.ts index 2a2ac35eb..0053f02e1 100644 --- a/test/utils/deploy/test/messageProxyForMainnetTester.ts +++ b/test/utils/deploy/test/messageProxyForMainnetTester.ts @@ -7,9 +7,9 @@ export async function deployMessageProxyForMainnetTester( contractManager: ContractManager ) { const factory = await ethers.getContractFactory(name); - if (await contractManager.getContract(name) !== "0x0000000000000000000000000000000000000000") { - return factory.attach(await contractManager.getContract(name)) as MessageProxyForMainnetTester; - } else { + try { + await contractManager.getContract(name); + } catch { const instance = await upgrades.deployProxy( factory, [await contractManager.getAddress()] @@ -17,4 +17,5 @@ export async function deployMessageProxyForMainnetTester( await contractManager.setContractsAddress(name, instance); return instance; } + return factory.attach(await contractManager.getContract(name)) as MessageProxyForMainnetTester; } From 1570fba668b6dede9ecc660c70efc21cfe61c6d0 Mon Sep 17 00:00:00 2001 From: Vadim Yavorsky Date: Wed, 20 Nov 2024 17:45:37 +0000 Subject: [PATCH 5/9] Add contractNamesToUpgrade --- migrations/upgradeMainnet.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/migrations/upgradeMainnet.ts b/migrations/upgradeMainnet.ts index 94386d2d0..7360ec949 100644 --- a/migrations/upgradeMainnet.ts +++ b/migrations/upgradeMainnet.ts @@ -154,10 +154,14 @@ async function updateAbi() { } async function main() { + const contractNamesToUpgrade = [ + "MessageProxyForMainnet", + "CommunityPool" + ] const upgrader = new ImaMainnetUpgrader( "2.1.0", await getImaMainnetInstance(), - contracts + contractNamesToUpgrade ); await upgrader.upgrade(); await updateAbi(); From 550aa0b959665e43c428129ef2ac004739e94dd5 Mon Sep 17 00:00:00 2001 From: Vadim Yavorsky Date: Wed, 20 Nov 2024 18:09:25 +0000 Subject: [PATCH 6/9] Fixed upgradeMainnet --- migrations/upgradeMainnet.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/migrations/upgradeMainnet.ts b/migrations/upgradeMainnet.ts index 7360ec949..4a9714c4d 100644 --- a/migrations/upgradeMainnet.ts +++ b/migrations/upgradeMainnet.ts @@ -103,13 +103,18 @@ class ImaMainnetUpgrader extends Upgrader { contractManagerInterface, ethers.provider ) + if (!process.env.ABI) { + console.log(chalk.red("Set path to file with ABI and addresses to ABI environment variables")); + process.exit(1); + } + const abi = JSON.parse(await fs.readFile(process.env.ABI, "utf-8")); for (const contractName of contracts) { try { const contractAddress = await contractManager.getContract(contractName); console.log(`Address of ${contractName} is set to ${contractAddress}`); } catch { // getContract failed because the contract is not set - const contract = await this.instance.getContract(contractName); + const contractAddress = abi[`${getContractKeyInAbiFile(contractName)}_address`] as string; this.transactions.push(Transaction.from( { to: await contractManager.getAddress(), @@ -117,12 +122,12 @@ class ImaMainnetUpgrader extends Upgrader { "setContractsAddress", [ contractName, - await contract.getAddress() + contractAddress ] ) } )) - console.log(`Set ${contractName} address to ${await contract.getAddress()}`); + console.log(`Set ${contractName} address to ${contractAddress}`); } } }; @@ -154,10 +159,14 @@ async function updateAbi() { } async function main() { - const contractNamesToUpgrade = [ - "MessageProxyForMainnet", - "CommunityPool" - ] + let contractNamesToUpgrade = contracts; + if (process.env.TEST_UPGRADE !== "true") { + contractNamesToUpgrade = [ + "MessageProxyForMainnet", + "CommunityPool" + ] + + } const upgrader = new ImaMainnetUpgrader( "2.1.0", await getImaMainnetInstance(), From 03812945943c5e75a86857c61b4e9829828d2012 Mon Sep 17 00:00:00 2001 From: Vadim Yavorsky Date: Thu, 21 Nov 2024 12:19:25 +0000 Subject: [PATCH 7/9] Add small improvements --- contracts/test/TestContractManager.sol | 8 +++++++- migrations/upgradeMainnet.ts | 13 ++++++------- scripts/test_upgrade.sh | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/contracts/test/TestContractManager.sol b/contracts/test/TestContractManager.sol index 036b7b6c3..6595a7b35 100644 --- a/contracts/test/TestContractManager.sol +++ b/contracts/test/TestContractManager.sol @@ -38,6 +38,10 @@ contract ContractManager is IContractManagerTester { event ContractUpgraded(string contractsName, address contractsAddress); + error ContractNotFound( + string contractName + ); + constructor() { owner = msg.sender; } @@ -73,6 +77,8 @@ contract ContractManager is IContractManagerTester { returns (address contractAddress) { contractAddress = contracts[keccak256(abi.encodePacked(name))]; - require(contractAddress != address(0), "Contract has not been found"); + if (contractAddress == address(0)) { + revert ContractNotFound(name); + } } } diff --git a/migrations/upgradeMainnet.ts b/migrations/upgradeMainnet.ts index 4a9714c4d..733c5ed44 100644 --- a/migrations/upgradeMainnet.ts +++ b/migrations/upgradeMainnet.ts @@ -159,13 +159,12 @@ async function updateAbi() { } async function main() { - let contractNamesToUpgrade = contracts; - if (process.env.TEST_UPGRADE !== "true") { - contractNamesToUpgrade = [ - "MessageProxyForMainnet", - "CommunityPool" - ] - + let contractNamesToUpgrade = [ + "MessageProxyForMainnet", + "CommunityPool" + ] + if (process.env.UPGRADE_ALL) { + contractNamesToUpgrade = contracts; } const upgrader = new ImaMainnetUpgrader( "2.1.0", diff --git a/scripts/test_upgrade.sh b/scripts/test_upgrade.sh index 8442ef58c..ada74dd73 100755 --- a/scripts/test_upgrade.sh +++ b/scripts/test_upgrade.sh @@ -71,7 +71,7 @@ cd "$GITHUB_WORKSPACE" rm -r --interactive=never "$DEPLOYED_DIR" MESSAGE_PROXY_FOR_MAINNET=$(cat data/proxyMainnet.json | jq -r .message_proxy_mainnet_address) -TEST_UPGRADE=true \ +UPGRADE_ALL=true \ ABI="data/proxyMainnet.json" \ TARGET="$MESSAGE_PROXY_FOR_MAINNET" \ ALLOW_NOT_ATOMIC_UPGRADE="OK" \ From 61d839fddfd369fc11007c9801d0b06fc0f4f918 Mon Sep 17 00:00:00 2001 From: Vadim Yavorsky Date: Thu, 21 Nov 2024 13:51:17 +0000 Subject: [PATCH 8/9] Add UPGRADE_ALL env for schains upgrade --- migrations/upgradeSchain.ts | 5 +++++ scripts/test_upgrade.sh | 1 + 2 files changed, 6 insertions(+) diff --git a/migrations/upgradeSchain.ts b/migrations/upgradeSchain.ts index 57f8ea942..1814a81a3 100644 --- a/migrations/upgradeSchain.ts +++ b/migrations/upgradeSchain.ts @@ -92,6 +92,11 @@ async function updateAbi(contracts: string[]) { async function main() { const pathToManifest: string = process.env.MANIFEST || ""; await manifestSetup(pathToManifest); + let contractNamesToUpgrade = [ + ] + if (process.env.UPGRADE_ALL) { + contractNamesToUpgrade = contracts; + } const upgrader = new ImaSchainUpgrader( "2.1.0", await getImaSchainInstance(), diff --git a/scripts/test_upgrade.sh b/scripts/test_upgrade.sh index ada74dd73..93201338f 100755 --- a/scripts/test_upgrade.sh +++ b/scripts/test_upgrade.sh @@ -87,6 +87,7 @@ MESSAGE_PROXY_FOR_SCHAIN=$(cat data/$ABI_FILENAME_SCHAIN | jq -r .message_proxy_ # ABI="data/$ABI_FILENAME_SCHAIN" \ # MANIFEST="data/ima-schain-$DEPLOYED_VERSION-manifest.json" \ # CHAIN_NAME_SCHAIN="Test" \ +# UPGRADE_ALL=true \ # ALLOW_NOT_ATOMIC_UPGRADE="OK" \ # TARGET="$MESSAGE_PROXY_FOR_SCHAIN" \ # VERSION=$VERSION_TAG \ From 49b0b3eb4d702d03342cff49c80b7db98c6a4801 Mon Sep 17 00:00:00 2001 From: Vadim Yavorsky Date: Thu, 21 Nov 2024 13:58:10 +0000 Subject: [PATCH 9/9] Fix eslint --- migrations/upgradeSchain.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/migrations/upgradeSchain.ts b/migrations/upgradeSchain.ts index 1814a81a3..fd7076722 100644 --- a/migrations/upgradeSchain.ts +++ b/migrations/upgradeSchain.ts @@ -92,7 +92,7 @@ async function updateAbi(contracts: string[]) { async function main() { const pathToManifest: string = process.env.MANIFEST || ""; await manifestSetup(pathToManifest); - let contractNamesToUpgrade = [ + let contractNamesToUpgrade: string[] = [ ] if (process.env.UPGRADE_ALL) { contractNamesToUpgrade = contracts; @@ -100,7 +100,7 @@ async function main() { const upgrader = new ImaSchainUpgrader( "2.1.0", await getImaSchainInstance(), - contracts + contractNamesToUpgrade ); await upgrader.upgrade(); updateAbi(contracts);