Skip to main content

hashtree_network/
lib.rs

1//! Mesh transport primitives for HashTree.
2//!
3//! This crate provides the reusable router, signaling, peer-link, and store
4//! layers for hashtree mesh networking. The default production composition uses
5//! Nostr websockets for signaling and WebRTC for direct links, but the same
6//! abstractions support LAN buses, Bluetooth transports, and simulation.
7//!
8//! # Overview
9//!
10//! - **Storage Backend**: Any [`hashtree_core::Store`] implementation
11//! - **Peer Discovery**: Any [`SignalingTransport`] implementation
12//! - **Data Exchange**: Any [`PeerLink`] / [`PeerLinkFactory`] implementation
13//! - **Protocol**: Request/response with hash-based addressing
14//! - **Adaptive Selection**: Intelligent peer selection based on performance
15//!
16//! # Example
17//!
18//! ```rust,no_run
19//! use hashtree_core::MemoryStore;
20//! use hashtree_network::{MeshStore, MeshStoreConfig};
21//! use nostr_sdk::prelude::*;
22//! use std::sync::Arc;
23//!
24//! #[tokio::main]
25//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
26//!     let local_store = Arc::new(MemoryStore::new());
27//!     let config = MeshStoreConfig::default();
28//!
29//!     let mut store = MeshStore::new(local_store, config);
30//!
31//!     // Generate or load Nostr keys
32//!     let keys = Keys::generate();
33//!
34//!     // Start P2P network
35//!     store.start(keys).await?;
36//!
37//!     // Now store.get() will try local first, then fetch from peers
38//!
39//!     Ok(())
40//! }
41//! ```
42
43pub mod bluetooth;
44pub mod bluetooth_peer;
45pub mod cashu;
46pub mod channel;
47pub mod local_bus;
48pub mod manager;
49pub mod mesh_session;
50pub mod mesh_store_core;
51pub mod mock;
52pub mod multicast;
53pub mod nostr;
54pub mod peer;
55pub mod peer_selector;
56pub mod protocol;
57pub mod real_factory;
58pub mod relay_bridge;
59pub mod root_events;
60pub mod runtime_control;
61pub mod runtime_peer;
62pub mod runtime_state;
63pub mod session;
64pub mod signaling;
65pub mod store;
66pub mod transport;
67pub mod types;
68pub mod wifi_aware;
69
70pub use bluetooth::{
71    install_mobile_bluetooth_bridge, BluetoothBackendState, BluetoothConfig, BluetoothMesh,
72    BluetoothPeerRegistrar, BluetoothRuntimeContext, MobileBluetoothBridge, PendingBluetoothLink,
73};
74pub use bluetooth_peer::{BluetoothFrame, BluetoothLink, BluetoothPeer};
75pub use cashu::{
76    cashu_mint_metadata_path, CashuMintMetadataRecord, CashuMintMetadataStore, CashuQuoteState,
77    CashuRoutingConfig, ExpectedSettlement, NegotiatedQuote, CASHU_MINT_METADATA_VERSION,
78};
79pub use channel::{ChannelError, LatencyChannel, MockChannel, PeerChannel};
80pub use local_bus::{LocalNostrBus, SharedLocalNostrBus};
81pub use manager::{
82    PeerClassifier, PeerEntry as RuntimePeerEntry, PeerStatus, WebRTCConfig, WebRTCManager,
83    WebRTCState,
84};
85pub use mesh_session::{
86    forward_mesh_frame_to_sessions, resolve_root_from_local_buses_with_source,
87    resolve_root_from_peer_sessions, MeshSession,
88};
89pub use mesh_store_core::{
90    build_hedged_wave_plan, normalize_dispatch_config, run_hedged_waves, sync_selector_peers,
91    DataPumpStats, HedgedWaveAction, MeshRoutingConfig, MeshStoreCore, ProductionMeshStore,
92    RequestDispatchConfig, ResponseBehaviorConfig, SimMeshStore,
93};
94pub use mock::{
95    clear_channel_registry, MockConnectionFactory, MockDataChannel, MockLatencyMode, MockRelay,
96    MockRelayTransport,
97};
98pub use multicast::{MulticastConfig, MulticastNostrBus};
99pub use nostr::{decode_signaling_event, encode_signaling_event, NostrRelayTransport};
100pub use peer::{ContentStore, Peer, PendingRequest};
101pub use peer_selector::{
102    peer_principal, PeerMetadataSnapshot, PeerSelector, PeerStats, PersistedPeerMetadata,
103    SelectionStrategy, SelectorSummary, PEER_METADATA_SNAPSHOT_VERSION,
104};
105pub use protocol::{
106    bytes_to_hash, create_fragment_response, create_quote_request, create_quote_response_available,
107    create_quote_response_unavailable, create_request, create_request_with_quote, create_response,
108    encode_chunk, encode_payment, encode_payment_ack, encode_quote_request, encode_quote_response,
109    encode_request, encode_response, hash_to_bytes, hash_to_key, is_fragmented, parse_message,
110    DataChunk, DataMessage, DataPayment, DataPaymentAck, DataQuoteRequest, DataQuoteResponse,
111    DataRequest, DataResponse, FRAGMENT_SIZE, MSG_TYPE_CHUNK, MSG_TYPE_PAYMENT,
112    MSG_TYPE_PAYMENT_ACK, MSG_TYPE_QUOTE_REQUEST, MSG_TYPE_QUOTE_RESPONSE, MSG_TYPE_REQUEST,
113    MSG_TYPE_RESPONSE,
114};
115pub use real_factory::WebRtcPeerLinkFactory;
116pub use relay_bridge::{
117    MeshEventStore, MeshRelayClient, SharedMeshEventStore, SharedMeshRelayClient,
118};
119pub use root_events::{
120    build_root_filter, hashtree_event_identifier, is_hashtree_labeled_event, pick_latest_event,
121    root_event_from_peer, PeerRootEvent, HASHTREE_KIND, HASHTREE_LABEL,
122};
123pub use runtime_control::{
124    can_track_source_peer, cleanup_stale_peers, create_signaling_event, dispatch_signaling_message,
125    forward_mesh_frame_from_runtime, handle_peer_state_event, handle_signaling_event,
126    handle_signaling_message, PeerStateEvent,
127};
128pub use runtime_peer::{
129    can_track_signal_path_peer, remember_peer_signal_path, ConnectionState, MeshPeerEntry,
130    PeerDirection, PeerSignalPath, PeerTransport, TransportPeerRegistrar,
131};
132pub use runtime_state::MeshRuntimeState;
133pub use session::MeshPeer;
134#[cfg(test)]
135pub use session::TestMeshPeer;
136pub use signaling::{MeshRouter, PeerEntry};
137pub use store::{MeshStore, MeshStoreError};
138pub use transport::{PeerLink, PeerLinkFactory, SignalingTransport, TransportError};
139pub use types::{
140    classifier_channel, decrement_htl_with_policy, is_polite_peer, should_forward,
141    should_forward_htl, validate_mesh_frame, ClassifierRx, ClassifierTx, ClassifyRequest, HtlMode,
142    HtlPolicy, IceCandidate, MeshNostrFrame, MeshNostrPayload, MeshStats, MeshStoreConfig,
143    PeerHTLConfig, PeerId, PeerPool, PeerState, PoolConfig, PoolSettings, SignalingMessage,
144    TimedSeenSet, WebRTCStats, BLOB_REQUEST_POLICY, DATA_CHANNEL_LABEL, DECREMENT_AT_MAX_PROB,
145    DECREMENT_AT_MIN_PROB, MAX_HTL, MESH_DEFAULT_HTL, MESH_EVENT_POLICY, MESH_MAX_HTL,
146    MESH_PROTOCOL, MESH_PROTOCOL_VERSION, MESH_SIGNALING_EVENT_KIND, NOSTR_KIND_HASHTREE,
147};
148pub use wifi_aware::{
149    install_mobile_wifi_aware_bridge, mobile_wifi_aware_bridge, MobileWifiAwareBridge,
150    WifiAwareConfig, WifiAwareEvent, WifiAwareNostrBus, WIFI_AWARE_SOURCE,
151};