Skip to content

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.hs
index 225f8ed..30cd748 100644
--- a/exe/Daemon.hs
+++ b/exe/Daemon.hs
@@ -30,10 +30,6 @@ import Radicle.Daemon.Ipfs
import qualified Radicle.Internal.CLI as Local
import qualified Radicle.Internal.ConcurrentMap as CMap
-
--- TODO(james): Check that the IPFS functions are doing all the
--- necessary pinning.
-
-- * Types
data MachineError
diff --git a/src/Radicle/Daemon/Ipfs.hs b/src/Radicle/Daemon/Ipfs.hs
index 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 pubsubHandler
Just msg -> Right msg
Nothing -> 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 id
diff --git a/src/Radicle/Internal/MachineBackend/Ipfs.hs b/src/Radicle/Internal/MachineBackend/Ipfs.hs
index 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 MachineEntryIndex
sendIpfs ipnsId values = do
Ipfs.NameResolveResponse cid <- Ipfs.nameResolve ipnsId
@@ -99,6 +100,8 @@ sendIpfs ipnsId values = do
Ipfs.namePublish ipnsId $ Ipfs.AddressIpfs newEntryCid
pure $ 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 = do
let MachineEntryIndex fromCid = fromMaybe (MachineEntryIndex emtpyMachineCid) maybeFrom
@@ -111,7 +114,9 @@ receiveIpfs ipnsId maybeFrom = do
if cid == fromCid || cid == emtpyMachineCid
then pure []
else do
- entry <- Ipfs.dagGet (Ipfs.AddressIpfs cid)
+ let addr = Ipfs.AddressIpfs cid
+ entry <- Ipfs.dagGet addr
+ _ <- Ipfs.pinAdd addr
rest <- getBlocks (entryPrevious entry) fromCid
pure $ rest <> entryExpressions entry
diff --git a/src/Radicle/Ipfs.hs b/src/Radicle/Ipfs.hs
index 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 where
newtype DagPutResponse
= DagPutResponse CID
+-- | Put and pin a dag node.
dagPut :: ToJSON a => a -> IO DagPutResponse
dagPut obj = ipfsHttpPost "dag/put" [("pin", "true")] "arg" (Aeson.encode obj)
@@ -188,7 +190,20 @@ instance FromJSON DagPutResponse where
Left _ -> 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 a
dagGet addr = do
result <- ipfsHttpGet "dag/get" [("arg", addressToText addr)]