shrub/pkg/arvo/sys/vane/alef.hoon

2933 lines
90 KiB
Plaintext
Raw Normal View History

2019-06-29 01:27:59 +03:00
:: Ames extends Arvo's %pass/%give move semantics across the network.
::
:: A "forward flow" message, which is like a request, is passed to
:: Ames from a local vane. Ames transmits the message to the peer's
:: Ames, which passes the message to the destination vane.
::
:: Once the peer has processed the "forward flow" message, it sends a
:: message acknowledgment over the wire back to the local Ames. This
:: ack can either be positive or negative, in which case we call it a
:: "nack". (Don't confuse Ames nacks with TCP nacks, which are a
:: different concept).
::
:: When the local Ames receives either a positive message ack or a
:: combination of a nack and nack-trace (explained in more detail
:: below), it gives an %done move to the local vane that had
:: requested the original "forward flow" message be sent.
::
:: A "backward flow" message, which is similar to a response or a
:: subscription update, is given to Ames from a local vane. Ames
:: transmits the message to the peer's Ames, which gives the message
:: to the destination vane.
::
:: Ames will give a %memo to a vane upon hearing the message from a
:: remote. This message is a "backward flow" message, forming one of
:: potentially many responses to a "forward flow" message that a
:: local vane had passed to our local Ames, and which local Ames had
:: relayed to the remote. Ames gives the %memo on the same duct the
:: local vane had originally used to pass Ames the "forward flow"
:: message.
::
:: Backward flow messages are acked automatically by the receiver.
:: They cannot be nacked, and Ames only uses the ack internally,
:: without notifying the client vane.
::
:: Forward flow messages can be nacked, in which case the peer will
:: send both a message-nack packet and a nack-trace message, which is
:: sent on a special diagnostic flow so as not to interfere with
:: normal operation. The nack-trace is sent as a full Ames message,
:: instead of just a packet, because the contained error information
:: can be arbitrarily large.
::
:: Once the local Ames has received the nack-trace, it knows the peer
:: has received the full message and failed to process it. This
:: means if we later hear an ack packet on the failed message, we can
:: ignore it.
::
:: Also, due to Ames's exactly-once delivery semantics, we know that
:: when we receive a nack-trace for message n, we know the peer has
:: positively acked all messages m+1 through n-1, where m is the last
:: message for which we heard a nack-trace. If we haven't heard acks
:: on all those messages, we apply positive acks when we hear the
:: nack-trace.
::
2019-05-25 08:53:29 +03:00
:: protocol-version: current version of the ames wire protocol
::
2019-06-29 02:43:52 +03:00
!:
2019-05-25 05:03:33 +03:00
=/ protocol-version=?(%0 %1 %2 %3 %4 %5 %6 %7) %0
2019-06-29 02:43:52 +03:00
=, ames
=, able
=* point point:able:jael
=* public-keys-result public-keys-result:able:jael
2019-11-15 03:10:48 +03:00
=>
=/ veb
:* pak=%.n
snd=%.n
rcv=%.n
odd=%.n
msg=%.y
==
|%
++ trace
|= [verb=? print=(trap tape)]
?. verb
same
(slog leaf/"ames: {(print)}" ~)
--
2019-06-29 02:43:52 +03:00
=>
2019-05-25 05:03:33 +03:00
|%
2019-05-27 02:54:23 +03:00
+| %generics
:: $mk-item: constructor for +ordered-map item type
2019-05-27 02:54:23 +03:00
::
2019-09-11 03:54:35 +03:00
++ mk-item |$ [key val] [key=key val=val]
:: +ordered-map: treap with user-specified horizontal order
::
:: Conceptually smaller items go on the left, so the item with the
:: smallest key can be popped off the head. If $key is `@` and
:: .compare is +lte, then the numerically smallest item is the head.
::
++ ordered-map
|* [key=mold val=mold]
=> |%
+$ item (mk-item key val)
--
:: +compare: item comparator for horizontal order
::
|= compare=$-([key key] ?)
|%
:: +check-balance: verify horizontal and vertical orderings
::
++ check-balance
=| [l=(unit key) r=(unit key)]
|= a=(tree item)
^- ?
:: empty tree is valid
::
?~ a %.y
:: nonempty trees must maintain several criteria
::
?& :: if .n.a is left of .u.l, assert horizontal comparator
::
?~(l %.y (compare key.n.a u.l))
:: if .n.a is right of .u.r, assert horizontal comparator
::
?~(r %.y (compare u.r key.n.a))
:: if .a is not leftmost element, assert vertical order between
:: .l.a and .n.a and recurse to the left with .n.a as right
:: neighbor
::
?~(l.a %.y &((mor key.n.a key.n.l.a) $(a l.a, l `key.n.a)))
:: if .a is not rightmost element, assert vertical order
:: between .r.a and .n.a and recurse to the right with .n.a as
:: left neighbor
::
?~(r.a %.y &((mor key.n.a key.n.r.a) $(a r.a, r `key.n.a)))
==
:: +put: ordered item insert
::
++ put
|= [a=(tree item) =key =val]
^- (tree item)
:: base case: replace null with single-item tree
::
?~ a [n=[key val] l=~ r=~]
:: base case: overwrite existing .key with new .val
::
?: =(key.n.a key) a(val.n val)
:: if item goes on left, recurse left then rebalance vertical order
::
?: (compare key key.n.a)
=/ l $(a l.a)
?> ?=(^ l)
?: (mor key.n.a key.n.l)
a(l l)
l(r a(l r.l))
:: item goes on right; recurse right then rebalance vertical order
::
=/ r $(a r.a)
?> ?=(^ r)
?: (mor key.n.a key.n.r)
a(r r)
r(l a(r l.r))
:: +peek: produce head (smallest item) or null
::
++ peek
|= a=(tree item)
^- (unit item)
::
?~ a ~
?~ l.a `n.a
$(a l.a)
:: +pop: produce .head (smallest item) and .rest or crash if empty
::
++ pop
|= a=(tree item)
^- [head=item rest=(tree item)]
::
?~ a !!
?~ l.a [n.a r.a]
::
=/ l $(a l.a)
:- head.l
:: load .rest.l back into .a and rebalance
::
?: |(?=(~ rest.l) (mor key.n.a key.n.rest.l))
a(l rest.l)
rest.l(r a(r r.rest.l))
:: +del: delete .key from .a if it exists, producing value iff deleted
::
++ del
|= [a=(tree item) =key]
^- [(unit val) (tree item)]
::
?~ a [~ ~]
:: we found .key at the root; delete and rebalance
::
?: =(key key.n.a)
[`val.n.a (nip a)]
:: recurse left or right to find .key
::
?: (compare key key.n.a)
=+ [found lef]=$(a l.a)
[found a(l lef)]
=+ [found rig]=$(a r.a)
[found a(r rig)]
:: +nip: remove root; for internal use
::
++ nip
|= a=(tree item)
^- (tree item)
::
?> ?=(^ a)
:: delete .n.a; merge and balance .l.a and .r.a
::
|- ^- (tree item)
?~ l.a r.a
?~ r.a l.a
?: (mor key.n.l.a key.n.r.a)
l.a(r $(l.a r.l.a))
r.a(l $(r.a l.r.a))
:: +traverse: stateful partial inorder traversal
::
:: Mutates .state on each run of .f. Starts at .start key, or if
:: .start is ~, starts at the head (item with smallest key). Stops
:: when .f produces .stop=%.y. Traverses from smaller to larger
:: keys. Each run of .f can replace an item's value or delete the
:: item.
::
++ traverse
2019-05-31 20:59:45 +03:00
|* state=mold
|= $: a=(tree item)
=state
f=$-([state item] [(unit val) ? state])
2019-05-31 20:59:45 +03:00
==
^+ [state a]
2019-05-31 20:59:45 +03:00
:: acc: accumulator
::
:: .stop: set to %.y by .f when done traversing
:: .state: threaded through each run of .f and produced by +abet
::
2019-05-31 20:59:45 +03:00
=/ acc [stop=`?`%.n state=state]
=< abet =< main
|%
2019-05-31 20:59:45 +03:00
++ abet [state.acc a]
:: +main: main recursive loop; performs a partial inorder traversal
::
++ main
^+ .
:: stop if empty or we've been told to stop
::
?~ a .
2019-05-31 20:59:45 +03:00
?: stop.acc .
2019-06-01 09:57:57 +03:00
:: inorder traversal: left -> node -> right, until .f sets .stop
::
=> left
2019-06-01 09:57:57 +03:00
?: stop.acc .
=> node
?: stop.acc .
right
:: +node: run .f on .n.a, updating .a, .state, and .stop
::
++ node
^+ .
:: run .f on node, updating .stop.acc and .state.acc
::
2019-06-01 09:57:57 +03:00
=^ res acc
?> ?=(^ a)
(f state.acc n.a)
:: apply update to .a from .f's product
::
=. a
:: if .f requested node deletion, merge and balance .l.a and .r.a
::
?~ res (nip a)
:: we kept the node; replace its .val; order is unchanged
::
?> ?=(^ a)
a(val.n u.res)
::
2019-06-01 09:57:57 +03:00
..node
2019-06-18 02:23:32 +03:00
:: +left: recurse on left subtree, copying mutant back into .l.a
::
++ left
^+ .
?~ a .
=/ lef main(a l.a)
lef(a a(l a.lef))
2019-06-18 02:23:32 +03:00
:: +right: recurse on right subtree, copying mutant back into .r.a
::
++ right
^+ .
?~ a .
=/ rig main(a r.a)
rig(a a(r a.rig))
--
:: +tap: convert to list, smallest to largest
::
++ tap
|= a=(tree item)
^- (list item)
::
=| b=(list item)
|- ^+ b
?~ a b
::
$(a l.a, b [n.a $(a r.a)])
:: +gas: put a list of items
::
++ gas
|= [a=(tree item) b=(list item)]
^- (tree item)
::
?~ b a
$(b t.b, a (put a i.b))
:: +uni: unify two ordered maps
::
2019-06-25 19:00:03 +03:00
:: .b takes precedence over .a if keys overlap.
2019-06-01 06:05:21 +03:00
::
++ uni
|= [a=(tree item) b=(tree item)]
^- (tree item)
::
?~ b a
?~ a b
?: =(key.n.a key.n.b)
2019-06-01 06:05:21 +03:00
::
[n=n.b l=$(a l.a, b l.b) r=$(a r.a, b r.b)]
::
?: (mor key.n.a key.n.b)
2019-06-01 06:05:21 +03:00
::
?: (compare key.n.b key.n.a)
$(l.a $(a l.a, r.b ~), b r.b)
$(r.a $(a r.a, l.b ~), b l.b)
::
?: (compare key.n.a key.n.b)
$(l.b $(b l.b, r.a ~), a r.a)
$(r.b $(b r.b, l.a ~), a l.a)
--
2019-05-27 02:54:23 +03:00
::
2019-05-25 08:53:29 +03:00
+| %atomics
2019-05-25 05:03:33 +03:00
::
2019-05-25 08:53:29 +03:00
+$ bone @udbone
2019-05-27 02:54:23 +03:00
+$ fragment @uwfragment
2019-05-27 04:48:41 +03:00
+$ fragment-num @udfragmentnum
2019-07-28 10:50:32 +03:00
+$ message-blob @udmessageblob
2019-05-27 02:54:23 +03:00
+$ message-num @udmessagenum
+$ private-key @uwprivatekey
2019-05-25 08:53:29 +03:00
+$ public-key @uwpublickey
2019-05-27 02:54:23 +03:00
+$ signature @uwsignature
2019-05-25 08:53:29 +03:00
+$ symmetric-key @uwsymmetrickey
:: $rank: which kind of ship address, by length
::
:: 0: galaxy or star -- 2 bytes
:: 1: planet -- 4 bytes
:: 2: moon -- 8 bytes
:: 3: comet -- 16 bytes
2019-05-25 05:03:33 +03:00
::
+$ rank ?(%0 %1 %2 %3)
2019-05-25 08:53:29 +03:00
::
2019-05-27 06:22:38 +03:00
+| %kinetics
2019-05-27 04:48:41 +03:00
:: $channel: combined sender and receiver identifying data
2019-05-25 08:53:29 +03:00
::
2019-05-27 04:48:41 +03:00
+$ channel
2019-05-27 04:52:31 +03:00
$: [our=ship her=ship]
now=@da
2019-05-27 04:48:41 +03:00
:: our data, common to all dyads
::
$: =our=life
crypto-core=acru:ames
==
:: her data, specific to this dyad
::
$: =symmetric-key
=her=life
=her=public-key
her-sponsor=ship
2019-05-27 04:48:41 +03:00
== ==
2019-05-25 08:53:29 +03:00
:: $dyad: pair of sender and receiver ships
::
+$ dyad [sndr=ship rcvr=ship]
:: $packet: noun representation of an ames datagram packet
::
:: Roundtrips losslessly through atom encoding and decoding.
::
:: .origin is ~ unless the packet is being forwarded. If present,
:: it's an atom that encodes a route to another ship, such as an IPv4
:: address. Routes are opaque to Arvo and only have meaning in the
:: interpreter. This enforces that Ames is transport-agnostic.
::
+$ packet [dyad encrypted=? origin=(unit lane) content=*]
2019-05-27 02:54:23 +03:00
:: $open-packet: unencrypted packet payload, for comet self-attestation
::
2019-06-18 02:23:32 +03:00
:: The .signature applies to all other fields in this data structure.
::
2019-05-27 02:54:23 +03:00
+$ open-packet
$: =signature
2019-06-18 02:23:32 +03:00
=public-key
sndr=ship
2019-05-27 02:54:23 +03:00
=sndr=life
rcvr=ship
2019-06-18 02:23:32 +03:00
=rcvr=life
2019-05-27 02:54:23 +03:00
==
:: $shut-packet: encrypted packet payload
::
+$ shut-packet
$: =sndr=life
=rcvr=life
=bone
=message-num
meat=(each fragment-meat ack-meat)
==
:: $fragment-meat: contents of a message-fragment packet
::
+$ fragment-meat
$: num-fragments=fragment-num
=fragment-num
=fragment
==
:: $ack-meat: contents of an acknowledgment packet; fragment or message
::
:: Fragment acks reference the $fragment-num of the target packet.
::
:: Message acks contain a success flag .ok, which is %.n in case of
:: negative acknowledgment (nack), along with .lag that describes the
:: time it took to process the message. .lag is zero if the message
:: was processed during a single Arvo event. At the moment, .lag is
:: always zero.
::
+$ ack-meat (each fragment-num [ok=? lag=@dr])
:: $naxplanation: nack trace; explains which message failed and why
::
+$ naxplanation [=message-num =error]
2019-05-25 08:53:29 +03:00
::
2019-05-27 04:52:31 +03:00
+| %statics
2019-05-25 08:53:29 +03:00
::
:: $ames-state: state for entire vane
::
+$ ames-state
$: peers=(map ship ship-state)
2019-06-08 13:13:38 +03:00
=unix=duct
2019-05-25 08:53:29 +03:00
=life
crypto-core=acru:ames
==
:: $ship-state: all we know about a peer
::
:: %alien: no PKI data, so enqueue actions to perform once we learn it
2019-05-28 06:15:17 +03:00
:: %known: we know their life and public keys, so we have a channel
2019-05-25 08:53:29 +03:00
::
+$ ship-state
$% [%alien alien-agenda]
2019-05-28 06:15:17 +03:00
[%known peer-state]
==
:: $alien-agenda: what to do when we learn a peer's life and keys
2019-05-28 06:15:17 +03:00
::
:: messages: pleas local vanes have asked us to send
:: packets: packets we've tried to send
2019-11-04 04:35:45 +03:00
:: heeds: local tracking requests; passed through into $peer-state
::
+$ alien-agenda
$: messages=(list [=duct =plea])
packets=(set =blob)
2019-11-04 04:35:45 +03:00
heeds=(set duct)
2019-05-25 08:53:29 +03:00
==
:: $peer-state: state for a peer with known life and keys
::
:: route: transport-layer destination for packets to peer
:: qos: quality of service; connection status to peer
:: ossuary: bone<->duct mapper
:: snd: per-bone message pumps to send messages as fragments
:: rcv: per-bone message sinks to assemble messages from fragments
:: nax: unprocessed nacks (negative acknowledgments)
:: Each value is ~ when we've received the ack packet but not a
:: nack-trace, or an error when we've received a nack-trace but
:: not the ack packet.
::
:: When we hear a nack packet or an explanation, if there's no
:: entry in .nax, we make a new entry. Otherwise, if this new
:: information completes the packet+nack-trace, we remove the
:: entry and emit a nack to the local vane that asked us to send
:: the message.
2019-11-04 04:35:45 +03:00
:: heeds: listeners for %clog notifications
::
2019-05-25 08:53:29 +03:00
+$ peer-state
$: $: =symmetric-key
=life
=public-key
sponsor=ship
2019-05-25 08:53:29 +03:00
==
route=(unit [direct=? =lane])
=qos
2019-05-25 08:53:29 +03:00
=ossuary
2019-05-27 02:54:23 +03:00
snd=(map bone message-pump-state)
rcv=(map bone message-sink-state)
2019-06-19 02:38:20 +03:00
nax=(set [=bone =message-num])
2019-11-04 04:35:45 +03:00
heeds=(set duct)
2019-05-25 08:53:29 +03:00
==
:: $qos: quality of service; how is our connection to a peer doing?
::
+$ qos
$~ [%unborn ~]
$% [%live last-contact=@da]
[%dead last-contact=@da]
[%unborn ~]
==
2019-05-27 04:38:19 +03:00
:: $ossuary: bone<->duct bijection and .next-bone to map to a duct
2019-05-25 08:53:29 +03:00
::
2019-06-19 02:42:58 +03:00
:: The first bone is 0. They increment by 4, since each flow includes
:: a bit for each message determining forward vs. backward and a
:: second bit for whether the message is on the normal flow or the
:: associated diagnostic flow (for nack-traces).
2019-06-11 23:46:20 +03:00
::
2019-05-25 08:53:29 +03:00
+$ ossuary
2019-06-19 02:42:58 +03:00
$: =next=bone
2019-05-25 08:53:29 +03:00
by-duct=(map duct bone)
by-bone=(map bone duct)
==
2019-05-27 06:22:38 +03:00
:: $message-pump-state: persistent state for |message-pump
::
2019-05-27 07:34:18 +03:00
:: Messages queue up in |message-pump's .unsent-messages until they
:: can be packetized and fed into |packet-pump for sending. When we
:: pop a message off .unsent-messages, we push as many fragments as
:: we can into |packet-pump, then place the remaining in
:: .unsent-fragments.
2019-05-27 07:34:18 +03:00
::
:: When we hear a packet ack, we send it to |packet-pump. If we
:: haven't seen it before, |packet-pump reports the fresh ack.
2019-05-27 07:34:18 +03:00
::
2019-05-29 05:56:05 +03:00
:: When we hear a message ack (positive or negative), we treat that
:: as though all fragments have been acked. If this message is not
:: .current, then it's a future message and .current has not yet been
:: acked, so we place the message in .queued-message-acks.
::
:: If we hear a message ack before we've sent all the
:: fragments for that message, clear .unsent-fragments. If the
:: message ack was positive, print it out because it indicates the
:: peer is not behaving properly.
::
:: If the ack is for the current message, emit the message ack,
:: increment .current, and check if this next message is in
:: .queued-message-acks. If it is, emit the message (n)ack,
:: increment .current, and check the next message. Repeat until
:: .current is not fully acked.
2019-05-27 07:34:18 +03:00
::
:: When we hear a message nack, we send it to |packet-pump, which
:: deletes all packets from that message. If .current gets nacked,
:: clear .unsent-fragments and go into the same flow as when we hear
:: the last packet ack on a message.
::
:: The following equation is always true:
:: .next - .current == number of messages in flight
::
2019-06-20 10:21:37 +03:00
:: At the end of a task, |message-pump sends a %halt task to
2019-05-27 07:44:11 +03:00
:: |packet-pump, which can trigger a timer to be set or cleared based
:: on congestion control calculations. When it fires, the timer will
:: generally cause one or more packets to be resent.
::
2019-06-11 23:46:20 +03:00
:: Message sequence numbers start at 1 so the first message will be
:: greater than .last-acked.message-sink-state on the receiver.
2019-06-11 23:46:20 +03:00
::
2019-06-21 04:36:16 +03:00
:: current: sequence number of earliest message sent or being sent
2019-05-27 07:34:18 +03:00
:: next: sequence number of next message to send
2019-05-27 06:22:38 +03:00
:: unsent-messages: messages to be sent after current message
:: unsent-fragments: fragments of current message waiting for sending
2019-05-29 05:56:05 +03:00
:: queued-message-acks: future message acks to be applied after current
2019-05-27 06:22:38 +03:00
:: packet-pump-state: state of corresponding |packet-pump
::
2019-05-27 02:54:23 +03:00
+$ message-pump-state
2019-06-20 03:33:40 +03:00
$: current=_`message-num`1
next=_`message-num`1
2019-07-28 10:50:32 +03:00
unsent-messages=(qeu message-blob)
2019-05-27 04:38:19 +03:00
unsent-fragments=(list static-fragment)
2019-05-29 05:56:05 +03:00
queued-message-acks=(map message-num ok=?)
2019-05-27 02:54:23 +03:00
=packet-pump-state
==
+$ static-fragment
$: =message-num
num-fragments=fragment-num
=fragment-num
=fragment
==
2019-05-27 06:22:38 +03:00
:: $packet-pump-state: persistent state for |packet-pump
::
:: next-wake: last timer we've set, or null
2019-05-27 07:44:11 +03:00
:: live: packets in flight; sent but not yet acked
2019-06-01 05:03:09 +03:00
:: metrics: congestion control information
2019-05-27 06:22:38 +03:00
::
2019-05-27 02:54:23 +03:00
+$ packet-pump-state
$: next-wake=(unit @da)
live=(tree [live-packet-key live-packet-val])
2019-06-01 05:03:09 +03:00
metrics=pump-metrics
2019-05-27 02:54:23 +03:00
==
:: $pump-metrics: congestion control state for a |packet-pump
2019-06-04 15:38:08 +03:00
::
:: This is an Ames adaptation of TCP's Reno congestion control
:: algorithm. The information signals and their responses are
:: identical to those of the "NewReno" variant of Reno; the
:: implementation differs because Ames acknowledgments differ from
:: TCP's and because we're using functional data structures.
::
:: If .skips reaches 3, we perform a fast retransmit and fast
:: recovery. This corresponds to Reno's handling of "three duplicate
:: acks".
::
:: rto: retransmission timeout
:: rtt: roundtrip time estimate, low-passed using EWMA
:: rttvar: mean deviation of .rtt, also low-passed with EWMA
:: num-live: how many packets sent, awaiting ack
:: ssthresh: slow-start threshold
:: cwnd: congestion window; max unacked packets
2019-06-04 15:38:08 +03:00
::
2019-05-27 04:38:19 +03:00
+$ pump-metrics
$: rto=_~s1
rtt=_~s1
rttvar=_~s1
ssthresh=_10.000
cwnd=_1
num-live=@ud
counter=@ud
2019-05-27 04:48:41 +03:00
==
2019-10-05 06:21:29 +03:00
+$ live-packet
$: key=live-packet-key
val=live-packet-val
2019-05-27 04:38:19 +03:00
==
2019-10-05 06:21:29 +03:00
+$ live-packet-key
2019-05-27 04:38:19 +03:00
$: =message-num
2019-05-27 02:54:23 +03:00
=fragment-num
2019-10-05 06:21:29 +03:00
==
+$ live-packet-val
$: packet-state
num-fragments=fragment-num
2019-05-27 02:54:23 +03:00
=fragment
2019-05-25 08:53:29 +03:00
==
+$ packet-state
$: last-sent=@da
retries=@ud
skips=@ud
2019-05-25 08:53:29 +03:00
==
:: $message-sink-state: state of |message-sink to assemble messages
::
:: last-acked: highest $message-num we've fully acknowledged
:: last-heard: highest $message-num we've heard all fragments on
:: pending-vane-ack: heard but not processed by local vane
:: live-messages: partially received messages
::
+$ message-sink-state
2019-05-27 04:38:19 +03:00
$: last-acked=message-num
last-heard=message-num
2019-07-28 10:50:32 +03:00
pending-vane-ack=(qeu [=message-num message=*])
2019-05-27 04:38:19 +03:00
live-messages=(map message-num partial-rcv-message)
2019-06-19 03:38:25 +03:00
nax=(set message-num)
2019-05-27 04:38:19 +03:00
==
:: $partial-rcv-message: message for which we've received some fragments
::
:: num-fragments: total number of fragments in this message
:: num-received: how many fragments we've received so far
:: fragments: fragments we've received, eventually producing a $message
::
2019-05-27 04:38:19 +03:00
+$ partial-rcv-message
$: num-fragments=fragment-num
num-received=fragment-num
fragments=(map fragment-num fragment)
2019-05-25 08:53:29 +03:00
==
2019-05-27 06:22:38 +03:00
::
+| %dialectics
::
:: $move: output effect; either request or response
::
2019-05-28 06:15:17 +03:00
+$ move [=duct card=(wind note gift)]
2019-06-29 00:26:40 +03:00
:: $queued-event: event to be handled after initial boot completes
::
2019-06-29 00:26:40 +03:00
+$ queued-event
$% [%call =duct type=* wrapped-task=(hobo task)]
[%take =wire =duct type=* =sign]
2019-05-28 04:43:10 +03:00
==
:: $note: request to other vane
::
:: TODO: specialize gall interface for subscription management
::
2019-07-28 10:50:32 +03:00
:: Ames passes a %plea note to another vane when it receives a
:: message on a "forward flow" from a peer, originally passed from
:: one of the peer's vanes to the peer's Ames.
::
2019-07-28 10:50:32 +03:00
:: Ames passes a %plea to itself to trigger a heartbeat message to
2019-06-25 01:18:18 +03:00
:: our sponsor.
::
2019-06-25 02:52:22 +03:00
:: Ames passes a %private-keys to Jael to request our private keys.
:: Ames passes a %public-keys to Jael to request a peer's public
:: keys.
::
2019-05-28 04:43:10 +03:00
+$ note
2019-06-29 00:26:40 +03:00
$~ [%b %wait *@da]
$% $: %b
2019-05-28 04:43:10 +03:00
$% [%wait date=@da]
[%rest date=@da]
== ==
$: %d
$% [%flog flog:dill]
2019-05-28 04:43:10 +03:00
== ==
$: %j
2019-06-29 00:26:40 +03:00
$% [%private-keys ~]
2019-06-29 01:27:59 +03:00
[%public-keys ships=(set ship)]
2019-05-28 04:43:10 +03:00
[%turf ~]
== ==
2019-06-29 00:26:40 +03:00
$: @tas
2019-07-28 10:50:32 +03:00
$% [%plea =ship =plea]
2019-05-28 04:43:10 +03:00
== == ==
:: $sign: response from other vane
::
2019-07-28 10:50:32 +03:00
:: A vane gives a %boon sign to Ames on a duct on which it had
:: previously received a message on a "forward flow". Ames will
:: transmit the message to the peer that had originally sent the
:: message on the forward flow. The peer's Ames will then give the
:: message to the remote vane from which the forward flow message
:: originated.
::
2019-05-28 04:43:10 +03:00
+$ sign
2019-06-29 00:26:40 +03:00
$~ [%b %wake ~]
$% $: %b
2019-05-28 04:43:10 +03:00
$% [%wake error=(unit tang)]
== ==
$: %j
$% [%private-keys =life vein=(map life ring)]
[%public-keys =public-keys-result]
2019-06-22 01:17:09 +03:00
[%turf turfs=(list turf)]
2019-06-18 21:21:12 +03:00
== ==
2019-06-29 00:26:40 +03:00
$: @tas
2019-06-20 10:21:37 +03:00
$% [%done error=(unit error)]
2019-07-28 10:50:32 +03:00
[%boon payload=*]
2019-05-28 04:43:10 +03:00
== == ==
2019-05-27 06:22:38 +03:00
:: $message-pump-task: job for |message-pump
::
2019-06-25 19:00:03 +03:00
:: %memo: packetize and send application-level message
2019-06-20 10:21:37 +03:00
:: %hear: handle receipt of ack on fragment or message
2019-05-27 06:22:38 +03:00
:: %wake: handle timer firing
::
+$ message-pump-task
2019-07-28 10:50:32 +03:00
$% [%memo =message-blob]
2019-06-20 10:21:37 +03:00
[%hear =message-num =ack-meat]
2019-05-27 06:22:38 +03:00
[%wake ~]
==
:: $message-pump-gift: effect from |message-pump
::
2019-06-20 10:21:37 +03:00
:: %done: report message acknowledgment
2019-05-31 03:20:42 +03:00
:: %send: emit message fragment
:: %wait: set a new timer at .date
:: %rest: cancel timer at .date
2019-05-27 06:22:38 +03:00
::
+$ message-pump-gift
2019-06-20 10:21:37 +03:00
$% [%done =message-num ok=?]
2019-05-31 03:20:42 +03:00
[%send =static-fragment]
[%wait date=@da]
[%rest date=@da]
2019-05-27 06:22:38 +03:00
==
:: $packet-pump-task: job for |packet-pump
::
2019-06-20 10:21:37 +03:00
:: %hear: deal with a packet acknowledgment
:: %done: deal with message acknowledgment
:: %halt: finish event, possibly updating timer
2019-05-27 06:22:38 +03:00
:: %wake: handle timer firing
::
+$ packet-pump-task
2019-06-20 10:21:37 +03:00
$% [%hear =message-num =fragment-num]
[%done =message-num lag=@dr]
[%halt ~]
2019-05-27 06:22:38 +03:00
[%wake ~]
==
:: $packet-pump-gift: effect from |packet-pump
::
2019-05-31 03:20:42 +03:00
:: %send: emit message fragment
:: %wait: set a new timer at .date
:: %rest: cancel timer at .date
2019-05-27 06:22:38 +03:00
::
+$ packet-pump-gift
2019-05-31 03:20:42 +03:00
$% [%send =static-fragment]
[%wait date=@da]
[%rest date=@da]
2019-05-27 06:22:38 +03:00
==
:: $message-sink-task: job for |message-sink
::
2019-06-18 21:21:12 +03:00
:: %done: receive confirmation from vane of processing or failure
2019-06-20 10:21:37 +03:00
:: %drop: clear .message-num from .nax.state
2019-06-11 03:31:50 +03:00
:: %hear: handle receiving a message fragment packet
2019-08-06 02:05:40 +03:00
:: .ok: %.y unless previous failed attempt
::
+$ message-sink-task
2019-06-18 21:21:12 +03:00
$% [%done ok=?]
2019-06-20 10:21:37 +03:00
[%drop =message-num]
2019-08-06 02:05:40 +03:00
[%hear =lane =shut-packet ok=?]
==
:: $message-sink-gift: effect from |message-sink
::
2019-07-28 10:50:32 +03:00
:: %memo: assembled from received packets
2019-06-20 10:21:37 +03:00
:: %send: emit an ack packet
::
+$ message-sink-gift
2019-07-28 10:50:32 +03:00
$% [%memo =message-num message=*]
2019-06-20 10:21:37 +03:00
[%send =message-num =ack-meat]
==
2019-05-25 05:03:33 +03:00
--
2019-05-28 04:43:10 +03:00
:: external vane interface
::
|= pit=vase
2019-06-29 00:26:40 +03:00
:: larval ames, before %born sets .unix-duct; wraps adult ames core
::
=< =* adult-gate .
=| queued-events=(qeu queued-event)
::
|= [our=ship now=@da eny=@ scry-gate=sley]
2019-06-29 00:26:40 +03:00
=* larval-gate .
=* adult-core (adult-gate +<)
|%
:: +call: handle request $task
::
++ call
|= [=duct type=* wrapped-task=(hobo task)]
::
=/ =task
?. ?=(%soft -.wrapped-task)
wrapped-task
2019-08-16 07:12:59 +03:00
~| our^%ames-fail-soft
;;(task p.wrapped-task)
2019-06-29 00:26:40 +03:00
:: %born: set .unix-duct and start draining .queued-events
::
?: ?=(%born -.task)
2019-06-29 00:26:40 +03:00
:: process %born using wrapped adult ames
::
=^ moves adult-gate (call:adult-core duct type task)
2019-06-29 00:26:40 +03:00
:: if no events were queued up, metamorphose
::
?~ queued-events
2019-08-10 21:48:40 +03:00
~> %slog.0^leaf/"ames: metamorphosis"
2019-06-29 00:26:40 +03:00
[moves adult-gate]
:: kick off a timer to process the first of .queued-events
::
=. moves :_(moves [duct %pass /larva %b %wait now])
[moves larval-gate]
:: any other event: enqueue it until we have a .unix-duct
::
=. queued-events (~(put to queued-events) %call duct type task)
2019-06-29 00:26:40 +03:00
[~ larval-gate]
:: +take: handle response $sign
::
++ take
|= [=wire =duct type=* =sign]
:: enqueue event if not a larval drainage timer
::
?. =(/larva wire)
=. queued-events (~(put to queued-events) %take wire duct type sign)
[~ larval-gate]
:: larval event drainage timer; pop and process a queued event
::
?. ?=([%b %wake *] sign)
2019-08-10 21:48:40 +03:00
~> %slog.0^leaf/"ames: larva: strange sign"
[~ larval-gate]
2019-06-29 00:26:40 +03:00
=^ first-event queued-events ~(get to queued-events)
=^ moves adult-gate
?- -.first-event
%call (call:adult-core +.first-event)
%take (take:adult-core +.first-event)
==
:: .queued-events has been cleared; metamorphose
::
?~ queued-events
2019-08-10 21:48:40 +03:00
~> %slog.0^leaf/"ames: metamorphosis"
2019-06-29 00:26:40 +03:00
[moves adult-gate]
2019-08-10 21:48:40 +03:00
~> %slog.0^leaf/"ames: larva: drain"
2019-06-29 00:26:40 +03:00
:: set timer to drain next event
::
=. moves :_(moves [duct %pass /larva %b %wait now])
[moves larval-gate]
:: lifecycle arms; mostly pass-throughs to the contained adult ames
::
++ scry scry:adult-core
2019-08-10 21:48:40 +03:00
++ stay [%larva queued-events ames-state.adult-gate]
2019-06-29 00:26:40 +03:00
++ load
2019-07-31 21:51:31 +03:00
|= $= old
$% [%larva events=_queued-events state=_ames-state.adult-gate]
[%adult state=_ames-state.adult-gate]
==
2019-07-24 02:55:35 +03:00
::
?- -.old
%adult
(load:adult-core state.old)
::
%larva
2019-08-10 21:48:40 +03:00
~> %slog.0^leaf/"ames: larva: load"
2019-07-24 02:55:35 +03:00
=. queued-events events.old
=. adult-gate (load:adult-core state.old)
larval-gate
==
2019-06-29 00:26:40 +03:00
--
:: adult ames, after metamorphosis from larva
::
=<
2019-05-28 04:43:10 +03:00
=| =ames-state
|= [our=ship now=@da eny=@ scry-gate=sley]
2019-05-28 04:43:10 +03:00
=* ames-gate .
|%
:: +call: handle request $task
::
++ call
|= [=duct type=* wrapped-task=(hobo task)]
^- [(list move) _ames-gate]
::
=/ =task
?. ?=(%soft -.wrapped-task)
wrapped-task
2019-08-10 23:46:53 +03:00
~| %ames-bad-task^p.wrapped-task
;;(task p.wrapped-task)
::
=/ event-core (per-event [our now eny scry-gate] duct ames-state)
::
=^ moves ames-state
=< abet
?- -.task
2019-06-22 01:26:26 +03:00
%born on-born:event-core
%crud (on-crud:event-core [p q]:task)
%hear (on-hear:event-core [lane blob]:task)
2019-11-04 04:35:45 +03:00
%heed (on-heed:event-core ship.task)
2019-07-26 00:50:08 +03:00
%hole (on-hole:event-core [lane blob]:task)
2019-06-29 02:43:52 +03:00
%init (on-init:event-core ship=p.task)
2019-11-04 04:35:45 +03:00
%jilt (on-jilt:event-core ship.task)
2019-06-22 01:19:24 +03:00
%vega on-vega:event-core
2019-06-22 01:00:04 +03:00
%wegh on-wegh:event-core
2019-07-28 10:50:32 +03:00
%plea (on-plea:event-core [ship plea]:task)
==
::
[moves ames-gate]
2019-05-28 04:43:10 +03:00
:: +take: handle response $sign
::
++ take
|= [=wire =duct type=* =sign]
^- [(list move) _ames-gate]
::
=/ event-core (per-event [our now eny scry-gate] duct ames-state)
2019-06-18 21:21:12 +03:00
::
=^ moves ames-state
=< abet
?- sign
2019-06-29 00:26:40 +03:00
[@ %done *] (on-take-done:event-core wire error.sign)
2019-07-28 10:50:32 +03:00
[@ %boon *] (on-take-boon:event-core wire payload.sign)
::
2019-06-29 00:26:40 +03:00
[%b %wake *] (on-take-wake:event-core wire error.sign)
::
2019-06-25 02:52:22 +03:00
[%j %turf *] (on-take-turf:event-core turfs.sign)
[%j %private-keys *] (on-priv:event-core [life vein]:sign)
[%j %public-keys *] (on-publ:event-core wire public-keys-result.sign)
2019-06-18 21:21:12 +03:00
==
::
[moves ames-gate]
2019-05-28 04:43:10 +03:00
:: +stay: extract state before reload
::
2019-07-24 02:55:35 +03:00
++ stay [%adult ames-state]
2019-05-28 04:43:10 +03:00
:: +load: load in old state after reload
::
++ load
2019-07-24 02:55:35 +03:00
|= old-state=_ames-state
2019-07-31 05:31:15 +03:00
^+ ames-gate
2019-07-24 02:55:35 +03:00
ames-gate(ames-state old-state)
2019-05-28 04:43:10 +03:00
:: +scry: dereference namespace
::
++ scry
|= [fur=(unit (set monk)) ren=@tas why=shop syd=desk lot=coin tyl=path]
^- (unit (unit cage))
::
[~ ~]
--
:: helpers
2019-05-28 04:43:10 +03:00
::
2019-05-25 05:03:33 +03:00
|%
++ per-event
=| moves=(list move)
|= [[our=ship now=@da eny=@ scry-gate=sley] =duct =ames-state]
|%
++ event-core .
++ abet [(flop moves) ames-state]
2019-05-28 06:15:17 +03:00
++ emit |=(=move event-core(moves [move moves]))
2019-06-20 10:47:13 +03:00
:: +on-take-done: handle notice from vane that it processed a message
::
2019-06-20 10:47:13 +03:00
++ on-take-done
2019-06-19 02:59:25 +03:00
|= [=wire error=(unit error)]
2019-06-18 21:21:12 +03:00
^+ event-core
:: relay the vane ack to the foreign peer
2019-06-25 01:18:18 +03:00
::
2019-06-19 03:38:25 +03:00
=+ ^- [her=ship =bone] (parse-bone-wire wire)
::
2019-06-25 00:32:53 +03:00
=/ =peer-state (got-peer-state her)
=/ =channel [[our her] now |2.ames-state -.peer-state]
2019-06-25 00:32:53 +03:00
=/ peer-core (make-peer-core peer-state channel)
2019-07-28 10:50:32 +03:00
:: if processing succeded, send positive ack packet and exit
2019-06-19 03:38:25 +03:00
::
2019-07-28 10:50:32 +03:00
?~ error
abet:(run-message-sink:peer-core bone %done ok=%.y)
2019-07-28 10:50:32 +03:00
:: failed; send message nack packet
2019-06-19 03:38:25 +03:00
::
=. event-core abet:(run-message-sink:peer-core bone %done ok=%.n)
2019-07-28 10:50:32 +03:00
:: construct nack-trace message, referencing .failed $message-num
::
=/ failed=message-num last-acked:(~(got by rcv.peer-state) bone)
=/ =naxplanation [failed u.error]
=/ =message-blob (jam naxplanation)
2019-07-28 10:50:32 +03:00
:: send nack-trace message on associated .nack-trace-bone
2019-06-19 03:38:25 +03:00
::
=. peer-core (make-peer-core peer-state channel)
=/ nack-trace-bone=^bone (mix 0b10 bone)
::
abet:(run-message-pump:peer-core nack-trace-bone %memo message-blob)
:: +on-crud: handle event failure; print to dill
::
++ on-crud
|= =error
^+ event-core
(emit duct %pass /crud %d %flog %crud error)
2019-11-04 04:35:45 +03:00
:: +on-heed: handle request to track .ship's responsiveness
::
++ on-heed
|= =ship
^+ event-core
=/ ship-state (~(get by peers.ames-state) ship)
?. ?=([~ %known *] ship-state)
%+ enqueue-alien-todo ship
|= todos=alien-agenda
todos(heeds (~(put in heeds.todos) duct))
::
=/ =peer-state +.u.ship-state
=/ =channel [[our ship] now |2.ames-state -.peer-state]
abet:on-heed:(make-peer-core peer-state channel)
:: +on-jilt: handle request to stop tracking .ship's responsiveness
::
++ on-jilt
|= =ship
^+ event-core
=/ ship-state (~(get by peers.ames-state) ship)
?. ?=([~ %known *] ship-state)
%+ enqueue-alien-todo ship
|= todos=alien-agenda
todos(heeds (~(del in heeds.todos) duct))
::
=/ =peer-state +.u.ship-state
=/ =channel [[our ship] now |2.ames-state -.peer-state]
abet:on-jilt:(make-peer-core peer-state channel)
2019-06-25 03:48:05 +03:00
:: +on-hear: handle raw packet receipt
2019-06-18 21:21:12 +03:00
::
++ on-hear
|= [=lane =blob]
^+ event-core
2019-08-06 02:05:40 +03:00
(on-hear-packet lane (decode-packet blob) ok=%.y)
2019-07-26 00:50:08 +03:00
:: +on-hole: handle packet crash notification
::
++ on-hole
|= [=lane =blob]
^+ event-core
::
2019-08-10 21:48:40 +03:00
~> %slog.0^leaf/"ames: %hole"
2019-08-06 02:05:40 +03:00
(on-hear-packet lane (decode-packet blob) ok=%.n)
2019-06-25 03:48:05 +03:00
:: +on-hear-packet: handle mildly processed packet receipt
::
++ on-hear-packet
2019-08-06 02:05:40 +03:00
|= [=lane =packet ok=?]
2019-06-25 03:48:05 +03:00
^+ event-core
::
2019-07-24 03:31:51 +03:00
?: =(our sndr.packet)
event-core
::
2019-07-28 10:50:32 +03:00
%. +<
::
?. =(our rcvr.packet)
on-hear-forward
::
?: encrypted.packet
on-hear-shut
on-hear-open
2019-06-22 00:21:58 +03:00
:: +on-hear-forward: maybe forward a packet to someone else
::
:: Note that this performs all forwarding requests without
:: filtering. Any protection against DDoS amplification will be
:: provided by Vere.
2019-06-22 00:35:07 +03:00
::
++ on-hear-forward
2019-08-06 02:05:40 +03:00
|= [=lane =packet ok=?]
^+ event-core
2019-08-21 00:44:44 +03:00
:: set .origin.packet if it doesn't already have one, re-encode, and send
::
2019-08-21 00:44:44 +03:00
=? origin.packet ?=(~ origin.packet) `lane
=/ =blob (encode-packet packet)
(send-blob rcvr.packet blob)
2019-06-18 02:23:32 +03:00
:: +on-hear-open: handle receipt of plaintext comet self-attestation
::
++ on-hear-open
2019-08-06 02:05:40 +03:00
|= [=lane =packet ok=?]
^+ event-core
2019-06-18 02:23:32 +03:00
:: if we already know .sndr, ignore duplicate attestation
::
2019-06-18 02:23:32 +03:00
=/ ship-state (~(get by peers.ames-state) sndr.packet)
?: ?=([~ %known *] ship-state)
event-core
::
=/ =open-packet ;;(open-packet packet)
:: assert .our and .her and lives match
::
2019-06-25 19:00:03 +03:00
?> .= sndr.open-packet sndr.packet
?> .= rcvr.open-packet our
?> .= sndr-life.open-packet 1
?> .= rcvr-life.open-packet life.ames-state
2019-06-18 02:23:32 +03:00
:: no ghost comets allowed
::
?> (lte 256 (^sein:title sndr.packet))
:: comet public-key must hash to its @p address
::
:: TODO how does this validation work elsewhere?
::
?> =(`@`sndr.packet `@`(shaf %pawn public-key.open-packet))
:: everything after .signature is signed
::
:: TODO: should this double-cue instead of re-jamming?
::
=/ signed=@ (jam +.open-packet)
?> (verify-signature signed [public-key signature]:open-packet)
:: store comet as peer in our state
::
=. peers.ames-state
%+ ~(put by peers.ames-state) sndr.packet
^- ^ship-state
:- %known
=| =peer-state
2019-06-18 02:23:32 +03:00
=/ our-private-key sec:ex:crypto-core.ames-state
=/ =symmetric-key
(derive-symmetric-key public-key.open-packet our-private-key)
::
%_ peer-state
2019-06-18 02:23:32 +03:00
symmetric-key symmetric-key
life sndr-life.open-packet
public-key public-key.open-packet
sponsor (^sein:title sndr.packet)
route `[direct=%.y lane]
2019-06-18 02:23:32 +03:00
==
::
event-core
2019-06-22 00:35:07 +03:00
:: +on-hear-shut: handle receipt of encrypted packet
::
++ on-hear-shut
2019-08-06 02:05:40 +03:00
|= [=lane =packet ok=?]
^+ event-core
2019-05-28 06:59:53 +03:00
:: encrypted packet content must be an encrypted atom
::
?> ?=(@ content.packet)
::
=/ sndr-state (~(get by peers.ames-state) sndr.packet)
:: if we don't know them, maybe enqueue a jael %public-keys request
::
?. ?=([~ %known *] sndr-state)
(enqueue-alien-todo sndr.packet |=(alien-agenda +<))
2019-11-22 05:12:42 +03:00
:: channel: immutable data about relationship with .sndr.packet
2019-06-01 06:32:13 +03:00
::
:: If we know them, we have a $channel with them, which we've
:: populated with a .symmetric-key derived from our private key
:: and their public key using elliptic curve Diffie-Hellman.
::
2019-05-28 06:59:53 +03:00
=/ =peer-state +.u.sndr-state
=/ =channel [[our sndr.packet] now |2.ames-state -.peer-state]
:: ward against replay attacks
::
2019-06-01 06:32:13 +03:00
:: We only accept packets from a ship at their known life, and to
:: us at our current life.
::
2019-11-22 05:15:05 +03:00
~| [[her our-life her-life]:channel [sndr-life rcvr-life]:shut-packet]
?> =(sndr-life.shut-packet her-life.channel)
?> =(rcvr-life.shut-packet our-life.channel)
2019-11-22 05:12:42 +03:00
:: decrypt packet contents using symmetric-key.channel
::
=/ =shut-packet (decrypt symmetric-key.channel content.packet)
:: non-galaxy: update route with heard lane or forwarded lane
::
2019-07-26 01:56:33 +03:00
=? route.peer-state !=(%czar (clan:title her.channel))
?~ origin.packet
2019-08-16 12:26:03 +03:00
`[direct=%.y lane]
`[direct=%.n u.origin.packet]
:: perform peer-specific handling of packet
::
=/ peer-core (make-peer-core peer-state channel)
2019-08-06 02:05:40 +03:00
abet:(on-hear-shut-packet:peer-core lane shut-packet ok)
2019-07-28 10:50:32 +03:00
:: +on-take-boon: receive request to give message to peer
2019-06-19 02:51:06 +03:00
::
2019-07-28 10:50:32 +03:00
++ on-take-boon
|= [=wire payload=*]
2019-06-19 02:51:06 +03:00
^+ event-core
::
2019-06-21 00:46:31 +03:00
=+ ^- [her=ship =bone] (parse-bone-wire wire)
::
2019-06-25 00:32:53 +03:00
=/ =peer-state (got-peer-state her)
=/ =channel [[our her] now |2.ames-state -.peer-state]
2019-06-19 02:51:06 +03:00
::
2019-07-28 10:50:32 +03:00
abet:(on-memo:(make-peer-core peer-state channel) bone payload)
:: +on-plea: handle request to send message
2019-06-09 09:26:01 +03:00
::
2019-07-28 10:50:32 +03:00
++ on-plea
|= [=ship =plea]
2019-06-09 09:26:01 +03:00
^+ event-core
:: .plea is from local vane to foreign ship
2019-06-09 09:26:01 +03:00
::
2019-06-22 00:35:07 +03:00
=/ ship-state (~(get by peers.ames-state) ship)
2019-06-09 09:26:01 +03:00
::
2019-06-22 00:35:07 +03:00
?. ?=([~ %known *] ship-state)
2019-06-25 00:13:45 +03:00
%+ enqueue-alien-todo ship
|= todos=alien-agenda
todos(messages [[duct plea] messages.todos])
2019-06-09 09:26:01 +03:00
::
2019-06-22 00:35:07 +03:00
=/ =peer-state +.u.ship-state
=/ =channel [[our ship] now |2.ames-state -.peer-state]
2019-06-09 09:26:01 +03:00
::
=^ =bone ossuary.peer-state (bind-duct ossuary.peer-state duct)
2019-11-15 03:10:48 +03:00
%- %+ trace msg.veb
|. ^- tape
=/ sndr [our our-life.channel]
=/ rcvr [ship her-life.channel]
"plea {<sndr^rcvr^bone^vane.plea^path.plea>}"
2019-06-21 00:46:31 +03:00
::
2019-07-28 10:50:32 +03:00
abet:(on-memo:(make-peer-core peer-state channel) bone plea)
2019-06-19 02:59:25 +03:00
:: +on-take-wake: receive wakeup or error notification from behn
::
++ on-take-wake
|= [=wire error=(unit tang)]
^+ event-core
::
2019-06-22 00:35:07 +03:00
=+ ^- [her=ship =bone] (parse-pump-timer-wire wire)
::
2019-06-25 00:32:53 +03:00
=/ =peer-state (got-peer-state her)
=/ =channel [[our her] now |2.ames-state -.peer-state]
2019-06-22 00:35:07 +03:00
::
abet:(on-wake:(make-peer-core peer-state channel) bone error)
2019-06-22 01:25:18 +03:00
:: +on-init: first boot; subscribe to our info from jael
::
++ on-init
2019-06-25 02:52:22 +03:00
|= our=ship
2019-06-22 01:25:18 +03:00
^+ event-core
::
=~ (emit duct %pass /turf %j %turf ~)
(emit duct %pass /private-keys %j %private-keys ~)
2019-06-25 00:48:53 +03:00
==
2019-06-25 02:52:22 +03:00
:: +on-priv: set our private key to jael's response
::
++ on-priv
|= [=life vein=(map life private-key)]
2019-06-25 02:52:22 +03:00
^+ event-core
::
=/ =private-key (~(got by vein) life)
2019-06-25 02:52:22 +03:00
=. life.ames-state life
=. crypto-core.ames-state (nol:nu:crub:crypto private-key)
:: recalculate each peer's symmetric key
::
=/ our-private-key sec:ex:crypto-core.ames-state
=. peers.ames-state
%- ~(run by peers.ames-state)
|= =ship-state
^+ ship-state
::
?. ?=(%known -.ship-state)
ship-state
::
=/ =peer-state +.ship-state
=. symmetric-key.peer-state
(derive-symmetric-key public-key.+.ship-state our-private-key)
::
[%known peer-state]
2019-06-25 02:52:22 +03:00
::
event-core
:: +on-publ: update pki data for peer or self
::
++ on-publ
|= [=wire =public-keys-result]
2019-06-25 02:52:22 +03:00
^+ event-core
::
|^ ^+ event-core
::
?- public-keys-result
[%diff @ %rift *]
2019-11-03 07:07:51 +03:00
(on-publ-breach who.public-keys-result)
2019-06-25 02:52:22 +03:00
::
[%diff @ %keys *]
(on-publ-rekey [who to.diff]:public-keys-result)
2019-06-25 02:52:22 +03:00
::
[%diff @ %spon *]
(on-publ-sponsor [who to.diff]:public-keys-result)
2019-06-25 02:52:22 +03:00
::
[%full *]
(on-publ-full points.public-keys-result)
2019-11-03 07:07:51 +03:00
::
[%breach *]
(on-publ-breach who.public-keys-result)
2019-06-25 02:52:22 +03:00
==
:: +on-publ-breach: handle continuity breach of .ship; wipe its state
::
2019-08-23 06:53:45 +03:00
:: Abandon all pretense of continuity and delete all messaging state
:: associated with .ship, including sent and unsent messages.
::
:: TODO: cancel all timers? otherwise we'll get spurious firings
:: from behn
2019-06-25 02:52:22 +03:00
::
2019-08-23 05:51:51 +03:00
:: TODO: cancel gall subscriptions on breach
::
2019-06-25 02:52:22 +03:00
++ on-publ-breach
2019-11-03 07:07:51 +03:00
|= =ship
2019-06-25 02:52:22 +03:00
^+ event-core
::
2019-08-23 05:51:51 +03:00
=/ ship-state (~(get by peers.ames-state) ship)
:: we shouldn't be hearing about ships we don't care about
::
?~ ship-state
2019-11-03 07:07:51 +03:00
~> %slog.0^leaf/"ames: breach unknown {<our^ship>}"
2019-08-23 05:51:51 +03:00
event-core
:: if an alien breached, this doesn't affect us
::
2019-08-23 06:53:45 +03:00
?: ?=([~ %alien *] ship-state)
2019-11-03 07:07:51 +03:00
~> %slog.0^leaf/"ames: breach alien {<our^ship>}"
2019-08-23 05:51:51 +03:00
event-core
2019-11-03 07:07:51 +03:00
~> %slog.0^leaf/"ames: breach peer {<our^ship>}"
2019-08-23 05:51:51 +03:00
:: a peer breached; drop messaging state
::
=/ =peer-state +.u.ship-state
2019-08-23 06:53:45 +03:00
=/ old-qos=qos qos.peer-state
:: reset all peer state other than pki data
::
=. +.peer-state +:*^peer-state
:: print change to quality of service, if any
::
=/ text=(unit tape) (qos-update-text ship old-qos qos.peer-state)
::
=? event-core ?=(^ text)
(emit duct %pass /qos %d %flog %text u.text)
:: reinitialize galaxy route if applicable
::
=? route.peer-state =(%czar (clan:title ship))
`[direct=%.y lane=[%& ship]]
::
2019-08-23 05:51:51 +03:00
=. peers.ames-state
2019-08-23 06:53:45 +03:00
(~(put by peers.ames-state) ship [%known peer-state])
2019-08-23 05:51:51 +03:00
::
event-core
2019-06-25 03:14:02 +03:00
:: +on-publ-rekey: handle new key for peer
::
:: TODO: assert .crypto-suite compatibility
2019-06-25 02:52:22 +03:00
::
++ on-publ-rekey
|= $: =ship
=life
crypto-suite=@ud
=public-key
2019-06-25 02:52:22 +03:00
==
^+ event-core
::
2019-08-10 21:48:40 +03:00
~> %slog.0^leaf/"ames: rekey {<our^ship^life>}"
2019-08-14 03:56:56 +03:00
::
=/ ship-state (~(get by peers.ames-state) ship)
?. ?=([~ %known *] ship-state)
=| =point
=. life.point life
=. keys.point (my [life crypto-suite public-key]~)
=. sponsor.point `(^sein:title ship)
::
(on-publ-full (my [ship point]~))
::
=/ =peer-state +.u.ship-state
::
=/ =private-key sec:ex:crypto-core.ames-state
=. symmetric-key.peer-state
(derive-symmetric-key public-key private-key)
::
=. life.peer-state life
=. public-key.peer-state public-key
::
=. peers.ames-state (~(put by peers.ames-state) ship %known peer-state)
event-core
:: +on-publ-sponsor: handle new or lost sponsor for peer
2019-06-25 03:30:43 +03:00
::
:: TODO: handle sponsor loss
2019-06-25 02:52:22 +03:00
::
++ on-publ-sponsor
|= [=ship sponsor=(unit ship)]
^+ event-core
::
2019-06-25 03:16:32 +03:00
?~ sponsor
2019-08-10 21:48:40 +03:00
~| %ames-lost-sponsor^our^ship !!
2019-06-25 03:16:32 +03:00
::
=/ =peer-state (got-peer-state ship)
2019-06-25 03:16:32 +03:00
=. sponsor.peer-state u.sponsor
::
=. peers.ames-state (~(put by peers.ames-state) ship %known peer-state)
event-core
2019-06-25 03:30:43 +03:00
:: +on-publ-full: handle new pki data for peer(s)
2019-06-25 02:52:22 +03:00
::
++ on-publ-full
|= points=(map ship point)
^+ event-core
::
2019-06-25 03:30:43 +03:00
=> .(points ~(tap by points))
|^ ^+ event-core
?~ points event-core
::
2019-08-06 03:00:04 +03:00
=+ ^- [=ship =point] i.points
2019-06-25 03:30:43 +03:00
::
2019-08-14 03:56:56 +03:00
=/ old-ship-state (~(get by peers.ames-state) ship)
::
=. event-core (insert-peer-state ship point)
::
=? event-core ?=([~ %alien *] old-ship-state)
(meet-alien ship point +.u.old-ship-state)
2019-06-25 03:30:43 +03:00
::
$(points t.points)
::
++ meet-alien
|= [=ship =point todos=alien-agenda]
2019-06-25 03:30:43 +03:00
^+ event-core
:: if we're a comet, send self-attestation packet first
2019-06-26 21:45:07 +03:00
::
=? event-core =(%pawn (clan:title our))
(send-blob ship (attestation-packet ship life.point))
2019-10-05 06:38:32 +03:00
:: apply outgoing messages, reversing for FIFO order
2019-06-25 03:48:05 +03:00
::
=. event-core
%+ reel messages.todos
2019-10-05 06:21:29 +03:00
|= [[=^duct =plea] core=_event-core]
(on-plea:core(duct duct) ship plea)
2019-06-26 21:45:07 +03:00
:: apply outgoing packet blobs
2019-06-25 03:48:05 +03:00
::
=. event-core
%+ roll ~(tap in packets.todos)
2019-10-05 06:21:29 +03:00
|= [=blob core=_event-core]
(send-blob:core ship blob)
2019-06-25 03:48:05 +03:00
::
event-core
2019-06-25 03:30:43 +03:00
--
2019-06-25 03:48:05 +03:00
::
++ insert-peer-state
2019-08-14 03:56:56 +03:00
|= [=ship =point]
2019-06-25 03:48:05 +03:00
^+ event-core
::
2019-08-14 03:56:56 +03:00
=/ =peer-state (gut-peer-state ship)
=/ =public-key pass:(~(got by keys.point) life.point)
2019-06-25 03:48:05 +03:00
=/ =private-key sec:ex:crypto-core.ames-state
=/ =symmetric-key (derive-symmetric-key public-key private-key)
::
2019-08-14 03:56:56 +03:00
=. life.peer-state life.point
2019-06-25 03:48:05 +03:00
=. public-key.peer-state public-key
=. symmetric-key.peer-state symmetric-key
2019-08-14 03:56:56 +03:00
=. sponsor.peer-state (fall sponsor.point (^sein:title ship))
2019-07-24 02:55:35 +03:00
:: automatically set galaxy route, since unix handles lookup
::
=? route.peer-state ?=(%czar (clan:title ship))
`[direct=%.y lane=[%& ship]]
2019-06-25 03:48:05 +03:00
::
=. peers.ames-state
(~(put by peers.ames-state) ship %known peer-state)
::
event-core
2019-06-25 02:52:22 +03:00
--
2019-06-22 01:17:09 +03:00
:: +on-take-turf: relay %turf move from jael to unix
::
++ on-take-turf
|= turfs=(list turf)
^+ event-core
::
(emit unix-duct.ames-state %give %turf turfs)
2019-06-22 01:00:04 +03:00
:: +on-wegh: produce memory usage report
::
++ on-wegh
^+ event-core
::
=+ [known alien]=(skid ~(tap by peers.ames-state) |=(^ =(%known +<-)))
::
%- emit
:^ duct %give %mass
:+ %ames %|
:~ peers-known+&+known
peers-alien+&+alien
dot+&+ames-state
==
2019-06-22 01:26:26 +03:00
:: +on-born: handle unix process restart
2019-07-25 01:56:13 +03:00
::
2019-07-31 05:31:15 +03:00
++ on-born
^+ event-core
::
=. unix-duct.ames-state duct
::
=/ turfs
;; (list turf)
=< q.q %- need %- need
(scry-gate [%141 %noun] ~ %j `beam`[[our %turf %da now] /])
2019-07-31 05:31:15 +03:00
::
(emit unix-duct.ames-state %give %turf turfs)
2019-06-22 01:19:24 +03:00
:: +on-vega: handle kernel reload
::
++ on-vega event-core
2019-06-25 00:13:45 +03:00
:: +enqueue-alien-todo: helper to enqueue a pending request
::
2019-06-26 21:45:07 +03:00
:: Also requests key and life from Jael on first request.
:: On a comet, enqueues self-attestation packet on first request.
2019-06-25 00:13:45 +03:00
::
++ enqueue-alien-todo
|= [=ship mutate=$-(alien-agenda alien-agenda)]
2019-06-25 00:13:45 +03:00
^+ event-core
::
=/ ship-state (~(get by peers.ames-state) ship)
:: create a default $alien-agenda on first contact
2019-06-25 00:13:45 +03:00
::
=+ ^- [already-pending=? todos=alien-agenda]
2019-06-25 00:13:45 +03:00
?~ ship-state
[%.n *alien-agenda]
2019-06-25 00:13:45 +03:00
[%.y ?>(?=(%alien -.u.ship-state) +.u.ship-state)]
:: mutate .todos and apply to permanent state
::
=. todos (mutate todos)
=. peers.ames-state (~(put by peers.ames-state) ship %alien todos)
:: ask jael for .sndr life and keys on first contact
::
?: already-pending
event-core
(emit duct %pass /public-keys %j %public-keys [n=ship ~ ~])
:: +send-blob: fire packet at .ship and maybe sponsors
::
2019-08-16 12:26:03 +03:00
:: Send to .ship and sponsors until we find a direct lane or
:: encounter .our in the sponsorship chain.
::
:: If we have no PKI data for a recipient, enqueue the packet and
:: request the information from Jael if we haven't already.
::
++ send-blob
|= [=ship =blob]
::
2019-08-16 12:26:03 +03:00
|^ ^+ event-core
::
=/ ship-state (~(get by peers.ames-state) ship)
::
?. ?=([~ %known *] ship-state)
%+ enqueue-alien-todo ship
|= todos=alien-agenda
todos(packets (~(put in packets.todos) blob))
2019-08-16 12:26:03 +03:00
::
=/ =peer-state +.u.ship-state
::
?~ route=route.peer-state
(try-next-sponsor sponsor.peer-state)
::
=. event-core
(emit unix-duct.ames-state %give %send lane.u.route blob)
::
?: direct.u.route
event-core
(try-next-sponsor sponsor.peer-state)
::
2019-08-16 12:26:03 +03:00
++ try-next-sponsor
|= sponsor=^ship
^+ event-core
::
?: =(ship sponsor)
event-core
2019-08-16 12:26:03 +03:00
?: =(our sponsor)
event-core
2019-08-16 12:26:03 +03:00
^$(ship sponsor)
--
:: +attestation-packet: generate signed self-attestation for .her
::
:: Sent by a comet on first contact with a peer. Not acked.
::
++ attestation-packet
|= [her=ship =her=life]
^- blob
::
=/ signed=_+:*open-packet
:* ^= public-key pub:ex:crypto-core.ames-state
^= sndr our
^= sndr-life life.ames-state
^= rcvr her
^= rcvr-life her-life
==
2019-06-25 00:36:39 +03:00
::
=/ =private-key sec:ex:crypto-core.ames-state
=/ =signature (sign-open-packet private-key signed)
=/ =open-packet [signature signed]
=/ =packet [[our her] encrypted=%.n origin=~ open-packet]
::
(encode-packet packet)
2019-06-25 00:32:53 +03:00
:: +got-peer-state: lookup .her state or crash
::
++ got-peer-state
|= her=ship
^- peer-state
::
~| %freaky-alien^her
=- ?>(?=(%known -<) ->)
(~(got by peers.ames-state) her)
2019-08-14 03:56:56 +03:00
:: +gut-peer-state: lookup .her state or default
::
++ gut-peer-state
|= her=ship
^- peer-state
=/ ship-state (~(get by peers.ames-state) her)
?. ?=([~ %known *] ship-state)
*peer-state
+.u.ship-state
2019-06-09 09:26:01 +03:00
:: +make-peer-core: create nested |peer-core for per-peer processing
::
++ make-peer-core
|= [=peer-state =channel]
|%
++ peer-core .
++ emit |=(move peer-core(event-core (^emit +<)))
::
++ abet
^+ event-core
::
=. peers.ames-state
(~(put by peers.ames-state) her.channel %known peer-state)
::
event-core
2019-11-04 04:35:45 +03:00
::
++ on-heed peer-core(heeds.peer-state (~(put in heeds.peer-state) duct))
++ on-jilt peer-core(heeds.peer-state (~(del in heeds.peer-state) duct))
:: +update-qos: update and maybe print connection status
::
++ update-qos
|= =new=qos
^+ peer-core
::
=^ old-qos qos.peer-state [qos.peer-state new-qos]
2019-11-04 04:35:45 +03:00
:: if no update worth reporting, we're done
::
?~ text=(qos-update-text her.channel old-qos new-qos)
peer-core
2019-11-04 04:35:45 +03:00
:: print message
::
=. peer-core (emit duct %pass /qos %d %flog %text u.text)
:: if peer is ok, we're done
::
?. ?=(?(%dead %unborn) -.qos.peer-state)
peer-core
:: peer has stopped responding; notify client vanes
::
%+ roll ~(tap in heeds.peer-state)
|=([d=^duct core=_peer-core] (emit:core d %give %clog her.channel))
:: +on-hear-shut-packet: handle receipt of ack or message fragment
::
++ on-hear-shut-packet
2019-08-06 02:05:40 +03:00
|= [=lane =shut-packet ok=?]
^+ peer-core
:: update and print connection status
::
2019-08-08 08:18:25 +03:00
=. peer-core (update-qos %live last-contact=now)
::
=/ =bone bone.shut-packet
::
2019-06-10 19:13:11 +03:00
?: ?=(%& -.meat.shut-packet)
(run-message-sink bone %hear lane shut-packet ok)
2019-08-06 02:05:40 +03:00
:: ignore .ok for |message-pump; just try again on error
::
2019-06-20 10:21:37 +03:00
(run-message-pump bone %hear [message-num +.meat]:shut-packet)
2019-06-20 11:18:07 +03:00
:: +on-memo: handle request to send message
::
++ on-memo
2019-07-28 10:50:32 +03:00
|= [=bone payload=*]
2019-06-20 11:18:07 +03:00
^+ peer-core
::
2019-07-28 10:50:32 +03:00
=/ =message-blob (jam payload)
(run-message-pump bone %memo message-blob)
2019-06-22 00:35:07 +03:00
:: +on-wake: handle timer expiration
::
++ on-wake
|= [=bone error=(unit tang)]
^+ peer-core
2019-11-22 03:52:57 +03:00
:: if we previously errored out, print and reset timer for later
2019-06-22 00:35:07 +03:00
::
2019-11-22 03:52:57 +03:00
:: This really shouldn't happen, but if it does, make sure we
:: don't brick either this messaging flow or Behn.
::
?^ error
=. peer-core
(emit duct %pass /wake-fail %d %flog %crud %ames-wake u.error)
::
2019-11-22 05:13:09 +03:00
?~ message-pump-state=(~(get by snd.peer-state) bone)
peer-core
?~ next-wake.packet-pump-state.u.message-pump-state
peer-core
::
2019-11-22 03:52:57 +03:00
=/ =wire (make-pump-timer-wire her.channel bone)
2019-11-22 04:41:17 +03:00
(emit duct %pass wire %b %wait (add now.channel ~s30))
:: update and print connection state
::
=. peer-core %- update-qos
2019-08-23 06:53:45 +03:00
?: ?=(%unborn -.qos.peer-state)
[%dead now]
?. ?& ?=(%live -.qos.peer-state)
(gte now (add ~s30 last-contact.qos.peer-state))
==
qos.peer-state
[%dead last-contact.qos.peer-state]
2019-06-25 00:09:42 +03:00
:: expire direct route
::
:: Since a packet's timer expired, mark the .lane.route as
:: indirect. The next packets we emit will be sent to the
:: receiver's sponsorship chain in case the receiver's
:: transport address has changed and this lane is no longer
:: valid.
::
:: If .her is a galaxy, the lane will always remain direct.
::
=? route.peer-state
2019-07-24 02:55:35 +03:00
?& ?=(^ route.peer-state)
direct.u.route.peer-state
!=(%czar (clan:title her.channel))
==
route.peer-state(direct.u %.n)
:: resend comet attestation packet if first message times out
::
:: The attestation packet doesn't get acked, so if we tried to
:: send a packet but it timed out, maybe they didn't get our
:: attestation.
::
:: Only resend on timeout of packets in the first message we
:: send them, since they should remember forever.
::
=? event-core
?& ?=(%pawn (clan:title our))
=(1 current:(~(got by snd.peer-state) bone))
==
(send-blob her.channel (attestation-packet [her her-life]:channel))
:: maybe resend some timed out packets
2019-06-25 00:09:42 +03:00
::
2019-06-22 00:35:07 +03:00
(run-message-pump bone %wake ~)
2019-07-28 10:50:32 +03:00
:: +send-shut-packet: fire encrypted packet at rcvr and maybe sponsors
::
++ send-shut-packet
|= =shut-packet
^+ peer-core
:: swizzle bone just before sending; TODO document
::
=. bone.shut-packet (mix 1 bone.shut-packet)
::
=/ content (encrypt symmetric-key.channel shut-packet)
=/ =packet [[our her.channel] encrypted=%.y origin=~ content]
=/ =blob (encode-packet packet)
::
=. event-core (send-blob her.channel blob)
peer-core
:: +got-duct: look up $duct by .bone, asserting already bound
::
++ got-duct
|= =bone
^- ^duct
~| %dangling-bone^her.channel^bone
(~(got by by-bone.ossuary.peer-state) bone)
2019-06-10 19:13:11 +03:00
:: +run-message-pump: process $message-pump-task and its effects
2019-06-09 09:26:01 +03:00
::
++ run-message-pump
|= [=bone task=message-pump-task]
^+ peer-core
:: pass .task to the |message-pump and apply state mutations
::
=/ =message-pump-state
(~(gut by snd.peer-state) bone *message-pump-state)
::
2019-06-09 09:26:01 +03:00
=/ message-pump (make-message-pump message-pump-state channel)
=^ pump-gifts message-pump-state (work:message-pump task)
=. snd.peer-state (~(put by snd.peer-state) bone message-pump-state)
:: process effects from |message-pump
::
2019-06-09 09:26:01 +03:00
|^ ^+ peer-core
?~ pump-gifts peer-core
=* gift i.pump-gifts
=. peer-core
?- -.gift
2019-06-20 11:13:54 +03:00
%done (on-pump-done [message-num ok]:gift)
%send (on-pump-send static-fragment.gift)
%wait (on-pump-wait date.gift)
%rest (on-pump-rest date.gift)
2019-06-09 09:26:01 +03:00
==
$(pump-gifts t.pump-gifts)
2019-06-20 11:13:54 +03:00
:: +on-pump-done: handle |message-pump's report of message (n)ack
::
2019-06-20 11:13:54 +03:00
++ on-pump-done
2019-06-09 09:26:01 +03:00
|= [=message-num ok=?]
^+ peer-core
2019-06-20 03:48:29 +03:00
:: if odd bone, ack is on "subscription update" message; no-op
2019-06-19 03:52:10 +03:00
::
2019-06-20 03:48:29 +03:00
?: =(1 (end 0 1 bone))
2019-06-19 03:52:10 +03:00
peer-core
2019-06-20 03:48:29 +03:00
:: even bone; is this bone a nack-trace bone?
2019-06-19 03:38:25 +03:00
::
?: =(1 (end 0 1 (rsh 0 1 bone)))
:: nack-trace bone; assume .ok, clear nack from |message-sink
2019-06-19 03:52:10 +03:00
::
=/ target-bone=^bone (mix 0b10 bone)
::
(run-message-sink target-bone %drop message-num)
2019-06-19 03:52:10 +03:00
:: not a nack-trace bone; positive ack gets emitted trivially
::
2019-06-09 09:26:01 +03:00
?: ok
(emit (got-duct bone) %give %done error=~)
:: nack; enqueue, pending nack-trace message
2019-06-09 09:26:01 +03:00
::
:: The pump must never emit duplicate acks. If we heard the
:: nack-trace message already, the pump should not generate a
:: duplicate %done event when we hear a message nack packet.
::
=/ nax-key [bone message-num]
2019-06-19 02:38:20 +03:00
?< (~(has in nax.peer-state) nax-key)
=. nax.peer-state (~(put in nax.peer-state) nax-key)
2019-06-09 09:26:01 +03:00
::
2019-06-18 21:21:12 +03:00
peer-core
:: +on-pump-send: emit message fragment requested by |message-pump
2019-06-09 09:26:01 +03:00
::
2019-06-20 11:13:54 +03:00
++ on-pump-send
2019-06-09 09:26:01 +03:00
|= =static-fragment
^+ peer-core
:: encrypt and encode .static-fragment to .blob bitstream
::
2019-06-09 09:40:30 +03:00
%- send-shut-packet :*
our-life.channel
her-life.channel
2019-06-11 23:42:32 +03:00
bone
2019-06-09 09:40:30 +03:00
message-num.static-fragment
%& +.static-fragment
==
2019-06-20 11:13:54 +03:00
:: +on-pump-wait: relay |message-pump's set-timer request
2019-06-09 09:26:01 +03:00
::
2019-06-20 11:13:54 +03:00
++ on-pump-wait
2019-06-09 09:26:01 +03:00
|= date=@da
^+ peer-core
::
2019-06-19 02:59:25 +03:00
=/ =wire (make-pump-timer-wire her.channel bone)
=/ duct ~[/ames]
(emit duct %pass wire %b %wait date)
2019-06-20 11:13:54 +03:00
:: +on-pump-rest: relay |message-pump's unset-timer request
2019-06-09 09:26:01 +03:00
::
2019-06-20 11:13:54 +03:00
++ on-pump-rest
2019-06-09 09:26:01 +03:00
|= date=@da
^+ peer-core
::
2019-06-19 02:59:25 +03:00
=/ =wire (make-pump-timer-wire her.channel bone)
=/ duct ~[/ames]
(emit duct %pass wire %b %rest date)
2019-06-09 09:26:01 +03:00
--
:: +run-message-sink: process $message-sink-task and its effects
::
++ run-message-sink
|= [=bone task=message-sink-task]
^+ peer-core
:: pass .task to the |message-sink and apply state mutations
2019-06-08 12:45:30 +03:00
::
=/ =message-sink-state
(~(gut by rcv.peer-state) bone *message-sink-state)
2019-06-08 12:45:30 +03:00
::
=/ message-sink (make-message-sink message-sink-state channel)
=^ sink-gifts message-sink-state (work:message-sink task)
=. rcv.peer-state (~(put by rcv.peer-state) bone message-sink-state)
:: process effects from |message-sink
2019-06-08 12:45:30 +03:00
::
2019-06-20 11:13:54 +03:00
|^ ^+ peer-core
?~ sink-gifts peer-core
=* gift i.sink-gifts
2019-06-20 11:13:54 +03:00
=. peer-core
?- -.gift
%memo (on-sink-memo [message-num message]:gift)
%send (on-sink-send [message-num ack-meat]:gift)
2019-06-20 11:13:54 +03:00
==
$(sink-gifts t.sink-gifts)
:: +on-sink-send: emit ack packet as requested by |message-sink
::
++ on-sink-send
2019-06-20 11:13:54 +03:00
|= [=message-num =ack-meat]
^+ peer-core
2019-06-20 10:21:37 +03:00
::
2019-06-20 11:13:54 +03:00
%- send-shut-packet :*
our-life.channel
her-life.channel
bone
message-num
%| ack-meat
==
:: +on-sink-memo: dispatch message received by |message-sink
2019-06-20 11:13:54 +03:00
::
2019-07-28 10:50:32 +03:00
:: odd bone: %plea request message
:: even bone, 0 second bit: %boon response message
:: even bone, 1 second bit: nack-trace %boon message
2019-06-20 11:13:54 +03:00
::
++ on-sink-memo
2019-07-28 10:50:32 +03:00
?: =(1 (end 0 1 bone))
on-sink-plea
2019-07-28 10:50:32 +03:00
?: =(0 (end 0 1 (rsh 0 1 bone)))
on-sink-boon
on-sink-nack-trace
:: +on-sink-boon: handle response message received by |message-sink
2019-07-28 10:50:32 +03:00
::
:: .bone must be mapped in .ossuary.peer-state, or we crash.
:: This means a malformed message will kill a channel. We
:: could change this to a no-op if we had some sort of security
:: reporting.
::
:: TODO: This handles a previous crash in the client vane, but not in
:: Ames itself.
::
++ on-sink-boon
2019-07-28 10:50:32 +03:00
|= [=message-num message=*]
2019-06-20 11:13:54 +03:00
^+ peer-core
:: send ack unconditionally
2019-06-20 11:13:54 +03:00
::
=. peer-core (run-message-sink bone %done ok=%.y)
2019-06-20 11:13:54 +03:00
::
?. ?=([%hear * * ok=%.n] task)
:: fresh boon; give message to client vane
2019-06-25 01:18:18 +03:00
::
2019-11-15 03:10:48 +03:00
%- (trace msg.veb |.("boon {<her.channel^bone>}"))
(emit (got-duct bone) %give %boon message)
:: we previously crashed on this message; notify client vane
2019-06-20 11:13:54 +03:00
::
2019-11-15 03:10:48 +03:00
%- (trace msg.veb |.("crashed on boon {<her.channel^bone>}"))
(emit (got-duct bone) %give %lost ~)
:: +on-sink-nack-trace: handle nack-trace received by |message-sink
2019-07-28 10:50:32 +03:00
::
++ on-sink-nack-trace
2019-07-28 10:50:32 +03:00
|= [=message-num message=*]
^+ peer-core
2019-11-15 03:10:48 +03:00
%- (trace msg.veb |.("nack trace {<her.channel^bone>}"))
2019-06-20 11:13:54 +03:00
::
=+ ;; =naxplanation message
:: ack nack-trace message (only applied if we don't later crash)
2019-06-20 11:13:54 +03:00
::
=. peer-core (run-message-sink bone %done ok=%.y)
2019-06-20 11:13:54 +03:00
:: flip .bone's second bit to find referenced flow
::
=/ target-bone=^bone (mix 0b10 bone)
=/ nax-key [target-bone message-num.naxplanation]
2019-06-20 11:13:54 +03:00
:: if we haven't heard a message nack, pretend we have
::
:: The nack-trace message counts as a valid message nack on
:: the original failed message.
::
:: This prevents us from having to wait for a message nack
:: packet, which would mean we couldn't immediately ack the
:: nack-trace message, which would in turn violate the
:: semantics of backward flows.
::
=? peer-core !(~(has in nax.peer-state) nax-key)
%- run-message-pump
[target-bone %hear message-num.naxplanation %| ok=%.n lag=`@dr`0]
2019-06-20 11:13:54 +03:00
:: clear the nack from our state and relay to vane
::
=. nax.peer-state (~(del in nax.peer-state) nax-key)
::
(emit (got-duct target-bone) %give %done `error.naxplanation)
:: +on-sink-plea: handle request message received by |message-sink
2019-06-09 09:40:30 +03:00
::
++ on-sink-plea
2019-07-28 10:50:32 +03:00
|= [=message-num message=*]
^+ peer-core
2019-11-15 03:10:48 +03:00
%- (trace msg.veb |.("plea {<her.channel^bone>}"))
2019-08-06 02:05:40 +03:00
:: is this the first time we're trying to process this message?
2019-07-28 10:50:32 +03:00
::
2019-08-06 02:05:40 +03:00
?. ?=([%hear * * ok=%.n] task)
:: fresh plea; pass to client vane
::
=+ ;; =plea message
::
=/ =wire (make-bone-wire her.channel bone)
::
?+ vane.plea ~| %ames-evil-vane^our^her.channel^vane.plea !!
%a (emit duct %pass wire %a %plea her.channel plea)
2019-08-06 02:05:40 +03:00
%c (emit duct %pass wire %c %plea her.channel plea)
%g (emit duct %pass wire %g %plea her.channel plea)
%j (emit duct %pass wire %j %plea her.channel plea)
2019-08-06 02:05:40 +03:00
==
:: we previously crashed on this message; send nack
::
=. peer-core (run-message-sink bone %done ok=%.n)
:: also send nack-trace with blank .error for security
2019-08-06 02:05:40 +03:00
::
=/ nack-trace-bone=^bone (mix 0b10 bone)
=/ =naxplanation [message-num *error]
=/ =message-blob (jam naxplanation)
2019-08-06 02:05:40 +03:00
::
(run-message-pump nack-trace-bone %memo message-blob)
2019-07-28 10:50:32 +03:00
--
--
--
2019-06-01 06:32:13 +03:00
:: +make-message-pump: constructor for |message-pump
::
++ make-message-pump
|= [state=message-pump-state =channel]
=| gifts=(list message-pump-gift)
2019-05-29 05:56:05 +03:00
::
|%
2019-05-29 05:56:05 +03:00
++ message-pump .
++ give |=(gift=message-pump-gift message-pump(gifts [gift gifts]))
2019-06-19 02:33:53 +03:00
++ packet-pump (make-packet-pump packet-pump-state.state channel)
2019-05-29 05:56:05 +03:00
:: +work: handle a $message-pump-task
::
++ work
|= task=message-pump-task
^+ [gifts state]
::
2019-06-19 02:33:53 +03:00
=~ (dispatch-task task)
2019-05-29 05:56:05 +03:00
feed-packets
2019-06-20 10:21:37 +03:00
(run-packet-pump %halt ~)
[(flop gifts) state]
2019-05-29 05:56:05 +03:00
==
2019-06-19 02:33:53 +03:00
:: +dispatch-task: perform task-specific processing
::
++ dispatch-task
|= task=message-pump-task
^+ message-pump
::
?- -.task
2019-07-28 10:50:32 +03:00
%memo (on-memo message-blob.task)
2019-06-19 02:33:53 +03:00
%wake (run-packet-pump task)
2019-06-20 10:21:37 +03:00
%hear
2019-06-19 02:33:53 +03:00
?- -.ack-meat.task
2019-06-20 10:21:37 +03:00
%& (on-hear [message-num fragment-num=p.ack-meat]:task)
%| (on-done [message-num [ok lag]:p.ack-meat]:task)
2019-06-19 02:33:53 +03:00
== ==
2019-06-25 19:01:05 +03:00
:: +on-memo: handle request to send a message
2019-05-29 05:56:05 +03:00
::
2019-06-25 19:01:05 +03:00
++ on-memo
2019-07-28 10:50:32 +03:00
|= =message-blob
2019-05-29 05:56:05 +03:00
^+ message-pump
::
2019-07-28 10:50:32 +03:00
=. unsent-messages.state (~(put to unsent-messages.state) message-blob)
2019-05-29 05:56:05 +03:00
message-pump
2019-06-20 10:21:37 +03:00
:: +on-hear: handle packet acknowledgment
::
2019-06-20 10:21:37 +03:00
++ on-hear
|= [=message-num =fragment-num]
^+ message-pump
:: pass to |packet-pump unless duplicate or future ack
::
?. (is-message-num-in-range message-num)
2019-11-15 03:10:48 +03:00
%- (trace snd.veb |.("hear pump out of range"))
message-pump
2019-06-20 10:21:37 +03:00
(run-packet-pump %hear message-num fragment-num)
:: +on-done: handle message acknowledgment
2019-05-29 05:56:05 +03:00
::
2019-06-20 10:21:37 +03:00
++ on-done
2019-06-21 04:36:16 +03:00
:: check-old: loop terminator variable
::
=/ check-old=? %.y
2019-05-29 05:56:05 +03:00
|= [=message-num ok=? lag=@dr]
^+ message-pump
2019-06-21 04:36:16 +03:00
:: unsent messages from the future should never get acked
::
?> (lth message-num next.state)
:: ignore duplicate message acks
::
?: (lth message-num current.state)
message-pump
2019-06-20 03:08:38 +03:00
:: future nack implies positive ack on all earlier messages
::
2019-06-21 04:36:16 +03:00
?: &(!ok check-old)
2019-06-20 03:08:38 +03:00
|- ^+ message-pump
:: base case: current message got nacked; handle same as ack
::
?: =(message-num current.state)
2019-06-21 04:36:16 +03:00
^$(check-old %.n)
2019-06-20 03:08:38 +03:00
:: recursive case: future message got nacked
::
=. message-pump ^$(ok %.y, message-num current.state)
$
:: ignore duplicate and future acks
2019-05-29 05:56:05 +03:00
::
?. (is-message-num-in-range message-num)
2019-05-29 05:56:05 +03:00
message-pump
:: clear and print .unsent-fragments if nonempty
::
=? unsent-fragments.state
2019-06-09 09:31:42 +03:00
&(=(current next) ?=(^ unsent-fragments)):state
::
2019-08-10 21:48:40 +03:00
~> %slog.0^leaf/"ames: early message ack {<ok^her.channel>}"
2019-05-29 05:56:05 +03:00
~
:: clear all packets from this message from the packet pump
::
2019-06-20 10:21:37 +03:00
=. message-pump (run-packet-pump %done message-num lag)
:: enqueue this ack to be sent back to local client vane
2019-05-29 05:56:05 +03:00
::
=. queued-message-acks.state
(~(put by queued-message-acks.state) message-num ok)
:: emit local acks from .queued-message-acks until incomplete
2019-05-29 05:56:05 +03:00
::
|- ^+ message-pump
:: if .current hasn't been fully acked, we're done
2019-05-29 05:56:05 +03:00
::
?~ ack=(~(get by queued-message-acks.state) current.state)
2019-05-29 05:56:05 +03:00
message-pump
:: .current is complete; pop, emit local ack, and try next message
2019-05-29 05:56:05 +03:00
::
=. queued-message-acks.state
(~(del by queued-message-acks.state) current.state)
2019-05-29 05:56:05 +03:00
::
2019-06-20 10:21:37 +03:00
=. message-pump (give %done current.state ok.u.ack)
2019-05-29 05:56:05 +03:00
::
$(current.state +(current.state))
:: +is-message-num-in-range: %.y unless duplicate or future ack
::
++ is-message-num-in-range
|= =message-num
^- ?
::
?: (gte message-num next.state)
%.n
?: (lth message-num current.state)
%.n
!(~(has by queued-message-acks.state) message-num)
2019-06-01 06:32:13 +03:00
:: +feed-packets: give packets to |packet-pump until full
2019-05-29 05:56:05 +03:00
::
++ feed-packets
:: if nothing to send, no-op
::
?: &(=(~ unsent-messages) =(~ unsent-fragments)):state
2019-05-29 05:56:05 +03:00
message-pump
:: we have unsent fragments of the current message; feed them
::
?. =(~ unsent-fragments.state)
=/ res (feed:packet-pump unsent-fragments.state)
=+ [unsent packet-pump-gifts packet-pump-state]=res
2019-05-29 05:56:05 +03:00
::
=. unsent-fragments.state unsent
=. packet-pump-state.state packet-pump-state
2019-05-29 05:56:05 +03:00
::
=. message-pump (process-packet-pump-gifts packet-pump-gifts)
:: if it sent all of them, feed it more; otherwise, we're done
::
?~ unsent
feed-packets
message-pump
:: .unsent-messages is nonempty; pop a message off and feed it
::
2019-07-28 10:50:32 +03:00
=^ =message-blob unsent-messages.state ~(get to unsent-messages.state)
2019-06-01 06:32:13 +03:00
:: break .message into .chunks and set as .unsent-fragments
2019-05-29 05:56:05 +03:00
::
2019-07-28 10:50:32 +03:00
=. unsent-fragments.state (split-message next.state message-blob)
2019-06-01 06:32:13 +03:00
:: try to feed packets from the next message
2019-05-29 05:56:05 +03:00
::
=. next.state +(next.state)
2019-05-29 05:56:05 +03:00
feed-packets
2019-06-01 06:32:13 +03:00
:: +run-packet-pump: call +work:packet-pump and process results
2019-05-29 05:56:05 +03:00
::
++ run-packet-pump
|= =packet-pump-task
^+ message-pump
::
=^ packet-pump-gifts packet-pump-state.state
2019-05-29 05:56:05 +03:00
(work:packet-pump packet-pump-task)
::
(process-packet-pump-gifts packet-pump-gifts)
2019-06-01 06:32:13 +03:00
:: +process-packet-pump-gifts: pass |packet-pump effects up the chain
2019-05-29 05:56:05 +03:00
::
++ process-packet-pump-gifts
|= packet-pump-gifts=(list packet-pump-gift)
^+ message-pump
::
?~ packet-pump-gifts
message-pump
=. message-pump (give i.packet-pump-gifts)
::
$(packet-pump-gifts t.packet-pump-gifts)
--
2019-06-01 06:32:13 +03:00
:: +make-packet-pump: construct |packet-pump core
2019-05-29 05:56:05 +03:00
::
++ make-packet-pump
2019-06-01 05:03:09 +03:00
|= [state=packet-pump-state =channel]
2019-05-29 05:56:05 +03:00
=| gifts=(list packet-pump-gift)
|%
++ packet-pump .
2019-06-01 03:50:22 +03:00
++ give |=(packet-pump-gift packet-pump(gifts [+< gifts]))
2019-06-01 06:32:13 +03:00
:: +packet-queue: type for all sent fragments, ordered by sequence number
::
++ packet-queue
%- (ordered-map live-packet-key live-packet-val)
|= [a=live-packet-key b=live-packet-key]
^- ?
::
?: (lth message-num.a message-num.b)
%.y
?: (gth message-num.a message-num.b)
%.n
(lte fragment-num.a fragment-num.b)
:: +gauge: inflate a |pump-gauge to track congestion control
::
2019-06-01 05:03:09 +03:00
++ gauge (make-pump-gauge now.channel metrics.state)
:: +work: handle $packet-pump-task request
::
2019-05-29 05:56:05 +03:00
++ work
|= task=packet-pump-task
2019-06-01 05:03:09 +03:00
^+ [gifts state]
2019-05-29 05:56:05 +03:00
::
2019-06-01 05:03:09 +03:00
=- [(flop gifts) state]
::
?- -.task
2019-06-20 10:21:37 +03:00
%hear (on-hear [message-num fragment-num]:task)
%done (on-done message-num.task)
%wake on-wake
2019-06-20 10:21:37 +03:00
%halt set-wake
==
:: +on-wake: handle packet timeout
::
++ on-wake
^+ packet-pump
:: assert temporal coherence
2019-06-01 03:50:22 +03:00
::
?< =(~ next-wake.state)
?> (gte now.channel (need next-wake.state))
=. next-wake.state ~
:: tell congestion control a packet timed out
::
2019-10-02 10:46:34 +03:00
=. metrics.state on-timeout:gauge
:: re-send first packet and update its state in-place
::
2019-11-15 03:10:48 +03:00
=- =* res -
=. live.state live.res
=. packet-pump (give %send static-fragment.res)
%- %+ trace snd.veb
|.("dead {<fragment-num.static-fragment.res^show:gauge>}")
packet-pump
2019-06-01 03:50:22 +03:00
::
=| acc=static-fragment
^+ [static-fragment=acc live=live.state]
2019-06-01 03:50:22 +03:00
::
%^ (traverse:packet-queue _acc) live.state acc
|= $: acc=_acc
2019-06-01 03:50:22 +03:00
key=live-packet-key
val=live-packet-val
==
^- [new-val=(unit live-packet-val) stop=? _acc]
:: packet has expired; update it in-place, stop, and produce it
2019-06-01 03:50:22 +03:00
::
=. last-sent.val now.channel
=. retries.val +(retries.val)
2019-06-01 03:50:22 +03:00
::
2019-10-05 06:21:29 +03:00
[`val stop=%.y (to-static-fragment key val)]
:: +feed: try to send a list of packets, returning unsent and effects
::
++ feed
2019-05-29 05:56:05 +03:00
|= fragments=(list static-fragment)
2019-06-01 05:03:09 +03:00
^+ [fragments gifts state]
:: return unsent back to caller and reverse effects to finalize
::
=- [unsent (flop gifts) state]
::
^+ [unsent=fragments packet-pump]
2019-06-01 06:05:21 +03:00
:: bite off as many fragments as we can send
::
=/ num-slots num-slots:gauge
=/ sent (scag num-slots fragments)
=/ unsent (slag num-slots fragments)
::
:- unsent
^+ packet-pump
:: if nothing to send, we're done
::
?~ sent packet-pump
:: convert $static-fragment's into +ordered-set [key val] pairs
::
=/ send-list
2019-06-01 06:05:21 +03:00
%+ turn sent
|= static-fragment
^- [key=live-packet-key val=live-packet-val]
2019-06-01 06:05:21 +03:00
::
:- [message-num fragment-num]
:- [sent-date=now.channel retries=0 skips=0]
2019-06-01 06:05:21 +03:00
[num-fragments fragment]
:: update .live and .metrics
::
=. live.state (gas:packet-queue live.state send-list)
=. metrics.state (on-sent:gauge (lent send-list))
:: TMI
2019-06-01 06:05:21 +03:00
::
=> .(sent `(list static-fragment)`sent)
:: emit a $packet-pump-gift for each packet to send
::
2019-10-05 06:21:29 +03:00
%+ roll sent
|= [packet=static-fragment core=_packet-pump]
(give:core %send packet)
2019-06-20 10:21:37 +03:00
:: +on-hear: handle ack on a live packet
::
:: If the packet was in our queue, delete it and update our
:: metrics. Otherwise, no-op.
::
2019-06-20 10:21:37 +03:00
++ on-hear
|= [=message-num =fragment-num]
^+ packet-pump
::
=- :: if no sent packet matches the ack, don't apply mutations or effects
::
?. found.-
2019-11-15 03:10:48 +03:00
%- (trace snd.veb |.("miss {<show:gauge>}"))
packet-pump
::
=. metrics.state metrics.-
2019-06-01 05:03:09 +03:00
=. live.state live.-
2019-11-15 03:10:48 +03:00
%- %+ trace
?. snd.veb %.n
?| =(0 fragment-num)
=(0 (mod counter.metrics.state 20))
==
|.("{<[fragment-num show:gauge]>}")
2019-10-05 06:21:29 +03:00
:: .resends is backward, so fold backward and emit
::
2019-10-05 06:21:29 +03:00
%+ reel resends.-
|= [packet=static-fragment core=_packet-pump]
(give:core %send packet)
::
=/ acc
:* found=`?`%.n
resends=*(list static-fragment)
metrics=metrics.state
==
::
2019-10-05 06:21:29 +03:00
^+ [acc live=live.state]
::
%^ (traverse:packet-queue _acc) live.state acc
|= $: acc=_acc
key=live-packet-key
val=live-packet-val
==
^- [new-val=(unit live-packet-val) stop=? _acc]
::
=/ gauge (make-pump-gauge now.channel metrics.acc)
:: is this the acked packet?
::
?: =(key [message-num fragment-num])
:: delete acked packet, update metrics, and stop traversal
::
2019-10-05 06:21:29 +03:00
=. found.acc %.y
=. metrics.acc (on-ack:gauge -.val)
[new-val=~ stop=%.y acc]
:: ack was on later packet; mark skipped, tell gauge, and continue
::
=. skips.val +(skips.val)
=^ resend metrics.acc (on-skipped-packet:gauge -.val)
?. resend
[new-val=`val stop=%.n acc]
::
2019-10-05 06:21:29 +03:00
=. last-sent.val now.channel
=. retries.val +(retries.val)
=. resends.acc [(to-static-fragment key val) resends.acc]
[new-val=`val stop=%.n acc]
2019-06-20 10:21:37 +03:00
:: +on-done: apply ack to all packets from .message-num
::
2019-06-20 10:21:37 +03:00
++ on-done
2019-05-29 05:56:05 +03:00
|= =message-num
^+ packet-pump
::
=- =. metrics.state metrics.-
=. live.state live.-
::
2019-11-15 03:10:48 +03:00
%- (trace snd.veb |.("done {<message-num^show:gauge>}"))
packet-pump
::
2019-10-05 06:21:29 +03:00
^+ [metrics=metrics.state live=live.state]
::
%^ (traverse:packet-queue pump-metrics) live.state acc=metrics.state
|= $: metrics=pump-metrics
key=live-packet-key
val=live-packet-val
==
^- [new-val=(unit live-packet-val) stop=? pump-metrics]
::
=/ gauge (make-pump-gauge now.channel metrics)
:: if we get an out-of-order ack for a message, no-op
::
:: We need to receive message acks in order, so if we get an ack
:: for anything other than the first unacked message, pretend we
:: never heard it. If the other end is correct, the first
:: message will get acked, and we'll re-send the second message
:: once it times out.
::
:: This arrangement could probably be optimized, but it isn't
:: very likely to happen, so it's more important we stay correct.
::
?: (lth message-num.key message-num)
[new-val=`val stop=%.y metrics]
:: if packet was from acked message, delete it and continue
::
?: =(message-num.key message-num)
[new-val=~ stop=%.n metrics=(on-ack:gauge -.val)]
:: we've gone past the acked message; we're done
::
[new-val=`val stop=%.y metrics]
:: +set-wake: set, unset, or reset timer, emitting moves
::
++ set-wake
2019-05-29 05:56:05 +03:00
^+ packet-pump
:: if nonempty .live, peek at head to get next wake time
::
=/ new-wake=(unit @da)
?~ head=(peek:packet-queue live.state)
~
`next-expiry:gauge
:: no-op if no change
::
?: =(new-wake next-wake.state) packet-pump
:: unset old timer if non-null
::
=? packet-pump !=(~ next-wake.state)
=/ old (need next-wake.state)
=. next-wake.state ~
(give %rest old)
:: set new timer if non-null
::
=? packet-pump ?=(^ new-wake)
=. next-wake.state new-wake
(give %wait u.new-wake)
::
packet-pump
--
2019-10-05 06:21:29 +03:00
:: +to-static-fragment: convenience function for |packet-pump
::
2019-10-05 06:21:29 +03:00
++ to-static-fragment
|= [live-packet-key live-packet-val]
^- static-fragment
[message-num num-fragments fragment-num fragment]
2019-06-01 03:50:22 +03:00
:: +make-pump-gauge: construct |pump-gauge congestion control core
2019-06-01 06:05:21 +03:00
::
++ make-pump-gauge
2019-06-01 05:03:09 +03:00
|= [now=@da pump-metrics]
=* metrics +<+
|%
2019-06-01 06:05:21 +03:00
:: +next-expiry: when should a newly sent fresh packet time out?
2019-06-01 03:50:22 +03:00
::
:: Use rtt + 4*sigma, where sigma is the mean deviation of rtt.
:: This should make it unlikely that a packet would time out from a
:: delay, as opposed to an actual packet loss.
2019-06-01 06:05:21 +03:00
::
++ next-expiry
2019-06-01 06:05:21 +03:00
^- @da
(add now rto)
2019-06-01 06:05:21 +03:00
:: +num-slots: how many packets can we send right now?
::
++ num-slots
^- @ud
(sub-safe cwnd num-live)
:: +on-sent: adjust metrics based on sending .num-sent fresh packets
2019-06-01 03:50:22 +03:00
::
++ on-sent
|= num-sent=@ud
^- pump-metrics
::
=. num-live (add num-live num-sent)
2019-06-01 05:03:09 +03:00
metrics
2019-06-01 03:50:22 +03:00
:: +on-ack: adjust metrics based on a packet getting acknowledged
::
++ on-ack
|= =packet-state
2019-06-01 05:03:09 +03:00
^- pump-metrics
::
=. counter +(counter)
=. num-live (dec num-live)
:: if below congestion threshold, add 1; else, add avg. 1 / cwnd
2019-08-29 03:28:31 +03:00
::
=. cwnd
?: in-slow-start
+(cwnd)
(add cwnd !=(0 (mod (mug now) cwnd)))
:: if this was a re-send, don't adjust rtt or downstream state
::
?. =(0 retries.packet-state)
metrics
:: rtt-datum: new rtt measurement based on this packet roundtrip
::
=/ rtt-datum=@dr (sub-safe now last-sent.packet-state)
:: rtt-error: difference between this rtt measurement and expected
::
=/ rtt-error=@dr
?: (gte rtt-datum rtt)
(sub rtt-datum rtt)
(sub rtt rtt-datum)
:: exponential weighting ratio for .rtt and .rttvar
::
=. rtt (div (add rtt-datum (mul rtt 7)) 8)
=. rttvar (div (add rtt-error (mul rttvar 7)) 8)
=. rto (clamp-rto (add rtt (mul 4 rttvar)))
::
metrics
2019-10-05 06:21:29 +03:00
:: +on-skipped-packet: handle misordered ack
2019-06-01 03:50:22 +03:00
::
++ on-skipped-packet
|= packet-state
2019-10-05 06:21:29 +03:00
^- [resend=? pump-metrics]
2019-06-01 06:05:21 +03:00
::
2019-10-05 06:21:29 +03:00
=/ resend=? &(=(0 retries) |(in-recovery (gte skips 3)))
:- resend
::
=? cwnd !in-recovery (max 2 (div cwnd 2))
2019-11-15 03:10:48 +03:00
%- %+ trace snd.veb
|.("skip {<[resend=resend in-recovery=in-recovery show]>}")
metrics
:: +on-timeout: (re)enter slow-start mode on packet loss
2019-06-01 06:05:21 +03:00
::
++ on-timeout
2019-06-01 05:03:09 +03:00
^- pump-metrics
::
=: ssthresh (max 1 (div cwnd 2))
cwnd 1
rto (clamp-rto (mul rto 2))
==
2019-06-04 15:38:08 +03:00
metrics
:: +clamp-rto: apply min and max to an .rto value
::
++ clamp-rto
|= rto=@dr
^+ rto
(min ~m2 (max ^~((div ~s1 5)) rto))
2019-10-05 06:21:29 +03:00
:: +in-slow-start: %.y iff we're in "slow-start" mode
::
++ in-slow-start
^- ?
(lth cwnd ssthresh)
2019-10-05 06:21:29 +03:00
:: +in-recovery: %.y iff we're recovering from a skipped packet
::
:: We finish recovering when .num-live finally dips back down to
:: .cwnd.
::
++ in-recovery
^- ?
(gth num-live cwnd)
:: +sub-safe: subtract with underflow protection
::
++ sub-safe
|= [a=@ b=@]
^- @
?:((lte a b) 0 (sub a b))
2019-10-05 06:21:29 +03:00
:: +show: produce a printable version of .metrics
::
++ show
=/ ms (div ~s1 1.000)
::
:* rto=(div rto ms)
rtt=(div rtt ms)
rttvar=(div rttvar ms)
ssthresh=ssthresh
cwnd=cwnd
num-live=num-live
counter=counter
==
--
:: +make-message-sink: construct |message-sink message receiver core
::
++ make-message-sink
|= [state=message-sink-state =channel]
=| gifts=(list message-sink-gift)
|%
++ message-sink .
++ give |=(message-sink-gift message-sink(gifts [+< gifts]))
++ work
|= task=message-sink-task
2019-06-09 20:32:15 +03:00
^+ [gifts state]
::
2019-06-09 20:32:15 +03:00
=- [(flop gifts) state]
::
?- -.task
2019-06-20 10:21:37 +03:00
%done (on-done ok.task)
%drop (on-drop message-num.task)
2019-08-06 02:05:40 +03:00
%hear (on-hear [lane shut-packet ok]:task)
==
:: +on-hear: receive message fragment, possibly completing message
::
++ on-hear
2019-08-06 02:05:40 +03:00
|= [=lane =shut-packet ok=?]
^+ message-sink
2019-06-09 20:32:15 +03:00
:: we know this is a fragment, not an ack; expose into namespace
::
2019-06-09 20:32:15 +03:00
?> ?=(%& -.meat.shut-packet)
=+ [num-fragments fragment-num fragment]=+.meat.shut-packet
:: seq: message sequence number, for convenience
::
=/ seq message-num.shut-packet
:: ignore messages from far future; limit to 10 in progress
::
?: (gte seq (add 10 last-acked.state))
2019-11-15 03:10:48 +03:00
%- (trace odd.veb |.("future %hear {<seq^last-acked.state>}"))
message-sink
2019-06-09 20:32:15 +03:00
::
=/ is-last-fragment=? =(+(fragment-num) num-fragments)
:: always ack a dupe!
::
?: (lte seq last-acked.state)
?. is-last-fragment
:: single packet ack
::
2019-11-15 03:10:48 +03:00
%- (trace rcv.veb |.("send dupe ack {<seq^fragment-num>}"))
2019-06-20 10:21:37 +03:00
(give %send seq %& fragment-num)
2019-06-19 03:38:25 +03:00
:: whole message (n)ack
2019-06-09 20:32:15 +03:00
::
2019-08-21 00:44:44 +03:00
=/ ok=? !(~(has in nax.state) seq)
2019-11-15 03:10:48 +03:00
%- (trace rcv.veb |.("send dupe message ack {<seq>} ok={<ok>}"))
2019-06-20 10:21:37 +03:00
(give %send seq %| ok lag=`@dr`0)
2019-06-09 20:32:15 +03:00
:: last-acked<seq<=last-heard; heard message, unprocessed
::
?: (lte seq last-heard.state)
?: is-last-fragment
:: drop last packet since we don't know whether to ack or nack
::
2019-11-15 03:10:48 +03:00
%- (trace rcv.veb |.("hear last in-progress {<her.channel^seq>}"))
message-sink
2019-06-09 20:32:15 +03:00
:: ack all other packets
::
2019-11-15 03:10:48 +03:00
%- (trace rcv.veb |.("send ack {<seq^fragment-num>}"))
2019-06-20 10:21:37 +03:00
(give %send seq %& fragment-num)
2019-06-09 20:32:15 +03:00
:: last-heard<seq<10+last-heard; this is a packet in a live message
::
=/ =partial-rcv-message
:: create default if first fragment
::
?~ existing=(~(get by live-messages.state) seq)
[num-fragments num-received=0 fragments=~]
:: we have an existing partial message; check parameters match
::
?> (gth num-fragments.u.existing fragment-num)
?> =(num-fragments.u.existing num-fragments)
::
u.existing
::
2019-08-01 06:14:28 +03:00
=/ already-heard-fragment=?
(~(has by fragments.partial-rcv-message) fragment-num)
2019-06-09 20:32:15 +03:00
:: ack dupes except for the last fragment, in which case drop
::
2019-08-01 06:14:28 +03:00
?: already-heard-fragment
2019-06-09 20:32:15 +03:00
?: is-last-fragment
2019-11-15 03:10:48 +03:00
%- (trace rcv.veb |.("hear last dupe {<her.channel^seq>}"))
message-sink
2019-11-15 03:10:48 +03:00
%- (trace rcv.veb |.("send dupe ack {<her.channel^seq^fragment-num>}"))
2019-06-20 10:21:37 +03:00
(give %send seq %& fragment-num)
2019-06-09 20:32:15 +03:00
:: new fragment; store in state and check if message is done
::
=. num-received.partial-rcv-message
+(num-received.partial-rcv-message)
::
=. fragments.partial-rcv-message
(~(put by fragments.partial-rcv-message) fragment-num fragment)
::
=. live-messages.state
(~(put by live-messages.state) seq partial-rcv-message)
:: ack any packet other than the last one, and continue either way
::
=? message-sink !is-last-fragment
2019-11-15 03:10:48 +03:00
%- (trace rcv.veb |.("send ack {<seq^fragment-num>}"))
2019-06-20 10:21:37 +03:00
(give %send seq %& fragment-num)
2019-06-09 20:32:15 +03:00
:: enqueue all completed messages starting at +(last-heard.state)
::
|- ^+ message-sink
2019-06-09 20:32:15 +03:00
:: if this is not the next message to ack, we're done
::
?. =(seq +(last-heard.state))
message-sink
2019-06-09 20:32:15 +03:00
:: if we haven't heard anything from this message, we're done
::
?~ live=(~(get by live-messages.state) seq)
message-sink
2019-06-09 20:32:15 +03:00
:: if the message isn't done yet, we're done
::
?. =(num-received num-fragments):u.live
message-sink
2019-06-09 20:32:15 +03:00
:: we have whole message; update state, assemble, and send to vane
::
=. last-heard.state +(last-heard.state)
=. live-messages.state (~(del by live-messages.state) seq)
::
2019-11-15 03:10:48 +03:00
%- (trace msg.veb |.("hear {<her.channel>} {<num-fragments.u.live>}kb"))
2019-07-28 10:50:32 +03:00
=/ message=* (assemble-fragments [num-fragments fragments]:u.live)
=. message-sink (enqueue-to-vane seq message)
2019-06-09 20:32:15 +03:00
::
$(seq +(seq))
2019-06-19 03:38:25 +03:00
:: +enqueue-to-vane: enqueue message to be sent to local vane
2019-06-09 20:32:15 +03:00
::
++ enqueue-to-vane
2019-07-28 10:50:32 +03:00
|= [seq=message-num message=*]
^+ message-sink
2019-06-09 20:32:15 +03:00
::
=/ empty=? =(~ pending-vane-ack.state)
=. pending-vane-ack.state (~(put to pending-vane-ack.state) seq message)
?. empty
message-sink
2019-06-20 10:21:37 +03:00
(give %memo seq message)
2019-06-11 03:31:50 +03:00
:: +on-done: handle confirmation of message processing from vane
::
++ on-done
2019-06-18 21:21:12 +03:00
|= ok=?
^+ message-sink
::
2019-06-11 03:31:50 +03:00
=^ pending pending-vane-ack.state ~(get to pending-vane-ack.state)
2019-06-19 03:38:25 +03:00
=/ =message-num message-num.p.pending
::
=. last-acked.state +(last-acked.state)
=? nax.state !ok (~(put in nax.state) message-num)
2019-06-11 03:31:50 +03:00
::
2019-06-20 10:21:37 +03:00
(give %send message-num %| ok lag=`@dr`0)
:: +on-drop: drop .message-num from our .nax state
2019-06-19 03:38:25 +03:00
::
2019-06-20 10:21:37 +03:00
++ on-drop
2019-06-19 03:38:25 +03:00
|= =message-num
^+ message-sink
2019-06-19 03:38:25 +03:00
::
=. nax.state (~(del in nax.state) message-num)
::
message-sink
--
:: +qos-update-text: notice text for if connection state changes
::
++ qos-update-text
|= [=ship old=qos new=qos]
^- (unit tape)
::
?+ [-.old -.new] ~
[%unborn %live] `"; {(scow %p ship)} is your neighbor"
[%dead %live] `"; {(scow %p ship)} is ok"
2019-08-23 06:53:45 +03:00
[%live %dead] `"; {(scow %p ship)} not responding still trying"
[%unborn %dead] `"; {(scow %p ship)} not responding still trying"
[%live %unborn] `"; {(scow %p ship)} is dead"
[%dead %unborn] `"; {(scow %p ship)} is dead"
==
2019-06-20 10:39:38 +03:00
:: +split-message: split message into kilobyte-sized fragments
::
++ split-message
2019-07-28 10:50:32 +03:00
|= [=message-num =message-blob]
2019-06-20 10:39:38 +03:00
^- (list static-fragment)
::
2019-07-28 10:50:32 +03:00
=/ fragments=(list fragment) (rip 13 message-blob)
2019-06-20 10:39:38 +03:00
=/ num-fragments=fragment-num (lent fragments)
=| counter=@
::
|- ^- (list static-fragment)
?~ fragments ~
::
:- [message-num num-fragments counter i.fragments]
::
$(fragments t.fragments, counter +(counter))
2019-06-09 20:32:15 +03:00
:: +assemble-fragments: concatenate fragments into a $message
::
++ assemble-fragments
|= [num-fragments=fragment-num fragments=(map fragment-num fragment)]
^- *
2019-06-09 20:32:15 +03:00
::
=| sorted=(list fragment)
=. sorted
=/ index=fragment-num 0
|- ^+ sorted
?: =(index num-fragments)
sorted
$(index +(index), sorted [(~(got by fragments) index) sorted])
::
%- cue
%+ can 13
%+ turn (flop sorted)
|=(a=@ [1 a])
:: +bind-duct: find or make new $bone for .duct in .ossuary
2019-06-21 00:46:31 +03:00
::
++ bind-duct
2019-06-09 09:26:01 +03:00
|= [=ossuary =duct]
^+ [next-bone.ossuary ossuary]
::
?^ existing=(~(get by by-duct.ossuary) duct)
[u.existing ossuary]
::
:- next-bone.ossuary
2019-06-19 02:42:58 +03:00
:+ (add 4 next-bone.ossuary)
2019-06-09 09:26:01 +03:00
(~(put by by-duct.ossuary) duct next-bone.ossuary)
(~(put by by-bone.ossuary) next-bone.ossuary duct)
2019-06-19 03:38:25 +03:00
:: +make-bone-wire: encode ship and bone in wire for sending to vane
::
++ make-bone-wire
|= [her=ship =bone]
^- wire
::
/bone/(scot %p her)/(scot %ud bone)
:: +parse-bone-wire: decode ship and bone from wire from local vane
::
++ parse-bone-wire
|= =wire
^- [her=ship =bone]
::
2019-08-01 00:21:07 +03:00
~| %ames-wire-bone^wire
2019-06-19 03:38:25 +03:00
?> ?=([%bone @ @ ~] wire)
[`@p`(slav %p i.t.wire) `@ud`(slav %ud i.t.t.wire)]
2019-06-19 02:59:25 +03:00
:: +make-pump-timer-wire: construct wire for |packet-pump timer
::
2019-06-19 02:59:25 +03:00
++ make-pump-timer-wire
|= [her=ship =bone]
^- wire
/pump/(scot %p her)/(scot %ud bone)
2019-06-19 02:59:25 +03:00
:: +parse-pump-timer-wire: parse .her and .bone from |packet-pump wire
::
++ parse-pump-timer-wire
|= =wire
^- [her=ship =bone]
::
2019-08-01 00:21:07 +03:00
~| %ames-wire-timer^wire
2019-06-19 02:59:25 +03:00
?> ?=([%pump @ @ ~] wire)
[`@p`(slav %p i.t.wire) `@ud`(slav %ud i.t.t.wire)]
2019-06-26 21:45:07 +03:00
:: +sign-open-packet: sign the contents of an $open-packet
::
++ sign-open-packet
|= [=private-key signed=_+:*open-packet]
^- signature
::
(sign:ed:crypto private-key (jam signed))
2019-06-18 02:23:32 +03:00
:: +verify-signature: use .public-key to verify .signature on .content
::
++ verify-signature
|= [content=@ =public-key =signature]
^- ?
::
(veri:ed:crypto signature content public-key)
:: +derive-symmetric-key: $symmetric-key from $private-key and $public-key
::
:: Assumes keys have a tag on them like the result of the |ex:crub core.
::
++ derive-symmetric-key
|= [=public-key =private-key]
^- symmetric-key
::
?> =('b' (end 3 1 public-key))
=. public-key (rsh 8 1 (rsh 3 1 public-key))
::
?> =('B' (end 3 1 private-key))
=. private-key (rsh 8 1 (rsh 3 1 private-key))
::
`@`(shar:ed:crypto public-key private-key)
2019-05-28 06:59:53 +03:00
:: +encrypt: encrypt $shut-packet into atomic packet content
::
++ encrypt
|= [=symmetric-key plaintext=shut-packet]
^- @
::
(en:crub:crypto symmetric-key (jam plaintext))
:: +decrypt: decrypt packet content to a $shut-packet or die
::
++ decrypt
|= [=symmetric-key ciphertext=@]
^- shut-packet
::
;; shut-packet
%- cue
%- need
(de:crub:crypto symmetric-key ciphertext)
2019-05-25 08:53:29 +03:00
:: +encode-packet: serialize a packet into a bytestream
2019-05-25 05:03:33 +03:00
::
++ encode-packet
|= packet
2019-05-25 08:53:29 +03:00
^- blob
2019-05-25 05:03:33 +03:00
::
=/ sndr-meta (encode-ship-metadata sndr)
=/ rcvr-meta (encode-ship-metadata rcvr)
2019-05-25 05:03:33 +03:00
:: body: <<sndr rcvr (jam [origin content])>>
::
2019-05-25 08:53:29 +03:00
:: The .sndr and .rcvr ship addresses are encoded with fixed
:: lengths specified by the packet header. They live outside
:: the jammed-data section to simplify packet filtering in the
:: interpreter.
::
2019-05-25 05:03:33 +03:00
=/ body=@
;: mix
sndr
(lsh 3 size.sndr-meta rcvr)
2019-05-25 05:03:33 +03:00
(lsh 3 (add size.sndr-meta size.rcvr-meta) (jam [origin content]))
==
:: header: 32-bit header assembled from bitstreams of fields
::
:: <<version checksum sndr-rank rcvr-rank encryption-type unused>>
:: 4 bits at the end of the header are unused.
::
2019-05-25 08:53:29 +03:00
=/ header=@
2019-05-25 05:03:33 +03:00
%+ can 0
:~ [3 protocol-version]
[20 (mug body)]
[2 rank.sndr-meta]
[2 rank.rcvr-meta]
[5 ?:(encrypted %0 %1)]
==
:: result is <<header body>>
::
(mix header (lsh 5 1 body))
2019-05-25 08:53:29 +03:00
:: +decode-packet: deserialize packet from bytestream or crash
2019-05-25 05:03:33 +03:00
::
++ decode-packet
2019-05-25 08:53:29 +03:00
|= =blob
2019-05-25 05:03:33 +03:00
^- packet
:: first 32 (2^5) bits are header; the rest is body
::
=/ header (end 5 1 blob)
=/ body (rsh 5 1 blob)
::
=/ version (end 0 3 header)
=/ checksum (cut 0 [3 20] header)
=/ sndr-size (decode-ship-size (cut 0 [23 2] header))
=/ rcvr-size (decode-ship-size (cut 0 [25 2] header))
=/ encrypted ?+((cut 0 [27 5] header) !! %0 %.y, %1 %.n)
::
=/ =dyad
:- sndr=(end 3 sndr-size body)
rcvr=(cut 3 [sndr-size rcvr-size] body)
::
2019-08-01 00:21:07 +03:00
?. =(protocol-version version)
~| %ames-protocol^version^dyad !!
?. =(checksum (end 0 20 (mug body)))
~| %ames-checksum^dyad !!
::
=+ ~| %ames-invalid-packet
;; [origin=(unit lane) content=*]
~| %ames-invalid-noun
2019-05-25 05:03:33 +03:00
%- cue
(rsh 3 (add rcvr-size sndr-size) body)
::
[dyad encrypted origin content]
:: +decode-ship-size: decode a 2-bit ship type specifier into a byte width
::
:: Type 0: galaxy or star -- 2 bytes
:: Type 1: planet -- 4 bytes
:: Type 2: moon -- 8 bytes
:: Type 3: comet -- 16 bytes
::
++ decode-ship-size
|= rank=@
^- @
::
?+ rank !!
%0 2
%1 4
%2 8
%3 16
==
2019-05-25 08:53:29 +03:00
:: +encode-ship-metadata: produce size (in bytes) and address rank for .ship
2019-05-25 05:03:33 +03:00
::
:: 0: galaxy or star
:: 1: planet
:: 2: moon
:: 3: comet
::
++ encode-ship-metadata
|= =ship
^- [size=@ =rank]
::
=/ size=@ (met 3 ship)
::
?: (lte size 2) [2 %0]
?: (lte size 4) [4 %1]
?: (lte size 8) [8 %2]
[16 %3]
--