Skip to content

A peer-to-peer stack for code collaboration

Use machine backend type to make primfn names

Thomas Scholtesf1a71903bccf
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index fcc4360..c4a778e 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -1092,6 +1092,22 @@ Send the pure prelude to a chain.
Send code from a file to a remote chain.
+``(send! machine-id inputs)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Update a machine with the vector of ``inputs`` to evaluate. Returns an
+index that identifies that last input. This index can be passed to
+``receive!``
+
+``(receive! machine-id index)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Get inputs from a machine. Returns a ``[index inputs]`` pair where
+``inputs`` is a vector of expressions and ``index`` is the index of the
+last input in ``inputs``. The ``index`` argument is either ``:nothing``
+in which case all inputs are fetched or ``[:just i]`` in which case all
+inputs following after the index ``i`` are fetched.
+
``prelude/state-machine``
-------------------------
diff --git a/rad/examples/counter.rad b/rad/examples/counter.rad
index 2ed7b8c..2673cd8 100644
--- a/rad/examples/counter.rad
+++ b/rad/examples/counter.rad
@@ -31,7 +31,7 @@
(def counter/init-chain!
"Send the counter application prelude to a chain."
(fn [chain-id]
- (send! chain-id counter/prelude)
+ (chain/send! chain-id counter/prelude)
:done
))
@@ -41,7 +41,7 @@
(def chain (chain/load-chain! chain-id))
(def expr '(increment))
(def result (lookup :result (chain/eval-in-chain expr chain)))
- (send! chain-id [expr])
+ (chain/send! chain-id [expr])
result
))
diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.rad
index 58aa41e..97783f8 100644
--- a/rad/monadic/issues.rad
+++ b/rad/monadic/issues.rad
@@ -58,8 +58,9 @@
(def send-signed-command!
"Send a command signed by the keys in `my-keys.rad`."
(fn [chain cmd payload]
- (send! (lookup :url (read-ref chain))
- [(list cmd (sign-entity! payload))])))
+ (chain/send!
+ (lookup :url (read-ref chain))
+ [(list cmd (sign-entity! payload))])))
(def create-issue!
"Create a new remote issue with the keys in `my-keys.rad`."
diff --git a/rad/monadic/names.rad b/rad/monadic/names.rad
index 1af8a32..9f1cf0e 100644
--- a/rad/monadic/names.rad
+++ b/rad/monadic/names.rad
@@ -20,7 +20,7 @@
(def add-name!
(fn [name-chain-url name key]
- (send!
+ (chain/send!
name-chain-url
[(list 'add-name name key)])))
diff --git a/rad/prelude/chain.rad b/rad/prelude/chain.rad
index 3c33da1..70de6ac 100644
--- a/rad/prelude/chain.rad
+++ b/rad/prelude/chain.rad
@@ -2,7 +2,7 @@
:doc "Functions for simulating remote chains."
:exports '[new-chain load-chain! eval-in-chain update-chain-ref! eval
updatable-eval update-chain! eval-fn-app send-prelude!
- send-code!]}
+ send-code! send! receive!]}
(import prelude/io '[read-file-values!] :as 'io)
(import prelude/ref '[modify-ref] :unqualified)
@@ -16,6 +16,22 @@
;; == base-eval')
;; - A sequence of inputs.
+(def send!
+ "Update a machine with the vector of `inputs` to evaluate. Returns an
+ index that identifies that last input. This index can be passed to
+ `receive!`"
+ (fn [machine-id inputs]
+ (machine/eval-server/update! machine-id inputs)))
+
+(def receive!
+ "Get inputs from a machine. Returns a `[index inputs]` pair where
+ `inputs` is a vector of expressions and `index` is the index of the
+ last input in `inputs`. The `index` argument is either `:nothing` in
+ which case all inputs are fetched or `[:just i]` in which case all
+ inputs following after the index `i` are fetched."
+ (fn [machine-id index]
+ (machine/eval-server/get-log! machine-id index)))
+
(def @var
"A lens for variables in states of chains."
(fn [ident]
diff --git a/src/Radicle/Internal/MachineBackend/EvalServer.hs b/src/Radicle/Internal/MachineBackend/EvalServer.hs
index 216292b..6797837 100644
--- a/src/Radicle/Internal/MachineBackend/EvalServer.hs
+++ b/src/Radicle/Internal/MachineBackend/EvalServer.hs
@@ -81,8 +81,8 @@ chainSinceEndpoint = Proxy
-- * Client
--
--- | Returns definitions for the primitive functions
--- @send!@ and @receive@.
+-- | Returns definitions for the primitive functions for the
+-- @eval-server@ machine backend.
createEvalServerBackendPrimFns :: MonadIO m => IO (PrimFns m)
createEvalServerBackendPrimFns = do
httpMgr <- HttpClient.newManager HttpClient.defaultManagerSettings
@@ -91,25 +91,17 @@ createEvalServerBackendPrimFns = do
httpMachineBackendPrimFns :: MonadIO m => HttpClient.Manager -> PrimFns m
httpMachineBackendPrimFns mgr =
buildMachineBackendPrimFns MachineBackend
- { machineUpdate =
- ( "send!"
- , "Given a URL string and a value, sends the value `v` to the remote\
- \ chain located at the URL for evaluation. Returns the index of the\
- \ submitted input."
- , \url values -> do
+ { machineType = "eval-server"
+ , machineUpdate =
+ \url values -> do
res <- liftIO $ runClientM' url mgr (submit $ toList values)
pure $ case res of
Left servantError -> Left $ formatServantError servantError
Right val -> case fromRad val of
Left err -> Left $ "cannot parse server response: " <> err
Right n -> Right n
- )
, machineGetLog =
- ( "receive!"
- , "Given a URL string and a `[:just n]`, where `n` is an integer, returns\
- \ all inputs from the remote chain after index `n`. If the second argument\
- \ is `:nothing` all inputs are returned."
- , \url maybeIndex -> do
+ \url maybeIndex -> do
-- There is mismatch between the HTTP API and the
-- storage interface that we need to account for here.
-- 1. If we send the index `n` to the HTTP API the
@@ -126,7 +118,6 @@ httpMachineBackendPrimFns mgr =
pure $ case res of
Left err -> Left $ formatServantError err
Right values -> Right (length values + index, values)
- )
}
where
formatServantError = \case
diff --git a/src/Radicle/Internal/MachineBackend/Interface.hs b/src/Radicle/Internal/MachineBackend/Interface.hs
index 9fdf5ff..f8cf656 100644
--- a/src/Radicle/Internal/MachineBackend/Interface.hs
+++ b/src/Radicle/Internal/MachineBackend/Interface.hs
@@ -22,6 +22,7 @@ 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
@@ -31,8 +32,9 @@ import Radicle.Internal.Type
-- 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
- { machineUpdate :: (Text, Text, MachineUpdate i m)
- , machineGetLog :: (Text, Text, MachineGetLog i m)
+ { machineType :: Text
+ , machineUpdate :: MachineUpdate i m
+ , machineGetLog :: MachineGetLog i m
}
-- | Send a list of expressions to a chain identified by the first
@@ -57,38 +59,33 @@ buildMachineBackendPrimFns
:: forall i m. (Monad m, FromRad WithPos i, ToRad WithPos i)
=> MachineBackend i m -> PrimFns m
buildMachineBackendPrimFns backend =
- fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop]
+ fromList [ (unsafeToIdent updateName, Nothing, updatePrimFn)
+ , (unsafeToIdent getLogName, Nothing, getLogPrimFn)]
where
- (sendName, sendDoc, send) = machineUpdate backend
- sendPrimop =
- ( sendName
- , sendDoc
- , PrimFns.twoArg sendName $ \case
+ updateName = "machine/" <> machineType backend <> "/update!"
+ updatePrimFn =
+ PrimFns.twoArg updateName $ \case
(String id, Vec v) -> do
- res <- lift $ send id v
+ res <- lift $ machineUpdate backend id v
case res of
Left e -> throwErrorHere (SendError e)
Right r -> pure $ toRad r
- (String _, v) -> throwErrorHere $ TypeError sendName 1 TVec v
- (v, _) -> throwErrorHere $ TypeError sendName 0 TString v
- )
+ (String _, v) -> throwErrorHere $ TypeError updateName 1 TVec v
+ (v, _) -> throwErrorHere $ TypeError updateName 0 TString v
- (receiveName, receiveDoc, receive) = machineGetLog backend
- receivePrimop =
- ( receiveName
- , receiveDoc
- , PrimFns.twoArg receiveName $ \case
+ 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 $ receive id index
+ 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 receiveName 0 TString v
- )
+ (v, _) -> throwErrorHere $ TypeError getLogName 0 TString v
throwReceiveError :: Text -> Lang m a
- throwReceiveError msg = throwErrorHere $ OtherError $ receiveName <> ": " <> msg
+ throwReceiveError msg = throwErrorHere $ OtherError $ getLogName <> ": " <> msg
diff --git a/test/server.rad b/test/server.rad
index 0e24a31..b9c0d72 100644
--- a/test/server.rad
+++ b/test/server.rad
@@ -3,7 +3,7 @@
;; Test correctness of the storage interface provided by the server.
;;
;; This script tests that the HTTP server implements the storage protocol with
-;; `send!` and `receive!` properly.
+;; `machine/eval-server/update!` and `machine/eval-server/get-log!` properly.
;;
;; The script accepts the hostname of the server as an optional argument. The
;; hostname defaults to `localhost`. We always use port 8000.
@@ -30,41 +30,41 @@
(def exprs-2 [ :f1 :f2 :f3 ])
(def test/receive-all
- "Passing `:nothing` to `receive!` returns all expressions"
+ "Passing `:nothing` to `machine/eval-server/get-log!` returns all expressions"
(fn []
(def machine-id (create-machine))
- (send! machine-id exprs-1)
- (send! machine-id exprs-2)
+ (machine/eval-server/update! machine-id exprs-1)
+ (machine/eval-server/update! machine-id exprs-2)
(def all-exprs (<> exprs-1 exprs-2))
- (match (receive! machine-id :nothing)
+ (match (machine/eval-server/get-log! machine-id :nothing)
[_ 'received-exprs] (assert-equal received-exprs all-exprs))
))
(def test/receive-last-index
(fn []
- "Passing the last index from `send!` to `receive!` returns no expressions"
+ "Passing the last index from `machine/eval-server/update!` to `machine/eval-server/get-log!` returns no expressions"
(def machine-id (create-machine))
- (def index (send! machine-id exprs-1))
- (match (receive! machine-id [:just index])
+ (def index (machine/eval-server/update! machine-id exprs-1))
+ (match (machine/eval-server/get-log! machine-id [:just index])
[_ 'received-exprs] (assert-equal received-exprs []))
))
(def test/receive-with-index
(fn []
- "Passing an index from `send!` to `receive!` returns no expressions"
+ "Passing an index from `machine/eval-server/update!` to `machine/eval-server/get-log!` returns no expressions"
(def machine-id (create-machine))
- (def index (send! machine-id exprs-1))
- (send! machine-id exprs-2)
- (match (receive! machine-id [:just index])
+ (def index (machine/eval-server/update! machine-id exprs-1))
+ (machine/eval-server/update! machine-id exprs-2)
+ (match (machine/eval-server/get-log! machine-id [:just index])
[_ 'received-exprs] (assert-equal received-exprs exprs-2))
))
(def test/receive-returns-sent-index
- "`receive!` returns the last sent index"
+ "`machine/eval-server/get-log!` returns the last sent index"
(fn []
(def machine-id (create-machine))
- (def index-send (send! machine-id exprs-1))
- (match (receive! machine-id :nothing)
+ (def index-send (machine/eval-server/update! machine-id exprs-1))
+ (match (machine/eval-server/get-log! machine-id :nothing)
['index-receive _] (assert-equal index-receive index-send))
))
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index 5e47e6a..c91d8d0 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -118,32 +118,27 @@ sourceFiles = do
testBindings :: Bindings (PrimFns (StateT WorldState IO))
testBindings = addPrimFns memoryMachineBackendPrimFns (replBindings [])
--- | Provides @send!@ and @receive!@ functions that store chains in a
--- 'Map' in the 'WorldState'.
+-- | 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
- { machineUpdate =
- ( "send!"
- , "Mocked version of `send!` that stores sent values in a map with chains as keys."
- , \id values -> do
+ { 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 =
- ( "receive!"
- , "Mocked version of `receive!` that retrieves values from a map with chains as keys."
- , \id maybeIndex -> do
+ \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