Skip to content

Implement Expert Directory Pagination#48

Merged
Bosun-Josh121 merged 1 commit intoLightForgeHub:mainfrom
Josue19-08:feat/expert-directory-pagination
Mar 25, 2026
Merged

Implement Expert Directory Pagination#48
Bosun-Josh121 merged 1 commit intoLightForgeHub:mainfrom
Josue19-08:feat/expert-directory-pagination

Conversation

@Josue19-08
Copy link
Copy Markdown
Contributor

@Josue19-08 Josue19-08 commented Mar 25, 2026

🧠 SkillSphere Pull Request 🌐

Mark with an x all the checkboxes that apply (like [x])

Closes #44

  • Added tests (if necessary)
  • Ran cargo test (All tests passed)
  • Evidence attached (Screenshots, Logs, or Transaction Hashes)
  • Commented the code

📌 Type of Change

  • 📚 Documentation (updates to README, docs, or comments)
  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ Enhancement (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 🏗️ Refactor (code improvement/cleanup without logical changes)

📝 Changes Description

This PR implements pagination for the expert directory in the identity-registry-contract. The frontend/indexer needed to fetch experts in batches instead of individual indexes for performance.

Changes:

  • Added get_experts_paginated(env, start_index, limit) function in contract.rs that returns a vector of expert addresses
  • Exposed the function in the public API (lib.rs)
  • Added comprehensive test with 15 experts to validate pagination behavior (fetch 0-10 returns 10, fetch 10-20 returns 5)

Impact: This allows efficient batch fetching of large expert lists, significantly improving performance for the frontend and indexer.


📸 Evidence

All tests pass including the new pagination test:

running 21 tests
test test::test_ban_before_contract_initialized ... ok
test test::test_batch_verification_no_admin - should panic ... ok
test test::test_batch_verification_max_vec - should panic ... ok
test test::test_add_expert_unauthorized - should panic ... ok
test test::test_ban_unverified_expert ... ok
test test::test_ban_expert_unauthorized - should panic ... ok
test test::test_ban_expert ... ok
test test::test_data_uri_persisted_on_verify ... ok
test test::test_complete_expert_lifecycle ... ok
test test::test_add_expert ... ok
test test::test_expert_directory_no_duplicates_on_reverify ... ok
test test::test_initialization ... ok
test test::test_expert_status_changed_event ... ok
test test::test_update_profile_rejections ... ok
test test::test_update_profile_updates_uri_and_emits_event ... ok
test test::test_getters ... ok
test test::test_expert_directory_enumeration ... ok
test test::test_ban_expert_workflow ... ok
test test::test_expert_directory_via_batch_add ... ok
test test::test_batch_verification_check_status ... ok
test test::test_expert_pagination ... ok

test result: ok. 21 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

🌌 Comments

The implementation handles edge cases properly:

  • Returns remaining experts if start_index + limit exceeds total
  • Returns empty vector if start_index is beyond total experts
  • Maintains the same chronological order as get_expert_by_index

Thank you for contributing to SkillSphere! 🌍

We are glad you have chosen to help us democratize access to knowledge on the Stellar network. Your contribution brings us one step closer to a trustless, peer-to-peer consulting economy. Let's build the future together! 🚀

Summary by CodeRabbit

  • New Features

    • Added pagination support for expert queries, allowing retrieval of experts in specified batches using start index and limit parameters for improved query efficiency.
  • Tests

    • Added unit tests validating pagination functionality across various batch sizes and boundary conditions.

Add get_experts_paginated function to efficiently fetch experts in batches.
This addresses the performance issue of fetching large expert lists.

- Implement get_experts_paginated in contract.rs with start_index and limit
- Expose function in public API (lib.rs)
- Add comprehensive test with 15 experts, validating pagination behavior
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 25, 2026

📝 Walkthrough

Walkthrough

A new pagination function is added to the identity registry contract to fetch expert addresses in batches. The implementation includes the core pagination logic in contract.rs, public API exposure in lib.rs, and comprehensive test coverage verifying fetch operations with multiple page ranges.

Changes

Cohort / File(s) Summary
Pagination Implementation
contracts/identity-registry-contract/src/contract.rs, contracts/identity-registry-contract/src/lib.rs
Added get_experts_paginated(env, start_index, limit) function that retrieves a bounded slice of expert addresses from storage by computing an end_index capped at total count and iterating through the range. Core logic and public contract method exposure.
Pagination Tests
contracts/identity-registry-contract/src/test.rs
Added test_expert_pagination unit test that registers 15 experts and validates pagination behavior with two fetches: (start=0, limit=10) returning 10 results, and (start=10, limit=10) returning 5 results, confirming ordering correctness.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • LightForceHub/SkillSphere-Contracts#28: The pagination getter directly utilizes the index-based helper functions (total count and get-by-index retrieval) introduced in this prior PR.

Poem

🐰 Hop, hop! The experts now come in batches,
No more one-by-one fetches—
Pagination's our trick, from start to the end,
Frontend and indexer, the best of friends!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: implementing pagination functionality for the expert directory.
Description check ✅ Passed The description follows the template structure, includes all key sections, checks relevant boxes, and provides comprehensive details about changes, testing, and edge cases.
Linked Issues check ✅ Passed All requirements from issue #44 are met: pagination function implemented in contract.rs, exposed in lib.rs via #[contractimpl], and comprehensive tests added with 15 experts validating (0,10)->10 and (10,10)->5 results.
Out of Scope Changes check ✅ Passed All changes are directly in-scope for issue #44; only the pagination function, its public API exposure, and related tests were modified with no extraneous changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
contracts/identity-registry-contract/src/test.rs (1)

585-625: Add an explicit out-of-range pagination assertion.

This test validates the main paths well, but it does not lock the start_index > total behavior. Adding that check here will prevent regressions on the empty-page contract.

✅ Suggested test extension
 fn test_expert_pagination() {
@@
     // Verify the last 5 experts match
     for i in 0..5 {
         assert_eq!(page2.get(i as u32).unwrap(), experts.get((i + 10) as u32).unwrap());
     }
+
+    // Fetch start beyond total (should return empty)
+    let page3 = client.get_experts_paginated(&20u64, &10u64);
+    assert_eq!(page3.len(), 0);
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@contracts/identity-registry-contract/src/test.rs` around lines 585 - 625, The
test_expert_pagination lacks an assertion for the out-of-range start index case;
extend the test_expert_pagination to call get_experts_paginated with a start
index greater than client.get_total_experts() (e.g., start = total + 1) and
assert the returned page is empty (len == 0) to lock expected start_index >
total behavior and prevent regressions in get_experts_paginated.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@contracts/identity-registry-contract/src/contract.rs`:
- Around line 168-172: The pagination end bound computation in
get_experts_paginated can overflow when computing start_index + limit; replace
the raw addition with saturating arithmetic (e.g.,
start_index.saturating_add(limit)) when computing end_index so the sum never
wraps, then compare that saturated sum to total to pick the min for end_index
(retain the existing variables end_index, start_index, limit, total and the same
branch logic but use saturating_add).

---

Nitpick comments:
In `@contracts/identity-registry-contract/src/test.rs`:
- Around line 585-625: The test_expert_pagination lacks an assertion for the
out-of-range start index case; extend the test_expert_pagination to call
get_experts_paginated with a start index greater than client.get_total_experts()
(e.g., start = total + 1) and assert the returned page is empty (len == 0) to
lock expected start_index > total behavior and prevent regressions in
get_experts_paginated.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 59fbd94b-125b-4a0f-a1ff-4b8366955336

📥 Commits

Reviewing files that changed from the base of the PR and between 01a3a5c and a09f6dd.

📒 Files selected for processing (3)
  • contracts/identity-registry-contract/src/contract.rs
  • contracts/identity-registry-contract/src/lib.rs
  • contracts/identity-registry-contract/src/test.rs

Comment on lines +168 to +172
let end_index = if start_index + limit > total {
total
} else {
start_index + limit
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

cd contracts/identity-registry-contract && head -n 180 src/contract.rs | tail -n 30

Repository: LightForgeHub/SkillSphere-Contracts

Length of output: 1031


🏁 Script executed:

cd contracts/identity-registry-contract && cat -n src/contract.rs | sed -n '150,180p'

Repository: LightForgeHub/SkillSphere-Contracts

Length of output: 1306


🏁 Script executed:

rg -n "get_experts_paginated" contracts/identity-registry-contract/src/contract.rs -A 25

Repository: LightForgeHub/SkillSphere-Contracts

Length of output: 708


🏁 Script executed:

rg -n "get_experts_paginated" contracts/identity-registry-contract/src/ -B 2 -A 2

Repository: LightForgeHub/SkillSphere-Contracts

Length of output: 2085


🏁 Script executed:

rg -n "#\[contract\]|#\[derive_contract\]|pub fn get_experts" contracts/identity-registry-contract/src/contract.rs -B 1 -A 5

Repository: LightForgeHub/SkillSphere-Contracts

Length of output: 474


🏁 Script executed:

fd "contract.rs|lib.rs|mod.rs" contracts/identity-registry-contract/src/ -type f

Repository: LightForgeHub/SkillSphere-Contracts

Length of output: 249


🏁 Script executed:

cat -n contracts/identity-registry-contract/src/lib.rs | head -n 80

Repository: LightForgeHub/SkillSphere-Contracts

Length of output: 3309


🏁 Script executed:

rg -n "#\[contract\]" contracts/identity-registry-contract/src/

Repository: LightForgeHub/SkillSphere-Contracts

Length of output: 141


Prevent u64 overflow in pagination bound calculation.

get_experts_paginated is a public contract method that accepts arbitrary start_index and limit values from external callers. On lines 168 and 171, start_index + limit can overflow; in Soroban release builds, this wraps to an unexpected value and breaks the bounds check. Use saturating arithmetic to ensure predictable, safe behavior.

🔧 Proposed fix
-    let end_index = if start_index + limit > total {
-        total
-    } else {
-        start_index + limit
-    };
+    let end_index = core::cmp::min(start_index.saturating_add(limit), total);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let end_index = if start_index + limit > total {
total
} else {
start_index + limit
};
let end_index = core::cmp::min(start_index.saturating_add(limit), total);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@contracts/identity-registry-contract/src/contract.rs` around lines 168 - 172,
The pagination end bound computation in get_experts_paginated can overflow when
computing start_index + limit; replace the raw addition with saturating
arithmetic (e.g., start_index.saturating_add(limit)) when computing end_index so
the sum never wraps, then compare that saturated sum to total to pick the min
for end_index (retain the existing variables end_index, start_index, limit,
total and the same branch logic but use saturating_add).

@Bosun-Josh121 Bosun-Josh121 merged commit f4f8b17 into LightForgeHub:main Mar 25, 2026
2 checks passed
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

Successfully merging this pull request may close these issues.

Pagination for Expert Directory

2 participants