shrub/pkg/arvo/app/azimuth-tracker.hoon

420 lines
13 KiB
Plaintext
Raw Normal View History

2019-07-05 04:15:53 +03:00
/+ tapp, stdio
=, able:kale
=> |%
+$ pending-udiffs (map number:block udiffs:point)
2019-07-19 01:26:15 +03:00
+$ app-state
2019-07-23 21:39:29 +03:00
$: %2
url=@ta
2019-07-19 03:08:01 +03:00
=number:block
2019-07-19 01:26:15 +03:00
=pending-udiffs
2019-07-19 03:08:01 +03:00
blocks=(list block)
whos=(set ship)
2019-07-19 01:26:15 +03:00
==
2019-07-05 04:15:53 +03:00
+$ peek-data ~
+$ in-poke-data
2019-07-19 01:26:15 +03:00
$: %azimuth-tracker-poke
$% [%listen whos=(list ship) =source:kale]
[%watch url=@ta]
2019-07-19 01:26:15 +03:00
==
2019-07-05 04:15:53 +03:00
==
+$ out-poke-data ~
+$ in-peer-data ~
+$ out-peer-data
[%azimuth-udiff =ship =udiff:point]
2019-07-05 04:15:53 +03:00
++ tapp
%: ^tapp
app-state
peek-data
in-poke-data
out-poke-data
in-peer-data
out-peer-data
==
++ tapp-async tapp-async:tapp
++ stdio (^stdio out-poke-data out-peer-data)
--
::
:: Async helpers
::
=> |%
++ topics
|= ships=(set ship)
:- => azimuth-events:azimuth
:~ broke-continuity
changed-keys
lost-sponsor
escape-accepted
==
?: =(~ ships)
~
[(turn ~(tap in ships) ,@) ~]
2019-07-05 04:15:53 +03:00
::
++ request-rpc
|= [url=@ta id=(unit @t) req=request:rpc:ethereum]
=/ m (async:stdio ,json)
^- form:m
%+ (retry json) `10
=/ m (async:stdio ,(unit json))
^- form:m
|^
=/ =request:http
:* method=%'POST'
url=url
header-list=['Content-Type'^'application/json' ~]
^= body
%- some %- as-octt:mimes:html
%- en-json:html
(request-to-json:rpc:ethereum id req)
==
;< ~ bind:m (send-request:stdio request)
2019-07-05 23:59:29 +03:00
;< rep=(unit client-response:iris) bind:m
2019-07-05 04:15:53 +03:00
take-maybe-response:stdio
?~ rep
(pure:m ~)
(parse-response u.rep)
::
++ parse-response
2019-07-05 23:59:29 +03:00
|= =client-response:iris
2019-07-05 04:15:53 +03:00
=/ m (async:stdio ,(unit json))
^- form:m
?> ?=(%finished -.client-response)
2019-08-02 20:16:05 +03:00
?~ full-file.client-response
(pure:m ~)
=/ body=@t q.data.u.full-file.client-response
2019-07-05 04:15:53 +03:00
=/ jon=(unit json) (de-json:html body)
?~ jon
(pure:m ~)
=, dejs-soft:format
=/ array=(unit (list response:rpc:jstd))
((ar parse-one-response) u.jon)
?~ array
=/ res=(unit response:rpc:jstd) (parse-one-response u.jon)
?~ res
(async-fail:stdio %request-rpc-parse-error >id< ~)
?: ?=(%error -.u.res)
(async-fail:stdio %request-rpc-error >id< >+.res< ~)
?. ?=(%result -.u.res)
(async-fail:stdio %request-rpc-fail >u.res< ~)
(pure:m `res.u.res)
(async-fail:stdio %request-rpc-batch >%not-implemented< ~)
:: (pure:m `[%batch u.array])
::
++ parse-one-response
|= =json
^- (unit response:rpc:jstd)
=/ res=(unit [@t ^json])
%. json
=, dejs-soft:format
(ot id+so result+some ~)
?^ res `[%result u.res]
~| parse-one-response=json
:+ ~ %error %- need
%. json
=, dejs-soft:format
(ot id+so error+(ot code+no message+so ~) ~)
--
::
++ retry
|* result=mold
|= [crash-after=(unit @ud) computation=_*form:(async:stdio (unit result))]
=/ m (async:stdio ,result)
=| try=@ud
|^
|- ^- form:m
=* loop $
?: =(crash-after `try)
(async-fail:stdio %retry-too-many ~)
;< ~ bind:m (backoff try ~m1)
;< res=(unit result) bind:m computation
?^ res
(pure:m u.res)
loop(try +(try))
::
++ backoff
|= [try=@ud limit=@dr]
=/ m (async:stdio ,~)
^- form:m
;< eny=@uvJ bind:m get-entropy:stdio
;< now=@da bind:m get-time:stdio
%- wait:stdio
%+ add now
%+ min limit
?: =(0 try) ~s0
%+ add
(mul ~s1 (bex (dec try)))
(mul ~s0..0001 (~(rad og eny) 1.000))
--
::
++ get-latest-block
|= url=@ta
=/ m (async:stdio ,block)
^- form:m
;< =json bind:m (request-rpc url `'block number' %eth-block-number ~)
(get-block-by-number url (parse-eth-block-number:rpc:ethereum json))
::
++ get-block-by-number
|= [url=@ta =number:block]
=/ m (async:stdio ,block)
^- form:m
|^
;< =json bind:m
(request-rpc url `'block by number' %eth-get-block-by-number number |)
=/ =block (parse-block json)
?. =(number number.id.block)
(async-fail:stdio %reorg-detected >number< >block< ~)
(pure:m block)
::
++ parse-block
|= =json
^- block
=< [[&1 &2] |2]
^- [@ @ @]
~| json
%. json
=, dejs:format
%- ot
:~ hash+parse-hex-result:rpc:ethereum
number+parse-hex-result:rpc:ethereum
'parentHash'^parse-hex-result:rpc:ethereum
==
--
::
++ get-logs-by-hash
|= [url=@ta whos=(set ship) =hash:block]
2019-07-05 04:15:53 +03:00
=/ m (async:stdio udiffs:point)
^- form:m
;< =json bind:m
%+ request-rpc url
:* `'logs by hash'
%eth-get-logs-by-hash
hash
~[azimuth:contracts:azimuth]
(topics whos)
2019-07-05 04:15:53 +03:00
==
=/ event-logs=(list event-log:rpc:ethereum)
(parse-event-logs:rpc:ethereum json)
2019-07-19 23:13:14 +03:00
=/ =udiffs:point (event-logs-to-udiffs event-logs)
(pure:m udiffs)
::
++ get-logs-by-range
|= [url=@ta whos=(set ship) =from=number:block =to=number:block]
2019-07-19 23:13:14 +03:00
=/ m (async:stdio udiffs:point)
^- form:m
;< =json bind:m
%+ request-rpc url
:* `'logs by range'
%eth-get-logs
`number+from-number
`number+to-number
~[azimuth:contracts:azimuth]
(topics whos)
2019-07-19 23:13:14 +03:00
==
=/ event-logs=(list event-log:rpc:ethereum)
(parse-event-logs:rpc:ethereum json)
=/ =udiffs:point (event-logs-to-udiffs event-logs)
2019-07-05 04:15:53 +03:00
(pure:m udiffs)
::
2019-07-19 23:13:14 +03:00
++ event-logs-to-udiffs
|= event-logs=(list =event-log:rpc:ethereum)
^- =udiffs:point
%+ murn event-logs
|= =event-log:rpc:ethereum
^- (unit [=ship =udiff:point])
?~ mined.event-log
~
?: removed.u.mined.event-log
~& [%removed-log event-log]
~
=/ =id:block [block-hash block-number]:u.mined.event-log
=, azimuth-events:azimuth
=, abi:ethereum
?: =(broke-continuity i.topics.event-log)
=/ who=@ (decode-topics t.topics.event-log ~[%uint])
=/ num=@ (decode-results data.event-log ~[%uint])
`[who id %rift num]
?: =(changed-keys i.topics.event-log)
=/ who=@ (decode-topics t.topics.event-log ~[%uint])
=+ ^- [enc=octs aut=octs sut=@ud rev=@ud]
%+ decode-results data.event-log
~[[%bytes-n 32] [%bytes-n 32] %uint %uint]
`[who id %keys rev sut (pass-from-eth:azimuth enc aut sut)]
?: =(lost-sponsor i.topics.event-log)
=+ ^- [who=@ pos=@]
(decode-topics t.topics.event-log ~[%uint %uint])
`[who id %spon ~]
?: =(escape-accepted i.topics.event-log)
=+ ^- [who=@ wer=@]
(decode-topics t.topics.event-log ~[%uint %uint])
`[who id %spon `wer]
~& [%bad-topic event-log]
~
::
2019-07-05 04:15:53 +03:00
++ jael-update
|= =udiffs:point
=/ m (async:stdio ,~)
|- ^- form:m
=* loop $
?~ udiffs
(pure:m ~)
=/ =path /(scot %p ship.i.udiffs)
;< ~ bind:m (give-result:stdio / %azimuth-udiff i.udiffs)
;< ~ bind:m (give-result:stdio path %azimuth-udiff i.udiffs)
2019-07-05 04:15:53 +03:00
loop(udiffs t.udiffs)
--
::
:: Main loop
::
=> |%
2019-07-19 01:26:15 +03:00
::
2019-07-19 03:08:01 +03:00
:: Send %listen to kale
2019-07-19 01:26:15 +03:00
::
2019-07-19 03:08:01 +03:00
++ listen
|= [state=app-state whos=(list ship) =source:kale]
2019-07-19 01:26:15 +03:00
=/ m (async:stdio ,app-state)
^- form:m
;< ~ bind:m (send-effect:stdio %listen /lo (silt whos) source)
2019-07-19 01:26:15 +03:00
(pure:m state)
::
:: Start watching a node
::
++ start
|= state=app-state
2019-07-19 01:26:15 +03:00
=/ m (async:stdio ,app-state)
2019-07-05 04:15:53 +03:00
^- form:m
=: number.state 0
2019-07-19 01:26:15 +03:00
pending-udiffs.state *pending-udiffs
2019-07-19 03:08:01 +03:00
blocks.state *(list block)
2019-07-19 01:26:15 +03:00
==
(get-updates state)
::
:: Get updates since last checked
::
++ get-updates
2019-07-19 23:13:14 +03:00
|= state=app-state
2019-07-19 01:26:15 +03:00
=/ m (async:stdio ,app-state)
^- form:m
2019-07-27 09:36:59 +03:00
;< =latest=block bind:m (get-latest-block url.state)
;< state=app-state bind:m (zoom state number.id.latest-block)
2019-07-05 04:15:53 +03:00
|- ^- form:m
=* walk-loop $
2019-07-19 23:13:14 +03:00
?: (gth number.state number.id.latest-block)
2019-07-05 04:15:53 +03:00
;< now=@da bind:m get-time:stdio
;< ~ bind:m (wait-effect:stdio (add now ~m5))
2019-07-19 23:13:14 +03:00
(pure:m state)
;< =block bind:m (get-block-by-number url.state number.state)
;< [=new=pending-udiffs new-blocks=(lest ^block)] bind:m
%- take-block
[url.state whos.state pending-udiffs.state block blocks.state]
2019-07-19 23:13:14 +03:00
=: pending-udiffs.state new-pending-udiffs
blocks.state new-blocks
number.state +(number.id.i.new-blocks)
2019-07-05 04:15:53 +03:00
==
walk-loop
::
2019-07-19 01:26:15 +03:00
:: Process a block, detecting and handling reorgs
::
2019-07-05 04:15:53 +03:00
++ take-block
|= [url=@ta whos=(set ship) =a=pending-udiffs =block blocks=(list block)]
2019-07-05 04:15:53 +03:00
=/ m (async:stdio ,[pending-udiffs (lest ^block)])
^- form:m
?: &(?=(^ blocks) !=(parent-hash.block hash.id.i.blocks))
2019-07-05 06:53:24 +03:00
(rewind url a-pending-udiffs block blocks)
2019-07-05 04:15:53 +03:00
;< =b=pending-udiffs bind:m
(release-old-events a-pending-udiffs number.id.block)
;< =new=udiffs:point bind:m (get-logs-by-hash url whos hash.id.block)
2019-07-05 04:15:53 +03:00
=. b-pending-udiffs (~(put by b-pending-udiffs) number.id.block new-udiffs)
(pure:m b-pending-udiffs block blocks)
::
2019-07-19 01:26:15 +03:00
:: Release events if they're more than 30 blocks ago
::
2019-07-05 04:15:53 +03:00
++ release-old-events
|= [=pending-udiffs =number:block]
=/ m (async:stdio ,^pending-udiffs)
^- form:m
2019-08-06 09:11:40 +03:00
=/ rel-number (sub number 30)
2019-07-05 04:15:53 +03:00
=/ =udiffs:point (~(get ja pending-udiffs) rel-number)
;< ~ bind:m (jael-update udiffs)
(pure:m (~(del by pending-udiffs) rel-number))
::
2019-07-19 01:26:15 +03:00
:: Reorg detected, so rewind until we're back in sync
::
2019-07-05 04:15:53 +03:00
++ rewind
2019-07-05 06:53:24 +03:00
|= [url=@ta =pending-udiffs =block blocks=(list block)]
=/ m (async:stdio ,[^pending-udiffs (lest ^block)])
2019-07-05 04:15:53 +03:00
|- ^- form:m
=* loop $
2019-07-05 06:53:24 +03:00
?~ blocks
(pure:m pending-udiffs block blocks)
?: =(parent-hash.block hash.id.i.blocks)
(pure:m pending-udiffs block blocks)
;< =next=^block bind:m (get-block-by-number url number.id.i.blocks)
2019-07-05 04:15:53 +03:00
?: =(~ pending-udiffs)
2019-07-05 06:53:24 +03:00
;< ~ bind:m (disavow block)
loop(block next-block, blocks t.blocks)
=. pending-udiffs (~(del by pending-udiffs) number.id.block)
loop(block next-block, blocks t.blocks)
2019-07-05 04:15:53 +03:00
::
2019-07-19 01:26:15 +03:00
:: Tell subscribers there was a deep reorg
::
2019-07-05 04:15:53 +03:00
++ disavow
|= =block
=/ m (async:stdio ,~)
^- form:m
(jael-update [*ship id.block %disavow ~]~)
2019-07-19 23:13:14 +03:00
::
:: Zoom forward to near a given block number.
::
:: Zooming doesn't go forward one block at a time. As a
:: consequence, it cannot detect and handle reorgs. Only use it
:: at a safe distance -- 500 blocks ago is probably sufficient.
::
++ zoom
|= [state=app-state =latest=number:block]
2019-07-27 09:36:59 +03:00
=/ m (async:stdio ,app-state)
2019-07-19 23:13:14 +03:00
^- form:m
2019-08-05 21:40:13 +03:00
=/ zoom-margin=number:block 100
?: (lth latest-number (add number.state zoom-margin))
2019-07-27 09:36:59 +03:00
(pure:m state)
=/ to-number=number:block (sub latest-number zoom-margin)
2019-07-19 23:13:14 +03:00
;< =udiffs:point bind:m
(get-logs-by-range url.state whos.state number.state to-number)
2019-07-19 23:13:14 +03:00
;< ~ bind:m (jael-update udiffs)
2019-07-27 09:36:59 +03:00
=. number.state +(to-number)
=. blocks.state ~
(pure:m state)
2019-07-05 04:15:53 +03:00
--
::
:: Main
::
=* default-tapp default-tapp:tapp
%- create-tapp-poke-peer-take:tapp
2019-07-19 03:08:01 +03:00
|_ [=bowl:gall state=app-state]
2019-07-05 04:15:53 +03:00
++ handle-poke
|= =in-poke-data
=/ m tapp-async
^- form:m
2019-07-19 01:26:15 +03:00
?- +<.in-poke-data
%listen (listen state +>.in-poke-data)
%watch (pure:m state(url url.in-poke-data))
2019-07-05 04:15:53 +03:00
==
::
++ handle-take
|= =sign:tapp
2019-07-19 01:26:15 +03:00
=/ m tapp-async
^- form:m
2019-07-19 03:08:01 +03:00
?+ -.sign ~|([%strange-sign -.sign] !!)
%wake (get-updates state)
2019-07-19 01:26:15 +03:00
==
2019-07-05 04:15:53 +03:00
::
++ handle-peer
|= =path
=/ m tapp-async
^- form:m
=/ who=(unit ship) ?~(path ~ `(slav %p i.path))
=. whos.state
?~ who
~
(~(put in whos.state) u.who)
(start state)
2019-07-05 04:15:53 +03:00
--