Skip to content

Commit

Permalink
Merge pull request #1732 from skalenetwork/hotfix-for-initialize
Browse files Browse the repository at this point in the history
Hotfix for initialize
  • Loading branch information
DmytroNazarenko authored Nov 21, 2024
2 parents 86c207a + 49b0b3e commit 8b8077f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 20 deletions.
18 changes: 16 additions & 2 deletions contracts/test/TestContractManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ contract ContractManager is IContractManagerTester {

event ContractUpgraded(string contractsName, address contractsAddress);

error ContractNotFound(
string contractName
);

constructor() {
owner = msg.sender;
}
Expand All @@ -64,7 +68,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 ContractNotFound(name);
}
}
}
21 changes: 18 additions & 3 deletions migrations/upgradeMainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,27 @@ 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 contractAddress = await this.instance.getContract(contractName);
const contractAddress = abi[`${getContractKeyInAbiFile(contractName)}_address`] as string;
this.transactions.push(Transaction.from(
{
to: await contractManager.getAddress(),
data: contractManager.interface.encodeFunctionData(
"setContractsAddress",
[contractAddress]
[
contractName,
contractAddress
]
)
}
))
Expand Down Expand Up @@ -151,10 +159,17 @@ async function updateAbi() {
}

async function main() {
let contractNamesToUpgrade = [
"MessageProxyForMainnet",
"CommunityPool"
]
if (process.env.UPGRADE_ALL) {
contractNamesToUpgrade = contracts;
}
const upgrader = new ImaMainnetUpgrader(
"2.1.0",
await getImaMainnetInstance(),
contracts
contractNamesToUpgrade
);
await upgrader.upgrade();
await updateAbi();
Expand Down
7 changes: 6 additions & 1 deletion migrations/upgradeSchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,15 @@ async function updateAbi(contracts: string[]) {
async function main() {
const pathToManifest: string = process.env.MANIFEST || "";
await manifestSetup(pathToManifest);
let contractNamesToUpgrade: string[] = [
]
if (process.env.UPGRADE_ALL) {
contractNamesToUpgrade = contracts;
}
const upgrader = new ImaSchainUpgrader(
"2.1.0",
await getImaSchainInstance(),
contracts
contractNamesToUpgrade
);
await upgrader.upgrade();
updateAbi(contracts);
Expand Down
3 changes: 2 additions & 1 deletion scripts/test_upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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 \
Expand Down
7 changes: 4 additions & 3 deletions test/utils/deploy/mainnet/messageProxyForMainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 4 additions & 3 deletions test/utils/deploy/test/messageProxyForMainnetTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ 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()]
) as unknown as MessageProxyForMainnetTester;
await contractManager.setContractsAddress(name, instance);
return instance;
}
return factory.attach(await contractManager.getContract(name)) as MessageProxyForMainnetTester;
}
26 changes: 19 additions & 7 deletions test/utils/skale-manager-utils/contractManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8b8077f

Please sign in to comment.