Complete BZZZ functionality port to CHORUS
🎭 CHORUS now contains full BZZZ functionality adapted for containers Core systems ported: - P2P networking (libp2p with DHT and PubSub) - Task coordination (COOEE protocol) - HMMM collaborative reasoning - SHHH encryption and security - SLURP admin election system - UCXL content addressing - UCXI server integration - Hypercore logging system - Health monitoring and graceful shutdown - License validation with KACHING Container adaptations: - Environment variable configuration (no YAML files) - Container-optimized logging to stdout/stderr - Auto-generated agent IDs for container deployments - Docker-first architecture All proven BZZZ P2P protocols, AI integration, and collaboration features are now available in containerized form. Next: Build and test container deployment. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
167
p2p/config.go
Normal file
167
p2p/config.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Config holds configuration for a Bzzz P2P node
|
||||
type Config struct {
|
||||
// Network configuration
|
||||
ListenAddresses []string
|
||||
NetworkID string
|
||||
|
||||
// Discovery configuration
|
||||
EnableMDNS bool
|
||||
MDNSServiceTag string
|
||||
|
||||
// DHT configuration
|
||||
EnableDHT bool
|
||||
DHTBootstrapPeers []string
|
||||
DHTMode string // "client", "server", "auto"
|
||||
DHTProtocolPrefix string
|
||||
|
||||
// Connection limits
|
||||
MaxConnections int
|
||||
MaxPeersPerIP int
|
||||
ConnectionTimeout time.Duration
|
||||
|
||||
// Security configuration
|
||||
EnableSecurity bool
|
||||
|
||||
// Pubsub configuration
|
||||
EnablePubsub bool
|
||||
BzzzTopic string // Task coordination topic
|
||||
HmmmTopic string // Meta-discussion topic
|
||||
MessageValidationTime time.Duration
|
||||
}
|
||||
|
||||
// Option is a function that modifies the node configuration
|
||||
type Option func(*Config)
|
||||
|
||||
// DefaultConfig returns a default configuration for Bzzz nodes
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
// Listen on specific port 3333 for TCP
|
||||
ListenAddresses: []string{
|
||||
"/ip4/0.0.0.0/tcp/3333",
|
||||
"/ip6/::/tcp/3333",
|
||||
},
|
||||
NetworkID: "bzzz-network",
|
||||
|
||||
// Discovery settings
|
||||
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,
|
||||
ConnectionTimeout: 30 * time.Second,
|
||||
|
||||
// Security enabled by default
|
||||
EnableSecurity: true,
|
||||
|
||||
// Pubsub for coordination and meta-discussion
|
||||
EnablePubsub: true,
|
||||
BzzzTopic: "bzzz/coordination/v1",
|
||||
HmmmTopic: "hmmm/meta-discussion/v1",
|
||||
MessageValidationTime: 10 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
// WithListenAddresses sets the addresses to listen on
|
||||
func WithListenAddresses(addrs ...string) Option {
|
||||
return func(c *Config) {
|
||||
c.ListenAddresses = addrs
|
||||
}
|
||||
}
|
||||
|
||||
// WithNetworkID sets the network ID
|
||||
func WithNetworkID(networkID string) Option {
|
||||
return func(c *Config) {
|
||||
c.NetworkID = networkID
|
||||
}
|
||||
}
|
||||
|
||||
// WithMDNS enables or disables mDNS discovery
|
||||
func WithMDNS(enabled bool) Option {
|
||||
return func(c *Config) {
|
||||
c.EnableMDNS = enabled
|
||||
}
|
||||
}
|
||||
|
||||
// WithMDNSServiceTag sets the mDNS service tag
|
||||
func WithMDNSServiceTag(tag string) Option {
|
||||
return func(c *Config) {
|
||||
c.MDNSServiceTag = tag
|
||||
}
|
||||
}
|
||||
|
||||
// WithMaxConnections sets the maximum number of connections
|
||||
func WithMaxConnections(max int) Option {
|
||||
return func(c *Config) {
|
||||
c.MaxConnections = max
|
||||
}
|
||||
}
|
||||
|
||||
// WithConnectionTimeout sets the connection timeout
|
||||
func WithConnectionTimeout(timeout time.Duration) Option {
|
||||
return func(c *Config) {
|
||||
c.ConnectionTimeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
// WithSecurity enables or disables security
|
||||
func WithSecurity(enabled bool) Option {
|
||||
return func(c *Config) {
|
||||
c.EnableSecurity = enabled
|
||||
}
|
||||
}
|
||||
|
||||
// WithPubsub enables or disables pubsub
|
||||
func WithPubsub(enabled bool) Option {
|
||||
return func(c *Config) {
|
||||
c.EnablePubsub = enabled
|
||||
}
|
||||
}
|
||||
|
||||
// WithTopics sets the Bzzz and HMMM topic names
|
||||
func WithTopics(bzzzTopic, hmmmTopic string) Option {
|
||||
return func(c *Config) {
|
||||
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
|
||||
}
|
||||
}
|
||||
200
p2p/node.go
Normal file
200
p2p/node.go
Normal file
@@ -0,0 +1,200 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"chorus.services/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"
|
||||
)
|
||||
|
||||
// Node represents a Bzzz P2P node
|
||||
type Node struct {
|
||||
host host.Host
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
config *Config
|
||||
dht *dht.LibP2PDHT // Optional DHT for distributed discovery
|
||||
}
|
||||
|
||||
// NewNode creates a new P2P node with the given configuration
|
||||
func NewNode(ctx context.Context, opts ...Option) (*Node, error) {
|
||||
config := DefaultConfig()
|
||||
for _, opt := range opts {
|
||||
opt(config)
|
||||
}
|
||||
|
||||
nodeCtx, cancel := context.WithCancel(ctx)
|
||||
|
||||
// Build multiaddresses for listening
|
||||
var listenAddrs []multiaddr.Multiaddr
|
||||
for _, addr := range config.ListenAddresses {
|
||||
ma, err := multiaddr.NewMultiaddr(addr)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, fmt.Errorf("invalid listen address %s: %w", addr, err)
|
||||
}
|
||||
listenAddrs = append(listenAddrs, ma)
|
||||
}
|
||||
|
||||
// Create libp2p host with security and transport options
|
||||
h, err := libp2p.New(
|
||||
libp2p.ListenAddrs(listenAddrs...),
|
||||
libp2p.Security(noise.ID, noise.New),
|
||||
libp2p.Transport(tcp.NewTCPTransport),
|
||||
libp2p.DefaultMuxers,
|
||||
libp2p.EnableRelay(),
|
||||
)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, fmt.Errorf("failed to create libp2p host: %w", err)
|
||||
}
|
||||
|
||||
node := &Node{
|
||||
host: h,
|
||||
ctx: nodeCtx,
|
||||
cancel: cancel,
|
||||
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.NewLibP2PDHT(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()
|
||||
|
||||
return node, nil
|
||||
}
|
||||
|
||||
// Host returns the underlying libp2p host
|
||||
func (n *Node) Host() host.Host {
|
||||
return n.host
|
||||
}
|
||||
|
||||
// ID returns the peer ID of this node
|
||||
func (n *Node) ID() peer.ID {
|
||||
return n.host.ID()
|
||||
}
|
||||
|
||||
// Addresses returns the multiaddresses this node is listening on
|
||||
func (n *Node) Addresses() []multiaddr.Multiaddr {
|
||||
return n.host.Addrs()
|
||||
}
|
||||
|
||||
// Connect connects to a peer at the given multiaddress
|
||||
func (n *Node) Connect(ctx context.Context, addr string) error {
|
||||
ma, err := multiaddr.NewMultiaddr(addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid multiaddress %s: %w", addr, err)
|
||||
}
|
||||
|
||||
addrInfo, err := peer.AddrInfoFromP2pAddr(ma)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse addr info: %w", err)
|
||||
}
|
||||
|
||||
return n.host.Connect(ctx, *addrInfo)
|
||||
}
|
||||
|
||||
// Peers returns the list of connected peers
|
||||
func (n *Node) Peers() []peer.ID {
|
||||
return n.host.Network().Peers()
|
||||
}
|
||||
|
||||
// ConnectedPeers returns the number of connected peers
|
||||
func (n *Node) ConnectedPeers() int {
|
||||
return len(n.Peers())
|
||||
}
|
||||
|
||||
// startBackgroundTasks starts background maintenance tasks
|
||||
func (n *Node) startBackgroundTasks() {
|
||||
ticker := time.NewTicker(30 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-n.ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
// Periodic maintenance tasks
|
||||
n.logConnectionStatus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// logConnectionStatus logs the current connection status
|
||||
func (n *Node) logConnectionStatus() {
|
||||
peers := n.Peers()
|
||||
fmt.Printf("🐝 Bzzz Node Status - ID: %s, Connected Peers: %d\n",
|
||||
n.ID().ShortString(), len(peers))
|
||||
|
||||
if len(peers) > 0 {
|
||||
fmt.Printf(" Connected to: ")
|
||||
for i, p := range peers {
|
||||
if i > 0 {
|
||||
fmt.Printf(", ")
|
||||
}
|
||||
fmt.Printf("%s", p.ShortString())
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
// DHT returns the DHT instance (if enabled)
|
||||
func (n *Node) DHT() *dht.LibP2PDHT {
|
||||
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