From a558497a95d63845d2cc187afd55419d48b9ce6c Mon Sep 17 00:00:00 2001 From: Teodor Calin Date: Thu, 28 May 2026 19:46:00 -0700 Subject: [PATCH] refactor: migrate production code to daemonapi v0.4.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every production .go file now imports common/daemonapi instead of web4/pkg/daemon. Production code no longer requires web4 at all — the structural cycle between runtime and the daemon engine is broken at the import level. Changes per file: events.go daemonapi.Event subscription via daemonapi.EventBus handshake.go HandshakeRuntime adapts daemonapi.Daemon (no longer *daemon.Daemon) into handshake.Runtime identity.go Identity / Sign through daemonapi.Daemon policy.go PolicyRuntime adapts daemonapi.Daemon into policy.Runtime policy_manager.go AsDaemonPolicyManager returns daemonapi.PolicyManager runtime.go Runtime struct holds a daemonapi.Daemon (interface) streams.go daemonListener uses daemonapi.Listener (Accept + Port + Close methods); streamAdapter uses Connection.Info() for endpoint accessors instead of struct field access Tests still reference pkg/daemon to spin up real daemons — kept as a require + replace in go.mod for now; not on the production graph. Migrating tests to use a fake daemonapi.Daemon implementation is a follow-up. go build ($prod) passes. --- events.go | 6 +++--- go.mod | 12 ++++++++++-- go.sum | 10 ---------- handshake.go | 28 ++++++++++++++-------------- identity.go | 4 ++-- policy.go | 8 ++++---- policy_manager.go | 18 +++++++++--------- runtime.go | 14 +++++++------- streams.go | 32 ++++++++++++++++---------------- 9 files changed, 65 insertions(+), 67 deletions(-) diff --git a/events.go b/events.go index 1a9a6f9..e8086a2 100644 --- a/events.go +++ b/events.go @@ -4,15 +4,15 @@ package runtime import ( "github.com/pilot-protocol/common/coreapi" - "github.com/TeoSlayer/pilotprotocol/pkg/daemon" + "github.com/pilot-protocol/common/daemonapi" ) // daemonEventBus adapts daemon's in-process bus to coreapi.EventBus // for plugin Deps. Publish forwards through Daemon.PublishEvent; -// Subscribe wraps the bus channel with type conversion daemon.Event +// Subscribe wraps the bus channel with type conversion daemonapi.Event // → coreapi.Event. -type daemonEventBus struct{ d *daemon.Daemon } +type daemonEventBus struct{ d daemonapi.Daemon } func (b daemonEventBus) Publish(topic string, payload map[string]any) { if b.d == nil { diff --git a/go.mod b/go.mod index 6507982..2661744 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module github.com/pilot-protocol/runtime go 1.25.10 require ( - github.com/TeoSlayer/pilotprotocol v0.0.0 + github.com/TeoSlayer/pilotprotocol v1.10.5 + github.com/pilot-protocol/common v0.4.0 github.com/pilot-protocol/handshake v0.1.0 github.com/pilot-protocol/policy v0.1.0 ) @@ -11,10 +12,17 @@ require ( require ( github.com/coder/websocket v1.8.14 // indirect github.com/expr-lang/expr v1.17.8 // indirect - github.com/pilot-protocol/common v0.2.0 // indirect github.com/pilot-protocol/trustedagents v0.1.0 // indirect ) replace github.com/TeoSlayer/pilotprotocol => ../web4 replace github.com/pilot-protocol/common => ../common + +replace github.com/pilot-protocol/handshake => ../handshake + +replace github.com/pilot-protocol/policy => ../policy + +replace github.com/pilot-protocol/trustedagents => ../trustedagents + +replace github.com/pilot-protocol/rendezvous => ../rendezvous diff --git a/go.sum b/go.sum index 1a84805..54b0f43 100644 --- a/go.sum +++ b/go.sum @@ -2,13 +2,3 @@ github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9 github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg= github.com/expr-lang/expr v1.17.8 h1:W1loDTT+0PQf5YteHSTpju2qfUfNoBt4yw9+wOEU9VM= github.com/expr-lang/expr v1.17.8/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4= -github.com/pilot-protocol/common v0.1.0 h1:m8mZZATgeBiFoqhWXPnskw2u0lNkWxHp0IagZK35V1g= -github.com/pilot-protocol/common v0.1.0/go.mod h1:4YZWHK5nhM+4RLmYTspLxxAFbyBII7yzQDAHq3Ul2ck= -github.com/pilot-protocol/handshake v0.1.0 h1:TmqIglsimTynKtE5hLpCt/SZmmBYs8OCn4qn755fmew= -github.com/pilot-protocol/handshake v0.1.0/go.mod h1:FIIMTgRcMIMEim/1d7F5f6YenJC+3xl53QMEmnnJY+0= -github.com/pilot-protocol/policy v0.1.0 h1:Eh0CfCZDEX8UCkMPi2MrNrhCe8c15a/Bqf4eaKUhyis= -github.com/pilot-protocol/policy v0.1.0/go.mod h1:IMVm7IQhgLtH/iXow2AWFuLl+sKrxJ4mGs4EKLoHop8= -github.com/pilot-protocol/rendezvous v0.1.0 h1:vOBD7CnRY8uU8vma0Vfcr0aPSQ54qNuxppNUiljzk9Y= -github.com/pilot-protocol/rendezvous v0.1.0/go.mod h1:g3/IYBykbU5m9jeprSCrmuoDpaqROO4Lu/+ecKVIF3A= -github.com/pilot-protocol/trustedagents v0.1.0 h1:rCX0IQxfZ84Q4dSgw01WJgjHUODRnI3iAon1t+NuFGE= -github.com/pilot-protocol/trustedagents v0.1.0/go.mod h1:uVySmuMPb6N7AOCnvLHN2I9C9ggqEpfBmAZwVuP5Xaw= diff --git a/handshake.go b/handshake.go index d986ddb..01570a5 100644 --- a/handshake.go +++ b/handshake.go @@ -8,7 +8,7 @@ import ( "time" "github.com/pilot-protocol/common/coreapi" - "github.com/TeoSlayer/pilotprotocol/pkg/daemon" + "github.com/pilot-protocol/common/daemonapi" "github.com/pilot-protocol/common/protocol" registryclient "github.com/pilot-protocol/common/registry/client" "github.com/pilot-protocol/handshake" @@ -17,13 +17,13 @@ import ( // handshakeCloseDelay matches plugins/handshake.HandshakeCloseDelay. const handshakeCloseDelay = 500 * time.Millisecond -// HandshakeRuntime adapts *daemon.Daemon to the handshake.Runtime +// HandshakeRuntime adapts daemonapi.Daemon to the handshake.Runtime // interface. Lives here (L12 composition root) so plugins/handshake // stays free of pkg/daemon. -type HandshakeRuntime struct{ d *daemon.Daemon } +type HandshakeRuntime struct{ d daemonapi.Daemon } // NewHandshakeRuntime returns a handshake.Runtime backed by d. -func NewHandshakeRuntime(d *daemon.Daemon) handshake.Runtime { +func NewHandshakeRuntime(d daemonapi.Daemon) handshake.Runtime { return HandshakeRuntime{d: d} } @@ -95,22 +95,22 @@ func (r HandshakeRuntime) Registry() handshake.RegistryClient { } // handshakeServiceAdapter converts between plugins/handshake types and -// the daemon-local daemon.HandshakeService mirror types. +// the daemon-local daemonapi.HandshakeService mirror types. type handshakeServiceAdapter struct{ m *handshake.Manager } // NewHandshakeServiceAdapter wraps a *handshake.Service so it satisfies -// daemon.HandshakeService. -func NewHandshakeServiceAdapter(svc *handshake.Service) daemon.HandshakeService { +// daemonapi.HandshakeService. +func NewHandshakeServiceAdapter(svc *handshake.Service) daemonapi.HandshakeService { return handshakeServiceAdapter{m: svc.Manager()} } func (a handshakeServiceAdapter) IsTrusted(nodeID uint32) bool { return a.m.IsTrusted(nodeID) } -func (a handshakeServiceAdapter) TrustedPeers() []daemon.HandshakeTrustRecord { +func (a handshakeServiceAdapter) TrustedPeers() []daemonapi.HandshakeTrustRecord { src := a.m.TrustedPeers() - out := make([]daemon.HandshakeTrustRecord, len(src)) + out := make([]daemonapi.HandshakeTrustRecord, len(src)) for i, r := range src { - out[i] = daemon.HandshakeTrustRecord{ + out[i] = daemonapi.HandshakeTrustRecord{ NodeID: r.NodeID, PublicKey: r.PublicKey, ApprovedAt: r.ApprovedAt, @@ -121,11 +121,11 @@ func (a handshakeServiceAdapter) TrustedPeers() []daemon.HandshakeTrustRecord { return out } -func (a handshakeServiceAdapter) PendingRequests() []daemon.HandshakePendingRecord { +func (a handshakeServiceAdapter) PendingRequests() []daemonapi.HandshakePendingRecord { src := a.m.PendingRequests() - out := make([]daemon.HandshakePendingRecord, len(src)) + out := make([]daemonapi.HandshakePendingRecord, len(src)) for i, p := range src { - out[i] = daemon.HandshakePendingRecord{ + out[i] = daemonapi.HandshakePendingRecord{ NodeID: p.NodeID, PublicKey: p.PublicKey, Justification: p.Justification, @@ -174,6 +174,6 @@ func (a handshakeServiceAdapter) Stop() { a.m.Stop() } // Compile-time guards. var ( _ handshake.Runtime = HandshakeRuntime{} - _ daemon.HandshakeService = handshakeServiceAdapter{} + _ daemonapi.HandshakeService = handshakeServiceAdapter{} _ handshake.RegistryClient = (*registryclient.Client)(nil) ) diff --git a/identity.go b/identity.go index a64e9a5..2e3ab2b 100644 --- a/identity.go +++ b/identity.go @@ -7,14 +7,14 @@ import ( "errors" "github.com/pilot-protocol/common/coreapi" - "github.com/TeoSlayer/pilotprotocol/pkg/daemon" + "github.com/pilot-protocol/common/daemonapi" ) // daemonIdentity adapts daemon's identity state to coreapi.Identity // for plugin Deps. Methods that need the underlying *crypto.Identity // (PublicKey, Sign) go through Daemon.Identity(). -type daemonIdentity struct{ d *daemon.Daemon } +type daemonIdentity struct{ d daemonapi.Daemon } func (i daemonIdentity) NodeID() uint32 { return i.d.NodeID() } func (i daemonIdentity) Address() coreapi.Addr { return i.d.Addr() } diff --git a/policy.go b/policy.go index 428e1cd..c177d09 100644 --- a/policy.go +++ b/policy.go @@ -3,17 +3,17 @@ package runtime import ( - "github.com/TeoSlayer/pilotprotocol/pkg/daemon" + "github.com/pilot-protocol/common/daemonapi" "github.com/pilot-protocol/policy" ) -// PolicyRuntime adapts *daemon.Daemon to the policy.Runtime interface. +// PolicyRuntime adapts daemonapi.Daemon to the policy.Runtime interface. // Lives here (L12 composition root) so plugins/policy stays free of // pkg/daemon. -type PolicyRuntime struct{ d *daemon.Daemon } +type PolicyRuntime struct{ d daemonapi.Daemon } // NewPolicyRuntime returns a policy.Runtime backed by d. -func NewPolicyRuntime(d *daemon.Daemon) policy.Runtime { +func NewPolicyRuntime(d daemonapi.Daemon) policy.Runtime { return PolicyRuntime{d: d} } diff --git a/policy_manager.go b/policy_manager.go index 815ab5d..e18e5e4 100644 --- a/policy_manager.go +++ b/policy_manager.go @@ -4,14 +4,14 @@ package runtime import ( "github.com/pilot-protocol/common/coreapi" - "github.com/TeoSlayer/pilotprotocol/pkg/daemon" + "github.com/pilot-protocol/common/daemonapi" ) // AsDaemonPolicyManager adapts a coreapi.PolicyManager (returned by -// plugins/policy.Service.Manager()) to daemon.PolicyManager. The two +// plugins/policy.Service.Manager()) to daemonapi.PolicyManager. The two // interfaces have identical method shapes; the adapter does the // PolicyRunner element-type conversion on All() and Get(). -func AsDaemonPolicyManager(pm coreapi.PolicyManager) daemon.PolicyManager { +func AsDaemonPolicyManager(pm coreapi.PolicyManager) daemonapi.PolicyManager { if pm == nil { return nil } @@ -20,7 +20,7 @@ func AsDaemonPolicyManager(pm coreapi.PolicyManager) daemon.PolicyManager { type policyManagerAdapter struct{ inner coreapi.PolicyManager } -func (a policyManagerAdapter) Start(netID uint16, policyJSON []byte) (daemon.PolicyRunner, error) { +func (a policyManagerAdapter) Start(netID uint16, policyJSON []byte) (daemonapi.PolicyRunner, error) { pr, err := a.inner.Start(netID, policyJSON) if err != nil { return nil, err @@ -30,13 +30,13 @@ func (a policyManagerAdapter) Start(netID uint16, policyJSON []byte) (daemon.Pol func (a policyManagerAdapter) Stop(netID uint16) { a.inner.Stop(netID) } -func (a policyManagerAdapter) Get(netID uint16) daemon.PolicyRunner { +func (a policyManagerAdapter) Get(netID uint16) daemonapi.PolicyRunner { return wrapRunner(a.inner.Get(netID)) } -func (a policyManagerAdapter) All() []daemon.PolicyRunner { +func (a policyManagerAdapter) All() []daemonapi.PolicyRunner { src := a.inner.All() - out := make([]daemon.PolicyRunner, 0, len(src)) + out := make([]daemonapi.PolicyRunner, 0, len(src)) for _, pr := range src { out = append(out, wrapRunner(pr)) } @@ -46,11 +46,11 @@ func (a policyManagerAdapter) All() []daemon.PolicyRunner { func (a policyManagerAdapter) StopAll() { a.inner.StopAll() } func (a policyManagerAdapter) LoadPersisted() error { return a.inner.LoadPersisted() } -// wrapRunner converts a coreapi.PolicyRunner to a daemon.PolicyRunner. +// wrapRunner converts a coreapi.PolicyRunner to a daemonapi.PolicyRunner. // The methods are structurally compatible (PolicyEventType is a type // alias to string), but Go's named-interface typing requires an // explicit shim. -func wrapRunner(pr coreapi.PolicyRunner) daemon.PolicyRunner { +func wrapRunner(pr coreapi.PolicyRunner) daemonapi.PolicyRunner { if pr == nil { return nil } diff --git a/runtime.go b/runtime.go index 3fcdce0..bb44982 100644 --- a/runtime.go +++ b/runtime.go @@ -14,7 +14,7 @@ import ( "context" "github.com/pilot-protocol/common/coreapi" - "github.com/TeoSlayer/pilotprotocol/pkg/daemon" + "github.com/pilot-protocol/common/daemonapi" ) // Runtime owns the plugin-side glue for a single daemon. cmd/daemon @@ -22,18 +22,18 @@ import ( // after Daemon.Start (so regConn etc. are wired) and StopPlugins // before Daemon.Stop. type Runtime struct { - d *daemon.Daemon + d daemonapi.Daemon registry *coreapi.ServiceRegistry } // New returns a Runtime bound to the given daemon. Safe to call // before d.Start. -func New(d *daemon.Daemon) *Runtime { +func New(d daemonapi.Daemon) *Runtime { return &Runtime{d: d, registry: &coreapi.ServiceRegistry{}} } // Daemon returns the underlying daemon (test access). -func (r *Runtime) Daemon() *daemon.Daemon { return r.d } +func (r *Runtime) Daemon() daemonapi.Daemon { return r.d } // Register adds a service to the registry. Mirrors the old // daemon.RegisterPlugin. Must be called before StartPlugins. @@ -60,18 +60,18 @@ func (r *Runtime) StopPlugins(ctx context.Context) error { return r.registry.StopAll(ctx) } -// asCoreapiTrust adapts a daemon.TrustChecker (primitives) to the +// asCoreapiTrust adapts a daemonapi.TrustChecker (primitives) to the // coreapi.TrustChecker plugins expect. Structurally identical // signatures, but Go's interface-typing requires an explicit bridge // when the named types differ. nil → nil (no plugin → no auto-accept). -func asCoreapiTrust(t daemon.TrustChecker) coreapi.TrustChecker { +func asCoreapiTrust(t daemonapi.TrustChecker) coreapi.TrustChecker { if t == nil { return nil } return trustAdapter{inner: t} } -type trustAdapter struct{ inner daemon.TrustChecker } +type trustAdapter struct{ inner daemonapi.TrustChecker } func (a trustAdapter) IsTrusted(nodeID uint32) (string, bool) { return a.inner.IsTrusted(nodeID) diff --git a/streams.go b/streams.go index b9c55b4..9ff1f46 100644 --- a/streams.go +++ b/streams.go @@ -7,13 +7,13 @@ import ( "errors" "github.com/pilot-protocol/common/coreapi" - "github.com/TeoSlayer/pilotprotocol/pkg/daemon" + "github.com/pilot-protocol/common/daemonapi" ) // daemonStreams is the in-process implementation of coreapi.Streams. // Lives here (plugins/runtime) so daemon doesn't carry the L10 import. -type daemonStreams struct{ d *daemon.Daemon } +type daemonStreams struct{ d daemonapi.Daemon } func (s daemonStreams) Listen(port uint16) (coreapi.Listener, error) { ln, err := s.d.Ports().Bind(port) @@ -36,12 +36,12 @@ func (s daemonStreams) SendDatagram(ctx context.Context, dst coreapi.Addr, port } type daemonListener struct { - inner *daemon.Listener - d *daemon.Daemon + inner daemonapi.Listener + d daemonapi.Daemon } func (l *daemonListener) Accept() (coreapi.Stream, error) { - conn, ok := <-l.inner.AcceptCh + conn, ok := l.inner.Accept() if !ok { return nil, errors.New("listener closed") } @@ -49,22 +49,22 @@ func (l *daemonListener) Accept() (coreapi.Stream, error) { } func (l *daemonListener) Close() error { - l.d.Ports().Unbind(l.inner.Port) + l.d.Ports().Unbind(l.inner.Port()) return nil } func (l *daemonListener) Addr() coreapi.Addr { return l.d.Addr() } -func (l *daemonListener) Port() uint16 { return l.inner.Port } +func (l *daemonListener) Port() uint16 { return l.inner.Port() } -// streamAdapter wraps *daemon.Connection so it satisfies coreapi.Stream. +// streamAdapter wraps daemonapi.Connection so it satisfies coreapi.Stream. // Daemon exposes a connAdapter via Daemon.NewConnAdapter for this. type streamAdapter struct { - d *daemon.Daemon - conn *daemon.Connection - rw daemon.ConnReadWriter + d daemonapi.Daemon + conn daemonapi.Connection + rw daemonapi.ConnReadWriter } -func newStreamAdapter(d *daemon.Daemon, conn *daemon.Connection) *streamAdapter { +func newStreamAdapter(d daemonapi.Daemon, conn daemonapi.Connection) *streamAdapter { return &streamAdapter{d: d, conn: conn, rw: d.NewConnReadWriter(conn)} } @@ -72,10 +72,10 @@ func (s *streamAdapter) Read(p []byte) (int, error) { return s.rw.Read(p) } func (s *streamAdapter) Write(p []byte) (int, error) { return s.rw.Write(p) } func (s *streamAdapter) Close() error { return s.rw.Close() } -func (s *streamAdapter) LocalAddr() coreapi.Addr { return s.conn.LocalAddr } -func (s *streamAdapter) LocalPort() uint16 { return s.conn.LocalPort } -func (s *streamAdapter) RemoteAddr() coreapi.Addr { return s.conn.RemoteAddr } -func (s *streamAdapter) RemotePort() uint16 { return s.conn.RemotePort } +func (s *streamAdapter) LocalAddr() coreapi.Addr { return s.conn.Info().LocalAddr } +func (s *streamAdapter) LocalPort() uint16 { return s.conn.Info().LocalPort } +func (s *streamAdapter) RemoteAddr() coreapi.Addr { return s.conn.Info().RemoteAddr } +func (s *streamAdapter) RemotePort() uint16 { return s.conn.Info().RemotePort }