Skip to content

Commit

Permalink
Fix transaction usage of CAD3 encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 2, 2024
1 parent 3d3a1ed commit ac8bd87
Show file tree
Hide file tree
Showing 20 changed files with 195 additions and 343 deletions.
16 changes: 1 addition & 15 deletions convex-core/src/main/java/convex/core/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,6 @@ private static String checkValues(AVector<ACell> values) {
return null;
}

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

/**
* Reads a Result from a Blob encoding. Assumes tag byte already checked.
*
Expand All @@ -306,10 +299,8 @@ public static Result read(Blob b, int pos) throws BadFormatException {

// we can't check values yet because might be missing data

Blob enc=v.getEncoding();
Result r=buildFromVector(v);
v.attachEncoding(null); // This is an invalid encoding for vector, see above
r.attachEncoding(enc);
r.attachEncoding(b.slice(pos, epos));
return r;
}

Expand Down Expand Up @@ -389,11 +380,6 @@ public Result withID(ACell id) {
return withValues(values.assoc(ID_POS, id));
}

@Override
public RecordFormat getFormat() {
return RESULT_FORMAT;
}

/**
* Constructs a result from a caught exception
* @param e Exception caught
Expand Down
7 changes: 0 additions & 7 deletions convex-core/src/main/java/convex/core/cpos/Belief.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ public static Belief read(Blob b, int pos) throws BadFormatException {
if (values.count()!=1) throw new BadFormatException("Wrong number of values for Belief");

Belief result=new Belief(values);

values.attachEncoding(null);
result.attachEncoding(b.slice(pos,epos));
return result;
}
Expand Down Expand Up @@ -209,11 +207,6 @@ public HashMap<AccountKey, SignedData<Order>> getOrdersHashMap() {
return getOrders().toHashMap();
}

@Override
public RecordFormat getFormat() {
return BELIEF_FORMAT;
}

/**
* Extract a collection of Orders from a Cell, suitable for Belief merge
* @param payload Cell to extra orders from
Expand Down
5 changes: 5 additions & 0 deletions convex-core/src/main/java/convex/core/cpos/CPoSConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public class CPoSConstants {
* Memory allowance for genesis user / peer accounts
*/
public static final long INITIAL_ACCOUNT_ALLOWANCE = 1000000;

/**
* Maximum allowed encoded peer message length in bytes (50mb)
*/
public static final long MAX_MESSAGE_LENGTH = 50000000;


}
21 changes: 16 additions & 5 deletions convex-core/src/main/java/convex/core/cvm/ARecordGeneric.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,22 @@ public ACell get(Keyword key) {
}

@Override
public int getRefCount() {
public final RecordFormat getFormat() {
return format;
}

@Override
public final int getRefCount() {
return values.getRefCount();
}

@Override
public int encode(byte[] bs, int pos) {
public int estimatedEncodingSize() {
return values.estimatedEncodingSize();
}

@Override
public final int encode(byte[] bs, int pos) {
bs[pos++]=tag;
return encodeRaw(bs,pos);
}
Expand All @@ -57,7 +67,7 @@ public int encode(byte[] bs, int pos) {
* @param bs Array to write to
*/
@Override
public int encodeRaw(byte[] bs, int pos) {
public final int encodeRaw(byte[] bs, int pos) {
return values.encodeRaw(bs, pos);
}

Expand All @@ -83,13 +93,14 @@ protected boolean equals(ARecordGeneric a) {
}

@Override
public <R extends ACell> Ref<R> getRef(int index) {
public final <R extends ACell> Ref<R> getRef(int index) {
return values.getRef(index);
}

@Override
public ARecordGeneric updateRefs(IRefFunction func) {
public final ARecordGeneric updateRefs(IRefFunction func) {
AVector<ACell> newValues=values.updateRefs(func); // ensure values are canonical via toVector
if (newValues==values) return this;
return withValues(newValues);
}

Expand Down
2 changes: 0 additions & 2 deletions convex-core/src/main/java/convex/core/cvm/CVMTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class CVMTag {
* Tag for Convex Address type
*/
public static final byte ADDRESS = (byte) 0xEA;

public static final byte SYNTAX = (byte) 0x88;

// ==========================================
Expand Down Expand Up @@ -78,7 +77,6 @@ public class CVMTag {
// CVM Code
public static final byte OP = (byte) 0xC0;


public static final byte FN = (byte) 0xCF;

public static final byte FN_MULTI = (byte) 0xCB;
Expand Down
2 changes: 2 additions & 0 deletions convex-core/src/main/java/convex/core/cvm/Keywords.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public class Keywords {
public static final Keyword OFFER = Keyword.intern("offer");
public static final Keyword ORIGIN = Keyword.intern("origin");
public static final Keyword TARGET = Keyword.intern("target");
public static final Keyword ARGS = Keyword.intern("args");


public static final Keyword BLOCKS = Keyword.intern("blocks");
public static final Keyword CONSENSUS_POINT = Keyword.intern("consensus-point");
Expand Down
5 changes: 0 additions & 5 deletions convex-core/src/main/java/convex/core/cvm/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -891,11 +891,6 @@ public boolean equals(State a) {
return values.equals(a.values);
}

@Override
public RecordFormat getFormat() {
return FORMAT;
}

/**
* Gets the Convex Coin value in the memory exchange pool
* @return Memory pool Convex Coin amount (coppers)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package convex.core.cvm.transactions;

import convex.core.cvm.ACVMRecord;
import convex.core.cvm.ARecordGeneric;
import convex.core.cvm.Address;
import convex.core.cvm.Context;
import convex.core.cvm.Keywords;
import convex.core.cvm.RecordFormat;
import convex.core.data.ACell;
import convex.core.data.AVector;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.Keyword;
import convex.core.data.type.AType;
import convex.core.data.type.Transaction;
import convex.core.lang.RT;

/**
* Abstract base class for immutable transactions
Expand All @@ -20,7 +24,7 @@
* indicating a code error or system integrity issue.
*
*/
public abstract class ATransaction extends ACVMRecord {
public abstract class ATransaction extends ARecordGeneric {

/**
* Sequence number for transactions where required sequence is currently unknown
Expand All @@ -30,29 +34,13 @@ public abstract class ATransaction extends ACVMRecord {
protected final Address origin;
protected final long sequence;

protected ATransaction(byte tag,long count,Address origin, long sequence) {
super(tag,count);
protected ATransaction(byte tag,RecordFormat format, AVector<ACell> values) {
super(tag,format,values);
this.origin=RT.ensureAddress(values.get(0));
if (origin==null) throw new IllegalArgumentException("Null Origin Address for transaction");
this.origin=origin;
this.sequence = sequence;
this.sequence = RT.ensureLong(values.get(1)).longValue();
}

/**
* Writes this transaction to a byte array, including the message tag
*/
@Override
public abstract int encode(byte[] bs, int pos);

@Override
public int encodeRaw(byte[] bs, int pos) {
pos = Format.writeVLQCount(bs,pos, origin.longValue());
pos = Format.writeVLQCount(bs,pos, sequence);
return pos;
}

@Override
public abstract int estimatedEncodingSize();

/**
* Applies the functional effect of this transaction to the current state.
*
Expand Down Expand Up @@ -94,6 +82,13 @@ public AType getType() {
return Transaction.INSTANCE;
}

@Override
public ACell get(Keyword key) {
if (Keywords.ORIGIN.equals(key)) return origin;
if (Keywords.SEQUENCE.equals(key)) return values.get(1);
return null;
}

/**
* Updates this transaction with the specified sequence number
* @param newSequence New sequence number
Expand Down
Loading

0 comments on commit ac8bd87

Please sign in to comment.