Swarm and Whisper
Study notes on Ethereum's Swarm (decentralised storage) and Whisper (decentralised messaging): concepts, how they work, API/syntax to remember, worked examples, common mistakes and exam preparation.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Swarm and Whisper are parts of the Ethereum-era decentralised infrastructure: Swarm provides a peer-to-peer (P2P) content-addressed storage layer useful for storing dApp assets, backups and large off-chain data; Whisper provides a P2P, privacy-oriented messaging system for nodes and dApps. An engineer building decentralized applications needs to know when to use Swarm for persistent content and when lightweight gossip/messaging like Whisper (or its successors) is appropriate for signalling, notifications, or private messages.
Key ideas
Start from fundamentals:
- Peer-to-peer (P2P) network — a network architecture in which nodes (peers) connect and exchange data directly without relying on a central server. Assumption: peers can discover and route to one another using an overlay routing protocol.
- Content-addressed storage — a storage model where data is retrieved by its cryptographic hash rather than by location or filename. Exact definition: Given a cryptographic hash function H, the address of content C is H(C). Assumption: H is collision-resistant and preimage-resistant. In Swarm/Bee specifically, H is a BMT hash with an 8-byte span prefix, not a plain hash of the entire file.
Swarm (high-level):
- Swarm — a decentralised storage and content distribution system designed for the Ethereum stack. It stores files by splitting them into chunks, addressing them by cryptographic hashes, and distributing chunks across many peers. Assumptions: nodes run Swarm-compatible clients (e.g., Bee), an overlay routing layer maps content addresses to peers.
- Chunk — a fixed-size block of a file. In Swarm/Bee the chunk payload is 4096 bytes; an 8-byte span (total data length covered) is also included for hashing. For the final, possibly shorter chunk, the span encodes the exact byte length; unused segment bytes are treated as zero during hashing (no user-visible padding/truncation). Assumption: chunk size is constant for a given network implementation.
- Binary Merkle Tree (BMT) — the hashing scheme Swarm uses to hash a single 4 KiB chunk by splitting it into 32-byte segments, hashing pairwise up a binary tree, and mixing in an 8-byte span (total covered length). Files larger than one chunk are represented as trees of chunks: parent (intermediate) chunks contain 32-byte references to child chunks and a span; every chunk (data or intermediate) is BMT-hashed. The root chunk's hash is the file's Swarm address. Assumption: H is a strong hash (Keccak-256 in Bee).
- Content routing (Kademlia-like) — an overlay routing table structure that maps content addresses to the nodes holding that content. Exact statement (informal): each node maintains a routing table keyed by node IDs in the same keyspace as content addresses; queries route to nodes whose IDs are closer (in XOR metric) to the content hash. Assumption: routing behaves as in Kademlia (log N hops expected).
- Postage / incentives — modern Swarm implementations include an incentive mechanism so nodes keep and replicate data (for example, a postage-stamp or payment scheme). Exact mechanism varies by implementation and may require payment or proof-of-storage. Assumption: incentives are required for long-term persistence in permissionless networks.
Whisper (high-level):
- Whisper — a P2P messaging protocol designed for short-lived, private messages between nodes in the Ethereum ecosystem. It is a gossip protocol: messages are broadcast or forwarded among peers and storage is ephemeral. Assumptions: the network is small enough that gossip is feasible; messages are not guaranteed persistently delivered.
- Topic — a 4-byte label used in Whisper to let nodes filter messages. Exact description: a topic is not encryption; it is metadata used to route or filter messages efficiently.
- Envelope / payload — an envelope is the outer message container (headers like TTL, PoW, topic, etc.); the payload is the encrypted content. Whisper supports symmetric and asymmetric encryption: you can send to a public key (asymmetric) or to a symmetric key shared between parties.
- Proof-of-work (PoW) for messages — Whisper requires each outgoing message to include a small PoW to make spamming expensive. Exact statement: message headers include PoW parameters (difficulty target or powTime/powTarget) and nodes reject messages below the required PoW. Assumption: PoW parameters are adjustable and exist to limit spam.
- TTL (time-to-live) — each Whisper message carries a TTL measured in seconds; nodes drop the message after TTL expires. Exact statement: message TTL bounds how long nodes store and forward that message in memory.
Diagrams to draw (if asked in exams):
- Swarm chunking and Merkle tree: draw a file, split into 4 KiB blocks labeled c1..c4; beneath each block draw hash H(ci) as leaf nodes; then pairwise combine into parent nodes using H(left||right) until the root R is produced; label the root as the Swarm address (bzz root).
- Whisper message flow: draw nodes A, B, C in overlay; A encrypts a payload and broadcasts; arrows show gossip replication; label message with topic, TTL, and PoW; indicate nodes filtering by topic and decrypting if they have key.
Notes on real-world status: Whisper is largely unmaintained in mainline Ethereum clients and has been succeeded in practice by protocols like Waku (from the Status project). Swarm development also has multiple implementations; check which client (Bee, Swarm) and which API version you use.
Formulas
Remember command/API patterns rather than physics formulas. Common Swarm/Bee syntax and endpoints (examples — implementations vary):
bee start — start the Bee node (background daemon).
curl -F file=@<filename> http://localhost:1633/bzz — upload file to a local Bee node (returns JSON with the reference hash, e.g., {"reference":"<hex>"}). Verify your node's HTTP port.
curl http://localhost:1633/bzz/<reference> — download file/content by Swarm reference.
HTTP API basics (Swarm/Bee): POST /bzz to upload; GET /bzz/<reference> to retrieve. Some older clients exposed /bzz:/; check your client's RPC docs.
Whisper (geth JSON-RPC namespace) common method names and parameter names:
shh_newKeyPair() — create new Whisper identity (asymmetric keypair). (Older docs called this shh_newIdentity.)
shh_generateSymKeyFromPassword(password) — derive symmetric key.
shh_post({"symKeyID"|"pubKey"|"sig":..., "ttl":..., "topic":..., "payload":..., "powTime":..., "powTarget":... }) — send a whisper message. Parameter meanings: ttl in seconds, topic short label, payload hex data, powTime time spent computing PoW, powTarget difficulty target.
shh_subscribe("messages", {filter object}) — subscribe to received messages matching filters (topic, minPow, etc.).
(Exact method names and JSON parameter keys depend on client and version; check your client's RPC docs.)
Worked examples
Example 1 — Store a small file in Swarm (conceptual, step-by-step): Given: a text file hello.txt containing the ASCII string "Hello\n" (6 bytes). Swarm chunk size = 4096 bytes. Steps:
- Chunking: file size (6 bytes) < chunk size, so single chunk c1 = "Hello\n".
- Leaf hash: compute L1 = H(c1). Write formula: L1 = H("Hello\n"). (H is the BMT/cryptographic hash used by implementation.)
- Merkle root: only one leaf, so root R = L1.
- Upload: client stores chunk c1 on some peers and returns content address R (the Bee API returns JSON like {"reference":"<R>"}; gateways may display it as bzz://<R>).
- Retrieval: any node querying
bzz:/<R>requests chunk by that hash; on success it returns the original file "Hello\n". Final answer: The Swarm content address is the root hash R computed by Swarm’s BMT hasher over the chunk with its length prefix (span). It is not equal to a plain hash of "Hello\n".
Example 2 — Whisper message flow (algorithmic trace): Given: Node A wants to send a short private message to B using asymmetric Whisper. Parameters: TTL=60s, topic="0x12345678" (4-byte topic), payload="0x48656c6c6f" (hex for "Hello"), powTarget tuned to small difficulty. Steps:
- A prepares a JSON-RPC
shh_postwithpubKey= B's public key,ttl= 60,topic= 0x12345678,payload= 0x48656c6c6f, plus PoW parameters. - A computes PoW header to meet
powTarget(proof attaches to message). This ensures peers will accept/forward the message. - A broadcasts message to its connected peers. Each peer checks: is message PoW >= required? if yes, store in memory for TTL and forward to peers.
- B receives the message during gossip, sees topic matches, decrypts using its private key, reads payload "Hello". Final answer: B obtains the plaintext if B is online and message TTL and routing allowed delivery; otherwise message may be lost due to ephemeral storage.
Example 3 — Typical API call (practical):
To upload a file to a local Bee node (example command, port may vary):
curl -F file=@hello.txt http://localhost:1633/bzz:/ -> returns JSON or plain text with the bzz address: bzz:/<hash>.
To publish a Whisper message (geth JSON-RPC example):
JSON-RPC call shh_post with payload and topic; watch logs or subscribe with shh_subscribe on another client to trace delivery.
Common mistakes
- Expecting Swarm to guarantee permanent storage by default. Unless you use an incentives/postage mechanism or pinning on a node, data may not be stored forever.
- Thinking Whisper guarantees delivery and persistence. Whisper is ephemeral with TTL and best-effort delivery; it is not a message queue.
- Mixing up topics and encryption in Whisper: topics are not encryption keys; they are filters.
- Forgetting PoW in Whisper: sending a message without required PoW will get it dropped as spam.
- Using exact port numbers or API endpoints without checking client version: Bee/Swarm clients and geth/parity differ.
For your exam
Question types you will see:
- Definitions: define Swarm, content-addressed storage, chunk, Merkle tree, Whisper, topic, TTL.
- Short notes: explain how Swarm stores and retrieves a file; describe Whisper message lifecycle.
- Derivations/diagrams: draw and explain Swarm chunking and Merkle tree; show how content address is formed from chunk hashes.
- Practical/API: write the curl command or JSON-RPC params to upload a file to Swarm or to send a Whisper message.
- Compare/critique: compare Swarm vs IPFS; Whisper vs centralized messaging. Include strengths and limitations. What a full-marks answer includes:
- Precise definitions (bold terms) and assumptions.
- One clear diagram for Merkle tree or overlay routing (label nodes, hashes, chunk data).
- Step-by-step explanation: chunking -> hashing -> building tree -> content address -> routing -> retrieval.
- Practical example or correct API call syntax (mention client/version caveats).
Quick check
- What is a content-address (one-line)?
- In Swarm, why do we split files into small chunks (give two reasons)?
- Name two ways Whisper limits spam or abuse. Answers: 1) A content-address is the cryptographic hash of the content (H(content)) used as its identifier. 2) To allow parallel download from many peers and to enable efficient deduplication and small-object addressing. 3) Message TTL (ephemeral storage) and required per-message proof-of-work (PoW) difficulty; also topic filtering reduces unnecessary processing.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?