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

[BUG]: Issues with Multiple Filters in MemoryDatabase.store.list Method #1165

Open
mshakeg opened this issue Feb 26, 2025 · 0 comments
Open

Comments

@mshakeg
Copy link
Contributor

mshakeg commented Feb 26, 2025

Description

When using the store.list method with multiple filters in the Sentio SDK's MemoryDatabase, we've encountered two distinct issues that break our processor tests:

  1. Two valid filters return no results: When combining two individually working filters, the combination produces an empty result set even when matching records exist
  2. Error when filtering with relation IDs: When filtering on a relation ID field alongside another filter, an error is thrown: TypeError: Cannot read properties of undefined (reading 'stringValue')

Reproduction Steps

You can reproduce both issues using our buggy-filter-scenarios branch.

To reproduce:

  1. Clone the repository and check out the branch
  2. Run the specific test case that demonstrates the issue:
     sentio test # there's no test filter option, but it's the "Stake with existing subscription" test case

The test is located at ./src/tests/multi-rewards/tests/stake.test.ts. The problematic code is in the event handler for onEventStakeEvent in the multi-rewards processor, where it attempts to fetch active subscriptions.

The problematic code pattern is:

// Get all active subscriptions for this user using list
const activeSubscriptions = await store.list(MRUserSubscription, [
  // Scenario 1: Two valid filters return empty results
  { field: "user_address", op: "=", value: userAddress },
  { field: "is_currently_subscribed", op: "=", value: true },
  
  // Scenario 2: Throws error with relation ID field
  // { field: "userID", op: "=", value: userAddress },
  // { field: "is_currently_subscribed", op: "=", value: true },
]);

The schema for the entity being filtered is:

type MRUserSubscription @entity {
  id: ID!
  user_address: String! @index
  pool_address: String! @index
  is_currently_subscribed: Boolean! @index
  pool: MRStakingPool!
  user: MRUser!
  staked_balance: MRUserStakedBalance!
  user_reward_datas: [MRUserRewardData!]! @derivedFrom(field: "subscription")
  subscribed_at: BigInt!
}

Expected Behavior

  1. When multiple valid filters are provided, records matching ALL filters should be returned
  2. Filtering on relation ID fields should work correctly and not throw errors

Actual Behavior

  1. Scenario 1: When applying two valid filters together (both of which work individually), an empty array is returned even when matching records exist
  2. Scenario 2: When using a relation ID field (userID) in a filter with another filter, the following error is thrown:
    TypeError [Error]: Cannot read properties of undefined (reading 'stringValue')
        at equal (/node_modules/@sentio/sdk/src/testing/memory-database.ts:256:13)
    

Analysis

After debugging, we found:

  1. Each filter works correctly when applied individually
  2. The issue appears to be in how the filter or equal functions in the MemoryDatabase handle multiple filters and relation ID fields
  3. The error in Scenario 2 suggests type mismatches when comparing field values for different field types

Current Workaround

We've implemented a workaround by applying filters sequentially in JavaScript:

// Get subscriptions by user first
const userSubscriptions = await store.list(MRUserSubscription, [
  { field: "user_address", op: "=", value: userAddress }
]);

// Then filter manually in JavaScript
const activeSubscriptions = userSubscriptions.filter(
  subscription => subscription.is_currently_subscribed === true
);

Environment Information

  • Sentio SDK Version: 2.57.8
  • Node.js Version: v22.12.0

Additional Notes

This bug may be related to type handling in the filter and equal functions in the MemoryDatabase implementation, specifically around how it handles:

  1. Applying multiple filters in combination
  2. Comparing fields of different types (strings, booleans, relation IDs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant