Skip to content

Commit 3c42f68

Browse files
CopilotNicoSerranoP
andcommitted
Optimize encodeEventTopics by precomputing event topics (#16)
* Initial plan * Optimize encodeEventTopics by precomputing event topics Co-authored-by: NicoSerranoP <38594836+NicoSerranoP@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: NicoSerranoP <38594836+NicoSerranoP@users.noreply.github.com>
1 parent 4044f30 commit 3c42f68

5 files changed

Lines changed: 18 additions & 8 deletions

File tree

gas-benchmarks/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
ETH_RPC_URL=https://mainnet.gateway.tenderly.co
22

33
NUMBER_OF_TRANSACTIONS=100
4+
5+
MAX_NUMBER_OF_RPC_TRIES=50

gas-benchmarks/src/railgun/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { NUMBER_OF_TRANSACTIONS } from "../utils/constants.js";
66
* Maximum number of logs with a Shield event to be searched.
77
* Depends on the ratio of how many AA txs - EOA txs
88
*/
9-
export const MAX_OF_LOGS = NUMBER_OF_TRANSACTIONS * 5;
9+
export const MAX_OF_LOGS = NUMBER_OF_TRANSACTIONS * 10;
1010

1111
/**
1212
* Proxy contract that points to the RailgunSmartWallet contract:

gas-benchmarks/src/utils/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const ETH_RPC_URL = String(process.env.ETH_RPC_URL);
66

77
export const BLOCK_RANGE = 1_000n;
88

9-
export const MAX_NUMBER_OF_RPC_TRIES = 10;
9+
export const MAX_NUMBER_OF_RPC_TRIES = Number(process.env.MAX_NUMBER_OF_RPC_TRIES);
1010

1111
export const NUMBER_OF_TRANSACTIONS = Number(process.env.NUMBER_OF_TRANSACTIONS);
1212

gas-benchmarks/src/utils/rpc.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ export const publicClient = createPublicClient({
1919
chain: mainnet,
2020
});
2121

22-
export const getEventLogs = async ({ contractAddress, event, maxLogs }: GetEventLogs): Promise<Log[]> => {
22+
export const getEventLogs = async ({
23+
contractAddress,
24+
event,
25+
maxLogs,
26+
fromBlock: initialFromBlock,
27+
}: GetEventLogs): Promise<Log[]> => {
2328
const latestBlock = await publicClient.getBlockNumber();
2429
let toBlock = latestBlock;
25-
let fromBlock = getBlockInRange(toBlock);
30+
let fromBlock = initialFromBlock ?? getBlockInRange(toBlock);
2631

2732
let tries = 0;
2833
const logs: Log[] = [];
@@ -79,8 +84,10 @@ export const getUniqueLogs = (logs: Log[]): Log[] => {
7984
export const getTransactionsWithEvents = async (
8085
logs: Log[],
8186
events: readonly AbiEvent[],
82-
): Promise<TransactionReceipt[]> =>
83-
logs.reduce<Promise<TransactionReceipt[]>>(async (accumulatorPromise, log) => {
87+
): Promise<TransactionReceipt[]> => {
88+
const eventTopics = events.map((event) => encodeEventTopics({ abi: [event] })[0]);
89+
90+
return logs.reduce<Promise<TransactionReceipt[]>>(async (accumulatorPromise, log) => {
8491
const accumulator = await accumulatorPromise;
8592

8693
if (accumulator.length >= NUMBER_OF_TRANSACTIONS) {
@@ -93,9 +100,8 @@ export const getTransactionsWithEvents = async (
93100
return accumulator;
94101
}
95102

96-
const hasAllEvents = events.every((event, index) => {
103+
const hasAllEvents = eventTopics.every((eventTopic, index) => {
97104
const logTopic = receipt.logs[index]!.topics[0];
98-
const eventTopic = encodeEventTopics({ abi: [event] })[0];
99105

100106
return logTopic === eventTopic;
101107
});
@@ -108,3 +114,4 @@ export const getTransactionsWithEvents = async (
108114

109115
return accumulator;
110116
}, Promise.resolve([]));
117+
};

gas-benchmarks/src/utils/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export interface GetEventLogs {
1010
contractAddress: Hex;
1111
event: AbiEvent;
1212
maxLogs: number;
13+
fromBlock?: bigint;
1314
}

0 commit comments

Comments
 (0)