Skip to content

Commit 9219595

Browse files
committed
Tighten assumptions around CAD3 vs. CVM values
1 parent d82c72c commit 9219595

File tree

10 files changed

+32
-44
lines changed

10 files changed

+32
-44
lines changed

convex-core/src/main/java/convex/core/cpos/Block.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,6 @@ public boolean equals(Block a) {
214214
if (!(Cells.equals(transactions, a.transactions))) return false;
215215
return true;
216216
}
217-
218-
@Override
219-
public boolean isCVMValue() {
220-
return false;
221-
}
222217

223218
@Override
224219
public int getRefCount() {

convex-core/src/main/java/convex/core/cpos/Order.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,6 @@ public static Order read(Blob b, int pos) throws BadFormatException {
159159
result.attachEncoding(b.slice(pos, epos));
160160
return result;
161161
}
162-
163-
164-
@Override public final boolean isCVMValue() {
165-
// Orders exist outside CVM only
166-
return false;
167-
}
168162

169163
/**
170164
* Checks if another Order is consistent with this Order.

convex-core/src/main/java/convex/core/cvm/Receipt.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ public byte getTag() {
8989
int tag=Tag.RECEIPT+(isError?Tag.RECEIPT_ERROR_MASK:0)+((log==null)?0:Tag.RECEIPT_LOG_MASK);
9090
return (byte)tag;
9191
}
92-
93-
@Override public final boolean isCVMValue() {
94-
// Receipts exist outside CVM only
95-
return false;
96-
}
9792

9893
@Override
9994
public RecordFormat getFormat() {

convex-core/src/main/java/convex/core/data/ACell.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import convex.core.util.Utils;
99

1010
/**
11-
* Abstract base class for Cells.
11+
* Abstract base class for Cells in CAD3 data format.
1212
*
13-
* Cells may contain Refs to other Cells, which can be tested with getRefCount()
13+
* Cells may contain Refs to 0-63 child Cells, which can be tested with getRefCount()
1414
*
1515
* All data objects intended for on-chain usage / serialisation should extend this.
1616
*
@@ -348,25 +348,24 @@ public boolean isEmbedded() {
348348
public abstract boolean isCanonical();
349349

350350
/**
351-
* Converts this Cell to a canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise.
351+
* Converts this Cell to a canonical version, if not already canonical.
352352
*
353-
* Callers should usually use getCanonical(), which caches canonical instances once created
353+
* Must return this Cell if already canonical, may be O(n) in size of value otherwise.
354+
*
355+
* Users should usually use getCanonical(), which caches canonical instances once created
354356
*
355357
* @return Canonical version of Cell
356358
*/
357359
protected abstract ACell toCanonical();
358360

359361
/**
360-
* Returns true if this cell is a first class CVM Value used in the CVM state
361-
*
362-
* Sub-structural cells that are not themselves first class values
363-
* should return false
362+
* Returns true if this cell is a first class CVM Value.
364363
*
365-
* Records and types that are not permissible on the CVM should return false.
364+
* CAD3 Records and types that are not recognised by the CVM must return false.
366365
*
367-
* Pretty much everything else should return true.
366+
* Everything the CVM can recognise must return true.
368367
*
369-
* Note: CVM values might not be in a canonical format, e.g. temporary data structures
368+
* Note: CVM values might still not be in a canonical format, e.g. temporary data structures
370369
*
371370
* @return true if the object is a CVM Value, false otherwise
372371
*/

convex-core/src/main/java/convex/core/data/ARecord.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import convex.core.lang.RecordFormat;
1414

1515
/**
16-
* Base class for Record data types.
16+
* Base class for CVM Record data types.
1717
*
1818
* Records are Map-like data structures with fixed sets of Keyword keys, and optional custom behaviour.
1919
*
@@ -50,7 +50,7 @@ protected ARecord toCanonical() {
5050
return this;
5151
}
5252

53-
@Override public boolean isCVMValue() {
53+
@Override final public boolean isCVMValue() {
5454
return true;
5555
}
5656

convex-core/src/main/java/convex/core/data/MapTree.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,13 @@ public AHashMap<K, V> mapEntries(Function<MapEntry<K, V>, MapEntry<K, V>> func)
838838
public void validate() throws InvalidDataException {
839839
super.validate();
840840

841-
// Perform child validation
842-
validateWithPrefix(getFirstHash(),shift);
841+
try {
842+
Hash firstHash=getFirstHash();
843+
// Perform child validation
844+
validateWithPrefix(firstHash,shift);
845+
} catch (ClassCastException e) {
846+
throw new InvalidDataException("Can't get first hash of map: "+e.getMessage(),e);
847+
}
843848
}
844849

845850
@Override

convex-core/src/main/java/convex/core/data/SignedData.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,6 @@ public boolean isCanonical() {
323323
return true;
324324
}
325325

326-
@Override public final boolean isCVMValue() {
327-
return false;
328-
}
329-
330326
@Override
331327
public final int getRefCount() {
332328
// Value Ref only

convex-core/src/main/java/convex/core/data/prim/ByteFlagExtended.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public int encode(byte[] bs, int pos) {
5252
return pos;
5353
}
5454

55-
55+
@Override public boolean isCVMValue() {
56+
return (tag==Tag.TRUE)||(tag==Tag.FALSE);
57+
}
5658

5759
@Override
5860
public long longValue() {

convex-core/src/main/java/convex/core/transactions/ATransaction.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ public final long getSequence() {
8888
return sequence;
8989
}
9090

91-
@Override public final boolean isCVMValue() {
92-
// Transactions exist outside CVM only
93-
return false;
94-
}
95-
9691
@Override
9792
public AType getType() {
9893
return Transaction.INSTANCE;

convex-core/src/test/java/convex/core/data/AdversarialDataTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
import convex.core.Result;
1515
import convex.core.cvm.AccountStatus;
16-
import convex.core.cvm.Receipt;
16+
import convex.core.data.impl.DummyCell;
17+
import convex.core.data.prim.ByteFlagExtended;
1718
import convex.core.data.prim.CVMDouble;
1819
import convex.core.data.prim.CVMLong;
1920
import convex.core.exceptions.BadFormatException;
@@ -30,17 +31,23 @@
3031
*/
3132
public class AdversarialDataTest {
3233

33-
// A value that is valid, but not a first class CVM value
34-
public static final ACell NON_CVM=Receipt.create(null);
34+
// A value that is valid CAD3, but not a first class CVM value
35+
public static final ACell NON_CVM=ByteFlagExtended.create(15);
3536

3637
// A value that is non-canonical but otherwise valid CVM value
3738
public static final Blob NON_CANONICAL=Blob.createRandom(new Random(), Blob.CHUNK_LENGTH+1);
3839

3940
// A value that is invalid
4041
public static final SetLeaf<CVMLong> NON_VALID=SetLeaf.unsafeCreate(new CVMLong[0]);
4142

43+
// A value that is illegal in any encoding
44+
@SuppressWarnings("exports")
45+
public static final DummyCell NON_LEGAL=new DummyCell();
46+
47+
4248
@Test public void testAssumptions() {
4349
assertFalse(NON_CVM.isCVMValue());
50+
assertFalse(NON_LEGAL.isCVMValue());
4451
assertFalse(NON_CANONICAL.isCanonical());
4552
assertThrows(InvalidDataException.class, ()->NON_VALID.validate());
4653
}

0 commit comments

Comments
 (0)