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.hsindex 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.NewResponsenewMachine = do- id <- createMachine `ipfs` CouldNotCreateMachine+ id <- ipfs createMachine CouldNotCreateMachinesub <- machineIpfs id $ initSubscription idlogInfo 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 <- getIdxt <- liftIO $ Time.getSystemTimelet m' = m { machineState = newState- , machineLastIndex = Just idx+ , machineLastIndex = idx, machineLastUpdated = t}after rs@@ -370,7 +370,7 @@ emptyMachine id mode sub = dot <- Time.getSystemTimepure Machine{ machineId = id, machineState = pureEnv- , machineLastIndex = Nothing+ , machineLastIndex = emptyMachineEntryIndex, machineMode = mode, machineSubscription = sub, machineLastUpdated = t@@ -428,8 +428,8 @@ refreshAsReader id = modifyMachine id refreshwhererefresh m = dolet 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 == newIdxthen dologInfo 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 idxm <- loadMachine Writer idactAsWriter mpure mdiff --git a/src/Radicle/Daemon/Common.hs b/src/Radicle/Daemon/Common.hsindex 80c72e7..8de15a8 100644--- a/src/Radicle/Daemon/Common.hs+++ b/src/Radicle/Daemon/Common.hs@@ -39,7 +39,7 @@ instance A.FromJSON ReaderOrWriterdata Machine = Machine{ machineId :: MachineId, machineState :: Bindings (PrimFns Identity)- , machineLastIndex :: Maybe MachineEntryIndex+ , machineLastIndex :: MachineEntryIndex, machineMode :: ReaderOrWriter, machineSubscription :: TopicSubscription, machineLastUpdated :: Time.SystemTimediff --git a/src/Radicle/Daemon/Ipfs.hs b/src/Radicle/Daemon/Ipfs.hsindex 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 = doLeft 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 subscriptionstype 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.emptyMachineCiddiff --git a/src/Radicle/Daemon/MachineConfig.hs b/src/Radicle/Daemon/MachineConfig.hsindex 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.Ipfsimport Radicle.Daemon.Monadimport 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 MachinesConfigreadMachineConfigIO lock ff = withFileLock lock $ doexists <- doesFileExist fft <- if existsthen readFile ff- else let noFollows = toS (A.encode (Map.empty :: MachineConfig))+ else let noFollows = toS (A.encode (Map.empty :: MachinesConfig))in writeFile ff noFollows $> noFollowscase A.decode (toS t) ofNothing -> panic $ "Invalid daemon-follow file: could not decode " <> toS ff@@ -42,8 +54,8 @@ writeMachineConfig = dolet fs = mode <$> mswriteFile ff (toS (A.encode fs))where- mode (UninitialisedReader _) = Reader- mode (Cached c) = machineMode c+ mode (Cached Machine{machineMode = Writer, ..}) = ConfigWriter machineLastIndex+ mode _ = ConfigReaderwithFileLock :: FileLock -> IO a -> IO awithFileLock lock = withMVar lock . constdiff --git a/src/Radicle/Internal/MachineBackend/Ipfs.hs b/src/Radicle/Internal/MachineBackend/Ipfs.hsindex 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 = dores <- liftIO $ tryAny $ doIpfs.KeyGenResponse machineId <- Ipfs.keyGen name- Ipfs.namePublish machineId $ Ipfs.AddressIpfs emtpyMachineCid+ Ipfs.namePublish machineId $ Ipfs.AddressIpfs emptyMachineCidpure machineIdpure $ first (toS . displayException) res@@ -70,6 +70,12 @@ ipfsMachineCreate name = donewtype MachineEntryIndex = MachineEntryIndex CIDderiving (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 wheretoRad (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) maybeFromIpfs.NameResolveResponse cid <- Ipfs.nameResolve ipnsIdblocks <- getBlocks cid fromCidpure $ (MachineEntryIndex cid, blocks)wheregetBlocks :: CID -> CID -> IO [Value]getBlocks cid fromCid = do- if cid == fromCid || cid == emtpyMachineCid+ if cid == fromCid || cid == emptyMachineCidthen pure []else dolet 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" ofLeft e -> panic $ toS eRight cid -> cid