Skip to content

Commit

Permalink
Merge pull request #1743 from skalenetwork/merge-develop
Browse files Browse the repository at this point in the history
Merge post-deployment actions
  • Loading branch information
DimaStebaev authored Dec 13, 2024
2 parents c9a8ef1 + cdc3349 commit fb51ab8
Show file tree
Hide file tree
Showing 11 changed files with 675 additions and 105 deletions.
596 changes: 596 additions & 0 deletions .openzeppelin/mainnet.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion DEPLOYED
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.0-stable.0
2.2.0-stable.0
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 calldata 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);
}
}
}
14 changes: 10 additions & 4 deletions migrations/generateManifest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from "hardhat";
import { ethers, upgrades } from "hardhat";
import { contracts, getContractKeyInAbiFile } from "./deploySchain";
import { networkNames } from "@openzeppelin/upgrades-core";
import { promises as fs } from "fs";
Expand Down Expand Up @@ -92,6 +92,11 @@ async function readValidations() {
}
}

async function getManifestAdmin(abi: {[ key in string ]: string | []}) {
const randomProxy = abi[getContractKeyInAbiFile(contracts[0]) + "_address"] as string;
return await upgrades.erc1967.getAdminAddress(randomProxy);
}

export async function generateManifest(addresses: Addresses) {
const newManifest: ManifestData = emptyManifest();
newManifest.admin = getDeployment(addresses.admin);
Expand All @@ -106,12 +111,13 @@ export async function generateManifest(addresses: Addresses) {
}

export async function importAddresses(manifest: ManifestData, abi: {[ key in string ]: string | []}) {
if (!manifest.admin) {
const manifestAdminAddress = await getManifestAdmin(abi);
if (!manifestAdminAddress) {
throw Error("Proxy admin is missing in manifest file");
}
const addresses: Addresses = {};
addresses.admin = manifest.admin.address;
console.log("Admin address", manifest.admin.address, "imported");
addresses.admin = manifestAdminAddress;
console.log("Admin address", manifestAdminAddress, "imported");
for (const contract of contracts) {
const proxyAddress = abi[getContractKeyInAbiFile(contract) + "_address"];
if (Array.isArray(proxyAddress)) {
Expand Down
83 changes: 9 additions & 74 deletions migrations/upgradeMainnet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from "chalk";
import { ethers } from "hardhat";
import { promises as fs } from 'fs';
import { Interface, Transaction } from "ethers";
import { Transaction } from "ethers";
import { getAbi, getVersion, Submitter, Upgrader } from "@skalenetwork/upgrade-tools";
import { skaleContracts, Instance } from "@skalenetwork/skale-contracts-ethers-v6";
import { MessageProxyForMainnet } from "../typechain";
Expand Down Expand Up @@ -60,76 +60,7 @@ class ImaMainnetUpgrader extends Upgrader {

// deployNewContracts = () => { };

initialize = async () => {
const contractManagerAddress = await (await this.getMessageProxyForMainnet()).contractManagerOfSkaleManager();
const contractManagerInterface = new Interface([{
"type": "function",
"name": "getContract",
"constant": true,
"stateMutability": "view",
"payable": false,
"inputs": [
{
"type": "string",
"name": "name"
}
],
"outputs": [
{
"type": "address",
"name": "contractAddress"
}
]
},
{
"type": "function",
"name": "setContractsAddress",
"constant": false,
"payable": false,
"inputs": [
{
"type": "string",
"name": "contractsName"
},
{
"type": "address",
"name": "newContractsAddress"
}
],
"outputs": []
}]);
const contractManager = new ethers.Contract(
contractManagerAddress,
contractManagerInterface,
ethers.provider
)
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);
this.transactions.push(Transaction.from(
{
to: await contractManager.getAddress(),
data: contractManager.interface.encodeFunctionData(
"setContractsAddress",
[contractAddress]
)
}
))
console.log(`Set ${contractName} address to ${contractAddress}`);
}
}
};

_getContractKeyInAbiFile(contract: string) {
if (contract === "MessageProxyForMainnet") {
return "message_proxy_mainnet";
}
return contract.replace(/([a-z0-9])(?=[A-Z])/g, '$1_').toLowerCase();
}
// initialize = async () => { };
}

async function updateAbi() {
Expand All @@ -145,16 +76,20 @@ async function updateAbi() {
const contractInterface = (await ethers.getContractFactory(contract)).interface;
abi[getContractKeyInAbiFile(contract) + "_abi"] = getAbi(contractInterface);
}
const newAbiFilename = `mainnet-ima-${version}-${network.name}.json`;
const newAbiFilename = `data/mainnet-ima-${version}-${network.name}.json`;
await fs.writeFile(newAbiFilename, JSON.stringify(abi, null, 4));
console.log(chalk.green(`ABI updated and saved to ${newAbiFilename}`));
}

async function main() {
let contractNamesToUpgrade: string[] = [];
if (process.env.UPGRADE_ALL) {
contractNamesToUpgrade = contracts;
}
const upgrader = new ImaMainnetUpgrader(
"2.1.0",
"2.2.0",
await getImaMainnetInstance(),
contracts
contractNamesToUpgrade
);
await upgrader.upgrade();
await updateAbi();
Expand Down
11 changes: 8 additions & 3 deletions migrations/upgradeSchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,23 @@ async function updateAbi(contracts: string[]) {
const contractInterface = (await ethers.getContractFactory(contract)).interface;
abi[getContractKeyInAbiFile(contract) + "_abi"] = getAbi(contractInterface);
}
const newAbiFilename = `schain-ima-${version}-${network.name}.json`;
const newAbiFilename = `data/schain-ima-${version}-${network.name}.json`;
await fs.writeFile(newAbiFilename, JSON.stringify(abi, null, 4));
console.log(chalk.green(`ABI updated and saved to ${newAbiFilename}`));
}

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",
"2.2.0",
await getImaSchainInstance(),
contracts
contractNamesToUpgrade
);
await upgrader.upgrade();
updateAbi(contracts);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@
"ts-node": "^8.10.2",
"typechain": "^8.3.1",
"typescript": "^5.1.6"
}
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
13 changes: 6 additions & 7 deletions scripts/test_upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ DEPLOYED_DIR=$GITHUB_WORKSPACE/deployed-IMA/

git clone --branch "$DEPLOYED_TAG" "https://github.com/$GITHUB_REPOSITORY.git" "$DEPLOYED_DIR"

ACCOUNTS_FILENAME="$DEPLOYED_DIR/proxy/generatedAccounts.json"
ACCOUNTS_FILENAME="$DEPLOYED_DIR/generatedAccounts.json"
GANACHE=$(npx ganache \
--😈 \
--miner.blockGasLimit 12000000 \
Expand All @@ -32,7 +32,7 @@ GANACHE=$(npx ganache \
--wallet.accountKeysPath "$ACCOUNTS_FILENAME" \
)

cd "$DEPLOYED_DIR/proxy"
cd "$DEPLOYED_DIR"
yarn install
PRIVATE_KEY_FOR_ETHEREUM=$(cat "$ACCOUNTS_FILENAME" | jq -r '.private_keys | to_entries | .[8].value')
PRIVATE_KEY_FOR_SCHAIN=$(cat "$ACCOUNTS_FILENAME" | jq -r '.private_keys | to_entries | .[0].value')
Expand All @@ -43,11 +43,8 @@ VERSION="$DEPLOYED_VERSION" \
PRIVATE_KEY_FOR_ETHEREUM="$PRIVATE_KEY_FOR_ETHEREUM" \
PRIVATE_KEY_FOR_SCHAIN="$PRIVATE_KEY_FOR_SCHAIN" \
npx hardhat run migrations/deploySkaleManagerComponents.ts --network localhost
# TODO: remove this line after upgrading to 2.2.0
perl -0777 -i -pe 's/await contractManagerInst\.setContractsAddress\( "MessageProxyForMainnet",[^\}]*console\.log\( "Successfully registered MessageProxy in ContractManager" \);/await contractManagerInst.setContractsAddress( "MessageProxyForMainnet", deployed.get( "MessageProxyForMainnet" )?.address);\nawait contractManagerInst.setContractsAddress( "CommunityPool", deployed.get( "CommunityPool" )?.address);\nawait contractManagerInst.setContractsAddress( "Linker", deployed.get( "Linker" )?.address);\nfor (const contractName of contractsToDeploy) {\n const contract = deployed.get(contractName);\n if (contract === undefined) {\n throw new Error(`\${contractName} was not found`);\n }\n await contractManagerInst.setContractsAddress( contractName, contract.address);\n}\nconsole.log( "Successfully registered MessageProxy in ContractManager" );/s' migrations/deployMainnet.ts
# end of TODO
SKALE_MANAGER=$(cat data/skaleManagerComponents.json | jq -r .skale_manager_address)
VERSION="$DEPLOYED_VERSION" TARGET=$SKALE_MANAGER npx hardhat run migrations/deployMainnet.ts --network localhost
VERSION="$DEPLOYED_VERSION" SKALE_MANAGER_ADDRESS=$SKALE_MANAGER npx hardhat run migrations/deployMainnet.ts --network localhost


CHAIN_NAME_SCHAIN="Test" \
Expand All @@ -56,6 +53,7 @@ URL_W3_S_CHAIN="$URL_W3_S_CHAIN" \
PRIVATE_KEY_FOR_SCHAIN="$PRIVATE_KEY_FOR_SCHAIN" \
npx hardhat run migrations/deploySchain.ts --network schain

cp "$GITHUB_WORKSPACE/migrations/generateManifest.ts" "$DEPLOYED_DIR/migrations/generateManifest.ts"
ABI_FILENAME_SCHAIN="proxySchain_Test.json"
ABI="data/$ABI_FILENAME_SCHAIN" \
MANIFEST=".openzeppelin/unknown-1337.json" \
Expand All @@ -71,7 +69,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 +85,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 fb51ab8

Please sign in to comment.