eth: move existing chain requests into ethio

To avoid reimplementing these (on top of ethio) every time. This seems
like a good place to centralize chain request logic going forward.
This commit is contained in:
Fang 2019-11-04 18:59:30 +01:00
parent dc35958918
commit 5c6c75c7ea
No known key found for this signature in database
GPG Key ID: EB035760C1BBA972
2 changed files with 83 additions and 80 deletions

View File

@ -57,79 +57,6 @@
:: Async helpers
::
=> |%
++ get-latest-block
|= url=@ta
=/ m (async:stdio ,block)
^- form:m
;< =json bind:m
(request-rpc:ethio 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:ethio 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 =hash:block contracts=(list address) =topics]
=/ m (async:stdio loglist)
^- form:m
;< =json bind:m
%+ request-rpc:ethio url
:* `'logs by hash'
%eth-get-logs-by-hash
hash
contracts
topics
==
%- pure:m
(parse-event-logs:rpc:ethereum json)
::
++ get-logs-by-range
|= $: url=@ta
contracts=(list address)
=topics
=from=number:block
=to=number:block
==
=/ m (async:stdio loglist)
^- form:m
;< =json bind:m
%+ request-rpc:ethio url
:* `'logs by range'
%eth-get-logs
`number+from-number
`number+to-number
contracts
topics
==
%- pure:m
(parse-event-logs:rpc:ethereum json)
::
++ send-logs
|= [=path =loglist]
=/ m (async:stdio ,~)
@ -170,13 +97,13 @@
|= context
=/ m (async:stdio ,watchdog)
^- form:m
;< =latest=block bind:m (get-latest-block url.dog)
;< =latest=block bind:m (get-latest-block:ethio url.dog)
;< dog=watchdog bind:m (zoom [path dog] number.id.latest-block)
|- ^- form:m
=* loop $
?: (gth number.dog number.id.latest-block)
(pure:m dog)
;< =block bind:m (get-block-by-number url.dog number.dog)
;< =block bind:m (get-block-by-number:ethio url.dog number.dog)
;< dog=watchdog bind:m
(take-block [path dog] block)
loop(dog dog)
@ -193,7 +120,7 @@
;< [=new=pending-logs =released=loglist] bind:m
(release-old-events path pending-logs.dog number.id.block)
;< =new=loglist bind:m :: oldest first
(get-logs-by-hash url.dog hash.id.block contracts.dog topics.dog)
(get-logs-by-hash:ethio url.dog hash.id.block contracts.dog topics.dog)
=. new-pending-logs
(~(put by new-pending-logs) number.id.block new-loglist)
%- pure:m
@ -233,7 +160,7 @@
(pure:m dog(blocks [block blocks]))
:: next-block: the new target block
;< =next=^block bind:m
(get-block-by-number url.dog number.id.i.blocks)
(get-block-by-number:ethio url.dog number.id.i.blocks)
:: remove from either pending-logs or history
?: =(~ pending-logs.dog)
:: if no more pending logs, start deleting from history instead
@ -275,7 +202,7 @@
(pure:m dog)
=/ to-number=number:block (sub latest-number zoom-margin)
;< =loglist bind:m :: oldest first
%: get-logs-by-range
%: get-logs-by-range:ethio
url.dog
contracts.dog
topics.dog

View File

@ -1,10 +1,13 @@
:: ethio: Asynchronous Ethereum input/output functions.
::.
/+ stdio
=, ethereum-types
=, able:jael
::
|* [out-poke-data=mold out-peer-data=mold]
=> |%
++ stdio (^stdio out-poke-data out-peer-data)
++ stdio (^stdio out-poke-data out-peer-data)
+$ topics (list ?(@ux (list @ux)))
--
|%
:: +request-rpc: send rpc request, with retry
@ -102,4 +105,77 @@
=, dejs-soft:format
(ot id+so error+(ot code+no message+so ~) ~)
--
--
::
++ 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 =hash:block contracts=(list address) =topics]
=/ m (async:stdio (list event-log:rpc:ethereum))
^- form:m
;< =json bind:m
%+ request-rpc url
:* `'logs by hash'
%eth-get-logs-by-hash
hash
contracts
topics
==
%- pure:m
(parse-event-logs:rpc:ethereum json)
::
++ get-logs-by-range
|= $: url=@ta
contracts=(list address)
=topics
=from=number:block
=to=number:block
==
=/ m (async:stdio (list event-log:rpc:ethereum))
^- form:m
;< =json bind:m
%+ request-rpc url
:* `'logs by range'
%eth-get-logs
`number+from-number
`number+to-number
contracts
topics
==
%- pure:m
(parse-event-logs:rpc:ethereum json)
--