// Package wire is the fixed message vocabulary shared by torly's domains. // // It contains only value types and constants: no mutable package state, no // behavior, and no channels. Domains exchange these values over the channels // bound when they were spawned. Slices, maps and the socket capability in a // message are handed over with ownership: the sender must not touch them after // sending, and the receiver becomes their only owner. package wire import ( "net" "git.smesh.lol/torly/pkg/nostr" ) // ConnID identifies one client connection within a relay process. type ConnID uint64 // ReqID correlates an asynchronous request with its response. type ReqID uint64 // Ready reports the outcome of a domain's startup work. type Ready struct { OK bool Err string Addr string } // Socket is an accepted connection capability. Ownership moves from the // acceptor to a reader worker, which shares the read/write capability with its // paired writer domain. Only the writer closes it. type Socket struct { Conn net.Conn } // Assign hands an accepted socket to a reader worker. type Assign struct { ID ConnID Socket *Socket } // Inbound kinds, reader worker -> network. const ( InOpened uint8 = iota InText InHTTP InClosed ) // HTTPRequest is a complete HTTP request parsed by a reader worker. type HTTPRequest struct { Method string Path string Query string Header map[string]string Body []byte } // Inbound is one event from a reader worker. type Inbound struct { Conn ConnID Kind uint8 Text []byte HTTP HTTPRequest Reason string } // Outbound kinds, network -> writer worker. const ( OutText uint8 = iota OutHTTP OutClose OutPing ) // HTTPResponse is a complete HTTP response for a writer to serialize. type HTTPResponse struct { Status int Header [][2]string Body []byte } // Outbound is one action for a writer worker. type Outbound struct { Conn ConnID Kind uint8 Text []byte HTTP HTTPResponse Code uint16 Reason string } // Writer control kinds, reader worker -> writer worker. const ( CtlBind uint8 = iota CtlUpgrade CtlPong CtlClose CtlWrite CtlRespond ) // WriterCtl is a control message from a reader worker to its writer. type WriterCtl struct { Kind uint8 Conn ConnID Socket *Socket Payload []byte Close bool } // DB request kinds. const ( DBQuery uint8 = iota DBSave DBCount DBGraph ) // Result codes carried instead of Go error values across a domain boundary. const ( ResOK uint8 = iota ResDuplicate ResReplaced ResDeleted ResError ) // DBRequest asks the database domain for work. type DBRequest struct { ID ReqID Kind uint8 Event nostr.Event Filters []nostr.Filter Graph nostr.GraphQuery Limit int } // DBResponse is the database domain's answer. type DBResponse struct { ID ReqID Kind uint8 Events []nostr.Event Graph *nostr.GraphResult Count int Approximate bool Stored bool Code uint8 Err string } // VerifyRequest asks a signature worker to check one BIP-340 signature. type VerifyRequest struct { ID ReqID Pub [32]byte Msg [32]byte Sig [64]byte } // VerifyResponse is a signature worker's answer. type VerifyResponse struct { ID ReqID OK bool } // BlobRequest is one HTTP request for the Blossom domain. type BlobRequest struct { ID ReqID Method string Path string Query string Header map[string]string Body []byte } // BlobResponse is the Blossom domain's answer. Handled is false for paths the // Blossom server does not own. type BlobResponse struct { ID ReqID Handled bool Status int Header [][2]string Body []byte }