annotate osx/include/capnp/rpc.capnp @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 0994c39f1e94
children
rev   line source
cannam@62 1 # Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@62 2 # Licensed under the MIT License:
cannam@62 3 #
cannam@62 4 # Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@62 5 # of this software and associated documentation files (the "Software"), to deal
cannam@62 6 # in the Software without restriction, including without limitation the rights
cannam@62 7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@62 8 # copies of the Software, and to permit persons to whom the Software is
cannam@62 9 # furnished to do so, subject to the following conditions:
cannam@62 10 #
cannam@62 11 # The above copyright notice and this permission notice shall be included in
cannam@62 12 # all copies or substantial portions of the Software.
cannam@62 13 #
cannam@62 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@62 15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@62 16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@62 17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@62 18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@62 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@62 20 # THE SOFTWARE.
cannam@62 21
cannam@62 22 @0xb312981b2552a250;
cannam@62 23 # Recall that Cap'n Proto RPC allows messages to contain references to remote objects that
cannam@62 24 # implement interfaces. These references are called "capabilities", because they both designate
cannam@62 25 # the remote object to use and confer permission to use it.
cannam@62 26 #
cannam@62 27 # Recall also that Cap'n Proto RPC has the feature that when a method call itself returns a
cannam@62 28 # capability, the caller can begin calling methods on that capability _before the first call has
cannam@62 29 # returned_. The caller essentially sends a message saying "Hey server, as soon as you finish
cannam@62 30 # that previous call, do this with the result!". Cap'n Proto's RPC protocol makes this possible.
cannam@62 31 #
cannam@62 32 # The protocol is significantly more complicated than most RPC protocols. However, this is
cannam@62 33 # implementation complexity that underlies an easy-to-grasp higher-level model of object oriented
cannam@62 34 # programming. That is, just like TCP is a surprisingly complicated protocol that implements a
cannam@62 35 # conceptually-simple byte stream abstraction, Cap'n Proto is a surprisingly complicated protocol
cannam@62 36 # that implements a conceptually-simple object abstraction.
cannam@62 37 #
cannam@62 38 # Cap'n Proto RPC is based heavily on CapTP, the object-capability protocol used by the E
cannam@62 39 # programming language:
cannam@62 40 # http://www.erights.org/elib/distrib/captp/index.html
cannam@62 41 #
cannam@62 42 # Cap'n Proto RPC takes place between "vats". A vat hosts some set of objects and talks to other
cannam@62 43 # vats through direct bilateral connections. Typically, there is a 1:1 correspondence between vats
cannam@62 44 # and processes (in the unix sense of the word), although this is not strictly always true (one
cannam@62 45 # process could run multiple vats, or a distributed virtual vat might live across many processes).
cannam@62 46 #
cannam@62 47 # Cap'n Proto does not distinguish between "clients" and "servers" -- this is up to the application.
cannam@62 48 # Either end of any connection can potentially hold capabilities pointing to the other end, and
cannam@62 49 # can call methods on those capabilities. In the doc comments below, we use the words "sender"
cannam@62 50 # and "receiver". These refer to the sender and receiver of an instance of the struct or field
cannam@62 51 # being documented. Sometimes we refer to a "third-party" that is neither the sender nor the
cannam@62 52 # receiver. Documentation is generally written from the point of view of the sender.
cannam@62 53 #
cannam@62 54 # It is generally up to the vat network implementation to securely verify that connections are made
cannam@62 55 # to the intended vat as well as to encrypt transmitted data for privacy and integrity. See the
cannam@62 56 # `VatNetwork` example interface near the end of this file.
cannam@62 57 #
cannam@62 58 # When a new connection is formed, the only interesting things that can be done are to send a
cannam@62 59 # `Bootstrap` (level 0) or `Accept` (level 3) message.
cannam@62 60 #
cannam@62 61 # Unless otherwise specified, messages must be delivered to the receiving application in the same
cannam@62 62 # order in which they were initiated by the sending application. The goal is to support "E-Order",
cannam@62 63 # which states that two calls made on the same reference must be delivered in the order which they
cannam@62 64 # were made:
cannam@62 65 # http://erights.org/elib/concurrency/partial-order.html
cannam@62 66 #
cannam@62 67 # Since the full protocol is complicated, we define multiple levels of support that an
cannam@62 68 # implementation may target. For many applications, level 1 support will be sufficient.
cannam@62 69 # Comments in this file indicate which level requires the corresponding feature to be
cannam@62 70 # implemented.
cannam@62 71 #
cannam@62 72 # * **Level 0:** The implementation does not support object references. Only the bootstrap interface
cannam@62 73 # can be called. At this level, the implementation does not support object-oriented protocols and
cannam@62 74 # is similar in complexity to JSON-RPC or Protobuf services. This level should be considered only
cannam@62 75 # a temporary stepping-stone toward level 1 as the lack of object references drastically changes
cannam@62 76 # how protocols are designed. Applications _should not_ attempt to design their protocols around
cannam@62 77 # the limitations of level 0 implementations.
cannam@62 78 #
cannam@62 79 # * **Level 1:** The implementation supports simple bilateral interaction with object references
cannam@62 80 # and promise pipelining, but interactions between three or more parties are supported only via
cannam@62 81 # proxying of objects. E.g. if Alice (in Vat A) wants to send Bob (in Vat B) a capability
cannam@62 82 # pointing to Carol (in Vat C), Alice must create a proxy of Carol within Vat A and send Bob a
cannam@62 83 # reference to that; Bob cannot form a direct connection to Carol. Level 1 implementations do
cannam@62 84 # not support checking if two capabilities received from different vats actually point to the
cannam@62 85 # same object ("join"), although they should be able to do this check on capabilities received
cannam@62 86 # from the same vat.
cannam@62 87 #
cannam@62 88 # * **Level 2:** The implementation supports saving persistent capabilities -- i.e. capabilities
cannam@62 89 # that remain valid even after disconnect, and can be restored on a future connection. When a
cannam@62 90 # capability is saved, the requester receives a `SturdyRef`, which is a token that can be used
cannam@62 91 # to restore the capability later.
cannam@62 92 #
cannam@62 93 # * **Level 3:** The implementation supports three-way interactions. That is, if Alice (in Vat A)
cannam@62 94 # sends Bob (in Vat B) a capability pointing to Carol (in Vat C), then Vat B will automatically
cannam@62 95 # form a direct connection to Vat C rather than have requests be proxied through Vat A.
cannam@62 96 #
cannam@62 97 # * **Level 4:** The entire protocol is implemented, including joins (checking if two capabilities
cannam@62 98 # are equivalent).
cannam@62 99 #
cannam@62 100 # Note that an implementation must also support specific networks (transports), as described in
cannam@62 101 # the "Network-specific Parameters" section below. An implementation might have different levels
cannam@62 102 # depending on the network used.
cannam@62 103 #
cannam@62 104 # New implementations of Cap'n Proto should start out targeting the simplistic two-party network
cannam@62 105 # type as defined in `rpc-twoparty.capnp`. With this network type, level 3 is irrelevant and
cannam@62 106 # levels 2 and 4 are much easier than usual to implement. When such an implementation is paired
cannam@62 107 # with a container proxy, the contained app effectively gets to make full use of the proxy's
cannam@62 108 # network at level 4. And since Cap'n Proto IPC is extremely fast, it may never make sense to
cannam@62 109 # bother implementing any other vat network protocol -- just use the correct container type and get
cannam@62 110 # it for free.
cannam@62 111
cannam@62 112 using Cxx = import "/capnp/c++.capnp";
cannam@62 113 $Cxx.namespace("capnp::rpc");
cannam@62 114
cannam@62 115 # ========================================================================================
cannam@62 116 # The Four Tables
cannam@62 117 #
cannam@62 118 # Cap'n Proto RPC connections are stateful (although an application built on Cap'n Proto could
cannam@62 119 # export a stateless interface). As in CapTP, for each open connection, a vat maintains four state
cannam@62 120 # tables: questions, answers, imports, and exports. See the diagram at:
cannam@62 121 # http://www.erights.org/elib/distrib/captp/4tables.html
cannam@62 122 #
cannam@62 123 # The question table corresponds to the other end's answer table, and the imports table corresponds
cannam@62 124 # to the other end's exports table.
cannam@62 125 #
cannam@62 126 # The entries in each table are identified by ID numbers (defined below as 32-bit integers). These
cannam@62 127 # numbers are always specific to the connection; a newly-established connection starts with no
cannam@62 128 # valid IDs. Since low-numbered IDs will pack better, it is suggested that IDs be assigned like
cannam@62 129 # Unix file descriptors -- prefer the lowest-number ID that is currently available.
cannam@62 130 #
cannam@62 131 # IDs in the questions/answers tables are chosen by the questioner and generally represent method
cannam@62 132 # calls that are in progress.
cannam@62 133 #
cannam@62 134 # IDs in the imports/exports tables are chosen by the exporter and generally represent objects on
cannam@62 135 # which methods may be called. Exports may be "settled", meaning the exported object is an actual
cannam@62 136 # object living in the exporter's vat, or they may be "promises", meaning the exported object is
cannam@62 137 # the as-yet-unknown result of an ongoing operation and will eventually be resolved to some other
cannam@62 138 # object once that operation completes. Calls made to a promise will be forwarded to the eventual
cannam@62 139 # target once it is known. The eventual replacement object does *not* get the same ID as the
cannam@62 140 # promise, as it may turn out to be an object that is already exported (so already has an ID) or
cannam@62 141 # may even live in a completely different vat (and so won't get an ID on the same export table
cannam@62 142 # at all).
cannam@62 143 #
cannam@62 144 # IDs can be reused over time. To make this safe, we carefully define the lifetime of IDs. Since
cannam@62 145 # messages using the ID could be traveling in both directions simultaneously, we must define the
cannam@62 146 # end of life of each ID _in each direction_. The ID is only safe to reuse once it has been
cannam@62 147 # released by both sides.
cannam@62 148 #
cannam@62 149 # When a Cap'n Proto connection is lost, everything on the four tables is lost. All questions are
cannam@62 150 # canceled and throw exceptions. All imports become broken (all future calls to them throw
cannam@62 151 # exceptions). All exports and answers are implicitly released. The only things not lost are
cannam@62 152 # persistent capabilities (`SturdyRef`s). The application must plan for this and should respond by
cannam@62 153 # establishing a new connection and restoring from these persistent capabilities.
cannam@62 154
cannam@62 155 using QuestionId = UInt32;
cannam@62 156 # **(level 0)**
cannam@62 157 #
cannam@62 158 # Identifies a question in the sender's question table (which corresponds to the receiver's answer
cannam@62 159 # table). The questioner (caller) chooses an ID when making a call. The ID remains valid in
cannam@62 160 # caller -> callee messages until a Finish message is sent, and remains valid in callee -> caller
cannam@62 161 # messages until a Return message is sent.
cannam@62 162
cannam@62 163 using AnswerId = QuestionId;
cannam@62 164 # **(level 0)**
cannam@62 165 #
cannam@62 166 # Identifies an answer in the sender's answer table (which corresponds to the receiver's question
cannam@62 167 # table).
cannam@62 168 #
cannam@62 169 # AnswerId is physically equivalent to QuestionId, since the question and answer tables correspond,
cannam@62 170 # but we define a separate type for documentation purposes: we always use the type representing
cannam@62 171 # the sender's point of view.
cannam@62 172
cannam@62 173 using ExportId = UInt32;
cannam@62 174 # **(level 1)**
cannam@62 175 #
cannam@62 176 # Identifies an exported capability or promise in the sender's export table (which corresponds
cannam@62 177 # to the receiver's import table). The exporter chooses an ID before sending a capability over the
cannam@62 178 # wire. If the capability is already in the table, the exporter should reuse the same ID. If the
cannam@62 179 # ID is a promise (as opposed to a settled capability), this must be indicated at the time the ID
cannam@62 180 # is introduced (e.g. by using `senderPromise` instead of `senderHosted` in `CapDescriptor`); in
cannam@62 181 # this case, the importer shall expect a later `Resolve` message that replaces the promise.
cannam@62 182 #
cannam@62 183 # ExportId/ImportIds are subject to reference counting. Whenever an `ExportId` is sent over the
cannam@62 184 # wire (from the exporter to the importer), the export's reference count is incremented (unless
cannam@62 185 # otherwise specified). The reference count is later decremented by a `Release` message. Since
cannam@62 186 # the `Release` message can specify an arbitrary number by which to reduce the reference count, the
cannam@62 187 # importer should usually batch reference decrements and only send a `Release` when it believes the
cannam@62 188 # reference count has hit zero. Of course, it is possible that a new reference to the export is
cannam@62 189 # in-flight at the time that the `Release` message is sent, so it is necessary for the exporter to
cannam@62 190 # keep track of the reference count on its end as well to avoid race conditions.
cannam@62 191 #
cannam@62 192 # When a connection is lost, all exports are implicitly released. It is not possible to restore
cannam@62 193 # a connection state after disconnect (although a transport layer could implement a concept of
cannam@62 194 # persistent connections if it is transparent to the RPC layer).
cannam@62 195
cannam@62 196 using ImportId = ExportId;
cannam@62 197 # **(level 1)**
cannam@62 198 #
cannam@62 199 # Identifies an imported capability or promise in the sender's import table (which corresponds to
cannam@62 200 # the receiver's export table).
cannam@62 201 #
cannam@62 202 # ImportId is physically equivalent to ExportId, since the export and import tables correspond,
cannam@62 203 # but we define a separate type for documentation purposes: we always use the type representing
cannam@62 204 # the sender's point of view.
cannam@62 205 #
cannam@62 206 # An `ImportId` remains valid in importer -> exporter messages until the importer has sent
cannam@62 207 # `Release` messages that (it believes) have reduced the reference count to zero.
cannam@62 208
cannam@62 209 # ========================================================================================
cannam@62 210 # Messages
cannam@62 211
cannam@62 212 struct Message {
cannam@62 213 # An RPC connection is a bi-directional stream of Messages.
cannam@62 214
cannam@62 215 union {
cannam@62 216 unimplemented @0 :Message;
cannam@62 217 # The sender previously received this message from the peer but didn't understand it or doesn't
cannam@62 218 # yet implement the functionality that was requested. So, the sender is echoing the message
cannam@62 219 # back. In some cases, the receiver may be able to recover from this by pretending the sender
cannam@62 220 # had taken some appropriate "null" action.
cannam@62 221 #
cannam@62 222 # For example, say `resolve` is received by a level 0 implementation (because a previous call
cannam@62 223 # or return happened to contain a promise). The level 0 implementation will echo it back as
cannam@62 224 # `unimplemented`. The original sender can then simply release the cap to which the promise
cannam@62 225 # had resolved, thus avoiding a leak.
cannam@62 226 #
cannam@62 227 # For any message type that introduces a question, if the message comes back unimplemented,
cannam@62 228 # the original sender may simply treat it as if the question failed with an exception.
cannam@62 229 #
cannam@62 230 # In cases where there is no sensible way to react to an `unimplemented` message (without
cannam@62 231 # resource leaks or other serious problems), the connection may need to be aborted. This is
cannam@62 232 # a gray area; different implementations may take different approaches.
cannam@62 233
cannam@62 234 abort @1 :Exception;
cannam@62 235 # Sent when a connection is being aborted due to an unrecoverable error. This could be e.g.
cannam@62 236 # because the sender received an invalid or nonsensical message (`isCallersFault` is true) or
cannam@62 237 # because the sender had an internal error (`isCallersFault` is false). The sender will shut
cannam@62 238 # down the outgoing half of the connection after `abort` and will completely close the
cannam@62 239 # connection shortly thereafter (it's up to the sender how much of a time buffer they want to
cannam@62 240 # offer for the client to receive the `abort` before the connection is reset).
cannam@62 241
cannam@62 242 # Level 0 features -----------------------------------------------
cannam@62 243
cannam@62 244 bootstrap @8 :Bootstrap; # Request the peer's bootstrap interface.
cannam@62 245 call @2 :Call; # Begin a method call.
cannam@62 246 return @3 :Return; # Complete a method call.
cannam@62 247 finish @4 :Finish; # Release a returned answer / cancel a call.
cannam@62 248
cannam@62 249 # Level 1 features -----------------------------------------------
cannam@62 250
cannam@62 251 resolve @5 :Resolve; # Resolve a previously-sent promise.
cannam@62 252 release @6 :Release; # Release a capability so that the remote object can be deallocated.
cannam@62 253 disembargo @13 :Disembargo; # Lift an embargo used to enforce E-order over promise resolution.
cannam@62 254
cannam@62 255 # Level 2 features -----------------------------------------------
cannam@62 256
cannam@62 257 obsoleteSave @7 :AnyPointer;
cannam@62 258 # Obsolete request to save a capability, resulting in a SturdyRef. This has been replaced
cannam@62 259 # by the `Persistent` interface defined in `persistent.capnp`. This operation was never
cannam@62 260 # implemented.
cannam@62 261
cannam@62 262 obsoleteDelete @9 :AnyPointer;
cannam@62 263 # Obsolete way to delete a SturdyRef. This operation was never implemented.
cannam@62 264
cannam@62 265 # Level 3 features -----------------------------------------------
cannam@62 266
cannam@62 267 provide @10 :Provide; # Provide a capability to a third party.
cannam@62 268 accept @11 :Accept; # Accept a capability provided by a third party.
cannam@62 269
cannam@62 270 # Level 4 features -----------------------------------------------
cannam@62 271
cannam@62 272 join @12 :Join; # Directly connect to the common root of two or more proxied caps.
cannam@62 273 }
cannam@62 274 }
cannam@62 275
cannam@62 276 # Level 0 message types ----------------------------------------------
cannam@62 277
cannam@62 278 struct Bootstrap {
cannam@62 279 # **(level 0)**
cannam@62 280 #
cannam@62 281 # Get the "bootstrap" interface exported by the remote vat.
cannam@62 282 #
cannam@62 283 # For level 0, 1, and 2 implementations, the "bootstrap" interface is simply the main interface
cannam@62 284 # exported by a vat. If the vat acts as a server fielding connections from clients, then the
cannam@62 285 # bootstrap interface defines the basic functionality available to a client when it connects.
cannam@62 286 # The exact interface definition obviously depends on the application.
cannam@62 287 #
cannam@62 288 # We call this a "bootstrap" because in an ideal Cap'n Proto world, bootstrap interfaces would
cannam@62 289 # never be used. In such a world, any time you connect to a new vat, you do so because you
cannam@62 290 # received an introduction from some other vat (see `ThirdPartyCapId`). Thus, the first message
cannam@62 291 # you send is `Accept`, and further communications derive from there. `Bootstrap` is not used.
cannam@62 292 #
cannam@62 293 # In such an ideal world, DNS itself would support Cap'n Proto -- performing a DNS lookup would
cannam@62 294 # actually return a new Cap'n Proto capability, thus introducing you to the target system via
cannam@62 295 # level 3 RPC. Applications would receive the capability to talk to DNS in the first place as
cannam@62 296 # an initial endowment or part of a Powerbox interaction. Therefore, an app can form arbitrary
cannam@62 297 # connections without ever using `Bootstrap`.
cannam@62 298 #
cannam@62 299 # Of course, in the real world, DNS is not Cap'n-Proto-based, and we don't want Cap'n Proto to
cannam@62 300 # require a whole new internet infrastructure to be useful. Therefore, we offer bootstrap
cannam@62 301 # interfaces as a way to get up and running without a level 3 introduction. Thus, bootstrap
cannam@62 302 # interfaces are used to "bootstrap" from other, non-Cap'n-Proto-based means of service discovery,
cannam@62 303 # such as legacy DNS.
cannam@62 304 #
cannam@62 305 # Note that a vat need not provide a bootstrap interface, and in fact many vats (especially those
cannam@62 306 # acting as clients) do not. In this case, the vat should either reply to `Bootstrap` with a
cannam@62 307 # `Return` indicating an exception, or should return a dummy capability with no methods.
cannam@62 308
cannam@62 309 questionId @0 :QuestionId;
cannam@62 310 # A new question ID identifying this request, which will eventually receive a Return message
cannam@62 311 # containing the restored capability.
cannam@62 312
cannam@62 313 deprecatedObjectId @1 :AnyPointer;
cannam@62 314 # ** DEPRECATED **
cannam@62 315 #
cannam@62 316 # A Vat may export multiple bootstrap interfaces. In this case, `deprecatedObjectId` specifies
cannam@62 317 # which one to return. If this pointer is null, then the default bootstrap interface is returned.
cannam@62 318 #
cannam@62 319 # As of verison 0.5, use of this field is deprecated. If a service wants to export multiple
cannam@62 320 # bootstrap interfaces, it should instead define a single bootstarp interface that has methods
cannam@62 321 # that return each of the other interfaces.
cannam@62 322 #
cannam@62 323 # **History**
cannam@62 324 #
cannam@62 325 # In the first version of Cap'n Proto RPC (0.4.x) the `Bootstrap` message was called `Restore`.
cannam@62 326 # At the time, it was thought that this would eventually serve as the way to restore SturdyRefs
cannam@62 327 # (level 2). Meanwhile, an application could offer its "main" interface on a well-known
cannam@62 328 # (non-secret) SturdyRef.
cannam@62 329 #
cannam@62 330 # Since level 2 RPC was not implemented at the time, the `Restore` message was in practice only
cannam@62 331 # used to obtain the main interface. Since most applications had only one main interface that
cannam@62 332 # they wanted to restore, they tended to designate this with a null `objectId`.
cannam@62 333 #
cannam@62 334 # Unfortunately, the earliest version of the EZ RPC interfaces set a precedent of exporting
cannam@62 335 # multiple main interfaces by allowing them to be exported under string names. In this case,
cannam@62 336 # `objectId` was a Text value specifying the name.
cannam@62 337 #
cannam@62 338 # All of this proved problematic for several reasons:
cannam@62 339 #
cannam@62 340 # - The arrangement assumed that a client wishing to restore a SturdyRef would know exactly what
cannam@62 341 # machine to connect to and would be able to immediately restore a SturdyRef on connection.
cannam@62 342 # However, in practice, the ability to restore SturdyRefs is itself a capability that may
cannam@62 343 # require going through an authentication process to obtain. Thus, it makes more sense to
cannam@62 344 # define a "restorer service" as a full Cap'n Proto interface. If this restorer interface is
cannam@62 345 # offered as the vat's bootstrap interface, then this is equivalent to the old arrangement.
cannam@62 346 #
cannam@62 347 # - Overloading "Restore" for the purpose of obtaining well-known capabilities encouraged the
cannam@62 348 # practice of exporting singleton services with string names. If singleton services are desired,
cannam@62 349 # it is better to have one main interface that has methods that can be used to obtain each
cannam@62 350 # service, in order to get all the usual benefits of schemas and type checking.
cannam@62 351 #
cannam@62 352 # - Overloading "Restore" also had a security problem: Often, "main" or "well-known"
cannam@62 353 # capabilities exported by a vat are in fact not public: they are intended to be accessed only
cannam@62 354 # by clients who are capable of forming a connection to the vat. This can lead to trouble if
cannam@62 355 # the client itself has other clients and wishes to foward some `Restore` requests from those
cannam@62 356 # external clients -- it has to be very careful not to allow through `Restore` requests
cannam@62 357 # addressing the default capability.
cannam@62 358 #
cannam@62 359 # For example, consider the case of a sandboxed Sandstorm application and its supervisor. The
cannam@62 360 # application exports a default capability to its supervisor that provides access to
cannam@62 361 # functionality that only the supervisor is supposed to access. Meanwhile, though, applications
cannam@62 362 # may publish other capabilities that may be persistent, in which case the application needs
cannam@62 363 # to field `Restore` requests that could come from anywhere. These requests of course have to
cannam@62 364 # pass through the supervisor, as all communications with the outside world must. But, the
cannam@62 365 # supervisor has to be careful not to honor an external request addressing the application's
cannam@62 366 # default capability, since this capability is privileged. Unfortunately, the default
cannam@62 367 # capability cannot be given an unguessable name, because then the supervisor itself would not
cannam@62 368 # be able to address it!
cannam@62 369 #
cannam@62 370 # As of Cap'n Proto 0.5, `Restore` has been renamed to `Bootstrap` and is no longer planned for
cannam@62 371 # use in restoring SturdyRefs.
cannam@62 372 #
cannam@62 373 # Note that 0.4 also defined a message type called `Delete` that, like `Restore`, addressed a
cannam@62 374 # SturdyRef, but indicated that the client would not restore the ref again in the future. This
cannam@62 375 # operation was never implemented, so it was removed entirely. If a "delete" operation is desired,
cannam@62 376 # it should exist as a method on the same interface that handles restoring SturdyRefs. However,
cannam@62 377 # the utility of such an operation is questionable. You wouldn't be able to rely on it for
cannam@62 378 # garbage collection since a client could always disappear permanently without remembering to
cannam@62 379 # delete all its SturdyRefs, thus leaving them dangling forever. Therefore, it is advisable to
cannam@62 380 # design systems such that SturdyRefs never represent "owned" pointers.
cannam@62 381 #
cannam@62 382 # For example, say a SturdyRef points to an image file hosted on some server. That image file
cannam@62 383 # should also live inside a collection (a gallery, perhaps) hosted on the same server, owned by
cannam@62 384 # a user who can delete the image at any time. If the user deletes the image, the SturdyRef
cannam@62 385 # stops working. On the other hand, if the SturdyRef is discarded, this has no effect on the
cannam@62 386 # existence of the image in its collection.
cannam@62 387 }
cannam@62 388
cannam@62 389 struct Call {
cannam@62 390 # **(level 0)**
cannam@62 391 #
cannam@62 392 # Message type initiating a method call on a capability.
cannam@62 393
cannam@62 394 questionId @0 :QuestionId;
cannam@62 395 # A number, chosen by the caller, that identifies this call in future messages. This number
cannam@62 396 # must be different from all other calls originating from the same end of the connection (but
cannam@62 397 # may overlap with question IDs originating from the opposite end). A fine strategy is to use
cannam@62 398 # sequential question IDs, but the recipient should not assume this.
cannam@62 399 #
cannam@62 400 # A question ID can be reused once both:
cannam@62 401 # - A matching Return has been received from the callee.
cannam@62 402 # - A matching Finish has been sent from the caller.
cannam@62 403
cannam@62 404 target @1 :MessageTarget;
cannam@62 405 # The object that should receive this call.
cannam@62 406
cannam@62 407 interfaceId @2 :UInt64;
cannam@62 408 # The type ID of the interface being called. Each capability may implement multiple interfaces.
cannam@62 409
cannam@62 410 methodId @3 :UInt16;
cannam@62 411 # The ordinal number of the method to call within the requested interface.
cannam@62 412
cannam@62 413 allowThirdPartyTailCall @8 :Bool = false;
cannam@62 414 # Indicates whether or not the receiver is allowed to send a `Return` containing
cannam@62 415 # `acceptFromThirdParty`. Level 3 implementations should set this true. Otherwise, the callee
cannam@62 416 # will have to proxy the return in the case of a tail call to a third-party vat.
cannam@62 417
cannam@62 418 params @4 :Payload;
cannam@62 419 # The call parameters. `params.content` is a struct whose fields correspond to the parameters of
cannam@62 420 # the method.
cannam@62 421
cannam@62 422 sendResultsTo :union {
cannam@62 423 # Where should the return message be sent?
cannam@62 424
cannam@62 425 caller @5 :Void;
cannam@62 426 # Send the return message back to the caller (the usual).
cannam@62 427
cannam@62 428 yourself @6 :Void;
cannam@62 429 # **(level 1)**
cannam@62 430 #
cannam@62 431 # Don't actually return the results to the sender. Instead, hold on to them and await
cannam@62 432 # instructions from the sender regarding what to do with them. In particular, the sender
cannam@62 433 # may subsequently send a `Return` for some other call (which the receiver had previously made
cannam@62 434 # to the sender) with `takeFromOtherQuestion` set. The results from this call are then used
cannam@62 435 # as the results of the other call.
cannam@62 436 #
cannam@62 437 # When `yourself` is used, the receiver must still send a `Return` for the call, but sets the
cannam@62 438 # field `resultsSentElsewhere` in that `Return` rather than including the results.
cannam@62 439 #
cannam@62 440 # This feature can be used to implement tail calls in which a call from Vat A to Vat B ends up
cannam@62 441 # returning the result of a call from Vat B back to Vat A.
cannam@62 442 #
cannam@62 443 # In particular, the most common use case for this feature is when Vat A makes a call to a
cannam@62 444 # promise in Vat B, and then that promise ends up resolving to a capability back in Vat A.
cannam@62 445 # Vat B must forward all the queued calls on that promise back to Vat A, but can set `yourself`
cannam@62 446 # in the calls so that the results need not pass back through Vat B.
cannam@62 447 #
cannam@62 448 # For example:
cannam@62 449 # - Alice, in Vat A, call foo() on Bob in Vat B.
cannam@62 450 # - Alice makes a pipelined call bar() on the promise returned by foo().
cannam@62 451 # - Later on, Bob resolves the promise from foo() to point at Carol, who lives in Vat A (next
cannam@62 452 # to Alice).
cannam@62 453 # - Vat B dutifully forwards the bar() call to Carol. Let us call this forwarded call bar'().
cannam@62 454 # Notice that bar() and bar'() are travelling in opposite directions on the same network
cannam@62 455 # link.
cannam@62 456 # - The `Call` for bar'() has `sendResultsTo` set to `yourself`, with the value being the
cannam@62 457 # question ID originally assigned to the bar() call.
cannam@62 458 # - Vat A receives bar'() and delivers it to Carol.
cannam@62 459 # - When bar'() returns, Vat A immediately takes the results and returns them from bar().
cannam@62 460 # - Meanwhile, Vat A sends a `Return` for bar'() to Vat B, with `resultsSentElsewhere` set in
cannam@62 461 # place of results.
cannam@62 462 # - Vat A sends a `Finish` for that call to Vat B.
cannam@62 463 # - Vat B receives the `Return` for bar'() and sends a `Return` for bar(), with
cannam@62 464 # `receivedFromYourself` set in place of the results.
cannam@62 465 # - Vat B receives the `Finish` for bar() and sends a `Finish` to bar'().
cannam@62 466
cannam@62 467 thirdParty @7 :RecipientId;
cannam@62 468 # **(level 3)**
cannam@62 469 #
cannam@62 470 # The call's result should be returned to a different vat. The receiver (the callee) expects
cannam@62 471 # to receive an `Accept` message from the indicated vat, and should return the call's result
cannam@62 472 # to it, rather than to the sender of the `Call`.
cannam@62 473 #
cannam@62 474 # This operates much like `yourself`, above, except that Carol is in a separate Vat C. `Call`
cannam@62 475 # messages are sent from Vat A -> Vat B and Vat B -> Vat C. A `Return` message is sent from
cannam@62 476 # Vat B -> Vat A that contains `acceptFromThirdParty` in place of results. When Vat A sends
cannam@62 477 # an `Accept` to Vat C, it receives back a `Return` containing the call's actual result. Vat C
cannam@62 478 # also sends a `Return` to Vat B with `resultsSentElsewhere`.
cannam@62 479 }
cannam@62 480 }
cannam@62 481
cannam@62 482 struct Return {
cannam@62 483 # **(level 0)**
cannam@62 484 #
cannam@62 485 # Message type sent from callee to caller indicating that the call has completed.
cannam@62 486
cannam@62 487 answerId @0 :AnswerId;
cannam@62 488 # Equal to the QuestionId of the corresponding `Call` message.
cannam@62 489
cannam@62 490 releaseParamCaps @1 :Bool = true;
cannam@62 491 # If true, all capabilities that were in the params should be considered released. The sender
cannam@62 492 # must not send separate `Release` messages for them. Level 0 implementations in particular
cannam@62 493 # should always set this true. This defaults true because if level 0 implementations forget to
cannam@62 494 # set it they'll never notice (just silently leak caps), but if level >=1 implementations forget
cannam@62 495 # to set it to false they'll quickly get errors.
cannam@62 496
cannam@62 497 union {
cannam@62 498 results @2 :Payload;
cannam@62 499 # The result.
cannam@62 500 #
cannam@62 501 # For regular method calls, `results.content` points to the result struct.
cannam@62 502 #
cannam@62 503 # For a `Return` in response to an `Accept`, `results` contains a single capability (rather
cannam@62 504 # than a struct), and `results.content` is just a capability pointer with index 0. A `Finish`
cannam@62 505 # is still required in this case.
cannam@62 506
cannam@62 507 exception @3 :Exception;
cannam@62 508 # Indicates that the call failed and explains why.
cannam@62 509
cannam@62 510 canceled @4 :Void;
cannam@62 511 # Indicates that the call was canceled due to the caller sending a Finish message
cannam@62 512 # before the call had completed.
cannam@62 513
cannam@62 514 resultsSentElsewhere @5 :Void;
cannam@62 515 # This is set when returning from a `Call` that had `sendResultsTo` set to something other
cannam@62 516 # than `caller`.
cannam@62 517
cannam@62 518 takeFromOtherQuestion @6 :QuestionId;
cannam@62 519 # The sender has also sent (before this message) a `Call` with the given question ID and with
cannam@62 520 # `sendResultsTo.yourself` set, and the results of that other call should be used as the
cannam@62 521 # results here.
cannam@62 522
cannam@62 523 acceptFromThirdParty @7 :ThirdPartyCapId;
cannam@62 524 # **(level 3)**
cannam@62 525 #
cannam@62 526 # The caller should contact a third-party vat to pick up the results. An `Accept` message
cannam@62 527 # sent to the vat will return the result. This pairs with `Call.sendResultsTo.thirdParty`.
cannam@62 528 # It should only be used if the corresponding `Call` had `allowThirdPartyTailCall` set.
cannam@62 529 }
cannam@62 530 }
cannam@62 531
cannam@62 532 struct Finish {
cannam@62 533 # **(level 0)**
cannam@62 534 #
cannam@62 535 # Message type sent from the caller to the callee to indicate:
cannam@62 536 # 1) The questionId will no longer be used in any messages sent by the callee (no further
cannam@62 537 # pipelined requests).
cannam@62 538 # 2) If the call has not returned yet, the caller no longer cares about the result. If nothing
cannam@62 539 # else cares about the result either (e.g. there are no other outstanding calls pipelined on
cannam@62 540 # the result of this one) then the callee may wish to immediately cancel the operation and
cannam@62 541 # send back a Return message with "canceled" set. However, implementations are not required
cannam@62 542 # to support premature cancellation -- instead, the implementation may wait until the call
cannam@62 543 # actually completes and send a normal `Return` message.
cannam@62 544 #
cannam@62 545 # TODO(someday): Should we separate (1) and implicitly releasing result capabilities? It would be
cannam@62 546 # possible and useful to notify the server that it doesn't need to keep around the response to
cannam@62 547 # service pipeline requests even though the caller still wants to receive it / hasn't yet
cannam@62 548 # finished processing it. It could also be useful to notify the server that it need not marshal
cannam@62 549 # the results because the caller doesn't want them anyway, even if the caller is still sending
cannam@62 550 # pipelined calls, although this seems less useful (just saving some bytes on the wire).
cannam@62 551
cannam@62 552 questionId @0 :QuestionId;
cannam@62 553 # ID of the call whose result is to be released.
cannam@62 554
cannam@62 555 releaseResultCaps @1 :Bool = true;
cannam@62 556 # If true, all capabilities that were in the results should be considered released. The sender
cannam@62 557 # must not send separate `Release` messages for them. Level 0 implementations in particular
cannam@62 558 # should always set this true. This defaults true because if level 0 implementations forget to
cannam@62 559 # set it they'll never notice (just silently leak caps), but if level >=1 implementations forget
cannam@62 560 # set it false they'll quickly get errors.
cannam@62 561 }
cannam@62 562
cannam@62 563 # Level 1 message types ----------------------------------------------
cannam@62 564
cannam@62 565 struct Resolve {
cannam@62 566 # **(level 1)**
cannam@62 567 #
cannam@62 568 # Message type sent to indicate that a previously-sent promise has now been resolved to some other
cannam@62 569 # object (possibly another promise) -- or broken, or canceled.
cannam@62 570 #
cannam@62 571 # Keep in mind that it's possible for a `Resolve` to be sent to a level 0 implementation that
cannam@62 572 # doesn't implement it. For example, a method call or return might contain a capability in the
cannam@62 573 # payload. Normally this is fine even if the receiver is level 0, because they will implicitly
cannam@62 574 # release all such capabilities on return / finish. But if the cap happens to be a promise, then
cannam@62 575 # a follow-up `Resolve` may be sent regardless of this release. The level 0 receiver will reply
cannam@62 576 # with an `unimplemented` message, and the sender (of the `Resolve`) can respond to this as if the
cannam@62 577 # receiver had immediately released any capability to which the promise resolved.
cannam@62 578 #
cannam@62 579 # When implementing promise resolution, it's important to understand how embargos work and the
cannam@62 580 # tricky case of the Tribble 4-way race condition. See the comments for the Disembargo message,
cannam@62 581 # below.
cannam@62 582
cannam@62 583 promiseId @0 :ExportId;
cannam@62 584 # The ID of the promise to be resolved.
cannam@62 585 #
cannam@62 586 # Unlike all other instances of `ExportId` sent from the exporter, the `Resolve` message does
cannam@62 587 # _not_ increase the reference count of `promiseId`. In fact, it is expected that the receiver
cannam@62 588 # will release the export soon after receiving `Resolve`, and the sender will not send this
cannam@62 589 # `ExportId` again until it has been released and recycled.
cannam@62 590 #
cannam@62 591 # When an export ID sent over the wire (e.g. in a `CapDescriptor`) is indicated to be a promise,
cannam@62 592 # this indicates that the sender will follow up at some point with a `Resolve` message. If the
cannam@62 593 # same `promiseId` is sent again before `Resolve`, still only one `Resolve` is sent. If the
cannam@62 594 # same ID is sent again later _after_ a `Resolve`, it can only be because the export's
cannam@62 595 # reference count hit zero in the meantime and the ID was re-assigned to a new export, therefore
cannam@62 596 # this later promise does _not_ correspond to the earlier `Resolve`.
cannam@62 597 #
cannam@62 598 # If a promise ID's reference count reaches zero before a `Resolve` is sent, the `Resolve`
cannam@62 599 # message may or may not still be sent (the `Resolve` may have already been in-flight when
cannam@62 600 # `Release` was sent, but if the `Release` is received before `Resolve` then there is no longer
cannam@62 601 # any reason to send a `Resolve`). Thus a `Resolve` may be received for a promise of which
cannam@62 602 # the receiver has no knowledge, because it already released it earlier. In this case, the
cannam@62 603 # receiver should simply release the capability to which the promise resolved.
cannam@62 604
cannam@62 605 union {
cannam@62 606 cap @1 :CapDescriptor;
cannam@62 607 # The object to which the promise resolved.
cannam@62 608 #
cannam@62 609 # The sender promises that from this point forth, until `promiseId` is released, it shall
cannam@62 610 # simply forward all messages to the capability designated by `cap`. This is true even if
cannam@62 611 # `cap` itself happens to desigate another promise, and that other promise later resolves --
cannam@62 612 # messages sent to `promiseId` shall still go to that other promise, not to its resolution.
cannam@62 613 # This is important in the case that the receiver of the `Resolve` ends up sending a
cannam@62 614 # `Disembargo` message towards `promiseId` in order to control message ordering -- that
cannam@62 615 # `Disembargo` really needs to reflect back to exactly the object designated by `cap` even
cannam@62 616 # if that object is itself a promise.
cannam@62 617
cannam@62 618 exception @2 :Exception;
cannam@62 619 # Indicates that the promise was broken.
cannam@62 620 }
cannam@62 621 }
cannam@62 622
cannam@62 623 struct Release {
cannam@62 624 # **(level 1)**
cannam@62 625 #
cannam@62 626 # Message type sent to indicate that the sender is done with the given capability and the receiver
cannam@62 627 # can free resources allocated to it.
cannam@62 628
cannam@62 629 id @0 :ImportId;
cannam@62 630 # What to release.
cannam@62 631
cannam@62 632 referenceCount @1 :UInt32;
cannam@62 633 # The amount by which to decrement the reference count. The export is only actually released
cannam@62 634 # when the reference count reaches zero.
cannam@62 635 }
cannam@62 636
cannam@62 637 struct Disembargo {
cannam@62 638 # **(level 1)**
cannam@62 639 #
cannam@62 640 # Message sent to indicate that an embargo on a recently-resolved promise may now be lifted.
cannam@62 641 #
cannam@62 642 # Embargos are used to enforce E-order in the presence of promise resolution. That is, if an
cannam@62 643 # application makes two calls foo() and bar() on the same capability reference, in that order,
cannam@62 644 # the calls should be delivered in the order in which they were made. But if foo() is called
cannam@62 645 # on a promise, and that promise happens to resolve before bar() is called, then the two calls
cannam@62 646 # may travel different paths over the network, and thus could arrive in the wrong order. In
cannam@62 647 # this case, the call to `bar()` must be embargoed, and a `Disembargo` message must be sent along
cannam@62 648 # the same path as `foo()` to ensure that the `Disembargo` arrives after `foo()`. Once the
cannam@62 649 # `Disembargo` arrives, `bar()` can then be delivered.
cannam@62 650 #
cannam@62 651 # There are two particular cases where embargos are important. Consider object Alice, in Vat A,
cannam@62 652 # who holds a promise P, pointing towards Vat B, that eventually resolves to Carol. The two
cannam@62 653 # cases are:
cannam@62 654 # - Carol lives in Vat A, i.e. next to Alice. In this case, Vat A needs to send a `Disembargo`
cannam@62 655 # message that echos through Vat B and back, to ensure that all pipelined calls on the promise
cannam@62 656 # have been delivered.
cannam@62 657 # - Carol lives in a different Vat C. When the promise resolves, a three-party handoff occurs
cannam@62 658 # (see `Provide` and `Accept`, which constitute level 3 of the protocol). In this case, we
cannam@62 659 # piggyback on the state that has already been set up to handle the handoff: the `Accept`
cannam@62 660 # message (from Vat A to Vat C) is embargoed, as are all pipelined messages sent to it, while
cannam@62 661 # a `Disembargo` message is sent from Vat A through Vat B to Vat C. See `Accept.embargo` for
cannam@62 662 # an example.
cannam@62 663 #
cannam@62 664 # Note that in the case where Carol actually lives in Vat B (i.e., the same vat that the promise
cannam@62 665 # already pointed at), no embargo is needed, because the pipelined calls are delivered over the
cannam@62 666 # same path as the later direct calls.
cannam@62 667 #
cannam@62 668 # Keep in mind that promise resolution happens both in the form of Resolve messages as well as
cannam@62 669 # Return messages (which resolve PromisedAnswers). Embargos apply in both cases.
cannam@62 670 #
cannam@62 671 # An alternative strategy for enforcing E-order over promise resolution could be for Vat A to
cannam@62 672 # implement the embargo internally. When Vat A is notified of promise resolution, it could
cannam@62 673 # send a dummy no-op call to promise P and wait for it to complete. Until that call completes,
cannam@62 674 # all calls to the capability are queued locally. This strategy works, but is pessimistic:
cannam@62 675 # in the three-party case, it requires an A -> B -> C -> B -> A round trip before calls can start
cannam@62 676 # being delivered directly to from Vat A to Vat C. The `Disembargo` message allows latency to be
cannam@62 677 # reduced. (In the two-party loopback case, the `Disembargo` message is just a more explicit way
cannam@62 678 # of accomplishing the same thing as a no-op call, but isn't any faster.)
cannam@62 679 #
cannam@62 680 # *The Tribble 4-way Race Condition*
cannam@62 681 #
cannam@62 682 # Any implementation of promise resolution and embargos must be aware of what we call the
cannam@62 683 # "Tribble 4-way race condition", after Dean Tribble, who explained the problem in a lively
cannam@62 684 # Friam meeting.
cannam@62 685 #
cannam@62 686 # Embargos are designed to work in the case where a two-hop path is being shortened to one hop.
cannam@62 687 # But sometimes there are more hops. Imagine that Alice has a reference to a remote promise P1
cannam@62 688 # that eventually resolves to _another_ remote promise P2 (in a third vat), which _at the same
cannam@62 689 # time_ happens to resolve to Bob (in a fourth vat). In this case, we're shortening from a 3-hop
cannam@62 690 # path (with four parties) to a 1-hop path (Alice -> Bob).
cannam@62 691 #
cannam@62 692 # Extending the embargo/disembargo protocol to be able to shorted multiple hops at once seems
cannam@62 693 # difficult. Instead, we make a rule that prevents this case from coming up:
cannam@62 694 #
cannam@62 695 # One a promise P has been resolved to a remove object reference R, then all further messages
cannam@62 696 # received addressed to P will be forwarded strictly to R. Even if it turns out later that R is
cannam@62 697 # itself a promise, and has resolved to some other object Q, messages sent to P will still be
cannam@62 698 # forwarded to R, not directly to Q (R will of course further forward the messages to Q).
cannam@62 699 #
cannam@62 700 # This rule does not cause a significant performance burden because once P has resolved to R, it
cannam@62 701 # is expected that people sending messages to P will shortly start sending them to R instead and
cannam@62 702 # drop P. P is at end-of-life anyway, so it doesn't matter if it ignores chances to further
cannam@62 703 # optimize its path.
cannam@62 704
cannam@62 705 target @0 :MessageTarget;
cannam@62 706 # What is to be disembargoed.
cannam@62 707
cannam@62 708 using EmbargoId = UInt32;
cannam@62 709 # Used in `senderLoopback` and `receiverLoopback`, below.
cannam@62 710
cannam@62 711 context :union {
cannam@62 712 senderLoopback @1 :EmbargoId;
cannam@62 713 # The sender is requesting a disembargo on a promise that is known to resolve back to a
cannam@62 714 # capability hosted by the sender. As soon as the receiver has echoed back all pipelined calls
cannam@62 715 # on this promise, it will deliver the Disembargo back to the sender with `receiverLoopback`
cannam@62 716 # set to the same value as `senderLoopback`. This value is chosen by the sender, and since
cannam@62 717 # it is also consumed be the sender, the sender can use whatever strategy it wants to make sure
cannam@62 718 # the value is unambiguous.
cannam@62 719 #
cannam@62 720 # The receiver must verify that the target capability actually resolves back to the sender's
cannam@62 721 # vat. Otherwise, the sender has committed a protocol error and should be disconnected.
cannam@62 722
cannam@62 723 receiverLoopback @2 :EmbargoId;
cannam@62 724 # The receiver previously sent a `senderLoopback` Disembargo towards a promise resolving to
cannam@62 725 # this capability, and that Disembargo is now being echoed back.
cannam@62 726
cannam@62 727 accept @3 :Void;
cannam@62 728 # **(level 3)**
cannam@62 729 #
cannam@62 730 # The sender is requesting a disembargo on a promise that is known to resolve to a third-party
cannam@62 731 # capability that the sender is currently in the process of accepting (using `Accept`).
cannam@62 732 # The receiver of this `Disembargo` has an outstanding `Provide` on said capability. The
cannam@62 733 # receiver should now send a `Disembargo` with `provide` set to the question ID of that
cannam@62 734 # `Provide` message.
cannam@62 735 #
cannam@62 736 # See `Accept.embargo` for an example.
cannam@62 737
cannam@62 738 provide @4 :QuestionId;
cannam@62 739 # **(level 3)**
cannam@62 740 #
cannam@62 741 # The sender is requesting a disembargo on a capability currently being provided to a third
cannam@62 742 # party. The question ID identifies the `Provide` message previously sent by the sender to
cannam@62 743 # this capability. On receipt, the receiver (the capability host) shall release the embargo
cannam@62 744 # on the `Accept` message that it has received from the third party. See `Accept.embargo` for
cannam@62 745 # an example.
cannam@62 746 }
cannam@62 747 }
cannam@62 748
cannam@62 749 # Level 2 message types ----------------------------------------------
cannam@62 750
cannam@62 751 # See persistent.capnp.
cannam@62 752
cannam@62 753 # Level 3 message types ----------------------------------------------
cannam@62 754
cannam@62 755 struct Provide {
cannam@62 756 # **(level 3)**
cannam@62 757 #
cannam@62 758 # Message type sent to indicate that the sender wishes to make a particular capability implemented
cannam@62 759 # by the receiver available to a third party for direct access (without the need for the third
cannam@62 760 # party to proxy through the sender).
cannam@62 761 #
cannam@62 762 # (In CapTP, `Provide` and `Accept` are methods of the global `NonceLocator` object exported by
cannam@62 763 # every vat. In Cap'n Proto, we bake this into the core protocol.)
cannam@62 764
cannam@62 765 questionId @0 :QuestionId;
cannam@62 766 # Question ID to be held open until the recipient has received the capability. A result will be
cannam@62 767 # returned once the third party has successfully received the capability. The sender must at some
cannam@62 768 # point send a `Finish` message as with any other call, and that message can be used to cancel the
cannam@62 769 # whole operation.
cannam@62 770
cannam@62 771 target @1 :MessageTarget;
cannam@62 772 # What is to be provided to the third party.
cannam@62 773
cannam@62 774 recipient @2 :RecipientId;
cannam@62 775 # Identity of the third party that is expected to pick up the capability.
cannam@62 776 }
cannam@62 777
cannam@62 778 struct Accept {
cannam@62 779 # **(level 3)**
cannam@62 780 #
cannam@62 781 # Message type sent to pick up a capability hosted by the receiving vat and provided by a third
cannam@62 782 # party. The third party previously designated the capability using `Provide`.
cannam@62 783 #
cannam@62 784 # This message is also used to pick up a redirected return -- see `Return.redirect`.
cannam@62 785
cannam@62 786 questionId @0 :QuestionId;
cannam@62 787 # A new question ID identifying this accept message, which will eventually receive a Return
cannam@62 788 # message containing the provided capability (or the call result in the case of a redirected
cannam@62 789 # return).
cannam@62 790
cannam@62 791 provision @1 :ProvisionId;
cannam@62 792 # Identifies the provided object to be picked up.
cannam@62 793
cannam@62 794 embargo @2 :Bool;
cannam@62 795 # If true, this accept shall be temporarily embargoed. The resulting `Return` will not be sent,
cannam@62 796 # and any pipelined calls will not be delivered, until the embargo is released. The receiver
cannam@62 797 # (the capability host) will expect the provider (the vat that sent the `Provide` message) to
cannam@62 798 # eventually send a `Disembargo` message with the field `context.provide` set to the question ID
cannam@62 799 # of the original `Provide` message. At that point, the embargo is released and the queued
cannam@62 800 # messages are delivered.
cannam@62 801 #
cannam@62 802 # For example:
cannam@62 803 # - Alice, in Vat A, holds a promise P, which currently points toward Vat B.
cannam@62 804 # - Alice calls foo() on P. The `Call` message is sent to Vat B.
cannam@62 805 # - The promise P in Vat B ends up resolving to Carol, in Vat C.
cannam@62 806 # - Vat B sends a `Provide` message to Vat C, identifying Vat A as the recipient.
cannam@62 807 # - Vat B sends a `Resolve` message to Vat A, indicating that the promise has resolved to a
cannam@62 808 # `ThirdPartyCapId` identifying Carol in Vat C.
cannam@62 809 # - Vat A sends an `Accept` message to Vat C to pick up the capability. Since Vat A knows that
cannam@62 810 # it has an outstanding call to the promise, it sets `embargo` to `true` in the `Accept`
cannam@62 811 # message.
cannam@62 812 # - Vat A sends a `Disembargo` message to Vat B on promise P, with `context.accept` set.
cannam@62 813 # - Alice makes a call bar() to promise P, which is now pointing towards Vat C. Alice doesn't
cannam@62 814 # know anything about the mechanics of promise resolution happening under the hood, but she
cannam@62 815 # expects that bar() will be delivered after foo() because that is the order in which she
cannam@62 816 # initiated the calls.
cannam@62 817 # - Vat A sends the bar() call to Vat C, as a pipelined call on the result of the `Accept` (which
cannam@62 818 # hasn't returned yet, due to the embargo). Since calls to the newly-accepted capability
cannam@62 819 # are embargoed, Vat C does not deliver the call yet.
cannam@62 820 # - At some point, Vat B forwards the foo() call from the beginning of this example on to Vat C.
cannam@62 821 # - Vat B forwards the `Disembargo` from Vat A on to vat C. It sets `context.provide` to the
cannam@62 822 # question ID of the `Provide` message it had sent previously.
cannam@62 823 # - Vat C receives foo() before `Disembargo`, thus allowing it to correctly deliver foo()
cannam@62 824 # before delivering bar().
cannam@62 825 # - Vat C receives `Disembargo` from Vat B. It can now send a `Return` for the `Accept` from
cannam@62 826 # Vat A, as well as deliver bar().
cannam@62 827 }
cannam@62 828
cannam@62 829 # Level 4 message types ----------------------------------------------
cannam@62 830
cannam@62 831 struct Join {
cannam@62 832 # **(level 4)**
cannam@62 833 #
cannam@62 834 # Message type sent to implement E.join(), which, given a number of capabilities that are
cannam@62 835 # expected to be equivalent, finds the underlying object upon which they all agree and forms a
cannam@62 836 # direct connection to it, skipping any proxies that may have been constructed by other vats
cannam@62 837 # while transmitting the capability. See:
cannam@62 838 # http://erights.org/elib/equality/index.html
cannam@62 839 #
cannam@62 840 # Note that this should only serve to bypass fully-transparent proxies -- proxies that were
cannam@62 841 # created merely for convenience, without any intention of hiding the underlying object.
cannam@62 842 #
cannam@62 843 # For example, say Bob holds two capabilities hosted by Alice and Carol, but he expects that both
cannam@62 844 # are simply proxies for a capability hosted elsewhere. He then issues a join request, which
cannam@62 845 # operates as follows:
cannam@62 846 # - Bob issues Join requests on both Alice and Carol. Each request contains a different piece
cannam@62 847 # of the JoinKey.
cannam@62 848 # - Alice is proxying a capability hosted by Dana, so forwards the request to Dana's cap.
cannam@62 849 # - Dana receives the first request and sees that the JoinKeyPart is one of two. She notes that
cannam@62 850 # she doesn't have the other part yet, so she records the request and responds with a
cannam@62 851 # JoinResult.
cannam@62 852 # - Alice relays the JoinAswer back to Bob.
cannam@62 853 # - Carol is also proxying a capability from Dana, and so forwards her Join request to Dana as
cannam@62 854 # well.
cannam@62 855 # - Dana receives Carol's request and notes that she now has both parts of a JoinKey. She
cannam@62 856 # combines them in order to form information needed to form a secure connection to Bob. She
cannam@62 857 # also responds with another JoinResult.
cannam@62 858 # - Bob receives the responses from Alice and Carol. He uses the returned JoinResults to
cannam@62 859 # determine how to connect to Dana and attempts to form the connection. Since Bob and Dana now
cannam@62 860 # agree on a secret key that neither Alice nor Carol ever saw, this connection can be made
cannam@62 861 # securely even if Alice or Carol is conspiring against the other. (If Alice and Carol are
cannam@62 862 # conspiring _together_, they can obviously reproduce the key, but this doesn't matter because
cannam@62 863 # the whole point of the join is to verify that Alice and Carol agree on what capability they
cannam@62 864 # are proxying.)
cannam@62 865 #
cannam@62 866 # If the two capabilities aren't actually proxies of the same object, then the join requests
cannam@62 867 # will come back with conflicting `hostId`s and the join will fail before attempting to form any
cannam@62 868 # connection.
cannam@62 869
cannam@62 870 questionId @0 :QuestionId;
cannam@62 871 # Question ID used to respond to this Join. (Note that this ID only identifies one part of the
cannam@62 872 # request for one hop; each part has a different ID and relayed copies of the request have
cannam@62 873 # (probably) different IDs still.)
cannam@62 874 #
cannam@62 875 # The receiver will reply with a `Return` whose `results` is a JoinResult. This `JoinResult`
cannam@62 876 # is relayed from the joined object's host, possibly with transformation applied as needed
cannam@62 877 # by the network.
cannam@62 878 #
cannam@62 879 # Like any return, the result must be released using a `Finish`. However, this release
cannam@62 880 # should not occur until the joiner has either successfully connected to the joined object.
cannam@62 881 # Vats relaying a `Join` message similarly must not release the result they receive until the
cannam@62 882 # return they relayed back towards the joiner has itself been released. This allows the
cannam@62 883 # joined object's host to detect when the Join operation is canceled before completing -- if
cannam@62 884 # it receives a `Finish` for one of the join results before the joiner successfully
cannam@62 885 # connects. It can then free any resources it had allocated as part of the join.
cannam@62 886
cannam@62 887 target @1 :MessageTarget;
cannam@62 888 # The capability to join.
cannam@62 889
cannam@62 890 keyPart @2 :JoinKeyPart;
cannam@62 891 # A part of the join key. These combine to form the complete join key, which is used to establish
cannam@62 892 # a direct connection.
cannam@62 893
cannam@62 894 # TODO(before implementing): Change this so that multiple parts can be sent in a single Join
cannam@62 895 # message, so that if multiple join parts are going to cross the same connection they can be sent
cannam@62 896 # together, so that the receive can potentially optimize its handling of them. In the case where
cannam@62 897 # all parts are bundled together, should the recipient be expected to simply return a cap, so
cannam@62 898 # that the caller can immediately start pipelining to it?
cannam@62 899 }
cannam@62 900
cannam@62 901 # ========================================================================================
cannam@62 902 # Common structures used in messages
cannam@62 903
cannam@62 904 struct MessageTarget {
cannam@62 905 # The target of a `Call` or other messages that target a capability.
cannam@62 906
cannam@62 907 union {
cannam@62 908 importedCap @0 :ImportId;
cannam@62 909 # This message is to a capability or promise previously imported by the caller (exported by
cannam@62 910 # the receiver).
cannam@62 911
cannam@62 912 promisedAnswer @1 :PromisedAnswer;
cannam@62 913 # This message is to a capability that is expected to be returned by another call that has not
cannam@62 914 # yet been completed.
cannam@62 915 #
cannam@62 916 # At level 0, this is supported only for addressing the result of a previous `Bootstrap`, so
cannam@62 917 # that initial startup doesn't require a round trip.
cannam@62 918 }
cannam@62 919 }
cannam@62 920
cannam@62 921 struct Payload {
cannam@62 922 # Represents some data structure that might contain capabilities.
cannam@62 923
cannam@62 924 content @0 :AnyPointer;
cannam@62 925 # Some Cap'n Proto data structure. Capability pointers embedded in this structure index into
cannam@62 926 # `capTable`.
cannam@62 927
cannam@62 928 capTable @1 :List(CapDescriptor);
cannam@62 929 # Descriptors corresponding to the cap pointers in `content`.
cannam@62 930 }
cannam@62 931
cannam@62 932 struct CapDescriptor {
cannam@62 933 # **(level 1)**
cannam@62 934 #
cannam@62 935 # When an application-defined type contains an interface pointer, that pointer contains an index
cannam@62 936 # into the message's capability table -- i.e. the `capTable` part of the `Payload`. Each
cannam@62 937 # capability in the table is represented as a `CapDescriptor`. The runtime API should not reveal
cannam@62 938 # the CapDescriptor directly to the application, but should instead wrap it in some kind of
cannam@62 939 # callable object with methods corresponding to the interface that the capability implements.
cannam@62 940 #
cannam@62 941 # Keep in mind that `ExportIds` in a `CapDescriptor` are subject to reference counting. See the
cannam@62 942 # description of `ExportId`.
cannam@62 943
cannam@62 944 union {
cannam@62 945 none @0 :Void;
cannam@62 946 # There is no capability here. This `CapDescriptor` should not appear in the payload content.
cannam@62 947 # A `none` CapDescriptor can be generated when an application inserts a capability into a
cannam@62 948 # message and then later changes its mind and removes it -- rewriting all of the other
cannam@62 949 # capability pointers may be hard, so instead a tombstone is left, similar to the way a removed
cannam@62 950 # struct or list instance is zeroed out of the message but the space is not reclaimed.
cannam@62 951 # Hopefully this is unusual.
cannam@62 952
cannam@62 953 senderHosted @1 :ExportId;
cannam@62 954 # A capability newly exported by the sender. This is the ID of the new capability in the
cannam@62 955 # sender's export table (receiver's import table).
cannam@62 956
cannam@62 957 senderPromise @2 :ExportId;
cannam@62 958 # A promise that the sender will resolve later. The sender will send exactly one Resolve
cannam@62 959 # message at a future point in time to replace this promise. Note that even if the same
cannam@62 960 # `senderPromise` is received multiple times, only one `Resolve` is sent to cover all of
cannam@62 961 # them. If `senderPromise` is released before the `Resolve` is sent, the sender (of this
cannam@62 962 # `CapDescriptor`) may choose not to send the `Resolve` at all.
cannam@62 963
cannam@62 964 receiverHosted @3 :ImportId;
cannam@62 965 # A capability (or promise) previously exported by the receiver (imported by the sender).
cannam@62 966
cannam@62 967 receiverAnswer @4 :PromisedAnswer;
cannam@62 968 # A capability expected to be returned in the results of a currently-outstanding call posed
cannam@62 969 # by the sender.
cannam@62 970
cannam@62 971 thirdPartyHosted @5 :ThirdPartyCapDescriptor;
cannam@62 972 # **(level 3)**
cannam@62 973 #
cannam@62 974 # A capability that lives in neither the sender's nor the receiver's vat. The sender needs
cannam@62 975 # to form a direct connection to a third party to pick up the capability.
cannam@62 976 #
cannam@62 977 # Level 1 and 2 implementations that receive a `thirdPartyHosted` may simply send calls to its
cannam@62 978 # `vine` instead.
cannam@62 979 }
cannam@62 980 }
cannam@62 981
cannam@62 982 struct PromisedAnswer {
cannam@62 983 # **(mostly level 1)**
cannam@62 984 #
cannam@62 985 # Specifies how to derive a promise from an unanswered question, by specifying the path of fields
cannam@62 986 # to follow from the root of the eventual result struct to get to the desired capability. Used
cannam@62 987 # to address method calls to a not-yet-returned capability or to pass such a capability as an
cannam@62 988 # input to some other method call.
cannam@62 989 #
cannam@62 990 # Level 0 implementations must support `PromisedAnswer` only for the case where the answer is
cannam@62 991 # to a `Bootstrap` message. In this case, `path` is always empty since `Bootstrap` always returns
cannam@62 992 # a raw capability.
cannam@62 993
cannam@62 994 questionId @0 :QuestionId;
cannam@62 995 # ID of the question (in the sender's question table / receiver's answer table) whose answer is
cannam@62 996 # expected to contain the capability.
cannam@62 997
cannam@62 998 transform @1 :List(Op);
cannam@62 999 # Operations / transformations to apply to the result in order to get the capability actually
cannam@62 1000 # being addressed. E.g. if the result is a struct and you want to call a method on a capability
cannam@62 1001 # pointed to by a field of the struct, you need a `getPointerField` op.
cannam@62 1002
cannam@62 1003 struct Op {
cannam@62 1004 union {
cannam@62 1005 noop @0 :Void;
cannam@62 1006 # Does nothing. This member is mostly defined so that we can make `Op` a union even
cannam@62 1007 # though (as of this writing) only one real operation is defined.
cannam@62 1008
cannam@62 1009 getPointerField @1 :UInt16;
cannam@62 1010 # Get a pointer field within a struct. The number is an index into the pointer section, NOT
cannam@62 1011 # a field ordinal, so that the receiver does not need to understand the schema.
cannam@62 1012
cannam@62 1013 # TODO(someday): We could add:
cannam@62 1014 # - For lists, the ability to address every member of the list, or a slice of the list, the
cannam@62 1015 # result of which would be another list. This is useful for implementing the equivalent of
cannam@62 1016 # a SQL table join (not to be confused with the `Join` message type).
cannam@62 1017 # - Maybe some ability to test a union.
cannam@62 1018 # - Probably not a good idea: the ability to specify an arbitrary script to run on the
cannam@62 1019 # result. We could define a little stack-based language where `Op` specifies one
cannam@62 1020 # "instruction" or transformation to apply. Although this is not a good idea
cannam@62 1021 # (over-engineered), any narrower additions to `Op` should be designed as if this
cannam@62 1022 # were the eventual goal.
cannam@62 1023 }
cannam@62 1024 }
cannam@62 1025 }
cannam@62 1026
cannam@62 1027 struct ThirdPartyCapDescriptor {
cannam@62 1028 # **(level 3)**
cannam@62 1029 #
cannam@62 1030 # Identifies a capability in a third-party vat that the sender wants the receiver to pick up.
cannam@62 1031
cannam@62 1032 id @0 :ThirdPartyCapId;
cannam@62 1033 # Identifies the third-party host and the specific capability to accept from it.
cannam@62 1034
cannam@62 1035 vineId @1 :ExportId;
cannam@62 1036 # A proxy for the third-party object exported by the sender. In CapTP terminology this is called
cannam@62 1037 # a "vine", because it is an indirect reference to the third-party object that snakes through the
cannam@62 1038 # sender vat. This serves two purposes:
cannam@62 1039 #
cannam@62 1040 # * Level 1 and 2 implementations that don't understand how to connect to a third party may
cannam@62 1041 # simply send calls to the vine. Such calls will be forwarded to the third-party by the
cannam@62 1042 # sender.
cannam@62 1043 #
cannam@62 1044 # * Level 3 implementations must release the vine once they have successfully picked up the
cannam@62 1045 # object from the third party. This ensures that the capability is not released by the sender
cannam@62 1046 # prematurely.
cannam@62 1047 #
cannam@62 1048 # The sender will close the `Provide` request that it has sent to the third party as soon as
cannam@62 1049 # it receives either a `Call` or a `Release` message directed at the vine.
cannam@62 1050 }
cannam@62 1051
cannam@62 1052 struct Exception {
cannam@62 1053 # **(level 0)**
cannam@62 1054 #
cannam@62 1055 # Describes an arbitrary error that prevented an operation (e.g. a call) from completing.
cannam@62 1056 #
cannam@62 1057 # Cap'n Proto exceptions always indicate that something went wrong. In other words, in a fantasy
cannam@62 1058 # world where everything always works as expected, no exceptions would ever be thrown. Clients
cannam@62 1059 # should only ever catch exceptions as a means to implement fault-tolerance, where "fault" can
cannam@62 1060 # mean:
cannam@62 1061 # - Bugs.
cannam@62 1062 # - Invalid input.
cannam@62 1063 # - Configuration errors.
cannam@62 1064 # - Network problems.
cannam@62 1065 # - Insufficient resources.
cannam@62 1066 # - Version skew (unimplemented functionality).
cannam@62 1067 # - Other logistical problems.
cannam@62 1068 #
cannam@62 1069 # Exceptions should NOT be used to flag application-specific conditions that a client is expected
cannam@62 1070 # to handle in an application-specific way. Put another way, in the Cap'n Proto world,
cannam@62 1071 # "checked exceptions" (where an interface explicitly defines the exceptions it throws and
cannam@62 1072 # clients are forced by the type system to handle those exceptions) do NOT make sense.
cannam@62 1073
cannam@62 1074 reason @0 :Text;
cannam@62 1075 # Human-readable failure description.
cannam@62 1076
cannam@62 1077 type @3 :Type;
cannam@62 1078 # The type of the error. The purpose of this enum is not to describe the error itself, but
cannam@62 1079 # rather to describe how the client might want to respond to the error.
cannam@62 1080
cannam@62 1081 enum Type {
cannam@62 1082 failed @0;
cannam@62 1083 # A generic problem occurred, and it is believed that if the operation were repeated without
cannam@62 1084 # any change in the state of the world, the problem would occur again.
cannam@62 1085 #
cannam@62 1086 # A client might respond to this error by logging it for investigation by the developer and/or
cannam@62 1087 # displaying it to the user.
cannam@62 1088
cannam@62 1089 overloaded @1;
cannam@62 1090 # The request was rejected due to a temporary lack of resources.
cannam@62 1091 #
cannam@62 1092 # Examples include:
cannam@62 1093 # - There's not enough CPU time to keep up with incoming requests, so some are rejected.
cannam@62 1094 # - The server ran out of RAM or disk space during the request.
cannam@62 1095 # - The operation timed out (took significantly longer than it should have).
cannam@62 1096 #
cannam@62 1097 # A client might respond to this error by scheduling to retry the operation much later. The
cannam@62 1098 # client should NOT retry again immediately since this would likely exacerbate the problem.
cannam@62 1099
cannam@62 1100 disconnected @2;
cannam@62 1101 # The method failed because a connection to some necessary capability was lost.
cannam@62 1102 #
cannam@62 1103 # Examples include:
cannam@62 1104 # - The client introduced the server to a third-party capability, the connection to that third
cannam@62 1105 # party was subsequently lost, and then the client requested that the server use the dead
cannam@62 1106 # capability for something.
cannam@62 1107 # - The client previously requested that the server obtain a capability from some third party.
cannam@62 1108 # The server returned a capability to an object wrapping the third-party capability. Later,
cannam@62 1109 # the server's connection to the third party was lost.
cannam@62 1110 # - The capability has been revoked. Revocation does not necessarily mean that the client is
cannam@62 1111 # no longer authorized to use the capability; it is often used simply as a way to force the
cannam@62 1112 # client to repeat the setup process, perhaps to efficiently move them to a new back-end or
cannam@62 1113 # get them to recognize some other change that has occurred.
cannam@62 1114 #
cannam@62 1115 # A client should normally respond to this error by releasing all capabilities it is currently
cannam@62 1116 # holding related to the one it called and then re-creating them by restoring SturdyRefs and/or
cannam@62 1117 # repeating the method calls used to create them originally. In other words, disconnect and
cannam@62 1118 # start over. This should in turn cause the server to obtain a new copy of the capability that
cannam@62 1119 # it lost, thus making everything work.
cannam@62 1120 #
cannam@62 1121 # If the client receives another `disconnencted` error in the process of rebuilding the
cannam@62 1122 # capability and retrying the call, it should treat this as an `overloaded` error: the network
cannam@62 1123 # is currently unreliable, possibly due to load or other temporary issues.
cannam@62 1124
cannam@62 1125 unimplemented @3;
cannam@62 1126 # The server doesn't implement the requested method. If there is some other method that the
cannam@62 1127 # client could call (perhaps an older and/or slower interface), it should try that instead.
cannam@62 1128 # Otherwise, this should be treated like `failed`.
cannam@62 1129 }
cannam@62 1130
cannam@62 1131 obsoleteIsCallersFault @1 :Bool;
cannam@62 1132 # OBSOLETE. Ignore.
cannam@62 1133
cannam@62 1134 obsoleteDurability @2 :UInt16;
cannam@62 1135 # OBSOLETE. See `type` instead.
cannam@62 1136 }
cannam@62 1137
cannam@62 1138 # ========================================================================================
cannam@62 1139 # Network-specific Parameters
cannam@62 1140 #
cannam@62 1141 # Some parts of the Cap'n Proto RPC protocol are not specified here because different vat networks
cannam@62 1142 # may wish to use different approaches to solving them. For example, on the public internet, you
cannam@62 1143 # may want to authenticate vats using public-key cryptography, but on a local intranet with trusted
cannam@62 1144 # infrastructure, you may be happy to authenticate based on network address only, or some other
cannam@62 1145 # lightweight mechanism.
cannam@62 1146 #
cannam@62 1147 # To accommodate this, we specify several "parameter" types. Each type is defined here as an
cannam@62 1148 # alias for `AnyPointer`, but a specific network will want to define a specific set of types to use.
cannam@62 1149 # All vats in a vat network must agree on these parameters in order to be able to communicate.
cannam@62 1150 # Inter-network communication can be accomplished through "gateways" that perform translation
cannam@62 1151 # between the primitives used on each network; these gateways may need to be deeply stateful,
cannam@62 1152 # depending on the translations they perform.
cannam@62 1153 #
cannam@62 1154 # For interaction over the global internet between parties with no other prior arrangement, a
cannam@62 1155 # particular set of bindings for these types is defined elsewhere. (TODO(someday): Specify where
cannam@62 1156 # these common definitions live.)
cannam@62 1157 #
cannam@62 1158 # Another common network type is the two-party network, in which one of the parties typically
cannam@62 1159 # interacts with the outside world entirely through the other party. In such a connection between
cannam@62 1160 # Alice and Bob, all objects that exist on Bob's other networks appear to Alice as if they were
cannam@62 1161 # hosted by Bob himself, and similarly all objects on Alice's network (if she even has one) appear
cannam@62 1162 # to Bob as if they were hosted by Alice. This network type is interesting because from the point
cannam@62 1163 # of view of a simple application that communicates with only one other party via the two-party
cannam@62 1164 # protocol, there are no three-party interactions at all, and joins are unusually simple to
cannam@62 1165 # implement, so implementing at level 4 is barely more complicated than implementing at level 1.
cannam@62 1166 # Moreover, if you pair an app implementing the two-party network with a container that implements
cannam@62 1167 # some other network, the app can then participate on the container's network just as if it
cannam@62 1168 # implemented that network directly. The types used by the two-party network are defined in
cannam@62 1169 # `rpc-twoparty.capnp`.
cannam@62 1170 #
cannam@62 1171 # The things that we need to parameterize are:
cannam@62 1172 # - How to store capabilities long-term without holding a connection open (mostly level 2).
cannam@62 1173 # - How to authenticate vats in three-party introductions (level 3).
cannam@62 1174 # - How to implement `Join` (level 4).
cannam@62 1175 #
cannam@62 1176 # Persistent references
cannam@62 1177 # ---------------------
cannam@62 1178 #
cannam@62 1179 # **(mostly level 2)**
cannam@62 1180 #
cannam@62 1181 # We want to allow some capabilities to be stored long-term, even if a connection is lost and later
cannam@62 1182 # recreated. ExportId is a short-term identifier that is specific to a connection, so it doesn't
cannam@62 1183 # help here. We need a way to specify long-term identifiers, as well as a strategy for
cannam@62 1184 # reconnecting to a referenced capability later.
cannam@62 1185 #
cannam@62 1186 # Three-party interactions
cannam@62 1187 # ------------------------
cannam@62 1188 #
cannam@62 1189 # **(level 3)**
cannam@62 1190 #
cannam@62 1191 # In cases where more than two vats are interacting, we have situations where VatA holds a
cannam@62 1192 # capability hosted by VatB and wants to send that capability to VatC. This can be accomplished
cannam@62 1193 # by VatA proxying requests on the new capability, but doing so has two big problems:
cannam@62 1194 # - It's inefficient, requiring an extra network hop.
cannam@62 1195 # - If VatC receives another capability to the same object from VatD, it is difficult for VatC to
cannam@62 1196 # detect that the two capabilities are really the same and to implement the E "join" operation,
cannam@62 1197 # which is necessary for certain four-or-more-party interactions, such as the escrow pattern.
cannam@62 1198 # See: http://www.erights.org/elib/equality/grant-matcher/index.html
cannam@62 1199 #
cannam@62 1200 # Instead, we want a way for VatC to form a direct, authenticated connection to VatB.
cannam@62 1201 #
cannam@62 1202 # Join
cannam@62 1203 # ----
cannam@62 1204 #
cannam@62 1205 # **(level 4)**
cannam@62 1206 #
cannam@62 1207 # The `Join` message type and corresponding operation arranges for a direct connection to be formed
cannam@62 1208 # between the joiner and the host of the joined object, and this connection must be authenticated.
cannam@62 1209 # Thus, the details are network-dependent.
cannam@62 1210
cannam@62 1211 using SturdyRef = AnyPointer;
cannam@62 1212 # **(level 2)**
cannam@62 1213 #
cannam@62 1214 # Identifies a persisted capability that can be restored in the future. How exactly a SturdyRef
cannam@62 1215 # is restored to a live object is specified along with the SturdyRef definition (i.e. not by
cannam@62 1216 # rpc.capnp).
cannam@62 1217 #
cannam@62 1218 # Generally a SturdyRef needs to specify three things:
cannam@62 1219 # - How to reach the vat that can restore the ref (e.g. a hostname or IP address).
cannam@62 1220 # - How to authenticate the vat after connecting (e.g. a public key fingerprint).
cannam@62 1221 # - The identity of a specific object hosted by the vat. Generally, this is an opaque pointer whose
cannam@62 1222 # format is defined by the specific vat -- the client has no need to inspect the object ID.
cannam@62 1223 # It is important that the objec ID be unguessable if the object is not public (and objects
cannam@62 1224 # should almost never be public).
cannam@62 1225 #
cannam@62 1226 # The above are only suggestions. Some networks might work differently. For example, a private
cannam@62 1227 # network might employ a special restorer service whose sole purpose is to restore SturdyRefs.
cannam@62 1228 # In this case, the entire contents of SturdyRef might be opaque, because they are intended only
cannam@62 1229 # to be forwarded to the restorer service.
cannam@62 1230
cannam@62 1231 using ProvisionId = AnyPointer;
cannam@62 1232 # **(level 3)**
cannam@62 1233 #
cannam@62 1234 # The information that must be sent in an `Accept` message to identify the object being accepted.
cannam@62 1235 #
cannam@62 1236 # In a network where each vat has a public/private key pair, this could simply be the public key
cannam@62 1237 # fingerprint of the provider vat along with the question ID used in the `Provide` message sent from
cannam@62 1238 # that provider.
cannam@62 1239
cannam@62 1240 using RecipientId = AnyPointer;
cannam@62 1241 # **(level 3)**
cannam@62 1242 #
cannam@62 1243 # The information that must be sent in a `Provide` message to identify the recipient of the
cannam@62 1244 # capability.
cannam@62 1245 #
cannam@62 1246 # In a network where each vat has a public/private key pair, this could simply be the public key
cannam@62 1247 # fingerprint of the recipient. (CapTP also calls for a nonce to identify the object. In our
cannam@62 1248 # case, the `Provide` message's `questionId` can serve as the nonce.)
cannam@62 1249
cannam@62 1250 using ThirdPartyCapId = AnyPointer;
cannam@62 1251 # **(level 3)**
cannam@62 1252 #
cannam@62 1253 # The information needed to connect to a third party and accept a capability from it.
cannam@62 1254 #
cannam@62 1255 # In a network where each vat has a public/private key pair, this could be a combination of the
cannam@62 1256 # third party's public key fingerprint, hints on how to connect to the third party (e.g. an IP
cannam@62 1257 # address), and the question ID used in the corresponding `Provide` message sent to that third party
cannam@62 1258 # (used to identify which capability to pick up).
cannam@62 1259
cannam@62 1260 using JoinKeyPart = AnyPointer;
cannam@62 1261 # **(level 4)**
cannam@62 1262 #
cannam@62 1263 # A piece of a secret key. One piece is sent along each path that is expected to lead to the same
cannam@62 1264 # place. Once the pieces are combined, a direct connection may be formed between the sender and
cannam@62 1265 # the receiver, bypassing any men-in-the-middle along the paths. See the `Join` message type.
cannam@62 1266 #
cannam@62 1267 # The motivation for Joins is discussed under "Supporting Equality" in the "Unibus" protocol
cannam@62 1268 # sketch: http://www.erights.org/elib/distrib/captp/unibus.html
cannam@62 1269 #
cannam@62 1270 # In a network where each vat has a public/private key pair and each vat forms no more than one
cannam@62 1271 # connection to each other vat, Joins will rarely -- perhaps never -- be needed, as objects never
cannam@62 1272 # need to be transparently proxied and references to the same object sent over the same connection
cannam@62 1273 # have the same export ID. Thus, a successful join requires only checking that the two objects
cannam@62 1274 # come from the same connection and have the same ID, and then completes immediately.
cannam@62 1275 #
cannam@62 1276 # However, in networks where two vats may form more than one connection between each other, or
cannam@62 1277 # where proxying of objects occurs, joins are necessary.
cannam@62 1278 #
cannam@62 1279 # Typically, each JoinKeyPart would include a fixed-length data value such that all value parts
cannam@62 1280 # XOR'd together forms a shared secret that can be used to form an encrypted connection between
cannam@62 1281 # the joiner and the joined object's host. Each JoinKeyPart should also include an indication of
cannam@62 1282 # how many parts to expect and a hash of the shared secret (used to match up parts).
cannam@62 1283
cannam@62 1284 using JoinResult = AnyPointer;
cannam@62 1285 # **(level 4)**
cannam@62 1286 #
cannam@62 1287 # Information returned as the result to a `Join` message, needed by the joiner in order to form a
cannam@62 1288 # direct connection to a joined object. This might simply be the address of the joined object's
cannam@62 1289 # host vat, since the `JoinKey` has already been communicated so the two vats already have a shared
cannam@62 1290 # secret to use to authenticate each other.
cannam@62 1291 #
cannam@62 1292 # The `JoinResult` should also contain information that can be used to detect when the Join
cannam@62 1293 # requests ended up reaching different objects, so that this situation can be detected easily.
cannam@62 1294 # This could be a simple matter of including a sequence number -- if the joiner receives two
cannam@62 1295 # `JoinResult`s with sequence number 0, then they must have come from different objects and the
cannam@62 1296 # whole join is a failure.
cannam@62 1297
cannam@62 1298 # ========================================================================================
cannam@62 1299 # Network interface sketch
cannam@62 1300 #
cannam@62 1301 # The interfaces below are meant to be pseudo-code to illustrate how the details of a particular
cannam@62 1302 # vat network might be abstracted away. They are written like Cap'n Proto interfaces, but in
cannam@62 1303 # practice you'd probably define these interfaces manually in the target programming language. A
cannam@62 1304 # Cap'n Proto RPC implementation should be able to use these interfaces without knowing the
cannam@62 1305 # definitions of the various network-specific parameters defined above.
cannam@62 1306
cannam@62 1307 # interface VatNetwork {
cannam@62 1308 # # Represents a vat network, with the ability to connect to particular vats and receive
cannam@62 1309 # # connections from vats.
cannam@62 1310 # #
cannam@62 1311 # # Note that methods returning a `Connection` may return a pre-existing `Connection`, and the
cannam@62 1312 # # caller is expected to find and share state with existing users of the connection.
cannam@62 1313 #
cannam@62 1314 # # Level 0 features -----------------------------------------------
cannam@62 1315 #
cannam@62 1316 # connect(vatId :VatId) :Connection;
cannam@62 1317 # # Connect to the given vat. The transport should return a promise that does not
cannam@62 1318 # # resolve until authentication has completed, but allows messages to be pipelined in before
cannam@62 1319 # # that; the transport either queues these messages until authenticated, or sends them encrypted
cannam@62 1320 # # such that only the authentic vat would be able to decrypt them. The latter approach avoids a
cannam@62 1321 # # round trip for authentication.
cannam@62 1322 #
cannam@62 1323 # accept() :Connection;
cannam@62 1324 # # Wait for the next incoming connection and return it. Only connections formed by
cannam@62 1325 # # connect() are returned by this method.
cannam@62 1326 #
cannam@62 1327 # # Level 4 features -----------------------------------------------
cannam@62 1328 #
cannam@62 1329 # newJoiner(count :UInt32) :NewJoinerResponse;
cannam@62 1330 # # Prepare a new Join operation, which will eventually lead to forming a new direct connection
cannam@62 1331 # # to the host of the joined capability. `count` is the number of capabilities to join.
cannam@62 1332 #
cannam@62 1333 # struct NewJoinerResponse {
cannam@62 1334 # joinKeyParts :List(JoinKeyPart);
cannam@62 1335 # # Key parts to send in Join messages to each capability.
cannam@62 1336 #
cannam@62 1337 # joiner :Joiner;
cannam@62 1338 # # Used to establish the final connection.
cannam@62 1339 # }
cannam@62 1340 #
cannam@62 1341 # interface Joiner {
cannam@62 1342 # addJoinResult(result :JoinResult) :Void;
cannam@62 1343 # # Add a JoinResult received in response to one of the `Join` messages. All `JoinResult`s
cannam@62 1344 # # returned from all paths must be added before trying to connect.
cannam@62 1345 #
cannam@62 1346 # connect() :ConnectionAndProvisionId;
cannam@62 1347 # # Try to form a connection to the joined capability's host, verifying that it has received
cannam@62 1348 # # all of the JoinKeyParts. Once the connection is formed, the caller should send an `Accept`
cannam@62 1349 # # message on it with the specified `ProvisionId` in order to receive the final capability.
cannam@62 1350 # }
cannam@62 1351 #
cannam@62 1352 # acceptConnectionFromJoiner(parts :List(JoinKeyPart), paths :List(VatPath))
cannam@62 1353 # :ConnectionAndProvisionId;
cannam@62 1354 # # Called on a joined capability's host to receive the connection from the joiner, once all
cannam@62 1355 # # key parts have arrived. The caller should expect to receive an `Accept` message over the
cannam@62 1356 # # connection with the given ProvisionId.
cannam@62 1357 # }
cannam@62 1358 #
cannam@62 1359 # interface Connection {
cannam@62 1360 # # Level 0 features -----------------------------------------------
cannam@62 1361 #
cannam@62 1362 # send(message :Message) :Void;
cannam@62 1363 # # Send the message. Returns successfully when the message (and all preceding messages) has
cannam@62 1364 # # been acknowledged by the recipient.
cannam@62 1365 #
cannam@62 1366 # receive() :Message;
cannam@62 1367 # # Receive the next message, and acknowledges receipt to the sender. Messages are received in
cannam@62 1368 # # the order in which they are sent.
cannam@62 1369 #
cannam@62 1370 # # Level 3 features -----------------------------------------------
cannam@62 1371 #
cannam@62 1372 # introduceTo(recipient :Connection) :IntroductionInfo;
cannam@62 1373 # # Call before starting a three-way introduction, assuming a `Provide` message is to be sent on
cannam@62 1374 # # this connection and a `ThirdPartyCapId` is to be sent to `recipient`.
cannam@62 1375 #
cannam@62 1376 # struct IntroductionInfo {
cannam@62 1377 # sendToRecipient :ThirdPartyCapId;
cannam@62 1378 # sendToTarget :RecipientId;
cannam@62 1379 # }
cannam@62 1380 #
cannam@62 1381 # connectToIntroduced(capId :ThirdPartyCapId) :ConnectionAndProvisionId;
cannam@62 1382 # # Given a ThirdPartyCapId received over this connection, connect to the third party. The
cannam@62 1383 # # caller should then send an `Accept` message over the new connection.
cannam@62 1384 #
cannam@62 1385 # acceptIntroducedConnection(recipientId :RecipientId) :Connection;
cannam@62 1386 # # Given a RecipientId received in a `Provide` message on this `Connection`, wait for the
cannam@62 1387 # # recipient to connect, and return the connection formed. Usually, the first message received
cannam@62 1388 # # on the new connection will be an `Accept` message.
cannam@62 1389 # }
cannam@62 1390 #
cannam@62 1391 # struct ConnectionAndProvisionId {
cannam@62 1392 # # **(level 3)**
cannam@62 1393 #
cannam@62 1394 # connection :Connection;
cannam@62 1395 # # Connection on which to issue `Accept` message.
cannam@62 1396 #
cannam@62 1397 # provision :ProvisionId;
cannam@62 1398 # # `ProvisionId` to send in the `Accept` message.
cannam@62 1399 # }