Skip to content

Commit

Permalink
Add CAD3 Extension values 0xE0 - 0xEF
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 12, 2024
1 parent 2a18c06 commit 89490c6
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 121 deletions.
3 changes: 1 addition & 2 deletions convex-core/src/main/java/convex/core/data/ABlobLike.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,5 @@ public Blob toFlatBlob() {
*/
public abstract boolean equalsBytes(ABlob b);

@Override
public abstract int compareTo(ABlobLike<?> b);

}
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/data/ACell.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ protected static boolean genericEquals(ACell a, ACell b) {
}

/**
* Writes this Cell's encoding to a byte array, including a tag byte which will be written first.
* Writes this Cell's CAD3 encoding to a byte array, including the tag byte which will be written first.
*
* Cell must be canonical, or else an error may occur.
*
Expand All @@ -177,7 +177,7 @@ protected static boolean genericEquals(ACell a, ACell b) {
public abstract int encode(byte[] bs, int pos);

/**
* Writes this Cell's encoding to a byte array, excluding the tag byte.
* Writes this Cell's CAD3 encoding to a byte array, excluding the tag byte.
*
* @param bs A byte array to which to write the encoding
* @param pos The offset into the byte array
Expand Down
27 changes: 0 additions & 27 deletions convex-core/src/main/java/convex/core/data/ACode.java

This file was deleted.

129 changes: 129 additions & 0 deletions convex-core/src/main/java/convex/core/data/AExtensionValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package convex.core.data;

import convex.core.data.impl.LongBlob;
import convex.core.data.prim.CVMLong;
import convex.core.util.ErrorMessages;
import convex.core.util.Utils;

/**
* Abstract base class for 8-byte bloblike extension Values such as Address and CAD Extension Values
*/
public abstract class AExtensionValue extends ABlobLike<CVMLong> {

/**
* Length of an Address in bytes (when considered as a Blob)
*/
static final int BYTE_LENGTH = 8;

@Override
public final long count() {
return BYTE_LENGTH;
}

@Override
public int estimatedEncodingSize() {
// tag VLC bytes
return 1 + Format.MAX_VLQ_COUNT_LENGTH;
}

@Override
public Blob slice(long start, long end) {
return toFlatBlob().slice(start,end);
}

@Override
public ABlob toBlob() {
return LongBlob.create(longValue());
}

protected static void checkIndex(long i) {
if ((i < 0) || (i >= BYTE_LENGTH)) throw new IndexOutOfBoundsException(ErrorMessages.badIndex(i));
}

@Override
public Blob toFlatBlob() {
byte[] bs=new byte[BYTE_LENGTH];
getBytes(bs,0);
return Blob.wrap(bs);
}

@Override
protected ACell toCanonical() {
// Always canonical
return this;
}

@Override
public long hexMatch(ABlobLike<?> b, long start, long length) {
for (int i=0; i<length; i++) {
int c=b.getHexDigit(start+i);
if (c!=getHexDigit(start+i)) return i;
}
return length;
}

@Override
public boolean equalsBytes(ABlob b) {
if (b.count()!=BYTE_LENGTH) return false;
return b.longValue()==longValue();
}

protected int compareTo(long bvalue) {
return Long.compareUnsigned(longValue(), bvalue);
}

@Override
public int compareTo(ABlobLike<?> b) {
if (b.count()==BYTE_LENGTH) {
return compareTo(b.longValue());
} else {
// safe because must be a different type
return -b.compareTo(this);
}
}

@Override
public AExtensionValue empty() {
// There is no empty extension value
return null;
}

@Override
protected final long calcMemorySize() {
// always embedded and no child Refs, so memory size == 0
return 0;
}

@Override
public boolean equals(ACell a) {
if (a instanceof AExtensionValue) return equals((AExtensionValue) a);
return false;
}

protected boolean equals(AExtensionValue a) {
if (getTag()!=a.getTag()) return false;
return longValue()==a.longValue();
}

@Override
public CVMLong get(long i) {
checkIndex(i);
return CVMLong.create(Utils.longByteAt(longValue(),i));
}

@Override
public <R extends ACell> Ref<R> getRef(int i) {
throw new IndexOutOfBoundsException(i);
}

@Override
public ACell updateRefs(IRefFunction func) {
return this;
}

@Override
public final int getRefCount() {
// No Refs
return 0;
}
}
93 changes: 5 additions & 88 deletions convex-core/src/main/java/convex/core/data/Address.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package convex.core.data;

import convex.core.data.impl.LongBlob;
import convex.core.data.prim.CVMLong;
import convex.core.data.type.AType;
import convex.core.data.type.Types;
import convex.core.data.util.BlobBuilder;
import convex.core.exceptions.BadFormatException;
import convex.core.exceptions.InvalidDataException;
import convex.core.lang.RT;
import convex.core.util.Bits;
import convex.core.util.ErrorMessages;
import convex.core.util.Utils;

/**
Expand All @@ -19,7 +16,7 @@
* serves as an index into the vector of accounts for the current state.
*
*/
public final class Address extends ABlobLike<CVMLong> {
public final class Address extends AExtensionValue {

public static final int LENGTH=8;

Expand All @@ -41,11 +38,6 @@ public final class Address extends ABlobLike<CVMLong> {
*/
public static final Address MAX_VALUE = Address.create(Long.MAX_VALUE);

/**
* Length of an Address in bytes (when considered as a Blob)
*/
static final int BYTE_LENGTH = 8;

/**
* 64-bit address value
*/
Expand Down Expand Up @@ -93,12 +85,7 @@ public static Address create(ABlobLike<?> b) {
public AType getType() {
return Types.ADDRESS;
}

@Override
public int hashCode() {
return Bits.hash32(value);
}


@Override
public boolean equals(Object a) {
if (a==this) return true; // Fast path, avoids cast
Expand Down Expand Up @@ -222,22 +209,11 @@ public String toString() {
return "#"+value;
}

@Override
public int estimatedEncodingSize() {
// tag VLC bytes
return 1 + Format.MAX_VLQ_COUNT_LENGTH;
}

@Override
public void validateCell() throws InvalidDataException {
if (value<0) throw new InvalidDataException("Address must be positive",this);
}

@Override
public Blob slice(long start, long end) {
return toFlatBlob().slice(start,end);
}

@Override
public Blob toFlatBlob() {
byte[] bs=new byte[BYTE_LENGTH];
Expand Down Expand Up @@ -277,25 +253,6 @@ public final byte byteAtUnchecked(long i) {
return (byte) Utils.longByteAt(value,i);
}

private static void checkIndex(long i) {
if ((i < 0) || (i >= LENGTH)) throw new IndexOutOfBoundsException(ErrorMessages.badIndex(i));
}

@Override
public long hexMatch(ABlobLike<?> b, long start, long length) {
for (int i=0; i<length; i++) {
int c=b.getHexDigit(start+i);
if (c!=getHexDigit(start+i)) return i;
}
return length;
}

@Override
public Address empty() {
// There is no empty Address
return null;
}

@Override
public final int getBytes(byte[] bs, int pos) {
pos=Utils.writeLong(bs, pos, value);
Expand All @@ -307,34 +264,20 @@ public long longValue() {
return value;
}

@Override
public ABlob toBlob() {
return LongBlob.create(value);
}

@Override
public boolean equalsBytes(ABlob b) {
if (b.count()!=LENGTH) return false;
return b.longValue()==(value);
}

@Override
public int compareTo(ABlobLike<?> b) {
if (b.count()==LENGTH) {
return compareTo(b.longValue());
} else {
// safe because must be a different type
return -b.compareTo(this);
}
}

protected int compareTo(long bvalue) {
return Long.compareUnsigned(value, bvalue);
}

@Override
public long count() {
return LENGTH;
public boolean isCVMValue() {
return true;
}

@Override
Expand All @@ -345,33 +288,7 @@ public CVMLong get(long i) {

@Override
public boolean isCanonical() {
// Always canonical, we assume valid by construction
return true;
}

@Override
protected long calcMemorySize() {
// always embedded and no child Refs, so memory size == 0
return 0;
}

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

@Override
public <R extends ACell> Ref<R> getRef(int i) {
throw new IndexOutOfBoundsException(i);
}

@Override
public ACell updateRefs(IRefFunction func) {
return this;
}

@Override
public int getRefCount() {
// No Refs
return 0;
}
}
Loading

0 comments on commit 89490c6

Please sign in to comment.