@@ -0,0 +1,926 @@
1+/+ tiny
2+!.
3+=> => tiny
4+:: Laconic bit
5+::
6+=| lac=?
7+:: Constants
8+::
9+|%
10+:: Transfers on L1 to this address count as depositing to L2
11+::
12+++ deposit-address 0x1111.1111.1111.1111.1111.1111.1111.1111.1111.1111
13+++ log-names
14+ |%
15+ :: Generated with (keccak-256:keccak:crypto (as-octs:mimes:html name))
16+ ::
17+ :: OwnerChanged(uint32,address)
18+ ++ owner-changed
19+ 0x16d0.f539.d49c.6cad.822b.767a.9445.bfb1.
20+ cf7e.a6f2.a6c2.b120.a7ea.4cc7.660d.8fda
21+ ::
22+ :: Activated(uint32)
23+ ++ activated
24+ 0xe74c.0380.9d07.69e1.b1f7.06cc.8414.258c.
25+ d1f3.b6fe.020c.d15d.0165.c210.ba50.3a0f
26+ ::
27+ :: Spawned(uint32,uint32)
28+ ++ spawned
29+ 0xb2d3.a6e7.a339.f5c8.ff96.265e.2f03.a010.
30+ a854.1070.f374.4a24.7090.9644.1508.1546
31+ ::
32+ :: OwnershipTransferred(address,address)
33+ ++ ownership-transferred
34+ 0x8be0.079c.5316.5914.1344.cd1f.d0a4.f284.
35+ 1949.7f97.22a3.daaf.e3b4.186f.6b64.57e0
36+ ::
37+ :: EscapeRequested(uint32,uint32)
38+ ++ escape-requested
39+ 0xb4d4.850b.8f21.8218.141c.5665.cba3.79e5.
40+ 3e9b.b015.b51e.8d93.4be7.0210.aead.874a
41+ ::
42+ :: EscapeCanceled(uint32,uint32)
43+ ++ escape-canceled
44+ 0xd653.bb0e.0bb7.ce83.93e6.24d9.8fbf.17cd.
45+ a590.2c83.28ed.0cd0.9988.f368.90d9.932a
46+ ::
47+ :: EscapeAccepted(uint32,uint32)
48+ ++ escape-accepted
49+ 0x7e44.7c9b.1bda.4b17.4b07.96e1.00bf.7f34.
50+ ebf3.6dbb.7fe6.6549.0b1b.fce6.246a.9da5
51+ ::
52+ :: LostSponsor(uint32,uint32)
53+ ++ lost-sponsor
54+ 0xd770.4f9a.2519.3dbd.0b0c.b4a8.09fe.ffff.
55+ a7f1.9d1a.ae88.17a7.1346.c194.4482.10d5
56+ ::
57+ :: ChangedKeys(uint32,bytes32,bytes32,uint32,uint32)
58+ ++ changed-keys
59+ 0xaa10.e7a0.117d.4323.f1d9.9d63.0ec1.69be.
60+ bb3a.988e.8957.70e3.5198.7e01.ff54.23d5
61+ ::
62+ :: BrokeContinuity(uint32,uint32)
63+ ++ broke-continuity
64+ 0x2929.4799.f1c2.1a37.ef83.8e15.f79d.d91b.
65+ cee2.df99.d63c.d1c1.8ac9.68b1.2951.4e6e
66+ ::
67+ :: ChangedSpawnProxy(uint32,address)
68+ ++ changed-spawn-proxy
69+ 0x9027.36af.7b3c.efe1.0d9e.840a.ed0d.687e.
70+ 35c8.4095.122b.2505.1a20.ead8.866f.006d
71+ ::
72+ :: ChangedTransferProxy(uint32,address)
73+ ++ changed-transfer-proxy
74+ 0xcfe3.69b7.197e.7f0c.f067.93ae.2472.a9b1.
75+ 3583.fecb.ed2f.78df.a14d.1f10.796b.847c
76+ ::
77+ :: ChangedManagementProxy(uint32,address)
78+ ++ changed-management-proxy
79+ 0xab9c.9327.cffd.2acc.168f.afed.be06.139f.
80+ 5f55.cb84.c761.df05.e051.1c25.1e2e.e9bf
81+ ::
82+ :: ChangedVotingProxy(uint32,address)
83+ ++ changed-voting-proxy
84+ 0xcbd6.269e.c714.57f2.c7b1.a227.74f2.46f6.
85+ c5a2.eae3.795e.d730.0db5.1768.0c61.c805
86+ ::
87+ :: ChangedDns(string,string,string)
88+ ++ changed-dns
89+ 0xfafd.04ad.e1da.ae2e.1fdb.0fc1.cc6a.899f.
90+ d424.063e.d5c9.2120.e67e.0730.53b9.4898
91+ ::
92+ :: ApprovalForAll(address,address,bool)
93+ ++ approval-for-all
94+ 0x1730.7eab.39ab.6107.e889.9845.ad3d.59bd.
95+ 9653.f200.f220.9204.89ca.2b59.3769.6c31
96+ --
97+-- =>
98+:: Types
99+|%
100+:: ethereum address, 20 bytes.
101+::
102++$ address @ux
103++$ nonce @ud
104++$ dominion ?(%l1 %l2 %spawn)
105++$ keys [=life suite=@ud auth=@ crypt=@]
106+++ orm ((on ship point) por)
107+++ point
108+ $: :: domain
109+ ::
110+ =dominion
111+ ::
112+ :: ownership
113+ ::
114+ $= own
115+ $: owner=[=address =nonce]
116+ spawn-proxy=[=address =nonce]
117+ management-proxy=[=address =nonce]
118+ voting-proxy=[=address =nonce]
119+ transfer-proxy=[=address =nonce]
120+ ==
121+ ::
122+ :: networking
123+ ::
124+ $= net
125+ $: rift=@ud
126+ =keys
127+ sponsor=[has=? who=@p]
128+ escape=(unit @p)
129+ ==
130+ ==
131+::
132+++ diff
133+ $% [%nonce =ship =proxy =nonce]
134+ [%tx =raw-tx err=(unit @tas)]
135+ [%operator owner=address operator=address approved=?]
136+ [%dns domains=(list @t)]
137+ $: %point =ship
138+ $% [%rift =rift]
139+ [%keys =keys]
140+ [%sponsor sponsor=(unit @p)]
141+ [%escape to=(unit @p)]
142+ [%owner =address]
143+ [%spawn-proxy =address]
144+ [%management-proxy =address]
145+ [%voting-proxy =address]
146+ [%transfer-proxy =address]
147+ [%dominion =dominion]
148+ == == ==
149+::
150++$ state
151+ $: %0
152+ =points
153+ =operators
154+ dns=(list @t)
155+ ==
156++$ points (tree [ship point])
157++$ operators (jug address address)
158++$ effects (list diff)
159++$ proxy ?(%own %spawn %manage %vote %transfer)
160++$ roll (list raw-tx)
161++$ raw-tx [sig=@ raw=octs =tx]
162++$ tx [from=[=ship =proxy] skim-tx]
163++$ skim-tx
164+ $% [%transfer-point =address reset=?]
165+ [%spawn =ship =address]
166+ [%configure-keys encrypt=@ auth=@ crypto-suite=@ breach=?]
167+ [%escape parent=ship]
168+ [%cancel-escape parent=ship]
169+ [%adopt =ship]
170+ [%reject =ship]
171+ [%detach =ship]
172+ [%set-management-proxy =address]
173+ [%set-spawn-proxy =address]
174+ [%set-transfer-proxy =address]
175+ ==
176+::
177++$ event-log
178+ $: address=@ux
179+ data=@ux
180+ topics=(lest @ux)
181+ ==
182++$ input
183+ $: block=@ud
184+ $% [%bat batch=@]
185+ [%log =event-log]
186+ == ==
187+:: ECDSA verifier.
188+::
189+:: Must keccak `dat` and recover the ethereum address which signed.
190+:: Must not crash. `v` will normally be between 0 and 3; if it is not,
191+:: should produce null.
192+::
193++$ verifier $-([dat=octs v=@ r=@ s=@] (unit address))
194+-- =>
195+::
196+|%
197+++ debug
198+ |* [meg=@t *]
199+ ?: lac
200+ +<+
201+ ~> %slog.[0 meg]
202+ +<+
203+::
204+++ parse-roll
205+ |= batch=@
206+ =| =roll
207+ =| pos=@ud
208+ =/ las (met 0 batch)
209+ |- ^+ roll
210+ ?: (gte pos las)
211+ (flop roll)
212+ =/ parse-result (parse-raw-tx pos batch)
213+ :: Parsing failed, abort batch
214+ ::
215+ ?~ parse-result
216+ (debug %parse-failed ~)
217+ =^ =raw-tx pos u.parse-result
218+ $(roll [raw-tx roll])
219+::
220+++ parse-raw-tx
221+ |= [pos=@ud batch=@]
222+ ^- (unit [raw-tx pos=@ud])
223+ |^
224+ =^ sig pos (take 3 65)
225+ =/ res=(unit [=tx pos=@ud]) parse-tx
226+ ?~ res ~
227+ =/ dif (sub pos.u.res pos)
228+ =/ len =>((dvr dif 8) ?>(=(0 q) p))
229+ :- ~ :_ pos.u.res
230+ [sig [len (cut 0 [pos dif] batch)] tx.u.res]
231+ ::
232+ ++ parse-tx
233+ ^- (unit [tx pos=@ud])
234+ =^ from-proxy=@ pos (take 0 3)
235+ ?. ?=(?(%0 %1 %2 %3 %4) from-proxy) (debug %bad-proxy ~)
236+ =/ =proxy
237+ ?- from-proxy
238+ %0 %own
239+ %1 %spawn
240+ %2 %manage
241+ %3 %vote
242+ %4 %transfer
243+ ==
244+ =^ pad pos (take 0 5)
245+ =^ from-ship=ship pos (take 3 4)
246+ =- ?~ res
247+ ~
248+ `[[[from-ship proxy] skim-tx.u.res] pos.u.res]
249+ ^- res=(unit [=skim-tx pos=@ud])
250+ =^ op pos (take 0 7)
251+ ?+ op (debug %strange-opcode ~)
252+ %0
253+ =^ reset=@ pos (take 0)
254+ =^ =address pos (take 3 20)
255+ `[[%transfer-point address =(0 reset)] pos]
256+ ::
257+ %1
258+ =^ pad=@ pos (take 0)
259+ =^ =ship pos (take 3 4)
260+ =^ =address pos (take 3 20)
261+ `[[%spawn ship address] pos]
262+ ::
263+ %2
264+ =^ breach=@ pos (take 0)
265+ =^ encrypt=@ pos (take 3 32)
266+ =^ auth=@ pos (take 3 32)
267+ =^ crypto-suite=@ pos (take 3 4)
268+ `[[%configure-keys encrypt auth crypto-suite =(0 breach)] pos]
269+ ::
270+ %3 =^(res pos take-ship `[[%escape res] pos])
271+ %4 =^(res pos take-ship `[[%cancel-escape res] pos])
272+ %5 =^(res pos take-ship `[[%adopt res] pos])
273+ %6 =^(res pos take-ship `[[%reject res] pos])
274+ %7 =^(res pos take-ship `[[%detach res] pos])
275+ %8 =^(res pos take-address `[[%set-management-proxy res] pos])
276+ %9 =^(res pos take-address `[[%set-spawn-proxy res] pos])
277+ %10 =^(res pos take-address `[[%set-transfer-proxy res] pos])
278+ ==
279+ ::
280+ :: Take a bite
281+ ::
282+ ++ take
283+ |= =bite
284+ ^- [@ @ud]
285+ =/ =step
286+ ?@ bite (bex bite)
287+ (mul step.bite (bex bloq.bite))
288+ [(cut 0 [pos step] batch) (add pos step)]
289+ :: Encode ship and address
290+ ::
291+ ++ take-address
292+ ^- [address @ud]
293+ =^ pad=@ pos (take 0)
294+ =^ =address pos (take 3 20)
295+ [address pos]
296+ :: Encode escape-related txs
297+ ::
298+ ++ take-ship
299+ ^- [ship @ud]
300+ =^ pad=@ pos (take 0)
301+ =^ other=ship pos (take 3 4)
302+ [other pos]
303+ --
304+::
305+++ proxy-from-point
306+ |= [=proxy point]
307+ ^- [=address =nonce]
308+ ?- proxy
309+ %own owner.own
310+ %spawn spawn-proxy.own
311+ %manage management-proxy.own
312+ %vote voting-proxy.own
313+ %transfer transfer-proxy.own
314+ ==
315+::
316+++ verify-sig-and-nonce
317+ |= [=verifier chain-t=@t =state =raw-tx]
318+ ^- ?
319+ |^
320+ =/ point (get-point state ship.from.tx.raw-tx)
321+ ?> ?=(^ point) :: we never parse more than four bytes for a ship
322+ =/ need=[=address =nonce]
323+ (proxy-from-point proxy.from.tx.raw-tx u.point)
324+ :: We include a domain separator to avoid letting signatures be
325+ :: accidentally reused with other applications. We include the name
326+ :: UrbitID, a signature format version number, and the EIP-155 chain
327+ :: ID.
328+ ::
329+ :: We also include a nonce so that a transaction cannot be
330+ :: rebroadcast.
331+ ::
332+ =/ prepared-data=octs
333+ %: cad 3
334+ 14^'UrbitIDV1Chain'
335+ (met 3 chain-t)^chain-t
336+ 1^':'
337+ 4^nonce.need
338+ raw.raw-tx
339+ ~
340+ ==
341+ :: Wallets which support personal_sign include this preamble to avoid
342+ :: letting personal_sign be used to sign ethereum transactions
343+ ::
344+ =/ signed-data=octs
345+ =/ len (ud-to-ascii p.prepared-data)
346+ %: cad 3
347+ 26^'\19Ethereum Signed Message:\0a'
348+ (met 3 len)^len
349+ prepared-data
350+ ~
351+ ==
352+ =/ dress (verify-sig sig.raw-tx signed-data)
353+ ?~ dress
354+ |
355+ =(address.need u.dress)
356+ :: Verify signature and produce signer address
357+ ::
358+ ++ verify-sig
359+ |= [sig=@ txdata=octs]
360+ ^- (unit address)
361+ |^
362+ :: Reversed of the usual r-s-v order because Ethereum integers are
363+ :: big-endian
364+ ::
365+ =^ v sig (take 3)
366+ =^ s sig (take 3 32)
367+ =^ r sig (take 3 32)
368+ :: In Ethereum, v is generally 27 + recid, and verifier expects a
369+ :: recid. Old versions of geth used 0 + recid, so most software
370+ :: now supports either format. See:
371+ ::
372+ :: https://github.com/ethereum/go-ethereum/issues/2053
373+ ::
374+ =? v (gte v 27) (sub v 27)
375+ (verifier txdata v r s)
376+ ::
377+ ++ take
378+ |= =bite
379+ [(end bite sig) (rsh bite sig)]
380+ --
381+ --
382+:: ASCII-decimal encode
383+::
384+++ ud-to-ascii
385+ |= n=@ud
386+ ?~ n '0'
387+ =| l=(list @)
388+ |- ^- @t
389+ ?~ n (rep 3 l)
390+ =+ (dvr n 10)
391+ $(n p, l [(add '0' q) l])
392+::
393+++ ship-rank
394+ |= =ship
395+ ^- ?(%0 %1 %2 %3 %4)
396+ ?: (lth ship 0x100) %0
397+ ?: (lth ship 0x1.0000) %1
398+ ?: (lth ship 0x1.0000.0000) %2
399+ ?: (lth ship 0x1.0000.0000.0000.0000) %3
400+ %4
401+::
402+++ sein :: autoboss
403+ |= who=ship
404+ ^- ship
405+ =/ mir (ship-rank who)
406+ ?- mir
407+ %0 who
408+ %1 (end 3 who)
409+ %2 (end 4 who)
410+ %3 (end 5 who)
411+ %4 (end 4 who)
412+ ==
413+::
414+:: Produces null only if ship is not a galaxy, star, or planet
415+::
416+++ get-point
417+ |= [=state =ship]
418+ ^- (unit point)
419+ =/ existing (get:orm points.state ship)
420+ ?^ existing
421+ `u.existing
422+ =| =point
423+ =. who.sponsor.net.point (sein ship)
424+ ?+ (ship-rank ship) (debug %strange-point ~)
425+ %0 `point(dominion %l1)
426+ ?(%1 %2)
427+ =/ existing-parent $(ship (sein ship))
428+ ?~ existing-parent ~
429+ :- ~
430+ %= point
431+ dominion
432+ ?- dominion.u.existing-parent
433+ %l1 %l1
434+ %l2 %l2
435+ %spawn %l2
436+ ==
437+ ==
438+ ==
439+-- =>
440+|%
441+:: Receive log from L1 transaction
442+::
443+++ receive-log
444+ |= [=state log=event-log]
445+ ^- [effects ^state]
446+ =* log-name i.topics.log
447+ ?: =(log-name activated:log-names) `state
448+ ?: =(log-name spawned:log-names) `state
449+ ?: =(log-name ownership-transferred:log-names) `state
450+ ?: =(log-name changed-dns:log-names)
451+ ?> ?=(~ t.topics.log)
452+ =/ words (rip 8 data.log)
453+ :: This is only true if each domain is <= 32 bytes
454+ ::
455+ ?. ?=([c=@ @ b=@ @ a=@ @ @ @ @ ~] words) `state
456+ =* one &5.words
457+ =* two &3.words
458+ =* tri &1.words
459+ =/ domains ~[(swp 3 one) (swp 3 two) (swp 3 tri)]
460+ :- [%dns domains]~
461+ state(dns domains)
462+ ::
463+ ?: =(log-name approval-for-all:log-names)
464+ ?> ?=([@ @ ~] t.topics.log)
465+ =* owner i.t.topics.log
466+ =* operator i.t.t.topics.log
467+ =/ approved !=(0 data.log)
468+ :- [%operator owner operator approved]~
469+ =- state(operators -)
470+ ?: approved
471+ (~(put ju operators.state) owner operator)
472+ (~(del ju operators.state) owner operator)
473+ ::
474+ :: The rest of the logs modify a particular ship, specified in the
475+ :: second topic. We fetch it, and insert the modification back into
476+ :: our state.
477+ ::
478+ ?> ?=([@ *] t.topics.log)
479+ =* ship=@ i.t.topics.log
480+ =/ the-point (get-point state ship)
481+ ?> ?=(^ the-point)
482+ =* point u.the-point
483+ ::
484+ :: Important to fully no-op on failure so we don't insert an entry
485+ :: into points.state
486+ ::
487+ =- ?~ res
488+ `state
489+ [effects.u.res state(points (put:orm points.state ship new-point.u.res))]
490+ ^- res=(unit [=effects new-point=^point])
491+ ::
492+ ?: =(log-name changed-spawn-proxy:log-names)
493+ ?. ?=(%l1 -.point) ~
494+ ?> ?=([@ ~] t.t.topics.log)
495+ =* to i.t.t.topics.log
496+ :: Depositing to L2 is represented by a spawn proxy change on L1,
497+ :: but it doesn't change the actual spawn proxy.
498+ ::
499+ ?: =(deposit-address to)
500+ :+ ~ [%point ship %dominion %spawn]~
501+ point(dominion %spawn)
502+ :+ ~ [%point ship %spawn-proxy to]~
503+ point(address.spawn-proxy.own to)
504+ ::
505+ ?: =(log-name escape-accepted:log-names)
506+ ?> ?=([@ ~] t.t.topics.log)
507+ =* parent=@ i.t.t.topics.log
508+ =/ parent-point (get-point state parent)
509+ ?> ?=(^ parent-point)
510+ ?: ?=(%l2 -.u.parent-point) ~
511+ :+ ~ [%point ship %sponsor `parent]~
512+ point(escape.net ~, sponsor.net [%& parent])
513+ ::
514+ ?: =(log-name lost-sponsor:log-names)
515+ ?> ?=([@ ~] t.t.topics.log)
516+ =* parent=@ i.t.t.topics.log
517+ :: If the sponsor we lost was not our actual sponsor, we didn't
518+ :: actually lose anything.
519+ ::
520+ ?. =(parent who.sponsor.net.point) ~
521+ ::
522+ =/ parent-point (get-point state parent)
523+ ?> ?=(^ parent-point)
524+ ::
525+ :: We can detach even if the child is on L2, as long as the parent
526+ :: is on L1.
527+ ::
528+ ?: ?=(%l2 -.u.parent-point) ~
529+ :+ ~ [%point ship %sponsor ~]~
530+ point(has.sponsor.net %|)
531+ ::
532+ :: The rest can be done by any ship on L1, even if their spawn proxy
533+ :: is set to L2
534+ ::
535+ ?: ?=(%l2 -.point) ~
536+ ::
537+ ?: =(log-name escape-requested:log-names)
538+ ?> ?=([@ ~] t.t.topics.log)
539+ =* parent=@ i.t.t.topics.log
540+ =/ parent-point (get-point state parent)
541+ ?> ?=(^ parent-point)
542+ :+ ~ [%point ship %escape `parent]~
543+ point(escape.net `parent)
544+ ::
545+ ?: =(log-name escape-canceled:log-names)
546+ ?> ?=([@ ~] t.t.topics.log)
547+ =* parent=@ i.t.t.topics.log
548+ =/ parent-point (get-point state parent)
549+ ?> ?=(^ parent-point)
550+ :+ ~ [%point ship %escape ~]~
551+ point(escape.net ~)
552+ ::
553+ ?: =(log-name broke-continuity:log-names)
554+ ?> ?=(~ t.t.topics.log)
555+ =* rift=@ data.log
556+ :+ ~ [%point ship %rift rift]~
557+ point(rift.net rift)
558+ ::
559+ ?: =(log-name changed-keys:log-names)
560+ ?> ?=(~ t.t.topics.log)
561+ =/ =keys
562+ :* life=(cut 8 [0 1] data.log)
563+ suite=(cut 8 [1 1] data.log)
564+ auth=(cut 8 [2 1] data.log)
565+ crypt=(cut 8 [3 1] data.log)
566+ ==
567+ :+ ~ [%point ship %keys keys]~
568+ point(keys.net keys)
569+ ::
570+ ?: =(log-name owner-changed:log-names)
571+ ?> ?=([@ ~] t.t.topics.log)
572+ =* to i.t.t.topics.log
573+ :: Depositing to L2 is represented by an ownership change on L1,
574+ :: but it doesn't change who actually owns the ship.
575+ ::
576+ ?: =(deposit-address to)
577+ :+ ~ [%point ship %dominion %l2]~
578+ point(dominion %l2)
579+ :+ ~ [%point ship %owner to]~
580+ point(address.owner.own to)
581+ ::
582+ ?: =(log-name changed-transfer-proxy:log-names)
583+ ?> ?=([@ ~] t.t.topics.log)
584+ =* to i.t.t.topics.log
585+ :+ ~ [%point ship %transfer-proxy to]~
586+ point(address.transfer-proxy.own to)
587+ ::
588+ ?: =(log-name changed-management-proxy:log-names)
589+ ?> ?=([@ ~] t.t.topics.log)
590+ =* to i.t.t.topics.log
591+ :+ ~ [%point ship %management-proxy to]~
592+ point(address.management-proxy.own to)
593+ ::
594+ ?: =(log-name changed-voting-proxy:log-names)
595+ ?> ?=([@ ~] t.t.topics.log)
596+ =* to i.t.t.topics.log
597+ :+ ~ [%point ship %voting-proxy to]~
598+ point(address.voting-proxy.own to)
599+ ::
600+ (debug %unknown-log ~)
601+::
602+:: Receive batch of L2 transactions
603+::
604+++ receive-batch
605+ |= [=verifier chain-id=@ud =state batch=@]
606+ =/ chain-t (ud-to-ascii chain-id)
607+ =/ =roll (parse-roll batch)
608+ |- ^- [effects ^state]
609+ ?~ roll
610+ [~ state]
611+ :: Verify signature, else skip tx
612+ ::
613+ ?. (verify-sig-and-nonce verifier chain-t state i.roll)
614+ %+ debug %l2-sig-failed
615+ =^ effects state $(roll t.roll)
616+ :_ state
617+ [[%tx i.roll `%sig-or-nonce-failed] effects]
618+ :: Increment nonce, even if it later fails
619+ ::
620+ =^ effects-1 points.state (increment-nonce state from.tx.i.roll)
621+ :: Process tx
622+ ::
623+ =^ effects-2 state
624+ =/ tx-result=(unit [=effects =^state]) (receive-tx state tx.i.roll)
625+ ?~ tx-result
626+ %+ debug %l2-tx-failed
627+ [[%tx i.roll `%tx-failed]~ state]
628+ [[[%tx i.roll ~] effects.u.tx-result] state.u.tx-result]
629+ =^ effects-3 state $(roll t.roll)
630+ [:(welp effects-1 effects-2 effects-3) state]
631+::
632+++ increment-nonce
633+ |= [=state =ship =proxy]
634+ =/ point (get-point state ship)
635+ ?> ?=(^ point) :: we only parsed 4 bytes
636+ =* own own.u.point
637+ =^ nonce u.point
638+ ?- proxy
639+ %own
640+ :- nonce.owner.own
641+ u.point(nonce.owner.own +(nonce.owner.own))
642+ ::
643+ %spawn
644+ :- nonce.spawn-proxy.own
645+ u.point(nonce.spawn-proxy.own +(nonce.spawn-proxy.own))
646+ ::
647+ %manage
648+ :- nonce.management-proxy.own
649+ u.point(nonce.management-proxy.own +(nonce.management-proxy.own))
650+ ::
651+ %vote
652+ :- nonce.voting-proxy.own
653+ u.point(nonce.voting-proxy.own +(nonce.voting-proxy.own))
654+ ::
655+ %transfer
656+ :- nonce.transfer-proxy.own
657+ u.point(nonce.transfer-proxy.own +(nonce.transfer-proxy.own))
658+ ==
659+ ::
660+ :- [%nonce ship proxy nonce]~
661+ (put:orm points.state ship u.point)
662+::
663+:: Receive an individual L2 transaction
664+::
665+++ receive-tx
666+ |= [=state =tx]
667+ |^
668+ ^- (unit [effects ^state])
669+ ?- +<.tx
670+ %spawn (process-spawn +>.tx)
671+ %transfer-point (w-point process-transfer-point ship.from.tx +>.tx)
672+ %configure-keys (w-point process-configure-keys ship.from.tx +>.tx)
673+ %escape (w-point-esc process-escape ship.from.tx +>.tx)
674+ %cancel-escape (w-point-esc process-cancel-escape ship.from.tx +>.tx)
675+ %adopt (w-point-esc process-adopt ship.tx +>.tx)
676+ %reject (w-point-esc process-reject ship.tx +>.tx)
677+ %detach (w-point-esc process-detach ship.tx +>.tx)
678+ %set-spawn-proxy
679+ (w-point-spawn process-set-spawn-proxy ship.from.tx +>.tx)
680+ ::
681+ %set-transfer-proxy
682+ (w-point process-set-transfer-proxy ship.from.tx +>.tx)
683+ ::
684+ %set-management-proxy
685+ (w-point process-set-management-proxy ship.from.tx +>.tx)
686+ ==
687+ ::
688+ ++ w-point
689+ |* [fun=$-([ship point *] (unit [effects point])) =ship rest=*]
690+ ^- (unit [effects ^state])
691+ =/ point (get-point state ship)
692+ ?~ point (debug %strange-ship ~)
693+ ?. ?=(%l2 -.u.point) (debug %ship-not-on-l2 ~)
694+ :: Important to fully no-op on failure so we don't insert an entry
695+ :: into points.state
696+ ::
697+ =/ res=(unit [=effects new-point=^point]) (fun u.point rest)
698+ ?~ res
699+ ~
700+ `[effects.u.res state(points (put:orm points.state ship new-point.u.res))]
701+ ::
702+ ++ w-point-esc
703+ |* [fun=$-([ship point *] (unit [effects point])) =ship rest=*]
704+ ^- (unit [effects ^state])
705+ =/ point (get-point state ship)
706+ ?~ point (debug %strange-ship ~)
707+ =/ res=(unit [=effects new-point=^point]) (fun u.point rest)
708+ ?~ res
709+ ~
710+ `[effects.u.res state(points (put:orm points.state ship new-point.u.res))]
711+ ::
712+ ++ w-point-spawn
713+ |* [fun=$-([ship point *] (unit [effects point])) =ship rest=*]
714+ ^- (unit [effects ^state])
715+ =/ point (get-point state ship)
716+ ?~ point (debug %strange-ship ~)
717+ ?: ?=(%l1 -.u.point) (debug %ship-on-l2 ~)
718+ =/ res=(unit [=effects new-point=^point]) (fun u.point rest)
719+ ?~ res
720+ ~
721+ `[effects.u.res state(points (put:orm points.state ship new-point.u.res))]
722+ ::
723+ ++ process-transfer-point
724+ |= [=point to=address reset=?]
725+ =* ship ship.from.tx
726+ :: Assert from owner or transfer prxoy
727+ ::
728+ ?. |(=(%own proxy.from.tx) =(%transfer proxy.from.tx))
729+ (debug %bad-permission ~)
730+ :: Execute transfer
731+ ::
732+ =/ effects-1
733+ ~[[%point ship %owner to] [%point ship %transfer-proxy *address]]
734+ =: address.owner.own.point to
735+ address.transfer-proxy.own.point *address
736+ ==
737+ :: Execute reset if requested
738+ ::
739+ ?. reset
740+ `[effects-1 point]
741+ ::
742+ =^ effects-2 net.point
743+ ?: =([0 0 0] +.keys.net.point)
744+ `net.point
745+ =/ =keys [+(life.keys.net.point) 0 0 0]
746+ :- [%point ship %keys keys]~
747+ [rift.net.point keys sponsor.net.point escape.net.point]
748+ =^ effects-3 rift.net.point
749+ ?: =(0 life.keys.net.point)
750+ `rift.net.point
751+ :- [%point ship %rift +(rift.net.point)]~
752+ +(rift.net.point)
753+ =/ effects-4
754+ :~ [%point ship %spawn-proxy *address]
755+ [%point ship %management-proxy *address]
756+ [%point ship %voting-proxy *address]
757+ [%point ship %transfer-proxy *address]
758+ ==
759+ =: address.spawn-proxy.own.point *address
760+ address.management-proxy.own.point *address
761+ address.voting-proxy.own.point *address
762+ address.transfer-proxy.own.point *address
763+ ==
764+ `[:(welp effects-1 effects-2 effects-3 effects-4) point]
765+ ::
766+ ++ process-spawn
767+ |= [=ship to=address]
768+ ^- (unit [effects ^state])
769+ =/ parent=^ship (sein ship)
770+ :: Assert parent is on L2
771+ ::
772+ =/ parent-point (get-point state parent)
773+ ?~ parent-point ~
774+ ?. ?=(?(%l2 %spawn) -.u.parent-point) ~
775+ :: Assert from owner or spawn proxy
776+ ::
777+ ?. ?& =(parent ship.from.tx)
778+ |(=(%own proxy.from.tx) =(%spawn proxy.from.tx))
779+ ==
780+ (debug %bad-permission ~)
781+ :: Assert child not already spawned
782+ ::
783+ ?^ (get:orm points.state ship) (debug %spawn-exists ~)
784+ :: Assert one-level-down
785+ ::
786+ ?. =(+((ship-rank parent)) (ship-rank ship)) (debug %bad-rank ~)
787+ ::
788+ =/ [=effects new-point=point]
789+ =/ point=(unit point) (get-point state ship)
790+ ?> ?=(^ point) :: only parsed 4 bytes
791+ :: If spawning to self, just do it
792+ ::
793+ ?: ?| ?& =(%own proxy.from.tx)
794+ =(to address.owner.own.u.parent-point)
795+ ==
796+ ?& =(%spawn proxy.from.tx)
797+ =(to address.spawn-proxy.own.u.parent-point)
798+ ==
799+ ==
800+ :- ~[[%point ship %dominion %l2] [%point ship %owner to]]
801+ u.point(address.owner.own to)
802+ :: Else spawn to parent and set transfer proxy
803+ ::
804+ :- :~ [%point ship %dominion %l2]
805+ [%point ship %owner address.owner.own.u.parent-point]
806+ [%point ship %transfer-proxy to]
807+ ==
808+ %= u.point
809+ address.owner.own address.owner.own.u.parent-point
810+ address.transfer-proxy.own to
811+ ==
812+ `[effects state(points (put:orm points.state ship new-point))]
813+ ::
814+ ++ process-configure-keys
815+ |= [=point crypt=@ auth=@ suite=@ breach=?]
816+ =* ship ship.from.tx
817+ ::
818+ ?. |(=(%own proxy.from.tx) =(%manage proxy.from.tx))
819+ (debug %bad-permission ~)
820+ ::
821+ =^ rift-effects rift.net.point
822+ ?. breach
823+ `rift.net.point
824+ [[%point ship %rift +(rift.net.point)]~ +(rift.net.point)]
825+ ::
826+ =^ keys-effects keys.net.point
827+ ?: =(+.keys.net.point [suite auth crypt])
828+ `keys.net.point
829+ =/ =keys
830+ [+(life.keys.net.point) suite auth crypt]
831+ [[%point ship %keys keys]~ keys]
832+ ::
833+ `[(welp rift-effects keys-effects) point]
834+ ::
835+ ++ process-escape
836+ |= [=point parent=ship]
837+ =* ship ship.from.tx
838+ ?. |(=(%own proxy.from.tx) =(%manage proxy.from.tx))
839+ (debug %bad-permission ~)
840+ ::
841+ ?. =(+((ship-rank parent)) (ship-rank ship))
842+ (debug %bad-rank ~)
843+ ::
844+ :+ ~ [%point ship %escape `parent]~
845+ point(escape.net `parent)
846+ ::
847+ ++ process-cancel-escape
848+ |= [=point parent=ship]
849+ =* ship ship.from.tx
850+ ?. |(=(%own proxy.from.tx) =(%manage proxy.from.tx))
851+ (debug %bad-permission ~)
852+ ::
853+ :+ ~ [%point ship %escape ~]~
854+ point(escape.net ~)
855+ ::
856+ ++ process-adopt
857+ |= [=point =ship]
858+ =* parent ship.from.tx
859+ ?. |(=(%own proxy.from.tx) =(%manage proxy.from.tx))
860+ (debug %bad-permission ~)
861+ ::
862+ ?. =(escape.net.point `parent) (debug %no-adopt ~)
863+ :+ ~ [%point ship %sponsor `parent]~
864+ point(escape.net ~, sponsor.net [%& parent])
865+ ::
866+ ++ process-reject
867+ |= [=point =ship]
868+ =* parent ship.from.tx
869+ ?. |(=(%own proxy.from.tx) =(%manage proxy.from.tx))
870+ (debug %bad-permission ~)
871+ ::
872+ ?. =(escape.net.point `parent) (debug %no-reject ~)
873+ :+ ~ [%point ship %escape ~]~
874+ point(escape.net ~)
875+ ::
876+ ++ process-detach
877+ |= [=point =ship]
878+ =* parent ship.from.tx
879+ ?. |(=(%own proxy.from.tx) =(%manage proxy.from.tx))
880+ (debug %bad-permission ~)
881+ ::
882+ ?. =(who.sponsor.net.point parent) (debug %no-detach ~)
883+ :+ ~ [%point ship %sponsor ~]~
884+ point(has.sponsor.net %|)
885+ ::
886+ ++ process-set-management-proxy
887+ |= [=point =address]
888+ ?. |(=(%own proxy.from.tx) =(%manage proxy.from.tx))
889+ (debug %bad-permission ~)
890+ ::
891+ :+ ~ [%point ship.from.tx %management-proxy address]~
892+ point(address.management-proxy.own address)
893+ ::
894+ ++ process-set-spawn-proxy
895+ |= [=point =address]
896+ ?. |(=(%own proxy.from.tx) =(%spawn proxy.from.tx))
897+ (debug %bad-permission ~)
898+ ::
899+ ?: (gte (ship-rank ship.from.tx) 2)
900+ (debug %spawn-proxy-planet ~)
901+ ::
902+ :+ ~ [%point ship.from.tx %spawn-proxy address]~
903+ point(address.spawn-proxy.own address)
904+ ::
905+ ++ process-set-transfer-proxy
906+ |= [=point =address]
907+ ?. |(=(%own proxy.from.tx) =(%transfer proxy.from.tx))
908+ (debug %bad-permission ~)
909+ ::
910+ :+ ~ [%point ship.from.tx %transfer-proxy address]~
911+ point(address.transfer-proxy.own address)
912+ --
913+--
914+::
915+:: State transition function
916+::
917+|= [=verifier chain-id=@ud =state =input]
918+^- [effects ^state]
919+?: ?=(%log +<.input)
920+ :: Received log from L1 transaction
921+ ::
922+ (receive-log state event-log.input)
923+:: Received L2 batch
924+::
925+:: %+ debug %batch
926+(receive-batch verifier chain-id state batch.input)