NexusCore P2P
Modern P2P networking layer for blockchain applications, built on libp2p.
Features
- Peer Discovery: mDNS for local networks, DHT for global discovery
- Block Synchronization: Efficient block sync between peers
- Message Gossiping: PubSub for transaction and consensus messages
- Modular Architecture: Clean separation with no import cycles
- Type Safety: Structured interfaces and message types
- Advanced Rate Limiting: Token bucket rate limiting per peer and protocol
- DoS Protection: Comprehensive DoS protection with reputation system
- Security Monitoring: Real-time security statistics and anomaly detection
Installation
go get github.com/diegofxm/nexuscore-p2p
Usage
import (
"github.com/diegofxm/nexuscore-p2p/pkg/p2p"
"github.com/diegofxm/nexuscore-p2p/pkg/types"
)
// Create P2P node
config := types.DefaultConfig()
config.NetworkID = "my-network"
node, err := p2p.NewNode(config)
if err != nil {
panic(err)
}
// Start the node
err = node.Start()
if err != nil {
panic(err)
}
// Get connected peers
peers := node.GetPeers()
fmt.Printf("Connected to %d peers\n", len(peers))
// Stop the node
defer node.Stop()
Advanced Security Features
import (
"github.com/diegofxm/nexuscore-p2p/pkg/ratelimit"
"github.com/diegofxm/nexuscore-p2p/pkg/security"
)
// Rate Limiting
rateLimiter := ratelimit.NewRateLimiter(ratelimit.DefaultRateLimitConfig())
allowed := rateLimiter.Allow(peerID, protocolID)
// DoS Protection
dosProtection := security.NewDoSProtection(security.DefaultDoSConfig())
err := dosProtection.ValidateConnection(peerID)
err = dosProtection.ValidateMessage(peerID, protocolID, messageSize)
// Security Statistics
stats := dosProtection.GetStats()
fmt.Printf("Blocked peers: %d\n", stats.BlockedPeers)
Demo
Run the comprehensive demo to see all features:
go run main.go
Architecture
pkg/
├── types/ # Shared types and constants
├── discovery/ # Peer discovery (mDNS, DHT)
├── sync/ # Block synchronization
├── gossip/ # Message gossiping
├── protocols/ # Custom protocol handlers
├── ratelimit/ # Advanced rate limiting system
├── security/ # DoS protection and reputation system
└── p2p/ # Main node orchestrator
License
MIT License