Skip to content

Commit

Permalink
Tighten assumptions around CAD3 vs. CVM values
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 12, 2024
1 parent d82c72c commit 9219595
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 44 deletions.
5 changes: 0 additions & 5 deletions convex-core/src/main/java/convex/core/cpos/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,6 @@ public boolean equals(Block a) {
if (!(Cells.equals(transactions, a.transactions))) return false;
return true;
}

@Override
public boolean isCVMValue() {
return false;
}

@Override
public int getRefCount() {
Expand Down
6 changes: 0 additions & 6 deletions convex-core/src/main/java/convex/core/cpos/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,6 @@ public static Order read(Blob b, int pos) throws BadFormatException {
result.attachEncoding(b.slice(pos, epos));
return result;
}


@Override public final boolean isCVMValue() {
// Orders exist outside CVM only
return false;
}

/**
* Checks if another Order is consistent with this Order.
Expand Down
5 changes: 0 additions & 5 deletions convex-core/src/main/java/convex/core/cvm/Receipt.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ public byte getTag() {
int tag=Tag.RECEIPT+(isError?Tag.RECEIPT_ERROR_MASK:0)+((log==null)?0:Tag.RECEIPT_LOG_MASK);
return (byte)tag;
}

@Override public final boolean isCVMValue() {
// Receipts exist outside CVM only
return false;
}

@Override
public RecordFormat getFormat() {
Expand Down
21 changes: 10 additions & 11 deletions convex-core/src/main/java/convex/core/data/ACell.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import convex.core.util.Utils;

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

/**
* Converts this Cell to a canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise.
* Converts this Cell to a canonical version, if not already canonical.
*
* Callers should usually use getCanonical(), which caches canonical instances once created
* Must return this Cell if already canonical, may be O(n) in size of value otherwise.
*
* Users should usually use getCanonical(), which caches canonical instances once created
*
* @return Canonical version of Cell
*/
protected abstract ACell toCanonical();

/**
* Returns true if this cell is a first class CVM Value used in the CVM state
*
* Sub-structural cells that are not themselves first class values
* should return false
* Returns true if this cell is a first class CVM Value.
*
* Records and types that are not permissible on the CVM should return false.
* CAD3 Records and types that are not recognised by the CVM must return false.
*
* Pretty much everything else should return true.
* Everything the CVM can recognise must return true.
*
* Note: CVM values might not be in a canonical format, e.g. temporary data structures
* Note: CVM values might still not be in a canonical format, e.g. temporary data structures
*
* @return true if the object is a CVM Value, false otherwise
*/
Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/data/ARecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import convex.core.lang.RecordFormat;

/**
* Base class for Record data types.
* Base class for CVM Record data types.
*
* Records are Map-like data structures with fixed sets of Keyword keys, and optional custom behaviour.
*
Expand Down Expand Up @@ -50,7 +50,7 @@ protected ARecord toCanonical() {
return this;
}

@Override public boolean isCVMValue() {
@Override final public boolean isCVMValue() {
return true;
}

Expand Down
9 changes: 7 additions & 2 deletions convex-core/src/main/java/convex/core/data/MapTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,13 @@ public AHashMap<K, V> mapEntries(Function<MapEntry<K, V>, MapEntry<K, V>> func)
public void validate() throws InvalidDataException {
super.validate();

// Perform child validation
validateWithPrefix(getFirstHash(),shift);
try {
Hash firstHash=getFirstHash();
// Perform child validation
validateWithPrefix(firstHash,shift);
} catch (ClassCastException e) {
throw new InvalidDataException("Can't get first hash of map: "+e.getMessage(),e);
}
}

@Override
Expand Down
4 changes: 0 additions & 4 deletions convex-core/src/main/java/convex/core/data/SignedData.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ public boolean isCanonical() {
return true;
}

@Override public final boolean isCVMValue() {
return false;
}

@Override
public final int getRefCount() {
// Value Ref only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public int encode(byte[] bs, int pos) {
return pos;
}


@Override public boolean isCVMValue() {
return (tag==Tag.TRUE)||(tag==Tag.FALSE);
}

@Override
public long longValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ public final long getSequence() {
return sequence;
}

@Override public final boolean isCVMValue() {
// Transactions exist outside CVM only
return false;
}

@Override
public AType getType() {
return Transaction.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

import convex.core.Result;
import convex.core.cvm.AccountStatus;
import convex.core.cvm.Receipt;
import convex.core.data.impl.DummyCell;
import convex.core.data.prim.ByteFlagExtended;
import convex.core.data.prim.CVMDouble;
import convex.core.data.prim.CVMLong;
import convex.core.exceptions.BadFormatException;
Expand All @@ -30,17 +31,23 @@
*/
public class AdversarialDataTest {

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

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

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

// A value that is illegal in any encoding
@SuppressWarnings("exports")
public static final DummyCell NON_LEGAL=new DummyCell();


@Test public void testAssumptions() {
assertFalse(NON_CVM.isCVMValue());
assertFalse(NON_LEGAL.isCVMValue());
assertFalse(NON_CANONICAL.isCanonical());
assertThrows(InvalidDataException.class, ()->NON_VALID.validate());
}
Expand Down

0 comments on commit 9219595

Please sign in to comment.