Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 8: Exhaustive Code Walkthrough (kinetic-network & kinetic-dns)

If kinetic-core defines the rules and kinetic-daemon orchestrates the loops, then kinetic-network and kinetic-dns are the physical gateways. They represent the precise boundary layers where the untrusted outside world collides with the strictly enforced mathematical reality of the local client.

In this chapter, we explore the exact Rust code that filters P2P Kademlia gossip and synthesizes local OS DNS responses.


1. The Immunological Filter: kinetic-network

The kinetic-network crate utilizes libp2p-kad to participate in the global DHT swarm. However, as discussed in Chapter 3, standard Kademlia is entirely “blind.” To enforce Competitive Gossip, Kinetic provides a highly hostile custom implementation of the RecordStore trait.

1.1 The KineticRecordStore Implementation

Located in kinetic-network/src/store.rs, the KineticRecordStore intercepts every single piece of data a remote peer attempts to store on the local node.

#![allow(unused)]
fn main() {
use libp2p::kad::store::{RecordStore, Result};
use libp2p::kad::Record;
use kinetic_core::types::{Reveal, Heartbeat};

pub struct KineticRecordStore {
    // In-memory or Sled-backed hashmap
    records: HashMap<Vec<u8>, Record>, 
    vdf_engine: Arc<dyn VdfEngine>,
}

impl RecordStore for KineticRecordStore {
    type RecordsIter<'a> = std::vec::IntoIter<std::borrow::Cow<'a, Record>>;
    type ProvidedIter<'a> = std::vec::IntoIter<std::borrow::Cow<'a, ProviderRecord>>;

    fn put(&mut self, record: Record) -> Result<()> {
        // 1. Deserialization Attempt
        if let Ok(reveal) = bincode::deserialize::<Reveal>(&record.value) {
            // It's a Reveal payload. Send to rigorous validation.
            if self.validate_reveal(&reveal) {
                self.records.insert(record.key.as_ref().to_vec(), record);
                return Ok(());
            } else {
                return Err(libp2p::kad::store::Error::ValueInvalid); // REJECT
            }
        }
        
        if let Ok(heartbeat) = bincode::deserialize::<Heartbeat>(&record.value) {
            // It's a Heartbeat payload. Send to validation.
            if self.validate_heartbeat(&heartbeat) {
                self.records.insert(record.key.as_ref().to_vec(), record);
                return Ok(());
            } else {
                return Err(libp2p::kad::store::Error::ValueInvalid); // REJECT
            }
        }

        // If it deserializes into neither, it is garbage spam.
        Err(libp2p::kad::store::Error::ValueInvalid) // REJECT
    }
}
}

This put function is the front line of defense. The node strictly attempts to deserialize the incoming byte array using bincode. If it fails, the node drops the record. If it succeeds, it triggers the intense cryptographic filter.

1.2 The Cryptographic Filter

If the payload is a Reveal, the node executes validate_reveal.

#![allow(unused)]
fn main() {
    fn validate_reveal(&self, reveal: &Reveal) -> bool {
        // 1. Validate the Signature
        let pubkey = ed25519_dalek::VerifyingKey::from_bytes(&reveal.pubkey).unwrap();
        let sig = ed25519_dalek::Signature::from_bytes(&reveal.signature).unwrap();
        
        if pubkey.verify_strict(&reveal.signable_bytes(), &sig).is_err() {
            return false; // Forged signature
        }

        // 2. Reconstruct the Commitment Hash
        let challenge_bytes = hex::decode(&reveal.drand_randomness).unwrap();
        let mut hasher = sha2::Sha256::new();
        hasher.update(reveal.name.as_bytes());
        hasher.update(&reveal.salt);
        hasher.update(&challenge_bytes);
        hasher.update(&reveal.pubkey);
        let mut hash = [0u8; 32];
        hash.copy_from_slice(&hasher.finalize());

        // 3. Verify VDF Math Requirements
        let required = calculate_required_iterations(&reveal.name);
        if reveal.iterations < required {
            return false; // Did not compute the required squatter penalty
        }

        // 4. The Final \\(O(\log T)\\) O(1) Verification
        self.vdf_engine.verify(
            &Commitment { hash },
            reveal.iterations,
            &reveal.vdf_proof
        )
    }
}

This execution block is perfectly deterministic. If validate_reveal returns false, the RecordStore immediately throws a ValueInvalid error. libp2p interprets this error by dropping the data and, crucially, refusing to gossip the record to any other peers.

This active immune response ensures that fake data cannot propagate beyond the specific peer the attacker is directly attacking.


2. The OS Interceptor: kinetic-dns

The kinetic-dns crate leverages the hickory-dns framework to intercept the user’s OS-level traffic on 127.0.0.1:53.

2.1 The Split-DNS Traffic Handler

Inside kinetic-dns/src/server.rs, the KineticDnsHandler implements the RequestHandler trait from Hickory.

#![allow(unused)]
fn main() {
use hickory_server::server::{Request, RequestHandler, ResponseHandler, ResponseInfo};
use hickory_server::authority::MessageResponseBuilder;
use hickory_server::proto::op::Header;
use hickory_server::proto::rr::{Record, RData, Name};

#[async_trait::async_trait]
impl RequestHandler for KineticDnsHandler {
    async fn handle_request<R: ResponseHandler>(
        &self,
        request: &Request,
        mut response_handle: R,
    ) -> ResponseInfo {
        let query = request.queries().first().unwrap();
        let name_str = query.name().to_string();

        if name_str.ends_with(".kin.") {
            // 1. SOVEREIGN TRAFFIC INTERCEPTION
            // Issue a Kademlia GET query down to the network layer
            match self.network_client.resolve_name(name_str.clone()).await {
                Ok(reveal) => {
                    // Extract the IP payload
                    let ip_str = String::from_utf8(reveal.payload).unwrap();
                    let ip_addr: Ipv4Addr = ip_str.parse().unwrap();
                    
                    // Synthesize a perfectly standard DNS 'A' record response
                    let mut record = Record::with(query.name().clone(), hickory_server::proto::rr::RecordType::A, 60);
                    record.set_data(Some(RData::A(ip_addr)));

                    let builder = MessageResponseBuilder::from_message_request(request);
                    let mut header = Header::response_from_request(request.header());
                    header.set_authoritative(true); // We are the absolute authority for .kin

                    let response = builder.build(header, vec![&record], vec![], vec![], vec![]);
                    response_handle.send_response(response).await.unwrap()
                }
                Err(_) => {
                    // Name not found in DHT
                    send_nxdomain(request, response_handle).await
                }
            }
        } else {
            // 2. LEGACY PASS-THROUGH
            // The user typed google.com. Do not leak or block it. 
            // Forward it natively to 1.1.1.1 over UDP.
            self.forward_to_upstream(request, response_handle).await
        }
    }
}
}

Line-by-Line Breakdown:

  • if name_str.ends_with(".kin."): The simple, brutal suffix check. This is the exact moment the traffic is split.
  • self.network_client.resolve_name(name_str.clone()).await: This triggers the Kademlia swarm lookup. The network client will aggressively query the DHT utilizing the multi-key Redundant Deterministic Storage algorithm to bypass Eclipse attacks, eventually verifying the signature/VDF and bubbling the pure Reveal struct back up to the DNS handler.
  • record.set_data(Some(RData::A(ip_addr))): This is the magic. The browser thinks it just talked to a highly trusted ICANN root server. It has no idea that the IP address it is receiving was actually extracted from an Ed25519-signed Kademlia payload. The browser instantly routes the user’s traffic to the Web3 application.
  • self.forward_to_upstream(...): For all non-.kin queries, the daemon maintains a persistent UDP socket to Cloudflare (1.1.1.1) or Google (8.8.8.8). It proxies the raw byte buffer back and forth, acting as a transparent tunnel. This is why running kinetic-daemon does not break the user’s internet.

Through kinetic-network and kinetic-dns, the protocol effectively weaponizes the user’s local operating system against the legacy ICANN infrastructure, creating a parallel, mathematically sovereign internet that seamlessly lives alongside the old one.