Skip to content

A peer-to-peer stack for code collaboration

Remove 'MachineBackend' abstraction

Thomas Scholtescdca7559dda3
diff --git a/src/Radicle/Internal/MachineBackend/Interface.hs b/src/Radicle/Internal/MachineBackend/Interface.hs
deleted file mode 100644
index f8cf656..0000000
--- a/src/Radicle/Internal/MachineBackend/Interface.hs
+++ /dev/null
@@ -1,91 +0,0 @@
--- | This module defines an abstract 'MachineBackend' interface to
--- interact with Radicle State Machines. We provide the
--- 'buildMachinePrimFns' to add an implementation of 'MachineBackend'
--- to the primitive functions of the interpreter
---
--- A storage backend defines two functions 'MachineUpdate' and
--- 'MachineGetLog' explained below.
---
--- Machine backends use /indices/ to identify entries in a chain. These
--- indices can be used like cursors in a database.
---
--- For usage examples see "Radicle.Internal.HttpStorage" and
--- "Radicle.Internal.TestCapabilities".
-module Radicle.Internal.MachineBackend.Interface
- ( MachineBackend(..)
- , buildMachineBackendPrimFns
- ) where
-
-import Protolude hiding (TypeError)
-
-import GHC.Exts (fromList)
-
-import Radicle.Internal.Annotation (WithPos)
-import Radicle.Internal.Core
-import Radicle.Internal.Identifier (unsafeToIdent)
-import qualified Radicle.Internal.PrimFns as PrimFns
-import Radicle.Internal.Type
-
--- | Definitions for @send@ and @receive@ functions of a storage
--- backend.
---
--- The first tuple item is the Radicle identifier the function will be
--- exposed as. The second tuple item is documentation.
-data MachineBackend i m = MachineBackend
- { machineType :: Text
- , machineUpdate :: MachineUpdate i m
- , machineGetLog :: MachineGetLog i m
- }
-
--- | Send a list of expressions to a chain identified by the first
--- argument. Returns an error or the index of the expression that was
--- sent.
-type MachineUpdate i m = Text -> Seq Value -> m (Either Text i)
-
--- | Get all inputs to a machine following the given input index and
--- return a new index for further queries.
---
--- If second arugment is @'Just' i@ then we return all input expresions
--- that follow the input indexed by @i@ and not including that
--- input.
---
--- If the second argument is 'Nothing' we return all expressions.
---
--- The first item in the tuple returned is the index of the last entry
--- in the list of inputs returned.
-type MachineGetLog i m = Text -> Maybe i -> m (Either Text (i, [Value]))
-
-buildMachineBackendPrimFns
- :: forall i m. (Monad m, FromRad WithPos i, ToRad WithPos i)
- => MachineBackend i m -> PrimFns m
-buildMachineBackendPrimFns backend =
- fromList [ (unsafeToIdent updateName, Nothing, updatePrimFn)
- , (unsafeToIdent getLogName, Nothing, getLogPrimFn)]
- where
- updateName = "machine/" <> machineType backend <> "/update!"
- updatePrimFn =
- PrimFns.twoArg updateName $ \case
- (String id, Vec v) -> do
- res <- lift $ machineUpdate backend id v
- case res of
- Left e -> throwErrorHere (SendError e)
- Right r -> pure $ toRad r
- (String _, v) -> throwErrorHere $ TypeError updateName 1 TVec v
- (v, _) -> throwErrorHere $ TypeError updateName 0 TString v
-
- getLogName = "machine/" <> machineType backend <> "/get-log!"
- getLogPrimFn =
- PrimFns.twoArg updateName $ \case
- (String id, v) -> do
- index <- case fromRad v of
- Right Nothing -> pure Nothing
- Right (Just n) -> pure (Just n)
- Left e -> throwReceiveError $ "failed to parse second argument: " <> e
- res <- lift $ machineGetLog backend id index
- case res of
- Left err -> throwReceiveError $ "request failed: " <> err
- Right (newIndex, values) -> pure $ Vec $ fromList [toRad newIndex, Vec $ fromList values]
- (v, _) -> throwErrorHere $ TypeError getLogName 0 TString v
-
- throwReceiveError :: Text -> Lang m a
- throwReceiveError msg = throwErrorHere $ OtherError $ getLogName <> ": " <> msg
diff --git a/src/Radicle/Internal/MachineBackend/Ipfs.hs b/src/Radicle/Internal/MachineBackend/Ipfs.hs
index 69bc00a..9fd4697 100644
--- a/src/Radicle/Internal/MachineBackend/Ipfs.hs
+++ b/src/Radicle/Internal/MachineBackend/Ipfs.hs
@@ -17,7 +17,7 @@
--
module Radicle.Internal.MachineBackend.Ipfs where
-import Protolude hiding (TypeError, catch, try)
+import Protolude hiding (catch, try)
import Control.Exception.Safe
import Control.Monad.Fail
@@ -26,34 +26,10 @@ import qualified Data.Aeson as Aeson
import Data.IPLD.CID
import Radicle.Internal.Core
-import Radicle.Internal.Identifier
-import Radicle.Internal.MachineBackend.Interface
import Radicle.Internal.Parse
import Radicle.Internal.Pretty
-import qualified Radicle.Internal.PrimFns as PrimFns
-import Radicle.Internal.Type
import qualified Radicle.Ipfs as Ipfs
--- | Primitive functions for the IPFS machine backend and the primitive
--- function @machine/ipfs/create!@.
-ipfsPrimFns :: MonadIO m => PrimFns m
-ipfsPrimFns =
- PrimFns.addPrimFn (unsafeToIdent createFnName) createFnDoc fn $
- buildMachineBackendPrimFns ipfsMachineBackend
- where
- createFnName = "machine/ipfs/create!"
- createFnDoc =
- "Create an IPFS machine and return its ID. Takes a local alias\
- \ for the machine as an argument. To avoid conflicts this should\
- \ be a UUID in production use."
- fn = PrimFns.oneArg createFnName $ \case
- String name ->
- liftIO (ipfsMachineCreate name) >>= \case
- Left err -> throwErrorHere $ OtherError err
- Right machineId -> pure $ String machineId
- v -> throwErrorHere $ TypeError createFnName 0 TString v
-
-
-- | Create a Radicle machine and return its identifier. The argument
-- is the name to store with the returned key on the local. It is not
-- portable. To avoid conflicts it is recommended to use a UUID.
@@ -75,33 +51,8 @@ instance Aeson.ToJSON MachineEntryIndex where
instance Aeson.FromJSON MachineEntryIndex where
parseJSON = fmap MachineEntryIndex . Ipfs.parseIpldLink
-instance (CPA t) => ToRad t MachineEntryIndex where
- toRad (MachineEntryIndex cid) = String $ cidToText cid
-
-instance (CPA t) => FromRad t MachineEntryIndex where
- fromRad (String cid) =
- case cidFromText cid of
- Left err -> Left $ toS err
- Right cid' -> Right $ MachineEntryIndex cid'
- fromRad _ = Left "IPFS entry index must be a string"
-
-
-ipfsMachineBackend :: MonadIO m => MachineBackend MachineEntryIndex m
-ipfsMachineBackend =
- MachineBackend
- { machineType = "ipfs"
- , machineUpdate =
- \id values -> do
- res <- liftIO $ tryAny $ sendIpfs id values
- pure $ first (toS . displayException) res
- , machineGetLog =
- \id maybeFrom -> do
- res <- liftIO $ tryAny $ receiveIpfs id maybeFrom
- pure $ first (toS . displayException) res
- }
-
-- | Write and pin a sequence of expressions to a machine.
-sendIpfs :: Text -> Seq Value -> IO MachineEntryIndex
+sendIpfs :: Ipfs.IpnsId -> Seq Value -> IO MachineEntryIndex
sendIpfs ipnsId values = do
Ipfs.NameResolveResponse cid <- Ipfs.nameResolve ipnsId
Ipfs.DagPutResponse newEntryCid <- Ipfs.dagPut $ MachineEntry (toList values) cid
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index 92c2f65..f3732c6 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -22,7 +22,6 @@ import qualified System.FilePath.Find as FP
import Radicle
import Radicle.Internal.Crypto
import Radicle.Internal.Effects.Capabilities
-import Radicle.Internal.MachineBackend.Interface
import qualified Radicle.Internal.UUID as UUID
@@ -81,7 +80,7 @@ runPureCode code =
runCodeWithWorld :: WorldState -> Text -> IO (Either (LangError Value) Value, [Text])
runCodeWithWorld ws code = do
let prog = interpretMany "[test]" code
- bindings <- testBindings
+ bindings <- createImpureBindings []
(result, ws') <- runStateT (fst <$> runLang bindings prog) ws
pure $ (result, worldStateStdout ws')
@@ -97,33 +96,6 @@ sourceFiles = do
pure (toS path, contents)
pure $ Map.fromList filesWithContent
--- | Bindings with REPL and client stuff mocked.
-testBindings :: IO (Bindings (PrimFns (StateT WorldState IO)))
-testBindings = addPrimFns memoryMachineBackendPrimFns <$> createImpureBindings []
-
--- | Provides a fake implementation of the @eval-server" machine
--- backend that store chains in a 'Map' in the 'WorldState'.
-memoryMachineBackendPrimFns :: PrimFns (StateT WorldState IO)
-memoryMachineBackendPrimFns =
- buildMachineBackendPrimFns MachineBackend
- { machineType = "eval-server"
- , machineUpdate =
- \id values -> do
- chains <- gets worldStateRemoteChains
- let exprs = Map.findWithDefault mempty id chains
- let newExprs = exprs Seq.>< values
- modify $ \s ->
- s { worldStateRemoteChains = Map.insert id newExprs $ worldStateRemoteChains s }
- pure $ Right $ Seq.length newExprs
- , machineGetLog =
- \id maybeIndex -> do
- chains <- gets worldStateRemoteChains
- let exprs = Map.findWithDefault mempty id chains
- let index = fromMaybe 0 maybeIndex
- let newIndex = Seq.length exprs
- pure $ Right $ (newIndex, toList $ Seq.drop index exprs)
- }
-
instance {-# OVERLAPPING #-} Stdin TestLang where
getLineS = do
ws <- lift get