Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
chore: automatic contracts details generation
Browse files Browse the repository at this point in the history
  • Loading branch information
benceharomi committed Oct 18, 2023
1 parent 247c90f commit 0a9a14f
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 147 deletions.
84 changes: 56 additions & 28 deletions SystemContractsHashes.json
Original file line number Diff line number Diff line change
@@ -1,101 +1,101 @@
[
{
"contractName": "AccountCodeStorage",
"sourceCodePath": "contracts/AccountCodeStorage.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/AccountCodeStorage.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/AccountCodeStorage.sol/AccountCodeStorage.json",
"bytecodeHash": "0x0100009b3cd9a137912ffbd406a1d73eaffbcf40a760f3956fea7e051f0c6101",
"sourceCodeHash": "0x721f289059b0bf670fecc03ad7a1f58ca7da2fe1fd9802457708944a82418590"
"sourceCodeHash": "0xf56f18d6ccec4a1e083ece9d5dea511b610905b3be42bf81e81e53f8a7028162"
},
{
"contractName": "BootloaderUtilities",
"sourceCodePath": "contracts/BootloaderUtilities.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/BootloaderUtilities.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/BootloaderUtilities.sol/BootloaderUtilities.json",
"bytecodeHash": "0x01000975e811c2ba4a3b28f70426598129f0029feb086714980f9513f59531c7",
"sourceCodeHash": "0xb03c4703cb68b22cd7f74e04fa8b5d43025b98737a53c830a50138e385e2628b"
"sourceCodeHash": "0xcb8d18786a9dca90524de992e3216f57d89192600c2aa758f071a6a6ae3162c4"
},
{
"contractName": "ComplexUpgrader",
"sourceCodePath": "contracts/ComplexUpgrader.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/ComplexUpgrader.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/ComplexUpgrader.sol/ComplexUpgrader.json",
"bytecodeHash": "0x0100005b2eef785c804dc40ec24b3c2339b11a314fec6eb91db551a2523d6a2b",
"sourceCodeHash": "0x774da238fac06b112f94e0b10064ce5dbb45dab9378ae7bc88fe096127c36b78"
"sourceCodeHash": "0x02b3234b8aa3dde88cf2cf6c1447512dd953ed355be9ba21c22d48ca6d3eee67"
},
{
"contractName": "Compressor",
"sourceCodePath": "contracts/Compressor.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/Compressor.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/Compressor.sol/Compressor.json",
"bytecodeHash": "0x010001b7a7bb988e52b8fca05d82bccf63ea34c6617ebea1765c91e911386756",
"sourceCodeHash": "0xfed07845a7f4b19cf41351b56687311d3f55cfca90a8d3b2a3d3df1737873bbd"
"sourceCodeHash": "0x214a2b123ecdf3b135709d0b6207b3d41d9e8c68a0aa74b88c64fc983382d7b0"
},
{
"contractName": "ContractDeployer",
"sourceCodePath": "contracts/ContractDeployer.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/ContractDeployer.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/ContractDeployer.sol/ContractDeployer.json",
"bytecodeHash": "0x010005bb3e1bb343565920b37c6c0d716dcfca45bbdada20a305e80ab60a6916",
"sourceCodeHash": "0x837fdb9e6f2616fe6e189417ff48f8960d1c79245d75b275684fa10c9ac7d6c3"
"sourceCodeHash": "0xed9088758b3cbc9c450da0ac18e0e11359efe7341219ac1c331a4f5712c2dacb"
},
{
"contractName": "DefaultAccount",
"sourceCodePath": "contracts/DefaultAccount.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/DefaultAccount.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/DefaultAccount.sol/DefaultAccount.json",
"bytecodeHash": "0x0100065d0ea6130f484f6cd4936f2d5114abc9961328d6acd8b311dd00b94546",
"sourceCodeHash": "0x0af09156a1485eb2f398a082027504281a8224f8d611662570d8a43efd967372"
"sourceCodeHash": "0x34aaf3d8fbe90cf35efcfa5d8361de8a97be0a7cb60b9b117cda0dfd78fab6a6"
},
{
"contractName": "EmptyContract",
"sourceCodePath": "contracts/EmptyContract.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/EmptyContract.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/EmptyContract.sol/EmptyContract.json",
"bytecodeHash": "0x01000007c08e60bc60d70f759bc49f2488b70054b0cec1a64f0cf27953448f4c",
"sourceCodeHash": "0xa1ba9e2afa6b403f23d21b8f173d273f93b522daaaba03f4cc75692ebc71a853"
"sourceCodeHash": "0x34cf9324829a0a1653486242a5dbee58aa93a8b9888415791bafe2c7a966400d"
},
{
"contractName": "ImmutableSimulator",
"sourceCodePath": "contracts/ImmutableSimulator.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/ImmutableSimulator.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/ImmutableSimulator.sol/ImmutableSimulator.json",
"bytecodeHash": "0x01000047b7e40b0e0f7bd7051e20853a49b972c6c0ac16872425067cb3288f08",
"sourceCodeHash": "0xcaaaa98efde534901cd80aee33fb65a8d69fe702ef0fffd1e840f607d8b67b7c"
"sourceCodeHash": "0x315e71df564977165decbbbda504fee9d3dd98b6ca1e5dc68572d74bc308b03f"
},
{
"contractName": "KnownCodesStorage",
"sourceCodePath": "contracts/KnownCodesStorage.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/KnownCodesStorage.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/KnownCodesStorage.sol/KnownCodesStorage.json",
"bytecodeHash": "0x0100008b806f904a40cadb94318db1d8a8ae9a579f46ee0b50432e4c221572ee",
"sourceCodeHash": "0x0900ec65dd10108eeb76e4123e5ee95db16e4caf76502698511c0ac0ac2e1c55"
"sourceCodeHash": "0x33c7e9af04650d7e802ecfcf099fefde1ddb1a4268f521c0d69dea014ce5853d"
},
{
"contractName": "L1Messenger",
"sourceCodePath": "contracts/L1Messenger.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/L1Messenger.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/L1Messenger.sol/L1Messenger.json",
"bytecodeHash": "0x010002fb863dc09dbfdae276418c307eb39af03f335a0b23a2edc8bcd1835fce",
"sourceCodeHash": "0x85d7b2639d7844735a6d2f166c77c45bd81f0197b3131d9d6a3594c313dc965a"
"sourceCodeHash": "0x1c355d04ecf4e4c39ab6481f2bb17e5e30d3aa4563342aaa4c9aa122ac3a14d3"
},
{
"contractName": "L2EthToken",
"sourceCodePath": "contracts/L2EthToken.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/L2EthToken.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/L2EthToken.sol/L2EthToken.json",
"bytecodeHash": "0x01000139b0930df0818b0f10f7c78feed9ca93020efcb72e749a7ea842d08576",
"sourceCodeHash": "0xc563fe25f07906ef60b17ccb48884784543f02493648f4f5bf19c9a9c74464d5"
"sourceCodeHash": "0xb8e404a5e82c50b9f0cfb6412049d1174df3fbe8af40750a756ad0c1cfefb593"
},
{
"contractName": "MsgValueSimulator",
"sourceCodePath": "contracts/MsgValueSimulator.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/MsgValueSimulator.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/MsgValueSimulator.sol/MsgValueSimulator.json",
"bytecodeHash": "0x0100006f5dab2685a586d5ebbd360a2c1c2d593df1ab8267d8e172d92a202bfa",
"sourceCodeHash": "0x2f4be7f4afe5ac19b02d62ae9a107c2fd6268d9cd7d96e997f049cb7eecb2c97"
"sourceCodeHash": "0x038cc8e7fe97ad4befa2d5ab4ae77fdefdecc20338142565b8086cd9342868ef"
},
{
"contractName": "NonceHolder",
"sourceCodePath": "contracts/NonceHolder.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/NonceHolder.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/NonceHolder.sol/NonceHolder.json",
"bytecodeHash": "0x0100012f7252eee16af884775bd3279b577bbed64f124349ac6179aeb6ae3cb8",
"sourceCodeHash": "0xfdc19bf88e636b5128e9591c9b4a072e6bb0dbe24d767685d00101e83393fa06"
"sourceCodeHash": "0xdfdd234e9d7f6cc7dfb0b9c8b6a2dea3dc40204539bfb836c9ae2bb1dc9cbb1f"
},
{
"contractName": "SystemContext",
"sourceCodePath": "contracts/SystemContext.sol",
"sourceCodePath": "cache-zk/solpp-generated-contracts/SystemContext.sol",
"bytecodePath": "artifacts-zk/cache-zk/solpp-generated-contracts/SystemContext.sol/SystemContext.json",
"bytecodeHash": "0x0100023f1761d12df53e8581fabcb359cb069bbd2a7a7a3ef0b49f2f5d46169a",
"sourceCodeHash": "0xd41f5281ff1d10055be4a06af5d35d86406a397e7bf9bb766e9f015fa4b57dce"
"sourceCodeHash": "0x60d9007efb7f1bf9417f0856f3799937357a64c2e5f858d13d3ee584e8b9832e"
},
{
"contractName": "EventWriter",
Expand Down Expand Up @@ -139,6 +139,34 @@
"bytecodeHash": "0x010000178d93b2d7d6448866009892223caf018a8e8dbcf090c2b9053a285f8d",
"sourceCodeHash": "0x8f5a719394836111c850774e89ffb22ef825ff4d24d116ca750888be906f0109"
},
{
"contractName": "bootloader_test",
"sourceCodePath": "bootloader/build/bootloader_test.yul",
"bytecodePath": "bootloader/build/artifacts/bootloader_test.yul/bootloader_test.yul.zbin",
"bytecodeHash": "0x0100037b0462ed355364eaabccbea2a018afad4c8841b9856514c027400f1b10",
"sourceCodeHash": "0x467a36057882d6740a016cda812798d1be9a0ea60cb7ef90996e2c5be55e75a4"
},
{
"contractName": "fee_estimate",
"sourceCodePath": "bootloader/build/fee_estimate.yul",
"bytecodePath": "bootloader/build/artifacts/fee_estimate.yul/fee_estimate.yul.zbin",
"bytecodeHash": "0x010009434283c0bc9f32e51a9aa84523ee7a381e3e0c5ae63f639998d915f54b",
"sourceCodeHash": "0x3fb415ac6f59c35ea17b85aabb551df1b44a6fc7e051c2e33f5fc76c17432167"
},
{
"contractName": "gas_test",
"sourceCodePath": "bootloader/build/gas_test.yul",
"bytecodePath": "bootloader/build/artifacts/gas_test.yul/gas_test.yul.zbin",
"bytecodeHash": "0x01000927ea81a1afe5a586853a9c43fb928bcf1f1fba51a19c48ce1b940867c7",
"sourceCodeHash": "0x84648c958714d952248b8553456b5a5e3860e00871f01644297531e991a67d64"
},
{
"contractName": "playground_batch",
"sourceCodePath": "bootloader/build/playground_batch.yul",
"bytecodePath": "bootloader/build/artifacts/playground_batch.yul/playground_batch.yul.zbin",
"bytecodeHash": "0x0100094d801bf4180d020692a95cf26a3c9adcaedfd5be47ec08b1637b0282da",
"sourceCodeHash": "0xe02bed16015da2f03dcf5a7ed1bf2132009e69f4bfb5335e13cc406327e84d5e"
},
{
"contractName": "proved_batch",
"sourceCodePath": "bootloader/build/proved_batch.yul",
Expand Down
181 changes: 62 additions & 119 deletions scripts/calculate-hashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,125 +11,67 @@ type ContractDetails = {
bytecodePath: string;
};

const SYSTEM_CONTRACTS_DETAILS: ContractDetails[] = [
// contracts dir sol
{
contractName: 'AccountCodeStorage',
sourceCodePath: 'contracts/AccountCodeStorage.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/AccountCodeStorage.sol/AccountCodeStorage.json'
},
{
contractName: 'BootloaderUtilities',
sourceCodePath: 'contracts/BootloaderUtilities.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/BootloaderUtilities.sol/BootloaderUtilities.json'
},
{
contractName: 'ComplexUpgrader',
sourceCodePath: 'contracts/ComplexUpgrader.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/ComplexUpgrader.sol/ComplexUpgrader.json'
},
{
contractName: 'Compressor',
sourceCodePath: 'contracts/Compressor.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/Compressor.sol/Compressor.json'
},
{
contractName: 'ContractDeployer',
sourceCodePath: 'contracts/ContractDeployer.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/ContractDeployer.sol/ContractDeployer.json'
},
{
contractName: 'DefaultAccount',
sourceCodePath: 'contracts/DefaultAccount.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/DefaultAccount.sol/DefaultAccount.json'
},
{
contractName: 'EmptyContract',
sourceCodePath: 'contracts/EmptyContract.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/EmptyContract.sol/EmptyContract.json'
},
{
contractName: 'ImmutableSimulator',
sourceCodePath: 'contracts/ImmutableSimulator.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/ImmutableSimulator.sol/ImmutableSimulator.json'
},
{
contractName: 'KnownCodesStorage',
sourceCodePath: 'contracts/KnownCodesStorage.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/KnownCodesStorage.sol/KnownCodesStorage.json'
},
{
contractName: 'L1Messenger',
sourceCodePath: 'contracts/L1Messenger.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/L1Messenger.sol/L1Messenger.json'
},
{
contractName: 'L2EthToken',
sourceCodePath: 'contracts/L2EthToken.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/L2EthToken.sol/L2EthToken.json'
},
{
contractName: 'MsgValueSimulator',
sourceCodePath: 'contracts/MsgValueSimulator.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/MsgValueSimulator.sol/MsgValueSimulator.json'
},
{
contractName: 'NonceHolder',
sourceCodePath: 'contracts/NonceHolder.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/NonceHolder.sol/NonceHolder.json'
},
{
contractName: 'SystemContext',
sourceCodePath: 'contracts/SystemContext.sol',
bytecodePath: 'artifacts-zk/cache-zk/solpp-generated-contracts/SystemContext.sol/SystemContext.json'
},
// contracts dir yul
{
contractName: 'EventWriter',
sourceCodePath: 'contracts/EventWriter.yul',
bytecodePath: 'contracts/artifacts/EventWriter.yul/EventWriter.yul.zbin'
},
// precompiles dir yul
{
contractName: 'EcAdd',
sourceCodePath: 'contracts/precompiles/EcAdd.yul',
bytecodePath: 'contracts/precompiles/artifacts/EcAdd.yul/EcAdd.yul.zbin'
},
{
contractName: 'EcMul',
sourceCodePath: 'contracts/precompiles/EcMul.yul',
bytecodePath: 'contracts/precompiles/artifacts/EcMul.yul/EcMul.yul.zbin'
},
{
contractName: 'Ecrecover',
sourceCodePath: 'contracts/precompiles/Ecrecover.yul',
bytecodePath: 'contracts/precompiles/artifacts/Ecrecover.yul/Ecrecover.yul.zbin'
},
{
contractName: 'Keccak256',
sourceCodePath: 'contracts/precompiles/Keccak256.yul',
bytecodePath: 'contracts/precompiles/artifacts/Keccak256.yul/Keccak256.yul.zbin'
},
{
contractName: 'SHA256',
sourceCodePath: 'contracts/precompiles/SHA256.yul',
bytecodePath: 'contracts/precompiles/artifacts/SHA256.yul/SHA256.yul.zbin'
},
// bootloader dir yul
{
contractName: 'proved_batch',
sourceCodePath: 'bootloader/build/proved_batch.yul',
bytecodePath: 'bootloader/build/artifacts/proved_batch.yul/proved_batch.yul.zbin'
}
];

type Hashes = {
sourceCodeHash: string;
bytecodeHash: string;
};

type SystemContractHashes = ContractDetails & Hashes;

const findDirsEndingWith =
(endingWith: string) =>
(path: string): string[] => {
const absolutePath = makePathAbsolute(path);
try {
const dirs = fs.readdirSync(absolutePath, { withFileTypes: true }).filter((dirent) => dirent.isDirectory());
const dirsEndingWithSol = dirs.filter((dirent) => dirent.name.endsWith(endingWith));
return dirsEndingWithSol.map((dirent) => dirent.name);
} catch (err) {
const msg = err instanceof Error ? err.message : 'Unknown error';
throw new Error(`Failed to read directory: ${absolutePath} Error: ${msg}`);
}
};

const SOLIDITY_ARTIFACTS_DIR = 'artifacts-zk';

const getSolidityContractDetails = (dir: string, contractName: string): ContractDetails => {
const bytecodePath = join(SOLIDITY_ARTIFACTS_DIR, dir, contractName + '.sol', contractName + '.json');
const sourceCodePath = join(dir, contractName + '.sol');
return {
contractName,
sourceCodePath,
bytecodePath
};
};

const getSolidityContractsDetails = (dir: string): ContractDetails[] => {
const bytecodesDir = join(SOLIDITY_ARTIFACTS_DIR, dir);
const dirsEndingWithSol = findDirsEndingWith('.sol')(bytecodesDir);
const contractNames = dirsEndingWithSol.map((d) => d.replace('.sol', ''));
const solidityContractsDetails = contractNames.map((c) => getSolidityContractDetails(dir, c));
return solidityContractsDetails;
};

const YUL_ARTIFACTS_DIR = 'artifacts';

const getYulContractDetails = (dir: string, contractName: string): ContractDetails => {
const bytecodePath = join(dir, YUL_ARTIFACTS_DIR, contractName + '.yul', contractName + '.yul.zbin');
const sourceCodePath = join(dir, contractName + '.yul');
return {
contractName,
sourceCodePath,
bytecodePath
};
};

const getYulContractsDetails = (dir: string): ContractDetails[] => {
const bytecodesDir = join(dir, YUL_ARTIFACTS_DIR);
const dirsEndingWithYul = findDirsEndingWith('.yul')(bytecodesDir);
const contractNames = dirsEndingWithYul.map((d) => d.replace('.yul', ''));
const yulContractsDetails = contractNames.map((c) => getYulContractDetails(dir, c));
return yulContractsDetails;
};

const makePathAbsolute = (path: string): string => {
return join(__dirname, '..', path);
};
Expand Down Expand Up @@ -187,8 +129,6 @@ const getSystemContractsHashes = (systemContractsDetails: ContractDetails[]): Sy
return systemContractHashes;
});

const OUTPUT_FILE_PATH = 'SystemContractsHashes.json';

const readSystemContractsHashesFile = (path: string): SystemContractHashes[] => {
const absolutePath = makePathAbsolute(path);
try {
Expand Down Expand Up @@ -240,6 +180,10 @@ const findDifferences = (newHashes: SystemContractHashes[], oldHashes: SystemCon
return differencesList;
};

const SOLIDITY_SOURCE_CODE_PATHS = ['cache-zk/solpp-generated-contracts'];
const YUL_SOURCE_CODE_PATHS = ['contracts', 'contracts/precompiles', 'bootloader/build'];
const OUTPUT_FILE_PATH = 'SystemContractsHashes.json';

const main = async () => {
const args = process.argv;
if (args.length > 3 || (args.length == 3 && !args.includes('--check-only'))) {
Expand All @@ -250,21 +194,20 @@ const main = async () => {
}
const checkOnly = args.includes('--check-only');

const newSystemContractsHashes = getSystemContractsHashes(SYSTEM_CONTRACTS_DETAILS);
const solidityContractsDetails = _.flatten(SOLIDITY_SOURCE_CODE_PATHS.map(getSolidityContractsDetails));
const yulContractsDetails = _.flatten(YUL_SOURCE_CODE_PATHS.map(getYulContractsDetails));
const systemContractsDetails = [...solidityContractsDetails, ...yulContractsDetails];

const newSystemContractsHashes = getSystemContractsHashes(systemContractsDetails);
const oldSystemContractsHashes = readSystemContractsHashesFile(OUTPUT_FILE_PATH);

if (_.isEqual(newSystemContractsHashes, oldSystemContractsHashes)) {
console.log('Calculated hashes match the hashes in the SystemContractsHashes.json file.');
console.log('Exiting...');
return;
}

const differences = findDifferences(newSystemContractsHashes, oldSystemContractsHashes);

console.log('Calculated hashes differ from the hashes in the SystemContractsHashes.json file. Differences:');
console.log(differences);

if (checkOnly) {
console.log('You can use the `yarn calculate-hashes` command to update the SystemContractsHashes.json file.');
console.log('Exiting...');
Expand Down

0 comments on commit 0a9a14f

Please sign in to comment.