Skip to content

Feat/P2P FFI Layer#101

Draft
nipsysdev wants to merge 16 commits intodurability-labs:mainfrom
nipsysdev:feat/ffi_layer
Draft

Feat/P2P FFI Layer#101
nipsysdev wants to merge 16 commits intodurability-labs:mainfrom
nipsysdev:feat/ffi_layer

Conversation

@nipsysdev
Copy link
Copy Markdown

@nipsysdev nipsysdev commented Mar 3, 2026

This PR is an attempt at implementing a C FFI library for Archivist.
I tried following the work that was done in the Logos Storage FFI layer.

This is still a WIP, here are some potential issues that I've identified and remain to be addressed:

Remaining issues to address:

1. Stack-allocated NodeServer stored in shared context pointer

File: library/archivist_context.nim:108

A local stack variable's address is stored in a shared context pointer. When the procedure exits, the stack frame is invalidated but the pointer remains, risking causing undefined behavior, crashes, or data corruption.

2. archivist_close missing logic

File: library/libarchivist.nim:183-191

The archivist_close function is essentially a no-op that only returns a success callback without performing any actual cleanup.

3. Missing implementation to query repo store

File: library/archivist_thread_requests/requests/node_storage_request.nim:120-133

The space() function returns hardcoded zero values for all storage metrics instead of querying the repository store.

4. Missing implementation to check block existence

File: library/archivist_thread_requests/requests/node_storage_request.nim:135-139

The hasLocalBlock() function always returns false regardless of whether the block exists.

5. Debug requests processing not implemented

File: library/archivist_thread_requests/requests/node_debug_request.nim

All 7 debug operations return stub responses instead of performing actual operations:

DEBUG - Returns hardcoded version "0.1.0" and status "running"
LOG_LEVEL - Only logs the request, doesn't change log level
CONNECTED_PEERS - Always returns "0"
CONNECTED_PEER_IDS - Always returns empty array
FIND_PEER - Validates input but returns empty JSON
PEER_INFO - Validates input but returns empty JSON
DISCONNECT - Validates input but returns "disconnected" without actual disconnection

6. Callback handling needs improvement for concurrency

File: library/archivist_thread_requests/archivist_thread_request.nim:52

Current limitations:

  • SPSC Channel limitation prevents concurrent request submissions from multiple client threads
  • Sequential processing of requests
  • Potential race conditions for progress callbacks
  • Immediate request cleanup risking use-after-free

7. Hardcoded archivist version and revision

File: library/archivist_thread_requests/requests/node_info_request.nim:20-22

Lines contain hardcoded values: archivistVersion = "0.1.0" and archivistRevision = "unknown".

TOML serialization issues

I had to manually implement TOML serialization as I wasn't able to leverage the existing toml_serialization library with custom readValue procedures.
See: (archivist/conf_serialization.nim)[https://github.com/nipsysdev/archivist-node/blob/a1098b2d55219f4779ea7664ab4dc9a09eb5fd83/archivist/conf_serialization.nim]

Test Suite Issues

The build commands work correclt,y but the test suites currently have runtime issues. Tests fail with SIGSEGV or hang indefinitely.

Running the tests

make test

make test-context      # Context lifecycle tests
make test-config       # Configuration tests
make test-version      # Version information tests
make test-p2p          # P2P operations tests
make test-storage      # Storage operations tests
make test-upload       # Upload operations tests
make test-download     # Download operations tests
make test-edge-cases   # Edge case tests

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.

1 participant