wire.go raw
1 // Package wire is the fixed message vocabulary shared by torly's domains.
2 //
3 // It contains only value types and constants: no mutable package state, no
4 // behavior, and no channels. Domains exchange these values over the channels
5 // bound when they were spawned. Slices, maps and the socket capability in a
6 // message are handed over with ownership: the sender must not touch them after
7 // sending, and the receiver becomes their only owner.
8 package wire
9
10 import (
11 "net"
12
13 "git.smesh.lol/torly/pkg/nostr"
14 )
15
16 // ConnID identifies one client connection within a relay process.
17 type ConnID uint64
18
19 // ReqID correlates an asynchronous request with its response.
20 type ReqID uint64
21
22 // Ready reports the outcome of a domain's startup work.
23 type Ready struct {
24 OK bool
25 Err string
26 Addr string
27 }
28
29 // Socket is an accepted connection capability. Ownership moves from the
30 // acceptor to a reader worker, which shares the read/write capability with its
31 // paired writer domain. Only the writer closes it.
32 type Socket struct {
33 Conn net.Conn
34 }
35
36 // Assign hands an accepted socket to a reader worker.
37 type Assign struct {
38 ID ConnID
39 Socket *Socket
40 }
41
42 // Inbound kinds, reader worker -> network.
43 const (
44 InOpened uint8 = iota
45 InText
46 InHTTP
47 InClosed
48 )
49
50 // HTTPRequest is a complete HTTP request parsed by a reader worker.
51 type HTTPRequest struct {
52 Method string
53 Path string
54 Query string
55 Header map[string]string
56 Body []byte
57 }
58
59 // Inbound is one event from a reader worker.
60 type Inbound struct {
61 Conn ConnID
62 Kind uint8
63 Text []byte
64 HTTP HTTPRequest
65 Reason string
66 }
67
68 // Outbound kinds, network -> writer worker.
69 const (
70 OutText uint8 = iota
71 OutHTTP
72 OutClose
73 OutPing
74 )
75
76 // HTTPResponse is a complete HTTP response for a writer to serialize.
77 type HTTPResponse struct {
78 Status int
79 Header [][2]string
80 Body []byte
81 }
82
83 // Outbound is one action for a writer worker.
84 type Outbound struct {
85 Conn ConnID
86 Kind uint8
87 Text []byte
88 HTTP HTTPResponse
89 Code uint16
90 Reason string
91 }
92
93 // Writer control kinds, reader worker -> writer worker.
94 const (
95 CtlBind uint8 = iota
96 CtlUpgrade
97 CtlPong
98 CtlClose
99 CtlWrite
100 CtlRespond
101 )
102
103 // WriterCtl is a control message from a reader worker to its writer.
104 type WriterCtl struct {
105 Kind uint8
106 Conn ConnID
107 Socket *Socket
108 Payload []byte
109 Close bool
110 }
111
112 // DB request kinds.
113 const (
114 DBQuery uint8 = iota
115 DBSave
116 DBCount
117 DBGraph
118 )
119
120 // Result codes carried instead of Go error values across a domain boundary.
121 const (
122 ResOK uint8 = iota
123 ResDuplicate
124 ResReplaced
125 ResDeleted
126 ResError
127 )
128
129 // DBRequest asks the database domain for work.
130 type DBRequest struct {
131 ID ReqID
132 Kind uint8
133 Event nostr.Event
134 Filters []nostr.Filter
135 Graph nostr.GraphQuery
136 Limit int
137 }
138
139 // DBResponse is the database domain's answer.
140 type DBResponse struct {
141 ID ReqID
142 Kind uint8
143 Events []nostr.Event
144 Graph *nostr.GraphResult
145 Count int
146 Approximate bool
147 Stored bool
148 Code uint8
149 Err string
150 }
151
152 // VerifyRequest asks a signature worker to check one BIP-340 signature.
153 type VerifyRequest struct {
154 ID ReqID
155 Pub [32]byte
156 Msg [32]byte
157 Sig [64]byte
158 }
159
160 // VerifyResponse is a signature worker's answer.
161 type VerifyResponse struct {
162 ID ReqID
163 OK bool
164 }
165
166 // BlobRequest is one HTTP request for the Blossom domain.
167 type BlobRequest struct {
168 ID ReqID
169 Method string
170 Path string
171 Query string
172 Header map[string]string
173 Body []byte
174 }
175
176 // BlobResponse is the Blossom domain's answer. Handled is false for paths the
177 // Blossom server does not own.
178 type BlobResponse struct {
179 ID ReqID
180 Handled bool
181 Status int
182 Header [][2]string
183 Body []byte
184 }
185