Skip to content

Commit

Permalink
Merge pull request #75 from 0xPolygonHermez/feature/no-overwrite-time…
Browse files Browse the repository at this point in the history
…stamp

Feature/no overwrite timestamp
  • Loading branch information
krlosMata authored Dec 1, 2022
2 parents aada404 + edb3ffa commit e25455d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@0xpolygonhermez/zkevm-commonjs",
"description": "Javascript library implementing common utilities for zkevm",
"version": "0.5.0.2",
"version": "0.5.1.0",
"main": "index.js",
"scripts": {
"setup": "npm i",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://github.com/0xPolygonHermez/zkevm-commonjs#readme",
"devDependencies": {
"@0xpolygonhermez/contracts-zkevm": "github:0xPolygonHermez/zkevm-contracts#v0.5.0.0",
"@0xpolygonhermez/zkevm-contracts": "github:0xPolygonHermez/zkevm-contracts#v0.5.0.0",
"@ethersproject/abi": "^5.6.4",
"@nomiclabs/hardhat-ethers": "^2.1.0",
"@nomiclabs/hardhat-waffle": "^2.0.2",
Expand Down
19 changes: 18 additions & 1 deletion src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,32 @@ module.exports = class Processor {
/**
* Set the global exit root in a specific storage slot of the globalExitRootManagerL2 for both vm and SMT
* Not store global exit root if it is zero
* Not overwrite storage position if timestamp is already set
* This will be performed before process the transactions
*/
async _setGlobalExitRoot() {
// return if globalExitRoot is 0
if (Scalar.eq(smtUtils.h4toScalar(this.globalExitRoot), Scalar.e(0))) {
return;
}

const newStorageEntry = {};
// check if timestamp is already set
const globalExitRootPos = ethers.utils.solidityKeccak256(['uint256', 'uint256'], [smtUtils.h4toString(this.globalExitRoot), Constants.GLOBAL_EXIT_ROOT_STORAGE_POS]);
const globalExitRootPosScalar = Scalar.e(globalExitRootPos).toString();

const resTimestamp = await stateUtils.getContractStorage(
Constants.ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2,
this.smt,
this.currentStateRoot,
[globalExitRootPos],
);

if (Scalar.neq(resTimestamp[globalExitRootPosScalar], Scalar.e(0))) {
return;
}

// Set globalExitRoot - timestamp
const newStorageEntry = {};
newStorageEntry[globalExitRootPos] = this.timestamp;

this.currentStateRoot = await stateUtils.setContractStorage(
Expand Down
22 changes: 13 additions & 9 deletions test/block-info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const lodash = require('lodash');

const artifactsPath = path.join(__dirname, 'artifacts/contracts');

const contractsPolygonHermez = require('@0xpolygonhermez/contracts-zkevm');
const contractsPolygonHermez = require('@0xpolygonhermez/zkevm-contracts');
const {
MemDB, ZkEVMDB, getPoseidon, processorUtils, smtUtils, Constants, stateUtils,
} = require('../index');
Expand Down Expand Up @@ -269,18 +269,23 @@ describe('Block info tests', function () {
newLeafs[address].balance = account.balance.toString();
newLeafs[address].nonce = account.nonce.toString();

if (account.isContract() || address === Constants.ADDRESS_SYSTEM
|| address === Constants.ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2) {
if (account.isContract() || address.toLowerCase() === Constants.ADDRESS_SYSTEM.toLowerCase()
|| address.toLowerCase() === Constants.ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2.toLowerCase()) {
const storage = await zkEVMDB.dumpStorage(address);
newLeafs[address].storage = storage;
}
}
for (const leaf of genesis) {
if (!newLeafs[leaf.address.toLowerCase()]) {
newLeafs[leaf.address] = { ...leaf };
delete newLeafs[leaf.address].address;
delete newLeafs[leaf.address].bytecode;
delete newLeafs[leaf.address].contractName;
const address = leaf.address.toLowerCase();
if (!newLeafs[address]) {
newLeafs[address] = { ...leaf };
const storage = await zkEVMDB.dumpStorage(address);
if (storage !== null) {
newLeafs[address].storage = storage;
}
delete newLeafs[address].address;
delete newLeafs[address].bytecode;
delete newLeafs[address].contractName;
}
}

Expand Down Expand Up @@ -329,7 +334,6 @@ describe('Block info tests', function () {
))[Scalar.e(globalExitRootPos)];

expect(Scalar.fromString(timestampVm.toString('hex'), 16)).to.equal(timestampSmt);
expect(timestampSmt).to.equal(Scalar.e(batch.timestamp));

// Check through a call in the EVM
if (bridgeDeployed) {
Expand Down
106 changes: 101 additions & 5 deletions test/helpers/test-vectors/block-info/block-info-batches.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@
"expectedNewLeafs": {
"0xae4bb80be56b819606589de61d5ec3b522eeb032": {
"balance": "0",
"nonce": "0"
"nonce": "0",
"storage": {
"0x53d3f126103a7f7d6245751d2392e0245bf71479d10a3c27a5b501ace6dde1b7": "0x73e6af6f"
}
},
"0x0000000000000000000000000000000000000000": {
"balance": "0",
Expand Down Expand Up @@ -327,10 +330,6 @@
],
"expectedNewRoot": "0x5a62e3d4a89fdae8794ec78ba1fc91db4778bff0286ecb4cee6fbe8f76544ab6",
"expectedNewLeafs": {
"0xae4bb80be56b819606589de61d5ec3b522eeb032": {
"balance": "0",
"nonce": "0"
},
"0x4d5cf5032b2a844602278b01199ed191a86c93ff": {
"balance": "199999548204000000000",
"nonce": "14"
Expand Down Expand Up @@ -375,13 +374,110 @@
"0x0000000000000000000000000000000000000000": {
"balance": "0",
"nonce": "0"
},
"0xae4bb80be56b819606589de61d5ec3b522eeb032": {
"balance": "0",
"nonce": "0",
"storage": {
"0x53d3f126103a7f7d6245751d2392e0245bf71479d10a3c27a5b501ace6dde1b7": "0x73e6af6f"
}
}
},
"batchL2Data": "0xea07843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae988084188ec3568203e880800be264304cfc20c400929c82d1435cfbaa18e9a49c34ce65274633820803c415132ab24e00f892d77ad39c87e897772100e3323f31915a3e5eb261e056d99bc31bea08843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae988084d1a82a9d8203e88080775e9a77a45dafb120ec840926129c5db8ad3843722de88f2c7f6df4383c92fe0176dcd120e97ff6a1a1d60150aac3fdbca81bbfd1e2e9349ed9da4fa3bd85a81bea09843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae9880846d6516c08203e88080f969b5ab22f54db86a57efcfa83f3369047d328d2c56044bd49d7f863e69ed2b5a95c2b1b41801a5b4d7bb548829fe5a51c72dd5e5a08bd2a56f49bc3246a3081cea0a843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae988084b6baffe38203e88080b60beddfce8bf365e93cb4f88f45ed0e5649659de56dbf52332e6c9c16aa10bd576fb90bd967e2495fe8fa09d75d58b82668c54dab54d91f5e4ab0d76d2649ae1bea0b843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae9880841a93d1c38203e88080416d143b11a02e0c28344feb6af0f90ecb5b0bd5eec26047b25c0f775502be751e40d32fefe94aee39ad9b2605afeb2e86f31b56ac82874f841054c6a01331851bea0c843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae9880843408e4708203e88080a1ec9b236321a7d3545cfe1a1baedf65d75e589223d732d424576756adb0396e74470d226d5b61895150d96dbd1b1ba8a8ad69a6bf9311a141ece602b6420d961cf84a0d843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae9880a4e9413d3800000000000000000000000000000000000000000000000000000000000000018203e8808068ad61295298051fac15519066926aad57ed5af83b351e586a0fbe70006c48295695cc59543fd10d4af41a8a266aef5efd2d691abc356fc679c66e2545fd9ef61b",
"globalExitRoot": "0x090bcaf734c4f06c93954a827b45a6e8c67b8e0fd1e0a35a1c5982d6961828f9",
"newLocalExitRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"batchHashData": "0x8196946b8a211125e92b0bdbce0c9f9b8b8fbebf2cd5e794cbb4cf8208641b3c",
"timestamp": 1944498031
},
{
"txs": [],
"expectedNewRoot": "0xbde28b0b2f9284f2d15cc416369528306af5d3cbc53d7e2575a39ad62b30cd6a",
"expectedNewLeafs": {
"0xae4bb80be56b819606589de61d5ec3b522eeb032": {
"balance": "0",
"nonce": "0",
"storage": {
"0x476b1edb8f44f82bc7ef0b7e1c441a7957942d3a0db3d831311e898e665722b5": "0x73e6af71",
"0x53d3f126103a7f7d6245751d2392e0245bf71479d10a3c27a5b501ace6dde1b7": "0x73e6af6f"
}
},
"0x0000000000000000000000000000000000000000": {
"balance": "0",
"nonce": "0"
},
"0x617b3a3528f9cdd6630fd3301b9c8911f7bf063d": {
"balance": "100000000000000000000",
"nonce": "1",
"pvtKey": "0x28b2b0318721be8c8339199172cd7cc8f5e273800a35616ec893083a4b32c02e"
},
"0x1275fbb540c8efc58b812ba83b0d0b8b9917ae98": {
"balance": "0",
"nonce": "1",
"storage": {
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x617b3a3528f9cdd6630fd3301b9c8911f7bf063d",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x73e6af6f",
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x0a",
"0x0000000000000000000000000000000000000000000000000000000000000005": "0x01c9c380",
"0x0000000000000000000000000000000000000000000000000000000000000006": "0x03e8",
"0x0000000000000000000000000000000000000000000000000000000000000001": "0xc18eb9ea7e6dfc3cf833e81797701ec6fcebe074c766c76c10f3c6290b077742"
}
},
"0x4d5cf5032b2a844602278b01199ed191a86c93ff": {
"pvtKey": "0x4d27a600dce8c29b7bd080e29a26972377dbb04d7a27d919adbb602bf13cfd23",
"balance": "200000000000000000000",
"nonce": "0"
}
},
"batchL2Data": "0x",
"globalExitRoot": "0x190bcaf734c4f06c93954a827b45a6e8c67b8e0fd1e0a35a1c5982d6961828f9",
"newLocalExitRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"batchHashData": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
"timestamp": 1944498033
},
{
"txs": [],
"expectedNewRoot": "0xbde28b0b2f9284f2d15cc416369528306af5d3cbc53d7e2575a39ad62b30cd6a",
"expectedNewLeafs": {
"0x0000000000000000000000000000000000000000": {
"balance": "0",
"nonce": "0"
},
"0xae4bb80be56b819606589de61d5ec3b522eeb032": {
"balance": "0",
"nonce": "0",
"storage": {
"0x476b1edb8f44f82bc7ef0b7e1c441a7957942d3a0db3d831311e898e665722b5": "0x73e6af71",
"0x53d3f126103a7f7d6245751d2392e0245bf71479d10a3c27a5b501ace6dde1b7": "0x73e6af6f"
}
},
"0x617b3a3528f9cdd6630fd3301b9c8911f7bf063d": {
"balance": "100000000000000000000",
"nonce": "1",
"pvtKey": "0x28b2b0318721be8c8339199172cd7cc8f5e273800a35616ec893083a4b32c02e"
},
"0x1275fbb540c8efc58b812ba83b0d0b8b9917ae98": {
"balance": "0",
"nonce": "1",
"storage": {
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x617b3a3528f9cdd6630fd3301b9c8911f7bf063d",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x73e6af6f",
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x0a",
"0x0000000000000000000000000000000000000000000000000000000000000005": "0x01c9c380",
"0x0000000000000000000000000000000000000000000000000000000000000006": "0x03e8",
"0x0000000000000000000000000000000000000000000000000000000000000001": "0xc18eb9ea7e6dfc3cf833e81797701ec6fcebe074c766c76c10f3c6290b077742"
}
},
"0x4d5cf5032b2a844602278b01199ed191a86c93ff": {
"pvtKey": "0x4d27a600dce8c29b7bd080e29a26972377dbb04d7a27d919adbb602bf13cfd23",
"balance": "200000000000000000000",
"nonce": "0"
}
},
"batchL2Data": "0x",
"globalExitRoot": "0x190bcaf734c4f06c93954a827b45a6e8c67b8e0fd1e0a35a1c5982d6961828f9",
"newLocalExitRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"batchHashData": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
"timestamp": 1944498042
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion test/processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const lodash = require('lodash');

const artifactsPath = path.join(__dirname, 'artifacts/contracts');

const contractsPolygonHermez = require('@0xpolygonhermez/contracts-zkevm');
const contractsPolygonHermez = require('@0xpolygonhermez/zkevm-contracts');
const {
MemDB, ZkEVMDB, getPoseidon, processorUtils, smtUtils, Constants, stateUtils,
} = require('../index');
Expand Down

0 comments on commit e25455d

Please sign in to comment.