Skip to content

Commit

Permalink
Merge pull request #78 from KasarLabs/deoxys/fix_commitments
Browse files Browse the repository at this point in the history
Deoxys/fix commitments
  • Loading branch information
antiyro authored Oct 30, 2023
2 parents f80c038 + bfdf6b8 commit 009da45
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15,956 deletions.
11 changes: 8 additions & 3 deletions crates/primitives/commitments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,23 @@ pub fn calculate_transaction_hash_with_signature<H: HasherT>(
where
H: HasherT,
{
let signature_hash = if matches!(tx, Transaction::Invoke(_)) {
let include_signature = block_number >= 61394;

let signature_hash = if matches!(tx, Transaction::Invoke(_)) || include_signature {
// Include signatures for Invoke transactions or for all transactions
// starting from block 61394
H::compute_hash_on_elements(
&tx.signature().iter().map(|elt| FieldElement::from(*elt)).collect::<Vec<FieldElement>>(),
)
} else {
// Before block 61394, and for non-Invoke transactions, signatures are not included
H::compute_hash_on_elements(&[])
};

let transactions_hashes =
let transaction_hashes =
H::hash_elements(FieldElement::from(tx.compute_hash::<H>(chain_id, false, Some(block_number))), signature_hash);

transactions_hashes
transaction_hashes
}

/// Calculate the hash of an event.
Expand Down
84 changes: 28 additions & 56 deletions tests/tests/tests-deoxys/rpc/starknet_getBlockTransactionCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { performance } from "perf_hooks";
import * as dotenv from "dotenv";
dotenv.config();

const REMOTE_RPC_URL = process.env.REMOTE_RPC;
const LOCAL_RPC_URL = process.env.DEOXYS_RPC;
const BLOCK_NUMBER = 49;
const REMOTE_RPC_URL = process.env.REMOTE_RPC!;
const LOCAL_RPC_URL = process.env.DEOXYS_RPC!;
const BLOCK_NUMBER = 1058;
const START_BLOCK = 0;
const END_BLOCK = 3000;
const END_BLOCK = 1700;

const requestDataForMethod = (method: string, params: any[]) => ({
id: 1,
Expand All @@ -16,60 +16,36 @@ const requestDataForMethod = (method: string, params: any[]) => ({
params: params,
});

const compareObjects = (obj1: any, obj2: any, path: string = ""): string => {
let differences = "";

for (const key in obj1) {
const currentPath = path ? `${path}.${key}` : key;
// Handle cases where a key is not present in one of the objects or is undefined
if (obj1[key] === undefined) {
differences += `\x1b[31mDIFFERENCE in Alchemy at ${currentPath}: ${obj2[key]}\x1b[0m\n`;
continue;
}

if (obj2[key] === undefined) {
differences += `\x1b[31mDIFFERENCE in Local at ${currentPath}: ${obj1[key]}\x1b[0m\n`;
continue;
}
if (typeof obj1[key] === "object" && obj1[key] !== null) {
differences += compareObjects(obj1[key], obj2[key], currentPath);
} else if (obj1[key] !== obj2[key]) {
differences += `\x1b[31mDIFFERENCE at ${currentPath}: ${obj1[key]} (Alchemy) vs ${obj2[key]} (Local)\x1b[0m\n`;
} else {
differences += `\x1b[32mMATCH at ${currentPath}: ${obj1[key]}\x1b[0m\n`;
}
const compareTransactionCount = (remoteCount: number, localCount: number, blockNumber: number): string => {
if (remoteCount !== localCount) {
return `\x1b[31mDIFFERENCE at block ${blockNumber}: ${remoteCount} (Remote) vs ${localCount} (Local)\x1b[0m\n`;
} else {
return `\x1b[32mMATCH at block ${blockNumber}: ${remoteCount}\x1b[0m\n`;
}

return differences;
};

async function benchmarkMethod(method: string, params: any[]): Promise<string> {
console.log(
`\x1b[34mBenchmarking method: ${method}\x1b[0m for params: ${JSON.stringify(
params,
)}`,
);
async function benchmarkTransactionCount(blockNumber: number): Promise<string> {
console.log(`\x1b[34mBenchmarking transaction count for block: ${blockNumber}\x1b[0m`);

const alchemyResponse = await axios.post(REMOTE_RPC_URL, requestDataForMethod("starknet_getBlockTransactionCount", [{ block_number: blockNumber }]));
const localResponse = await axios.post(LOCAL_RPC_URL, requestDataForMethod("starknet_getBlockTransactionCount", [{ block_number: blockNumber }]));

const differences = compareTransactionCount(alchemyResponse.data.result, localResponse.data.result, blockNumber);

const alchemyResponse = await axios.post(
REMOTE_RPC_URL,
requestDataForMethod(method, params),
);
const localResponse = await axios.post(
LOCAL_RPC_URL,
requestDataForMethod(method, params),
);
if (differences.includes("\x1b[31mDIFFERENCE")) {
console.log(`\x1b[31mBlock ${blockNumber} has differences.\x1b[0m`);
} else {
console.log(`\x1b[32mBlock ${blockNumber} matches.\x1b[0m`);
}

return compareObjects(alchemyResponse.data, localResponse.data);
return differences;
}

async function checkDifferencesInBlocks() {
const blocksWithDifferences: number[] = [];

for (let blockNumber = START_BLOCK; blockNumber < END_BLOCK; blockNumber++) {
const differences = await benchmarkMethod(
"starknet_getBlockTransactionCount",
[{ block_number: blockNumber }],
);
const differences = await benchmarkTransactionCount(blockNumber);

if (differences.includes("\x1b[31mDIFFERENCE")) {
blocksWithDifferences.push(blockNumber);
Expand All @@ -79,19 +55,15 @@ async function checkDifferencesInBlocks() {
if (blocksWithDifferences.length === 0) {
console.log("\x1b[32mAll blocks match!\x1b[0m");
} else {
console.log(
"\x1b[31mDifferences found in blocks:\x1b[0m",
blocksWithDifferences,
);
console.log("\x1b[31mDifferences found in blocks:\x1b[0m", JSON.stringify(blocksWithDifferences));
}
}

(async () => {
const singleCallDifferences = await benchmarkMethod(
"starknet_getBlockTransactionCount",
[{ block_number: BLOCK_NUMBER }],
);
console.log(singleCallDifferences);
// Single block test
const singleBlockDifferences = await benchmarkTransactionCount(BLOCK_NUMBER);
console.log(singleBlockDifferences);

// Loop through blocks
await checkDifferencesInBlocks();
})();
6 changes: 3 additions & 3 deletions tests/tests/tests-deoxys/rpc/starknet_getBlockWithTxs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ dotenv.config();

const REMOTE_RPC_URL = process.env.REMOTE_RPC;
const LOCAL_RPC_URL = process.env.DEOXYS_RPC;
const BLOCK_NUMBER = 0;
const START_BLOCK = 15000;
const END_BLOCK = 16000;
const BLOCK_NUMBER = 61394;
const START_BLOCK = 58985;
const END_BLOCK = 80000;

const requestDataForMethod = (method: string, params: any[]) => ({
id: 1,
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/tests-deoxys/rpc/starknet_getClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dotenv.config();

const REMOTE_RPC_URL = process.env.REMOTE_RPC;
const LOCAL_RPC_URL = process.env.DEOXYS_RPC;
const BLOCK_NUMBER = 2000;
const BLOCK_NUMBER = 4000;
const START_BLOCK = 500;
const END_BLOCK = 1000;

Expand Down Expand Up @@ -98,7 +98,7 @@ async function checkDifferencesInBlocks() {

const singleCallDifferences = await benchmarkMethod("starknet_getClass", [
{ block_number: BLOCK_NUMBER },
"0x01cb96b938da26c060d5fd807eef8b580c49490926393a5eeb408a89f84b9b46",
"0x03131fa018d520a037686ce3efddeab8f28895662f019ca3ca18a626650f7d1e",
]);

console.log(singleCallDifferences);
Expand Down
Loading

0 comments on commit 009da45

Please sign in to comment.