Skip to content

Commit 82c12d3

Browse files
committed
WIP
1 parent 59bce17 commit 82c12d3

File tree

4 files changed

+152
-90
lines changed

4 files changed

+152
-90
lines changed

src/genesis-hardhat/create-genesis-hardhat.ts

Lines changed: 54 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
133133
/// ///////////////////////////////////
134134
/// DEPLOY SOVEREIGN CONTRACTS ///
135135
/// ///////////////////////////////////
136-
const genesisInfo = [];
137136

138137
// Load deployer
139138
await ethers.provider.send('hardhat_impersonateAccount', [timelockOwner]);
@@ -153,28 +152,12 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
153152

154153
const sovereignChainBridgeContract = bridgeDeploymentResult.contract as unknown as BridgeL2SovereignChain;
155154

156-
// Get the deployment transaction for bridge
157-
const txDeployBridge = await sovereignChainBridgeContract.deploymentTransaction();
158-
const txDeployBridgeHash = txDeployBridge ? txDeployBridge.hash : undefined;
159-
const tokenWrappedAddress = await sovereignChainBridgeContract.getWrappedTokenBridgeImplementation();
160-
161-
genesisInfo.push({
162-
isProxy: true,
163-
name: GENESIS_CONTRACT_NAMES.SOVEREIGN_BRIDGE,
164-
address: sovereignChainBridgeContract.target,
165-
storagesWrites: await getTraceStorageWrites(txDeployBridgeHash),
166-
deployedAddresses: [
167-
{
168-
name: GENESIS_CONTRACT_NAMES.BYTECODE_STORER,
169-
address: await sovereignChainBridgeContract.wrappedTokenBytecodeStorer(),
170-
},
171-
{
172-
name: GENESIS_CONTRACT_NAMES.TOKEN_WRAPPED_IMPLEMENTATION,
173-
address: tokenWrappedAddress,
174-
},
175-
],
176-
});
177-
155+
// Get addresses from bridge deployment
156+
const bridgeProxyAddress = sovereignChainBridgeContract.target;
157+
const bridgeImplAddress = (await upgrades.erc1967.getImplementationAddress(bridgeProxyAddress)).toLocaleLowerCase();
158+
const tokenWrappedAddress = (
159+
await sovereignChainBridgeContract.getWrappedTokenBridgeImplementation()
160+
).toLocaleLowerCase();
178161
// deploy GlobalExitRootManagerL2SovereignChain
179162
const gerManagerL2SovereignChainFactory = await ethers.getContractFactory(
180163
GENESIS_CONTRACT_NAMES.GER_L2_SOVEREIGN,
@@ -189,6 +172,10 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
189172

190173
const gerManagerContract = gerDeploymentResult.contract as unknown as GlobalExitRootManagerL2SovereignChain;
191174

175+
// Get addresses from ger deployment
176+
const gerProxyAddress = gerManagerContract.target;
177+
const gerImplAddress = await upgrades.erc1967.getImplementationAddress(gerProxyAddress);
178+
192179
/// ///////////////////////////////////
193180
/// DEPLOY AGGORACLE COMMITTEE ////
194181
/// ///////////////////////////////////
@@ -268,6 +255,8 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
268255
proxiedTokensManager,
269256
);
270257

258+
const WETHTokenAddress = (await sovereignChainBridgeContract.WETHToken()).toLowerCase();
259+
271260
logger.info('Initializing GlobalExitRootManagerL2SovereignChain contract...');
272261
// Initialize the GlobalExitRootManagerL2SovereignChain contract
273262
const txInitializeGer = await gerManagerContract.initialize(globalExitRootUpdater, globalExitRootRemover);
@@ -281,6 +270,7 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
281270
deployer,
282271
ethers.ZeroAddress, // PolygonRollupManager address not needed in L2
283272
);
273+
const timelockContractAddress = timelockContract.target.toString().toLowerCase();
284274

285275
const txDeployTimelock = await timelockContract.deploymentTransaction();
286276
const txDeployTimelockHash = txDeployTimelock ? txDeployTimelock.hash : undefined;
@@ -302,7 +292,7 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
302292
);
303293
const txRevokeTimelockAdminRole = await timelockContract.connect(deployer).revokeRole(
304294
ethers.id('TIMELOCK_ADMIN_ROLE'),
305-
timelockContract.target, // Revoke the role from the deployer
295+
timelockContractAddress, // Revoke the role from the deployer
306296
);
307297

308298
/// /////////////////////////////////
@@ -349,9 +339,8 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
349339

350340
// Get storage modifications for Bridge contract
351341
logger.info('Getting storage modifications for Bridge contract...');
352-
const bridgeStorageWrites = await getTraceStorageWrites(txDeployBridgeHash);
353-
const depthBridgeStorageWrites = 1;
354-
storageModifications.BridgeL2SovereignChain = bridgeStorageWrites[depthBridgeStorageWrites];
342+
const bridgeStorageWrites = await getTraceStorageWrites(bridgeDeploymentResult.txHashes.proxy, bridgeProxyAddress);
343+
storageModifications.BridgeL2SovereignChain = bridgeStorageWrites;
355344

356345
// Get storage modifications for Bridge implementation
357346
if (bridgeDeploymentResult.txHashes.implementation) {
@@ -360,14 +349,13 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
360349
const implTx = await ethers.provider.getTransaction(bridgeDeploymentResult.txHashes.implementation);
361350
if (implTx) {
362351
const implStorageWrites = await getTraceStorageWrites(bridgeDeploymentResult.txHashes.implementation);
363-
const depthBridgeImpl = 1;
364-
const depthTokenWrappedImpl = 2;
365-
storageModifications.BridgeL2SovereignChain_Implementation = implStorageWrites[depthBridgeImpl];
352+
storageModifications.BridgeL2SovereignChain_Implementation = implStorageWrites[bridgeImplAddress];
366353
storageModifications.TokenWrappedBridgeUpgradeable_Implementation =
367-
implStorageWrites[depthTokenWrappedImpl];
354+
implStorageWrites[tokenWrappedAddress];
368355
}
369356
} catch (error) {
370357
logger.error('Could not get Bridge implementation storage writes:', error);
358+
throw new Error(error)
371359
}
372360
}
373361
// Get storage modifications for Bridge initialization
@@ -377,13 +365,10 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
377365
const initTx = await ethers.provider.getTransaction(txInitializeBridge.hash);
378366
if (initTx) {
379367
const initStorageWrites = await getTraceStorageWrites(txInitializeBridge.hash);
380-
const depthBridgeInit = 2;
381-
const depthTokenWrappedProxy = 3;
382-
const depthTokenWrappedInit = 4;
383-
storageModifications.BridgeL2SovereignChain_Initialization = initStorageWrites[depthBridgeInit];
384-
storageModifications.TokenWrappedBridgeUpgradeable = initStorageWrites[depthTokenWrappedProxy];
368+
storageModifications.BridgeL2SovereignChain_Initialization = initStorageWrites[bridgeImplAddress];
369+
storageModifications.TokenWrappedBridgeUpgradeable = initStorageWrites[WETHTokenAddress];
385370
storageModifications.TokenWrappedBridgeUpgradeable_Initialization =
386-
initStorageWrites[depthTokenWrappedInit];
371+
initStorageWrites[tokenWrappedAddress];
387372
}
388373
} catch (error) {
389374
logger.error('Could not get Bridge initialization storage writes:', error);
@@ -400,9 +385,9 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
400385
if (implTx) {
401386
const implStorageWrites = await getTraceStorageWrites(
402387
aggOracleCommitteeDeploymentResult.txHashes.implementation,
388+
aggOracleImplementationAddress
403389
);
404-
const depthImplStorageWrites = 1;
405-
storageModifications.AggOracleCommittee_Implementation = implStorageWrites[depthImplStorageWrites];
390+
storageModifications.AggOracleCommittee_Implementation = implStorageWrites;
406391
}
407392
} catch (error) {
408393
logger.error('Could not get Bridge implementation storage writes:', error);
@@ -415,9 +400,9 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
415400
if (implTx) {
416401
const implStorageWrites = await getTraceStorageWrites(
417402
aggOracleCommitteeDeploymentResult.txHashes.proxy,
403+
aggOracleCommitteeContract.target,
418404
);
419-
const depthImplStorageWrites = 1;
420-
storageModifications.AggOracleCommittee = implStorageWrites[depthImplStorageWrites];
405+
storageModifications.AggOracleCommittee = implStorageWrites;
421406
}
422407
} catch (error) {
423408
logger.error('Could not get Bridge proxy storage writes:', error);
@@ -428,9 +413,11 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
428413
try {
429414
const initTx = await ethers.provider.getTransaction(txInitializeAggOracleCommittee.hash);
430415
if (initTx) {
431-
const initStorageWrites = await getTraceStorageWrites(txInitializeAggOracleCommittee.hash);
432-
const depthInitStorageWrites = 2;
433-
storageModifications.AggOracleCommittee_Initialization = initStorageWrites[depthInitStorageWrites];
416+
const initStorageWrites = await getTraceStorageWrites(
417+
txInitializeAggOracleCommittee.hash,
418+
aggOracleImplementationAddress,
419+
);
420+
storageModifications.AggOracleCommittee_Initialization = initStorageWrites;
434421
}
435422
} catch (error) {
436423
logger.error('Could not get AggOracle initialization storage writes:', error);
@@ -443,9 +430,11 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
443430
try {
444431
const gerProxyTx = await ethers.provider.getTransaction(gerDeploymentResult.txHashes.proxy);
445432
if (gerProxyTx) {
446-
const gerStorageWrites = await getTraceStorageWrites(gerDeploymentResult.txHashes.proxy);
447-
const depthGerStorageWrites = 1;
448-
storageModifications.GlobalExitRootManagerL2SovereignChain = gerStorageWrites[depthGerStorageWrites];
433+
const gerStorageWrites = await getTraceStorageWrites(
434+
gerDeploymentResult.txHashes.proxy,
435+
gerProxyAddress
436+
);
437+
storageModifications.GlobalExitRootManagerL2SovereignChain = gerStorageWrites;
449438
}
450439
} catch (error) {
451440
logger.error('Could not get GER proxy storage writes:', error);
@@ -458,10 +447,11 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
458447
try {
459448
const gerImplTx = await ethers.provider.getTransaction(gerDeploymentResult.txHashes.implementation);
460449
if (gerImplTx) {
461-
const gerImplStorageWrites = await getTraceStorageWrites(gerDeploymentResult.txHashes.implementation);
462-
const depthGerImplStorageWrites = 1;
463-
storageModifications.GlobalExitRootManagerL2SovereignChain_Implementation =
464-
gerImplStorageWrites[depthGerImplStorageWrites];
450+
const gerImplStorageWrites = await getTraceStorageWrites(
451+
gerDeploymentResult.txHashes.implementation,
452+
gerImplAddress,
453+
);
454+
storageModifications.GlobalExitRootManagerL2SovereignChain_Implementation = gerImplStorageWrites;
465455
}
466456
} catch (error) {
467457
logger.error('Could not get GER implementation storage writes:', error);
@@ -474,10 +464,8 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
474464
try {
475465
const gerInitTx = await ethers.provider.getTransaction(txInitializeGer.hash);
476466
if (gerInitTx) {
477-
const gerInitStorageWrites = await getTraceStorageWrites(txInitializeGer.hash);
478-
const depthGerInitStorageWrites = 2;
479-
storageModifications.GlobalExitRootManagerL2SovereignChain_Initialization =
480-
gerInitStorageWrites[depthGerInitStorageWrites];
467+
const gerInitStorageWrites = await getTraceStorageWrites(txInitializeGer.hash, gerImplAddress);
468+
storageModifications.GlobalExitRootManagerL2SovereignChain_Initialization = gerInitStorageWrites;
481469
}
482470
} catch (error) {
483471
logger.error('Could not get GER initialization storage writes:', error);
@@ -486,18 +474,20 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
486474

487475
// Get storage modifications for Timelock contract
488476
logger.info('Getting storage modifications for Timelock contract...');
489-
const timelockStorageWrites = await getTraceStorageWrites(txDeployTimelockHash);
490-
const depthTimelockStorageWrites = 1;
491-
storageModifications.PolygonZkEVMTimelock = timelockStorageWrites[depthTimelockStorageWrites];
492-
const timelockStorageAdmin = (await getTraceStorageWrites(txTimelockAdminRole.hash))[depthTimelockStorageWrites];
493-
const timelockStorageRevokeAdmin = (await getTraceStorageWrites(txRevokeTimelockAdminRole.hash))[depthTimelockStorageWrites];
477+
const timelockStorageWrites = await getTraceStorageWrites(txDeployTimelockHash, timelockContractAddress);
478+
storageModifications.PolygonZkEVMTimelock = timelockStorageWrites;
479+
const timelockStorageAdmin = await getTraceStorageWrites(txTimelockAdminRole.hash, timelockContractAddress);
480+
const timelockStorageRevokeAdmin = await getTraceStorageWrites(
481+
txRevokeTimelockAdminRole.hash,
482+
timelockContractAddress,
483+
);
494484
expect(timelockStorageAdmin[getStorageTimelockAdminRoleMember(genesisBaseAddresses.timelockAddress)]).to.equal(
495485
'0x0000000000000000000000000000000000000000000000000000000000000001',
496486
);
497-
expect(timelockStorageRevokeAdmin[getStorageTimelockAdminRoleMember(timelockContract.target)]).to.equal(
487+
expect(timelockStorageRevokeAdmin[getStorageTimelockAdminRoleMember(timelockContractAddress)]).to.equal(
498488
'0x0000000000000000000000000000000000000000000000000000000000000000',
499489
);
500-
storageModifications.PolygonZkEVMTimelock[getStorageTimelockAdminRoleMember(timelockContract.target)] =
490+
storageModifications.PolygonZkEVMTimelock[getStorageTimelockAdminRoleMember(timelockContractAddress)] =
501491
'0x0000000000000000000000000000000000000000000000000000000000000000';
502492
storageModifications.PolygonZkEVMTimelock[getStorageTimelockAdminRoleMember(genesisBaseAddresses.timelockAddress)] =
503493
'0x0000000000000000000000000000000000000000000000000000000000000001';
@@ -578,7 +568,7 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
578568
expectedStorageModifications.PolygonZkEVMTimelock = getExpectedStoragePolygonZkEVMTimelock(
579569
timelockMinDelay,
580570
genesisBaseAddresses.timelockAddress,
581-
timelockContract.target,
571+
timelockContractAddress,
582572
);
583573

584574
/// //////////////////////////////
@@ -662,7 +652,7 @@ export async function createGenesisHardhat(_genesisBase: any, initializeParams:
662652
// PolygonZkEVMTimelock
663653
actualStorage.PolygonZkEVMTimelock = await getActualStorage(
664654
storageModifications.PolygonZkEVMTimelock,
665-
timelockContract.target,
655+
timelockContractAddress,
666656
);
667657

668658
if (isDebug) {

src/genesis-hardhat/storage.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ const TIMELOCK = {
1212
PROPOSER_ROLE: '0x3412d5605ac6cd444957cedb533e5dacad6378b4bc819ebe3652188a665066d6',
1313
EXECUTOR_ROLE: '0xdae2aa361dfd1ca020a396615627d436107c35eff9fe7738a3512819782d706a',
1414
CANCELLER_ROLE: '0xc3ad33e20b0c56a223ad5104fff154aa010f8715b9c981fd38fdc60a4d1a52fc',
15-
TIMELOCK_ADMIN_ROLE_MEMBER_CONTRACT: '0x9e4caf1904fc74e2e6cd38a4ce7a4db3ad409934bcf022412e2666a66f1d982d',
16-
TIMELOCK_ADMIN_ROLE_MEMBER_CONTRACT_AGG: '0x0a3658910cdf5d09ef64f268ff5f8afe57e4ce252c3a604ef77db2bc1d972974',
1715
TIMELOCK_ADMIN_ROLE_MEMBER: '0x5260bb5bfdf6d30973fe112a01dd48b2c8373fa79b78e1c9ab401c7db917ed9d',
1816
PROPOSER_ROLE_MEMBER: '0x8654cc165397dae98eb68aab684c9a3739eac045dbd90d8f910615c3096d1913',
1917
CANCELLER_ROLE_MEMBER: '0xcda2e398301eb583724013ef4a2213cded1c301411a9911301f79b7957d53079',

src/genesis-hardhat/utils.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,21 @@ import { logger } from '../logger';
99
* @param {string} txHash - transaction hash
1010
* @returns {Object} - storage writes: { depth: {"key": "value"} }
1111
*/
12-
export async function getTraceStorageWrites(txHash: any) {
12+
export async function getTraceStorageWrites(txHash: any, address = undefined) {
13+
const infoTx = await ethers.provider.getTransaction(txHash);
14+
if (!infoTx) {
15+
throw new Error(`No info tx: ${txHash}`);
16+
}
17+
const addressInfo: { address?: string; nonce?: number } = {};
18+
if (!infoTx.to) {
19+
const receipt = await ethers.provider.getTransactionReceipt(txHash);
20+
addressInfo.address = receipt?.contractAddress?.toLowerCase();
21+
addressInfo.nonce = 1;
22+
} else {
23+
addressInfo.address = infoTx.to.toLowerCase();
24+
addressInfo.nonce = await ethers.provider.getTransactionCount(addressInfo.address);
25+
}
26+
1327
const trace = await ethers.provider.send('debug_traceTransaction', [
1428
txHash,
1529
{
@@ -19,8 +33,8 @@ export async function getTraceStorageWrites(txHash: any) {
1933
enableReturnData: false,
2034
},
2135
]);
22-
23-
const computedStorageWrites = getStorageWrites(trace);
36+
const computedStorageWrites = await getStorageWrites(trace, addressInfo);
37+
if (address) return computedStorageWrites[address.toLowerCase()];
2438
return computedStorageWrites;
2539
}
2640

@@ -472,6 +486,7 @@ export function deepEqual(a, b) {
472486
const keysB = Object.keys(b);
473487
if (keysA.length !== keysB.length) {
474488
logger.error(`Length mismatch: a: ${keysA.length}, b: ${keysB.length}`);
489+
logger.error(`Keys: ${keysA}`);
475490
return false;
476491
}
477492
// eslint-disable-next-line no-restricted-syntax

0 commit comments

Comments
 (0)