From d6152ccd85c400345fb020251f87cac4b4201464 Mon Sep 17 00:00:00 2001 From: Sudo-Ivan Date: Wed, 31 Dec 2025 18:42:50 -0600 Subject: [PATCH] refactor: replace AnnounceHandler interface with Handler and update ReceivedAnnounce method to include hops parameter --- pkg/announce/announce.go | 18 ++++++------------ pkg/announce/handler.go | 2 +- pkg/transport/transport.go | 8 ++++---- pkg/wasm/wasm.go | 3 ++- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/pkg/announce/announce.go b/pkg/announce/announce.go index b97eb9d..d0c3421 100644 --- a/pkg/announce/announce.go +++ b/pkg/announce/announce.go @@ -49,12 +49,6 @@ const ( MAX_RETRIES = 3 ) -type AnnounceHandler interface { - AspectFilter() []string - ReceivedAnnounce(destinationHash []byte, announcedIdentity interface{}, appData []byte) error - ReceivePathResponses() bool -} - type Announce struct { mutex *sync.RWMutex destinationHash []byte @@ -67,7 +61,7 @@ type Announce struct { signature []byte pathResponse bool retries int - handlers []AnnounceHandler + handlers []Handler ratchetID []byte packet []byte hash []byte @@ -97,7 +91,7 @@ func New(dest *identity.Identity, destinationHash []byte, destinationName string timestamp: time.Now().Unix(), pathResponse: pathResponse, retries: 0, - handlers: make([]AnnounceHandler, 0), + handlers: make([]Handler, 0), } // Get current ratchet ID if enabled @@ -156,13 +150,13 @@ func (a *Announce) Propagate(interfaces []common.NetworkInterface) error { return nil } -func (a *Announce) RegisterHandler(handler AnnounceHandler) { +func (a *Announce) RegisterHandler(handler Handler) { a.mutex.Lock() defer a.mutex.Unlock() a.handlers = append(a.handlers, handler) } -func (a *Announce) DeregisterHandler(handler AnnounceHandler) { +func (a *Announce) DeregisterHandler(handler Handler) { a.mutex.Lock() defer a.mutex.Unlock() for i, h := range a.handlers { @@ -283,7 +277,7 @@ func (a *Announce) HandleAnnounce(data []byte) error { // Process with handlers for _, handler := range a.handlers { if handler.ReceivePathResponses() || !a.pathResponse { - if err := handler.ReceivedAnnounce(destHash, announcedIdentity, appData); err != nil { + if err := handler.ReceivedAnnounce(destHash, announcedIdentity, appData, hopCount); err != nil { return err } } @@ -480,7 +474,7 @@ func NewAnnounce(identity *identity.Identity, destinationHash []byte, appData [] destinationHash: destHash, hops: 0, mutex: &sync.RWMutex{}, - handlers: make([]AnnounceHandler, 0), + handlers: make([]Handler, 0), config: config, } diff --git a/pkg/announce/handler.go b/pkg/announce/handler.go index b9e6beb..b6ead39 100644 --- a/pkg/announce/handler.go +++ b/pkg/announce/handler.go @@ -2,6 +2,6 @@ package announce type Handler interface { AspectFilter() []string - ReceivedAnnounce(destHash []byte, identity interface{}, appData []byte) error + ReceivedAnnounce(destHash []byte, identity interface{}, appData []byte, hops uint8) error ReceivePathResponses() bool } diff --git a/pkg/transport/transport.go b/pkg/transport/transport.go index 5df1ead..854af4c 100644 --- a/pkg/transport/transport.go +++ b/pkg/transport/transport.go @@ -492,14 +492,14 @@ func (t *Transport) UnregisterAnnounceHandler(handler announce.Handler) { } } -func (t *Transport) notifyAnnounceHandlers(destHash []byte, identity interface{}, appData []byte) { +func (t *Transport) notifyAnnounceHandlers(destHash []byte, identity interface{}, appData []byte, hops uint8) { t.mutex.RLock() handlers := make([]announce.Handler, len(t.announceHandlers)) copy(handlers, t.announceHandlers) t.mutex.RUnlock() for _, handler := range handlers { - if err := handler.ReceivedAnnounce(destHash, identity, appData); err != nil { + if err := handler.ReceivedAnnounce(destHash, identity, appData, hops); err != nil { debug.Log(debug.DEBUG_ERROR, "Error in announce handler", "error", err) } } @@ -716,7 +716,7 @@ func (t *Transport) HandleAnnounce(data []byte, sourceIface common.NetworkInterf } // Notify handlers - t.notifyAnnounceHandlers(destHash, identity, appData) + t.notifyAnnounceHandlers(destHash, identity, appData, data[0]) return lastErr } @@ -1117,7 +1117,7 @@ func (t *Transport) handleAnnouncePacket(data []byte, iface common.NetworkInterf // Notify handlers first, regardless of forwarding limits debug.Log(debug.DEBUG_INFO, "Notifying announce handlers", "destHash", fmt.Sprintf("%x", destinationHash), "appDataLen", len(appData)) - t.notifyAnnounceHandlers(destinationHash, id, appData) + t.notifyAnnounceHandlers(destinationHash, id, appData, hopCount) debug.Log(debug.DEBUG_INFO, "Announce handlers notified") // Don't forward if max hops reached diff --git a/pkg/wasm/wasm.go b/pkg/wasm/wasm.go index 2c736dd..4e041ad 100644 --- a/pkg/wasm/wasm.go +++ b/pkg/wasm/wasm.go @@ -335,12 +335,13 @@ func (h *genericAnnounceHandler) ReceivePathResponses() bool { return false } -func (h *genericAnnounceHandler) ReceivedAnnounce(destHash []byte, ident interface{}, appData []byte) error { +func (h *genericAnnounceHandler) ReceivedAnnounce(destHash []byte, ident interface{}, appData []byte, hops uint8) error { if !announceHandler.IsUndefined() { hashStr := hex.EncodeToString(destHash) announceHandler.Invoke(js.ValueOf(map[string]interface{}{ "hash": hashStr, "appData": string(appData), + "hops": int(hops), })) } return nil