Igor Sirotin 97b7601c41
refactor: build against the single liblogosdelivery library (#116)
* refactor: build against the single liblogosdelivery library

logos-delivery#4012 unifies the node lifecycle on logosdelivery_* and ships the
full API — Messaging, Reliable Channels, and the low-level Kernel (waku_*) tier
— in one liblogosdelivery library, retiring the separate libwaku. Migrate the
bindings onto it.

- internal/ffi: collapse the two bridges into one internal/ffi/liblogosdelivery
  over the single library. The kernel wrappers keep calling waku_* (kernel
  header), the lifecycle now uses logosdelivery_create_node/start_node/
  stop_node/destroy (waku_new/start/stop/destroy are gone), events use
  logosdelivery_set_event_callback. Include liblogosdelivery_kernel.h (which
  re-exports the stable header) and link -llogosdelivery. Remove
  internal/ffi/libwaku.
- pkg/kernel: repoint at internal/ffi/liblogosdelivery; the Makefile links
  -llogosdelivery.
- CI: build only `make liblogosdelivery`, headers from library/, single
  -llogosdelivery. Pinned to logos-delivery#4012 until it merges (see the
  TODO/LOGOS_DELIVERY_REF in pr.yml).

The high-level MessagingClient (pkg/messaging) lands in a follow-up PR on top.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(kernel): drop removed config keys + add a runnable example

Running against the single liblogosdelivery library surfaced a config drift:
the consolidated WakuNodeConf strictly rejects unknown keys, and
common.WakuConfig still emitted `legacyStore` (removed upstream, and sent
unconditionally) and `host` (renamed to `listenAddress`). That failed node
creation, so every kernel test that starts a node failed. Reconcile the config:

- drop the LegacyStore field (and its two obsolete test references; the
  already-skipped TestCheckLegacyStore keeps compiling);
- map Host to the `listenAddress` JSON key.

Add examples/kernel: a small runnable check that drives the unified lifecycle
(logosdelivery_create_node/start/stop/destroy) and a few kernel ops (version,
listen addresses, is-online, relay subscribe/unsubscribe) over the single
library. `go run ./examples/kernel` prints OK.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(kernel): correct RelayPublish wire format; verify send/receive

RelayPublish marshalled the generated protobuf (content_topic), but the library
requires the WakuMessage wire format (contentTopic) and rejected it with
"Missing required field: contentTopic". Marshal the wire shape explicitly.

Turn examples/kernel into an end-to-end send/receive check: two relay nodes on
cluster 16 / shard 64, connected via the listen multiaddr, one publishes and the
other receives it off MsgChan. Verified against a locally-built liblogosdelivery:
prints "send/receive OK".

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* ci: build against logos-delivery master (unpin #4012)

logos-delivery#4012 merged, so drop the LOGOS_DELIVERY_REF pin and clone the
default branch; the cache key tracks master's HEAD again.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(ffi): flatten internal/ffi/liblogosdelivery into internal/ffi

With a single library there is no bridge to choose between, so the extra
subpackage adds nothing: move the bridge to internal/ffi (package ffi) and drop
the umbrella doc. pkg/kernel now imports internal/ffi and calls ffi.*.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(kernel): add a two-node relay send/receive integration test

TestRelaySendReceive connects two relay nodes, publishes a random payload from
one and requires the other to receive it — end-to-end over the single library.
Compiled by the PR gate; run under `make test` / locally against a built
liblogosdelivery. (Replaces the former examples/kernel, removed alongside the
ffi flatten.)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(ffi): keep two files in package ffi, one per header

The single library still ships two headers — liblogosdelivery.h (stable
Messaging/lifecycle) and liblogosdelivery_kernel.h (low-level waku_*). Mirror
that with two files in one package instead of one merged ffi.go: keep the
messaging bridge and all the shared plumbing (Handle/RetOK/lifecycle/callbacks/
call) in liblogosdelivery.go, and keep the kernel wrappers in libwaku.go, which
reuses the shared call + logosGoCallback. This is the same two files we had,
moved into package ffi — a much smaller diff than the merge.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Apply suggestions from code review

Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Ivan FB <128452529+Ivansete-status@users.noreply.github.com>
2026-07-20 17:55:30 +01:00

212 lines
7.0 KiB
Go

// Package ffi is the cgo bridge over the single liblogosdelivery C library,
// which exposes the full logos-delivery API. It is split across two files
// mirroring the two headers: this file (liblogosdelivery.go) owns the shared
// plumbing — the synchronous request/response callback, the async event
// callback, the handle->handler registry and the node lifecycle — plus the
// stable Messaging API (liblogosdelivery.h); libwaku.go adds the low-level
// Kernel API (waku_*, liblogosdelivery_kernel.h). It exposes Go-typed
// primitives so pkg/kernel and pkg/messaging stay pure Go.
package ffi
/*
#cgo LDFLAGS: -llogosdelivery
#include <liblogosdelivery.h>
#include <stdlib.h>
// logosGoCallback (sync request/response) and logosEventCallback (async events)
// are implemented in Go and exported below.
extern void logosGoCallback(int ret, char* msg, size_t len, void* resp);
extern void logosEventCallback(int ret, char* msg, size_t len, void* userData);
// logosResp carries a single synchronous call's result back from the callback,
// plus a pointer to the Go sync.WaitGroup the caller blocks on.
typedef struct {
int ret;
char* msg;
size_t len;
void* wg;
} logosResp;
static void* allocLogosResp(void* wg) {
logosResp* r = (logosResp*) calloc(1, sizeof(logosResp));
r->wg = wg;
return r;
}
static void freeLogosResp(void* resp) { if (resp != NULL) free(resp); }
static char* logosRespMsg(void* resp) { return resp ? ((logosResp*)resp)->msg : NULL; }
static size_t logosRespLen(void* resp) { return resp ? ((logosResp*)resp)->len : 0; }
static int logosRespRet(void* resp) { return resp ? ((logosResp*)resp)->ret : RET_ERR; }
// Thin wrappers binding the shared Go callback to each entry point.
static void* cGoCreateNode(const char* cfg, void* resp) {
return logosdelivery_create_node(cfg, (FFICallBack) logosGoCallback, resp);
}
static void cGoStartNode(void* ctx, void* resp) {
logosdelivery_start_node(ctx, (FFICallBack) logosGoCallback, resp);
}
static void cGoStopNode(void* ctx, void* resp) {
logosdelivery_stop_node(ctx, (FFICallBack) logosGoCallback, resp);
}
static void cGoDestroyNode(void* ctx, void* resp) {
logosdelivery_destroy(ctx, (FFICallBack) logosGoCallback, resp);
}
static void cGoSubscribe(void* ctx, const char* topic, void* resp) {
logosdelivery_subscribe(ctx, (FFICallBack) logosGoCallback, resp, topic);
}
static void cGoUnsubscribe(void* ctx, const char* topic, void* resp) {
logosdelivery_unsubscribe(ctx, (FFICallBack) logosGoCallback, resp, topic);
}
static void cGoSend(void* ctx, const char* msgJson, void* resp) {
logosdelivery_send(ctx, (FFICallBack) logosGoCallback, resp, msgJson);
}
static void cGoSetEventCallback(void* ctx) {
// ctx doubles as userData so the shared event callback can route the event
// to the right registered handler.
logosdelivery_set_event_callback(ctx, (FFICallBack) logosEventCallback, ctx);
}
*/
import "C"
import (
"errors"
"sync"
"unsafe"
)
// Handle is an opaque pointer to a node context owned by the C library.
type Handle = unsafe.Pointer
// RetOK is the return code callbacks report on success.
const RetOK = C.RET_OK
// EventHandler receives every event liblogosdelivery emits for a node: the raw
// event JSON when ret == RetOK, an error message otherwise.
type EventHandler func(ret int, msg string)
// eventHandlers maps a node handle to the Go function that receives its
// events. The shared C event callback looks the handler up by handle.
var (
eventHandlersMu sync.RWMutex
eventHandlers = make(map[Handle]EventHandler)
)
//export logosGoCallback
func logosGoCallback(ret C.int, msg *C.char, length C.size_t, resp unsafe.Pointer) {
if resp == nil {
return
}
r := (*C.logosResp)(resp)
r.ret = ret
r.msg = msg
r.len = length
wg := (*sync.WaitGroup)(r.wg)
wg.Done()
}
//export logosEventCallback
func logosEventCallback(ret C.int, msg *C.char, length C.size_t, userData unsafe.Pointer) {
eventHandlersMu.RLock()
fn := eventHandlers[userData] // userData carries the node's handle
eventHandlersMu.RUnlock()
if fn != nil {
fn(int(ret), C.GoStringN(msg, C.int(length)))
}
}
// call runs a synchronous entry point that reports its result through the
// response callback, blocks until it completes, and returns the callback
// message (on RetOK) or an error built from it.
func call(invoke func(resp unsafe.Pointer)) (string, error) {
var wg sync.WaitGroup
wg.Add(1)
resp := C.allocLogosResp(unsafe.Pointer(&wg))
defer C.freeLogosResp(resp)
invoke(resp)
wg.Wait()
msg := C.GoStringN(C.logosRespMsg(resp), C.int(C.logosRespLen(resp)))
if C.logosRespRet(resp) != C.RET_OK {
return "", errors.New(msg)
}
return msg, nil
}
// New builds a node from a WakuNodeConf JSON string and returns its handle.
// The handle must be released with Destroy.
func New(configJSON string) (Handle, error) {
cCfg := C.CString(configJSON)
defer C.free(unsafe.Pointer(cCfg))
var wg sync.WaitGroup
wg.Add(1)
resp := C.allocLogosResp(unsafe.Pointer(&wg))
defer C.freeLogosResp(resp)
ctx := C.cGoCreateNode(cCfg, resp)
wg.Wait()
if C.logosRespRet(resp) != C.RET_OK || ctx == nil {
msg := C.GoStringN(C.logosRespMsg(resp), C.int(C.logosRespLen(resp)))
if msg == "" {
msg = "logosdelivery_create_node returned no context"
}
return nil, errors.New(msg)
}
return Handle(ctx), nil
}
// SetEventHandler registers fn to receive events for the node and wires up the
// underlying C event callback. Call before Start.
func SetEventHandler(h Handle, fn EventHandler) {
eventHandlersMu.Lock()
eventHandlers[h] = fn
eventHandlersMu.Unlock()
C.cGoSetEventCallback(h)
}
// Start starts the node's protocols and Messaging API services.
func Start(h Handle) error {
_, err := call(func(resp unsafe.Pointer) { C.cGoStartNode(h, resp) })
return err
}
// Stop stops the node.
func Stop(h Handle) error {
_, err := call(func(resp unsafe.Pointer) { C.cGoStopNode(h, resp) })
return err
}
// Destroy releases the node context and unregisters its event handler.
func Destroy(h Handle) error {
_, err := call(func(resp unsafe.Pointer) { C.cGoDestroyNode(h, resp) })
eventHandlersMu.Lock()
delete(eventHandlers, h)
eventHandlersMu.Unlock()
return err
}
// Subscribe subscribes the node to a content topic.
func Subscribe(h Handle, contentTopic string) error {
cTopic := C.CString(contentTopic)
defer C.free(unsafe.Pointer(cTopic))
_, err := call(func(resp unsafe.Pointer) { C.cGoSubscribe(h, cTopic, resp) })
return err
}
// Unsubscribe unsubscribes the node from a content topic.
func Unsubscribe(h Handle, contentTopic string) error {
cTopic := C.CString(contentTopic)
defer C.free(unsafe.Pointer(cTopic))
_, err := call(func(resp unsafe.Pointer) { C.cGoUnsubscribe(h, cTopic, resp) })
return err
}
// Send sends a message (JSON: {contentTopic, payload(base64), ephemeral}) and
// returns the request id used to correlate later send events.
func Send(h Handle, messageJSON string) (requestID string, err error) {
cMsg := C.CString(messageJSON)
defer C.free(unsafe.Pointer(cMsg))
return call(func(resp unsafe.Pointer) { C.cGoSend(h, cMsg, resp) })
}