urbit/pkg/hs-urbit/lib/Vere/Pier.hs

118 lines
3.4 KiB
Haskell
Raw Normal View History

2019-07-16 03:01:45 +03:00
{-# OPTIONS_GHC -Wwarn #-}
module Vere.Pier where
2019-07-16 03:01:45 +03:00
import UrbitPrelude
import Vere.Pier.Types
2019-07-16 03:01:45 +03:00
import qualified System.Entropy as Ent
import qualified Vere.Log as Log
2019-07-16 05:20:23 +03:00
import qualified Vere.Persist as Persist
2019-07-16 03:01:45 +03:00
import qualified Vere.Serf as Serf
2019-07-12 22:18:14 +03:00
import Vere.Serf (EventId, Serf)
--------------------------------------------------------------------------------
ioDrivers = [] :: [IODriver]
2019-07-16 03:01:45 +03:00
--------------------------------------------------------------------------------
genEntropy :: IO Word512
genEntropy = fromIntegral . view (from atomBytes) <$> Ent.getEntropy 64
generateBootSeq :: Ship -> Pill -> IO BootSeq
generateBootSeq ship Pill{..} = do
ent <- genEntropy
let ovums = preKern ent <> pKernelOvums <> pUserspaceOvums
pure $ BootSeq ident pBootFormulas ovums
where
ident = LogIdentity ship True (fromIntegral $ length pBootFormulas)
preKern ent = [ Ovum (Path ["", "term", "1"]) (Boot $ Fake $ who ident)
, Ovum (Path ["", "arvo"]) (Whom ship)
, Ovum (Path ["", "arvo"]) (Wack ent)
]
--------------------------------------------------------------------------------
-- This is called to make a freshly booted pier. It assigns an identity to an
-- event log and takes a chill pill.
2019-07-16 05:20:23 +03:00
boot :: FilePath -> FilePath -> Ship -> IO (Serf, EventLog, EventId, Mug)
boot pillPath top ship = do
let logPath = top <> "/.urb/log"
2019-07-16 05:20:23 +03:00
pill <- loadFile @Pill pillPath >>= \case
Left l -> error (show l)
Right p -> pure p
seq@(BootSeq ident _ _) <- generateBootSeq ship pill
Log.wipeEvents logPath
log <- Log.open logPath
2019-07-16 05:20:23 +03:00
Log.writeIdent log ident
2019-07-17 02:14:46 +03:00
serf <- Serf.startSerfProcess top
(events, serfSt) <- Serf.bootFromSeq serf seq
2019-07-16 05:20:23 +03:00
2019-07-17 02:14:46 +03:00
Serf.requestSnapshot serf serfSt
Serf.shutdownAndKill serf 0
2019-07-16 05:20:23 +03:00
Persist.writeEvents log events
(eId, atom) : _ <- evaluate (reverse events)
Just (mug, _::Noun) <- evaluate (atom ^? atomBytes . _Cue >>= fromNoun)
2019-07-16 05:20:23 +03:00
pure (serf, log, eId, mug)
{-
What we really want to do is write the log identity and then do
normal startup, but writeIdent requires a full log state
including input/output queues.
-}
resume :: FilePath -> IO (Serf, EventLog, EventId, Mug)
resume top = do
log <- Log.open (top <> "/.urb/log")
ident <- Log.readIdent log
lastEv <- Log.latestEventNumber log
serf <- Serf.startSerfProcess top
(e, m) <- Serf.replay serf ident lastEv (Log.readEvents log)
pure (serf, log, e, m)
-- Run Pier --------------------------------------------------------------------
{-
performCommonPierStartup :: Serf.Serf
2019-06-19 03:04:57 +03:00
-> TQueue Ovum
-> TQueue (Writ [Eff])
-> TQueue (Writ [Eff])
-> LogState
-> IO Pier
performCommonPierStartup serf computeQ persistQ releaseQ logState = do
for ioDrivers $ \x -> do
bootMessage <- bornEvent x
atomically $ writeTQueue computeQ bootMessage
driverThreads <- for ioDrivers $ \x -> do
startDriver x (writeTQueue computeQ)
-- TODO: Don't do a bunch of extra work; we send all events to all drivers
portingThread <- async $ do
forever $ do
r <- atomically (readTQueue releaseQ)
for_ driverThreads $ \(_, k) ->
for_ (payload r) $ \eff ->
k eff
Serf.workerThread serf (readTQueue computeQ) undefined
pure (Pier{..})
-}