king: factor scrying logic into helper lib

Takes care of constructing the full scry path based off the given
components. Fills in timestamp with the current time. Logs errors on
result conversion failures.
This commit is contained in:
fang 2020-10-28 19:56:03 +01:00
parent af225f673a
commit 5615b5dc1a
No known key found for this signature in database
GPG Key ID: EB035760C1BBA972
3 changed files with 37 additions and 34 deletions

View File

@ -0,0 +1,31 @@
{-|
Scry helpers
-}
module Urbit.King.Scry (scryNow) where
import Urbit.Prelude
import Urbit.Vere.Serf.Types
import qualified Urbit.Noun.Time as Time
scryNow :: forall e n
. (HasLogFunc e, FromNoun n)
=> (Time.Wen -> Gang -> Path -> IO (Maybe (Term, Noun)))
-> Text
-> Ship
-> Text
-> [Text]
-> RIO e (Maybe n)
scryNow scry vare ship desk path = do
env <- ask
wen <- io Time.now
let wan = tshow $ Time.MkDate wen
let pax = Path $ fmap MkKnot $ vare : (tshow ship) : desk : wan : path
io (scry wen Nothing pax) >>= \case
Just (_, fromNoun @n -> Just v) -> pure $ Just v
Just (_, n) -> do
logError $ displayShow (vare, "uncanny scry result", pax, n)
pure Nothing
Nothing -> pure Nothing

View File

@ -9,6 +9,7 @@ import Urbit.Prelude
import Network.Socket hiding (recvFrom, sendTo)
import Urbit.Arvo hiding (Fake)
import Urbit.King.Config
import Urbit.King.Scry
import Urbit.Vere.Ames.LaneCache
import Urbit.Vere.Ames.Packet
import Urbit.Vere.Pier.Types
@ -341,23 +342,13 @@ ames env who isFake scry enqueueEv stderr = (initialEvents, runAmes)
scryLane :: HasLogFunc e
=> Ship
-> RIO e (Maybe [AmesDest])
scryLane ship = scry' ["peers", MkKnot $ tshow ship, "forward-lane"]
scryLane ship = scry' ["peers", tshow ship, "forward-lane"]
scry' :: forall e n
. (HasLogFunc e, FromNoun n)
=> [Knot]
=> [Text]
-> RIO e (Maybe n)
scry' p = do
env <- ask
wen <- io Time.now
let nkt = MkKnot $ tshow $ Time.MkDate wen
let pax = Path $ "ax" : MkKnot (tshow who) : "" : nkt : p
io (scry wen Nothing pax) >>= \case
Just (_, fromNoun @n -> Just v) -> pure $ Just v
Just (_, n) -> do
logError $ displayShow ("ames: uncanny scry result", pax, n)
pure Nothing
Nothing -> pure Nothing
scry' = scryNow scry "ax" who ""
ipv4Addr (Jammed (AAVoid v )) = absurd v
ipv4Addr (Jammed (AAIpv4 a p)) = SockAddrInet (fromIntegral p) (unIpv4 a)

View File

@ -12,6 +12,7 @@ module Urbit.Vere.Eyre.KingSubsite
import Urbit.Prelude hiding (Builder)
import Data.ByteString.Builder
import Urbit.King.Scry
import Urbit.Vere.Serf.Types
import Data.Conduit (ConduitT, Flush(..), yield)
@ -100,27 +101,7 @@ kingSubsite who scry func = do --TODO unify who into scry
=> Text
-> RIO e (Maybe Bool)
scryAuth cookie =
scry' $ fmap MkKnot ["authenticated", "cookie", textAsTa cookie]
--TODO refactor into scry lib, as:
-- (forall n. FromNoun n => Text -> Text -> [Text] -> IO (Maybe n))
-- vanecare -> desk -> restofpath
scry' :: forall e n
. (HasLogFunc e, FromNoun n)
=> [Knot]
-> RIO e (Maybe n)
scry' p = do
env <- ask
wen <- io Time.now
let nkt = MkKnot $ tshow $ Time.MkDate wen
let pax = Path $ "ex" : MkKnot (tshow who) : "" : nkt : p
putStrLn (tshow pax)
io (scry wen Nothing pax) >>= \case
Just (_, fromNoun @n -> Just v) -> pure $ Just v
Just (_, n) -> do
logError $ displayShow ("eyre: uncanny scry result", pax, n)
pure Nothing
Nothing -> pure Nothing
scryNow scry "ex" who "" $ ["authenticated", "cookie", textAsTa cookie]
fourOhFourSubsite :: Ship -> KingSubsite
fourOhFourSubsite who = KS $ \req respond ->