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

fix(protocol): replace __self in bytecode for all EssentialContracts when generating genesis JSON #15476

Merged
merged 4 commits into from
Jan 10, 2024
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
59 changes: 14 additions & 45 deletions packages/protocol/genesis/GenerateGenesis.g.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,9 @@ contract TestGenerateGenesis is Test, AddressResolver {
checkSavedAddress(addressManagerProxy, "ERC1155Vault", "erc1155_vault");
checkSavedAddress(addressManagerProxy, "SignalService", "signal_service");

AddressManager newAddressManager = new AddressManager();
vm.startPrank(addressManagerProxy.owner());

AddressManager addressManager =
AddressManager(getPredeployedContractAddress("SharedAddressManagerImpl"));

vm.startPrank(addressManager.owner());

addressManager.upgradeTo(address(newAddressManager));
addressManagerProxy.upgradeTo(address(new AddressManager()));

vm.stopPrank();
}
Expand All @@ -102,14 +97,9 @@ contract TestGenerateGenesis is Test, AddressResolver {
checkSavedAddress(addressManagerProxy, "TaikoL2", "taiko");
checkSavedAddress(addressManagerProxy, "SignalService", "signal_service");

AddressManager addressManager =
AddressManager(getPredeployedContractAddress("RollupAddressManagerImpl"));
vm.startPrank(addressManagerProxy.owner());

AddressManager newAddressManager = new AddressManager();

vm.startPrank(addressManager.owner());

addressManager.upgradeTo(address(newAddressManager));
addressManagerProxy.upgradeTo(address(new AddressManager()));

vm.stopPrank();
}
Expand Down Expand Up @@ -143,13 +133,9 @@ contract TestGenerateGenesis is Test, AddressResolver {
}
vm.stopPrank();

TaikoL2 taikoL2 = TaikoL2(getPredeployedContractAddress("TaikoL2Impl"));

vm.startPrank(taikoL2.owner());
vm.startPrank(taikoL2Proxy.owner());

TaikoL2 newTaikoL2 = new TaikoL2();

taikoL2.upgradeTo(address(newTaikoL2));
taikoL2Proxy.upgradeTo(address(new TaikoL2()));

vm.stopPrank();
}
Expand Down Expand Up @@ -206,11 +192,7 @@ contract TestGenerateGenesis is Test, AddressResolver {
bridgeProxy.unpause();
assertEq(bridgeProxy.paused(), false);

Bridge bridge = Bridge(payable(getPredeployedContractAddress("BridgeImpl")));

Bridge newBridge = new Bridge();

bridge.upgradeTo(address(newBridge));
bridgeProxy.upgradeTo(address(new Bridge()));

vm.stopPrank();
}
Expand All @@ -230,13 +212,9 @@ contract TestGenerateGenesis is Test, AddressResolver {
addressManager.setAddress(1, "erc20_vault", erc20VaultAddress);
vm.stopPrank();

ERC20Vault erc20Vault = ERC20Vault(getPredeployedContractAddress("ERC20VaultImpl"));

vm.startPrank(erc20Vault.owner());

ERC20Vault newERC20Vault = new ERC20Vault();
vm.startPrank(erc20VaultProxy.owner());

erc20Vault.upgradeTo(address(newERC20Vault));
erc20VaultProxy.upgradeTo(address(new ERC20Vault()));

vm.stopPrank();
}
Expand All @@ -256,12 +234,9 @@ contract TestGenerateGenesis is Test, AddressResolver {
addressManager.setAddress(1, "erc721_vault", erc721VaultAddress);
vm.stopPrank();

ERC721Vault erc721Vault = ERC721Vault(getPredeployedContractAddress("ERC721VaultImpl"));
vm.startPrank(erc721VaultProxy.owner());

vm.startPrank(erc721Vault.owner());
ERC721Vault newERC721Vault = new ERC721Vault();

erc721Vault.upgradeTo(address(newERC721Vault));
erc721VaultProxy.upgradeTo(address(new ERC721Vault()));

vm.stopPrank();
}
Expand All @@ -283,13 +258,10 @@ contract TestGenerateGenesis is Test, AddressResolver {

address erc1155VaultAddress = getPredeployedContractAddress("ERC1155VaultImpl");

ERC1155Vault erc1155Vault = ERC1155Vault(erc1155VaultAddress);

vm.startPrank(erc1155Vault.owner());

ERC1155Vault newERC1155Vault = new ERC1155Vault();
vm.startPrank(erc1155VaultProxy.owner());

erc1155Vault.upgradeTo(address(newERC1155Vault));
erc1155VaultProxy.upgradeTo(address(new ERC1155Vault()));

vm.stopPrank();
}
Expand All @@ -314,9 +286,7 @@ contract TestGenerateGenesis is Test, AddressResolver {
SignalService signalService =
SignalService(payable(getPredeployedContractAddress("SignalServiceImpl")));

SignalService newSignalService = new SignalService();

signalService.upgradeTo(address(newSignalService));
signalServiceProxy.upgradeTo(address(new SignalService()));

vm.stopPrank();
}
Expand All @@ -340,7 +310,6 @@ contract TestGenerateGenesis is Test, AddressResolver {
assertEq(address(contractAddress).code, vm.parseBytes(deployedCode));
}


function checkProxyImplementation(
string memory proxyName,
string memory contractName
Expand Down
123 changes: 107 additions & 16 deletions packages/protocol/utils/generate_genesis/taikoL2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ async function generateContractConfigs(

const addressMap: any = {};

const uupsImmutableReferencesMap: any = getUUPSImmutableReferences();

for (const [contractName, artifact] of Object.entries(contractArtifacts)) {
const bytecode = (artifact as any).bytecode;

Expand All @@ -215,9 +217,14 @@ async function generateContractConfigs(
// Shared Contracts
SharedAddressManagerImpl: {
address: addressMap.SharedAddressManagerImpl,
deployedBytecode:
contractArtifacts.SharedAddressManagerImpl.deployedBytecode
.object,
deployedBytecode: replaceUUPSImmutableVaules(
contractArtifacts.SharedAddressManagerImpl,
uupsImmutableReferencesMap,
ethers.utils.hexZeroPad(
addressMap.SharedAddressManagerImpl,
32,
),
).deployedBytecode.object,
variables: {
_owner: ownerSecurityCouncil,
},
Expand Down Expand Up @@ -270,7 +277,11 @@ async function generateContractConfigs(
BridgeImpl: {
address: addressMap.BridgeImpl,
deployedBytecode: linkContractLibs(
contractArtifacts.BridgeImpl,
replaceUUPSImmutableVaules(
contractArtifacts.BridgeImpl,
uupsImmutableReferencesMap,
ethers.utils.hexZeroPad(addressMap.BridgeImpl, 32),
),
addressMap,
),
variables: {
Expand Down Expand Up @@ -300,7 +311,11 @@ async function generateContractConfigs(
ERC20VaultImpl: {
address: addressMap.ERC20VaultImpl,
deployedBytecode: linkContractLibs(
contractArtifacts.ERC20VaultImpl,
replaceUUPSImmutableVaules(
contractArtifacts.ERC20VaultImpl,
uupsImmutableReferencesMap,
ethers.utils.hexZeroPad(addressMap.ERC20VaultImpl, 32),
),
addressMap,
),
variables: {
Expand Down Expand Up @@ -331,7 +346,11 @@ async function generateContractConfigs(
ERC721VaultImpl: {
address: addressMap.ERC721VaultImpl,
deployedBytecode: linkContractLibs(
contractArtifacts.ERC721VaultImpl,
replaceUUPSImmutableVaules(
contractArtifacts.ERC721VaultImpl,
uupsImmutableReferencesMap,
ethers.utils.hexZeroPad(addressMap.ERC721VaultImpl, 32),
),
addressMap,
),
variables: {
Expand Down Expand Up @@ -362,7 +381,11 @@ async function generateContractConfigs(
ERC1155VaultImpl: {
address: addressMap.ERC1155VaultImpl,
deployedBytecode: linkContractLibs(
contractArtifacts.ERC1155VaultImpl,
replaceUUPSImmutableVaules(
contractArtifacts.ERC1155VaultImpl,
uupsImmutableReferencesMap,
ethers.utils.hexZeroPad(addressMap.ERC1155VaultImpl, 32),
),
addressMap,
),
variables: {
Expand Down Expand Up @@ -392,8 +415,11 @@ async function generateContractConfigs(
},
BridgedERC20: {
address: addressMap.BridgedERC20Impl,
deployedBytecode:
contractArtifacts.BridgedERC20Impl.deployedBytecode.object,
deployedBytecode: replaceUUPSImmutableVaules(
contractArtifacts.BridgedERC20Impl,
uupsImmutableReferencesMap,
ethers.utils.hexZeroPad(addressMap.BridgedERC20Impl, 32),
).deployedBytecode.object,
},
BridgedERC721: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see BridgedERC1155 and BridgedERC20 getting patched, but not BridgedERC721. Shouldn't it also be patched?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plz review: #15479

address: addressMap.BridgedERC721Impl,
Expand All @@ -402,13 +428,20 @@ async function generateContractConfigs(
},
BridgedERC1155: {
address: addressMap.BridgedERC1155Impl,
deployedBytecode:
contractArtifacts.BridgedERC1155Impl.deployedBytecode.object,
deployedBytecode: replaceUUPSImmutableVaules(
contractArtifacts.BridgedERC1155Impl,
uupsImmutableReferencesMap,
ethers.utils.hexZeroPad(addressMap.BridgedERC1155Impl, 32),
).deployedBytecode.object,
},
SignalServiceImpl: {
address: addressMap.SignalServiceImpl,
deployedBytecode: linkContractLibs(
contractArtifacts.SignalServiceImpl,
replaceUUPSImmutableVaules(
contractArtifacts.SignalServiceImpl,
uupsImmutableReferencesMap,
ethers.utils.hexZeroPad(addressMap.SignalServiceImpl, 32),
),
addressMap,
),
variables: {
Expand Down Expand Up @@ -444,7 +477,11 @@ async function generateContractConfigs(
TaikoL2Impl: {
address: addressMap.TaikoL2Impl,
deployedBytecode: linkContractLibs(
contractArtifacts.TaikoL2Impl,
replaceUUPSImmutableVaules(
contractArtifacts.TaikoL2Impl,
uupsImmutableReferencesMap,
ethers.utils.hexZeroPad(addressMap.TaikoL2Impl, 32),
),
addressMap,
),
variables: {
Expand Down Expand Up @@ -483,9 +520,14 @@ async function generateContractConfigs(
},
RollupAddressManagerImpl: {
address: addressMap.RollupAddressManagerImpl,
deployedBytecode:
contractArtifacts.RollupAddressManagerImpl.deployedBytecode
.object,
deployedBytecode: replaceUUPSImmutableVaules(
contractArtifacts.RollupAddressManagerImpl,
uupsImmutableReferencesMap,
ethers.utils.hexZeroPad(
addressMap.RollupAddressManagerImpl,
32,
),
).deployedBytecode.object,
variables: {
_owner: ownerSecurityCouncil,
},
Expand Down Expand Up @@ -562,3 +604,52 @@ function getLinkLibs(artifact: any, linkRefs: any, addressMap: any) {

return result;
}

function getUUPSImmutableReferences() {
const references: any = {};
const contractName = "UUPSUpgradeable";
const immutableValueName = "__self";
const artifact = require(
path.join(ARTIFACTS_PATH, `./${contractName}.sol/${contractName}.json`),
);

for (const node of artifact.ast.nodes) {
if (node.nodeType !== "ContractDefinition") continue;

for (const subNode of node.nodes) {
if (subNode.name !== immutableValueName) continue;
references[`${contractName}`] = {
name: immutableValueName,
id: subNode.id,
};
break;
}
}

return references;
}

function replaceUUPSImmutableVaules(
artifact: any,
references: any,
value: string,
): any {
const offsets =
artifact.deployedBytecode.immutableReferences[
`${references.UUPSUpgradeable.id}`
];
let deployedBytecodeWithoutPrefix =
artifact.deployedBytecode.object.substring(2);
if (value.startsWith("0x")) value = value.substring(2);

for (const { start, length } of offsets) {
const prefix = deployedBytecodeWithoutPrefix.substring(0, start * 2);
const suffix = deployedBytecodeWithoutPrefix.substring(
start * 2 + length * 2,
);
deployedBytecodeWithoutPrefix = `${prefix}${value}${suffix}`;
}

artifact.deployedBytecode.object = `0x${deployedBytecodeWithoutPrefix}`;
return artifact;
}
Loading