Skip to content

Commit

Permalink
Tests for child node creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 21, 2023
1 parent 63ba128 commit f6c5624
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
17 changes: 15 additions & 2 deletions convex-core/src/main/cvx/convex/registry.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@
(recur (inc i) (first rec))

;; need to construct a new CNS node
(let []
(let [nref (call ref (cns-create-node pname cont))]
(call ref (cns-write pname nref cont nil) )
(recur (inc i) (call ref (cns-read pname)))))
(recur (inc i) nref)))
))
)
)
Expand Down Expand Up @@ -200,6 +200,19 @@
path
[addr monitor meta])))))

(defn cns-create-node
^{:callable? true
:doc {:description "Creates a child CNS node."
:examples [{:code "(call [cns-node cns-key] (cns-create-node \\\"child-name\\\"))"}]
:signature [{:params [sym]}]}}
[pname owner]
(or (trust/trusted? (get cns-owners *scope*) *caller* :create pname) (fail :TRUST "No permission to create CNS node"))
(let [path (conj *scope* pname)]
(if (get cns-database path) (fail :STATE "CNS node already exists"))
(set-in! cns-owners [path] owner)
(set-in! cns-database [path] {})
[~*address* path]))

(defn cns-read
^{:callable? true
:doc {:description "Reads a child CNS record from this CNS node. Assumes a record key passed in *scope*."
Expand Down
3 changes: 3 additions & 0 deletions convex-core/src/main/cvx/convex/trust/monitors.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
(defn all [arg & more]
[~*address* [:all (apply vector arg more)]])

(defn everyone []
[~*address* [:all []]])

(defn before [end]
[~*address* [:time [*timestamp* (int end)]]])

Expand Down
10 changes: 7 additions & 3 deletions convex-core/src/test/java/convex/lib/CNSTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ public class CNSTest extends ACVMTest {
assertEquals(eval("[#1 #1 nil]"), eval("(*registry*/read 'init)"));
}


@Test public void testCreateFromTop() {
Context ctx=context().forkWithAddress(Init.INIT_ADDRESS);
ctx=(step(ctx,"(*registry*/create 'foo.bar.bax #17)"));
assertNotError(ctx);
}

@Test public void testCreateTopLevel() {
// HERO shouldn't be able to create a top level CNS entry
assertTrustError(step("(*registry*/create 'foo)"));

// INIT should be able to create a top level CNS entry
Context ictx=context().forkWithAddress(Init.INIT_ADDRESS);
ictx=step(ictx,"(import convex.trust :as trust)");
ictx=(step(ictx,"(*registry*/create 'foo #17)"));
assertNotError(ictx);
ictx=step(ictx,"(def ref [*registry* [\"foo\"]])");
Expand All @@ -62,8 +68,6 @@ public class CNSTest extends ACVMTest {
assertTrustError(step(ictx,"(*registry*/create 'foo *address* *address* {})"));
assertTrustError(step(ictx,"(trust/change-control "+ref+" *address*)"));

ictx=ictx.forkWithAddress(Init.INIT_ADDRESS);
ictx=(step(ictx,"(trust/change-control ref "+HERO+")"));
}

}

0 comments on commit f6c5624

Please sign in to comment.