Skip to content

Commit e512eb1

Browse files
authored
Merge branch 'master' into fix/bucket-refresh-wrong-index
2 parents c729d05 + 502a36a commit e512eb1

57 files changed

Lines changed: 1468 additions & 7266 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/copilot-instructions.md

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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
470474
cd 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 |

.github/workflows/cbindings.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: C bindings examples
1+
name: C bindings
22

33
on:
44
push:
@@ -54,7 +54,7 @@ jobs:
5454
run: |
5555
nimble install_pinned
5656
57-
- name: Build and run c bindings examples
57+
- name: Build FFI library and generate C/CDDL bindings
5858
run: |
5959
nim --version
6060
nimble --version
@@ -64,18 +64,18 @@ jobs:
6464
cd cbind
6565
nimble install_pinned
6666
nimble setup
67+
nimble buildffi
68+
nimble genbindings_c
69+
nimble genbindings_cddl
70+
71+
- name: Build and run C bindings examples
72+
run: |
73+
cd cbind
6774
nimble examples
6875
69-
# Temporary parallel job: builds the new nim-ffi library (cbind/libp2p_ffi.nim)
70-
# and generates its C/CDDL bindings. Folded into the main flow at the flip PR,
71-
# when libp2p_ffi.nim replaces the legacy libp2p.nim.
72-
#
73-
# Disabled until the final PR of the nim-ffi migration series: libp2p_ffi.nim is
74-
# built up incrementally across PRs 2..9 and only compiles end-to-end at the
75-
# flip. Re-enable (drop `if: false`) once the library is complete and expected
76-
# to pass.
76+
# macOS coverage for the FFI library build + C/CDDL binding generation; the
77+
# main c-bindings job above covers linux.
7778
c-bindings-ffi:
78-
if: false
7979
timeout-minutes: 30
8080
strategy:
8181
fail-fast: false

.github/workflows/daily_ci_report.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
("Daily test all (latests dependnecies)", "daily_test_all_latest_deps.yml"),
4343
("Daily test all (without feature flags)", "daily_test_all_no_flags.yml"),
4444
("Daily runnable examples", "daily_runnable_examples.yml"),
45+
("Daily interop", "daily_interop.yml"),
4546
]
4647
4748
LABEL_NAME = "daily-ci-failure"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Daily interop
2+
3+
on:
4+
schedule:
5+
- cron: "30 7 * * *"
6+
workflow_dispatch:
7+
pull_request:
8+
types: [labeled, synchronize]
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
12+
cancel-in-progress: true
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
build-unified-testing-interop-images:
19+
name: Build unified-testing interop image (${{ matrix.name }})
20+
if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'run-daily-ci')
21+
runs-on: ubuntu-22.04
22+
strategy:
23+
fail-fast: false
24+
matrix:
25+
include:
26+
- name: hole-punching-v2
27+
dockerfile: interop/hole-punching-v2/Dockerfile
28+
- name: kad-dht-v2
29+
dockerfile: interop/kad-dht-v2/Dockerfile
30+
- name: transport-v2
31+
dockerfile: interop/transport-v2/Dockerfile
32+
- name: perf
33+
dockerfile: interop/perf/Dockerfile
34+
steps:
35+
- uses: actions/checkout@v6
36+
- uses: docker/setup-buildx-action@v4
37+
- name: Build image
38+
run: docker build -f ${{ matrix.dockerfile }} .

.github/workflows/mobile_android.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ jobs:
5959
test -f "$out/include/nim_ffi_prelude.h"
6060
test -f "$out/include/tinycbor/cbor.h"
6161
test -f "$out/include/cddl_bindings/libp2p.cddl"
62-
test -x "$out/bin/libp2p_ffi_android_check"
62+
test -x "$out/bin/libp2p_android_check"
6363
6464
readelf -h "$out/lib/liblibp2p.so" | tee liblibp2p.readelf
65-
readelf -h "$out/bin/libp2p_ffi_android_check" | tee check.readelf
65+
readelf -h "$out/bin/libp2p_android_check" | tee check.readelf
6666
6767
grep -q "Machine:[[:space:]]*${{ matrix.machine }}" liblibp2p.readelf
6868
grep -q "Machine:[[:space:]]*${{ matrix.machine }}" check.readelf
6969
7070
file "$out/lib/liblibp2p.so"
71-
file "$out/bin/libp2p_ffi_android_check"
71+
file "$out/bin/libp2p_android_check"
7272
7373
- name: Enable KVM for Android emulator
7474
if: matrix.abi == 'x86_64'
@@ -93,7 +93,7 @@ jobs:
9393
adb shell "mkdir -p /data/local/tmp/nim-libp2p"
9494
adb push "result-x86_64/lib/liblibp2p.so" "/data/local/tmp/nim-libp2p/"
9595
adb push "result-x86_64/lib/libc++_shared.so" "/data/local/tmp/nim-libp2p/"
96-
adb push "result-x86_64/bin/libp2p_ffi_android_check" "/data/local/tmp/nim-libp2p/"
97-
adb shell "chmod 755 /data/local/tmp/nim-libp2p/libp2p_ffi_android_check"
98-
adb shell "cd /data/local/tmp/nim-libp2p && LD_LIBRARY_PATH=/data/local/tmp/nim-libp2p ./libp2p_ffi_android_check; echo RC=\$?" | tee run.log
96+
adb push "result-x86_64/bin/libp2p_android_check" "/data/local/tmp/nim-libp2p/"
97+
adb shell "chmod 755 /data/local/tmp/nim-libp2p/libp2p_android_check"
98+
adb shell "cd /data/local/tmp/nim-libp2p && LD_LIBRARY_PATH=/data/local/tmp/nim-libp2p ./libp2p_android_check; echo RC=\$?" | tee run.log
9999
grep -q '^RC=0' run.log

.github/workflows/mobile_ios.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ jobs:
7171
test -f "$out/include/nim_ffi_prelude.h"
7272
test -f "$out/include/tinycbor/cbor.h"
7373
test -f "$out/include/cddl_bindings/libp2p.cddl"
74-
test -x "$out/bin/libp2p_ffi_ios_check"
74+
test -x "$out/bin/libp2p_ios_check"
7575
7676
otool -l "$out/lib/liblibp2p.dylib" | tee liblibp2p.otool
77-
otool -l "$out/bin/libp2p_ffi_ios_check" | tee check.otool
77+
otool -l "$out/bin/libp2p_ios_check" | tee check.otool
7878
lipo -info "$out/lib/liblibp2p.dylib" | tee liblibp2p.lipo
7979
lipo -info "$out/lib/liblibp2p.a" | tee liblibp2p_static.lipo
8080
@@ -84,7 +84,7 @@ jobs:
8484
grep -q "arm64" liblibp2p_static.lipo
8585
8686
file "$out/lib/liblibp2p.dylib"
87-
file "$out/bin/libp2p_ffi_ios_check"
87+
file "$out/bin/libp2p_ios_check"
8888
8989
- name: Run FFI check on iOS simulator
9090
if: matrix.target == 'simulator-arm64'
@@ -93,15 +93,15 @@ jobs:
9393
run_dir="$RUNNER_TEMP/nim-libp2p-ios-sim"
9494
rm -rf "$run_dir"
9595
mkdir -p "$run_dir/bin" "$run_dir/lib"
96-
cp "$out/bin/libp2p_ffi_ios_check" "$run_dir/bin/"
96+
cp "$out/bin/libp2p_ios_check" "$run_dir/bin/"
9797
cp "$out/lib/liblibp2p.dylib" "$run_dir/lib/"
9898
chmod -R u+w "$run_dir"
9999
100-
install_name="$(otool -L "$run_dir/bin/libp2p_ffi_ios_check" | awk '/liblibp2p\.dylib/{print $1; exit}')"
100+
install_name="$(otool -L "$run_dir/bin/libp2p_ios_check" | awk '/liblibp2p\.dylib/{print $1; exit}')"
101101
test -n "$install_name"
102-
install_name_tool -change "$install_name" "@executable_path/../lib/liblibp2p.dylib" "$run_dir/bin/libp2p_ffi_ios_check"
102+
install_name_tool -change "$install_name" "@executable_path/../lib/liblibp2p.dylib" "$run_dir/bin/libp2p_ios_check"
103103
install_name_tool -id "@rpath/liblibp2p.dylib" "$run_dir/lib/liblibp2p.dylib"
104-
codesign --force --sign - "$run_dir/lib/liblibp2p.dylib" "$run_dir/bin/libp2p_ffi_ios_check"
104+
codesign --force --sign - "$run_dir/lib/liblibp2p.dylib" "$run_dir/bin/libp2p_ios_check"
105105
106106
device_udid="$(
107107
xcrun simctl list devices --json | /usr/bin/python3 -c '
@@ -143,4 +143,4 @@ jobs:
143143
144144
xcrun simctl boot "$device_udid" || true
145145
xcrun simctl bootstatus "$device_udid" -b
146-
xcrun simctl spawn "$device_udid" "$run_dir/bin/libp2p_ffi_ios_check"
146+
xcrun simctl spawn "$device_udid" "$run_dir/bin/libp2p_ios_check"

cbind/alloc.nim

Lines changed: 0 additions & 145 deletions
This file was deleted.

0 commit comments

Comments
 (0)