shrub/pkg/arvo/app/azimuth.hoon

409 lines
10 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
$: %2
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)
=, old-state
[%1 url net whos nas own *sponsors logs]
=^ cards-1 old-state
?. ?=(%1 -.old-state)
`old-state
%- %- slog :_ ~
leaf+"azimuth: loading snapshot with {<(lent logs.old-state)>} events"
=. +.state +.old-state
=^ cards state
(%*(run-logs do nas.state *^state:naive) logs.state)
[(jael-update:do (to-udiffs:do cards)) state]
?> ?=(%2 -.old-state)
[cards-1 this(state old-state)]
::
++ app-states $%(state-0 state-1 app-state)
2021-11-10 19:46:22 +03:00
::
+$ state-1
$: %1
url=@ta
=net
whos=(set ship)
nas=^state:naive
own=owners
spo=sponsors
logs=(list =event-log:rpc:ethereum)
==
2021-11-10 19:46:22 +03:00
::
++ 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
==
[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"
2021-11-17 14:40:06 +03:00
=^ snap-cards state
(%*(run-logs do nas.state *^state:naive) logs.state)
[(weld (jael-update:do (to-udiffs:do snap-cards)) 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)
2021-11-17 08:08:01 +03:00
?^ error.sign-arvo
%- (slog 'azimuth: failed to initialize!' ~)
`this
:_ 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)
=| effects=(list tagged-diff)
!. :: saves 700MB replaying snapshot
=- =/ res (mule -)
?- -.res
%& p.res
%| (mean 'naive: fail!' p.res)
==
|.
?~ logs
[(flop effects) state]
?~ mined.i.logs
$(logs t.logs)
2021-11-24 17:31:59 +03:00
=+ cache=nas.state
=^ raw-effects 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]
(%*(. naive lac |) verifier chain-id.net nas.state input)
:: TODO: make index update optional?
::
2021-11-24 17:31:59 +03:00
=/ =indices [own spo]:state
=. indices
?: =(naive.net address.i.logs)
(tx-effects:dice chain-id.net raw-effects cache indices)
2021-11-24 17:31:59 +03:00
=< indices
(point-effects:dice raw-effects cache nas.state indices)
2021-11-24 17:31:59 +03:00
=: own.state own.indices
spo.state spo.indices
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 (welp (flop effects-1) effects)
$(logs t.logs)
::
++ 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]~
--