[WIP / DRAFT] P2P: Fast Trusted Relay (FTR) Implementation.#930
Draft
D-Stacks wants to merge 62 commits intokaspanet:masterfrom
Draft
[WIP / DRAFT] P2P: Fast Trusted Relay (FTR) Implementation.#930D-Stacks wants to merge 62 commits intokaspanet:masterfrom
D-Stacks wants to merge 62 commits intokaspanet:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fast Trusted Relay (FTR) Implementation
Summary
Note: This is a WIP draft PR, published for testing and discussion visibility, and not considered stable!
Introduces a Fast Trusted Relay (FTR) network protocol for Kaspa, enabling low-latency block propagation via a custom push based UDP transport protocol with Reed-Solomon Forward Error Correction (FEC). Designed for trusted operators (mining pools, infrastructure providers, or even just altruistic actors) to achieve minimal block transfer latency across geographically distributed nodes. Although this directly profits those that require low-latency block propagation, any fast relay overlay, also indirectly helps the global kaspa network reduce propagation. Further use-cases may include satellite communications, where RTTs can inherently become a limiting factor, or faster communication between nodes situated within one data-center. The application of the customizable FEC also facilitates adopting this protocol in potentially lossy network conditions.
Furthermore, Fast Relay networks are oftentimes exclusive services offered by enterprise for enterprise, which can easily compound, for example, pool advantages over individual solo miners, by offering an open source easily accessible Fast Relay option on the node, free for anyone to use, we limit such centralized advantages and also help facilitate more such overlays to establish themselves on the kaspa network.
The main observable changes are that logs will display the number of blocks propagated via the fast relay network, alongside the normal blocks added logs, as such:
One key limitation of this protocol is that due to its push-based nature it cannot function in isolation, and should / must be used together with standard relay. This is quintessential for resolving protocol aspects such as orphan resolution, etc... Currently, adding a peer to the FTR network will automatically add the peer to the standard relay as well, in order to facilitate such operations.
Although this PR is a far-shot away from replacing our standard tcp relay, adopting and adapting such a custom udp protocol could potentially be an entry point for gathering data and experience for such a maneuver, many projects, such as solana's Turbine block propagation, already use highly customized and optimized udp transport layers, in order to reduce latency.
Motivation
Standard P2P block relay (TCP-based with request-response semantics) introduces latency that compounds across multiple hops and during network congestion. For operations where latency directly impacts orphan rates and revenue, a dedicated push-based relay with erasure coding provides significant advantages:
One trade-off is that the FTR bypasses block signaling and propagates whole blocks. Furthermore, FEC increases the bandwidth usage, which quickly compounds with the number of peers in the network. As such, a large network bandwidth should be a prerequisite to run such a FTR network. That being said, facilitating 10-20 peers (which should easily be enough for a good globally dispersed network) on a 1Gbit or 10Gbit connection should easily be sustainable, As should a small handful of peers on even more limited bandwidth.
Example Usage:
./kaspad --trusted-relay-secret=[your secret] --trusted-relay-incoming=[list of peer ips we expect to get packets from] --trusted-relay-outgoing=[list of peer ips we wish to push packets to] --externalip=[your ip]Establishing a connection might take a while and connection re-attempts happen every 30 secs.
Note:
To increase these limits on Linux (in this example to 32 MB) the following can be run:
receive buffer:
sudo sysctl -w net.core.rmem_max=33554432send buffer:
sudo sysctl -w net.core.wmem_max=33554432Architecture
Two-Plane Design
Data Plane Pipeline
Note: The stack is designed upon crossbeam worker threads, and can easily be multithreaded in high load environments.
Key Features
Reed-Solomon FEC Encoding
reed-solomon-simdcrate (although actual benefits compared to other crates have not been benchmarked)HMAC-SHA256 Authentication
hmac(secret, header ‖ payload)hmac(secret, nonce ‖ direction ‖ udp_port)IBD-Aware Control
New Crate:
kaspa-trusted-relayModule Structure
Public API
Flow Integration
New Flow:
HandleFastTrustedRelayFlowThis is a new "routerless" flow added to p2p which listens on and handles incoming FTR blocks.
Located in
protocol/flows/src/fast_trusted_relay/flow.rs:Flowtrait (routerless)recv_block()InvRelayBlockListenerFlowContext Integration
FastTrustedRelayinstanceCLI Arguments
--trusted-relay-incomingVec<Address>[]--trusted-relay-outgoingVec<Address>[]--trusted-relay-secretString--fec-data-blocksusize16--fec-parity-blocksusize4--udp-payload-sizeusize1200NOTE:
The payload size must be under the Ethernet MTU to guarantee functionality, otherwise packets may get fragmented and corrupted. Currently this is clamped to "reasonable" allowed values.
All participants of the FTR must specify the same amount of FEC parity and data blocks, as well as share the same trusted relay secret.
A lot more configurations can be found and altered in
protocol/trusted_relay/src/params.rs. This includes options to multi-thread specific workers, etc., but defaults are still under consideration and as such not included in client args.currently ports will always revert to default ports (TCP control -> 16113, UDP transport -> 16114)
Wire Formats
Packet (UDP)
The wire format of a singular UDP packet sent over the FTR:
FtrBlock (internal)
This is a pre-encoding of a kaspa block, which includes an explicit block hash, as well as offsets to extract the block header and block txs.
TCP Handshake
Performance Optimizations
reed-solomon-simdfor encoding/decodingDependencies Added
reed-solomon-simd- SIMD-accelerated FEC codecsocket2- Low-level socket configurationfixedbitset- Data structure utilized for Compact deduplication trackingTesting
benches/for encoder/decoder throughputFuture Considerations
The main consideration is currently working on stability / bug fixes and proving the concept. Possibly implementing telemetry should be the next phase, for more comprehensive data gathering.
Here are some other ideas for the future: