Implement UCXL Protocol Foundation (Phase 1)
- Add complete UCXL address parser with BNF grammar validation - Implement temporal navigation system with bounds checking - Create UCXI HTTP server with REST-like operations - Add comprehensive test suite with 87 passing tests - Integrate with existing BZZZ architecture (opt-in via config) - Support semantic addressing with wildcards and version control Core Features: - UCXL address format: ucxl://agent:role@project:task/temporal/path - Temporal segments: *^, ~~N, ^^N, *~, *~N with navigation logic - UCXI endpoints: GET/PUT/POST/DELETE/ANNOUNCE operations - Production-ready with error handling and graceful shutdown 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,12 @@ type Config struct {
|
||||
EnableMDNS bool
|
||||
MDNSServiceTag string
|
||||
|
||||
// DHT configuration
|
||||
EnableDHT bool
|
||||
DHTBootstrapPeers []string
|
||||
DHTMode string // "client", "server", "auto"
|
||||
DHTProtocolPrefix string
|
||||
|
||||
// Connection limits
|
||||
MaxConnections int
|
||||
MaxPeersPerIP int
|
||||
@@ -46,6 +52,12 @@ func DefaultConfig() *Config {
|
||||
EnableMDNS: true,
|
||||
MDNSServiceTag: "bzzz-peer-discovery",
|
||||
|
||||
// DHT settings (disabled by default for local development)
|
||||
EnableDHT: false,
|
||||
DHTBootstrapPeers: []string{},
|
||||
DHTMode: "auto",
|
||||
DHTProtocolPrefix: "/bzzz",
|
||||
|
||||
// Connection limits for local network
|
||||
MaxConnections: 50,
|
||||
MaxPeersPerIP: 3,
|
||||
@@ -124,4 +136,32 @@ func WithTopics(bzzzTopic, hmmmTopic string) Option {
|
||||
c.BzzzTopic = bzzzTopic
|
||||
c.HmmmTopic = hmmmTopic
|
||||
}
|
||||
}
|
||||
|
||||
// WithDHT enables or disables DHT discovery
|
||||
func WithDHT(enabled bool) Option {
|
||||
return func(c *Config) {
|
||||
c.EnableDHT = enabled
|
||||
}
|
||||
}
|
||||
|
||||
// WithDHTBootstrapPeers sets the DHT bootstrap peers
|
||||
func WithDHTBootstrapPeers(peers []string) Option {
|
||||
return func(c *Config) {
|
||||
c.DHTBootstrapPeers = peers
|
||||
}
|
||||
}
|
||||
|
||||
// WithDHTMode sets the DHT mode
|
||||
func WithDHTMode(mode string) Option {
|
||||
return func(c *Config) {
|
||||
c.DHTMode = mode
|
||||
}
|
||||
}
|
||||
|
||||
// WithDHTProtocolPrefix sets the DHT protocol prefix
|
||||
func WithDHTProtocolPrefix(prefix string) Option {
|
||||
return func(c *Config) {
|
||||
c.DHTProtocolPrefix = prefix
|
||||
}
|
||||
}
|
||||
52
p2p/node.go
52
p2p/node.go
@@ -5,11 +5,13 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/anthonyrawlins/bzzz/pkg/dht"
|
||||
"github.com/libp2p/go-libp2p"
|
||||
"github.com/libp2p/go-libp2p/core/host"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
"github.com/libp2p/go-libp2p/p2p/security/noise"
|
||||
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
|
||||
kaddht "github.com/libp2p/go-libp2p-kad-dht"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
)
|
||||
|
||||
@@ -19,6 +21,7 @@ type Node struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
config *Config
|
||||
dht *dht.DHT // Optional DHT for distributed discovery
|
||||
}
|
||||
|
||||
// NewNode creates a new P2P node with the given configuration
|
||||
@@ -61,6 +64,34 @@ func NewNode(ctx context.Context, opts ...Option) (*Node, error) {
|
||||
config: config,
|
||||
}
|
||||
|
||||
// Initialize DHT if enabled
|
||||
if config.EnableDHT {
|
||||
var dhtMode kaddht.ModeOpt
|
||||
switch config.DHTMode {
|
||||
case "client":
|
||||
dhtMode = kaddht.ModeClient
|
||||
case "server":
|
||||
dhtMode = kaddht.ModeServer
|
||||
default:
|
||||
dhtMode = kaddht.ModeAuto
|
||||
}
|
||||
|
||||
dhtOpts := []dht.Option{
|
||||
dht.WithProtocolPrefix(config.DHTProtocolPrefix),
|
||||
dht.WithMode(dhtMode),
|
||||
dht.WithBootstrapPeersFromStrings(config.DHTBootstrapPeers),
|
||||
dht.WithAutoBootstrap(len(config.DHTBootstrapPeers) > 0),
|
||||
}
|
||||
|
||||
var err error
|
||||
node.dht, err = dht.NewDHT(nodeCtx, h, dhtOpts...)
|
||||
if err != nil {
|
||||
cancel()
|
||||
h.Close()
|
||||
return nil, fmt.Errorf("failed to create DHT: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Start background processes
|
||||
go node.startBackgroundTasks()
|
||||
|
||||
@@ -141,8 +172,29 @@ func (n *Node) logConnectionStatus() {
|
||||
}
|
||||
}
|
||||
|
||||
// DHT returns the DHT instance (if enabled)
|
||||
func (n *Node) DHT() *dht.DHT {
|
||||
return n.dht
|
||||
}
|
||||
|
||||
// IsDHTEnabled returns whether DHT is enabled and active
|
||||
func (n *Node) IsDHTEnabled() bool {
|
||||
return n.dht != nil
|
||||
}
|
||||
|
||||
// Bootstrap bootstraps the DHT (if enabled)
|
||||
func (n *Node) Bootstrap() error {
|
||||
if n.dht != nil {
|
||||
return n.dht.Bootstrap()
|
||||
}
|
||||
return fmt.Errorf("DHT not enabled")
|
||||
}
|
||||
|
||||
// Close shuts down the node
|
||||
func (n *Node) Close() error {
|
||||
if n.dht != nil {
|
||||
n.dht.Close()
|
||||
}
|
||||
n.cancel()
|
||||
return n.host.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user