@@ -41,7 +41,7 @@ nim-libp2p/
4141│ ├── integration/ # Integration tests (WebSocket, AutoTLS, peer ID auth)
4242│ └── interop/ # Cross-implementation interoperability tests
4343├── examples/ # Tutorial and example applications
44- ├── cbind/ # C/FFI bindings layer
44+ ├── cbind/ # C/CDDL FFI bindings (generated via nim-ffi)
4545├── docs/ # Documentation
4646│ ├── README.md # Documentation index
4747│ ├── development.md # Setup and testing guide
@@ -206,7 +206,8 @@ These flags are used in CI and tests:
206206
207207### Memory Management
208208- Memory model: ` --mm:refc ` (reference counting)
209- - For C bindings (` cbind/ ` ): use ` createShared ` /` freeShared ` for cross-thread objects
209+ - For C bindings (` cbind/ ` ): the FFI runtime (threads, request channel, shared
210+ memory, CBOR codec) is provided by ` nim-ffi ` ; annotate procs/types instead
210211
211212### Style
212213
@@ -454,28 +455,38 @@ These flags are used in CI and tests:
454455
455456---
456457
457- ## C Bindings (` cbind/ ` )
458+ ## C/CDDL Bindings (` cbind/ ` )
458459
459- The ` cbind/ ` directory contains the C/FFI layer for using nim-libp2p from C/C++:
460+ The ` cbind/ ` directory exposes nim-libp2p to C via the [ ` nim-ffi ` ] ( https://github.com/logos-messaging/nim-ffi )
461+ framework. nim-ffi provides the worker thread, request channel, shared memory,
462+ CBOR codec and event queue, and * generates* the C/CDDL bindings from the
463+ annotations in ` libp2p.nim ` :
460464
461- - ` libp2p.nim ` — FFI function implementations (exported with ` {.exportc .} ` )
462- - ` libp2p.h ` — Generated C header
463- - ` ffi_types.nim ` — C-compatible type definitions
464- - ` types .nim` — Additional C-compatible type implementations
465- - ` alloc.nim ` — Cross-thread memory allocation helpers
466- - ` libp2p_thread/ ` — Thread management for async operations from C
467- - ` examples/cbindings.c ` , ` examples/echo.c ` — C usage examples
465+ - ` libp2p.nim ` — the top-level FFI module: ` declareLibrary ` + ` {.ffi .} ` /
466+ ` {.ffiCtor.} ` / ` {.ffiDtor.} ` / ` {.ffiEvent.} ` annotations and the ported
467+ libp2p logic; ` genBindings() ` emits the bindings.
468+ - ` libp2p/config .nim` — config types and parsing, ` include ` d into ` libp2p.nim ` .
469+ - ` c_bindings/ ` — generated C header ( ` libp2p.h ` ), consumed by
470+ ` logos-co/logos-libp2p-module ` .
471+ - ` cddl_bindings/ ` — generated CDDL schema for the CBOR wire format.
468472
469473``` sh
470474cd cbind
471- nimble libDynamic # Build .so/.dylib/.dll
472- nimble libStatic # Build .a
473- nimble examples # Build and run C examples
475+ nimble setup
476+ nimble buildffi # Build ../build/liblibp2p.{so,dylib,dll}
477+ nimble genbindings_c # Generate c_bindings/libp2p.h
478+ nimble genbindings_cddl # Generate the CDDL schema
474479```
475480
476481** cbind conventions** :
477- - Validate all ` cstring ` pointer parameters for ` nil ` before use; call the callback with ` RET_ERR ` if nil
478- - Use ` valueOr ` (not ` tryGet() ` ) when converting cstring multiaddresses to ` MultiAddress ` objects
482+ - Requests/responses are CBOR; declare ` {.ffi.} ` object types for them — never
483+ raw ` ptr ` /` pointer ` across the boundary.
484+ - Stream handles are plain ` uint64 ` ids tracked in ` LibP2P ` state (nim-ffi has
485+ no handle type for incoming streams).
486+ - Convert config-parsing failures to ` return err(...) ` ; never ` raiseAssert ` on
487+ host-supplied input (the constructor returns a ` Result ` ).
488+ - Protocol/pubsub handlers are ` {.ffiEvent.} ` — host code subscribes via the
489+ generated ` libp2p_add_event_listener ` .
479490
480491---
481492
@@ -489,9 +500,9 @@ nimble examples # Build and run C examples
489500| ` daily_ci_report.yml ` | Daily CI failure reporting: opens/updates GitHub issues for failed daily CI runs |
490501| ` daily_nimbus.yml ` | Nimbus-specific test matrix |
491502| ` daily_runnable_examples.yml ` | Daily runnable examples checks |
492- | ` cbindings.yml ` | C bindings compilation and tests |
503+ | ` cbindings.yml ` | FFI library build + C/CDDL binding generation |
493504| ` coverage.yml ` | Code coverage (uploads to codecov) |
494- | ` linters.yml ` | nph formatting checks |
505+ | ` linters.yml ` | nph (Nim) + clang-format (cbind C/H) formatting checks |
495506| ` pr_lint.yml ` | PR title/description linting |
496507| ` auto_assign_pr.yml ` | Automatically assigns reviewers to PRs |
497508| ` update_copilot_instructions.yml ` | Weekly automated update of copilot-instructions.md |
0 commit comments