shrub/pkg/arvo/lib/ph/azimuth.hoon

485 lines
12 KiB
Plaintext
Raw Normal View History

:: Mock Azimuth
::
2019-08-03 01:19:40 +03:00
/+ ph, ph-util, ph-philter, ph-tests
=, ph
=, ph-util
=, ph-philter
2019-08-03 01:19:40 +03:00
|= our=ship
=> |%
+$ az-log [topics=(lest @) data=@t]
--
=| logs=(list az-log) :: oldest logs first
=| lives=(map ship [lyfe=life rut=rift])
=| $= eth-filters
$: next=_1 :: jael assumes != 0
all=(map @ud [from-block=@ud last-block=@ud address=@ux])
==
|%
++ this-az .
++ add-logs
|= new-logs=(list az-log)
^+ this-az
=. logs (weld logs new-logs)
this-az
::
++ router
=/ n (philter ,_this-az)
2019-04-23 00:47:25 +03:00
^- form:n
|%
++ stay this-az
++ run
|= pin=ph-input
^- output:n
=, enjs:format
2019-07-25 01:54:45 +03:00
=/ ask (extract-request uf.pin 'http://localhost:8545/')
?~ ask
[& ~ %wait ~]
2019-07-25 01:54:45 +03:00
?~ body.request.u.ask
[& ~ %wait ~]
2019-07-25 01:54:45 +03:00
=/ req q.u.body.request.u.ask
|^ ^- output:n
=/ method (get-method req)
2019-07-25 01:54:45 +03:00
:: =; a ~& [%give-azimuth-response a] -
?: =(method 'eth_blockNumber')
:- | :_ [%wait ~]
%+ answer-request req
2019-04-23 01:37:10 +03:00
s+(crip (num-to-hex:ethereum latest-block))
2019-07-23 21:39:29 +03:00
?: =(method 'eth_getBlockByNumber')
:- | :_ [%wait ~]
%+ answer-request req
:- %o
2019-07-30 01:09:08 +03:00
=/ number (hex-to-num:ethereum (get-first-param req))
=/ hash (number-to-hash number)
2019-07-27 09:36:59 +03:00
~& who=who.pin
2019-07-25 01:54:45 +03:00
~& number=number
~& hash=hash
2019-07-23 21:39:29 +03:00
=/ parent-hash (number-to-hash ?~(number number (dec number)))
2019-07-25 01:54:45 +03:00
~& parent-hash=parent-hash
2019-07-23 21:39:29 +03:00
%- malt
^- (list (pair term json))
2019-07-25 01:54:45 +03:00
:~ hash+s+(crip (prefix-hex:ethereum (render-hex-bytes:ethereum 32 hash)))
2019-07-23 21:39:29 +03:00
number+s+(crip (num-to-hex:ethereum number))
'parentHash'^s+(crip (num-to-hex:ethereum parent-hash))
==
?: =(method 'eth_getLogs')
:- | :_ [%wait ~]
%+ answer-request req
2019-07-25 01:54:45 +03:00
?^ (get-param-obj-maybe req 'blockHash')
2019-07-23 21:39:29 +03:00
%- logs-by-hash
(get-param-obj req 'blockHash')
%+ logs-by-range
(get-param-obj req 'fromBlock')
(get-param-obj req 'toBlock')
?: =(method 'eth_newFilter')
2019-04-23 00:55:58 +03:00
:+ |
(answer-request req s+(scot %ux next.eth-filters))
=. all.eth-filters
%+ ~(put by all.eth-filters)
next.eth-filters
:+
(get-param-obj req 'fromBlock')
(get-param-obj req 'fromBlock')
(get-param-obj req 'address')
=. next.eth-filters +(next.eth-filters)
[%cont ..stay]
?: =(method 'eth_getFilterLogs')
=/ fil (~(get by all.eth-filters) (get-filter-id req))
?~ fil
~|(%no-filter-not-implemented !!)
:+ |
%+ answer-request req
~| [eth-filters latest-block]
2019-07-23 21:39:29 +03:00
(logs-by-range from-block.u.fil latest-block)
=. last-block.u.fil latest-block
[%cont ..stay]
?: =(method 'eth_getFilterChanges')
=/ fil-id (get-filter-id req)
=/ fil (~(get by all.eth-filters) fil-id)
?~ fil
~|(%no-filter-not-implemented !!)
2019-04-23 00:55:58 +03:00
:+ |
%+ answer-request req
2019-07-23 21:39:29 +03:00
(logs-by-range last-block.u.fil latest-block)
=. all.eth-filters
%+ ~(put by all.eth-filters)
fil-id
u.fil(last-block latest-block)
[%cont ..stay]
2019-07-27 06:43:07 +03:00
~& [%ph-azimuth-miss req]
[& ~ %wait ~]
::
2019-04-23 01:37:10 +03:00
++ latest-block
2019-07-27 09:36:59 +03:00
(add launch:contracts:azimuth (dec (lent logs)))
2019-04-23 01:37:10 +03:00
::
++ get-id
|= req=@t
=, dejs:format
%. (need (de-json:html req))
(ot id+so ~)
::
++ get-method
|= req=@t
=, dejs:format
%. (need (de-json:html req))
(ot method+so ~)
::
++ get-param-obj
|= [req=@t param=@t]
=, dejs:format
%- hex-to-num:ethereum
=/ array
%. (need (de-json:html req))
(ot params+(ar (ot param^so ~)) ~)
?> ?=([* ~] array)
i.array
::
2019-07-23 21:39:29 +03:00
++ get-param-obj-maybe
|= [req=@t param=@t]
^- (unit @ud)
=, dejs-soft:format
=/ array
%. (need (de-json:html req))
(ot params+(ar (ot param^so ~)) ~)
?~ array
~
:- ~
?> ?=([* ~] u.array)
%- hex-to-num:ethereum
i.u.array
::
++ get-filter-id
|= req=@t
=, dejs:format
%- hex-to-num:ethereum
=/ id
%. (need (de-json:html req))
(ot params+(ar so) ~)
?> ?=([* ~] id)
i.id
::
2019-07-30 01:09:08 +03:00
++ get-first-param
2019-07-23 21:39:29 +03:00
|= req=@t
=, dejs:format
=/ id
%. (need (de-json:html req))
2019-07-25 01:54:45 +03:00
(ot params+(at so bo ~) ~)
-.id
2019-07-23 21:39:29 +03:00
::
++ answer-request
|= [req=@t result=json]
^- (list ph-event)
=/ resp
%- crip
%- en-json:html
%- pairs
:~ id+s+(get-id req)
jsonrpc+s+'2.0'
result+result
==
:_ ~
:* %event
who.pin
2019-05-14 23:39:09 +03:00
//http-client/0v1n.2m9vh
%receive
2019-07-25 01:54:45 +03:00
num.u.ask
2019-05-14 23:39:09 +03:00
[%start [200 ~] `(as-octs:mimes:html resp) &]
==
::
2019-07-23 21:39:29 +03:00
++ number-to-hash
|= =number:block:able:kale
2019-07-25 01:54:45 +03:00
^- @
?: (lth number launch:contracts:azimuth)
(cat 3 0x5364 (sub launch:contracts:azimuth number))
(cat 3 0x5363 (sub number launch:contracts:azimuth))
2019-07-23 21:39:29 +03:00
::
++ hash-to-number
|= =hash:block:able:kale
2019-07-25 01:54:45 +03:00
(add launch:contracts:azimuth (div hash 0x1.0000))
2019-07-23 21:39:29 +03:00
::
++ logs-by-range
|= [from-block=@ud to-block=@ud]
2019-07-30 01:09:08 +03:00
%+ logs-to-json (max launch:contracts:azimuth from-block)
2019-07-25 01:54:45 +03:00
?: (lth to-block launch:contracts:azimuth)
~
2019-07-23 21:39:29 +03:00
%+ swag
2019-07-27 09:36:59 +03:00
~& [%logs-by-range from-block to-block launch:contracts:azimuth]
2019-07-25 01:54:45 +03:00
?: (lth from-block launch:contracts:azimuth)
2019-07-27 09:36:59 +03:00
[0 +((sub to-block launch:contracts:azimuth))]
2019-07-25 01:54:45 +03:00
:- (sub from-block launch:contracts:azimuth)
2019-07-27 09:36:59 +03:00
+((sub to-block from-block))
2019-07-23 21:39:29 +03:00
logs
::
++ logs-by-hash
|= =hash:block:able:kale
=/ =number:block:able:kale (hash-to-number hash)
(logs-by-range number +(number))
::
++ logs-to-json
|= [count=@ud selected-logs=(list az-log)]
^- json
:- %a
|- ^- (list json)
?~ selected-logs
~
:_ $(selected-logs t.selected-logs, count +(count))
%- pairs
:~ 'logIndex'^s+'0x0'
'transactionIndex'^s+'0x0'
:+ 'transactionHash' %s
(crip (prefix-hex:ethereum (render-hex-bytes:ethereum 32 `@`0x5362)))
::
:+ 'blockHash' %s
2019-07-23 21:39:29 +03:00
=/ hash (number-to-hash count)
(crip (prefix-hex:ethereum (render-hex-bytes:ethereum 32 hash)))
::
:+ 'blockNumber' %s
2019-07-30 01:09:08 +03:00
(crip (num-to-hex:ethereum count))
::
:+ 'address' %s
(crip (address-to-hex:ethereum azimuth:contracts:azimuth))
::
'type'^s+'mined'
::
'data'^s+data.i.selected-logs
:+ 'topics' %a
%+ turn topics.i.selected-logs
|= topic=@ux
^- json
:- %s
%- crip
%- prefix-hex:ethereum
(render-hex-bytes:ethereum 32 `@`topic)
==
--
--
::
2019-08-03 01:19:40 +03:00
++ raw-real-ship
|= who=ship
2019-08-03 01:19:40 +03:00
=/ m (ph ,~)
^- form:m
;< now=@da bind:m
|= pin=ph-input
[& ~ %done now.pin]
(raw-ship:(ph-tests our) who `(dawn who now))
::
++ dawn
|= [who=ship now=@da]
^- dawn-event
2019-07-31 02:26:47 +03:00
=/ spon
=/ =ship (^sein:title who)
=/ spon-spon
?: ?=(%czar (clan:title ship))
[| ~zod]
[& (^sein:title ship)]
2019-08-03 01:19:40 +03:00
=/ life-rift ~|([ship lives] (~(got by lives) ship))
2019-07-31 02:26:47 +03:00
=/ =life lyfe.life-rift
=/ =rift rut.life-rift
=/ =pass
%^ pass-from-eth:azimuth
(as-octs:mimes:html (get-public ship life %crypt))
(as-octs:mimes:html (get-public ship life %auth))
1
2019-08-03 01:19:40 +03:00
:^ ship=ship
*[address address address address]:azimuth
2019-08-03 01:19:40 +03:00
`[life=life pass rift spon-spon ~]
~
2019-08-03 01:19:40 +03:00
=/ =seed:able:kale
?: =(%earl (clan:title who))
=/ pax
;: weld
/i/(scot %p ship.spon)/k/(scot %p ship.spon)/earl/(scot %da now)
/(scot %p who)/(scot %ud life.spon)
/noun
==
(need (scry-aqua (unit seed:able:kale) our now pax))
=/ life-rift (~(got by lives) who)
=/ =life lyfe.life-rift
[who life sec:ex:(get-keys who life) ~]
:* seed
2019-07-31 02:26:47 +03:00
spon
get-czars
~[~['arvo' 'netw' 'ork']]
0
`(need (de-purl:html 'http://localhost:8545'))
~
==
::
:: Should only do galaxies
::
++ get-czars
2019-07-30 01:09:08 +03:00
^- (map ship [rift life pass])
%- malt
%+ murn
~(tap by lives)
|= [who=ship lyfe=life rut=rift]
?. =(%czar (clan:title who))
~
%- some
2019-07-30 01:09:08 +03:00
:^ who rut lyfe
%^ pass-from-eth:azimuth
(as-octs:mimes:html (get-public who lyfe %crypt))
(as-octs:mimes:html (get-public who lyfe %auth))
1
::
2019-04-24 23:13:52 +03:00
++ spawn
|= who=@p
?< (~(has by lives) who)
=. lives (~(put by lives) who [1 0])
2019-08-06 09:11:40 +03:00
=. this-az
%- add-logs
%+ welp
?: =(%czar (clan:title who))
~
~[(spawned:lo (^sein:title who) who)]
:~ (activated:lo who)
(owner-changed:lo who 0xdead.beef)
%- changed-keys:lo
:* who
(get-public who 1 %crypt)
(get-public who 1 %auth)
1
1
==
==
(spam-logs 30)
::
:: our: host ship
:: who: cycle keys
:: her: wait until hears about cycle
::
++ cycle-keys-and-hear
|= [our=@p who=@p her=@p]
=. this-az (cycle-keys who)
=/ new-lyfe lyfe:(~(got by lives) who)
=/ m (ph ,_this-az)
;< [this-az=_this-az ~] bind:m
%+ (wrap-philter ,_this-az ,~)
router:this-az
^+ *form:(ph ,~)
|= pin=ph-input
:+ & ~
2019-04-30 20:40:38 +03:00
=/ aqua-pax
:- %i
2019-08-03 01:19:40 +03:00
/(scot %p her)/k/(scot %p her)/life/(scot %da now.pin)/(scot %p who)/noun
=/ lyfe (scry-aqua noun our now.pin aqua-pax)
~& [new-lyfe=[0 new-lyfe] lyfe=lyfe]
?: =([~ new-lyfe] lyfe)
[%done ~]
[%wait ~]
(pure:m this-az)
::
++ cycle-keys
|= who=@p
=/ prev (~(got by lives) who)
=/ lyfe +(lyfe.prev)
=. lives (~(put by lives) who [lyfe rut.prev])
%- add-logs
:_ ~
%- changed-keys:lo
:* who
(get-public who lyfe %crypt)
(get-public who lyfe %auth)
1
lyfe
==
::
:: our: host ship
:: who: breachee
:: her: wait until hears about breach
::
++ breach-and-hear
|= [our=@p who=@p her=@p]
=. this-az (breach who)
=/ new-rut rut:(~(got by lives) who)
=/ m (ph ,_this-az)
;< [this-az=_this-az ~] bind:m
%+ (wrap-philter ,_this-az ,~)
router:this-az
^+ *form:(ph ,~)
|= pin=ph-input
:+ & ~
2019-04-30 20:40:38 +03:00
=/ aqua-pax
:- %i
2019-07-27 06:43:07 +03:00
/(scot %p her)/k/(scot %p her)/rift/(scot %da now.pin)/(scot %p who)/noun
=/ rut (scry-aqua noun our now.pin aqua-pax)
?: =([~ new-rut] rut)
[%done ~]
[%wait ~]
(pure:m this-az)
::
++ breach
|= who=@p
=. this-az (cycle-keys who)
=/ prev (~(got by lives) who)
=/ rut +(rut.prev)
=. lives (~(put by lives) who [lyfe.prev rut])
2019-07-31 02:53:40 +03:00
=. this-az
%- add-logs
:_ ~
(broke-continuity:lo who rut)
2019-08-06 03:44:52 +03:00
(spam-logs 30)
::
++ spam-logs
|= n=@
?: =(n 0)
this-az
=. this-az ?:((~(has by lives) ~fes) (cycle-keys ~fes) (spawn ~fes))
$(n (dec n))
::
++ get-keys
|= [who=@p lyfe=life]
^- acru:ames
%+ pit:nu:crub:crypto 32
(can 5 [1 (scot %p who)] [1 (scot %ud lyfe)] ~)
::
++ get-public
|= [who=@p lyfe=life typ=?(%auth %crypt)]
=/ bod (rsh 3 1 pub:ex:(get-keys who lyfe))
=+ [enc=(rsh 8 1 bod) aut=(end 8 1 bod)]
?: =(%auth typ)
aut
enc
::
:: Generate logs
::
++ lo
=, azimuth-events:azimuth
|%
++ activated
|= who=ship
^- az-log
[~[^activated who] '']
::
2019-04-24 23:13:52 +03:00
++ broke-continuity
|= [who=ship rut=rift]
^- az-log
2019-04-24 23:13:52 +03:00
:- ~[^broke-continuity who]
%- crip
%- prefix-hex:ethereum
(render-hex-bytes:ethereum 32 `@`rut)
::
++ changed-keys
|= [who=ship enc=@ux aut=@ux crypto=@ud lyfe=life]
^- az-log
:- ~[^changed-keys who]
%- crip
%- prefix-hex:ethereum
;: welp
(render-hex-bytes:ethereum 32 `@`enc)
(render-hex-bytes:ethereum 32 `@`aut)
(render-hex-bytes:ethereum 32 `@`crypto)
(render-hex-bytes:ethereum 32 `@`lyfe)
==
::
2019-04-24 23:13:52 +03:00
++ owner-changed
|= [who=ship owner=@ux]
^- az-log
2019-04-24 23:13:52 +03:00
[~[^owner-changed who owner] '']
::
++ spawned
|= [par=ship who=ship]
^- az-log
[~[^spawned par who] '']
--
--