Skip to content

A peer-to-peer stack for code collaboration

Daemon republishes IPNS for writers on init. (#462)

James Haydon0b4a8b6df2a0
diff --git a/exe/Daemon.hs b/exe/Daemon.hs
index a9a365d..d3611ce 100644
--- a/exe/Daemon.hs
+++ b/exe/Daemon.hs
@@ -141,11 +141,11 @@ server env = hoistServer Api.daemonApi nt daemonServer
-- * Init
-- | Initiate machines according to follow file.
-init :: MachineConfig -> Daemon ()
+init :: MachinesConfig -> Daemon ()
init follows = traverse_ initMachine (Map.toList follows)
where
- initMachine (id, Reader) = initReaderNoFail id
- initMachine (id, Writer) = initAsWriter id $> ()
+ initMachine (id, ConfigReader) = initReaderNoFail id
+ initMachine (id, ConfigWriter idx) = initAsWriter id idx $> ()
-- * Endpoints
@@ -153,7 +153,7 @@ init follows = traverse_ initMachine (Map.toList follows)
-- /writer/.
newMachine :: Daemon Api.NewResponse
newMachine = do
- id <- createMachine `ipfs` CouldNotCreateMachine
+ id <- ipfs createMachine CouldNotCreateMachine
sub <- machineIpfs id $ initSubscription id
logInfo Normal "Created new IPFS machine" [("machine-id", getMachineId id)]
m <- liftIO $ emptyMachine id Writer sub
@@ -316,7 +316,7 @@ addInputs is getIdx after m =
idx <- getIdx
t <- liftIO $ Time.getSystemTime
let m' = m { machineState = newState
- , machineLastIndex = Just idx
+ , machineLastIndex = idx
, machineLastUpdated = t
}
after rs
@@ -370,7 +370,7 @@ emptyMachine id mode sub = do
t <- Time.getSystemTime
pure Machine{ machineId = id
, machineState = pureEnv
- , machineLastIndex = Nothing
+ , machineLastIndex = emptyMachineEntryIndex
, machineMode = mode
, machineSubscription = sub
, machineLastUpdated = t
@@ -428,8 +428,8 @@ refreshAsReader id = modifyMachine id refresh
where
refresh m = do
let currentIdx = machineLastIndex m
- (newIdx, is) <- machineIpfs id $ machineInputsFrom (machineId m) currentIdx
- if currentIdx == Just newIdx
+ (newIdx, is) <- machineIpfs id $ machineInputsFrom (machineId m) (Just currentIdx)
+ if currentIdx == newIdx
then do
logInfo Debug
"Reader is already up to date"
@@ -445,8 +445,9 @@ refreshAsReader id = modifyMachine id refresh
-- ** Writer
-initAsWriter :: MachineId -> Daemon Machine
-initAsWriter id = do
+initAsWriter :: MachineId -> MachineEntryIndex -> Daemon Machine
+initAsWriter id idx = do
+ machineIpfs id $ ipnsPublish id idx
m <- loadMachine Writer id
actAsWriter m
pure m
diff --git a/src/Radicle/Daemon/Common.hs b/src/Radicle/Daemon/Common.hs
index 80c72e7..8de15a8 100644
--- a/src/Radicle/Daemon/Common.hs
+++ b/src/Radicle/Daemon/Common.hs
@@ -39,7 +39,7 @@ instance A.FromJSON ReaderOrWriter
data Machine = Machine
{ machineId :: MachineId
, machineState :: Bindings (PrimFns Identity)
- , machineLastIndex :: Maybe MachineEntryIndex
+ , machineLastIndex :: MachineEntryIndex
, machineMode :: ReaderOrWriter
, machineSubscription :: TopicSubscription
, machineLastUpdated :: Time.SystemTime
diff --git a/src/Radicle/Daemon/Ipfs.hs b/src/Radicle/Daemon/Ipfs.hs
index 4bf3db7..39e3e9c 100644
--- a/src/Radicle/Daemon/Ipfs.hs
+++ b/src/Radicle/Daemon/Ipfs.hs
@@ -5,9 +5,11 @@ module Radicle.Daemon.Ipfs
, SubmitInputs(..)
, writeIpfs
, Radicle.Daemon.Ipfs.publish
+ , ipnsPublish
, machineInputsFrom
, createMachine
, Ipfs.MachineEntryIndex
+ , emptyMachineEntryIndex
, TopicSubscription
, initSubscription
, subscribeOne
@@ -139,6 +141,11 @@ createMachine = do
Left e -> throw (IpfsException e)
Right id -> pure (MachineId id)
+-- | Publish to IPNS a machine entry index for a machine. Should only be issued
+-- by the writer.
+ipnsPublish :: MachineId -> Ipfs.MachineEntryIndex -> IO ()
+ipnsPublish id (Ipfs.MachineEntryIndex cid) = Ipfs.namePublish (getMachineId id) $ Ipfs.AddressIpfs cid
+
-- * Topic subscriptions
type MsgHandler = Either Text Message -> IO ()
@@ -189,3 +196,6 @@ subscribeOne sub timeout pr badMsg = do
(addHandler sub onMsg)
(removeHandler sub)
(const $ forkIO (threadDelay (fromIntegral timeout * 1000) >> putMVar var Nothing) *> readMVar var)
+
+emptyMachineEntryIndex :: Ipfs.MachineEntryIndex
+emptyMachineEntryIndex = Ipfs.MachineEntryIndex Ipfs.emptyMachineCid
diff --git a/src/Radicle/Daemon/MachineConfig.hs b/src/Radicle/Daemon/MachineConfig.hs
index 780dcd5..610c820 100644
--- a/src/Radicle/Daemon/MachineConfig.hs
+++ b/src/Radicle/Daemon/MachineConfig.hs
@@ -2,7 +2,8 @@
-- 'MachineConfig' tells us which machine the daemon owns and which
-- machines it follows.
module Radicle.Daemon.MachineConfig
- ( MachineConfig
+ ( MachineConfig(..)
+ , MachinesConfig
, readMachineConfigIO
, writeMachineConfig
) where
@@ -18,15 +19,26 @@ import Radicle.Daemon.Ipfs
import Radicle.Daemon.Monad
import qualified Radicle.Internal.ConcurrentMap as CMap
+data MachineConfig
+ -- | The daemon follows this machine.
+ = ConfigReader
+ -- | The daemon owns this machine, and this is the last entry index published
+ -- to IPNS.
+ | ConfigWriter MachineEntryIndex
+ deriving (Generic)
+
+instance A.ToJSON MachineConfig
+instance A.FromJSON MachineConfig
+
-- | Holds the machines the daemon owns and follows.
-type MachineConfig = Map MachineId ReaderOrWriter
+type MachinesConfig = Map MachineId MachineConfig
-readMachineConfigIO :: FileLock -> FilePath -> IO MachineConfig
+readMachineConfigIO :: FileLock -> FilePath -> IO MachinesConfig
readMachineConfigIO lock ff = withFileLock lock $ do
exists <- doesFileExist ff
t <- if exists
then readFile ff
- else let noFollows = toS (A.encode (Map.empty :: MachineConfig))
+ else let noFollows = toS (A.encode (Map.empty :: MachinesConfig))
in writeFile ff noFollows $> noFollows
case A.decode (toS t) of
Nothing -> panic $ "Invalid daemon-follow file: could not decode " <> toS ff
@@ -42,8 +54,8 @@ writeMachineConfig = do
let fs = mode <$> ms
writeFile ff (toS (A.encode fs))
where
- mode (UninitialisedReader _) = Reader
- mode (Cached c) = machineMode c
+ mode (Cached Machine{machineMode = Writer, ..}) = ConfigWriter machineLastIndex
+ mode _ = ConfigReader
withFileLock :: FileLock -> IO a -> IO a
withFileLock lock = withMVar lock . const
diff --git a/src/Radicle/Internal/MachineBackend/Ipfs.hs b/src/Radicle/Internal/MachineBackend/Ipfs.hs
index 4f6cf14..4ee91f9 100644
--- a/src/Radicle/Internal/MachineBackend/Ipfs.hs
+++ b/src/Radicle/Internal/MachineBackend/Ipfs.hs
@@ -62,7 +62,7 @@ ipfsMachineCreate :: Text -> IO (Either Text Ipfs.IpnsId)
ipfsMachineCreate name = do
res <- liftIO $ tryAny $ do
Ipfs.KeyGenResponse machineId <- Ipfs.keyGen name
- Ipfs.namePublish machineId $ Ipfs.AddressIpfs emtpyMachineCid
+ Ipfs.namePublish machineId $ Ipfs.AddressIpfs emptyMachineCid
pure machineId
pure $ first (toS . displayException) res
@@ -70,6 +70,12 @@ ipfsMachineCreate name = do
newtype MachineEntryIndex = MachineEntryIndex CID
deriving (Show, Eq)
+instance Aeson.ToJSON MachineEntryIndex where
+ toJSON (MachineEntryIndex cid) = Ipfs.ipldLink cid
+
+instance Aeson.FromJSON MachineEntryIndex where
+ parseJSON = fmap MachineEntryIndex . Ipfs.parseIpldLink
+
instance (CPA t) => ToRad t MachineEntryIndex where
toRad (MachineEntryIndex cid) = String $ cidToText cid
@@ -107,14 +113,14 @@ sendIpfs ipnsId values = do
-- index. Pins the received expressions.
receiveIpfs :: Ipfs.IpnsId -> Maybe MachineEntryIndex -> IO (MachineEntryIndex, [Value])
receiveIpfs ipnsId maybeFrom = do
- let MachineEntryIndex fromCid = fromMaybe (MachineEntryIndex emtpyMachineCid) maybeFrom
+ let MachineEntryIndex fromCid = fromMaybe (MachineEntryIndex emptyMachineCid) maybeFrom
Ipfs.NameResolveResponse cid <- Ipfs.nameResolve ipnsId
blocks <- getBlocks cid fromCid
pure $ (MachineEntryIndex cid, blocks)
where
getBlocks :: CID -> CID -> IO [Value]
getBlocks cid fromCid = do
- if cid == fromCid || cid == emtpyMachineCid
+ if cid == fromCid || cid == emptyMachineCid
then pure []
else do
let addr = Ipfs.AddressIpfs cid
@@ -127,8 +133,8 @@ receiveIpfs ipnsId maybeFrom = do
-- The first entry in a machine log also points to this entry.
--
-- This is the CID produced by the document @{"radicle": true}@.
-emtpyMachineCid :: CID
-emtpyMachineCid =
+emptyMachineCid :: CID
+emptyMachineCid =
case cidFromText "zdpuAyyGtvC37aeZid2zh7LAGKCbFTn9MzdqoPpbNQm3BCwWT" of
Left e -> panic $ toS e
Right cid -> cid