Skip to content

A peer-to-peer stack for code collaboration

Switch to daemon-based functionality on RSMs. (#415)

Julian Arnia0dc7550ee43
diff --git a/bin/rad-diff b/bin/rad-diff
index 3bd14b5..03205d4 100755
--- a/bin/rad-diff
+++ b/bin/rad-diff
@@ -1,10 +1,5 @@
#!/usr/bin/env radicle
-;; TODO:
-;; - don't use chain/make-chain-ref! when we don't need loading
-;; - handle parsing numbers in cmd-parsing/validation logic
-
-
(load! (find-module-file! "prelude.rad"))
(load! (find-module-file! "monadic/diff.rad"))
(load! (find-module-file! "prelude/io-utils.rad"))
@@ -13,7 +8,7 @@
(import prelude/cmd-parsing :unqualified)
(import prelude/validation :as 'validation)
-(def chain-name (fn [] (get-rsm-of-type! :rad-diff)))
+(def machine-name (fn [] (get-rsm-of-type! :rad-diff)))
(def valid-list-options
["--pending" "--retracted" "--accepted" "--rejected" "--fancy"])
@@ -27,7 +22,6 @@
Usage:
rad-diff list " list-opts-help "
- rad-diff create-chain <chain-name>
rad-diff propose <commit>
rad-diff [retract|accept|reject|show] <diff-no>
rad-diff comment <diff-no> <comment>
@@ -40,7 +34,6 @@
retract - Retract your own diff
accept - Accept a diff
reject - Reject a diff
- create-chain - Create a new diffs chain
help - Print this help and exit
"))
@@ -49,8 +42,8 @@
(parse-failure error help)))
(def verify-diff-number
- (fn [diff-no chain-ref]
- (match (lookup-default diff-no #f (list-diffs chain-ref))
+ (fn [diff-no machine]
+ (match (lookup-default diff-no #f (list-diffs machine))
#f (do
(put-str! (string-append "DIFF " (show diff-no) " does not exist."))
(exit! 1))
@@ -103,17 +96,16 @@
(def checkout-diff!
"Creates a new branch diff/<DIFF-ID> based on the DIFF, and switches to it."
- (fn [chain diff-no]
- (def chain-ref (chain/make-chain-ref! chain))
- (def diff (verify-diff-number diff-no chain-ref))
+ (fn [machine diff-no]
+ (def diff (verify-diff-number diff-no machine))
(match (process! "git" ["checkout" "-b" (string-append "diff/" (show diff-no))] "")
:ok (apply-patch! (lookup :patch diff))
_ (put-str! (string-append "Command 'git checkout -b diff/" (show diff-no) "' failed")))))
(def push-diff!
"Merges the DIFF to master, and pushes the changes."
- (fn [chain-ref diff-no]
- (def diff (verify-diff-number diff-no chain-ref))
+ (fn [machine diff-no]
+ (def diff (verify-diff-number diff-no machine))
(process! "git" ["checkout" "master"] "")
(match (apply-patch! (lookup :patch diff))
:ok (do
@@ -123,8 +115,8 @@
(exit! 1)))))
(def new-diff!
- "Creates a new diff on `chain` with a patch from the given `commit`."
- (fn [chain commit]
+ "Creates a new diff on `machine` with a patch from the given `commit`."
+ (fn [machine commit]
(verify-commit commit)
(def patch (get-patch! commit))
(def author-name (get-author-name! commit))
@@ -138,8 +130,8 @@
:author-name author-name
:author-email author-email
:patch patch})
- (simple-create-diff! (chain/make-chain-ref! chain) diff)
- (put-str! (string-append "Proposing " short-hash " to " chain))))
+ (simple-create-diff! machine diff)
+ (put-str! (string-append "Proposing " short-hash " to " machine))))
;; showing diffs
@@ -193,8 +185,7 @@
"Lists all diffs and filters them by `options`. If `--new` is passed as an
option only recent items are shown. If `--fancy is passed, the list is opened
via fzf."
- (fn [chain options]
- (def chain-ref (chain/make-chain-ref! chain))
+ (fn [machine options]
(def mk-key
(fn [diff] [(pretty-headline diff) diff]))
@@ -207,7 +198,7 @@
(def items
(map mk-key
(filter filt
- (reverse (values (list-diffs chain-ref))))))
+ (reverse (values (list-diffs machine))))))
(def items-dict (dict-from-seq items))
(if (empty-seq? items)
@@ -230,42 +221,36 @@
(put-str!
(if (eq? added-comment "")
"Item was not updated."
- (do (simple-add-comment! chain-ref diff-number added-comment get-git-username!)
+ (do (simple-add-comment! machine diff-number added-comment get-git-username!)
(string-append "Sent comment: " added-comment)))))
_ (put-str! "No selection made."))
(map (fn [d] (put-str! (first d))) items)))))
(def show-diff!
"Shows a single DIFF `n`"
- (fn [chain-name n]
+ (fn [machine n]
(if (number? n)
(if (integral? n)
- (do (def chain-ref (chain/make-chain-ref! chain-name))
- (def diff (verify-diff-number n chain-ref))
+ (do
+ (def diff (verify-diff-number n machine))
(put-str! (pretty-diff diff)))
(cmd-parse-failure "Diff numbers must be whole numbers!"))
(cmd-parse-failure "To show a diff, please provide the diff number."))))
-(def new-diff-chain!
- "Create a new DIFF chain with the given URL"
- (fn [chain-name]
- (create-diffs-chain! chain-name)))
-
(def edit-diff-state!
"Edit the state of a DIFF. If the new state is `:accepted` the DIFF is then
also merged to master and the changes pushed."
- (fn [chain-name n new-state]
+ (fn [machine n new-state]
(if (number? n)
(if (integral? n)
(do
- (def chain-ref (chain/make-chain-ref! chain-name))
(def edit-state-with-feedback!
(fn [message]
- (simple-edit-diff! chain-ref n {:state new-state})
+ (simple-edit-diff! machine n {:state new-state})
(put-str! message)))
- (verify-diff-number n chain-ref)
+ (verify-diff-number n machine)
(if (eq? new-state :accepted)
- (match (push-diff! chain-ref n)
+ (match (push-diff! machine n)
:ok (edit-state-with-feedback!
(string-append "Diff " (show n) " has been " (pretty-state new-state) " and merged."))
_ (exit! 1))
@@ -276,12 +261,12 @@
(def create-comment!
"Add a new `comment` to DIFF."
- (fn [chain-name n comment]
+ (fn [machine n comment]
(if (number? n)
(if (integral? n)
- (do (def chain-ref (chain/make-chain-ref! chain-name))
- (verify-diff-number n chain-ref)
- (simple-add-comment! chain-ref n comment get-git-username!)
+ (do
+ (verify-diff-number n machine)
+ (simple-add-comment! machine n comment get-git-username!)
(put-str! (string-append "Added comment to Diff " (show n))))
(cmd-parse-failure "Diff numbers must be whole numbers!"))
(cmd-parse-failure "To create a comment, please provide the diff number."))))
@@ -299,15 +284,15 @@
(def args (get-args!))
(match args
- (/list-cmd 'options) (list-items (chain-name) options)
- (/cmd-1 "propose" 'commit help) (new-diff! (chain-name) commit)
- (/cmd-1 "checkout" 'diff-no help) (checkout-diff! (chain-name) (read diff-no))
- (/cmd-1 "show" 'diff-no help) (show-diff! (chain-name) (read diff-no))
- (/cmd-1 "retract" 'diff-no help) (edit-diff-state! (chain-name) (read diff-no) :retracted)
- (/cmd-1 "accept" 'diff-no help) (edit-diff-state! (chain-name) (read diff-no) :accepted)
- (/cmd-1 "reject" 'diff-no help) (edit-diff-state! (chain-name) (read diff-no) :rejected)
- ["comment" 'diff-no 'comment] (create-comment! (chain-name) (read diff-no) comment)
- (/cmd-0 "create-chain" help) (new-diff-chain! (chain-name))
+ (/list-cmd 'options) (list-items (machine-name) options)
+ (/cmd-1 "propose" 'commit help) (new-diff! (machine-name) commit)
+ (/cmd-1 "checkout" 'diff-no help) (checkout-diff! (machine-name) (read diff-no))
+ (/cmd-1 "show" 'diff-no help) (show-diff! (machine-name) (read diff-no))
+ (/cmd-1 "retract" 'diff-no help) (edit-diff-state! (machine-name) (read diff-no) :retracted)
+ (/cmd-1 "accept" 'diff-no help) (edit-diff-state! (machine-name) (read diff-no) :accepted)
+ (/cmd-1 "reject" 'diff-no help) (edit-diff-state! (machine-name) (read diff-no) :rejected)
+ ["comment" 'diff-no 'comment] (create-comment! (machine-name) (read diff-no) comment)
+ (/cmd-0 "create-machine" help) (new-diff-machine! (machine-name))
["help"] (put-str! help)
["-h"] (put-str! help)
["--help"] (put-str! help)
diff --git a/bin/rad-issues b/bin/rad-issues
index 229b79d..2040721 100755
--- a/bin/rad-issues
+++ b/bin/rad-issues
@@ -6,31 +6,25 @@
(load! (find-module-file! "prelude/io-utils.rad"))
(import prelude/validation :as 'validation)
-(import prelude/chain :as 'chain)
+(import prelude/machine :as 'machine)
(import prelude/cmd-parsing :unqualified)
-(def chain-name (fn [] (get-rsm-of-type! :rad-issues)))
-
-(def name-base "http://machines.radicle.xyz/chains/names")
+(def machine-name (fn [] (get-rsm-of-type! :rad-issues)))
(def help
"rad-issues - Radicle issue CLI
Usage:
rad-issues list [--closed] [--open]
- rad-issues [new|create-chain]
+ rad-issues new
rad-issues close <issue-number>
rad-issues make-key <nick>
- rad-issues names
- rad-issues create-chain <chain-name>
+ rad-issues create-machine
rad-issues help
list - Select from issues to view or comment on
new - Create a new issue in $EDITOR
close - Close an issue
- create-chain - Create a new issues chain
- make-key - Create a key-pair and register the name
- names - See all registered names
help - Print this help and exit
")
@@ -85,20 +79,14 @@
(def list-items
"Lists the recent items and uses an editor to add a relevant comment."
- (fn [chain options]
+ (fn [machine options]
- (def chain-ref (chain/make-chain-ref! chain))
- (load! (find-module-file! "monadic/names.rad"))
- (def names (chain/make-chain-ref! name-base))
(def add-username
(fn [d]
(def n (catch
'any
(lookup :github-username d)
- (fn [x]
- (match (lookup-key names (lookup :author d))
- :nothing "???"
- 'name name))))
+ (fn [_] "???")))
(insert :username n d)))
(def add-username-issue
@@ -122,7 +110,7 @@
(def items
(map mk-key
(filter filt
- (map (fn [i] {:issue i}) (reverse (values (list-issues chain-ref)))))))
+ (map (fn [i] {:issue i}) (reverse (values (list-issues machine)))))))
(def items-dict (dict-from-seq items))
(if (empty-seq? items)
@@ -146,23 +134,21 @@
(put-str!
(if (eq? added-comment "")
"Item was not updated."
- (do (simple-add-comment! chain-ref issue-number added-comment)
+ (do (simple-add-comment! machine issue-number added-comment)
(string-append "Sent comment: " added-comment)))))
_ (put-str! "No selection made.")))))
(def close-issue
- (fn [issue-number chain-name]
+ (fn [issue-number machine-name]
(def n (read issue-number))
(if (number? n)
(if (integral? n)
- (do (def chain-ref (ref (chain/new-chain chain-name)))
- (simple-edit-issue! chain-ref n {:state :closed}))
+ (simple-edit-issue! machine n {:state :closed})
(cmd-parse-failure "Issue numbers must be whole numbers!"))
(cmd-parse-failure "To close an issue, please provide the issue number."))))
(def new-issue
- (fn [chain]
- (def chain-ref (ref (chain/new-chain chain)))
+ (fn [machine]
(def template
";; Issue template. An empty or invalid value will abort.
{:title \"Pick a title\"
@@ -171,24 +157,7 @@
}
")
(def i (read (edit-in-editor! template)))
- (create-issue!
- chain-ref (simple-issue i))))
-
-(def create-chain
- (fn [chain]
- (create-issues-chain! chain)))
-
-(def make-key
- (fn [name]
- (def kp (gen-key-pair! (default-ecc-curve)))
- (write-file! "my-keys.rad" (show kp))
- (load! (find-module-file! "monadic/names.rad"))
- (add-name! name-base name (lookup :public-key kp))))
-
-(def names
- (fn []
- (load! (find-module-file! "monadic/names.rad"))
- (print! (list-names name-base))))
+ (create-issue! machine (simple-issue i))))
(def valid-list-options
["--open" "--closed"])
@@ -208,12 +177,11 @@
(def args (get-args!))
(match args
- (/list-cmd 'options) (list-items (chain-name) options)
- (/cmd-0 "names" help) (names)
+ (/list-cmd 'options) (list-items (machine-name) options)
(/cmd-1 "make-key" 'nick help) (make-key nick)
- (/cmd-0 "new" help) (new-issue (chain-name))
- (/cmd-1 "create-chain" 'chain-name help) (create-chain chain-name)
- (/cmd-1 "close" 'issue-number help) (close-issue issue-number (chain-name))
+ (/cmd-0 "new" help) (new-issue (machine-name))
+ (/cmd-1 "create-machine" 'machine-name help) (create-machine machine-name)
+ (/cmd-1 "close" 'issue-number help) (close-issue issue-number (machine-name))
["help"] (put-str! help)
["-h"] (put-str! help)
["--help"] (put-str! help)
diff --git a/bin/rad-project b/bin/rad-project
index 6e907b0..b9c670a 100755
--- a/bin/rad-project
+++ b/bin/rad-project
@@ -22,7 +22,7 @@
checkout - Checkout a project
")
-(def base "http://localhost:8000/chains/")
+(def base "http://localhost:8000/machines/")
(def prompt-for-metadata!
(fn []
@@ -52,11 +52,9 @@
(def checkout!
"Checkout a project."
- (fn [project-url]
- (def rsm (ref (chain/new-chain project-url)))
- (fetch! rsm)
- (def name (lookup :name (get-meta! rsm)))
- (def origin (lookup :id (first-rsm-of-type! rsm :rad-repo)))
+ (fn [project]
+ (def name (lookup :name (get-meta! project)))
+ (def origin (lookup :id (first-rsm-of-type! project :rad-repo)))
(match (process! "test" ["-d" name] "")
:ok (do
(put-str! (string-append
@@ -76,35 +74,24 @@
(exit! 1)))
(set-git-config! "radicle.project-id" project-url)))
-(def new-or-forked-project!
- "Create a new or forked project. If `maybe-upstream` is :nothing, creates a
- new project. Otherwise, uses that as the url to fork from"
- (fn [maybe-upstream]
- ;; TODO: switch to IPFS
- (def project-url (string-append base (uuid!)))
- (def issues-url (string-append base (uuid!)))
- (create-issues-chain! issues-url)
- (def diff-url (string-append base (uuid!)))
- (create-diffs-chain! diff-url)
+(def new-project!
+ "Create a new project."
+ (fn []
(def meta (prompt-for-metadata!))
+ (def project (create-project! meta))
(process! "git" ["init"] "")
- (def repo-url (match maybe-upstream
- :nothing (do
- (create-project! project-url meta)
- (init-git-ipfs-repo cid-of-empty-repo))
- 'upstream (do
- ;; TODO: make ipns origin point to same as ipns upstream
- (put-str! (<> "=> adding \"upstream\" remote: " upstream)
- (process! "git" ["remote" "add" "upstream" upstream] ""))
- (error "todo: get old meta"))))
- (put-str! (string-append "=> adding \"origin\" remote: " repo-url))
- (put-str! (string-append "=> project id: " project-url))
- (process! "git" ["remote" "add" "origin" repo-url] "")
- (def project-rsm (ref (chain/new-chain project-url)))
- (add-rsm! project-rsm { :id repo-url :type :rad-repo })
- (add-rsm! project-rsm { :id issues-url :type :rad-issues })
- (add-rsm! project-rsm { :id diff-url :type :rad-diff })
- (set-git-config! "radicle.project-id" project-url)
+ (def repo (init-git-ipfs-repo cid-of-empty-repo))
+ (put-str! (string-append "=> adding \"origin\" remote: " repo))
+ (def issues (create-issues-machine!))
+ (put-str! "=> Assembled rad-issues machine")
+ (def diff (create-diffs-machine!))
+ (put-str! "=> Assembled rad-diff machine")
+ (put-str! (string-append "=> project id: " project))
+ (process! "git" ["remote" "add" "origin" repo] "")
+ (add-rsm! project { :id repo :type :rad-repo })
+ (add-rsm! project { :id issues :type :rad-issues })
+ (add-rsm! project { :id diff :type :rad-diff })
+ (set-git-config! "radicle.project-id" project)
(put-str! "=> project created!")))
@@ -115,8 +102,7 @@
(def args (get-args!))
(match args
- (/cmd-0 "init" help) (new-or-forked-project! :nothing)
- (/cmd-1 "fork" 'upstream help) (new-or-forked-project! upstream)
+ (/cmd-0 "init" help) (new-project!)
(/cmd-0 "show-id" help) (show-project-id)
(/cmd-1 "checkout" 'project-id help) (checkout! project-id)
["help"] (put-str! help)
diff --git a/docs/source/guide/Issues.lrad b/docs/source/guide/Issues.lrad
index 4382742..c24f783 100644
--- a/docs/source/guide/Issues.lrad
+++ b/docs/source/guide/Issues.lrad
@@ -1,8 +1,8 @@
-# Issues chain
+# Issues machine
## Adding an removing issues
-In this example we will create a chain which stores some issues about a code
+In this example we will create a machine which stores some issues about a code
repository. At the start it will only support adding and removing issues, but then
we will add more features.
@@ -42,7 +42,7 @@ we just check that the ID isn't already used for another issue:
(and (uuid? id)
(not (member? id (read-ref issues))))))
-To add an issue we just add it to the `issues` dict, using the `:id` as the key. Note that in the case of an error we use `print!` in this tutorial, but in a real chain one would `throw` to refuse the transaction.
+To add an issue we just add it to the `issues` dict, using the `:id` as the key. Note that in the case of an error we use `print!` in this tutorial, but in a real machine one would `throw` to refuse the transaction.
(def new-issue
(fn [i]
@@ -58,7 +58,7 @@ Closing an issue is also easy--we just delete that issue from the dict:
(fn [id]
(mod-issues (fn [is] (delete id is)))))
-The behaviour we want from our chain is to accept some commands to perform
+The behaviour we want from our machine is to accept some commands to perform
issue-related tasks. We'll store these commands in a dict, keyed by the command
symbol. We put the whole thing in a ref so that we can add more commands later,
or even update existing commands.
@@ -84,12 +84,12 @@ arguments. Finally we print the issues so that we can see how the state evolves.
:ok))
Now we update the eval. We will check for a special command `update` for
-updating the semantics of our chain (by executing arbitrary code), but otherwise
+updating the semantics of our machine (by executing arbitrary code), but otherwise
we delegate to `process-command`. In general of course you wouldn't want to
leave such an `update` command with no restrictions; we'll talk about that
later.
- (load! "rad/chain.rad")
+ (load! "rad/machine.rad")
(def eval
(updatable-eval
@@ -176,7 +176,7 @@ TODO
## Verifying authors
-This chain has issues and comments coming from users but there is nothing that
+This machine has issues and comments coming from users but there is nothing that
enforces that the issues and comments are actually submitted by these users. I
(Alice) could easily submit the comment:
@@ -184,11 +184,11 @@ enforces that the issues and comments are actually submitted by these users. I
{ :author "bobs-id"
:comment "LGTM"})
-to the chain and no one would know it wasn't Bob. To remedy this we will use
-signatures. First of all we will assume that this chain has a unique ID:
+to the machine and no one would know it wasn't Bob. To remedy this we will use
+signatures. First of all we will assume that this machine has a unique ID:
(update
- (def chain-id "some-unique-id"))
+ (def machine-id "some-unique-id"))
Signatures are created using the `gen-signature!` function, which takes a secret
key `sk` and a string `msg`. The signature `sig` that it returns can be used
@@ -203,7 +203,7 @@ We create a function to verify that issues are valid:
(fn [i]
(verify-signature (lookup :author i)
(lookup :signature i)
- (string-append chain-id
+ (string-append machine-id
(lookup :id i)
(lookup :title i)
(lookup :body i))))))
@@ -216,7 +216,7 @@ Finally we modify the `add-issue` function. Note that we re-use our old
(fn [issue]
(if (issue-valid? issue)
(new-issue issue)
- ;; In a real chain we would throw, not print a message.
+ ;; In a real machine we would throw, not print a message.
(print! "The issue was not valid.")
))))
@@ -230,7 +230,7 @@ And we update our commands:
Issues now have to be signed to be valid, here is some code one could run in
another file to create such commands:
- ;; (def chain-id "some-unique-id")
+ ;; (def machine-id "some-unique-id")
;; (def kp (gen-key-pair! (default-ecc-curve)))
@@ -241,7 +241,7 @@ another file to create such commands:
;; (fn [title body]
;; (def id (uuid!))
;; (def msg
- ;; (string-append chain-id
+ ;; (string-append machine-id
;; id
;; title
;; body))
@@ -257,7 +257,7 @@ To generate a signed issue we can call, after loading that code into a REPL:
;; (make-issue "This issue has a verified author"
;; "This is the body of the first issue with a verified author.")
-And submit the result to the chain:
+And submit the result to the machine:
(create
{:author [:public-key
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index e5085be..ff1823d 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -8,8 +8,9 @@ Primitive functions
-------------------
Primitive functions are those that are built into the compiler. They are
-available on all chains but may be shadowed by later definitions. Those
-that end in a ``!`` are only available locally, not on 'pure' chains.
+available on all machines but may be shadowed by later definitions.
+Those that end in a ``!`` are only available locally, not on 'pure'
+machines.
``*``
~~~~~
@@ -1153,97 +1154,68 @@ full absolute path of the created file.
Bypass reading the keys from ``my-keys.rad``. This is intended for
testing.
-``prelude/chain``
------------------
-
-Functions for simulating remote chains.
-
-``(new-chain url)``
-~~~~~~~~~~~~~~~~~~~
+``prelude/machine``
+-------------------
-Return an empty chain dictionary with the given url.
+Functions for simulating remote machines.
-``(load-chain! url)``
+``(eval expr state)``
~~~~~~~~~~~~~~~~~~~~~
-Takes a ``url``, and fetches the inputs of a remote chain and return a
-chain dictionary with the chain state.
-
-``(eval-in-chain expr chain)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Evaluates ``expr`` in the ``chain`` and returns a dict with the
-``:result`` and the resulting ``:chain``.
-
-``(update-chain-ref! chain-ref)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Update ``chain-ref`` containing a chain with the new expressions from
-the remote chain
-
-``(eval expr env)``
-~~~~~~~~~~~~~~~~~~~
-
-An eval in which one can use ``(:enter-chain url)`` to make the eval
-behave as that of a remote chain, and ``:send`` to send all enqueued
-expressions.
+Evaluation function that adds :test macro to register tests.
-``(update-chain! chain)``
-~~~~~~~~~~~~~~~~~~~~~~~~~
+``(updatable-eval sub-eval)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Takes a chain, and returns a new chain updated with the new expressions
-from the remote chain
+Given an evaluation function ``f``, returns a new one which augments
+``f`` with a new command ``(update expr)`` which evaluates arbitrary
+expression using ``base-eval``.
-``(send-prelude! chain-id)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+``(eval-fn-app state f arg cb)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Send the pure prelude to a chain.
+Given a state, a function, an argument and a callback, returns the
+result of evaluating the function call on the arg in the given state,
+while also calling the callback on the result.
-``(send-signed-command! chain chain-id cmd payload)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+``(send-prelude! machine-id)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Send a command signed by the keys in ``my-keys.rad``.
+Send the pure prelude to a machine.
-``(sign-entity! e chain-id)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+``(new-machine!)``
+~~~~~~~~~~~~~~~~~~
-Assumes a key pair is stored at ``my-keys.rad``. Using that key pair,
-will sign a dict by adding ``:author`` and ``:signature`` fields, so
-that it is valid according to ``validator/signed``, while also adding a
-``:nonce``.
+Creates a new machine. Returns the machine name.
-``(send-code! chain-id filename)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+``(send-code! machine-id filename)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Send code from a file to a remote chain.
+Send code from a file to a remote machine.
``(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!``
+Update a machine with the vector of ``inputs`` to evaluate. Returns a
+vector with the evaluation results.
-``(receive! machine-id index)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+``(query! machine-id expr)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-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.
+Send an expression to be evaluated on a machine. Does not alter the
+machine.
-``(make-chain-ref! chain)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~
+``(install-remote-machine-fake)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Makes a new ref containing the loaded chain.
+Install test doubles for the ``send!``, ``query!``, and
+``new-machine! primitives that use a mutable dictionary to store RSMs. Requires``\ rad/test/stub-primitives\`
+to be loaded
-``(install-remote-chain-fake)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+``(send-signed-command! machine machine-id cmd payload)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Install test doubles for the ``send!`` and ``receive!`` primitives that
-use a mutable dictionary to store RSMs. Requires
-``rad/test/stub-primitives`` to be loaded
+Send a command signed by the keys in ``my-keys.rad``.
``prelude/state-machine``
-------------------------
diff --git a/exe/Doc.hs b/exe/Doc.hs
index 2109033..94a4a6c 100644
--- a/exe/Doc.hs
+++ b/exe/Doc.hs
@@ -26,7 +26,8 @@ run f = do
let extensions = def { readerExtensions = githubMarkdownExtensions }
pand <- runIOorExplode $ readMarkdown extensions txt
let code = T.append "(load! (find-module-file! \"prelude.rad\"))" (getCode pand)
- res <- runLang (replBindings []) $ interpretMany (toS f) $ code
+ bindings <- createImpureBindings []
+ res <- runLang bindings $ interpretMany (toS f) $ code
case res of
(Left err, _) -> die . toS $ "Error: " ++ show err
_ -> pure ()
diff --git a/exe/RadicleExe.hs b/exe/RadicleExe.hs
index 2c07b1a..2c47716 100644
--- a/exe/RadicleExe.hs
+++ b/exe/RadicleExe.hs
@@ -8,10 +8,7 @@ import Options.Applicative
import System.Directory (doesFileExist)
import Radicle
-import Radicle.Daemon.Client (createDaemonClientPrimFns)
import Radicle.Internal.Effects (exitCode)
-import Radicle.Internal.MachineBackend.EvalServer
-import Radicle.Internal.MachineBackend.Ipfs (ipfsPrimFns)
import Radicle.Internal.Pretty (putPrettyAnsi)
main :: IO ()
@@ -22,7 +19,7 @@ main = do
then do
src <- decodeUtf8With lenientDecode <$> BS.getContents
let prog = interpretMany "[stdin]" src
- bindings <- createBindings (toS <$> scriptArgs opts')
+ bindings <- createImpureBindings (toS <$> scriptArgs opts')
(result, _state) <- runLang bindings prog
case result of
Left (LangError _ (Exit n)) -> pure (exitCode n)
@@ -35,7 +32,7 @@ main = do
hist <- case histFile opts' of
Nothing -> getHistoryFile
Just h -> pure h
- bindings <- createBindings (toS <$> scriptArgs opts')
+ bindings <- createImpureBindings (toS <$> scriptArgs opts')
repl (Just hist) (toS $ sourceFile opts') src bindings
exitWith code
where
@@ -87,9 +84,3 @@ opts = Opts
)
))
<*> many (strArgument mempty)
-
-createBindings :: (MonadIO m, ReplM m) => [Text] -> IO (Bindings (PrimFns m))
-createBindings scriptArgs' = do
- daemonClientPrimFns <- createDaemonClientPrimFns
- evalServerPackendPrimFns <- createEvalServerBackendPrimFns
- pure $ addPrimFns (replPrimFns scriptArgs' <> evalServerPackendPrimFns <> ipfsPrimFns <> daemonClientPrimFns) pureEnv
diff --git a/exe/ReferenceDoc.hs b/exe/ReferenceDoc.hs
index 73dd0cc..d5a9056 100644
--- a/exe/ReferenceDoc.hs
+++ b/exe/ReferenceDoc.hs
@@ -33,13 +33,14 @@ instance FromJSON Content
main :: IO ()
main = do
content <- decodeFileThrow "reference-doc.yaml"
+ bindings <- createImpureBindings []
res_ <- interpret
"reference-doc"
( "(do"
<> "(file-module! \"prelude/test-eval.rad\") (import prelude/test-eval '[eval tests] :unqualified)"
<> foldMap (\m -> "(file-module! \"" <> m <> ".rad\")") (modules content)
<> "(get-current-state))")
- (replBindings [])
+ bindings
let res = res_ `lPanic` "Error running the prelude."
let env = bindingsEnv $ bindingsFromRadicle res `lPanic` "Couldn't convert radicle state."
let rst = runPure (writeRST Default.def $ Pandoc nullMeta (doc content env)) `lPanic` "Couldn't generate RST"
diff --git a/rad/monadic/diff-remote.rad b/rad/monadic/diff-remote.rad
index 87953d1..34b83ed 100644
--- a/rad/monadic/diff-remote.rad
+++ b/rad/monadic/diff-remote.rad
@@ -8,10 +8,10 @@
(import prelude/patterns :unqualified)
(import prelude/util :unqualified)
(import prelude/lens :unqualified)
-(import prelude/chain-remote '[make-updatable-eval] :unqualified)
+(import prelude/machine-remote '[make-updatable-eval] :unqualified)
-(def chain-id
- "The name of this chain."
+(def machine-id
+ "The name of this machine."
"monadic/radicle/diffs")
(def diffs
@@ -31,7 +31,7 @@
(def input-keys
{:nonce (noncer :validator)
- :chain-id (validator/= chain-id)})
+ :machine-id (validator/= machine-id)})
(def validator/time-created
(validator/and
@@ -50,8 +50,8 @@
{:modified-at validator/timestamp}))
(def validator/input
- "Checks that the input as a valid new *chain input*, that is, it has an unused
- nonce, is marked with this chain name, and is signed."
+ "Checks that the input as a valid new *machine input*, that is, it has an unused
+ nonce, is marked with this machine name, and is signed."
(validator/and
[(validator/keys input-keys)
validator/signed]))
@@ -113,7 +113,7 @@
"After an entity input has been processed/verified, we no longer need some of
the fields."
(fn [e]
- (delete-many [:chain-id :signature :nonce :diff-number] e)))
+ (delete-many [:machine-id :signature :nonce :diff-number] e)))
(def create-diff
"Create an diff from a dict, checking that it is valid and storing it in `diffs`."
@@ -159,7 +159,7 @@
(fn [] (read-ref diffs)))
(def commands
- "The set of allowed chain commands."
+ "The set of allowed machine commands."
{'create-diff create-diff
'list-diffs list-diffs
'edit-diff edit-diff
diff --git a/rad/monadic/diff.rad b/rad/monadic/diff.rad
index 8b30f9d..0426ea6 100644
--- a/rad/monadic/diff.rad
+++ b/rad/monadic/diff.rad
@@ -1,20 +1,22 @@
(load! (find-module-file! "prelude.rad"))
(import prelude/lens :unqualified)
-(import prelude/chain '[send-code! ]:unqualified)
+(import prelude/machine '[send-code!] :unqualified)
(import prelude/time '[install-fake-clock] :unqualified)
(import prelude/io :as 'io)
(import prelude/key-management '[use-fake-keys!] :unqualified)
-(def radicle-diff-chain-id
- "The name of this chain."
+(def radicle-diff-machine-id
+ "The name of this machine."
"monadic/radicle/diffs")
-(def create-diffs-chain!
- "Create a remote diff chain with the given url."
- (fn [url]
- (chain/send-prelude! url)
- (send-code! url (find-module-file! "monadic/diff-remote.rad"))))
+(def create-diffs-machine!
+ "Create a remote diff machine. Returns the machine id"
+ (fn []
+ (def id (machine/new-machine!))
+ (machine/send-prelude! id)
+ (send-code! id (find-module-file! "monadic/diff-remote.rad"))
+ id))
(def simple-diff
"Create an open diff with no comments or labels."
@@ -38,56 +40,53 @@
(def edit-diff!
"Edit a remote diff with the keys in `my-keys.rad`."
- (fn [chain i]
- (chain/send-signed-command! chain radicle-diff-chain-id 'edit-diff i)))
+ (fn [machine i]
+ (machine/send-signed-command! machine radicle-diff-machine-id 'edit-diff i)))
(def simple-edit-diff!
- (fn [chain n i]
- (edit-diff! chain
+ (fn [machine n i]
+ (edit-diff! machine
(<> {:diff-number n
:modified-at (now!)}
i))))
(def create-diff!
"Create a new remote diff with the keys in `my-keys.rad`."
- (fn [chain i]
- (chain/send-signed-command! chain radicle-diff-chain-id 'create-diff i)))
+ (fn [machine i]
+ (machine/send-signed-command! machine radicle-diff-machine-id 'create-diff i)))
(def add-comment!
"Create a remote comment with the keys in `my-keys.rad`."
- (fn [chain c]
- (chain/send-signed-command! chain radicle-diff-chain-id 'add-comment c)))
+ (fn [machine c]
+ (machine/send-signed-command! machine radicle-diff-machine-id 'add-comment c)))
(def simple-create-diff!
"Create a remote diff with sensible defaults."
- (fn [chain diff]
- (create-diff! chain (simple-diff diff))))
+ (fn [machine diff]
+ (create-diff! machine (simple-diff diff))))
(def simple-add-comment!
"Create a remote comment."
- (fn [chain n b name]
- (add-comment! chain (simple-comment n b name))))
+ (fn [machine n b name]
+ (add-comment! machine (simple-comment n b name))))
(def list-diffs
"Return the full map of diffs."
- (fn [chain]
- (lookup :result (chain/eval-in-chain '(list-diffs) (read-ref chain)))))
+ (fn [machine]
+ (machine/query! machine '(list-diffs))))
(:test
- "The monadic diffs chain works."
+ "The monadic diffs machine works."
[:setup
- (do (chain/install-remote-chain-fake)
+ (do (machine/install-remote-machine-fake)
(use-fake-keys!)
- (def chain-name "http://foo")
- (def chain (ref (chain/new-chain chain-name)))
- (create-diffs-chain! chain-name)
- (simple-create-diff! chain {:title "title0" :description "desc0" :patch "patch0"})
- (simple-create-diff! chain {:title "title1" :description "desc1" :patch "patch1"})
- (simple-add-comment! chain 0 "comment0" "name0")
- (simple-add-comment! chain 1 "comment1" "name1")
- (simple-edit-diff! chain 0 {:state :accepted})
- (chain/update-chain-ref! chain)
- (def diffs (list-diffs chain)))]
+ (def machine (create-diffs-machine!))
+ (simple-create-diff! machine {:title "title0" :description "desc0" :patch "patch0"})
+ (simple-create-diff! machine {:title "title1" :description "desc1" :patch "patch1"})
+ (simple-add-comment! machine 0 "comment0" "name0")
+ (simple-add-comment! machine 1 "comment1" "name1")
+ (simple-edit-diff! machine 0 {:state :accepted})
+ (def diffs (list-diffs machine)))]
[ (length (seq diffs)) ==> 2 ]
[ (view (.. (@ 0) (@ :title)) diffs) ==> "title0" ]
[ (view (.. (@ 1) (@ :title)) diffs) ==> "title1" ]
diff --git a/rad/monadic/issue-remote.rad b/rad/monadic/issue-remote.rad
index 129942e..16f42d8 100644
--- a/rad/monadic/issue-remote.rad
+++ b/rad/monadic/issue-remote.rad
@@ -1,5 +1,5 @@
-;; Issue chain
-;; Meant to be sent to a chain with send-code!
+;; Issue machine
+;; Meant to be sent to a machine with send-code!
(import prelude/basic :unqualified)
(import prelude/bool :unqualified)
@@ -11,10 +11,10 @@
(import prelude/util :unqualified)
(import prelude/lens :unqualified)
(import prelude/nonces :unqualified)
-(import prelude/chain-remote '[make-updatable-eval] :unqualified)
+(import prelude/machine-remote '[make-updatable-eval] :unqualified)
-(def chain-id
- "The name of this chain."
+(def machine-id
+ "The name of this machine."
"monadic/radicle/issues")
(def issues
@@ -39,7 +39,7 @@
(def input-keys
{:nonce (noncer :validator)
- :chain-id (validator/= chain-id)})
+ :machine-id (validator/= machine-id)})
(def validator/time-created
(validator/and
@@ -58,8 +58,8 @@
{:modified-at validator/timestamp}))
(def validator/input
- "Checks that the input as a valid new *chain input*, that is, it has an unused
- nonce, is marked with this chain name, and is signed."
+ "Checks that the input as a valid new *machine input*, that is, it has an unused
+ nonce, is marked with this machine name, and is signed."
(validator/and
[(validator/keys input-keys)
validator/signed]))
@@ -157,7 +157,7 @@
"After an entity input has been processed/verified, we no longer need some of
the fields."
(fn [e]
- (delete-many [:chain-id :signature :nonce :issue-number] e)))
+ (delete-many [:machine-id :signature :nonce :issue-number] e)))
(def fix-modified-at
"Some inputs come in with a missing `:modified-at` key. If this is the case, we
@@ -217,7 +217,7 @@
(def c_ (strip-input c))
(over-issues (... [(@ n) (@ :comments) (@nth i)]) (merge-new c_))))))
-;; These are intended for clients to run locally with eval-in-chain
+;; These are intended for clients to run locally with eval-in-machine
(def list-issues
"List existing issues."
@@ -226,7 +226,7 @@
;; General logic
(def commands
- "The set of allowed chain commands."
+ "The set of allowed machine commands."
{'create-issue create-issue
'edit-issue edit-issue
'add-comment add-comment
@@ -234,4 +234,3 @@
'list-issues list-issues})
(def eval (make-updatable-eval commands))
-
diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.rad
index b0a7b74..2134c96 100644
--- a/rad/monadic/issues.rad
+++ b/rad/monadic/issues.rad
@@ -1,27 +1,26 @@
-;; Helpers for setting up and interacting with issues chain (as defined in
+;; Helpers for setting up and interacting with the issues machine (as defined in
;; rad/monadic/issues-remote.rad)
-
-;; Create a remote issue chain. Returns a ref for the chain that you
-;; can assign to a variable.
{:module 'monadic/issues
- :doc "Functions for interacting with issues RSM"
- :exports '[create-issues-chain!]}
+ :doc "Functions for interacting with issues machine"
+ :exports '[create-issues-machine!]}
(import prelude/lens :unqualified)
-(import prelude/chain '[send-code! send-signed-command!] :unqualified)
+(import prelude/machine '[send-code! send-signed-command!] :unqualified)
(import prelude/time '[install-fake-clock] :unqualified)
(import prelude/io :as 'io)
(import prelude/key-management '[use-fake-keys!] :unqualified)
-(def radicle-issues-chain-id
- "The name of this chain."
+(def radicle-issues-machine-id
+ "The name of this machine."
"monadic/radicle/issues")
-(def create-issues-chain!
- "Create a remote issue chain with the given url."
- (fn [url]
- (chain/send-prelude! url)
- (send-code! url (find-module-file! "monadic/issue-remote.rad"))))
+(def create-issues-machine!
+ "Create a remote issue machine with the given url."
+ (fn []
+ (def id (machine/new-machine!))
+ (machine/send-prelude! id)
+ (send-code! id (find-module-file! "monadic/issue-remote.rad"))
+ id))
(def simple-issue
"Create an open issue with no comments or labels."
@@ -45,44 +44,44 @@
(def create-issue!
"Create a new remote issue with the keys in `my-keys.rad`."
- (fn [chain i]
- (chain/send-signed-command! chain radicle-issues-chain-id 'create-issue i)))
+ (fn [machine i]
+ (machine/send-signed-command! machine radicle-issues-machine-id 'create-issue i)))
(def add-comment!
"Create a remote comment with the keys in `my-keys.rad`."
- (fn [chain c]
- (chain/send-signed-command! chain radicle-issues-chain-id 'add-comment c)))
+ (fn [machine c]
+ (machine/send-signed-command! machine radicle-issues-machine-id 'add-comment c)))
(def edit-issue!
"Edit a remote issue with the keys in `my-keys.rad`."
- (fn [chain i]
- (chain/send-signed-command! chain radicle-issues-chain-id 'edit-issue i)))
+ (fn [machine i]
+ (machine/send-signed-command! machine radicle-issues-machine-id 'edit-issue i)))
(def edit-comment!
"Edit a remote comment with the keys in `my-keys.rad`."
- (fn [chain c]
- (chain/send-signed-command! chain radicle-issues-chain-id 'edit-comment c)))
+ (fn [machine c]
+ (machine/send-signed-command! machine radicle-issues-machine-id 'edit-comment c)))
(def simple-create-issue!
"Create a remote issue with sensible defaults."
- (fn [chain tit bod]
- (create-issue! chain (simple-issue {:title tit :body bod}))))
+ (fn [machine tit bod]
+ (create-issue! machine (simple-issue {:title tit :body bod}))))
(def simple-add-comment!
"Create a remote comment."
- (fn [chain in b]
- (add-comment! chain (simple-comment in b))))
+ (fn [machine in b]
+ (add-comment! machine (simple-comment in b))))
(def simple-edit-issue!
- (fn [chain n i]
- (edit-issue! chain
+ (fn [machine n i]
+ (edit-issue! machine
(<> {:issue-number n
:modified-at (now!)}
i))))
(def simple-edit-comment!
- (fn [chain n i c]
- (edit-comment! chain
+ (fn [machine n i c]
+ (edit-comment! machine
(<> {:issue-number n
:comment-index i
:modified-at (now!)}
@@ -90,19 +89,14 @@
(def list-issues
"Return the full map of issues."
- (fn [chain]
- (lookup :result (chain/eval-in-chain '(list-issues) (read-ref chain)))))
-
-(def fetch!
- "Update the locally cached state of the chain."
- (fn [chain]
- (chain/update-chain-ref! chain)))
+ (fn [machine]
+ (machine/query! machine '(list-issues))))
(def import-github
"Imports the github issues stored in file `githubissues.rad`. To create this
file use the `radicle-github-issues` executable. Will add a note to all
`:body`s that this is an imported entity."
- (fn [chain]
+ (fn [machine]
(def note-imported
(fn [x]
(insert
@@ -120,7 +114,7 @@
(map
(fn [i] (insert :modified-at (lookup :created-at i) (note-imported (note-comments i))))
issues))
- (map (fn [i] (create-issue! chain i)) imported)))
+ (map (fn [i] (create-issue! machine i)) imported)))
(def short
"Short render of an issue."
@@ -132,35 +126,34 @@
(def list-last
"Get the last `n` issues."
- (fn [chain n]
- (def is (sort-by (fn [i] (- 0 (lookup :number i))) (values (list-issues chain))))
+ (fn [machine n]
+ (def is (sort-by (fn [i] (- 0 (lookup :number i))) (values (list-issues machine))))
(take n is)))
(:test
- "The monadic issues chain works."
+ "The monadic issues machine works."
[:setup
- (do (chain/install-remote-chain-fake)
+ (do (machine/install-remote-machine-fake)
+ (use-fake-keys!)
+ (def machine (create-issues-machine!))
(use-fake-keys!)
- (def chain-name (string-append "http://foo"))
- (def chain (ref (chain/new-chain chain-name)))
- (create-issues-chain! chain-name)
- (simple-create-issue! chain "title0" "body0")
- (simple-create-issue! chain "title1" "body1")
- (simple-add-comment! chain 0 "comment0")
- (simple-add-comment! chain 1 "comment1")
- (simple-edit-issue! chain 0 {:state :closed})
- (simple-edit-comment! chain 0 0 {:body "comment0-edit"})
- (simple-create-issue! chain "new" "new")
- (simple-create-issue! chain "newer" "newer")
- (simple-add-comment! chain 0 "new")
- (fetch! chain)
- (def is (list-issues chain)))]
+ (simple-create-issue! machine "title0" "body0")
+ (simple-create-issue! machine "title1" "body1")
+ (simple-add-comment! machine 0 "comment0")
+ (simple-add-comment! machine 1 "comment1")
+ (simple-edit-issue! machine 0 {:state :closed})
+ (simple-edit-comment! machine 0 0 {:body "comment0-edit"})
+ (simple-create-issue! machine "new" "new")
+ (simple-create-issue! machine "newer" "newer")
+ (simple-add-comment! machine 0 "new")
+ (def is (list-issues machine)))
+ ]
[ (length (seq is)) ==> 4 ]
[ (view (.. (@ 0) (@ :title)) is) ==> "title0" ]
[ (view (.. (@ 1) (@ :title)) is) ==> "title1" ]
[ (view (... [(@ 1) (@ :comments) (@nth 0) (@ :body)]) is) ==> "comment1" ]
[ (view (... [(@ 0) (@ :comments) (@nth 0) (@ :body)]) is) ==> "comment0-edit" ]
[ (short (first (values is))) ==> "0: title0" ]
- [ (short (first (list-last chain 1))) ==> "3: newer" ]
+ [ (short (first (list-last machine 1))) ==> "3: newer" ]
[ (view (.. (@ 0) (@ :state)) is) ==> :closed ]
)
diff --git a/rad/monadic/names.rad b/rad/monadic/names.rad
index 0448d59..648c6f0 100644
--- a/rad/monadic/names.rad
+++ b/rad/monadic/names.rad
@@ -1,43 +1,37 @@
-(def create-name-chain!
- (fn [url]
- (chain/send-prelude! url)
- (chain/send-code! url (find-module-file! "rad/monadic/names-remote.rad"))))
+(def create-name-machine!
+ (fn []
+ (def id (machine/new-machine!))
+ (machine/send-prelude! id)
+ (send-code! id (find-module-file! "monadic/names-remote.rad"))
+ id))
(def lookup-key
- (fn [chain-ref key]
- (def chain (read-ref chain-ref))
- (def names (lookup :result (chain/eval-in-chain '(list-names) chain)))
+ (fn [machine key]
+ (def names (machine/query! machine '(list-names)))
(match (filter (fn [v] (eq? (nth 1 v) key)) names)
[['name _]] name
_ :nothing)))
(def list-names
- (fn [chain-name]
- (def chain (chain/load-chain! chain-name))
- (lookup :result (chain/eval-in-chain '(list-names) chain))))
+ (fn [machine-name]
+ (def machine (machine/load-machine! machine-name))
+ (lookup :result (machine/eval-in-machine '(list-names) machine))))
(def add-name!
- (fn [name-chain-url name key]
- (chain/send!
- name-chain-url
+ (fn [name-machine-url name key]
+ (machine/send!
+ name-machine-url
[(list 'add-name name key)])))
-(def fetch!
- "Update the locally cached state of the chain."
- (fn [chain]
- (chain/update-chain-ref! chain)))
-
(:test
- "The monadic name chain works."
+ "The monadic name machine works."
[:setup
- (do (chain/install-remote-chain-fake)
- (def chain-name "http://localhost:8000/chains/shkjksa")
- (create-name-chain! chain-name)
- (add-name! chain-name "alice" "a")
- (add-name! chain-name "bob" "b")
- (def chain (ref (chain/load-chain! chain-name))))
+ (do (machine/install-remote-machine-fake)
+ (def machine (create-name-machine!))
+ (add-name! machine "alice" "a")
+ (add-name! machine "bob" "b"))
]
- [ (lookup-key chain "a") ==> "alice" ]
- [ (lookup-key chain "b") ==> "bob" ]
- [ (lookup-key chain "c") ==> :nothing ]
+ [ (lookup-key machine "a") ==> "alice" ]
+ [ (lookup-key machine "b") ==> "bob" ]
+ [ (lookup-key machine "c") ==> :nothing ]
)
diff --git a/rad/monadic/project-remote.rad b/rad/monadic/project-remote.rad
index 261d104..012b8c5 100644
--- a/rad/monadic/project-remote.rad
+++ b/rad/monadic/project-remote.rad
@@ -1,5 +1,5 @@
;; Project RSM
-;; Meant to be sent to a chain with send-code!
+;; Meant to be sent to a machine with send-code!
(import prelude/basic :unqualified)
(import prelude/bool :unqualified)
@@ -12,7 +12,7 @@
(import prelude/lens :unqualified)
(import prelude/nonces :unqualified)
(import prelude/set :as 'set)
-(import prelude/chain-remote '[make-updatable-eval] :unqualified)
+(import prelude/machine-remote '[make-updatable-eval] :unqualified)
(def rsms
"A url-based dict of RSMs."
@@ -41,7 +41,7 @@
(def validator/input
(validator/and
[ (validator/keys {:nonce (noncer :validator)
- :chain-id (validator/= rsm-id)})
+ :machine-id (validator/= rsm-id)})
validator/signed] ))
(def add-rsm
diff --git a/rad/monadic/project.rad b/rad/monadic/project.rad
index bdcee52..afbd53e 100644
--- a/rad/monadic/project.rad
+++ b/rad/monadic/project.rad
@@ -1,7 +1,7 @@
;; Helpers for setting up and interacting with project RSMs
(import prelude/lens :unqualified)
-(import prelude/chain '[send-code! send-signed-command! make-chain-ref!] :unqualified)
+(import prelude/machine '[send-code! send-signed-command! new-machine!] :unqualified)
(import prelude/time '[install-fake-clock] :unqualified)
(import prelude/io :as 'io)
(import prelude/key-management '[use-fake-keys!] :unqualified)
@@ -21,70 +21,70 @@
(def set-meta!
"Set the metadata for a project"
- (fn [chain i]
- (send-signed-command! chain rsm-id 'set-meta i)))
+ (fn [machine i]
+ (send-signed-command! machine rsm-id 'set-meta i)))
(def get-meta!
"Get the metadata for a project"
- (fn [chain]
- (lookup :result (chain/eval-in-chain '(get-meta) (read-ref chain)))))
+ (fn [machine]
+ (machine/query! machine '(get-meta))))
(def create-project!
"Create a remote project RSM with the given url."
- (fn [url meta]
- (chain/send-prelude! url)
- (send-code! url (find-module-file! "monadic/project-remote.rad"))
- (set-meta! (ref {:url url}) meta)))
+ (fn [meta]
+ (def id (new-machine!))
+ (machine/send-prelude! id)
+ (send-code! id (find-module-file! "monadic/project-remote.rad"))
+ (set-meta! id meta)
+ id))
(def add-rsm!
"Add a new RSM to the project."
- (fn [chain i]
- (send-signed-command! chain rsm-id 'add-rsm i)))
+ (fn [machine i]
+ (send-signed-command! machine rsm-id 'add-rsm i)))
(def list-rsms!
"List RMSs of the project."
- (fn [chain]
- (lookup :result (chain/eval-in-chain '(list-rsms) (read-ref chain)))))
+ (fn [machine]
+ (machine/query! machine '(list-rsms))))
(def add-issues-rsm!
- (fn [chain id]
- (add-rsm! chain { :id id :type :rad-issues })))
+ (fn [machine id]
+ (add-rsm! machine { :id id :type :rad-issues })))
(def add-diff-rsm!
- (fn [chain id]
- (add-rsm! chain { :id id :type :rad-diff })))
+ (fn [machine id]
+ (add-rsm! machine { :id id :type :rad-diff })))
(def add-repo-rsm!
- (fn [chain id]
- (add-rsm! chain { :id id :type :rad-repo })))
+ (fn [machine id]
+ (add-rsm! machine { :id id :type :rad-repo })))
(def first-rsm-of-type!
- (fn [chain typ]
- (first (filter (fn [x] (eq? (lookup :type x) typ)) (list-rsms! chain)))))
+ (fn [machine typ]
+ (first (filter (fn [x] (eq? (lookup :type x) typ)) (list-rsms! machine)))))
(def get-rsm-of-type!
"Gets the project via get-project-url, and looks in the project for an RSM
of the specified type. The `:id` of the first matching RSM is returned."
(fn [rsm-type]
- (lookup :id (first-rsm-of-type! (make-chain-ref! (get-project-url!)) rsm-type))))
+ (lookup :id (first-rsm-of-type! (get-project-url!) rsm-type))))
-(:test "The monadic project chain works."
+(:test "The monadic project machine works."
[:setup
- (do (chain/install-remote-chain-fake)
+ (do (machine/install-remote-machine-fake)
(install-fake-clock)
(use-fake-keys!)
- (def chain-name "http://bar")
- (def chain (ref (chain/new-chain chain-name)))
- (create-project! chain-name { :name "test" :description "test project"})
- (add-issues-rsm! chain "a")
- (add-diff-rsm! chain "b")
- (add-repo-rsm! chain "c")
- (fetch! chain))
+ (def machine-name "http://bar")
+ (def machine (create-project! {:name "test" :description "test project"}))
+ (add-issues-rsm! machine "a")
+ (add-diff-rsm! machine "b")
+ (add-repo-rsm! machine "c"))
]
- [ (get-meta! chain) ==> { :name "test" :description "test project" }]
- [ (lookup :id (first-rsm-of-type! chain :rad-issues)) ==> "a" ]
- [ (lookup :id (first-rsm-of-type! chain :rad-diff)) ==> "b" ]
- [ (lookup :id (first-rsm-of-type! chain :rad-repo)) ==> "c" ]
+ [ (get-meta! machine) ==> { :name "test" :description "test project" }]
+ [ (lookup :id (first-rsm-of-type! machine :rad-issues)) ==> "a" ]
+ [ (lookup :id (first-rsm-of-type! machine :rad-diff)) ==> "b" ]
+ [ (lookup :id (first-rsm-of-type! machine :rad-repo)) ==> "c" ]
)
diff --git a/rad/prelude.rad b/rad/prelude.rad
index bf2c85c..e24cff2 100644
--- a/rad/prelude.rad
+++ b/rad/prelude.rad
@@ -15,8 +15,8 @@
(file-module! "prelude/ref.rad")
(file-module! "prelude/lens.rad")
(file-module! "prelude/key-management.rad")
-(file-module! "prelude/chain.rad")
-(file-module! "prelude/chain-remote.rad")
+(file-module! "prelude/machine.rad")
+(file-module! "prelude/machine-remote.rad")
(file-module! "prelude/state-machine.rad")
(file-module! "prelude/validation.rad")
(file-module! "prelude/nonces.rad")
@@ -40,5 +40,5 @@
(import prelude/nonces :unqualified)
(import prelude/util :unqualified)
(import prelude/io-utils :unqualified)
-(import prelude/chain :as 'chain)
-(import prelude/chain-remote :unqualified)
+(import prelude/machine :as 'machine)
+(import prelude/machine-remote :unqualified)
diff --git a/rad/prelude/chain.rad b/rad/prelude/chain.rad
deleted file mode 100644
index 35d2bd0..0000000
--- a/rad/prelude/chain.rad
+++ /dev/null
@@ -1,301 +0,0 @@
-{:module 'prelude/chain
- :doc "Functions for simulating remote chains."
- :exports '[new-chain load-chain! eval-in-chain update-chain-ref! eval
- update-chain! send-prelude! send-signed-command! sign-entity!
- send-code! send! receive! make-chain-ref! install-remote-chain-fake]}
-
-(import prelude/io '[read-file-value! read-file-values!] :as 'io)
-(import prelude/ref '[modify-ref] :unqualified)
-(import prelude/lens '[make-lens .. ... view set over @ @def] :unqualified)
-(import prelude/basic :unqualified)
-(import prelude/io :unqualified)
-(import prelude/patterns :unqualified)
-(import prelude/strings :unqualified)
-(import prelude/seq :unqualified)
-(import prelude/key-management :as 'key-mgmt)
-
-(def base-send!
- "See documentation of `send!`"
- (fn [id inputs]
- (match id
- (/prefix "ipfs://" 'rest) (machine/ipfs/update! rest inputs)
- (/prefix "http://" _) (machine/eval-server/update! id inputs)
- _ (throw 'unknown-machine-type (string-append "Cannot handle machine ID " id)))
- ))
-
-(def primitive-stub-ref/send! (ref base-send!))
-(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] ((read-ref primitive-stub-ref/send!) machine-id inputs)))
-
-(def base-receive!
- "See documentation of `receive!`"
- (fn [id index]
- (match id
- (/prefix "ipfs://" 'rest) (machine/ipfs/get-log! rest index)
- (/prefix "http://" _) (machine/eval-server/get-log! id index)
- _ (throw 'unknown-machine-type (string-append "Cannot handle machine ID " id)))
- ))
-
-(def primitive-stub-ref/receive! (ref base-receive!))
-(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] ((read-ref primitive-stub-ref/receive!) machine-id index)))
-
-
-(def env-var
- "A lens for variables in envs."
- (fn [i]
- (make-lens (fn [e] (get-binding i e))
- (fn [v e] (set-binding i v e)))))
-
-(def @env
- "A lens for env part of a machine state."
- (make-lens state->env set-env))
-
-(def @var
- "A lens for variables in machine states."
- (fn [i]
- (.. @env (env-var i))))
-
-(def new-chain
- "Return an empty chain dictionary with the given url."
- (fn [url]
- {:state (pure-state)
- :inputs []
- :url url
- :index :nothing}))
-
-;; eval-in-chain
-
-(def eval-in-chain
- "Evaluates `expr` in the `chain` and returns a dict with the `:result` and the resulting `:chain`."
- (fn [expr chain]
- (def chain-eval (view (.. (@ :state) (@var 'eval)) chain))
- (def x (chain-eval expr (lookup :state chain)))
- (def result (nth 0 x))
- (def new-state (nth 1 x))
- (def new-chain
- (insert
- :state
- new-state
- (over (@ :inputs)
- (fn [inputs] (add-right expr inputs))
- chain)))
- {:chain new-chain
- :result result }))
-
-(:test "eval-in-chain"
- [:setup
- (do (def empty-chain (new-chain (uuid!)))
- (def res1 (eval-in-chain '(def x 3) empty-chain))
- (def res (eval-in-chain '(+ x 2) (lookup :chain res1))))
- ]
- [ (view (@ :result) res) ==> 5 ]
- [ (view (.. (@ :chain) (@ :inputs)) res) ==> [(def x 3) (+ x 2)] ]
-)
-
-;; update-chain!
-
-(def update-chain!
- "Takes a chain, and returns a new chain updated with the new expressions from the remote chain"
- (fn [chain]
- (def index (length (lookup :inputs chain)))
- (def receive-result
- (receive! (lookup :url chain) (lookup :index chain)))
- (def new-index (nth 0 receive-result))
- (def new-inputs (nth 1 receive-result))
- (def upd-ch
- (fn [ch expr]
- (def x (eval-in-chain expr ch))
- (view (@ :chain) x)))
- (def chain (foldl upd-ch chain new-inputs))
- (insert :index [:just new-index] chain)))
-
-(def install-remote-chain-fake
- "Install test doubles for the `send!` and `receive!` primitives that
- use a mutable dictionary to store RSMs. Requires
- `rad/test/stub-primitives` to be loaded"
- (fn []
- (def remote-chains (ref {}))
-
- (write-ref primitive-stub-ref/send!
- (fn [name exprs]
- (modify-ref remote-chains
- (fn [chains] (over (@def name []) (fn [chain-exprs] (<> chain-exprs exprs)) chains)))))
-
- (write-ref primitive-stub-ref/receive!
- (fn [name maybeIndex]
- (def inputs (lookup name (read-ref remote-chains)))
- (def index
- (match maybeIndex
- (/just 'index) (+ index 1)
- :nothing 0))
- [(length inputs) (drop index inputs)]))
- ))
-(:test "update-chain!"
- [:setup
- (do
- (install-remote-chain-fake)
- (def chain-name (uuid!))
- (def chain (new-chain chain-name))
- (send! chain-name ['(+ 3 2)])
- (send! chain-name ['(+ 3 3)])
- (def chain (update-chain! chain)))
- ]
- [ (lookup :inputs chain) ==> [(+ 3 2) (+ 3 3)]]
-)
-
-(def load-chain!
- "Takes a `url`, and fetches the inputs of a remote chain and return a chain dictionary with the chain state."
- (fn [url]
- (update-chain! (new-chain url))))
-
-;; update-chain-ref!
-
-(def update-chain-ref!
- "Update `chain-ref` containing a chain with the new expressions from the remote chain"
- (fn [chain-ref]
- (modify-ref chain-ref update-chain!)
- :ok))
-
-(:test "update-chain-ref!"
- [:setup
- (do
- (install-remote-chain-fake)
- (def chain-name (uuid!))
- (def chain (ref (new-chain chain-name)))
- (send! chain-name ['(+ 3 2)])
- (send! chain-name ['(+ 3 3)])
- (update-chain-ref! chain))
- ]
- [ (lookup :inputs (read-ref chain)) ==> [(+ 3 2) (+ 3 3)]]
-)
-
-;; add-quit
-
-(def add-quit
- "Adds a `:quit` command to `before-quit-eval`, which switches to `after-quit-state` (and to the eval in that state)"
- (fn [after-quit-state before-quit-eval]
- (fn [expr env]
- (if (eq? expr :quit)
- (list :ok after-quit-state)
- (before-quit-eval expr env)))))
-
-;; store-exprs
-
-(def store-exprs
- "Store each new evaluated expression in `_inputs`"
- (fn [evalfn]
- (fn [expr env]
- (def env_ (over (@var '_inputs) (fn [x] (add-right expr x)) env))
- (evalfn expr env_ ))))
-
-;; enter-remote-chain!
-(def enter-remote-chain!
- "Make the eval behave as that of a remote chain. The second param is the env to return to after `:quit`."
- (fn [url env]
- (def chain (load-chain! url))
- (def chain-state (lookup :state chain))
- (def mod-state1 (set (@var '_inputs) [] chain-state))
- (def mod-state2 (set (@var '_cur-chain) (lookup :url chain) mod-state1))
- (def mod-state3 (over (@var 'eval)
- (fn [x] (add-quit env x))
- mod-state2))
- (list :ok mod-state3)))
-
-(def eval__ "The eval in place when `chain.rad` is loaded." eval)
-
-(def eval
- "An eval in which one can use `(:enter-chain url)` to make the
- eval behave as that of a remote chain, and `:send` to send all
- enqueued expressions."
- (fn [expr env]
- (if (list? expr)
- (cond
- (eq? (first expr) :enter-chain)
- (enter-remote-chain! (first (eval__ (nth 1 expr) env)) env)
- :else (eval__ expr env))
- (eval__ expr env))))
-
-
-(def pure-prelude-files
- "List of files which together define the pure prelude."
- (map find-module-file!
- ["prelude/basic.rad"
- "prelude/patterns.rad"
- "prelude/strings.rad"
- "prelude/dict.rad"
- "prelude/bool.rad"
- "prelude/maybe.rad"
- "prelude/list.rad"
- "prelude/set.rad"
- "prelude/ref.rad"
- "prelude/lens.rad"
- "prelude/state-machine.rad"
- "prelude/validation.rad"
- "prelude/nonces.rad"
- "prelude/chain-remote.rad"
- "prelude/util.rad"]))
-
-(def file-to-module
- "Make the text of a file into code for a module."
- (fn [file]
- (cons 'module (vec-to-list (io/read-file-values! file)))))
-
-(def pure-prelude-code
- "Code for the pure prelude. Useful as the first inputs to a new chain."
- (<> [(file-to-module (find-module-file! "prelude/test-eval.rad"))
- '(import prelude/test-eval '[eval tests] :unqualified)]
- (map file-to-module pure-prelude-files)))
-
-(def send-prelude!
- "Send the pure prelude to a chain."
- (fn [chain-id]
- (send! chain-id pure-prelude-code)))
-
-(def send-code!
- "Send code from a file to a remote chain."
- (fn [chain-id filename]
- (send! chain-id (io/read-file-values! filename))))
-
-(def get-keys!
- (fn []
- (match (key-mgmt/read-keys!)
- :nothing (do (put-str! "Missing file: my-keys.rad
-Do `rad-create-keys` to create your own key pair and rerun your command.")
- (exit! 1))
- 'keys keys)))
-
-(def sign-entity!
- "Assumes a key pair is stored at `my-keys.rad`. Using that key pair, will sign a
- dict by adding `:author` and `:signature` fields, so that it is valid
- according to `validator/signed`, while also adding a `:nonce`."
- (fn [e chain-id]
- (def nonce (uuid!))
- (def e_ (<> e {:chain-id chain-id
- :nonce nonce}))
- (def ks (get-keys!))
- (def sig
- (gen-signature! (lookup :private-key ks)
- (show e_)))
- (<> e_
- {:author (lookup :public-key ks)
- :signature sig})))
-
-(def send-signed-command!
- "Send a command signed by the keys in `my-keys.rad`."
- (fn [chain chain-id cmd payload]
- (send! (lookup :url (read-ref chain))
- [(list cmd (sign-entity! payload chain-id))])))
-
-(def make-chain-ref!
- "Makes a new ref containing the loaded chain."
- (fn [chain]
- (ref (load-chain! chain))))
diff --git a/rad/prelude/chain-remote.rad b/rad/prelude/machine-remote.rad
similarity index 98%
rename from rad/prelude/chain-remote.rad
rename to rad/prelude/machine-remote.rad
index 8b71e44..249ac1d 100644
--- a/rad/prelude/chain-remote.rad
+++ b/rad/prelude/machine-remote.rad
@@ -1,4 +1,4 @@
-{:module 'prelude/chain-remote
+{:module 'prelude/machine-remote
:doc "Functions for writing remote RSMs"
:exports '[make-updatable-eval]}
diff --git a/rad/prelude/machine.rad b/rad/prelude/machine.rad
new file mode 100644
index 0000000..3e7f76f
--- /dev/null
+++ b/rad/prelude/machine.rad
@@ -0,0 +1,198 @@
+{:module 'prelude/machine
+ :doc "Functions for simulating remote machines."
+ :exports '[eval updatable-eval eval-fn-app send-prelude! new-machine!
+ send-code! send! query! install-remote-machine-fake send-signed-command!]}
+
+(import prelude/io '[read-file-value! read-file-values!] :as 'io)
+(import prelude/ref '[modify-ref] :unqualified)
+(import prelude/lens '[make-lens .. ... view set over @ @def] :unqualified)
+(import prelude/basic :unqualified)
+(import prelude/io :unqualified)
+(import prelude/patterns :unqualified)
+(import prelude/strings :unqualified)
+(import prelude/seq :unqualified)
+(import prelude/key-management :as 'key-mgmt)
+
+(def primitive-stub-ref/send! (ref daemon/send!))
+(def send!
+ "Update a machine with the vector of `inputs` to evaluate. Returns a vector
+ with the evaluation results."
+ (fn [machine-id inputs] ((read-ref primitive-stub-ref/send!) machine-id inputs)))
+
+(def primitive-stub-ref/query! (ref daemon/query!))
+(def query!
+ "Send an expression to be evaluated on a machine. Does not alter the machine."
+ (fn [machine-id expr] ((read-ref primitive-stub-ref/query!) machine-id expr)))
+
+(def primitive-stub-ref/new-machine! (ref daemon/new-machine!))
+(def new-machine!
+ "Creates a new machine. Returns the machine name."
+ (fn [] ((read-ref primitive-stub-ref/new-machine!))))
+
+(def env-var
+ "A lens for variables in envs."
+ (fn [i]
+ (make-lens (fn [e] (get-binding i e))
+ (fn [v e] (set-binding i v e)))))
+
+(def @env
+ "A lens for env part of a machine state."
+ (make-lens state->env set-env))
+
+(def @var
+ "A lens for variables in machine states."
+ (fn [i]
+ (.. @env (env-var i))))
+
+
+(def new-fake-machine
+ "An empty fake machine, which does not use the daemon"
+ (pure-state))
+
+(def-rec eval-in-fake-machine
+ "Evaluates `expr` in the `machine` and returns a dict with the `:result` and the resulting `:machine`."
+ (fn [machine exprs]
+ (match exprs
+ [] {:machine machine :result []}
+ (/cons 'e 'es) (do
+ (def machine-eval (view (@var 'eval) machine))
+ (def x (machine-eval e machine))
+ (def result (nth 0 x))
+ (def new-state (nth 1 x))
+ (modify-map :result
+ (fn [i] (cons result i))
+ (eval-in-fake-machine new-state es))))))
+
+(:test "eval-in-fake-machine"
+ [:setup
+ (do (def res1 (eval-in-fake-machine new-fake-machine '[(def x 3)]))
+ (def res (eval-in-fake-machine (lookup :machine res1) '[(+ x 2)])))
+ ]
+ [ (view (@ :result) res) ==> [5] ]
+)
+
+(def install-remote-machine-fake
+ "Install test doubles for the `send!`, `query!`, and `new-machine! primitives
+ that use a mutable dictionary to store RSMs. Requires
+ `rad/test/stub-primitives` to be loaded"
+ (fn []
+ (def remote-machines (ref {}))
+
+ (write-ref primitive-stub-ref/send!
+ (fn [name exprs]
+ (def m (lookup name (read-ref remote-machines)))
+ (def res (eval-in-fake-machine m exprs))
+ (modify-ref remote-machines (fn [rms] (insert name (lookup :machine res) rms)))
+ (lookup :result res)))
+
+ (write-ref primitive-stub-ref/query!
+ (fn [name expr]
+ (def machine (lookup name (read-ref remote-machines)))
+ (first (lookup :result (eval-in-fake-machine machine [expr])))))
+
+ (write-ref primitive-stub-ref/new-machine!
+ (fn []
+ (def name (uuid!))
+ (modify-ref remote-machines (fn [rm] (insert name new-fake-machine rm)))
+ name))
+ ))
+
+(:test "install-remote-machine-fake"
+ [:setup
+ (do (install-remote-machine-fake)
+ (def machine (new-machine!))
+ (def send-result (send! machine '[(def foo 5) foo]))
+ )]
+ [ send-result ==> [() 5] ]
+ [ (query! machine '(+ 1 foo)) ==> 6 ]
+)
+
+
+(def eval-fn-app
+ "Given a state, a function, an argument and a callback, returns
+ the result of evaluating the function call on the arg in the given
+ state, while also calling the callback on the result."
+ (fn [state f arg cb]
+ (def exec (base-eval (list f (list 'quote arg)) state))
+ (cb (first exec))
+ exec))
+
+(def updatable-eval
+ "Given an evaluation function `f`, returns a new one which augments `f` with a new
+ command `(update expr)` which evaluates arbitrary expression using `base-eval`."
+ (fn [sub-eval]
+ (fn [expr state]
+ (match expr
+ (/cons 'cmd _) (if (eq? cmd 'update)
+ (base-eval (nth 1 expr) state)
+ (sub-eval expr state))
+ _ (sub-eval expr state)))))
+
+(def pure-prelude-files
+ "List of files which together define the pure prelude."
+ (map find-module-file!
+ ["prelude/basic.rad"
+ "prelude/patterns.rad"
+ "prelude/strings.rad"
+ "prelude/dict.rad"
+ "prelude/bool.rad"
+ "prelude/maybe.rad"
+ "prelude/list.rad"
+ "prelude/set.rad"
+ "prelude/ref.rad"
+ "prelude/lens.rad"
+ "prelude/state-machine.rad"
+ "prelude/validation.rad"
+ "prelude/nonces.rad"
+ "prelude/machine-remote.rad"
+ "prelude/util.rad"]))
+
+(def file-to-module
+ "Make the text of a file into code for a module."
+ (fn [file]
+ (cons 'module (vec-to-list (io/read-file-values! file)))))
+
+(def pure-prelude-code
+ "Code for the pure prelude. Useful as the first inputs to a new machine."
+ (<> [(file-to-module (find-module-file! "prelude/test-eval.rad"))
+ '(import prelude/test-eval '[eval tests] :unqualified)]
+ (map file-to-module pure-prelude-files)))
+
+(def send-prelude!
+ "Send the pure prelude to a machine."
+ (fn [machine-id]
+ (send! machine-id pure-prelude-code)))
+
+(def send-code!
+ "Send code from a file to a remote machine."
+ (fn [machine-id filename]
+ (send! machine-id (io/read-file-values! filename))))
+
+(def get-keys!
+ (fn []
+ (match (key-mgmt/read-keys!)
+ :nothing (do (put-str! "Missing file: my-keys.rad
+Do `rad-create-keys` to create your own key pair and rerun your command.")
+ (exit! 1))
+ 'keys keys)))
+
+(def sign-entity!
+ "Assumes a key pair is stored at `my-keys.rad`. Using that key pair, will sign a
+ dict by adding `:author` and `:signature` fields, so that it is valid
+ according to `validator/signed`, while also adding a `:nonce`."
+ (fn [e machine-id]
+ (def nonce (uuid!))
+ (def e_ (<> e {:machine-id machine-id
+ :nonce nonce}))
+ (def ks (get-keys!))
+ (def sig
+ (gen-signature! (lookup :private-key ks)
+ (show e_)))
+ (<> e_
+ {:author (lookup :public-key ks)
+ :signature sig})))
+
+(def send-signed-command!
+ "Send a command signed by the keys in `my-keys.rad`."
+ (fn [machine machine-id cmd payload]
+ (send! machine [(list cmd (sign-entity! payload machine-id))])))
diff --git a/rad/repl.rad b/rad/repl.rad
index c5861d5..b8b10f0 100644
--- a/rad/repl.rad
+++ b/rad/repl.rad
@@ -1,5 +1,5 @@
(load! (find-module-file! "prelude.rad"))
-(import prelude/chain '[eval] :unqualified)
+(import prelude/machine '[eval] :unqualified)
(def help
"Default help text."
diff --git a/reference-doc.yaml b/reference-doc.yaml
index 99c30f0..acd65b7 100644
--- a/reference-doc.yaml
+++ b/reference-doc.yaml
@@ -6,8 +6,8 @@ intro: >-
primFnsDoc: >-
Primitive functions are those that are built into the compiler. They are
- available on all chains but may be shadowed by later definitions. Those that
- end in a `!` are only available locally, not on 'pure' chains.
+ available on all machines but may be shadowed by later definitions. Those that
+ end in a `!` are only available locally, not on 'pure' machines.
preludeModulesDoc: >-
These are the modules included in the radicle prelude and the functions these
@@ -145,7 +145,7 @@ modules:
- prelude/ref
- prelude/lens
- prelude/key-management
-- prelude/chain
+- prelude/machine
- prelude/state-machine
- prelude/validation
- prelude/util
diff --git a/src/Radicle.hs b/src/Radicle.hs
index 3df9557..213e2bf 100644
--- a/src/Radicle.hs
+++ b/src/Radicle.hs
@@ -84,6 +84,7 @@ module Radicle
, replPrimFns
, addPrimFns
, ReplM
+ , createImpureBindings
-- * CLI
, getHistoryFile
diff --git a/src/Radicle/Daemon/Client.hs b/src/Radicle/Daemon/Client.hs
index 22c4f55..4452453 100644
--- a/src/Radicle/Daemon/Client.hs
+++ b/src/Radicle/Daemon/Client.hs
@@ -19,9 +19,11 @@ import qualified Network.HTTP.Client as HttpClient
import qualified Servant.Client as Servant
import System.Environment
-import Radicle
import Radicle.Daemon.HttpApi
+import Radicle.Internal.Core
+import Radicle.Internal.Identifier
import qualified Radicle.Internal.PrimFns as PrimFns
+import Radicle.Internal.Type
-- | Constraints for all client functions
diff --git a/src/Radicle/Daemon/HttpApi.hs b/src/Radicle/Daemon/HttpApi.hs
index e2f140c..ee5f8be 100644
--- a/src/Radicle/Daemon/HttpApi.hs
+++ b/src/Radicle/Daemon/HttpApi.hs
@@ -30,7 +30,7 @@ import Servant.API
import Servant.Swagger
import qualified Paths_radicle as Radicle
-import Radicle
+import Radicle.Internal.Core
instance FromHttpApiData MachineId where
parseUrlPiece = Right . MachineId . toS
diff --git a/src/Radicle/Daemon/Ipfs.hs b/src/Radicle/Daemon/Ipfs.hs
index 01ea062..367f9eb 100644
--- a/src/Radicle/Daemon/Ipfs.hs
+++ b/src/Radicle/Daemon/Ipfs.hs
@@ -30,8 +30,10 @@ import qualified Data.Sequence as Seq
import qualified Data.Unique as Unique
import qualified Network.HTTP.Client as Http
-import Radicle
+import Radicle.Internal.Core
import qualified Radicle.Internal.MachineBackend.Ipfs as Ipfs
+import Radicle.Internal.Parse
+import Radicle.Internal.Pretty
import qualified Radicle.Internal.UUID as UUID
import qualified Radicle.Ipfs as Ipfs
diff --git a/src/Radicle/Internal.hs b/src/Radicle/Internal.hs
index b2ff528..be28039 100644
--- a/src/Radicle/Internal.hs
+++ b/src/Radicle/Internal.hs
@@ -1,7 +1,10 @@
module Radicle.Internal
( module X
+ , createImpureBindings
) where
+import Protolude
+import Radicle.Daemon.Client (createDaemonClientPrimFns)
import Radicle.Internal.Annotation as X
import Radicle.Internal.CLI as X
import Radicle.Internal.Core as X
@@ -13,3 +16,10 @@ import Radicle.Internal.Parse as X
import Radicle.Internal.Pretty as X
import Radicle.Internal.PrimFns as X
import Radicle.Internal.Type as X
+
+-- | Create all impure bindings. This is in IO so as to create a
+-- manager for the HTTP requests to the daemon.
+createImpureBindings :: (MonadIO m, ReplM m) => [Text] -> IO (Bindings (PrimFns m))
+createImpureBindings scriptArgs' = do
+ daemonClientPrimFns <- createDaemonClientPrimFns
+ pure $ addPrimFns (replPrimFns scriptArgs' <> daemonClientPrimFns) pureEnv
diff --git a/src/Radicle/Internal/MachineBackend/EvalServer.hs b/src/Radicle/Internal/MachineBackend/EvalServer.hs
deleted file mode 100644
index 6797837..0000000
--- a/src/Radicle/Internal/MachineBackend/EvalServer.hs
+++ /dev/null
@@ -1,138 +0,0 @@
-{-# OPTIONS_GHC -fno-warn-orphans #-}
--- | Defines a machine backend that uses an HTTP controlled server that
--- also evaluates submitted expressions.
-module Radicle.Internal.MachineBackend.EvalServer
- ( ChainSubmitEndpoint
- , chainSubmitEndpoint
- , ChainSinceEndpoint
- , chainSinceEndpoint
- , Values(..)
-
- , createEvalServerBackendPrimFns
- ) where
-
-import Protolude hiding (TypeError, toList)
-
-import qualified Data.ByteString.Lazy as LBS
-import qualified Data.Text as T
-import GHC.Exts (IsList(..))
-import qualified Network.HTTP.Client as HttpClient
-import Servant.API
-import Servant.Client
-
-import Radicle
-import Radicle.Internal.Annotation (thisPos)
-import Radicle.Internal.MachineBackend.Interface
-
-
---
--- * API
---
-
-instance MimeRender PlainText Value where
- mimeRender _ x = LBS.fromStrict $ encodeUtf8 $ renderCompactPretty x
-
-instance MimeUnrender PlainText Value where
- mimeUnrender _ x =
- let src = decodeUtf8 $ LBS.toStrict x
- in first T.unpack $ parse "[request body]" src
-
-newtype Values = Values { getValues :: [Value] }
- deriving (Eq, Show, Ord)
-
-instance IsList Values where
- type Item Values = Value
- fromList = Values
- toList = getValues
-
-instance MimeRender PlainText Values where
- mimeRender _ (Values xs)
- = LBS.intercalate "\n" $ mimeRender p <$> xs
- where
- p = Proxy :: Proxy PlainText
-
-instance MimeUnrender PlainText Values where
- mimeUnrender _ x =
- let src = decodeUtf8 $ LBS.toStrict x
- in case parseValues "[request body]" src of
- Right vs -> Right $ Values vs
- Left err -> Left . toS $ renderPrettyDef err'
- where err' :: LangError Value
- err' = LangError [thisPos] (ParseError err)
-
--- | Endpoint to submit an expression to a chain. Responds with the
--- index of the submitted expression. The index is an integer encoded
--- as a Radicle value.
-type ChainSubmitEndpoint = "submit" :> ReqBody '[PlainText] Values :> Post '[PlainText] Value
-
-chainSubmitEndpoint :: Proxy ChainSubmitEndpoint
-chainSubmitEndpoint = Proxy
-
--- | Endpoint to obtain all entries in a chain starting from and
--- including the given index. Responds with a Radicle vector of the
--- expressions.
-type ChainSinceEndpoint = "since" :> Capture "index" Int :> Get '[PlainText] Values
-
-chainSinceEndpoint :: Proxy ChainSinceEndpoint
-chainSinceEndpoint = Proxy
-
-
---
--- * Client
---
-
--- | 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
- pure $ httpMachineBackendPrimFns httpMgr
-
-httpMachineBackendPrimFns :: MonadIO m => HttpClient.Manager -> PrimFns m
-httpMachineBackendPrimFns mgr =
- buildMachineBackendPrimFns MachineBackend
- { 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 =
- \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
- -- response will include the input with index `n`, the
- -- storage interface however says that it should not.
- -- To account for this we increase the index we
- -- receive by one.
- -- 2. The HTTP API always exepcts an index, so we use
- -- @-1@ if @Nothing@ is provided.
- -- 3. Th HTTP API does not return the index of the last
- -- input in the response. We calculate it locally
- let index = fromMaybe (-1) maybeIndex
- res <- liftIO $ runClientM' url mgr (since (index + 1))
- pure $ case res of
- Left err -> Left $ formatServantError err
- Right values -> Right (length values + index, values)
- }
- where
- formatServantError = \case
- FailureResponse Response{..} ->
- "status code: " <> show (fromEnum responseStatusCode) <> "\n" <> toS responseBody
- ConnectionError err -> err
- se -> show se
-
-submit :: [Value] -> ClientM Value
-submit = client chainSubmitEndpoint . Values
-
-since :: Int -> ClientM [Value]
-since idx = getValues <$> client chainSinceEndpoint idx
-
-runClientM' :: (MonadIO m) => Text -> HttpClient.Manager -> ClientM a -> m (Either ServantError a)
-runClientM' baseUrl manager endpoint = liftIO $ do
- url <- parseBaseUrl $ T.unpack baseUrl
- liftIO $ runClientM endpoint $ mkClientEnv manager url
diff --git a/src/Radicle/Internal/MachineBackend/Ipfs.hs b/src/Radicle/Internal/MachineBackend/Ipfs.hs
index 3021ba5..4f6cf14 100644
--- a/src/Radicle/Internal/MachineBackend/Ipfs.hs
+++ b/src/Radicle/Internal/MachineBackend/Ipfs.hs
@@ -26,10 +26,13 @@ import qualified Data.Aeson as Aeson
import Data.IPLD.CID
import qualified Data.Text as T
-import Radicle
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
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index d88159b..92c2f65 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -81,7 +81,8 @@ runPureCode code =
runCodeWithWorld :: WorldState -> Text -> IO (Either (LangError Value) Value, [Text])
runCodeWithWorld ws code = do
let prog = interpretMany "[test]" code
- (result, ws') <- runStateT (fst <$> runLang testBindings prog) ws
+ bindings <- testBindings
+ (result, ws') <- runStateT (fst <$> runLang bindings prog) ws
pure $ (result, worldStateStdout ws')
-- | Loads the content for all radicle sources in the "./rad" folder.
@@ -97,8 +98,8 @@ sourceFiles = do
pure $ Map.fromList filesWithContent
-- | Bindings with REPL and client stuff mocked.
-testBindings :: Bindings (PrimFns (StateT WorldState IO))
-testBindings = addPrimFns memoryMachineBackendPrimFns (replBindings [])
+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'.
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 78d10e2..121fe27 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -836,19 +836,6 @@ test_cbor =
<> "\nGot\n\t" <> show got
counterexample info $ got == expected
-test_macros :: [TestTree]
-test_macros =
- [ testCase ":enter-chain keeps old bindings" $ do
- let input = [ "(def x 0)"
- , "(:enter-chain \"http://blah\")"
- , "(def x 1)"
- , ":quit"
- , "x"
- ]
- assertReplInteraction input ["0"]
- ]
-
-
-- * Utils
kw :: Text -> Value