Skip to content

Latest commit

 

History

History
671 lines (519 loc) · 23.8 KB

PROTOCOL.md

File metadata and controls

671 lines (519 loc) · 23.8 KB

Protocol

This document describes the hbs2 wire protocol at the structural level: what messages exist, what they contain, and how they fit together. It is meant to be sufficient for an engineer who wants to understand the protocol shape, audit interoperability, or re-implement a peer in another language.

It is not a byte-exact specification. CBOR encoding details for each field type are inherited from the serialise Haskell library and would require an independent audit before being treated as normative. Sections below cite source files for each definition so the canonical form can be consulted directly.

Status

This is the first written version of the protocol spec. It is derived from the current source tree, not from a separate design document. Where the source has multiple versions of a message (v0, v1, v2), all are listed; the wire format must support all of them.

The initial draft flagged several items with (verify) tags pending deeper investigation. Those have since been closed against the source. A "Re-verification on change" section at the end lists the areas where future code changes should be reflected here.

Stability

The wire protocol is frozen as of release 0.25.3.0 at the following surface:

  • Every ProtocolId currently assigned in hbs2-peer/lib/HBS2/Peer/Proto.hs keeps its number and payload shape. The full registry is in the "Protocol IDs" table further down this document.
  • The CBOR framing ((natVal, payload) two-element tuple, see "Foundations / Framing" below) does not change.
  • The encrypted overlay (HBS2.Net.Messaging.Encrypted.ByPass) and its handshake do not change.
  • The hand-rolled Serialise instance for GroupKey 'Symm (hbs2-core/lib/HBS2/Net/Auth/GroupKeySymm.hs:195) does not change.
  • Hash type, merkle tree layout, and reference (RefLog, RefChan, LWWRef) wire formats do not change.

Future wire-level features receive new ProtocolIds. Existing ProtocolIds do not get new payload versions. Peers running 0.25.3.0 interoperate with the prior 0.24.x line and will interoperate with all future releases that respect this freeze.

The one exception: 13001 (mailbox)

MailBoxProto (13001) may gain constructors at the end of MailBoxProtoMessage, and nothing else. It may not renumber, remove or change an existing constructor, and it may not change the shape of any payload an existing constructor carries.

This is a real exception to the paragraph above and is written down rather than taken quietly: SendMessageStamped and DeleteMessagesStamped were both added this way, and pretending otherwise would leave a re-implementer with a spec the source contradicts.

What makes it safe to allow here and nowhere else:

  • serialise tags every constructor by position, including the sole constructor of a one-constructor type, so appending leaves the bytes of every existing constructor byte-identical.
  • A peer that meets an unknown constructor fails to decode the packet and drops it silently (runProto), exactly as it does for an unknown ProtocolId. It does not relay it and does not act on it.
  • Both additions so far are proof-of-work witnesses. Nothing sends one unless a peer's hbs2:mailbox:pow-min is non-zero. It was zero by default until 0.26.0.0 and is 18 since (defRelayFloor), so from that release a peer that sets nothing sends stamped packets.

The consequence a re-implementer must plan for: a stamped packet travels only over a path of peers that understand it, and an older peer in the path is a black hole rather than a peer enforcing less. The mailbox feature accepted that cost deliberately (PEP-21, "The PoW deployment cost").

The RPC service APIs under hbs2-peer/lib/HBS2/Peer/RPC/API/ are NOT covered by this freeze and never were: they are Unix-socket-only, they carry a version constant of their own (e.g. MailboxAPIProto), and that constant is bumped whenever an existing method's input or output changes meaning so that a mismatched client refuses to connect instead of acting on misread bytes. Appending a method does not bump it, since an older peer answers ErrorMethodNotFound and a caller reads that as "no answer".

A break of this commitment requires a major version bump and a new section in this document. The "Re-verification on change" list at the end remains the canonical place to record what to check whenever a release does propose breaking wire compatibility.

Foundations

Encoding

All wire messages are encoded with CBOR via the serialise package. Every protocol type derives Serialise (directly or via Generic).

The CBOR encoding for each type follows serialise's default generic derivation rules. A non-Haskell implementation must match those rules; the serialise source documents them.

Two protocol types ship with a hand-rolled Serialise instance.

GroupKey 'Symm s in hbs2-core/lib/HBS2/Net/Auth/GroupKeySymm.hs:195 carries a forward-compatibility scheme: the encoder writes a GroupKeySymmV1 payload, then a version tag (currently 2), then extension data (group-key id scheme, id, and timestamp). The decoder reads V1, checks for remaining bytes, then optionally decodes the extension.

PeerData e (the PeerPong handshake payload) in hbs2-peer/lib/HBS2/Peer/Proto/Peer.hs uses the same trick to add reachableVia :: Set NetworkClass (PEP-05 PEX policy) without breaking older peers: the encoder writes the original two-field PeerDataV1 payload (sign key, peer nonce) verbatim, then appends the reachableVia set as a list. A decoder reads the two fields, checks for remaining bytes, and decodes the set if present, otherwise defaults to {Clearnet}. Old peers reading new PeerData ignore the trailing bytes; new peers reading old PeerData get the {Clearnet} default. Other implementations must match this layout.

Everything else uses default Generic derivation.

Hashing

The base hash type is Hash HbSync, defined in hbs2-core/lib/HBS2/Hash.hs.

  • Algorithm: Blake2b-256. Plain (no salt, no personalisation, no domain separation), applied to the raw byte string. The type alias is defined as HashType HbSync = Blake2b_256 (Hash.hs:39-40) and the Hashed HbSync ByteString instance computes hash bytes :: Digest Blake2b_256 from cryptonite's Crypto.Hash.
  • Wire representation: 32-byte ByteString.
  • Display form: base58 (used in URLs like hbs23://<base58>).

HashRef (in hbs2-core/lib/HBS2/Data/Types/Refs.hs) is a thin newtype around Hash HbSync used for references to merkle trees and other named hashes.

Asymmetric cryptography

Two key kinds, both backed by NaCl (libsodium) bindings via saltine:

  • Signing keys: Ed25519. Type tag 'Sign. Used for peer identity, refchan writers, reflog owners, LWWRef writers.
  • Encryption keys: Curve25519. Type tag 'Encrypt. Used for group key recipient lists.

Public keys appear in messages as raw byte strings of the appropriate length. Signatures are detached Ed25519 signatures (64 bytes).

Signed envelope

SignedBox p s in hbs2-core/lib/HBS2/Data/Types/SignedBox.hs is the standard signed-payload envelope. Logically:

SignedBox p s = (PubKey 'Sign s, ByteString, Signature s)

where ByteString is the CBOR-encoded payload of type p, and the signature is over that byte string. Verification checks the signature against the public key.

This envelope is used everywhere a peer publishes something that others must authenticate: reflog updates, refchan proposals, refchan accepts, LWWRef updates, peer handshake responses.

Encrypted envelope

EncryptedBox t in hbs2-core/lib/HBS2/Data/Types/EncryptedBox.hs is an opaque encrypted byte string. The decryption procedure depends on the surrounding context (group key kind, recipient identity).

Merkle trees

Large objects are represented as merkle trees. The wire shape is in hbs2-core/lib/HBS2/Merkle.hs.

A MTree a is a recursive structure:

  • MNode MNodeData [Hash HbSync] for an internal node, holding metadata and the hashes of its children.
  • MLeaf a for a leaf, carrying a payload of type a.

MTreeAnn a annotates a merkle tree with metadata and an encryption descriptor:

MTreeAnn
  { _mtaMeta  :: AnnMetaData
  , _mtaCrypt :: MTreeEncryption
  , _mtaTree  :: MTree a
  }

AnnMetaData carries optional application-defined metadata (a short text blob or a reference to another block).

MTreeEncryption records how the tree contents are encrypted (see Encryption):

  • NullEncryption
  • CryptAccessKeyNaClAsymm (asymmetric group key)
  • EncryptGroupNaClSymm1 (symmetric group key, v1)
  • EncryptGroupNaClSymm2 (symmetric group key, v2 with options)

Protocols

Each protocol is implemented as a module in hbs2-peer/lib/HBS2/Peer/Proto/. Each module exports one or more sum types whose constructors are the wire messages.

The protocol stack runs on top of a generic messaging layer that multiplexes by protocol identifier. The identifier is a Nat assigned to each protocol's HasProtocol instance (Net/Proto/Types.hs:145-150).

On the wire, each message is framed as a two-element CBOR tuple:

serialise (protoId :: Integer, payload :: ByteString)

The first element is the protocol's Nat ID promoted to Integer; the second is the CBOR-encoded protocol-specific payload, treated as opaque bytes at the framing layer. This is the encoding used by the Messaging (Fabriq e) e (AnyMessage ByteString e) instance in Actors/Peer.hs:64-71. On receive, the framing layer decodes the tuple, looks up a handler for the ID, and feeds the payload bytes to that handler's decoder.

Protocol IDs are assigned per (protocol type, transport) pair. The authoritative registry is hbs2-peer/lib/HBS2/Peer/Proto.hs. Current assignments for L4Proto (UDP/TCP peers) are:

ID Protocol
1 BlockInfo
2 BlockChunks
3 BlockAnnounce
4 PeerHandshake
5 PeerAnnounce
6 PeerExchange
7 RefLogUpdate
8 RefLogRequest
9 PeerMetaProto
11001 RefChanHead
11002 RefChanUpdate
11003 RefChanRequest
11004 RefChanNotify
12001 LWWRefProto
13001 MailBoxProto HBS2Basic

The mailbox protocol (13001) is the wire layer behind the mailbox feature, which hbs2-peer mailbox and hbs2-hub both drive. Its payload is MailBoxProtoV1 wrapping one of:

tag constructor carries
0 SendMessage a Message
1 CheckMailbox an optional nonce and a mailbox key
2 MailboxStatus a peer-signed MailBoxStatusPayload
3 DeleteMessages an owner-signed DeleteMessagesPayload
4 SendMessageStamped a Message and a MessageStamp
5 DeleteMessagesStamped a delete box and a MessageStamp

Tags 4 and 5 are the append-only extension described under Stability. A MessageStamp is (mailbox key, nonce :: Word64), unsigned and self-verifying: the work is the leading zero bits of blake2b-256(CBOR (mailbox key, target hash, nonce)), where the target hash is that of the serialised Message for tag 4 and of the serialised signed delete box for tag 5.

Several additional IDs in the same registry cover Unix-socket-only protocols (notify channels, validators, the RPC service IDs); those are not part of the peer-to-peer wire protocol and a non-Haskell peer implementation can ignore them. Examples: RefChanValidate UNIX = 0xFFFA0001, RefChanNotify UNIX = 0xFFFB0001, the notify-protocol IDs in Notify.hs, and the service-RPC IDs in hbs2-peer/lib/HBS2/Peer/RPC/API/.

If the encrypted transport overlay is enabled, the two-element CBOR tuple above is the cleartext that gets sealed by ByPass.hs (see Encryption) before going on the wire.

Peer handshake

File: Peer.hs. Establishes that two peers can reach each other and that each holds the private key it claims.

data PeerHandshake e
  = PeerPing PingNonce
  | PeerPong PingNonce (Signature (Encryption e)) (PeerData e)

PeerPing carries a random nonce. The recipient responds with PeerPong containing the same nonce, a signature over it (proving ownership of the peer's signing key), and the peer's identity data (PeerData: signing key plus the peer's own nonce).

Peer discovery

PeerAnnounce

File: PeerAnnounce.hs. Multicast advertisement of presence.

data PeerAnnounce e = PeerAnnounce PeerNonce

Sent over UDP multicast on the LAN. Receivers respond by initiating a handshake.

PeerExchange (PEX)

File: PeerExchange.hs. Peer address swap; two protocol versions.

data PeerExchange e
  = PeerExchangeGet    (Nonce (PeerExchange e))
  | PeerExchangePeers  (Nonce (PeerExchange e)) [IPAddrPort e]
  | PeerExchangeGet2   (Nonce (PeerExchange e))
  | PeerExchangePeers2 (Nonce (PeerExchange e)) [PeerAddr e]

Get requests a peer list; Peers returns it. The v2 variant returns structured peer addresses (PeerAddr) that carry more than just IP and port.

PeerMeta

File: PeerMeta.hs. Capability and version exchange.

data PeerMetaProto e
  = GetPeerMeta
  | ThePeerMeta AnnMetaData

Reuses AnnMetaData from the merkle-tree metadata machinery.

Block plane

The block plane is the data-replication layer. Three protocols.

BlockInfo

File: BlockInfo.hs. Block-size query.

data BlockInfo e
  = GetBlockSize (Hash HbSync)
  | NoBlock      (Hash HbSync)
  | BlockSize    (Hash HbSync) Integer

A peer asks whether another peer holds a block and how large it is.

BlockAnnounce

File: BlockAnnounce.hs. Block-availability gossip.

data BlockInfoMeta
  = NoBlockInfoMeta
  | BlockInfoMetaShort ByteString
  | BlockInfoMetaRef   (Hash HbSync)

data BlockAnnounceInfo e = BlockAnnounceInfo
  { _biNonce :: BlockInfoNonce  -- Word64
  , _biMeta  :: BlockInfoMeta
  , _biSize  :: Integer
  , _biHash  :: Hash HbSync
  }

data BlockAnnounce e
  = BlockAnnounce PeerNonce (BlockAnnounceInfo e)

Sent by a peer when it acquires a new block, so that interested peers know to fetch it.

BlockChunks

File: BlockChunks.hs. Reliable block transfer.

data BlockChunksProto e
  = BlockGetAllChunks (Hash HbSync) ChunkSize
  | BlockGetChunks    (Hash HbSync) ChunkSize Word32 Word32
  | BlockNoChunks
  | BlockChunk        ChunkNum ByteString
  | BlockLost

data BlockChunks e = BlockChunks (Cookie e) (BlockChunksProto e)

A block is fetched in chunks of ChunkSize bytes. The cookie correlates request and response messages on the same transport.

Reference plane

The reference plane builds mutable named handles on top of the immutable block plane. Three flavors with very different semantics.

RefLog

File: RefLog.hs. Append-only signed log; one writer, many readers.

data RefLogRequest e
  = RefLogRequest  { refLog      :: PubKey 'Sign (Encryption e) }
  | RefLogResponse { refLog      :: PubKey 'Sign (Encryption e)
                   , refLogValue :: Hash HbSync
                   }

data RefLogUpdate e = RefLogUpdate
  { _refLogId       :: PubKey 'Sign (Encryption e)
  , _refLogUpdNonce :: Nonce (RefLogUpdate e)
  , _refLogUpdData  :: ByteString
  , _refLogUpdSign  :: Signature (Encryption e)
  }

An update is identified by the public signing key (_refLogId), carries an opaque payload (_refLogUpdData), a per-update nonce, and a signature over the nonce and data. Anyone with the public key can verify; only the holder of the matching private key can produce valid updates. Used by hbs2-git.

RefChan

Files: RefChan.hs, RefChan/*.hs. Quorum-based reference channel.

The channel is configured by a head block that names voting peers, authors, readers, and parameters. There are three head-block versions on the wire:

data RefChanHeadBlock e
  = RefChanHeadBlockSmall
      { _refChanHeadVersion    :: Integer
      , _refChanHeadQuorum     :: Integer
      , _refChanHeadWaitAccept :: Integer
      , _refChanHeadPeers      :: HashMap (PubKey 'Sign s) Weight
      , _refChanHeadAuthors    :: HashSet (PubKey 'Sign s)
      }
  | RefChanHeadBlock1   { ... , _refChanHeadReaders'   :: HashSet (PubKey 'Encrypt s) }
  | RefChanHeadBlock2   { ... , _refChanHeadNotifiers' :: HashSet (PubKey 'Sign s) }

v0 covers writers, voters, weights, and quorum. v1 adds an explicit list of reader (encryption) keys for encrypted channels. v2 adds notifier keys that can receive notifications without being voters.

Update flow is a two-phase commit:

data RefChanUpdate e
  = Propose (RefChanId e) (SignedBox (ProposeTran e) (Encryption e))
  | Accept  (RefChanId e) (SignedBox (AcceptTran  e) (Encryption e))

data ProposeTran e
  = ProposeTran HashRef (SignedBox ByteString (Encryption e))

data AcceptTran e
  = AcceptTran1                  HashRef HashRef
  | AcceptTran2 (Maybe AcceptTime) HashRef HashRef

An author broadcasts Propose carrying a signed payload. Voting peers verify and broadcast Accept referencing the proposal hash and the head hash. When the quorum threshold is met within _refChanHeadWaitAccept seconds, the proposal is considered applied.

AcceptTran has two versions: v1 carries proposal hash and head hash, v2 adds an optional timestamp.

Channels also support notifications:

data RefChanNotify e
  = Notify        (RefChanId e) (SignedBox ByteString (Encryption e))
  | ActionRequest (RefChanId e) RefChanActionRequest

Notify broadcasts an application-defined signed payload to subscribers. ActionRequest is a control message with exactly two constructors (RefChan/Types.hs:87-90):

data RefChanActionRequest
  = RefChanAnnounceBlock HashRef
  | RefChanFetch         HashRef

RefChanAnnounceBlock asks subscribers to (re-)announce a block; RefChanFetch asks them to fetch a block they may be missing.

LWWRef

File: LWWRef.hs. Lightweight last-writer-wins register.

data LWWRef s = LWWRef
  { lwwSeq   :: Word64
  , lwwValue :: HashRef
  , lwwProof :: Maybe HashRef
  }

data LWWRefProtoReq s
  = LWWProtoGet (LWWRefKey s)
  | LWWProtoSet (LWWRefKey s) (SignedBox (LWWRef s) s)

newtype LWWRefKey s = LWWRefKey { fromLwwRefKey :: PubKey 'Sign s }

The reference identity is the signing key; the value is a hash plus a monotonic sequence number. A higher lwwSeq wins on merge. Used for cheap pointer-to-the-real-thing where a full reflog or refchan would be overkill.

AnyRef

File: AnyRef.hs. Polymorphic reference key wrapper used in generic ref operations.

newtype AnyRefKey t s = AnyRefKey (PubKey 'Sign s)

The type tag t is a phantom type that exists only at the Haskell type level. There is no explicit Serialise instance, so the newtype encodes identically to its inner PubKey 'Sign s; the tag does not appear on the wire and cannot be recovered from a wire message alone. Consuming code knows the tag from context (which RPC endpoint or which protocol message carried the key).

The tag does, however, affect content addressing: the Hashed HbSync (AnyRefKey t s) instance prefixes the serialised pubkey with the literal byte string anyref| before hashing (AnyRef.hs:23-24), so the content hash of an AnyRefKey differs from the content hash of the bare pubkey. The tag still does not leak through, since the prefix is constant across all tag values.

Encryption

Two group-key schemes are supported.

Asymmetric group key

File: hbs2-core/lib/HBS2/Net/Auth/GroupKeyAsymm.hs.

data AccessKey s = AccessKeyNaClAsymm
  { permitted :: [(PubKey 'Encrypt s, EncryptedBox (KeyringEntry s))]
  }

data GroupKey 'Asymm s = GroupKeyNaClAsymm
  { recipientPk :: PubKey 'Encrypt s
  , accessKey   :: AccessKey s
  }

A group key consists of a list of (recipient public key, payload encrypted to that recipient). Each recipient decrypts the entry addressed to them and recovers a KeyringEntry which holds the actual signing/encryption keys for the group. Used for cases where the recipient set is small and explicit.

Symmetric group key

File: hbs2-core/lib/HBS2/Net/Auth/GroupKeySymm.hs.

A random symmetric secret is generated and encrypted under each authorized reader's public encryption key. Readers decrypt the secret and use it to decrypt the data. Wraps a MTreeAnn payload whose _mtaCrypt field indicates EncryptGroupNaClSymm1 or EncryptGroupNaClSymm2.

The symmetric primitive is NaCl crypto_secretbox (XSalsa20-Poly1305), invoked via saltine's Crypto.Saltine.Core.SecretBox.secretbox / secretboxOpen in GroupKeySymm.hs. The EncryptGroupNaClSymm2 variant additionally derives per-block keys via SipHash-based block indexing (EncryptGroupNaClSymmBlockSIP) so that different blocks of the same tree are encrypted under different nonces and keys.

Transport-level encryption

The messaging layer at hbs2-core/lib/HBS2/Net/Messaging/Encrypted/ wraps any transport (UDP, TCP, Unix) with peer-to-peer encryption. Two pieces:

  • ByPass.hs uses NaCl crypto_box (X25519 ECDH + XSalsa20 + Poly1305) via saltine's Crypto.Saltine.Core.Box. Each pair of peers precomputes a shared key (CombinedKey) once and then uses it to seal and open messages. Includes SipHash-based keyed indexing of trusted peers.
  • RandomPrefix.hs is a small bytecode-driven obfuscation layer prepended to encrypted packets. The "bytecode" is a sequence of primitive operations (NOP, LOADB, SKIPBI, ANDBI, ORBI, XORBI, ADDBI, SUBBI, MULTBI, REPEAT, RET) interpreted to produce a per-packet variable-length prefix. The goal is to hinder naive traffic analysis and protocol fingerprinting.

This is layered below the protocol stack and is independent of the group-key schemes used at the data layer.

Auxiliary types

PeerNonce is a 64-bit unsigned integer used to identify a peer session. Cookie e is a transport-level identifier (typically 32 bits) used to correlate request and response messages on the same connection. Nonce types specific to a protocol family carry their own structure (see each protocol's definition).

What is intentionally out of scope here

  • RPC over Unix socket between hbs2-peer and local clients. That is a service contract internal to a single machine, not a wire protocol between peers. See the hbs2-peer/lib/HBS2/Peer/RPC/API/ modules.
  • Storage on-disk format (NCQ3). Persistence layout, not wire format.
  • Git remote helper protocol. The interaction between git and git-remote-hbs23 is git's documented remote-helper protocol, not an hbs2 invention.

Re-verification on change

All items initially flagged as needing verification in the first draft of this document have been closed against the current source. Future maintainers should re-verify the following whenever the related code changes:

  • The set of hand-rolled Serialise instances (only GroupKey 'Symm s today; new ones may break interoperability with previously shipped peers).
  • The ProtocolId registry in hbs2-peer/lib/HBS2/Peer/Proto.hs. Adding a protocol means adding a row to the table above.
  • The frame layout in HBS2.Actors.Peer's Messaging instance: any change there breaks interop with every existing peer.
  • The cryptographic primitives used by the symmetric group key, the asymmetric group key, and the transport-level overlay. These are load-bearing for security and should not change silently.