urbit/pkg/arvo/app/azimuth.hoon

398 lines
9.9 KiB
Plaintext
Raw Normal View History

/- eth-watcher, *dice
/+ ethereum,
azimuth,
naive,
dice,
default-agent,
verb,
dbug
:: Generally don't update the snapshot until we have clay tombstoning.
:: To update, run:
:: =e -build-file %/lib/ethereum/hoon
:: =l .^((list event-log:rpc:e) %gx /=azimuth=/logs/noun)
:: */app/azimuth/logs/eth-logs &eth-logs l
::
/* snap %eth-logs /app/azimuth/logs/eth-logs
::
2021-04-21 07:57:04 +03:00
=/ last-snap :: maybe just use the last one?
%+ roll `(list event-log:rpc:ethereum)`snap ::~
2021-04-21 07:57:04 +03:00
|= [log=event-log:rpc:ethereum last=@ud]
?~ mined.log
last
(max block-number.u.mined.log last)
::
=, jael
|%
2021-08-27 17:05:59 +03:00
+$ app-state
2021-11-10 19:46:22 +03:00
$: %1
url=@ta
=net
whos=(set ship)
nas=^state:naive
own=owners
2021-11-10 19:46:22 +03:00
spo=sponsors
logs=(list =event-log:rpc:ethereum)
==
2021-08-27 17:05:59 +03:00
::
+$ poke-data
$% :: %listen
::
[%listen whos=(list ship) =source:jael]
2021-08-27 17:05:59 +03:00
:: %watch: configure node url and network
::
[%watch url=@ta =net]
==
::
2021-08-27 17:05:59 +03:00
+$ tagged-diff [=id:block diff:naive]
+$ card card:agent:gall
:: TODO: add to state?
::
++ refresh ~m5
--
::
2021-08-27 17:05:59 +03:00
=| state=app-state
%- agent:dbug
%+ verb |
^- agent:gall
=<
|_ =bowl:gall
+* this .
do ~(. +> bowl)
def ~(. (default-agent this %|) bowl)
::
++ on-init
^- (quip card _this)
=: net.state %default
logs.state snap
url.state 'http://eth-mainnet.urbit.org:8545'
==
2021-08-27 17:05:59 +03:00
:_ this
2021-11-10 09:36:05 +03:00
?: .^(? %j /(scot %p our.bowl)/fake/(scot %da now.bowl))
~
:~ :* %pass /old-tracker %agent [our.bowl %hood]
%poke %kiln-nuke !>([%azimuth-tracker %|])
==
::
[%pass /init %arvo %b %wait now.bowl]
==
2021-08-27 17:05:59 +03:00
::
++ on-save !>(state)
++ on-load
|= old=vase
^- (quip card _this)
2021-11-10 19:46:22 +03:00
|^
=+ !<(old-state=app-states old)
=? old-state ?=(%0 -.old-state)
^- app-state
=, old-state
[%1 url net whos nas own *sponsors logs]
?> ?=(%1 -.old-state)
`this(state old-state)
::
++ app-states $%(state-0 app-state)
::
++ state-0
$: %0
url=@ta
=net
whos=(set ship)
nas=^state:naive
own=owners
logs=(list =event-log:rpc:ethereum)
==
--
2021-08-27 17:05:59 +03:00
::
++ on-poke
|= [=mark =vase]
^- (quip card _this)
?: =(%noun mark)
?+ q.vase !!
%rerun
~& [%rerunning (lent logs.state)]
=. points.nas.state ~
=. own.state ~
=^ * state (run-logs:do logs.state)
`this
::
%resub
:_ this :_ ~
:* %pass /eth-watcher %agent [our.bowl %eth-watcher]
%watch /logs/[dap.bowl]
==
2021-08-27 17:05:59 +03:00
::
%resnap
=. logs.state snap
$(mark %noun, vase !>(%rerun))
==
?: =(%eth-logs mark)
=+ !<(logs=(list event-log:rpc:ethereum) vase)
=. logs.state logs
$(mark %noun, vase !>(%rerun))
::
?. ?=(%azimuth-poke mark)
(on-poke:def mark vase)
=+ !<(poke=poke-data vase)
?- -.poke
%listen
[[%pass /lo %arvo %j %listen (silt whos.poke) source.poke]~ this]
::
%watch
:: TODO: only wipe out state when switching networks?
:: ?: =(net.state net.poke)
:: [~ this]
=: nas.state *^state:naive
net.state net.poke
url.state url.poke
own.state ~
2021-11-10 19:46:22 +03:00
spo.state ~
logs.state ?:(?=(%default net.poke) snap ~)
2021-08-27 17:05:59 +03:00
==
%- %- slog :_ ~
leaf+"azimuth: loading snapshot with {<(lent logs.state)>} events"
=^ snap-cards state (run-logs:do logs.state)
[(weld (jael-update:do (to-udiffs:do snap-cards)) start:do) this]
2021-08-27 17:05:59 +03:00
==
::
2021-08-27 17:05:59 +03:00
++ on-watch
|= =path
^- (quip card _this)
?< =(/sole/drum path)
?: =(/event path)
:_ this
2021-11-10 19:46:22 +03:00
[%give %fact ~ %naive-state !>([nas.state own.state spo.state])]~
2021-08-27 17:05:59 +03:00
=/ who=(unit ship)
?~ path ~
?: ?=([@ ~] path) ~
`(slav %p i.path)
=. whos.state
?~ who
~
(~(put in whos.state) u.who)
^- (quip card _this)
2021-11-09 11:42:34 +03:00
:: Slow to recalculate all the diffs, but this is necessary to make
:: sure Jael gets the updates from the snapshot
::
%- %- slog :_ ~
leaf+"azimuth: loading snapshot with {<(lent logs.state)>} events"
=/ res (%*(run-logs do nas.state *^state:naive) logs.state)
[(weld (jael-update:do (to-udiffs:do -.res)) start:do) this]
2021-08-27 17:05:59 +03:00
::
++ on-leave on-leave:def
++ on-peek
|= =path
^- (unit (unit cage))
|^
?+ path (on-peek:def path)
[%x %logs ~] ``noun+!>(logs.state)
[%x %nas ~] ``noun+!>(nas.state)
[%x %dns ~] ``noun+!>(dns.nas.state)
[%x %own ~] ``noun+!>(own.state)
2021-11-10 20:39:43 +03:00
[%x %spo ~] ``noun+!>(spo.state)
[%x %refresh ~] ``atom+!>(refresh)
2021-08-27 17:05:59 +03:00
[%x %point @ ~] ``noun+(point i.t.t.path)
==
::
2021-08-27 17:05:59 +03:00
++ point
|= wat=@t
^- vase
!> ^- (unit point:naive)
?~ ship=(rush wat ;~(pfix sig fed:ag))
~
(get:orm:naive points.nas.state u.ship)
--
::
++ on-agent
|= [=wire =sign:agent:gall]
^- (quip card _this)
?. ?=([%eth-watcher ~] wire)
(on-agent:def wire sign)
?. ?=(%fact -.sign)
(on-agent:def wire sign)
?. ?=(%eth-watcher-diff p.cage.sign)
(on-agent:def wire sign)
=+ !<(diff=diff:eth-watcher q.cage.sign)
?: ?=(%disavow -.diff)
[(jael-update:do [*ship id.diff %disavow ~]~) this]
::
=. logs.state
?- -.diff
:: %history loglist.diff
%history (welp logs.state loglist.diff)
%logs (welp logs.state loglist.diff)
==
=? nas.state ?=(%history -.diff) *^state:naive
=^ effects state (run-logs:do loglist.diff)
::
:_ this
%+ weld
(event-update:do effects)
(jael-update:do (to-udiffs:do effects))
::
++ on-arvo
|= [=wire =sign-arvo]
?. &(=(/init wire) ?=(%wake +<.sign-arvo))
(on-arvo:def wire sign-arvo)
:_ this
:~ :* %pass /eth-watcher %agent [our.bowl %eth-watcher]
%watch /logs/[dap.bowl]
==
::
[%pass /lo %arvo %j %listen ~ [%| dap.bowl]]
==
::
2021-08-27 17:05:59 +03:00
++ on-fail on-fail:def
--
|_ =bowl:gall
:: TODO: maybe flop the endianness here so metamask signs it in normal
:: order?
::
++ verifier
^- ^verifier:naive
|= [dat=octs v=@ r=@ s=@]
?: (gth v 3) ~ :: TODO: move to jet
=/ result
%- mule
|.
=, secp256k1:secp:crypto
%- address-from-pub:key:ethereum
%- serialize-point
(ecdsa-raw-recover (keccak-256:keccak:crypto dat) v r s)
?- -.result
%| ~
%& `p.result
==
::
++ topics
|= ships=(set ship)
^- (list ?(@ux (list @ux)))
2021-04-15 11:02:58 +03:00
~
::
++ data-to-hex
|= data=@t
?~ data *@ux
?: =(data '0x') *@ux
(hex-to-num:ethereum data)
::
++ run-logs
2021-08-27 17:05:59 +03:00
|= [logs=(list event-log:rpc:ethereum)]
2021-08-04 16:53:57 +03:00
^- (quip tagged-diff _state)
2021-08-27 17:05:59 +03:00
=+ net=(get-network net.state)
?~ logs
`state
?~ mined.i.logs
$(logs t.logs)
=/ [raw-effects=effects:naive new-nas=_nas.state]
2021-04-15 11:02:58 +03:00
=/ =^input:naive
:- block-number.u.mined.i.logs
2021-08-27 17:05:59 +03:00
?: =(azimuth.net address.i.logs)
2021-04-15 11:02:58 +03:00
=/ data (data-to-hex data.i.logs)
=/ =event-log:naive
[address.i.logs data topics.i.logs]
[%log event-log]
?~ input.u.mined.i.logs
[%bat *@]
[%bat u.input.u.mined.i.logs]
=/ res
%- mule
2021-08-27 17:05:59 +03:00
|.((%*(. naive lac |) verifier chain-id.net nas.state input))
?- -.res
%& p.res
%| ((slog 'naive-fail' p.res) `nas.state)
==
2021-11-10 19:46:22 +03:00
:: TODO: move to /lib/dice ?
::
=/ [new-own=_own.state new-spo=_spo.state]
=< [own spo]
2021-08-27 17:05:59 +03:00
?. =(azimuth.net address.i.logs)
%: apply-effects:dice
2021-11-10 19:46:22 +03:00
chain-id.net
2021-08-27 17:05:59 +03:00
raw-effects
nas.state
own.state
2021-11-10 19:46:22 +03:00
spo.state
2021-08-27 17:05:59 +03:00
==
2021-11-10 19:46:22 +03:00
%: update-indices:dice
2021-08-27 17:05:59 +03:00
raw-effects
nas.state
new-nas
own.state
2021-11-10 19:46:22 +03:00
spo.state
==
=: nas.state new-nas
own.state new-own
spo.state new-spo
2021-08-27 17:05:59 +03:00
==
=/ effects-1
=/ =id:block [block-hash block-number]:u.mined.i.logs
(turn raw-effects |=(=diff:naive [id diff]))
=^ effects-2 state $(logs t.logs)
[(welp effects-1 effects-2) state]
::
++ to-udiffs
|= effects=(list tagged-diff)
^- =udiffs:point
%+ murn effects
|= tag=tagged-diff
^- (unit [=ship =udiff:point])
?. ?=(%point +<.tag) ~
?+ +>+<.tag ~
%rift `[ship.tag id.tag %rift rift.tag]
%sponsor `[ship.tag id.tag %spon sponsor.tag]
%keys
=/ =pass
(pass-from-eth:azimuth 32^crypt.keys.tag 32^auth.keys.tag suite.keys.tag)
`[ship.tag id.tag %keys life.keys.tag suite.keys.tag pass]
==
::
++ jael-update
|= =udiffs:point
2021-08-27 17:05:59 +03:00
^- (list card)
2021-11-09 08:56:47 +03:00
:: ?: & ~ :: XX
:- [%give %fact ~[/] %azimuth-udiffs !>(udiffs)]
2021-08-27 17:05:59 +03:00
|- ^- (list card)
?~ udiffs
~
=/ =path /(scot %p ship.i.udiffs)
:: Should really give all diffs involving each ship at the same time
::
:- [%give %fact ~[path] %azimuth-udiffs !>(~[i.udiffs])]
$(udiffs t.udiffs)
::
++ event-update
2021-05-29 15:53:57 +03:00
|= effects=(list tagged-diff)
2021-08-27 17:05:59 +03:00
^- (list card)
2021-05-29 15:53:57 +03:00
%+ murn effects
|= tag=tagged-diff
2021-08-27 17:05:59 +03:00
^- (unit card)
?. |(?=(%tx +<.tag) ?=(%point +<.tag)) ~
2021-05-29 15:53:57 +03:00
%- some
2021-08-27 17:05:59 +03:00
^- card
[%give %fact ~[/event] %naive-diffs !>(+.tag)]
::
++ get-network
|= =net
2021-08-27 17:05:59 +03:00
^- [azimuth=@ux naive=@ux chain-id=@ launch=@]
=< [azimuth naive chain-id launch]
=, azimuth
?- net
%mainnet mainnet-contracts
%ropsten ropsten-contracts
%local local-contracts
2021-11-09 09:56:57 +03:00
%default contracts
==
::
++ start
2021-08-27 17:05:59 +03:00
^- (list card)
=+ net=(get-network net.state)
=/ args=vase !>
2021-08-27 17:05:59 +03:00
:+ %watch /[dap.bowl]
^- config:eth-watcher
:* url.state =(%czar (clan:title our.bowl)) refresh ~h30
(max launch.net ?:(=(net.state %default) last-snap 0))
2021-08-27 17:05:59 +03:00
~[azimuth.net]
~[naive.net]
(topics whos.state)
==
2021-08-27 17:05:59 +03:00
[%pass /wa %agent [our.bowl %eth-watcher] %poke %eth-watcher-poke args]~
--