From f6c5624afe76bda1c4932ba4d5e39c4e68d28b10 Mon Sep 17 00:00:00 2001 From: mikera Date: Thu, 21 Dec 2023 15:05:30 +0000 Subject: [PATCH] Tests for child node creation --- convex-core/src/main/cvx/convex/registry.cvx | 17 +++++++++++++++-- .../src/main/cvx/convex/trust/monitors.cvx | 3 +++ .../src/test/java/convex/lib/CNSTest.java | 10 +++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/convex-core/src/main/cvx/convex/registry.cvx b/convex-core/src/main/cvx/convex/registry.cvx index f07b8c9fe..2897c1fd0 100644 --- a/convex-core/src/main/cvx/convex/registry.cvx +++ b/convex-core/src/main/cvx/convex/registry.cvx @@ -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))) )) ) ) @@ -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*." diff --git a/convex-core/src/main/cvx/convex/trust/monitors.cvx b/convex-core/src/main/cvx/convex/trust/monitors.cvx index 4f29057e7..cdce7955d 100644 --- a/convex-core/src/main/cvx/convex/trust/monitors.cvx +++ b/convex-core/src/main/cvx/convex/trust/monitors.cvx @@ -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)]]]) diff --git a/convex-core/src/test/java/convex/lib/CNSTest.java b/convex-core/src/test/java/convex/lib/CNSTest.java index ca0343ea2..b3062b927 100644 --- a/convex-core/src/test/java/convex/lib/CNSTest.java +++ b/convex-core/src/test/java/convex/lib/CNSTest.java @@ -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\"]])"); @@ -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+")")); } }