A peer-to-peer stack for code collaboration
Daemon pins all expressions in reader mode. (#428)
James Haydona7a57a47d9d7
diff --git a/exe/Daemon.hs b/exe/Daemon.hsindex 225f8ed..30cd748 100644--- a/exe/Daemon.hs+++ b/exe/Daemon.hs@@ -30,10 +30,6 @@ import Radicle.Daemon.Ipfsimport qualified Radicle.Internal.CLI as Localimport qualified Radicle.Internal.ConcurrentMap as CMap---- TODO(james): Check that the IPFS functions are doing all the--- necessary pinning.--- * Typesdata MachineErrordiff --git a/src/Radicle/Daemon/Ipfs.hs b/src/Radicle/Daemon/Ipfs.hsindex 072d682..01ea062 100644--- a/src/Radicle/Daemon/Ipfs.hs+++ b/src/Radicle/Daemon/Ipfs.hs@@ -127,7 +127,7 @@ safeIpfs io = ExceptT $ liftIO $_ -> Timeout--- | Write some inputs to an IPFS machine.+-- | Write and pin some inputs to an IPFS machine.-- TODO: we might want to make this safer by taking the expected head-- MachineEntryIndex as an argument.writeIpfs :: MachineId -> [Value] -> ExceptT IpfsError IO Ipfs.MachineEntryIndex@@ -153,7 +153,7 @@ subscribeForever id messageHandler = Ipfs.subscribe topic pubsubHandlerJust msg -> Right msgNothing -> Left (toS messageData)--- | Get inputs of an IPFS machine from a certain index.+-- | Get and pin inputs of an IPFS machine from a certain index.machineInputsFrom :: MachineId -> Maybe Ipfs.MachineEntryIndex -> ExceptT IpfsError IO (Ipfs.MachineEntryIndex, [Value])machineInputsFrom (MachineId id) = safeIpfs . Ipfs.receiveIpfs iddiff --git a/src/Radicle/Internal/MachineBackend/Ipfs.hs b/src/Radicle/Internal/MachineBackend/Ipfs.hsindex a301841..3021ba5 100644--- a/src/Radicle/Internal/MachineBackend/Ipfs.hs+++ b/src/Radicle/Internal/MachineBackend/Ipfs.hs@@ -92,6 +92,7 @@ ipfsMachineBackend =pure $ first (toS . displayException) res}+-- | Write and pin a sequence of expressions to a machine.sendIpfs :: Text -> Seq Value -> IO MachineEntryIndexsendIpfs ipnsId values = doIpfs.NameResolveResponse cid <- Ipfs.nameResolve ipnsId@@ -99,6 +100,8 @@ sendIpfs ipnsId values = doIpfs.namePublish ipnsId $ Ipfs.AddressIpfs newEntryCidpure $ MachineEntryIndex newEntryCid+-- | Get a sequence of expressions from a machine, starting from an entry+-- index. Pins the received expressions.receiveIpfs :: Ipfs.IpnsId -> Maybe MachineEntryIndex -> IO (MachineEntryIndex, [Value])receiveIpfs ipnsId maybeFrom = dolet MachineEntryIndex fromCid = fromMaybe (MachineEntryIndex emtpyMachineCid) maybeFrom@@ -111,7 +114,9 @@ receiveIpfs ipnsId maybeFrom = doif cid == fromCid || cid == emtpyMachineCidthen pure []else do- entry <- Ipfs.dagGet (Ipfs.AddressIpfs cid)+ let addr = Ipfs.AddressIpfs cid+ entry <- Ipfs.dagGet addr+ _ <- Ipfs.pinAdd addrrest <- getBlocks (entryPrevious entry) fromCidpure $ rest <> entryExpressions entrydiff --git a/src/Radicle/Ipfs.hs b/src/Radicle/Ipfs.hsindex de1a5fe..f9289a3 100644--- a/src/Radicle/Ipfs.hs+++ b/src/Radicle/Ipfs.hs@@ -14,6 +14,7 @@ module Radicle.Ipfs, DagPutResponse(..), dagPut, dagGet+ , pinAdd, namePublish, NameResolveResponse(..)@@ -177,6 +178,7 @@ instance FromJSON KeyGenResponse wherenewtype DagPutResponse= DagPutResponse CID+-- | Put and pin a dag node.dagPut :: ToJSON a => a -> IO DagPutResponsedagPut obj = ipfsHttpPost "dag/put" [("pin", "true")] "arg" (Aeson.encode obj)@@ -188,7 +190,20 @@ instance FromJSON DagPutResponse whereLeft _ -> fail "invalid CID"Right cid -> pure $ DagPutResponse cid+newtype PinResponse = PinResponse [CID]+instance FromJSON PinResponse where+ parseJSON = Aeson.withObject "v0/pin/add response" $ \o -> do+ cidTexts <- o .: "Pins"+ case traverse cidFromText cidTexts of+ Left _ -> fail "invalid CID"+ Right cids -> pure $ PinResponse cids++-- | Pin objects to local storage.+pinAdd :: Address -> IO PinResponse+pinAdd addr = ipfsHttpGet "pin/add" [("arg", addressToText addr)]++-- | Get a dag node.dagGet :: FromJSON a => Address -> IO adagGet addr = doresult <- ipfsHttpGet "dag/get" [("arg", addressToText addr)]