Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feath(filter): add handleBitmask #63

Merged
merged 9 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-zebras-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rabbitholegg/questdk": minor
---

Adds a $bitmask operator to filters
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
"editor.codeActionsOnSave": {
"source.organizeImports.rome": true,
"source.fixAll": true
"source.organizeImports.rome": "explicit",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is causing this, do you know?. I wake up every morning and this change is in my editor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old keyword has been deprecated - vscode auto updates I should update this in all repos so we don't get it anymore.

"source.fixAll": "explicit"
},
"[json]": {
"editor.defaultFormatter": "rome.rome"
Expand Down
69 changes: 69 additions & 0 deletions src/filter/filters.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
apply,
handleAnd,
handleBitmask,
handleGreaterThanOrEqual,
handleOr,
handleRegex,
Expand Down Expand Up @@ -67,6 +68,74 @@ describe('parser', () => {
})
})

describe('handleBitmask', () => {
test('applies the bitmask and compares the result to the provided value', () => {
const context = 0b1100
const filter = { bitmask: 0b0100, value: 0b0100 }
expect(handleBitmask(context, filter)).toBe(true)
})

test('returns false when the masked context does not equal the provided value', () => {
const context = 0b1100
const filter = { bitmask: 0b1100, value: 0b1000 }
expect(handleBitmask(context, filter)).toBe(false)
})

test('correctly handles large numbers', () => {
const context = '0x123456789abcdef0123456789abcdef0123456789abcdef'
const filter = {
bitmask: '0xfffffffffffffffffffffffffffffffffffffffffffffff',
value: '0x123456789abcdef0123456789abcdef0123456789abcdef',
}
expect(handleBitmask(context, filter)).toBe(true)
})

test('correctly handles string inputs', () => {
const context = '0b1100'
const filter = { bitmask: '0b1000', value: '0b1000' }
expect(handleBitmask(context, filter)).toBe(true)
})

test('should correctly filter the given transaction', () => {
const transaction = {
blockHash:
'0x2ed328c196cd31a299f51b8e7dc4dffef98184a37c0d8a1f40165f3fd9c668de',
blockNumber: '0x65715bb',
from: '0x4e187a21d8c49a175ee6ee9f2c2f3222f43d350e',
gas: '0x43707',
gasPrice: '0x777',
maxFeePerGas: '0x795',
maxPriorityFeePerGas: '0x735',
hash: '0xf175c933840e63013fbf77c7bab63d44f692003f5e07596e2dab8fedda55f8d3',
input:
'0xdf33dc1600018000000000000000000000000000000000000000000000000000000000000007f48928cec5cad1efeda632570843000000000000000000000000000000640000341940b709e8a6f6a2c930980000000341940b709e8a6f6a2c9309800000',
nonce: '0xa8',
to: '0xc4abade3a15064f9e3596943c699032748b13352',
transactionIndex: '0x4',
value: '0x0',
type: '0x2',
accessList: [],
chainId: '0xa',
v: '0x0',
r: '0x1e1b9faa0b562914b5b4f58e2f4dc67115d8f036227a855bc6c5ec2c0be237f5',
s: '0x6d4c35b77da3a0d934f2e186944820498d12546ae57f014927b2497ae367b6fe',
}

const filter = {
chainId: '0xa',
to: '0xc4abade3a15064f9e3596943c699032748b13352',
input: {
$bitmask: {
bitmask: '0xfffffff',
value: '0x9800000',
},
},
}

expect(apply(transaction, filter)).to.be.true
})
})

describe('handleAbstractAbiDecode', () => {
test('should return true when the ABI is found and applies', () => {
const transaction = {
Expand Down
22 changes: 21 additions & 1 deletion src/filter/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
AbiFilter,
AbiParamFilter,
AbstractAbiFilter,
BitmaskFilter,
Filter,
FilterObject,
TransactionFilter,
Expand Down Expand Up @@ -168,6 +169,17 @@ export const handleRegex = (context: any, filter: string): boolean => {
return re.test(context)
}

/**
* Applies a bitmask to the context and compares the result to a value.
* @param context - The context to apply the bitmask to.
* @param filter - An object containing the bitmask and the value to compare against.
* @returns True if the masked context is equal to the value, false otherwise.
*/
export const handleBitmask = (context: any, filter: BitmaskFilter): boolean => {
const maskedContext = BigInt(context) & BigInt(filter.bitmask)
return maskedContext === BigInt(filter.value)
}

/**
* Decodes ABI from the context using the filter.
* @param context - The context to decode.
Expand Down Expand Up @@ -289,6 +301,9 @@ const operators = {
$gte: handleGreaterThanOrEqual,
$gt: handleGreaterThan,
$regex: handleRegex,

// Bitmask operator
$bitmask: handleBitmask,
}

/**
Expand Down Expand Up @@ -355,7 +370,12 @@ export function apply(
// Handle the operator cases with a switch to enforce casing
// and type safety

if (!operator(context, filter as Filter[] & string & TransactionFilter)) {
if (
!operator(
context,
filter as Filter[] & string & TransactionFilter & BitmaskFilter,
)
) {
return false
}
continue
Expand Down
4 changes: 4 additions & 0 deletions src/filter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type Primitive = string | number | boolean
export type FilterObject = {
[key: string]: Filter
}
export type BitmaskFilter = {
bitmask: bigint | number | string
value: bigint | number | string
}
export type Filter = Primitive | FilterObject | FilterArray | Abi
export type FilterArray = Filter[]
export interface AbiFilter extends FilterObject {
Expand Down