network

package
v0.0.0-...-bb0dc25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Overview

Package network implements the CROM P2P networking layer using libp2p.

The network is sovereign: only peers that share the same Codebook BuildHash can communicate. This is enforced at the protocol level via a handshake on /crom/auth/1.0.

Index

Constants

View Source
const (
	// AuthProtocol is the protocol ID for the sovereignty handshake.
	AuthProtocol = "/crom/auth/2.0"

	// AuthTimeout is the maximum time allowed for the handshake.
	AuthTimeout = 10 * time.Second
)
View Source
const (
	MsgSyncReq   byte = 0x01 // Request a manifest for a specific original file hash (or filename)
	MsgManifest  byte = 0x02 // The serialized ChunkManifest
	MsgDiffReq   byte = 0x03 // Request missing chunks (payload is array of indices)
	MsgChunkData byte = 0x04 // Raw delta payload
	MsgError     byte = 0xFF // Error message
)

Message Types

View Source
const (
	// SyncProtocolID is the identifier for the CROM manifest exchange protocol.
	SyncProtocolID = "/crom/sync/1.0"
)

Variables

View Source
var DefaultBootstrapPeers = []string{
	"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
	"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
	"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
	"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
}

DefaultBootstrapPeers returns the default set of IPFS bootstrap peers. In a real private network, these would be dedicated bootstrap nodes for CROM.

Functions

func GenerateIdentity

func GenerateIdentity() error

GenerateIdentity creates a new Ed25519 keypair for libp2p

func IsPeerTrusted

func IsPeerTrusted(peerIDStr string) bool

IsPeerTrusted checks if a peer is in the Web of Trust

func LoadIdentity

func LoadIdentity() (crypto.PrivKey, error)

LoadIdentity loads the libp2p private key from disk

func ReceiveChunks

func ReceiveChunks(tempPath string, outPath string, manifest *cromsync.ChunkManifest, missingIndices []uint32, s network.Stream, codebookPath string, encryptionKey string) (int64, error)

ReceiveChunks reads streamed deltas and rebuilds the .crom file, taking the chunks it already has from the local version. It returns the bytes read from the network.

func StreamChunks

func StreamChunks(localPath, codebookPath, encryptionKey string, indices []uint32, s network.Stream) error

StreamChunks extracts the uncompressed XOR delta for each requested index from the local .crom file and sends it over the libp2p stream.

func TrustPeer

func TrustPeer(peerIDStr string) error

TrustPeer adds a peer ID to the Web of Trust

Types

type AnnounceMsg

type AnnounceMsg struct {
	Type         string `json:"type"`          // "NEW_FILE" or "CODEBOOK_UPDATE"
	Filename     string `json:"filename"`      // Basename of the .crom file
	OriginalSize uint64 `json:"original_size"` // Size of the original uncompressed file
	ChunkCount   uint32 `json:"chunk_count"`   // Total chunks
	Sender       string `json:"sender"`        // Peer ID of the announcer
}

AnnounceMsg represents a GossipSub message announcing a new or updated file.

type CromFECEngine

type CromFECEngine struct {
	DataShards   int
	ParityShards int
}

CromFECEngine provides Forward Error Correction using Reed-Solomon style mathematical matrices. This layer shields Kademlia Bitswap in LEO-Satellite or 4G Android connections: Missing P2P TCP chunks are RECONSTRUCTED algebraically instead of draining radio battery by re-asking peers.

func NewFECEngine

func NewFECEngine(dataShards, parityShards int) *CromFECEngine

NewFECEngine launches the Erasure Coding mathematical grid protector.

func (*CromFECEngine) Encode

func (fec *CromFECEngine) Encode(chunk []byte) ([][]byte, [][]byte, error)

Encode generates mathematical parity shards (Polynomials) for a given raw chunk payload.

func (*CromFECEngine) Reconstruct

func (fec *CromFECEngine) Reconstruct(shards [][]byte) ([]byte, error)

Reconstruct validates and mathematically reconstructs missing data shards utilizing active Parity Shards via Vandermonde matrix parsing.

type CromNode

type CromNode struct {
	Host         host.Host
	DHT          *dht.IpfsDHT
	PubSub       *pubsub.PubSub
	CodebookHash [32]byte // SHA-256 BuildHash — defines the network partition
	DataDir      string   // Directory containing local .crom files
	EncKey       string   // AES passphrase for encrypted files
	CodebookPath string   // Path to the .cromdb file
	// contains filtered or unexported fields
}

CromNode is the main P2P node for the CROM network.

func NewCromNode

func NewCromNode(codebookPath string, listenPort int, dataDir string, encKey string) (*CromNode, error)

NewCromNode creates and starts a libp2p host bound to the given codebook. The node identity (Ed25519 keypair) is persisted in dataDir/peer.key.

func (*CromNode) Addrs

func (n *CromNode) Addrs() []string

Addrs returns the multiaddrs this node is listening on.

func (*CromNode) AnnounceFile

func (n *CromNode) AnnounceFile(ctx context.Context, filename string, originalSize uint64, chunkCount uint32) error

AnnounceFile publishes a NEW_FILE message to the network.

func (*CromNode) AuthenticatePeer

func (n *CromNode) AuthenticatePeer(ctx context.Context, pid peer.ID) error

AuthenticatePeer initiates the handshake with a remote peer. Must be called after establishing a connection before any other protocol.

func (*CromNode) Context

func (n *CromNode) Context() context.Context

Context returns the node's context.

func (*CromNode) IsAuthenticated

func (n *CromNode) IsAuthenticated(pid peer.ID) bool

IsAuthenticated reports whether the peer proved it holds the same brain.

func (*CromNode) PeerID

func (n *CromNode) PeerID() peer.ID

PeerID returns this node's peer ID as a string.

func (*CromNode) ProposeUniversalPattern

func (n *CromNode) ProposeUniversalPattern(ctx context.Context, hash string, payload []byte, weight uint32, signature []byte) error

ProposeUniversalPattern publishes a PROPOSE_CHUNK message to federate learning.

func (*CromNode) SetupDHT

func (n *CromNode) SetupDHT(bootstrapAddrs []string, private bool) error

SetupDHT initializes the Kademlia Distributed Hash Table for WAN discovery. A private DHT (own protocol prefix) also accepts peers on private addresses.

func (*CromNode) Stop

func (n *CromNode) Stop() error

Stop gracefully shuts down the node.

type GossipManager

type GossipManager struct {
	// contains filtered or unexported fields
}

GossipManager handles pubsub operations for the node.

type Identity

type Identity struct {
	PrivKeyBytes []byte `json:"privKey"`
	PubKeyBytes  []byte `json:"pubKey"`
	PeerID       string `json:"peerID"`
}

Identity struct stores the keys locally

type ProposeChunkMsg

type ProposeChunkMsg struct {
	Type      string `json:"type"`      // "PROPOSE_CHUNK"
	Hash      string `json:"hash"`      // Hash of the chunk
	Payload   []byte `json:"payload"`   // Raw chunk data
	Weight    uint32 `json:"weight"`    // Recurrency score
	Signature []byte `json:"signature"` // Ed25519 signature of the sender
	Sender    string `json:"sender"`    // Peer ID
}

ProposeChunkMsg represents a federated learning proposal for a universal chunk.

type SyncProtocol

type SyncProtocol struct {

	// BytesReceived counts what the last RequestSync read from the network.
	BytesReceived int64
	// contains filtered or unexported fields
}

SyncProtocol handles manifest exchange and chunk transfers.

func NewSyncProtocol

func NewSyncProtocol(node *CromNode) *SyncProtocol

NewSyncProtocol registers the sync stream handler.

func (*SyncProtocol) RequestSync

func (p *SyncProtocol) RequestSync(ctx context.Context, pid peer.ID, filename string) error

RequestSync is called proactively by a node to download a file from a remote peer. Flow: 0. Codebook Handshake (hash exchange, download if mismatch) 1. Sends SYNC_REQ 2. Receives MANIFEST 3. Diff against local (if file exists) or request all chunks 4. Send DIFF_REQ with missing indices 5. Receive CHUNK_DATA stream and rebuild

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址