From b4263b0a7daf973bbd97204a016b6fd8b42fca58 Mon Sep 17 00:00:00 2001 From: mikera Date: Wed, 27 Nov 2024 10:31:23 +0000 Subject: [PATCH] Refactor utility functions from convex.core.data.Format => Cells --- .../convex/core/cvm/transactions/Invoke.java | 2 +- .../src/main/java/convex/core/data/Cells.java | 24 +++++++++++++++++++ .../main/java/convex/core/data/Format.java | 24 ------------------- .../main/java/convex/core/data/RefDirect.java | 2 +- .../main/java/convex/core/data/RefSoft.java | 2 +- .../test/java/convex/comms/GenTestFormat.java | 2 +- .../java/convex/core/data/EncodingTest.java | 10 ++++---- .../java/convex/core/data/FuzzTestFormat.java | 2 +- .../convex/core/data/GenTestAnyValue.java | 6 ++--- .../java/convex/core/data/ObjectsTest.java | 4 ++-- 10 files changed, 39 insertions(+), 39 deletions(-) diff --git a/convex-core/src/main/java/convex/core/cvm/transactions/Invoke.java b/convex-core/src/main/java/convex/core/cvm/transactions/Invoke.java index c31f6ceba..286ecdbbe 100644 --- a/convex-core/src/main/java/convex/core/cvm/transactions/Invoke.java +++ b/convex-core/src/main/java/convex/core/cvm/transactions/Invoke.java @@ -130,7 +130,7 @@ public void validateCell() throws InvalidDataException { // OK? ((AOp) command).validateCell(); } else { - if (!Format.isCanonical(command)) throw new InvalidDataException("Non-canonical object as command?", this); + if (!Cells.isCanonical(command)) throw new InvalidDataException("Non-canonical object as command?", this); } } diff --git a/convex-core/src/main/java/convex/core/data/Cells.java b/convex-core/src/main/java/convex/core/data/Cells.java index 310e7c43d..110ec026d 100644 --- a/convex-core/src/main/java/convex/core/data/Cells.java +++ b/convex-core/src/main/java/convex/core/data/Cells.java @@ -321,4 +321,28 @@ public static Hash cachedHash(ACell k) { return k.cachedHash(); } + /** + * Returns true if the object is a canonical data object. Canonical data objects + * can be used as first class decentralised data objects. + * + * @param o Value to test + * @return true if object is canonical, false otherwise. + */ + public static boolean isCanonical(ACell o) { + if (o==null) return true; + return o.isCanonical(); + } + + /** + * Determines if an object should be embedded directly in the encoding rather + * than referenced with a Ref / hash. Defined to be true for most small objects. + * + * @param cell Value to test + * @return true if object is embedded, false otherwise + */ + public static boolean isEmbedded(ACell cell) { + if (cell == null) return true; + return cell.isEmbedded(); + } + } diff --git a/convex-core/src/main/java/convex/core/data/Format.java b/convex-core/src/main/java/convex/core/data/Format.java index 12fcb3b6d..7c763e984 100644 --- a/convex-core/src/main/java/convex/core/data/Format.java +++ b/convex-core/src/main/java/convex/core/data/Format.java @@ -714,30 +714,6 @@ private static T readTransaction(byte tag, Blob b, int pos) th return (T) dr; } - /** - * Returns true if the object is a canonical data object. Canonical data objects - * can be used as first class decentralised data objects. - * - * @param o Value to test - * @return true if object is canonical, false otherwise. - */ - public static boolean isCanonical(ACell o) { - if (o==null) return true; - return o.isCanonical(); - } - - /** - * Determines if an object should be embedded directly in the encoding rather - * than referenced with a Ref / hash. Defined to be true for most small objects. - * - * @param cell Value to test - * @return true if object is embedded, false otherwise - */ - public static boolean isEmbedded(ACell cell) { - if (cell == null) return true; - return cell.isEmbedded(); - } - /** * Gets the encoded Blob for an object in canonical message format * diff --git a/convex-core/src/main/java/convex/core/data/RefDirect.java b/convex-core/src/main/java/convex/core/data/RefDirect.java index e42461ded..0f29216f0 100644 --- a/convex-core/src/main/java/convex/core/data/RefDirect.java +++ b/convex-core/src/main/java/convex/core/data/RefDirect.java @@ -99,7 +99,7 @@ public boolean equals(Ref a) { @Override public void validate() throws InvalidDataException { super.validate(); - if (isEmbedded() != Format.isEmbedded(value)) throw new InvalidDataException("Embedded flag is wrong!", this); + if (isEmbedded() != Cells.isEmbedded(value)) throw new InvalidDataException("Embedded flag is wrong!", this); if (value == null) { if (this != Ref.NULL_VALUE) throw new InvalidDataException("Null Ref not singleton!", this); } diff --git a/convex-core/src/main/java/convex/core/data/RefSoft.java b/convex-core/src/main/java/convex/core/data/RefSoft.java index c966f4d22..618305ed1 100644 --- a/convex-core/src/main/java/convex/core/data/RefSoft.java +++ b/convex-core/src/main/java/convex/core/data/RefSoft.java @@ -166,7 +166,7 @@ public void validate() throws InvalidDataException { if (hash == null) throw new InvalidDataException("Hash should never be null in soft ref", this); ACell val = softRef.get(); boolean embedded=isEmbedded(); - if (embedded!=Format.isEmbedded(val)) { + if (embedded!=Cells.isEmbedded(val)) { throw new InvalidDataException("Embedded flag ["+embedded+"] inconsistent with value", this); } } diff --git a/convex-core/src/test/java/convex/comms/GenTestFormat.java b/convex-core/src/test/java/convex/comms/GenTestFormat.java index fe6a8c45a..0f983e4d1 100644 --- a/convex-core/src/test/java/convex/comms/GenTestFormat.java +++ b/convex-core/src/test/java/convex/comms/GenTestFormat.java @@ -39,7 +39,7 @@ public void messageRoundTrip(String str) throws BadFormatException { @Property public void primitiveRoundTrip(@From(PrimitiveGen.class) ACell prim) throws BadFormatException, IOException { Blob b = Format.encodedBlob(prim); - if (!Format.isEmbedded(prim)) { + if (!Cells.isEmbedded(prim)) { // persist in case large Cells.persist(prim); } diff --git a/convex-core/src/test/java/convex/core/data/EncodingTest.java b/convex-core/src/test/java/convex/core/data/EncodingTest.java index 34c5424a4..5de021911 100644 --- a/convex-core/src/test/java/convex/core/data/EncodingTest.java +++ b/convex-core/src/test/java/convex/core/data/EncodingTest.java @@ -119,7 +119,7 @@ private void doVLQLongTest(long x) throws BadFormatException { Blob b=Format.encodedBlob(k); ACell o=Format.read(b); assertEquals(k,o); - assertTrue(Format.isEmbedded(k)); + assertTrue(Cells.isEmbedded(k)); Ref r=Ref.get(o); assertTrue(r.isDirect()); } @@ -178,10 +178,10 @@ private void doVLQLongTest(long x) throws BadFormatException { } @Test public void testCanonical() { - assertTrue(Format.isCanonical(Vectors.empty())); - assertTrue(Format.isCanonical(null)); - assertTrue(Format.isCanonical(RT.cvm(1))); - assertTrue(Format.isCanonical(Blob.create(new byte[1000]))); // should be OK + assertTrue(Cells.isCanonical(Vectors.empty())); + assertTrue(Cells.isCanonical(null)); + assertTrue(Cells.isCanonical(RT.cvm(1))); + assertTrue(Cells.isCanonical(Blob.create(new byte[1000]))); // should be OK assertFalse(Blob.create(new byte[10000]).isCanonical()); // too big to be canonical } diff --git a/convex-core/src/test/java/convex/core/data/FuzzTestFormat.java b/convex-core/src/test/java/convex/core/data/FuzzTestFormat.java index 906d4b9ef..97d4e4c5f 100644 --- a/convex-core/src/test/java/convex/core/data/FuzzTestFormat.java +++ b/convex-core/src/test/java/convex/core/data/FuzzTestFormat.java @@ -83,7 +83,7 @@ private static void doFuzzTest(Blob b) throws BadFormatException { // if we manage to read the object and it is not a Ref, it must be in canonical // format! - assertTrue(Format.isCanonical(v),()->"Not canonical: "+Utils.getClassName(v)); + assertTrue(Cells.isCanonical(v),()->"Not canonical: "+Utils.getClassName(v)); Blob b2 = Format.encodedBlob(v); assertEquals(v, Format.read(b2), diff --git a/convex-core/src/test/java/convex/core/data/GenTestAnyValue.java b/convex-core/src/test/java/convex/core/data/GenTestAnyValue.java index 6505742bb..758529796 100644 --- a/convex-core/src/test/java/convex/core/data/GenTestAnyValue.java +++ b/convex-core/src/test/java/convex/core/data/GenTestAnyValue.java @@ -74,7 +74,7 @@ public void testFuzzing(@From(ValueGen.class) ACell o) throws InvalidDataExcepti @Property public void validEmbedded(@From(ValueGen.class) ACell o) throws InvalidDataException, BadFormatException, IOException { - if (Format.isEmbedded(o)) { + if (Cells.isEmbedded(o)) { Cells.persist(o); // NOTE: may have child refs to persist Blob data=Format.encodedBlob(o); @@ -84,7 +84,7 @@ public void validEmbedded(@From(ValueGen.class) ACell o) throws InvalidDataExcep assertEquals(o,o2); AArrayBlob data2=Format.encodedBlob(o2); assertEquals(data,data2); - assertTrue(Format.isEmbedded(o2)); + assertTrue(Cells.isEmbedded(o2)); // when we persist a ref to an embedded object, should be the object itself Ref ref=Ref.get(o); @@ -113,7 +113,7 @@ public void dataRoundTrip(@From(ValueGen.class) ACell o) throws BadFormatExcepti // re-read data, should be canonical ACell o2=Format.read(data); - assertTrue(Format.isCanonical(o2)); + assertTrue(Cells.isCanonical(o2)); // equality checks assertEquals(o,o2); diff --git a/convex-core/src/test/java/convex/core/data/ObjectsTest.java b/convex-core/src/test/java/convex/core/data/ObjectsTest.java index 9622e5d04..29ccd8250 100644 --- a/convex-core/src/test/java/convex/core/data/ObjectsTest.java +++ b/convex-core/src/test/java/convex/core/data/ObjectsTest.java @@ -247,7 +247,7 @@ private static void doAnyEncodingTests(ACell a) { // If length exceeds MAX_EMBEDDED_LENGTH, cannot be an embedded value if (encoding.count > Format.MAX_EMBEDDED_LENGTH) { - assertFalse(Format.isEmbedded(a),()->"Should not be embedded: "+Utils.getClassName(a)+ " = "+Utils.toString(a)); + assertFalse(Cells.isEmbedded(a),()->"Should not be embedded: "+Utils.getClassName(a)+ " = "+Utils.toString(a)); } try { @@ -315,7 +315,7 @@ private static void doCanonicalTests(ACell a) { long cms=childRef.getMemorySize(); childMem+=cms; } - boolean embedded=Format.isEmbedded(a); + boolean embedded=Cells.isEmbedded(a); if (embedded) { assertEquals(memorySize,childMem); } else {