Skip to content

Commit

Permalink
add handle null in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jiqiang90 committed Feb 3, 2025
1 parent 8bc7f8c commit 7509046
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 20 deletions.
58 changes: 56 additions & 2 deletions packages/node/src/starknet/block.starknet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0

import { EventEmitter2 } from '@nestjs/event-emitter';
import { StarknetBlock } from '@subql/types-starknet';
import { StarknetBlock, StarknetTransaction } from '@subql/types-starknet';
import { StarknetApi } from './api.starknet';
import {
filterLogsProcessor,
Expand All @@ -17,7 +17,6 @@ jest.setTimeout(100000);
describe('block filters', () => {
let strkApi: StarknetApi;
const eventEmitter = new EventEmitter2();
let blockData: StarknetBlock;

const fetchBlock = async (height: number) => {
const block = await strkApi.fetchBlock(height);
Expand Down Expand Up @@ -110,6 +109,61 @@ describe('block filters', () => {
to: '0x047aaaaad',
}),
).toBeFalsy();

//Filter with function is "null"
expect(
filterTransactionsProcessor(tx!, {
function: null,
}),
).toBeFalsy();

//Filter with to is "null"
expect(
filterTransactionsProcessor(tx!, {
to: null,
}),
).toBeFalsy();
});

it('filter with tx function or to is null', () => {
const mockTx1 = {
calldata: ['0xmock', null],
parseCallData: jest.fn(),
} as unknown as StarknetTransaction;
//Filter with function is "null"
expect(
filterTransactionsProcessor(mockTx1, {
function: null,
}),
).toBeTruthy();

//Filter with to is "null"
expect(
filterTransactionsProcessor(mockTx1, {
to: null,
}),
).toBeTruthy();

// test with decode calls
const mockTx2 = {
parseCallData: () => {
return [{ to: null, selector: null, calldata: [] }];
},
} as unknown as StarknetTransaction;

//Filter with function is "null"
expect(
filterTransactionsProcessor(mockTx2, {
function: null,
}),
).toBeTruthy();

//Filter with to is "null"
expect(
filterTransactionsProcessor(mockTx2, {
to: null,
}),
).toBeTruthy();
});

it('should filter tx with multiple conditions', async () => {
Expand Down
40 changes: 23 additions & 17 deletions packages/node/src/starknet/block.starknet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,16 @@ export function filterTransactionsProcessor(

// Only decode calls lazily if filter applies
const decodedCalls = transaction.parseCallData();
const func = filter.function;
const to = filter.to;

if (decodedCalls && decodedCalls?.length !== 0) {
if (filter.function) {
const index = decodedCalls?.findIndex(
(call) =>
hexEq(call.selector, filter.function!) ||
hexEq(call.selector, encodeSelectorToHex(filter.function!)),
if (func !== undefined) {
const index = decodedCalls?.findIndex((call) =>
func === null
? call.selector === null
: hexEq(call.selector, func) ||
hexEq(call.selector, encodeSelectorToHex(func)),
);
if (index === -1) {
return false;
Expand All @@ -97,11 +100,14 @@ export function filterTransactionsProcessor(
}
// do not return true here
}
if (filter.to || address) {
// if filter.to is not provided, we use address as filter
const filterAddress = filter.to ?? address;
const index = decodedCalls?.findIndex(
(call) => filterAddress && hexEq(call.to, filterAddress),
if (to !== undefined || address) {
// If filter.to is undefined, we use address as filter
// We also handle when filter.to is null
const filterAddress = to !== undefined ? to : address;
const index = decodedCalls?.findIndex((call) =>
filterAddress === null
? call.to === null
: filterAddress && hexEq(call.to, filterAddress),
);
if (index === -1) {
return false;
Expand All @@ -112,19 +118,19 @@ export function filterTransactionsProcessor(
}
// In case decode calls failed, we try to look into raw calldata
else {
if (filter.function) {
const index = transaction.calldata?.findIndex(
(call) =>
call === filter.function! ||
call === encodeSelectorToHex(filter.function!),
if (func !== undefined) {
const index = transaction.calldata?.findIndex((call) =>
func === null
? call === null
: call === func || call === encodeSelectorToHex(func),
);
if (index === -1) {
return false;
}
}
if (filter.to) {
if (to !== undefined) {
const index = transaction.calldata?.findIndex((call) =>
hexEq(call, filter.to!),
to === null ? call === null : hexEq(call, to),
);
if (index === -1) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/starknet/utils.starknet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export function formatTransaction(
// Handle "INVOKE V1 and V3"
if (
transaction.type === 'INVOKE' &&
transaction.version !== ('0x0' || '0x100000000000000000000000000000000')
transaction.version !== '0x0' &&
transaction.version !== '0x100000000000000000000000000000000'
) {
return decodeInvokeCalldata(transaction.calldata);
}
Expand Down

0 comments on commit 7509046

Please sign in to comment.