2019-10-29 22:51:58 +03:00
|
|
|
:: eth-watcher: ethereum event log collector
|
|
|
|
::
|
|
|
|
/- *eth-watcher
|
2019-10-31 17:54:56 +03:00
|
|
|
/+ tapp, stdio, ethio
|
2019-10-25 19:39:02 +03:00
|
|
|
=, ethereum-types
|
2019-08-07 01:42:37 +03:00
|
|
|
=, able:jael
|
2019-11-04 20:57:17 +03:00
|
|
|
::
|
|
|
|
=> |%
|
|
|
|
++ refresh-rate ~m5
|
|
|
|
--
|
|
|
|
::
|
2019-07-05 04:15:53 +03:00
|
|
|
=> |%
|
2019-07-19 01:26:15 +03:00
|
|
|
+$ app-state
|
2019-10-25 19:39:02 +03:00
|
|
|
$: %0
|
|
|
|
dogs=(map path watchdog)
|
|
|
|
==
|
|
|
|
::
|
2019-10-29 22:51:58 +03:00
|
|
|
+$ context [=path dog=watchdog]
|
2019-10-25 19:39:02 +03:00
|
|
|
+$ watchdog
|
|
|
|
$: config
|
2019-07-19 03:08:01 +03:00
|
|
|
=number:block
|
2019-10-25 19:39:02 +03:00
|
|
|
=pending-logs
|
2019-10-30 02:37:12 +03:00
|
|
|
=history
|
2019-07-19 03:08:01 +03:00
|
|
|
blocks=(list block)
|
2019-07-19 01:26:15 +03:00
|
|
|
==
|
2019-10-25 19:39:02 +03:00
|
|
|
::
|
2019-10-30 20:44:34 +03:00
|
|
|
:: history: newest block first, oldest event first
|
2019-10-30 02:37:12 +03:00
|
|
|
+$ history (list loglist)
|
2019-10-25 19:39:02 +03:00
|
|
|
+$ pending-logs (map number:block loglist)
|
|
|
|
::
|
2019-10-29 21:11:36 +03:00
|
|
|
+$ peek-data
|
|
|
|
[%atom =next-block=number:block]
|
2019-07-05 04:15:53 +03:00
|
|
|
+$ in-poke-data
|
2019-10-25 19:39:02 +03:00
|
|
|
$: %eth-watcher-poke
|
2019-10-29 22:51:58 +03:00
|
|
|
poke
|
|
|
|
==
|
2019-07-05 04:15:53 +03:00
|
|
|
+$ out-poke-data ~
|
|
|
|
+$ in-peer-data ~
|
2019-07-28 07:01:55 +03:00
|
|
|
+$ out-peer-data
|
2019-10-29 21:14:21 +03:00
|
|
|
$: %eth-watcher-diff
|
2019-10-29 22:51:58 +03:00
|
|
|
diff
|
|
|
|
==
|
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)
|
2019-10-31 17:54:56 +03:00
|
|
|
++ ethio (^ethio out-poke-data out-peer-data)
|
2019-07-05 04:15:53 +03:00
|
|
|
--
|
|
|
|
::
|
|
|
|
:: Async helpers
|
|
|
|
::
|
|
|
|
=> |%
|
2019-10-25 19:39:02 +03:00
|
|
|
++ send-logs
|
|
|
|
|= [=path =loglist]
|
2019-07-05 04:15:53 +03:00
|
|
|
=/ m (async:stdio ,~)
|
|
|
|
|- ^- form:m
|
|
|
|
=* loop $
|
2019-10-25 19:39:02 +03:00
|
|
|
?~ loglist
|
2019-07-05 04:15:53 +03:00
|
|
|
(pure:m ~)
|
2019-10-25 19:39:02 +03:00
|
|
|
;< ~ bind:m (send-update path %log i.loglist)
|
|
|
|
loop(loglist t.loglist)
|
|
|
|
::
|
|
|
|
++ send-update
|
2019-10-29 22:51:58 +03:00
|
|
|
|= [=path =diff]
|
2019-10-25 19:39:02 +03:00
|
|
|
=/ m (async:stdio ,~)
|
|
|
|
^- form:m
|
|
|
|
=. path [%logs path]
|
2019-10-29 22:51:58 +03:00
|
|
|
(give-result:stdio path %eth-watcher-diff diff)
|
2019-07-05 04:15:53 +03:00
|
|
|
--
|
|
|
|
::
|
|
|
|
:: Main loop
|
|
|
|
::
|
|
|
|
=> |%
|
2019-07-19 01:26:15 +03:00
|
|
|
::
|
2019-10-25 19:39:02 +03:00
|
|
|
:: Update watchdog configuration, then look for updates
|
2019-07-19 01:26:15 +03:00
|
|
|
::
|
2019-10-25 19:39:02 +03:00
|
|
|
++ configure
|
|
|
|
|= [context =config]
|
|
|
|
=/ m (async:stdio ,watchdog)
|
2019-07-05 04:15:53 +03:00
|
|
|
^- form:m
|
2019-10-25 19:39:02 +03:00
|
|
|
%+ get-updates path
|
|
|
|
%_ dog
|
|
|
|
- config
|
2019-10-29 22:51:58 +03:00
|
|
|
number from.config
|
2019-10-25 19:39:02 +03:00
|
|
|
==
|
2019-07-19 01:26:15 +03:00
|
|
|
::
|
|
|
|
:: Get updates since last checked
|
|
|
|
::
|
|
|
|
++ get-updates
|
2019-10-25 19:39:02 +03:00
|
|
|
|= context
|
|
|
|
=/ m (async:stdio ,watchdog)
|
2019-07-19 01:26:15 +03:00
|
|
|
^- form:m
|
2019-11-04 20:59:30 +03:00
|
|
|
;< =latest=block bind:m (get-latest-block:ethio url.dog)
|
2019-10-25 19:39:02 +03:00
|
|
|
;< dog=watchdog bind:m (zoom [path dog] number.id.latest-block)
|
2019-07-05 04:15:53 +03:00
|
|
|
|- ^- form:m
|
2019-10-25 19:39:02 +03:00
|
|
|
=* loop $
|
|
|
|
?: (gth number.dog number.id.latest-block)
|
|
|
|
(pure:m dog)
|
2019-11-04 20:59:30 +03:00
|
|
|
;< =block bind:m (get-block-by-number:ethio url.dog number.dog)
|
2019-10-30 02:37:12 +03:00
|
|
|
;< dog=watchdog bind:m
|
2019-10-25 19:39:02 +03:00
|
|
|
(take-block [path dog] block)
|
2019-10-30 02:37:12 +03:00
|
|
|
loop(dog dog)
|
2019-07-05 04:15:53 +03:00
|
|
|
::
|
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
|
2019-10-25 19:39:02 +03:00
|
|
|
|= [context =block]
|
2019-10-30 02:37:12 +03:00
|
|
|
=/ m (async:stdio ,watchdog)
|
2019-07-05 04:15:53 +03:00
|
|
|
^- form:m
|
2019-10-30 02:37:12 +03:00
|
|
|
:: if this next block isn't direct descendant of our logs, reorg happened
|
2019-10-25 19:39:02 +03:00
|
|
|
?: &(?=(^ blocks.dog) !=(parent-hash.block hash.id.i.blocks.dog))
|
2019-10-30 02:37:12 +03:00
|
|
|
(rewind [path dog] block)
|
|
|
|
;< [=new=pending-logs =released=loglist] bind:m
|
2019-10-25 19:39:02 +03:00
|
|
|
(release-old-events path pending-logs.dog number.id.block)
|
2019-10-30 20:44:34 +03:00
|
|
|
;< =new=loglist bind:m :: oldest first
|
2019-11-04 20:59:30 +03:00
|
|
|
(get-logs-by-hash:ethio url.dog hash.id.block contracts.dog topics.dog)
|
2019-10-25 19:39:02 +03:00
|
|
|
=. new-pending-logs
|
|
|
|
(~(put by new-pending-logs) number.id.block new-loglist)
|
2019-10-30 02:37:12 +03:00
|
|
|
%- pure:m
|
|
|
|
%_ dog
|
|
|
|
number +(number.id.block)
|
|
|
|
pending-logs new-pending-logs
|
|
|
|
history [released-loglist history.dog]
|
|
|
|
blocks [block blocks.dog]
|
|
|
|
==
|
2019-07-05 04:15:53 +03:00
|
|
|
::
|
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
|
2019-10-25 19:39:02 +03:00
|
|
|
|= [=path =pending-logs =number:block]
|
2019-10-30 02:37:12 +03:00
|
|
|
=/ m (async:stdio ,[^pending-logs loglist])
|
2019-07-05 04:15:53 +03:00
|
|
|
^- form:m
|
2019-10-30 02:37:12 +03:00
|
|
|
?: (lth number 30) (pure:m pending-logs ~)
|
2019-08-06 09:11:40 +03:00
|
|
|
=/ rel-number (sub number 30)
|
2019-10-25 19:39:02 +03:00
|
|
|
=/ =loglist (~(get ja pending-logs) rel-number)
|
|
|
|
;< ~ bind:m (send-logs path loglist)
|
2019-10-30 02:37:12 +03:00
|
|
|
(pure:m (~(del by pending-logs) rel-number) loglist)
|
2019-07-05 04:15:53 +03:00
|
|
|
::
|
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-10-30 02:37:12 +03:00
|
|
|
:: block: wants to be head of blocks.dog, but might not match
|
|
|
|
|= [context =block]
|
|
|
|
=/ m (async:stdio ,watchdog)
|
|
|
|
=* blocks blocks.dog
|
2019-07-05 04:15:53 +03:00
|
|
|
|- ^- form:m
|
|
|
|
=* loop $
|
2019-10-30 02:37:12 +03:00
|
|
|
:: if we have no further history to rewind, we're done
|
2019-07-05 06:53:24 +03:00
|
|
|
?~ blocks
|
2019-10-30 02:37:12 +03:00
|
|
|
(pure:m dog(blocks [block blocks]))
|
|
|
|
:: if target block is directly after "latest", we're done
|
2019-07-05 06:53:24 +03:00
|
|
|
?: =(parent-hash.block hash.id.i.blocks)
|
2019-10-30 02:37:12 +03:00
|
|
|
(pure:m dog(blocks [block blocks]))
|
|
|
|
:: next-block: the new target block
|
|
|
|
;< =next=^block bind:m
|
2019-11-04 20:59:30 +03:00
|
|
|
(get-block-by-number:ethio url.dog number.id.i.blocks)
|
2019-10-30 02:37:12 +03:00
|
|
|
:: remove from either pending-logs or history
|
|
|
|
?: =(~ pending-logs.dog)
|
|
|
|
:: if no more pending logs, start deleting from history instead
|
|
|
|
::NOTE this assumes there's one history entry per item in blocks.
|
|
|
|
:: while +zoom breaks that assumption by clearing blocks, we won't
|
|
|
|
:: run out of history before running out of blocks, allowing us to
|
|
|
|
:: skip the =(number.id.block number.id.i.i.history) check.
|
|
|
|
?~ history.dog
|
|
|
|
loop(block next-block, blocks t.blocks)
|
|
|
|
;< ~ bind:m
|
|
|
|
:: don't bother sending a disavow if there were no logs there
|
|
|
|
?~ i.history.dog (pure:(async:stdio ,~) ~)
|
|
|
|
(disavow path block)
|
|
|
|
loop(block next-block, blocks t.blocks, history.dog t.history.dog)
|
|
|
|
=. pending-logs.dog
|
|
|
|
(~(del by pending-logs.dog) number.id.block)
|
2019-07-05 06:53:24 +03:00
|
|
|
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
|
2019-10-25 19:39:02 +03:00
|
|
|
|= [=path =block]
|
2019-07-05 04:15:53 +03:00
|
|
|
=/ m (async:stdio ,~)
|
|
|
|
^- form:m
|
2019-10-25 19:39:02 +03:00
|
|
|
(send-update path %disavow id.block)
|
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
|
2019-10-25 19:39:02 +03:00
|
|
|
|= [context =latest=number:block]
|
|
|
|
=/ m (async:stdio ,watchdog)
|
2019-07-19 23:13:14 +03:00
|
|
|
^- form:m
|
2019-08-05 21:40:13 +03:00
|
|
|
=/ zoom-margin=number:block 100
|
2019-10-25 19:39:02 +03:00
|
|
|
?: (lth latest-number (add number.dog zoom-margin))
|
|
|
|
(pure:m dog)
|
2019-07-23 00:11:54 +03:00
|
|
|
=/ to-number=number:block (sub latest-number zoom-margin)
|
2019-10-30 20:44:34 +03:00
|
|
|
;< =loglist bind:m :: oldest first
|
2019-11-04 20:59:30 +03:00
|
|
|
%: get-logs-by-range:ethio
|
2019-10-25 19:39:02 +03:00
|
|
|
url.dog
|
|
|
|
contracts.dog
|
|
|
|
topics.dog
|
|
|
|
number.dog
|
|
|
|
to-number
|
|
|
|
==
|
|
|
|
;< ~ bind:m (send-logs path loglist)
|
|
|
|
=. number.dog +(to-number)
|
|
|
|
=. blocks.dog ~
|
2019-10-30 02:37:12 +03:00
|
|
|
=. history.dog [loglist history.dog]
|
2019-10-25 19:39:02 +03:00
|
|
|
(pure:m dog)
|
2019-07-05 04:15:53 +03:00
|
|
|
--
|
|
|
|
::
|
|
|
|
:: Main
|
|
|
|
::
|
|
|
|
=* default-tapp default-tapp:tapp
|
2019-10-29 21:11:36 +03:00
|
|
|
%- create-tapp-all:tapp
|
|
|
|
^- tapp-core-all:tapp
|
2019-07-19 03:08:01 +03:00
|
|
|
|_ [=bowl:gall state=app-state]
|
2019-10-29 21:11:36 +03:00
|
|
|
++ handle-init
|
|
|
|
=/ m tapp-async
|
|
|
|
^- form:m
|
2019-10-29 21:13:16 +03:00
|
|
|
:: start update timer loop
|
|
|
|
;< now=@da bind:m get-time:stdio
|
2019-11-04 20:57:17 +03:00
|
|
|
;< ~ bind:m (wait-effect:stdio (add now refresh-rate))
|
2019-10-29 21:13:16 +03:00
|
|
|
(pure:m state)
|
2019-10-29 21:11:36 +03:00
|
|
|
::
|
|
|
|
++ handle-diff handle-diff:default-tapp
|
|
|
|
::
|
2019-07-05 04:15:53 +03:00
|
|
|
++ handle-poke
|
2019-10-30 18:46:37 +03:00
|
|
|
|= in=in-poke-data
|
2019-07-05 04:15:53 +03:00
|
|
|
=/ m tapp-async
|
|
|
|
^- form:m
|
2019-10-30 18:46:37 +03:00
|
|
|
?- +<.in
|
2019-10-25 19:39:02 +03:00
|
|
|
%watch
|
2019-10-30 18:46:37 +03:00
|
|
|
:: fully restart the watchdog if it doesn't exist yet,
|
|
|
|
:: or if the new config changes more than just the url.
|
|
|
|
=/ restart=?
|
|
|
|
?| !(~(has by dogs.state) path.in)
|
|
|
|
?! .= ->:(~(got by dogs.state) path.in)
|
|
|
|
+.config.in
|
|
|
|
==
|
|
|
|
~? &((~(has by dogs.state) path.in) restart)
|
|
|
|
[dap.bowl 'overwriting existing watchdog on' path.in]
|
2019-10-25 19:39:02 +03:00
|
|
|
;< dog=watchdog bind:m
|
2019-10-30 18:46:37 +03:00
|
|
|
=/ dog=watchdog
|
|
|
|
?: restart *watchdog
|
|
|
|
(~(got by dogs.state) path.in)
|
|
|
|
(configure [path.in dog] config.in)
|
|
|
|
=. dogs.state (~(put by dogs.state) path.in dog)
|
2019-10-25 19:39:02 +03:00
|
|
|
(pure:m state)
|
|
|
|
::
|
|
|
|
%clear
|
2019-10-30 18:35:12 +03:00
|
|
|
=. dogs.state (~(del by dogs.state) path.in)
|
2019-10-25 19:39:02 +03:00
|
|
|
(pure:m state)
|
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] !!)
|
2019-10-25 19:39:02 +03:00
|
|
|
%wake
|
2019-10-29 21:13:16 +03:00
|
|
|
;< ~ bind:m
|
|
|
|
;< now=@da bind:(async:tapp ,~) get-time:stdio
|
2019-11-04 20:57:17 +03:00
|
|
|
=/ next=@da (add now refresh-rate)
|
2019-10-29 21:13:16 +03:00
|
|
|
::NOTE we use +send-raw-card here to ensure we always set a new timer,
|
|
|
|
:: regardless of what happens further on in the flow.
|
|
|
|
(send-raw-card:stdio %wait /effect/(scot %da next) next)
|
2019-10-25 19:39:02 +03:00
|
|
|
::TODO ideally we'd process these in parallel. this seems possible,
|
|
|
|
:: but requires non-trivial work, as it deviates from tapp's flow.
|
|
|
|
:: (when making that change, take note of rpc request id's.)
|
|
|
|
=/ dogs=(list [=path dog=watchdog]) ~(tap by dogs.state)
|
|
|
|
|- ^- form:m
|
|
|
|
=* loop $
|
|
|
|
?~ dogs
|
|
|
|
(pure:m state)
|
|
|
|
=, i.dogs
|
|
|
|
;< dog=watchdog bind:m (get-updates path dog)
|
|
|
|
=. dogs.state (~(put by dogs.state) path dog)
|
|
|
|
loop(dogs t.dogs)
|
2019-07-19 01:26:15 +03:00
|
|
|
==
|
2019-07-05 04:15:53 +03:00
|
|
|
::
|
2019-10-30 02:37:12 +03:00
|
|
|
:: +handle-peer: subscribe & get initial subscription data
|
|
|
|
::
|
|
|
|
:: /logs/some-path:
|
|
|
|
::
|
2019-07-28 07:01:55 +03:00
|
|
|
++ handle-peer
|
|
|
|
|= =path
|
|
|
|
=/ m tapp-async
|
|
|
|
^- form:m
|
2019-10-30 02:37:12 +03:00
|
|
|
?. ?=([%logs ^] path)
|
|
|
|
~| [%invalid-subscription-path path]
|
|
|
|
!!
|
|
|
|
;< ~ bind:m
|
|
|
|
%+ send-effect-on-bone:stdio ost.bowl
|
|
|
|
:+ %diff %eth-watcher-diff
|
|
|
|
:- %history
|
|
|
|
^- loglist
|
2019-10-30 20:44:34 +03:00
|
|
|
%- zing
|
|
|
|
%- flop
|
|
|
|
=< history
|
|
|
|
(~(gut by dogs.state) t.path *watchdog)
|
2019-10-25 19:39:02 +03:00
|
|
|
(pure:m state)
|
2019-10-29 21:11:36 +03:00
|
|
|
::
|
|
|
|
:: +handle-peek: get diagnostics data
|
|
|
|
::
|
|
|
|
:: /block/some-path: get next block number to check for /some-path
|
|
|
|
::
|
|
|
|
++ handle-peek
|
|
|
|
|= =path
|
|
|
|
^- (unit (unit peek-data))
|
|
|
|
?. ?=([%x %block ^] path) ~
|
|
|
|
?. (~(has by dogs.state) t.t.path) ~
|
|
|
|
:+ ~ ~
|
|
|
|
:- %atom
|
|
|
|
number:(~(got by dogs.state) t.t.path)
|
2019-07-05 04:15:53 +03:00
|
|
|
--
|