Skip to content

Commit

Permalink
Refactor Address to use CAD3 Extension value 0xEA
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Nov 30, 2024
1 parent 041e8b2 commit a2239e8
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 22 deletions.
5 changes: 2 additions & 3 deletions convex-core/src/main/java/convex/core/cvm/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import convex.core.data.Blob;
import convex.core.data.Format;
import convex.core.data.Strings;
import convex.core.data.Tag;
import convex.core.data.prim.CVMLong;
import convex.core.data.type.AType;
import convex.core.data.type.Types;
Expand Down Expand Up @@ -191,7 +190,7 @@ public static Address read(Blob b, int pos) throws BadFormatException {

@Override
public int encode(byte[] bs, int pos) {
bs[pos++]=Tag.ADDRESS;
bs[pos++]=CVMTag.ADDRESS;
return encodeRaw(bs,pos);
}

Expand Down Expand Up @@ -234,7 +233,7 @@ public Blob toFlatBlob() {

@Override
public byte getTag() {
return Tag.ADDRESS;
return CVMTag.ADDRESS;
}

@Override
Expand Down
23 changes: 23 additions & 0 deletions convex-core/src/main/java/convex/core/cvm/CVMTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package convex.core.cvm;

/**
* Class defining tags for CVM CAD3 extension types
*/
public class CVMTag {

/**
* Tag for Convex Address type
*/
public static final byte ADDRESS = (byte) 0xEA;

/**
* CVM Core definitions
*/
public static final byte CORE_DEF = (byte) 0xED;

/**
* Special Ops
*/
public static final byte SPECIAL_OP = (byte) 0xE5;

}
3 changes: 2 additions & 1 deletion convex-core/src/main/java/convex/core/data/CAD3Encoder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package convex.core.data;

import convex.core.cvm.Address;
import convex.core.cvm.CVMTag;
import convex.core.data.prim.AByteFlag;
import convex.core.data.prim.ANumeric;
import convex.core.data.prim.CVMBigInteger;
Expand Down Expand Up @@ -44,7 +45,7 @@ protected ACell read(byte tag, Blob encoding, int offset) throws BadFormatExcept
return readNumeric(tag,encoding,offset);

case 2: // 0x20-0x2F : Addresses and references
if (tag == Tag.ADDRESS) return Address.read(encoding,offset);
if (tag == CVMTag.ADDRESS) return Address.read(encoding,offset);
// Note: 0x20 reference is invalid as a top level encoding
break;

Expand Down
5 changes: 4 additions & 1 deletion convex-core/src/main/java/convex/core/data/CVMEncoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package convex.core.data;

import convex.core.cvm.Address;
import convex.core.cvm.CVMTag;
import convex.core.exceptions.BadFormatException;
import convex.core.lang.Core;

Expand All @@ -18,7 +20,8 @@ public ACell read(Blob encoding,int offset) throws BadFormatException {
protected ACell readExtension(byte tag, Blob blob, int offset) throws BadFormatException {
// We expect a VLQ Count following the tag
long code=Format.readVLQCount(blob,offset+1);
if (tag == Tag.CORE_DEF) return Core.fromCode(code);
if (tag == CVMTag.CORE_DEF) return Core.fromCode(code);
if (tag == CVMTag.ADDRESS) return Address.create(code);

return ExtensionValue.create(tag, code);
}
Expand Down
5 changes: 3 additions & 2 deletions convex-core/src/main/java/convex/core/data/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import convex.core.cvm.AFn;
import convex.core.cvm.AccountStatus;
import convex.core.cvm.Address;
import convex.core.cvm.CVMTag;
import convex.core.cvm.Ops;
import convex.core.cvm.PeerStatus;
import convex.core.cvm.Receipt;
Expand Down Expand Up @@ -518,7 +519,7 @@ private static ACell readExtension(byte tag, Blob blob, int offset) throws BadFo
// We expect a VLQ Count following the tag
long code=readVLQCount(blob,offset+1);

if (tag == Tag.CORE_DEF) return Core.fromCode(code);
if (tag == CVMTag.CORE_DEF) return Core.fromCode(code);

return ExtensionValue.create(tag, code);
}
Expand Down Expand Up @@ -594,7 +595,7 @@ static <T extends ACell> T read(byte tag, Blob blob, int offset) throws BadForma

if (high == 0x30) return (T) readBasicObject(tag,blob,offset);

if (tag == Tag.ADDRESS) return (T) Address.read(blob,offset);
if (tag == CVMTag.ADDRESS) return (T) Address.read(blob,offset);

if (high == 0xB0) return (T) AByteFlag.read(tag);

Expand Down
9 changes: 1 addition & 8 deletions convex-core/src/main/java/convex/core/data/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Tag {

// crypto and security primitives
public static final byte REF = (byte) 0x20;
public static final byte ADDRESS = (byte) 0x21;


// =========================================
// Strings and Blobs (0x3x)
Expand Down Expand Up @@ -121,13 +121,6 @@ public class Tag {

public static final byte EXTENSION_VALUE_BASE = (byte) 0xE0;

// CVM Core definitions
public static final byte CORE_DEF = (byte) 0xED;

// Special Ops
public static final byte SPECIAL_OP = (byte) 0xE5;


//===========================================
// Illegal / reserved for special values (0xFx)
public static final byte ILLEGAL = (byte) 0xFF;
Expand Down
6 changes: 3 additions & 3 deletions convex-core/src/main/java/convex/core/lang/impl/CoreFn.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package convex.core.lang.impl;

import convex.core.cvm.AFn;
import convex.core.cvm.CVMTag;
import convex.core.cvm.Context;
import convex.core.data.ACell;
import convex.core.data.Format;
import convex.core.data.IRefFunction;
import convex.core.data.Ref;
import convex.core.data.Symbol;
import convex.core.data.Tag;
import convex.core.data.util.BlobBuilder;
import convex.core.exceptions.InvalidDataException;

Expand Down Expand Up @@ -47,7 +47,7 @@ public Symbol getIntrinsicSymbol() {
}

public byte getTag() {
return Tag.CORE_DEF;
return CVMTag.CORE_DEF;
}

protected String name() {
Expand Down Expand Up @@ -94,7 +94,7 @@ public boolean print(BlobBuilder sb, long limit) {

@Override
public int encode(byte[] bs, int pos) {
bs[pos++]=Tag.CORE_DEF;
bs[pos++]=CVMTag.CORE_DEF;
return encodeRaw(bs,pos);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package convex.core.data;
package convex.core.cvm;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import convex.core.cvm.Address;
import convex.core.data.ACell;
import convex.core.data.Blob;
import convex.core.data.Blobs;
import convex.core.data.BlobsTest;
import convex.core.data.Format;
import convex.core.data.ObjectsTest;
import convex.core.exceptions.BadFormatException;
import convex.core.util.Utils;

public class AddressTest {

Expand All @@ -27,6 +34,16 @@ public void testAddress2() {
assertEquals("#13",a1.toString());
}

@Test
public void testEncoding() throws BadFormatException {
Address a= Address.create(17);
Blob enc=a.getEncoding();
assertEquals(Utils.toHexString(CVMTag.ADDRESS)+"11",enc.toHexString());
ACell ra=Format.read(enc);
assertTrue(ra instanceof Address);
assertEquals(a,ra);
}

@Test
public void testParse() {
assertEquals("#1",Address.parse("#1").toString());
Expand Down
5 changes: 3 additions & 2 deletions convex-core/src/test/java/convex/core/data/CAD3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.api.TestInstance.Lifecycle;

import convex.core.cvm.Address;
import convex.core.cvm.CVMTag;
import convex.core.cvm.Context;
import convex.core.data.prim.CVMLong;
import convex.core.lang.ACVMTest;
Expand All @@ -34,11 +35,11 @@ public class CAD3Test extends ACVMTest {
}

@Test public void testExtensionCoreDefs() {
assertSame(Core.VECTOR,Reader.read("#["+Utils.toHexString(Tag.CORE_DEF)+"01]"));
assertSame(Core.VECTOR,Reader.read("#["+Utils.toHexString(CVMTag.CORE_DEF)+"01]"));
}

@Test public void testReadEncodings() {
assertSame(Address.ZERO,Reader.read("#[2100]"));
assertSame(Address.ZERO,Reader.read("#[EA00]"));
assertSame(CVMLong.ZERO,Reader.read("#[10]"));
assertSame(Vectors.empty(),Reader.read("#[8000]"));
assertNull(Reader.read("#[00]"));
Expand Down

0 comments on commit a2239e8

Please sign in to comment.