CubeNet is in-house developed decentralized high-performance networking library, designed for blockchain and distributed systems. It provides a modular, scalable, and pluggable network architecture, supporting efficient network communication for large-scale nodes, with elastic network topology and adaptive node scale capabilities.
- High-Performance Routing Protocol - Self-developed Cubemlia protocol, shortest path algorithm based on communication latency, outperforming traditional Kademlia protocol
- Multiple Broadcast Strategies - Supports SPT (Shortest Path Tree), BFS, Gossip, Multicast and other broadcast algorithms
- Multi-Protocol Support - Integrates TCP, TLS, Noise, WebSocket and other transport protocols
- Structured P2P Network - Supports Kademlia/Cubemlia DHT, suitable for thousand-node scale
- Multi-Layer Network - Supports node multi-identity, homogeneous multi-chain isolation
- Secure Communication - Noise protocol framework, TLS encrypted transmission, identity authentication
- Smart Routing - Dijkstra shortest path algorithm, automatic network partition avoidance
- Connection Management - Heartbeat detection, automatic reconnection, whitelist/blacklist management
CubeNet adopts a layered modular architecture design:
┌─────────────────────────────────────────────────────────┐
│ Application Layer │
├─────────────────────────────────────────────────────────┤
│ P2P Network Layer │
│ ┌──────────────┬─────────────┬──────────────────────┐ │
│ │ Gossip │ Routing │ Admin/Metrics │ │
│ ├──────────────┼─────────────┴──────────────────────┤ │
│ │ Broadcast │ Unicast/Multicast │ │
│ ├──────────────┴────────────────────────────────────┤ │
│ │ Connection Manager │ │
│ ├───────────────────────────────────────────────────┤ │
│ │ Transport Layer (Transport/Deliver/Session) │ │
│ ├───────────────────────────────────────────────────┤ │
│ │ Network Layer (TCP/TLS/Noise/WebSocket) │ │
│ └───────────────────────────────────────────────────┘ │
├─────────────────────────────────────────────────────────┤
│ base (Crypto/Utils/Log) │
└─────────────────────────────────────────────────────────┘
| Module | Description |
|---|---|
| P2pNetwork | Main P2P network interface, provides node management, message sending/receiving, etc. |
| Routing | Routing strategy management, supports Dijkstra, BFS, Kademlia and other algorithms |
| Gossip | Broadcast protocol implementation, supports multiple broadcast strategies |
| Transport | Transport layer abstraction, manages node connections and message delivery |
| ConnectionManager | Connection manager, handles node discovery, heartbeat, reconnection, etc. |
| Noise | Noise protocol framework implementation, provides secure encrypted communication |
CubeNet implements a complete P2P node discovery protocol:
- Bootstrap: Initial node bootstrapping to join the network
- FindNode: Discover new nodes from neighbor nodes (filtered with Bloom Filter)
- PingNode: Detect node reachability and network latency
- AddNode: Add new nodes to the neighbor table
| Feature | Kademlia | Cubemlia |
|---|---|---|
| Distance Algorithm | XOR logical distance | Communication latency |
| Query Complexity | O(log N) | O(log N) |
| Routing Efficiency | Suitable for public chains | More suitable for consortium chains |
| Latency Optimization | None | Automatic network topology optimization |
- Unicast: Unicast routing based on Dijkstra shortest path algorithm
- Broadcast:
- SPT (Shortest Path Tree): Shortest path tree broadcast
- BFS (Breadth-First Search): Breadth-first search broadcast
- Gossip: Random propagation protocol
- Multicast: Multicast message delivery
-
Noise Protocol: Modern encrypted communication protocol framework
- Supports X25519 key exchange
- ChaCha20-Poly1305 encryption
- SHA256 hashing
-
TLS/SSL: Traditional TLS encrypted channel
-
Identity Authentication: Public key-based node identity verification
Supports building multi-layer P2P networks:
- Node multi-identity management
- Effective utilization of physical resources
- Homogeneous multi-chain network isolation
- Node Scale: Supports stable networking for thousand-level nodes
- Message Latency: Optimized based on Cubemlia protocol, minimal network latency
- Throughput: Supports high-concurrency message delivery
- Fault Tolerance: Automatic handling of dynamic node join/leave
- Elastic Topology: Adaptive network scale changes
CubeNet has been applied to multiple production systems:
- Distributed Key System: Secure multi-party computation network
- Privacy-Preserving Computation: Fair platform privacy protection computing network
- Decentralized Applications: DAPP backend network layer
- Operating System: Linux (CentOS 7+, Ubuntu 24.04+)
- Compiler: GCC 7.0+
- Build Tool: Bazel 1.2.1
- Dependencies: OpenSSL, Boost 1.67.0+
Note: Since the
cpp-thirdpartydependency library is not yet open source, CubeNet cannot be independently compiled and built at this time. The build environment requires internal network access permissions.
- Clone the repository
git clone https://github.com/<REPO_NAME>/CubeNet.git
cd CubeNet- Build the project
# Build all targets
sh build.sh all
# Build p2p node program
sh build.sh p2p_node
- Run 4-node p2p node
cd integration_test
# Run p2p node program
sh local_deploy.sh run deploy
# Initialize network
python3 main.py --case_id case01_p2p_network_initialize
# Start network
python3 main.py --case_id case02_p2p_network_startup
# Send unicast message
python3 main.py --case_id case03_send_unicast
# Send broadcast message
python3 main.py --case_id case04_send_broadcast
......CubeNet can be compiled as a static library (.a file) or shared library (.so file) for other projects to reference.
Compile static library using Bazel:
# Compile static library (.a file)
bazel build //src/p2p_network:p2p_network
# Compile shared library (.so file)
bazel build //src/p2p_network:libcubenet.soAfter compilation, library files are located at:
- Static Library:
bazel-bin/src/p2p_network/libp2p_network.a - Shared Library:
bazel-bin/src/p2p_network/libcubenet.so
Add to your project's WORKSPACE file:
http_archive(
name = "cubenet",
urls = ["https://xxxxxx/archive/<commit-hash>.tar.gz"],
strip_prefix = "CubeNet-<commit-hash>",
)Then reference in BUILD file:
cc_library(
name = "your_library",
srcs = ["your_source.cpp"],
deps = [
"@cubenet//src/p2p_network:p2p_network",
],
)- Copy header and library files:
# Create installation directory
mkdir -p /usr/local/include/cubenet
mkdir -p /usr/local/lib
# Copy header files
cp -r include/cubenet/* /usr/local/include/cubenet/
# Copy static library
cp bazel-bin/src/p2p_network/libp2p_network.a /usr/local/lib/- Use in your project:
#include "include/cubenet/p2p_network/p2p_network.h"
int main() {
auto p2p_network = std::make_shared<cubenet::P2pNetwork>();
// Your code...
return 0;
}- Compile and link:
g++ your_program.cpp -o your_program \
-I/usr/local/include \
-L/usr/local/lib \
-lp2p_network \
-lssl -lcrypto \
-lboost_system -lboost_thread -lboost_filesystem \
-lpthread -lrt -ldl -lmNote: The static library requires additional dependencies. For an easier approach, use the shared library
libcubenet.soinstead.
CubeNet static library depends on the following libraries, which need to be linked together:
- OpenSSL/Tongsuo: SSL/TLS encryption support
- Boost 1.67.0+: boost_system, boost_thread, boost_filesystem
- spdlog: Logging library
- jsoncpp: JSON processing
- System Libraries: pthread, rt, dl, m
The shared library libcubenet.so includes all dependencies:
# Compile shared library
bazel build //src/p2p_network:libcubenet.so
# Create installation directory
INSTALL_DIR=/tmp/cubenet_install
mkdir -p $INSTALL_DIR/include/cubenet
mkdir -p $INSTALL_DIR/lib
# Install header files
cp -r include/cubenet/* $INSTALL_DIR/include/cubenet/
# Install shared library
cp bazel-bin/src/p2p_network/libcubenet.so $INSTALL_DIR/lib/
# Compile with shared library
g++ your_program.cpp -o your_program \
-I$INSTALL_DIR/include \
-L$INSTALL_DIR/lib \
-lcubenet \
-lstdc++ \
-lpthread -lrt -ldl -lm
# Run (set dynamic library path)
export LD_LIBRARY_PATH=$INSTALL_DIR/lib:$LD_LIBRARY_PATH
./your_program#!/bin/bash
# Compile CubeNet shared library
bazel build //src/p2p_network:libcubenet.so
# Create installation directory
INSTALL_DIR=${1:-/usr/local}
mkdir -p $INSTALL_DIR/include/cubenet
mkdir -p $INSTALL_DIR/lib
# Install header files
cp -r include/cubenet/* $INSTALL_DIR/include/cubenet/
# Install shared library
cp bazel-bin/src/p2p_network/libcubenet.so $INSTALL_DIR/lib/
echo "CubeNet installed to $INSTALL_DIR"
echo "Include path: $INSTALL_DIR/include/cubenet"
echo "Library: $INSTALL_DIR/lib/libcubenet.so"
echo ""
echo "To use, add to your compile command:"
echo " -I$INSTALL_DIR/include -L$INSTALL_DIR/lib -lcubenet"
echo "And set LD_LIBRARY_PATH: export LD_LIBRARY_PATH=$INSTALL_DIR/lib:\$LD_LIBRARY_PATH"Usage:
chmod +x install.sh
sudo ./install.sh /usr/localCubeNet/
├── include/cubenet/ # Public header files
│ ├── p2p_network/ # P2P network interface
│ ├── routing/ # Routing strategy
│ ├── gossip/ # Broadcast protocol
│ ├── transport/ # Transport layer
│ ├── network/ # Network layer
│ ├── noise/ # Noise protocol
│ └── utils/ # Utility library
├── src/ # Implementation files
├── example/ # Example code
│ ├── p2p_node/ # P2P node example
│ ├── tcp_server/ # TCP server example
│ └── tcp_client/ # TCP client example
├── test/ # Unit tests
├── integration_test/ # Integration tests
├── conf/ # Configuration file examples
└── third_party/ # Third-party dependencies
# Run all unit tests
cd tools/test
sh test_all_without_cov.sh
# Run integration tests
cd integration_test
sh build_image.sh p2p_node
sh local_deploy.sh run deploy
sh local_deploy.sh integration_test
The project follows Google C++ code conventions, checked with cpplint:
# Check code style
python tools/cpplint/cpplint.py <file>For interface documentation, please refer to:
- Interface Documentation - Detailed interface description
- Configuration File Example - Detailed configuration description
- Example Code - Various usage scenarios
- Configuration Description
- Follow code style conventions
- Add necessary unit tests
- Update relevant documentation
- Ensure all tests pass
This project is open sourced under Apache 2.0 license.