urbit/pkg/arvo/ted/eth-watcher.hoon

106 lines
3.2 KiB
Plaintext
Raw Normal View History

2019-11-12 08:36:32 +03:00
:: eth-watcher: ethereum event log collector
::
/- spider, *eth-watcher
2019-11-19 08:08:06 +03:00
/+ strandio, ethio
2019-11-12 08:36:32 +03:00
=, ethereum-types
=, able:jael
::
:: Main loop: get updates since last checked
::
2019-11-16 04:37:20 +03:00
|= args=vase
2019-11-12 08:36:32 +03:00
|^
=+ !<(pup=watchpup args)
=/ m (strand:strandio ,vase)
2019-11-12 08:36:32 +03:00
^- form:m
;< =latest=block bind:m (get-latest-block:ethio url.pup)
;< pup=watchpup bind:m (zoom pup number.id.latest-block)
=| vows=disavows
?. eager.pup
(pure:m !>([vows pup]))
2019-11-12 08:36:32 +03:00
|- ^- form:m
=* loop $
?: (gth number.pup number.id.latest-block)
(pure:m !>([vows pup]))
;< =block bind:m (get-block-by-number:ethio url.pup number.pup)
;< [=new=disavows pup=watchpup] bind:m (take-block pup block)
%= loop
pup pup
vows (weld vows new-disavows)
==
::
:: Process a block, detecting and handling reorgs
::
++ take-block
|= [pup=watchpup =block]
=/ m (strand:strandio ,[disavows watchpup])
2019-11-12 08:36:32 +03:00
^- form:m
:: if this next block isn't direct descendant of our logs, reorg happened
?: &(?=(^ blocks.pup) !=(parent-hash.block hash.id.i.blocks.pup))
(rewind pup block)
;< =new=loglist bind:m :: oldest first
(get-logs-by-hash:ethio url.pup hash.id.block contracts.pup topics.pup)
%- pure:m
:- ~
%_ pup
number +(number.id.block)
pending-logs (~(put by pending-logs.pup) number.id.block new-loglist)
blocks [block blocks.pup]
==
::
:: Reorg detected, so rewind until we're back in sync
::
++ rewind
:: block: wants to be head of blocks.pup, but might not match
|= [pup=watchpup =block]
=/ m (strand:strandio ,[disavows watchpup])
2019-11-12 08:36:32 +03:00
=* blocks blocks.pup
=| vows=disavows
|- ^- form:m
=* loop $
:: if we have no further history to rewind, we're done
?~ blocks
(pure:m (flop vows) pup(blocks [block blocks]))
:: if target block is directly after "latest", we're done
?: =(parent-hash.block hash.id.i.blocks)
(pure:m (flop vows) pup(blocks [block blocks]))
:: next-block: the new target block
;< =next=^block bind:m
(get-block-by-number:ethio url.pup number.id.i.blocks)
=. pending-logs.pup (~(del by pending-logs.pup) number.id.i.blocks)
=. vows [id.block vows]
loop(block next-block, blocks t.blocks)
::
:: 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 -- 100 blocks ago is probably sufficient.
2019-11-12 08:36:32 +03:00
::
++ zoom
|= [pup=watchpup =latest=number:block]
=/ m (strand:strandio ,watchpup)
2019-11-12 08:36:32 +03:00
^- form:m
=/ zoom-margin=number:block 30
=/ zoom-step=number:block 100.000
2019-11-12 08:36:32 +03:00
?: (lth latest-number (add number.pup zoom-margin))
(pure:m pup)
=/ up-to-number=number:block (sub latest-number zoom-margin)
|-
=* loop $
?: (gth number.pup up-to-number)
(pure:m pup(blocks ~))
=/ to-number=number:block
(min up-to-number (add number.pup zoom-step))
2019-11-12 08:36:32 +03:00
;< =loglist bind:m :: oldest first
%: get-logs-by-range:ethio
url.pup
contracts.pup
topics.pup
number.pup
to-number
==
=? pending-logs.pup ?=(^ loglist)
(~(put by pending-logs.pup) to-number loglist)
loop(number.pup +(to-number))
2019-11-12 08:36:32 +03:00
--