Skip to content

Commit

Permalink
Refactor secure effective percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Nov 29, 2023
1 parent 5e7dbe5 commit 3240d7b
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 74 deletions.
21 changes: 21 additions & 0 deletions src/block-keys-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,31 @@ async function keyTxLogs(_txIndex, _logIndex) {
return poseidon(key1, hk0);
}

/**
* Leaf type 12:
* hk0: H([0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])
* key: H([txIndex[0:4], txIndex[4:8], txIndex[8:12], txIndex[12:16], txIndex[16:20], 0, SMT_KEY_BLOCK_HEADER_EFFECTIVE_PERCENTAGE, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
* @param {Number} txIndex - current tx index
* @returns {Scalar} - key computed
*/
async function keyTxEffectivePercentage(_txIndex) {
const poseidon = await getPoseidon();
const { F } = poseidon;

const constant = F.e(constants.SMT_KEY_BLOCK_HEADER_EFFECTIVE_PERCENTAGE);
const txIndex = scalar2fea(F, Scalar.e(_txIndex));

const key1 = [txIndex[0], txIndex[1], txIndex[2], txIndex[3], txIndex[4], txIndex[5], constant, F.zero];
const key1Capacity = stringToH4(constants.HASH_POSEIDON_ALL_ZEROES);

return poseidon(key1, key1Capacity);
}

module.exports = {
keyBlockHeaderParams,
keyTxLogs,
keyTxStatus,
keyTxHash,
keyTxCumulativeGasUsed,
keyTxEffectivePercentage,
};
18 changes: 17 additions & 1 deletion src/block-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { Scalar } = require('ffjavascript');

const constants = require('./constants');
const {
keyBlockHeaderParams, keyTxLogs, keyTxStatus, keyTxHash, keyTxCumulativeGasUsed,
keyBlockHeaderParams, keyTxLogs, keyTxStatus, keyTxHash, keyTxCumulativeGasUsed, keyTxEffectivePercentage,
} = require('./block-keys-utils');

/**
Expand Down Expand Up @@ -97,6 +97,21 @@ async function setCumulativeGasUsed(smt, root, txIndex, cumulativeGasUsed) {
return result.newRoot;
}

/**
* Set effective percentage to smt
* @param {Object} smt merkle tree structure
* @param {Array[Field]} root merkle tree root
* @param {Number} txIndex transaction index
* @param {Number|String(hex)} effectivePercentage transaction effectivePercentage
* @returns {Array[Field]} new state root
*/
async function setEffectivePercentage(smt, root, txIndex, effectivePercentage) {
const keyStatus = await keyTxEffectivePercentage(txIndex);
const result = await smt.set(root, keyStatus, Scalar.e(effectivePercentage));

return result.newRoot;
}

/**
* Set logs to smt
* @param {Number} logIndex current tx index
Expand All @@ -121,4 +136,5 @@ module.exports = {
setTxStatus,
setCumulativeGasUsed,
setTxLog,
setEffectivePercentage,
};
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports.SMT_KEY_BLOCK_HEADER_TRANSACTION_HASH = 8;
module.exports.SMT_KEY_BLOCK_HEADER_STATUS = 9;
module.exports.SMT_KEY_BLOCK_HEADER_CUMULATIVE_GAS_USED = 10;
module.exports.SMT_KEY_BLOCK_HEADER_LOGS = 11;
module.exports.SMT_KEY_BLOCK_HEADER_EFFECTIVE_PERCENTAGE = 12;

// SMT block header data leaf keys
module.exports.INDEX_BLOCK_HEADER_PARAM_BLOCK_HASH = 0;
Expand Down
8 changes: 3 additions & 5 deletions src/processor-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,11 @@ function computeEffectiveGasPrice(gasPrice, effectivePercentage) {

/**
* Computes the L2 transaction hash from a transaction
* @param {Object} tx tx to compute l2 hash, must have nonce, gasPrice, gasLimit, to, value, data, from, effectivePercentage in hex string
* @param {Object} tx tx to compute l2 hash, must have nonce, gasPrice, gasLimit, to, value, data, from in hex string
* @returns computed l2 tx hash
*/
async function computeL2TxHash(tx) {
const hash = `${formatL2TxHashParam(tx.nonce)}${formatL2TxHashParam(tx.gasPrice)}${formatL2TxHashParam(tx.gasLimit)}${formatL2TxHashParam(tx.to)}${formatL2TxHashParam(tx.value)}${formatL2TxHashParam(tx.data)}${formatL2TxHashParam(tx.from)}${formatL2TxHashParam(tx.effectivePercentage)}`;
const hash = `${formatL2TxHashParam(tx.nonce)}${formatL2TxHashParam(tx.gasPrice)}${formatL2TxHashParam(tx.gasLimit)}${formatL2TxHashParam(tx.to)}${formatL2TxHashParam(tx.value)}${formatL2TxHashParam(tx.data)}${formatL2TxHashParam(tx.from)}`;
const txHash = await smtUtils.linearPoseidon(hash);

return txHash;
Expand All @@ -366,11 +366,9 @@ function formatL2TxHashParam(param) {
if (param.startsWith('0x')) {
param = param.slice(2);
}
if (param === '00') {
if (param === '00' || param === '') {
return param;
}
// Remove leading zeros
param = param.replace(/^0+/, '');
// format to bytes
if (param.length % 2 === 1) {
param = `0${param}`;
Expand Down
8 changes: 5 additions & 3 deletions src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const {
} = require('./processor-utils');
const { valueToHexStr, getFuncName } = require('./utils');
const {
initBlockHeader, setBlockGasUsed, setTxStatus, setTxHash, setCumulativeGasUsed, setTxLog,
initBlockHeader, setBlockGasUsed, setTxStatus, setTxHash, setCumulativeGasUsed, setTxLog, setEffectivePercentage,
} = require('./block-utils');
const { verifyMerkleProof } = require('./mt-bridge-utils');
const { getL1InfoTreeValue } = require('./l1-info-tree-utils');
Expand Down Expand Up @@ -464,7 +464,7 @@ module.exports = class Processor {
this.cumulativeGasUsed += bufferToInt(txResult.receipt.gasUsed);
// Fill block info tree with tx receipt
const txHash = await computeL2TxHash(currentTx);
await this.fillReceiptTree(txResult.receipt, txHash);
await this.fillReceiptTree(txResult.receipt, txHash, currentTx.effectivePercentage);
this.txIndex += 1;

// Check transaction completed
Expand Down Expand Up @@ -851,7 +851,7 @@ module.exports = class Processor {
return false;
}

async fillReceiptTree(txReceipt, txHash) {
async fillReceiptTree(txReceipt, txHash, effectivePercentage) {
// Set tx hash at smt
this.blockInfoRoot = await setTxHash(this.smt, this.blockInfoRoot, this.txIndex, txHash);
// Set tx status at smt
Expand All @@ -867,6 +867,8 @@ module.exports = class Processor {
this.blockInfoRoot = await setTxLog(this.smt, this.blockInfoRoot, this.txIndex, this.logIndex, encoded);
this.logIndex += 1;
}
// Set tx effective percentage at smt
this.blockInfoRoot = await setEffectivePercentage(this.smt, this.blockInfoRoot, this.txIndex, effectivePercentage);
}

_rollbackBatch() {
Expand Down
34 changes: 17 additions & 17 deletions test/helpers/test-vectors/block-info/block-info-batches.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"rawTx": "0xf88a06843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae9880a4e9413d3800000000000000000000000000000000000000000000000000000000000000008207f4a098f56115cad149a409a86e7610863248799a31d80e87d46582d08a3c0933a40aa025455a74fa3ca2ada0bf39dac88a172d032583bfec15061f39184ef9f7012677"
}
],
"expectedNewRoot": "0x1ea30a0faa282a8a776980b56e17a5a6bdb132695718e86c008f3e728e3e2dfb",
"expectedNewRoot": "0xbfb64640ff59472e12fda55a3819aa7235c5c4827d6c82f0ef3e7845e18a19bf",
"expectedNewLeafs": {
"0x0000000000000000000000000000000000000000": {
"balance": "0",
Expand Down Expand Up @@ -208,7 +208,7 @@
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x01",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x01",
"0xa6eef7e35abe7026729641147f7915573c7e97b47efa546f5f6e3230263bcb49": "0xf5c30bbb2b8a1bdd2b881cc575b2acbd13578c7cd64250fe8b6770fa95f915b3",
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x61bcc37f71c94afe08e4ebba4d622db1ba762991f025d1d8443e378975373b25"
"0x0000000000000000000000000000000000000000000000000000000000000003": "0xba4664e731b3a45357ae51ffb89244b400830b039385ddc1b1bf26b8f3cb58f5"
}
}
},
Expand Down Expand Up @@ -347,17 +347,17 @@
"rawTx": "0xf88a0d843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae9880a4e9413d3800000000000000000000000000000000000000000000000000000000000000018207f3a068ad61295298051fac15519066926aad57ed5af83b351e586a0fbe70006c4829a05695cc59543fd10d4af41a8a266aef5efd2d691abc356fc679c66e2545fd9ef6"
}
],
"expectedNewRoot": "0xf606600981e9fbaeae9e82bba71c349851c1c73d7d196062ecc3919ef39538ef",
"expectedNewRoot": "0x3e640ff8401c4c6f38ea13864bc08ab39a5633081653079d93ea1b74ceecd991",
"expectedNewLeafs": {
"0x000000000000000000000000000000005ca1ab1e": {
"balance": "0",
"nonce": "0",
"storage": {
"0xa6eef7e35abe7026729641147f7915573c7e97b47efa546f5f6e3230263bcb49": "0xf5c30bbb2b8a1bdd2b881cc575b2acbd13578c7cd64250fe8b6770fa95f915b3",
"0xcc69885fda6bcc1a4ace058b4a62bf5e179ea78fd58a1ccd71c22cc9b688792f": "0x1ea30a0faa282a8a776980b56e17a5a6bdb132695718e86c008f3e728e3e2dfb",
"0xcc69885fda6bcc1a4ace058b4a62bf5e179ea78fd58a1ccd71c22cc9b688792f": "0xbfb64640ff59472e12fda55a3819aa7235c5c4827d6c82f0ef3e7845e18a19bf",
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x02",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x73e6af70",
"0x0000000000000000000000000000000000000000000000000000000000000003": "0xaa2f714e522bd5ca095f0c2607be65bca6e26997616e8414b02f2d7f15d916ee"
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x2c22759402067a30efb07f69b9bdbf9be8b9041521b60d42cd386798a7cee272"
}
},
"0x4d5cf5032b2a844602278b01199ed191a86c93ff": {
Expand All @@ -373,7 +373,7 @@
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x02",
"0x0000000000000000000000000000000000000000000000000000000000000005": "0xffffffff",
"0x0000000000000000000000000000000000000000000000000000000000000006": "0x03e8",
"0x0000000000000000000000000000000000000000000000000000000000000001": "0x1ea30a0faa282a8a776980b56e17a5a6bdb132695718e86c008f3e728e3e2dfb"
"0x0000000000000000000000000000000000000000000000000000000000000001": "0xbfb64640ff59472e12fda55a3819aa7235c5c4827d6c82f0ef3e7845e18a19bf"
}
},
"0x617b3a3528f9cdd6630fd3301b9c8911f7bf063d": {
Expand Down Expand Up @@ -410,18 +410,18 @@
"customRawTx": "0x0b0000000100000000"
}
],
"expectedNewRoot": "0x670f25d4b65fe8a89462957f38b071bbf9151e27fcb33703e67c61acea3f8b9d",
"expectedNewRoot": "0x1c5e08e0e7339ac762a0ca37b3a0216649956860a486539d7d7f263f631f831a",
"expectedNewLeafs": {
"0x000000000000000000000000000000005ca1ab1e": {
"balance": "0",
"nonce": "0",
"storage": {
"0xa6eef7e35abe7026729641147f7915573c7e97b47efa546f5f6e3230263bcb49": "0xf5c30bbb2b8a1bdd2b881cc575b2acbd13578c7cd64250fe8b6770fa95f915b3",
"0xcc69885fda6bcc1a4ace058b4a62bf5e179ea78fd58a1ccd71c22cc9b688792f": "0x1ea30a0faa282a8a776980b56e17a5a6bdb132695718e86c008f3e728e3e2dfb",
"0xcc69885fda6bcc1a4ace058b4a62bf5e179ea78fd58a1ccd71c22cc9b688792f": "0xbfb64640ff59472e12fda55a3819aa7235c5c4827d6c82f0ef3e7845e18a19bf",
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x03",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x73e6af71",
"0x0000000000000000000000000000000000000000000000000000000000000003": "0xffd9cbe85b79874f62772b854de2c2441f2ab87ebe7c7812c74d17595775b4c5",
"0xd9d16d34ffb15ba3a3d852f0d403e2ce1d691fb54de27ac87cd2f993f3ec330f": "0xf606600981e9fbaeae9e82bba71c349851c1c73d7d196062ecc3919ef39538ef"
"0x0000000000000000000000000000000000000000000000000000000000000003": "0xbca7b98639dd0ed53a23115277dd496a7e7ba9c48a67f19600fa2111afa3561d",
"0xd9d16d34ffb15ba3a3d852f0d403e2ce1d691fb54de27ac87cd2f993f3ec330f": "0x3e640ff8401c4c6f38ea13864bc08ab39a5633081653079d93ea1b74ceecd991"
}
},
"0x0000000000000000000000000000000000000000": {
Expand All @@ -446,7 +446,7 @@
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x02",
"0x0000000000000000000000000000000000000000000000000000000000000005": "0xffffffff",
"0x0000000000000000000000000000000000000000000000000000000000000006": "0x03e8",
"0x0000000000000000000000000000000000000000000000000000000000000001": "0x1ea30a0faa282a8a776980b56e17a5a6bdb132695718e86c008f3e728e3e2dfb"
"0x0000000000000000000000000000000000000000000000000000000000000001": "0xbfb64640ff59472e12fda55a3819aa7235c5c4827d6c82f0ef3e7845e18a19bf"
}
},
"0x4d5cf5032b2a844602278b01199ed191a86c93ff": {
Expand Down Expand Up @@ -476,19 +476,19 @@
"customRawTx": "0x0b0000000100000000"
}
],
"expectedNewRoot": "0x91d3f0d5e1ec9b99488af8802ff2196baf82f199575efe2856fd3a7fdd945238",
"expectedNewRoot": "0x452c349b33fef9d900f39db035dbad4bf4d29f3164ece359667fa956367664ea",
"expectedNewLeafs": {
"0x000000000000000000000000000000005ca1ab1e": {
"balance": "0",
"nonce": "0",
"storage": {
"0xa6eef7e35abe7026729641147f7915573c7e97b47efa546f5f6e3230263bcb49": "0xf5c30bbb2b8a1bdd2b881cc575b2acbd13578c7cd64250fe8b6770fa95f915b3",
"0xcc69885fda6bcc1a4ace058b4a62bf5e179ea78fd58a1ccd71c22cc9b688792f": "0x1ea30a0faa282a8a776980b56e17a5a6bdb132695718e86c008f3e728e3e2dfb",
"0xcc69885fda6bcc1a4ace058b4a62bf5e179ea78fd58a1ccd71c22cc9b688792f": "0xbfb64640ff59472e12fda55a3819aa7235c5c4827d6c82f0ef3e7845e18a19bf",
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x04",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x73e6af72",
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x13f4a6d84fc33929de5f39814d3a69686b5f890b6e6467e64789339c4df870ad",
"0xd9d16d34ffb15ba3a3d852f0d403e2ce1d691fb54de27ac87cd2f993f3ec330f": "0xf606600981e9fbaeae9e82bba71c349851c1c73d7d196062ecc3919ef39538ef",
"0x7dfe757ecd65cbd7922a9c0161e935dd7fdbcc0e999689c7d31633896b1fc60b": "0x670f25d4b65fe8a89462957f38b071bbf9151e27fcb33703e67c61acea3f8b9d"
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x5c3047b3f2da52f44c8ab51520fd8e7572fdafb907c4c94b3ab2672db732ac7d",
"0xd9d16d34ffb15ba3a3d852f0d403e2ce1d691fb54de27ac87cd2f993f3ec330f": "0x3e640ff8401c4c6f38ea13864bc08ab39a5633081653079d93ea1b74ceecd991",
"0x7dfe757ecd65cbd7922a9c0161e935dd7fdbcc0e999689c7d31633896b1fc60b": "0x1c5e08e0e7339ac762a0ca37b3a0216649956860a486539d7d7f263f631f831a"
}
},
"0x0000000000000000000000000000000000000000": {
Expand All @@ -513,7 +513,7 @@
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x02",
"0x0000000000000000000000000000000000000000000000000000000000000005": "0xffffffff",
"0x0000000000000000000000000000000000000000000000000000000000000006": "0x03e8",
"0x0000000000000000000000000000000000000000000000000000000000000001": "0x1ea30a0faa282a8a776980b56e17a5a6bdb132695718e86c008f3e728e3e2dfb"
"0x0000000000000000000000000000000000000000000000000000000000000001": "0xbfb64640ff59472e12fda55a3819aa7235c5c4827d6c82f0ef3e7845e18a19bf"
}
},
"0x4d5cf5032b2a844602278b01199ed191a86c93ff": {
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/test-vectors/block-info/block-info.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"rawTx": "0xf86a05843b9aca00830186a0941275fbb540c8efc58b812ba83b0d0b8b9917ae9880843408e4708207f4a0e255f868a61169a2330cb539671e1a66ee846bc06cc888ea8fcd7ae51cdc6294a00c6dec27e6f756dd84923bcebb1f85db125299c0b622eaa8d5722cb3dace239d"
}
],
"expectedNewRoot": "0x37f738b77b704136cf09610e12dcfbb8b321d7239f7c7ae625c4d7bcff4f3d19",
"expectedNewRoot": "0x1f4d481d6b596fe055200de68958254babf03b7a044c4f79fcd72785abb13dc0",
"expectedNewLeafs": {
"0x0000000000000000000000000000000000000000": {
"balance": "0",
Expand Down Expand Up @@ -199,7 +199,7 @@
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x01",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x73e6af6f",
"0xa6eef7e35abe7026729641147f7915573c7e97b47efa546f5f6e3230263bcb49": "0xf5c30bbb2b8a1bdd2b881cc575b2acbd13578c7cd64250fe8b6770fa95f915b3",
"0x0000000000000000000000000000000000000000000000000000000000000003": "0xdfce875e2b6e7c257682ab7d3951fd20654c51eb57324b8e7d1dbfedb877cbe9"
"0x0000000000000000000000000000000000000000000000000000000000000003": "0xe0bb01e479ac965f5fb3456df5d16f214a8e173b0384f34e2ca22a7eb950b2c3"
},
"hashBytecode": "0x0000000000000000000000000000000000000000000000000000000000000000",
"bytecodeLength": 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
"rawTx": "0xf9014f0480831e848094cfa773cc48fbde3ca4d24eecb19d224d697026b2880de0b6b3a7640000b8e4cd5865790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c949254d682d8c9ad5682521675b8f43b102aec40000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000008207f3a022849420ce892ab01b2aab19a51c3fac9169b5a26c9789d9ee43041b1214048da05962de13a880e2b38309e41e7255e43bb5c5937dbb8270a7f6d990344b61a84e"
}
],
"expectedNewRoot": "0x8cd26cf88420b79d07551400f38c79efa8567084c6b7fa77eb1cf649b8002557",
"expectedNewRoot": "0x3b724f7c47e9aacf4f7bc39da4c7716622e2bf8de81d79dc154664666d08133b",
"expectedNewLeafs": {
"0xa40d5f56745a118d0906a34e69aec8c0db1cb8fa": {
"balance": "0",
Expand Down Expand Up @@ -345,7 +345,7 @@
"0x0000000000000000000000000000000000000000000000000000000000000000": "0x01",
"0x0000000000000000000000000000000000000000000000000000000000000002": "0x73e6af6f",
"0xa6eef7e35abe7026729641147f7915573c7e97b47efa546f5f6e3230263bcb49": "0x24c02821c526d801fd1d6d99afd57fdf862f4d3e2109fc36eb85bd5e2f5c508e",
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x692c1af4c4e6856ac0d01c3f473d3a05a2cebedcb904df30b84c4872343409b6"
"0x0000000000000000000000000000000000000000000000000000000000000003": "0x16e19e0c8048076cfb38f920857552c30fae282c9efaea63e9e08bf0713b8213"
},
"hashBytecode": "0x0000000000000000000000000000000000000000000000000000000000000000",
"bytecodeLength": 0
Expand Down
Loading

0 comments on commit 3240d7b

Please sign in to comment.