3.6 KiB
chat2-reliable: A Reliable P2P Chat Application
Background
chat2-reliable
is an enhanced version of a basic command-line chat application that uses the Waku v2 suite of protocols. This version implements an end-to-end reliability protocol to ensure message delivery and causal ordering in a distributed environment.
Features
- P2P chat capabilities using Waku v2 protocols
- Implementation of e2e reliability protocol
- Support for group chats and direct communication
- Scalable to large groups (up to 10K participants)
- Transport-agnostic design
E2E Reliability Protocol
The e2e reliability protocol in chat2-reliable
is an implementation of the proposal at Vac Forum and includes the following key features:
-
Lamport Clocks: Each participant maintains a Lamport clock for logical timestamping of messages.
-
Causal History: Messages include a short causal history (preceding message IDs) to establish causal relationships.
-
Bloom Filters: A rolling bloom filter is used to track received message IDs and detect duplicates.
-
Lazy Pull Mechanism: Missing messages are requested from peers when causal dependencies are unmet.
-
Eager Push Mechanism: Unacknowledged messages are resent to ensure delivery.
Usage
Building the Application
make
Starting the Application
Basic usage:
./build/chat2-reliable
With custom DNS server:
./build/chat2-reliable --dns-discovery-name-server 8.8.8.8
In-chat Commands
/help
: Display available commands/connect
: Interactively connect to a new peer/peers
: Display the list of connected peers
Example:
/connect /ip4/127.0.0.1/tcp/58426/p2p/16Uiu5rGt2QDLmPKas9zpsBgtr5kRzk473s9wkKSWoYwfcY4Hco33
Message Format
Messages in chat2-reliable
use the following protobuf format:
message Message {
string sender_id = 1;
string message_id = 2;
int32 lamport_timestamp = 3;
repeated string causal_history = 4;
string channel_id = 5;
bytes bloom_filter = 6;
string content = 7;
}
Implementation Details
-
Lamport Clocks: Implemented in the
Chat
struct with methods to increment, update, and retrieve the timestamp. -
Causal History: Stored in the
CausalHistory
field of each message, containing IDs of recent preceding messages. -
Bloom Filters: Implemented as a
RollingBloomFilter
to efficiently track received messages and detect duplicates. -
Message Processing:
- Incoming messages are checked against the bloom filter for duplicates.
- Causal dependencies are verified before processing.
- Messages with unmet dependencies are stored in an incoming buffer.
-
Message Recovery:
- Missing messages are requested from peers.
- A retry mechanism with exponential backoff is implemented for failed retrievals.
-
Conflict Resolution:
- Messages are ordered based on Lamport timestamps and message IDs for consistency.
-
Periodic Tasks:
- Buffer sweeps to process buffered messages and resend unacknowledged ones.
- Sync messages to maintain consistency across peers.
Testing
The implementation includes various tests to ensure the reliability features work as expected:
- Lamport timestamp correctness
- Causal ordering of messages
- Duplicate detection using bloom filters
- Message recovery after network partitions
- Concurrent message sending
- Large group scaling
- Eager push mechanism effectiveness
- Bloom filter window functionality
- Conflict resolution
- New node synchronization
go test -v