Skip to content

Commit

Permalink
Merge pull request #139 from 0xPolygonHermez/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
krlosMata committed Feb 29, 2024
2 parents fefa98e + 6de80b8 commit eb1ed1a
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 133 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": "3.0.0",
"version": "5.0.0",
"main": "index.js",
"scripts": {
"setup": "npm i",
Expand Down Expand Up @@ -53,7 +53,7 @@
"@ethereumjs/block": "^3.6.2",
"@ethereumjs/tx": "^3.4.0",
"@polygon-hermez/common": "2.6.4",
"@polygon-hermez/vm": "6.0.11",
"@polygon-hermez/vm": "6.0.12",
"ethereumjs-util": "^7.1.4",
"ethers": "^5.5.4",
"ffjavascript": "^0.2.55",
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports.ADDRESS_BRIDGE = '0x9D98DeAbC42dd696Deb9e40b4f1CAB7dDBF55988';
module.exports.ADDRESS_GLOBAL_EXIT_ROOT_MANAGER_L2 = '0xa40D5f56745a118D0906a34E69aeC8C0Db1cB8fA';
module.exports.GLOBAL_EXIT_ROOT_STORAGE_POS = 0;
module.exports.LOCAL_EXIT_ROOT_STORAGE_POS = 1;
module.exports.BLOCK_GAS_LIMIT = Scalar.e('18446744073709551615'); // 2**64 - 1
module.exports.BLOCK_GAS_LIMIT = 1125899906842624;
module.exports.TX_GAS_LIMIT = 30000000;
module.exports.BATCH_DIFFICULTY = 0;
module.exports.ADDRESS_SYSTEM = '0x000000000000000000000000000000005ca1ab1e';
Expand Down
14 changes: 7 additions & 7 deletions src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ module.exports = class Processor {
// Compute rlp parsing counters
for (let i = 0; i < this.decodedTxs.length; i++) {
if (this.decodedTxs[i].tx.type === Constants.TX_CHANGE_L2_BLOCK) {
// If is forced and it contains a changeL2Block, invalid batch
if (this.isForced) {
this.isInvalid = true;

return;
}
this.vcm.computeFunctionCounters('decodeChangeL2BlockTx');
} else {
const txDataLen = this.decodedTxs[i].tx.data ? (this.decodedTxs[i].tx.data.length - 2) / 2 : 0;
Expand Down Expand Up @@ -402,6 +408,7 @@ module.exports = class Processor {
return;
}
}
const currentTx = currentDecodedTx.tx;

// If it is a forced batch, we create a changeL2Block at the beginning
if (i === 0 && this.isForced) {
Expand All @@ -415,14 +422,7 @@ module.exports = class Processor {
if (currentDecodedTx.isInvalid) {
continue;
} else {
const currentTx = currentDecodedTx.tx;
if (currentTx.type === Constants.TX_CHANGE_L2_BLOCK) {
// If it is forced batch, invalidate
if (this.isForced) {
this.isInvalid = true;

return;
}
// Final function call that saves internal DB storage keys
const err = await this._processChangeL2BlockTx(currentTx);
if (err) {
Expand Down
55 changes: 33 additions & 22 deletions src/virtual-counters-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { expectedModExpCounters } = require('./virtual-counters-manager-utils');
const FPEC = Scalar.e('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F');
const FNEC = Scalar.e('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141');
const FNEC_MINUS_ONE = Scalar.sub(FNEC, Scalar.e(1));
const spentCountersByFunction = {};

module.exports = class VirtualCountersManager {
/**
Expand Down Expand Up @@ -130,6 +131,14 @@ module.exports = class VirtualCountersManager {
});
this._verbose(spentCounters);
this.currentCountersSnapshot = spentCounters;
// Fill counters consumption by function
if (!spentCountersByFunction[this.calledFunc]) {
spentCountersByFunction[this.calledFunc] = spentCounters;
} else {
Object.keys(this.currentCountersSnapshot).forEach((counter) => {
spentCountersByFunction[this.calledFunc][counter] += spentCounters[counter];
});
}

return spentCounters;
}
Expand Down Expand Up @@ -939,9 +948,10 @@ module.exports = class VirtualCountersManager {
_checkJumpDest(input) {
this._checkInput(input, ['isCreate', 'isDeploy']);
this._reduceCounters(10, 'S');
this._reduceCounters(1, 'B');
if (input.isCreate) {
this._reduceCounters(1, 'B');
if (input.isDeploy) {
this._reduceCounters(1, 'B');
this._mLoadX();
}
}
Expand Down Expand Up @@ -980,7 +990,7 @@ module.exports = class VirtualCountersManager {
_opLog(input) {
this._opcode(input);
this._checkInput(input, ['inputSize']);
this._reduceCounters(30 + 8 * 4, 'S'); // Count steps as if topics is 4
this._reduceCounters(34 + 7 * 4, 'S'); // Count steps as if topics is 4
this._saveMem({ length: input.inputSize });
this._mulArith();
this._divArith();
Expand Down Expand Up @@ -1134,9 +1144,8 @@ module.exports = class VirtualCountersManager {
_opPush(input) {
this._opcode(input);
this._checkInput(input, ['pushBytes', 'isCreate', 'isDeploy']);
this._reduceCounters(4, 'S');
this._reduceCounters(2, 'S');
if (input.isCreate || input.isDeploy) {
this._reduceCounters(1, 'B');
if (input.isCreate) {
this._reduceCounters(20, 'S');
this._mLoadX();
Expand All @@ -1145,7 +1154,6 @@ module.exports = class VirtualCountersManager {
this._reduceCounters(10, 'S');
for (let i = 0; i < input.pushBytes; i++) {
this._reduceCounters(10, 'S');
this._SHLarith();
}
}
} else {
Expand Down Expand Up @@ -1392,23 +1400,25 @@ module.exports = class VirtualCountersManager {

_readPush(input) {
this._checkInput(input, ['pushBytes']);
this._reduceCounters(15, 'S');
this._reduceCounters(1, 'B');

const numBlocks = Math.ceil(input.pushBytes / 4);
const leftBytes = input.pushBytes % 4;

for (let i = 0; i <= numBlocks; i++) {
this._reduceCounters(20, 'S');
this._reduceCounters(1, 'B');
for (let j = i - 1; j > 0; j--) {
this._reduceCounters(8, 'S');
}
}

for (let i = 0; i < leftBytes; i++) {
this._reduceCounters(40, 'S');
this._reduceCounters(4, 'B');
switch (input.pushBytes) {
case 1:
this._reduceCounters(2, 'S');
break;
case 2:
this._reduceCounters(4, 'S');
break;
case 3:
this._reduceCounters(5, 'S');
break;
case 4:
this._reduceCounters(6, 'S');
break;
case 32:
this._reduceCounters(45, 'S');
break;
default:
this._reduceCounters(6 + input.pushBytes * 2, 'S'); // approx value, is a bit less
break;
}
}

Expand Down Expand Up @@ -1648,6 +1658,7 @@ module.exports = class VirtualCountersManager {
// MCP + 100S + divArith + batchL2DataLength/136K + K
this._reduceCounters(100, 'S');
this._reduceCounters(MCP, 'P');
this._reduceCounters(2, 'B');
this._divArith();
this._reduceCounters(Math.ceil((batchL2DataLength + 1) / 136), 'K');
}
Expand Down
2 changes: 1 addition & 1 deletion test/block-info.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ describe('Block info tests', function () {
logIndex += logs.length;
// Consolidate block
blockInfoRoot = await setBlockGasUsed(smt, blockInfoRoot, cumulativeGasUsed);
expect(smtUtils.h4toString(blockInfoRoot)).to.be.equal(blockInfoTree[i].finalBlockInfoRoot);
}
expect(smtUtils.h4toString(blockInfoRoot)).to.be.equal(blockInfoTree[i].finalBlockInfoRoot);
}
});
});
2 changes: 1 addition & 1 deletion test/contract-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('contractUtils', function () {
const expectedBatchHashData = '0x5e7875ab198c4d93379c92990a5d0111af59a0e62b2c4a0e3898e5bd24a18e58';
// TODO: input taken from pil-stark
const expectedStarkHashExecutor = '0xcfae2cfa3b8f3f12abce1bccd90e9b203dfdbe56c0c412114f2d3e67c9a897db';
const expectedSnarkInputHash = '18866106746266856524258279357742200690030828045012096945213758495992135252470';
const expectedSnarkInputHash = '14261007919052674347941079765745619634521069459113175679360275962408703057507';

before(async () => {
testVector = JSON.parse(fs.readFileSync(path.join(pathTestVectors, 'inputs-executor/input_executor.json')));
Expand Down
Loading

0 comments on commit eb1ed1a

Please sign in to comment.