Skip to content

Commit

Permalink
Unit testing for computeL2TxHash function
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasirv committed Nov 28, 2023
1 parent dc1626f commit 5e7dbe5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/processor-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-use-before-define */
/* eslint-disable max-len */
const { ethers } = require('ethers');
const { Scalar } = require('ffjavascript');
Expand Down Expand Up @@ -355,11 +356,34 @@ function computeEffectiveGasPrice(gasPrice, effectivePercentage) {
* @returns computed l2 tx hash
*/
async function computeL2TxHash(tx) {
const txHash = await smtUtils.linearPoseidon(`0x${tx.nonce.slice(2)}${tx.gasPrice.slice(2)}${tx.gasLimit.slice(2)}${tx.to.slice(2)}${tx.value.slice(2)}${tx.data.slice(2)}${tx.from.slice(2)}${tx.effectivePercentage.slice(2)}`);
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 txHash = await smtUtils.linearPoseidon(hash);

return txHash;
}

function formatL2TxHashParam(param) {
if (param.startsWith('0x')) {
param = param.slice(2);
}
if (param === '00') {
return param;
}
// Remove leading zeros
param = param.replace(/^0+/, '');
// format to bytes
if (param.length % 2 === 1) {
param = `0${param}`;
}
// Checks hex correctness
const res = Buffer.from(param, 'hex').toString('hex');
if (res === '' || res.length !== param.length) {
throw new Error('Invalid hex string');
}

return res;
}

/**
* Decode string into a changeL2Transaction transaction type
* @param {String} _rawTx
Expand Down
60 changes: 60 additions & 0 deletions test/helpers/test-vectors/l2-tx-hash/txs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[
{
"nonce": "",
"gasPrice": "0x3b9aca00",
"gasLimit": "186a0",
"to": "0x1275fbb540c8efc58b812ba83b0d0b8b9917ae98",
"value": "0x100",
"data": "0xs5b8e9959000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000da00000000000000000000000000000000000000000000000000000000000000da608060405234801561001057600080fd5b5060bb8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063f6d97e8c14602d575b600080fd5b60336047565b604051603e91906062565b60405180910390f35b6000806003s90508091505090565b605c81607b565b82525050565b6000602082019050607560008301846055565b92915050565b600081905091905056fea2646970667358221220a33fdecaf587db45fa0e1fe4bfca25de09e35bb9a45fa6dab1bf1964244a929164736f6c63430008070033000000000000",
"from": "0x4d5Cf5032B2a844602278b01199ED191A86c93ff",
"effectivePercentage": "0xff",
"l2TxHash": "failed",
"reason": "Invalid nonce value"
},
{
"nonce": "0x00",
"gasPrice": "0x3b9aca00",
"gasLimit": "0x186a0",
"to": "0x005Cf5032B2a844602278b01199ED191A86c93ff",
"value": "0x00",
"data": "0x56d5be740000000000000000000000001275fbb540c8efc58b812ba83b0d0b8b9917ae98",
"from": "0x4d5Cf5032B2a844602278b01199ED191A86c93ff",
"effectivePercentage": "0x0f",
"l2TxHash": "0x7ba7d2f22cc11ea2d39808f11aecaf966ccae3b3d914eafe766b826f2aa7f6c4"
},
{
"nonce": "0x1",
"gasPrice": "0x3b9aca00",
"gasLimit": "0x186a0",
"to": "0x1275fbb540c8efc58b812ba83b0d0b8b9917ae98",
"value": "0x100",
"data": "0x5b8e9959000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000da00000000000000000000000000000000000000000000000000000000000000da608060405234801561001057600080fd5b5060bb8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063f6d97e8c14602d575b600080fd5b60336047565b604051603e91906062565b60405180910390f35b600080600390508091505090565b605c81607b565b82525050565b6000602082019050607560008301846055565b92915050565b600081905091905056fea2646970667358221220a33fdecaf587db45fa0e1fe4bfca25de09e35bb9a45fa6dab1bf1964244a929164736f6c63430008070033000000000000",
"from": "0x4d5Cf5032B2a844602278b01199ED191A86c93ff",
"effectivePercentage": "0xff",
"l2TxHash": "0x56ffb9dab72a8628dd290196d77eaefca60c52bcaff001d6451e1a7231bafd50"
},
{
"nonce": "0x1",
"gasPrice": "0x3b9aca00",
"gasLimit": "v186a0",
"to": "0x1275fbb540c8efc58b812ba83b0d0b8b9917ae98",
"value": "0x100",
"data": "0x5b8e9959000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000da00000000000000000000000000000000000000000000000000000000000000da608060405234801561001057600080fd5b5060bb8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063f6d97e8c14602d575b600080fd5b60336047565b604051603e91906062565b60405180910390f35b600080600390508091505090565b605c81607b565b82525050565b6000602082019050607560008301846055565b92915050565b600081905091905056fea2646970667358221220a33fdecaf587db45fa0e1fe4bfca25de09e35bb9a45fa6dab1bf1964244a929164736f6c63430008070033000000000000",
"from": "0x4d5Cf5032B2a844602278b01199ED191A86c93ff",
"effectivePercentage": "0xff",
"l2TxHash": "failed",
"reason": "Invalid gasLimit value"
},
{
"nonce": "0x21",
"gasPrice": "0x3b9aca00",
"gasLimit": "186a0",
"to": "0x1275fbb540c8efc58b812ba83b0d0b8b9917ae98",
"value": "0x100",
"data": "0xs5b8e9959000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000da00000000000000000000000000000000000000000000000000000000000000da608060405234801561001057600080fd5b5060bb8061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063f6d97e8c14602d575b600080fd5b60336047565b604051603e91906062565b60405180910390f35b6000806003s90508091505090565b605c81607b565b82525050565b6000602082019050607560008301846055565b92915050565b600081905091905056fea2646970667358221220a33fdecaf587db45fa0e1fe4bfca25de09e35bb9a45fa6dab1bf1964244a929164736f6c63430008070033000000000000",
"from": "0x4d5Cf5032B2a844602278b01199ED191A86c93ff",
"effectivePercentage": "0xff",
"l2TxHash": "failed",
"reason": "Invalid data value"
}
]
16 changes: 16 additions & 0 deletions test/processor-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,20 @@ describe('Processor utils', () => {
}
}
});

it('computeL2TxHash', async () => {
const txs = require('./helpers/test-vectors/l2-tx-hash/txs.json');
for (let i = 0; i < txs.length; i++) {
const tx = txs[i];
try {
const txHash = await processorUtils.computeL2TxHash(tx);
expect(txHash).to.be.equal(tx.l2TxHash);
} catch (e) {
if (e.message !== 'Invalid hex string') {
throw e;
}
expect(tx.l2TxHash).to.be.equal('failed');
}
}
});
});

0 comments on commit 5e7dbe5

Please sign in to comment.