From 45106c938cdea759947524db7393acfca1b82f56 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Thu, 21 Aug 2025 15:34:15 +0200 Subject: [PATCH 01/16] better generate.go file --- sds/generate.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sds/generate.go b/sds/generate.go index 93c4505..4cd5fea 100644 --- a/sds/generate.go +++ b/sds/generate.go @@ -1,8 +1,5 @@ -//go:build ignore -// +build ignore - package sds // This file contains the go:generate directive for building SDS native code. -//go:generate sh -c "cd sds && make build" +//go:generate sh -c "make build" From 267810e77f11374decac7a34fe4603c74bce7875 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Mon, 25 Aug 2025 17:06:04 +0200 Subject: [PATCH 02/16] add android support --- sds/Makefile | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/sds/Makefile b/sds/Makefile index b51a569..3d40eab 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -1,10 +1,5 @@ # Makefile for SDS Go Bindings -# Directories -THIRD_PARTY_DIR := ../third_party -NIM_SDS_REPO := https://github.com/waku-org/nim-sds -NIM_SDS_DIR := $(THIRD_PARTY_DIR)/nim-sds - .PHONY: all clean prepare build-libsds build # Default target @@ -13,31 +8,34 @@ all: build # Prepare third_party directory and clone nim-sds prepare: @echo "Creating third_party directory..." - @mkdir -p $(THIRD_PARTY_DIR) + @mkdir -p ../third_party @echo "Cloning nim-sds repository..." - @if [ ! -d "$(NIM_SDS_DIR)" ]; then \ - cd $(THIRD_PARTY_DIR) && \ - git clone $(NIM_SDS_REPO) && \ - cd $(NIM_SDS_DIR) && \ - git checkout master; \ - make update; \ + @if [ ! -d "../third_party/nim-sds" ]; then \ + cd ../third_party/ && \ + git clone https://github.com/waku-org/nim-sds && \ + cd nim-sds && \ + git checkout add-android-support; \ + make update; \ else \ echo "nim-sds repository already exists."; \ fi -# Build libsds -build-libsds: prepare +# Build SDS Go Bindings +build: prepare @echo "Building libsds..." @cd $(NIM_SDS_DIR) && make libsds + @echo "Building SDS Go Bindings..." + go build ./... -# Build SDS Go Bindings -build: build-libsds +build-android: prepare + @echo "Building libsds..." + @cd $(NIM_SDS_DIR) && make libsds-android @echo "Building SDS Go Bindings..." go build ./... # Clean up generated files clean: @echo "Cleaning up..." - @rm -rf $(THIRD_PARTY_DIR) - @rm -f sds-go-bindings \ No newline at end of file + @rm -rf ../third_party/ + @rm -f sds-go-bindings From f02f338452fac3ebe3af509977ff9fc44f3ca7d9 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Mon, 25 Aug 2025 17:54:46 +0200 Subject: [PATCH 03/16] update echo for Android in Makefile --- sds/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sds/Makefile b/sds/Makefile index 3d40eab..7e299de 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -29,9 +29,9 @@ build: prepare go build ./... build-android: prepare - @echo "Building libsds..." + @echo "Building libsds for Android..." @cd $(NIM_SDS_DIR) && make libsds-android - @echo "Building SDS Go Bindings..." + @echo "Building SDS Go Bindings for Android..." go build ./... # Clean up generated files From 83ca70c0c9c6619519aa19c30d1f754305069c34 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Mon, 25 Aug 2025 18:02:13 +0200 Subject: [PATCH 04/16] fix paths and tabs in sds/Makefile --- sds/Makefile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sds/Makefile b/sds/Makefile index 7e299de..af56669 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -13,10 +13,10 @@ prepare: @echo "Cloning nim-sds repository..." @if [ ! -d "../third_party/nim-sds" ]; then \ cd ../third_party/ && \ - git clone https://github.com/waku-org/nim-sds && \ - cd nim-sds && \ - git checkout add-android-support; \ - make update; \ + git clone https://github.com/waku-org/nim-sds && \ + cd nim-sds && \ + git checkout add-android-support; \ + make update; \ else \ echo "nim-sds repository already exists."; \ fi @@ -24,13 +24,13 @@ prepare: # Build SDS Go Bindings build: prepare @echo "Building libsds..." - @cd $(NIM_SDS_DIR) && make libsds + @cd ../third_party/nim-sds && make libsds @echo "Building SDS Go Bindings..." go build ./... build-android: prepare @echo "Building libsds for Android..." - @cd $(NIM_SDS_DIR) && make libsds-android + @cd ../third_party/nim-sds && make libsds-android @echo "Building SDS Go Bindings for Android..." go build ./... From 0b8dddea227569830a357678ee22adf90a64687b Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Mon, 25 Aug 2025 22:41:54 +0200 Subject: [PATCH 05/16] rm generate.go --- sds/generate.go | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 sds/generate.go diff --git a/sds/generate.go b/sds/generate.go deleted file mode 100644 index 4cd5fea..0000000 --- a/sds/generate.go +++ /dev/null @@ -1,5 +0,0 @@ -package sds - -// This file contains the go:generate directive for building SDS native code. - -//go:generate sh -c "make build" From f55a9f532e2d92ffe1299abc9ee29dee961ecfbc Mon Sep 17 00:00:00 2001 From: Ivan FB Date: Thu, 11 Sep 2025 14:34:11 +0200 Subject: [PATCH 06/16] add windows support --- sds/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sds/Makefile b/sds/Makefile index af56669..d9b61aa 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -15,7 +15,7 @@ prepare: cd ../third_party/ && \ git clone https://github.com/waku-org/nim-sds && \ cd nim-sds && \ - git checkout add-android-support; \ + git checkout add-windows-support; \ make update; \ else \ echo "nim-sds repository already exists."; \ From 3c377fa8caf270729ae8cce1c8c19c2ed71fb31a Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Sun, 21 Sep 2025 22:52:56 +0200 Subject: [PATCH 07/16] bump go to 1.24.0 --- go.mod | 2 +- go.sum | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index bec7095..6cf698c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/waku-org/sds-go-bindings -go 1.23 +go 1.24.0 require ( github.com/stretchr/testify v1.8.1 diff --git a/go.sum b/go.sum index ea719a5..d99d513 100644 --- a/go.sum +++ b/go.sum @@ -10,10 +10,13 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= From d9e869cfb3eeeb9eed939639b8440153d7005658 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Mon, 22 Sep 2025 16:06:15 +0200 Subject: [PATCH 08/16] simple change checkout master by default --- sds/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sds/Makefile b/sds/Makefile index d9b61aa..99fa234 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -15,7 +15,7 @@ prepare: cd ../third_party/ && \ git clone https://github.com/waku-org/nim-sds && \ cd nim-sds && \ - git checkout add-windows-support; \ + git checkout master; \ make update; \ else \ echo "nim-sds repository already exists."; \ From 20404c85a0b52a014a0900b0c8b81919291f6947 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Wed, 15 Oct 2025 19:12:05 +0200 Subject: [PATCH 09/16] mv echo line to better place in Makefile --- sds/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sds/Makefile b/sds/Makefile index 99fa234..4459480 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -10,8 +10,8 @@ prepare: @echo "Creating third_party directory..." @mkdir -p ../third_party - @echo "Cloning nim-sds repository..." @if [ ! -d "../third_party/nim-sds" ]; then \ + @echo "Cloning nim-sds repository..." cd ../third_party/ && \ git clone https://github.com/waku-org/nim-sds && \ cd nim-sds && \ From b5014429651a75fdc715271948a1dc73c025f53e Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Mon, 20 Oct 2025 23:29:00 +0200 Subject: [PATCH 10/16] pass the include and lib based on NIM_SDS_REPO_PATH env var --- sds/Makefile | 7 ++++++- sds/sds.go | 5 +---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/sds/Makefile b/sds/Makefile index 4459480..ffe12ab 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -1,5 +1,10 @@ # Makefile for SDS Go Bindings +# The NIM_SDS_REPO_PATH env var should be defined beforehand and it represents the absolute path +# to the cloned nim-sds repository, i.e, ....../third_party/nim-sds +export CGO_CFLAGS="-I${NIM_SDS_REPO_PATH}/library/" +export CGO_LDFLAGS="-L${NIM_SDS_REPO_PATH}/build -lsds -Wl,-rpath,${NIM_SDS_REPO_PATH}/build" + .PHONY: all clean prepare build-libsds build # Default target @@ -11,7 +16,7 @@ prepare: @mkdir -p ../third_party @if [ ! -d "../third_party/nim-sds" ]; then \ - @echo "Cloning nim-sds repository..." + echo "Cloning nim-sds repository..." ; \ cd ../third_party/ && \ git clone https://github.com/waku-org/nim-sds && \ cd nim-sds && \ diff --git a/sds/sds.go b/sds/sds.go index cd8168e..c4253bd 100644 --- a/sds/sds.go +++ b/sds/sds.go @@ -1,10 +1,7 @@ package sds /* - #cgo LDFLAGS: -L../third_party/nim-sds/build/ -lsds - #cgo LDFLAGS: -L../third_party/nim-sds -Wl,-rpath,../third_party/nim-sds/build/ - - #include "../third_party/nim-sds/library/libsds.h" + #include #include #include From 911541c82b4d8b1f101b1fa489798f53a2e15d2d Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Wed, 22 Oct 2025 00:49:05 +0200 Subject: [PATCH 11/16] properly differenciate between header and lib folders --- sds/Makefile | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sds/Makefile b/sds/Makefile index ffe12ab..4c4997e 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -1,9 +1,8 @@ # Makefile for SDS Go Bindings -# The NIM_SDS_REPO_PATH env var should be defined beforehand and it represents the absolute path -# to the cloned nim-sds repository, i.e, ....../third_party/nim-sds -export CGO_CFLAGS="-I${NIM_SDS_REPO_PATH}/library/" -export CGO_LDFLAGS="-L${NIM_SDS_REPO_PATH}/build -lsds -Wl,-rpath,${NIM_SDS_REPO_PATH}/build" +# The NIM_SDS_LIB_PATH and NIM_SDS_HEADER_PATH env vars should be defined beforehand +export CGO_CFLAGS="-I${NIM_SDS_HEADER_PATH}/" +export CGO_LDFLAGS="-L${NIM_SDS_LIB_PATH}/ -lsds -Wl,-rpath,${NIM_SDS_LIB_PATH}/" .PHONY: all clean prepare build-libsds build From d80dc61ae1e0bfd5f871a0a0999b70ebe5e90ed8 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Thu, 23 Oct 2025 01:15:54 +0200 Subject: [PATCH 12/16] adjust Makefile to make sure nim is always compiled --- sds/Makefile | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sds/Makefile b/sds/Makefile index 4c4997e..a9c8c74 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -11,20 +11,24 @@ all: build # Prepare third_party directory and clone nim-sds prepare: - @echo "Creating third_party directory..." + @echo "Preparing third_party directory..." @mkdir -p ../third_party - @if [ ! -d "../third_party/nim-sds" ]; then \ - echo "Cloning nim-sds repository..." ; \ - cd ../third_party/ && \ - git clone https://github.com/waku-org/nim-sds && \ - cd nim-sds && \ - git checkout master; \ - make update; \ + @echo "Setting up nim-sds..." + @if [ ! -d "../third_party/nim-sds/.git" ]; then \ + echo "Cloning nim-sds repository..."; \ + git clone https://github.com/waku-org/nim-sds ../third_party/nim-sds; \ else \ echo "nim-sds repository already exists."; \ fi + @echo "Updating nim-sds to get vendors and compile nim in nimbus-build-system..." + @cd ../third_party/nim-sds && \ + git fetch origin && \ + git checkout master && \ + echo "Running 'make update'..." && \ + make update + # Build SDS Go Bindings build: prepare @echo "Building libsds..." From 4399ce6ad90bf327e76b327ba9ca455f0d9ffcf6 Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Fri, 24 Oct 2025 22:31:51 +0200 Subject: [PATCH 13/16] add git pull master in sds Makefile --- sds/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/sds/Makefile b/sds/Makefile index a9c8c74..83a40ed 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -26,6 +26,7 @@ prepare: @cd ../third_party/nim-sds && \ git fetch origin && \ git checkout master && \ + git pull origin master && \ echo "Running 'make update'..." && \ make update From 3bcf73b5317c0f8d8d0dc99c95c2b2c4f03dd514 Mon Sep 17 00:00:00 2001 From: Igor Sirotin Date: Wed, 19 Nov 2025 18:36:37 +0000 Subject: [PATCH 14/16] feat: sds lint stub --- sds/sds.go | 166 +++++++--------------------------------------- sds/sds_common.go | 123 ++++++++++++++++++++++++++++++++++ sds/sds_lint.go | 38 +++++++++++ 3 files changed, 184 insertions(+), 143 deletions(-) create mode 100644 sds/sds_common.go create mode 100644 sds/sds_lint.go diff --git a/sds/sds.go b/sds/sds.go index c4253bd..eb08588 100644 --- a/sds/sds.go +++ b/sds/sds.go @@ -1,3 +1,5 @@ +//go:build !lint + package sds /* @@ -137,39 +139,23 @@ import ( "unsafe" ) -const requestTimeout = 30 * time.Second -const EventChanBufferSize = 1024 - //export SdsGoCallback func SdsGoCallback(ret C.int, msg *C.char, len C.size_t, resp unsafe.Pointer) { - if resp != nil { - m := (*C.SdsResp)(resp) - m.ret = ret - m.msg = msg + if resp != nil { + m := (*C.SdsResp)(resp) + m.ret = ret + m.msg = msg m.len = len wg := (*sync.WaitGroup)(m.ffiWg) wg.Done() } } -type EventCallbacks struct { - OnMessageReady func(messageId MessageID, channelId string) - OnMessageSent func(messageId MessageID, channelId string) - OnMissingDependencies func(messageId MessageID, missingDeps []MessageID, channelId string) - OnPeriodicSync func() -} - -// ReliabilityManager represents an instance of a nim-sds ReliabilityManager -type ReliabilityManager struct { - rmCtx unsafe.Pointer - callbacks EventCallbacks -} - func NewReliabilityManager() (*ReliabilityManager, error) { - Debug("Creating new Reliability Manager") - rm := &ReliabilityManager{} + Debug("Creating new Reliability Manager") + rm := &ReliabilityManager{} - wg := sync.WaitGroup{} + wg := sync.WaitGroup{} var resp = C.allocResp(unsafe.Pointer(&wg)) defer C.freeResp(resp) @@ -191,128 +177,22 @@ func NewReliabilityManager() (*ReliabilityManager, error) { return rm, nil } -// The event callback sends back the rm ctx to know to which -// rm is the event being emited for. Since we only have a global -// callback in the go side, We register all the rm's that we create -// so we can later obtain which instance of `ReliabilityManager` it should -// be invoked depending on the ctx received - -var rmRegistry map[unsafe.Pointer]*ReliabilityManager - -func init() { - rmRegistry = make(map[unsafe.Pointer]*ReliabilityManager) -} - -func registerReliabilityManager(rm *ReliabilityManager) { - _, ok := rmRegistry[rm.rmCtx] - if !ok { - rmRegistry[rm.rmCtx] = rm - } -} - -func unregisterReliabilityManager(rm *ReliabilityManager) { - delete(rmRegistry, rm.rmCtx) -} - //export sdsGlobalEventCallback func sdsGlobalEventCallback(callerRet C.int, msg *C.char, len C.size_t, userData unsafe.Pointer) { - if callerRet == C.RET_OK { - eventStr := C.GoStringN(msg, C.int(len)) - rm, ok := rmRegistry[userData] // userData contains rm's ctx - if ok { - rm.OnEvent(eventStr) - } - } else { - if len != 0 { - errMsg := C.GoStringN(msg, C.int(len)) - Error("sdsGlobalEventCallback retCode not ok, retCode: %v: %v", callerRet, errMsg) - } else { - Error("sdsGlobalEventCallback retCode not ok, retCode: %v", callerRet) - } - } -} - -type jsonEvent struct { - EventType string `json:"eventType"` -} - -type msgEvent struct { - MessageId MessageID `json:"messageId"` - ChannelId string `json:"channelId"` -} - -type missingDepsEvent struct { - MessageId MessageID `json:"messageId"` - MissingDeps []MessageID `json:"missingDeps"` - ChannelId string `json:"channelId"` -} - -func (rm *ReliabilityManager) RegisterCallbacks(callbacks EventCallbacks) { - rm.callbacks = callbacks -} - -func (rm *ReliabilityManager) OnEvent(eventStr string) { - - jsonEvent := jsonEvent{} - err := json.Unmarshal([]byte(eventStr), &jsonEvent) - if err != nil { - Error("could not unmarshal sds event string: %v", err) - - return - } - - switch jsonEvent.EventType { - case "message_ready": - rm.parseMessageReadyEvent(eventStr) - case "message_sent": - rm.parseMessageSentEvent(eventStr) - case "missing_dependencies": - rm.parseMissingDepsEvent(eventStr) - case "periodic_sync": - if rm.callbacks.OnPeriodicSync != nil { - rm.callbacks.OnPeriodicSync() - } - } - -} - -func (rm *ReliabilityManager) parseMessageReadyEvent(eventStr string) { - - msgEvent := msgEvent{} - err := json.Unmarshal([]byte(eventStr), &msgEvent) - if err != nil { - Error("could not parse message ready event %v", err) - } - - if rm.callbacks.OnMessageReady != nil { - rm.callbacks.OnMessageReady(msgEvent.MessageId, msgEvent.ChannelId) - } -} - -func (rm *ReliabilityManager) parseMessageSentEvent(eventStr string) { - - msgEvent := msgEvent{} - err := json.Unmarshal([]byte(eventStr), &msgEvent) - if err != nil { - Error("could not parse message sent event %v", err) - } - - if rm.callbacks.OnMessageSent != nil { - rm.callbacks.OnMessageSent(msgEvent.MessageId, msgEvent.ChannelId) - } -} - -func (rm *ReliabilityManager) parseMissingDepsEvent(eventStr string) { - - missingDepsEvent := missingDepsEvent{} - err := json.Unmarshal([]byte(eventStr), &missingDepsEvent) - if err != nil { - Error("could not parse missing dependencies event %v", err) - } - - if rm.callbacks.OnMissingDependencies != nil { - rm.callbacks.OnMissingDependencies(missingDepsEvent.MessageId, missingDepsEvent.MissingDeps, missingDepsEvent.ChannelId) - } + if callerRet == C.RET_OK { + eventStr := C.GoStringN(msg, C.int(len)) + rm, ok := rmRegistry[userData] // userData contains rm's ctx + if ok { + rm.OnEvent(eventStr) + } + } else { + if len != 0 { + errMsg := C.GoStringN(msg, C.int(len)) + Error("sdsGlobalEventCallback retCode not ok, retCode: %v: %v", callerRet, errMsg) + } else { + Error("sdsGlobalEventCallback retCode not ok, retCode: %v", callerRet) + } + } } func (rm *ReliabilityManager) Cleanup() error { diff --git a/sds/sds_common.go b/sds/sds_common.go new file mode 100644 index 0000000..3f89c47 --- /dev/null +++ b/sds/sds_common.go @@ -0,0 +1,123 @@ +package sds + +import ( + "encoding/json" + "unsafe" + "time" +) + +const requestTimeout = 30 * time.Second +const EventChanBufferSize = 1024 + +type EventCallbacks struct { + OnMessageReady func(messageId MessageID, channelId string) + OnMessageSent func(messageId MessageID, channelId string) + OnMissingDependencies func(messageId MessageID, missingDeps []MessageID, channelId string) + OnPeriodicSync func() +} + +// ReliabilityManager represents an instance of a nim-sds ReliabilityManager +type ReliabilityManager struct { + rmCtx unsafe.Pointer + callbacks EventCallbacks +} + +// The event callback sends back the rm ctx to know to which +// rm is the event being emited for. Since we only have a global +// callback in the go side, We register all the rm's that we create +// so we can later obtain which instance of `ReliabilityManager` it should +// be invoked depending on the ctx received +var rmRegistry map[unsafe.Pointer]*ReliabilityManager + +func init() { + rmRegistry = make(map[unsafe.Pointer]*ReliabilityManager) +} + +func registerReliabilityManager(rm *ReliabilityManager) { + _, ok := rmRegistry[rm.rmCtx] + if !ok { + rmRegistry[rm.rmCtx] = rm + } +} + +func unregisterReliabilityManager(rm *ReliabilityManager) { + delete(rmRegistry, rm.rmCtx) +} + +type jsonEvent struct { + EventType string `json:"eventType"` +} + +type msgEvent struct { + MessageId MessageID `json:"messageId"` + ChannelId string `json:"channelId"` +} + +type missingDepsEvent struct { + MessageId MessageID `json:"messageId"` + MissingDeps []MessageID `json:"missingDeps"` + ChannelId string `json:"channelId"` +} + +func (rm *ReliabilityManager) RegisterCallbacks(callbacks EventCallbacks) { + rm.callbacks = callbacks +} + +func (rm *ReliabilityManager) OnEvent(eventStr string) { + jsonEvent := jsonEvent{} + err := json.Unmarshal([]byte(eventStr), &jsonEvent) + if err != nil { + Error("could not unmarshal sds event string: %v", err) + return + } + + switch jsonEvent.EventType { + case "message_ready": + rm.parseMessageReadyEvent(eventStr) + case "message_sent": + rm.parseMessageSentEvent(eventStr) + case "missing_dependencies": + rm.parseMissingDepsEvent(eventStr) + case "periodic_sync": + if rm.callbacks.OnPeriodicSync != nil { + rm.callbacks.OnPeriodicSync() + } + } +} + +func (rm *ReliabilityManager) parseMessageReadyEvent(eventStr string) { + msgEvent := msgEvent{} + err := json.Unmarshal([]byte(eventStr), &msgEvent) + if err != nil { + Error("could not parse message ready event %v", err) + } + + if rm.callbacks.OnMessageReady != nil { + rm.callbacks.OnMessageReady(msgEvent.MessageId, msgEvent.ChannelId) + } +} + +func (rm *ReliabilityManager) parseMessageSentEvent(eventStr string) { + msgEvent := msgEvent{} + err := json.Unmarshal([]byte(eventStr), &msgEvent) + if err != nil { + Error("could not parse message sent event %v", err) + } + + if rm.callbacks.OnMessageSent != nil { + rm.callbacks.OnMessageSent(msgEvent.MessageId, msgEvent.ChannelId) + } +} + +func (rm *ReliabilityManager) parseMissingDepsEvent(eventStr string) { + missingDepsEvent := missingDepsEvent{} + err := json.Unmarshal([]byte(eventStr), &missingDepsEvent) + if err != nil { + Error("could not parse missing dependencies event %v", err) + } + + if rm.callbacks.OnMissingDependencies != nil { + rm.callbacks.OnMissingDependencies(missingDepsEvent.MessageId, missingDepsEvent.MissingDeps, missingDepsEvent.ChannelId) + } +} + diff --git a/sds/sds_lint.go b/sds/sds_lint.go new file mode 100644 index 0000000..ec9317a --- /dev/null +++ b/sds/sds_lint.go @@ -0,0 +1,38 @@ +//go:build lint + +package sds + +import "errors" + +// This file provides lint-only stubs that avoid requiring libsds.h/cgo +// so linters can analyze this package without native dependencies. + +// ErrLintBuild indicates a stubbed, lint-only build without native libsds. +var ErrLintBuild = errors.New("sds: lint-only build stub: native libsds not linked") + +// NewReliabilityManager returns an error in lint builds. +func NewReliabilityManager() (*ReliabilityManager, error) { + return nil, ErrLintBuild +} + +// Cleanup returns an error in lint builds. +func (rm *ReliabilityManager) Cleanup() error { return ErrLintBuild } + +// Reset returns an error in lint builds. +func (rm *ReliabilityManager) Reset() error { return ErrLintBuild } + +// WrapOutgoingMessage returns an error in lint builds. +func (rm *ReliabilityManager) WrapOutgoingMessage(message []byte, messageId MessageID, channelId string) ([]byte, error) { + return nil, ErrLintBuild +} + +// UnwrapReceivedMessage returns an error in lint builds. +func (rm *ReliabilityManager) UnwrapReceivedMessage(message []byte) (*UnwrappedMessage, error) { + return nil, ErrLintBuild +} + +// MarkDependenciesMet returns an error in lint builds. +func (rm *ReliabilityManager) MarkDependenciesMet(messageIDs []MessageID, channelId string) error { return ErrLintBuild } + +// StartPeriodicTasks returns an error in lint builds. +func (rm *ReliabilityManager) StartPeriodicTasks() error { return ErrLintBuild } From e78f76307965103adf21c275d96ba0ce5a85e52e Mon Sep 17 00:00:00 2001 From: Igor Sirotin Date: Wed, 19 Nov 2025 18:49:07 +0000 Subject: [PATCH 15/16] style: cleanup and format --- sds/sds.go | 43 ++++++++-------- sds/sds_common.go | 129 +++++++++++++++++++++++----------------------- 2 files changed, 85 insertions(+), 87 deletions(-) diff --git a/sds/sds.go b/sds/sds.go index eb08588..386d79b 100644 --- a/sds/sds.go +++ b/sds/sds.go @@ -135,16 +135,15 @@ import ( "strconv" "strings" "sync" - "time" "unsafe" ) //export SdsGoCallback func SdsGoCallback(ret C.int, msg *C.char, len C.size_t, resp unsafe.Pointer) { - if resp != nil { - m := (*C.SdsResp)(resp) - m.ret = ret - m.msg = msg + if resp != nil { + m := (*C.SdsResp)(resp) + m.ret = ret + m.msg = msg m.len = len wg := (*sync.WaitGroup)(m.ffiWg) wg.Done() @@ -152,10 +151,10 @@ func SdsGoCallback(ret C.int, msg *C.char, len C.size_t, resp unsafe.Pointer) { } func NewReliabilityManager() (*ReliabilityManager, error) { - Debug("Creating new Reliability Manager") - rm := &ReliabilityManager{} + Debug("Creating new Reliability Manager") + rm := &ReliabilityManager{} - wg := sync.WaitGroup{} + wg := sync.WaitGroup{} var resp = C.allocResp(unsafe.Pointer(&wg)) defer C.freeResp(resp) @@ -179,20 +178,20 @@ func NewReliabilityManager() (*ReliabilityManager, error) { //export sdsGlobalEventCallback func sdsGlobalEventCallback(callerRet C.int, msg *C.char, len C.size_t, userData unsafe.Pointer) { - if callerRet == C.RET_OK { - eventStr := C.GoStringN(msg, C.int(len)) - rm, ok := rmRegistry[userData] // userData contains rm's ctx - if ok { - rm.OnEvent(eventStr) - } - } else { - if len != 0 { - errMsg := C.GoStringN(msg, C.int(len)) - Error("sdsGlobalEventCallback retCode not ok, retCode: %v: %v", callerRet, errMsg) - } else { - Error("sdsGlobalEventCallback retCode not ok, retCode: %v", callerRet) - } - } + if callerRet == C.RET_OK { + eventStr := C.GoStringN(msg, C.int(len)) + rm, ok := rmRegistry[userData] // userData contains rm's ctx + if ok { + rm.OnEvent(eventStr) + } + } else { + if len != 0 { + errMsg := C.GoStringN(msg, C.int(len)) + Error("sdsGlobalEventCallback retCode not ok, retCode: %v: %v", callerRet, errMsg) + } else { + Error("sdsGlobalEventCallback retCode not ok, retCode: %v", callerRet) + } + } } func (rm *ReliabilityManager) Cleanup() error { diff --git a/sds/sds_common.go b/sds/sds_common.go index 3f89c47..32b9f05 100644 --- a/sds/sds_common.go +++ b/sds/sds_common.go @@ -1,25 +1,25 @@ package sds import ( - "encoding/json" - "unsafe" - "time" + "encoding/json" + "time" + "unsafe" ) const requestTimeout = 30 * time.Second const EventChanBufferSize = 1024 type EventCallbacks struct { - OnMessageReady func(messageId MessageID, channelId string) - OnMessageSent func(messageId MessageID, channelId string) - OnMissingDependencies func(messageId MessageID, missingDeps []MessageID, channelId string) - OnPeriodicSync func() + OnMessageReady func(messageId MessageID, channelId string) + OnMessageSent func(messageId MessageID, channelId string) + OnMissingDependencies func(messageId MessageID, missingDeps []MessageID, channelId string) + OnPeriodicSync func() } // ReliabilityManager represents an instance of a nim-sds ReliabilityManager type ReliabilityManager struct { - rmCtx unsafe.Pointer - callbacks EventCallbacks + rmCtx unsafe.Pointer + callbacks EventCallbacks } // The event callback sends back the rm ctx to know to which @@ -30,94 +30,93 @@ type ReliabilityManager struct { var rmRegistry map[unsafe.Pointer]*ReliabilityManager func init() { - rmRegistry = make(map[unsafe.Pointer]*ReliabilityManager) + rmRegistry = make(map[unsafe.Pointer]*ReliabilityManager) } func registerReliabilityManager(rm *ReliabilityManager) { - _, ok := rmRegistry[rm.rmCtx] - if !ok { - rmRegistry[rm.rmCtx] = rm - } + _, ok := rmRegistry[rm.rmCtx] + if !ok { + rmRegistry[rm.rmCtx] = rm + } } func unregisterReliabilityManager(rm *ReliabilityManager) { - delete(rmRegistry, rm.rmCtx) + delete(rmRegistry, rm.rmCtx) } type jsonEvent struct { - EventType string `json:"eventType"` + EventType string `json:"eventType"` } type msgEvent struct { - MessageId MessageID `json:"messageId"` - ChannelId string `json:"channelId"` + MessageId MessageID `json:"messageId"` + ChannelId string `json:"channelId"` } type missingDepsEvent struct { - MessageId MessageID `json:"messageId"` - MissingDeps []MessageID `json:"missingDeps"` - ChannelId string `json:"channelId"` + MessageId MessageID `json:"messageId"` + MissingDeps []MessageID `json:"missingDeps"` + ChannelId string `json:"channelId"` } func (rm *ReliabilityManager) RegisterCallbacks(callbacks EventCallbacks) { - rm.callbacks = callbacks + rm.callbacks = callbacks } func (rm *ReliabilityManager) OnEvent(eventStr string) { - jsonEvent := jsonEvent{} - err := json.Unmarshal([]byte(eventStr), &jsonEvent) - if err != nil { - Error("could not unmarshal sds event string: %v", err) - return - } + jsonEvent := jsonEvent{} + err := json.Unmarshal([]byte(eventStr), &jsonEvent) + if err != nil { + Error("could not unmarshal sds event string: %v", err) + return + } - switch jsonEvent.EventType { - case "message_ready": - rm.parseMessageReadyEvent(eventStr) - case "message_sent": - rm.parseMessageSentEvent(eventStr) - case "missing_dependencies": - rm.parseMissingDepsEvent(eventStr) - case "periodic_sync": - if rm.callbacks.OnPeriodicSync != nil { - rm.callbacks.OnPeriodicSync() - } - } + switch jsonEvent.EventType { + case "message_ready": + rm.parseMessageReadyEvent(eventStr) + case "message_sent": + rm.parseMessageSentEvent(eventStr) + case "missing_dependencies": + rm.parseMissingDepsEvent(eventStr) + case "periodic_sync": + if rm.callbacks.OnPeriodicSync != nil { + rm.callbacks.OnPeriodicSync() + } + } } func (rm *ReliabilityManager) parseMessageReadyEvent(eventStr string) { - msgEvent := msgEvent{} - err := json.Unmarshal([]byte(eventStr), &msgEvent) - if err != nil { - Error("could not parse message ready event %v", err) - } + msgEvent := msgEvent{} + err := json.Unmarshal([]byte(eventStr), &msgEvent) + if err != nil { + Error("could not parse message ready event %v", err) + } - if rm.callbacks.OnMessageReady != nil { - rm.callbacks.OnMessageReady(msgEvent.MessageId, msgEvent.ChannelId) - } + if rm.callbacks.OnMessageReady != nil { + rm.callbacks.OnMessageReady(msgEvent.MessageId, msgEvent.ChannelId) + } } func (rm *ReliabilityManager) parseMessageSentEvent(eventStr string) { - msgEvent := msgEvent{} - err := json.Unmarshal([]byte(eventStr), &msgEvent) - if err != nil { - Error("could not parse message sent event %v", err) - } + msgEvent := msgEvent{} + err := json.Unmarshal([]byte(eventStr), &msgEvent) + if err != nil { + Error("could not parse message sent event %v", err) + } - if rm.callbacks.OnMessageSent != nil { - rm.callbacks.OnMessageSent(msgEvent.MessageId, msgEvent.ChannelId) - } + if rm.callbacks.OnMessageSent != nil { + rm.callbacks.OnMessageSent(msgEvent.MessageId, msgEvent.ChannelId) + } } func (rm *ReliabilityManager) parseMissingDepsEvent(eventStr string) { - missingDepsEvent := missingDepsEvent{} - err := json.Unmarshal([]byte(eventStr), &missingDepsEvent) - if err != nil { - Error("could not parse missing dependencies event %v", err) - } + missingDepsEvent := missingDepsEvent{} + err := json.Unmarshal([]byte(eventStr), &missingDepsEvent) + if err != nil { + Error("could not parse missing dependencies event %v", err) + } - if rm.callbacks.OnMissingDependencies != nil { - rm.callbacks.OnMissingDependencies(missingDepsEvent.MessageId, missingDepsEvent.MissingDeps, missingDepsEvent.ChannelId) - } + if rm.callbacks.OnMissingDependencies != nil { + rm.callbacks.OnMissingDependencies(missingDepsEvent.MessageId, missingDepsEvent.MissingDeps, missingDepsEvent.ChannelId) + } } - From 68a4845a10eaf80d69d901a58a10e1e154735f9d Mon Sep 17 00:00:00 2001 From: Ivan Folgueira Bande Date: Tue, 25 Nov 2025 17:00:43 +0100 Subject: [PATCH 16/16] Simplify sds Makefile as we assume existing libsds library and header --- sds/Makefile | 59 +++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/sds/Makefile b/sds/Makefile index 83a40ed..49194f6 100644 --- a/sds/Makefile +++ b/sds/Makefile @@ -1,50 +1,53 @@ # Makefile for SDS Go Bindings # The NIM_SDS_LIB_PATH and NIM_SDS_HEADER_PATH env vars should be defined beforehand +# Therefore, we assume the libsds library and headers are located in the specified paths export CGO_CFLAGS="-I${NIM_SDS_HEADER_PATH}/" export CGO_LDFLAGS="-L${NIM_SDS_LIB_PATH}/ -lsds -Wl,-rpath,${NIM_SDS_LIB_PATH}/" +# Expected files +HEADER_FILE := $(NIM_SDS_HEADER_PATH)/libsds.h +LIB_FILES := $(wildcard $(NIM_SDS_LIB_PATH)/libsds.*) + .PHONY: all clean prepare build-libsds build # Default target all: build -# Prepare third_party directory and clone nim-sds -prepare: - @echo "Preparing third_party directory..." - @mkdir -p ../third_party - - @echo "Setting up nim-sds..." - @if [ ! -d "../third_party/nim-sds/.git" ]; then \ - echo "Cloning nim-sds repository..."; \ - git clone https://github.com/waku-org/nim-sds ../third_party/nim-sds; \ - else \ - echo "nim-sds repository already exists."; \ +# Validate necessary folders and files +check-folders: + @echo Checking header directory ... + @if [ -z "$(NIM_SDS_HEADER_PATH)" ]; then \ + echo "ERROR: NIM_SDS_HEADER_PATH not set"; exit 1; \ + fi + @if [ ! -d "$(NIM_SDS_HEADER_PATH)" ]; then \ + echo "ERROR: Header path does not exist: $(NIM_SDS_HEADER_PATH)"; exit 1; \ fi - @echo "Updating nim-sds to get vendors and compile nim in nimbus-build-system..." - @cd ../third_party/nim-sds && \ - git fetch origin && \ - git checkout master && \ - git pull origin master && \ - echo "Running 'make update'..." && \ - make update + @echo Checking lib directory ... + @if [ -z "$(NIM_SDS_LIB_PATH)" ]; then \ + echo "ERROR: NIM_SDS_LIB_PATH not set"; exit 1; \ + fi + @if [ ! -d "$(NIM_SDS_LIB_PATH)" ]; then \ + echo "ERROR: Library path does not exist: $(NIM_SDS_LIB_PATH)"; exit 1; \ + fi + + @echo Checking for libsds.h ... + @if [ ! -f "$(HEADER_FILE)" ]; then \ + echo "ERROR: libsds.h not found at: $(HEADER_FILE)"; exit 1; \ + fi + + @echo Checking for libsds library file ... + @if [ -z "$(LIB_FILES)" ]; then \ + echo "ERROR: No libsds library file found in: $(NIM_SDS_LIB_PATH)"; exit 1; \ + fi # Build SDS Go Bindings -build: prepare - @echo "Building libsds..." - @cd ../third_party/nim-sds && make libsds +build: check-folders @echo "Building SDS Go Bindings..." go build ./... -build-android: prepare - @echo "Building libsds for Android..." - @cd ../third_party/nim-sds && make libsds-android - @echo "Building SDS Go Bindings for Android..." - go build ./... - # Clean up generated files clean: @echo "Cleaning up..." - @rm -rf ../third_party/ @rm -f sds-go-bindings