@@ -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,37 @@ 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 only source file: ` declareLibrary ` + ` {.ffi.} ` /
466+ ` {.ffiCtor.} ` / ` {.ffiDtor.} ` / ` {.ffiEvent.} ` annotations and the ported
467+ libp2p logic; ` genBindings() ` emits the bindings.
468+ - ` c_bindings/ ` — generated C header (` libp2p.h ` ), consumed by
469+ ` logos-co/logos-libp2p-module ` .
470+ - ` cddl_bindings/ ` — generated CDDL schema for the CBOR wire format.
468471
469472``` sh
470473cd cbind
471- nimble libDynamic # Build .so/.dylib/.dll
472- nimble libStatic # Build .a
473- nimble examples # Build and run C examples
474+ nimble setup
475+ nimble buildffi # Build ../build/liblibp2p.{so,dylib,dll}
476+ nimble genbindings_c # Generate c_bindings/libp2p.h
477+ nimble genbindings_cddl # Generate the CDDL schema
474478```
475479
476480** 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
481+ - Requests/responses are CBOR; declare ` {.ffi.} ` object types for them — never
482+ raw ` ptr ` /` pointer ` across the boundary.
483+ - Stream handles are plain ` uint64 ` ids tracked in ` LibP2P ` state (nim-ffi has
484+ no handle type for incoming streams).
485+ - Convert config-parsing failures to ` return err(...) ` ; never ` raiseAssert ` on
486+ host-supplied input (the constructor returns a ` Result ` ).
487+ - Protocol/pubsub handlers are ` {.ffiEvent.} ` — host code subscribes via the
488+ generated ` libp2p_add_event_listener ` .
479489
480490---
481491
@@ -489,7 +499,7 @@ nimble examples # Build and run C examples
489499| ` daily_ci_report.yml ` | Daily CI failure reporting: opens/updates GitHub issues for failed daily CI runs |
490500| ` daily_nimbus.yml ` | Nimbus-specific test matrix |
491501| ` daily_runnable_examples.yml ` | Daily runnable examples checks |
492- | ` cbindings.yml ` | C bindings compilation and tests |
502+ | ` cbindings.yml ` | FFI library build + C/CDDL binding generation |
493503| ` coverage.yml ` | Code coverage (uploads to codecov) |
494504| ` linters.yml ` | nph formatting checks |
495505| ` pr_lint.yml ` | PR title/description linting |
0 commit comments