Skip to content

Commit

Permalink
More refactoring for Protonet encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Nov 27, 2024
1 parent b4263b0 commit b7a6371
Show file tree
Hide file tree
Showing 49 changed files with 137 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import convex.core.crypto.Hashing;
import convex.core.data.AArrayBlob;
import convex.core.data.Blob;
import convex.core.data.Format;
import convex.core.data.Cells;
import convex.core.data.prim.CVMLong;

/**
Expand All @@ -21,7 +21,7 @@ public class HashBenchmark {
@Benchmark
public void longHash_SHA_256() {
CVMLong l = CVMLong.create(17L);
AArrayBlob d = Format.encodedBlob(l);
AArrayBlob d = Cells.encode(l);
Hashing.sha256(d.getInternalArray());
}

Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import convex.core.data.AVector;
import convex.core.data.Address;
import convex.core.data.Blob;
import convex.core.data.Format;
import convex.core.data.Cells;
import convex.core.data.Keyword;
import convex.core.data.Keywords;
import convex.core.data.Maps;
Expand Down Expand Up @@ -302,7 +302,7 @@ public static Result read(Blob b, int pos) throws BadFormatException {
int epos=pos;
// include tag location since we are reading raw Vector (will ignore tag)
AVector<ACell> v=Vectors.read(b,epos);
epos+=Format.getEncodingLength(v);
epos+=Cells.getEncodingLength(v);

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

Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/cpos/Belief.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static Belief read(Blob b, int pos) throws BadFormatException {

Index<AccountKey, SignedData<Order>> orders = Format.read(b,epos);
if (orders == null) throw new BadFormatException("Null orders in Belief");
epos+=Format.getEncodingLength(orders);
epos+=Cells.getEncodingLength(orders);

Belief result= new Belief(orders);
result.attachEncoding(b.slice(pos, epos));
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/cpos/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static Block read(Blob b, int pos) throws BadFormatException {
epos+=8;

AVector<SignedData<ATransaction>> transactions = Format.read(b,epos);
epos+=Format.getEncodingLength(transactions);
epos+=Cells.getEncodingLength(transactions);

Block result=Block.create(timestamp, transactions);
result.attachEncoding(b.slice(pos, epos));
Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/cpos/BlockResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ public static BlockResult read(Blob b, int pos) throws BadFormatException {

State newState=Format.read(b,epos);
if (newState==null) throw new BadFormatException("Null state");
epos+=Format.getEncodingLength(newState);
epos+=Cells.getEncodingLength(newState);

AVector<Result> newResults=Format.read(b,epos);
if (newResults==null) throw new BadFormatException("Null results");
epos+=Format.getEncodingLength(newResults);
epos+=Cells.getEncodingLength(newResults);

BlockResult result=create(newState,newResults);
result.attachEncoding(b.slice(pos, epos));
Expand Down
6 changes: 3 additions & 3 deletions convex-core/src/main/java/convex/core/cvm/PeerStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ public int encodeRaw(byte[] bs, int pos) {
public static PeerStatus read(Blob b, int pos) throws BadFormatException{
int epos=pos+1; // skip tag
Address owner = Format.read(b,epos);
epos+=Format.getEncodingLength(owner);
epos+=Cells.getEncodingLength(owner);

long stake = Format.readVLQLong(b,epos);
epos+=Format.getVLQLongLength(stake);

Index<Address, CVMLong> stakes = Format.read(b,epos);
epos+=Format.getEncodingLength(stakes);
epos+=Cells.getEncodingLength(stakes);
if (stakes==null) {
stakes=EMPTY_STAKES;
} else if (stakes.isEmpty()) {
Expand All @@ -227,7 +227,7 @@ public static PeerStatus read(Blob b, int pos) throws BadFormatException{
epos+=Format.getVLQLongLength(delegatedStake);

AHashMap<ACell,ACell> metadata = Format.read(b,epos);
epos+=Format.getEncodingLength(metadata);
epos+=Cells.getEncodingLength(metadata);

long timestamp=Format.readVLQLong(b,epos);
epos+=Format.getVLQLongLength(timestamp);
Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/java/convex/core/cvm/Receipt.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ public static Receipt read(byte tag,Blob b, int pos) throws BadFormatException {
boolean hasLog=((tag&Tag.RECEIPT_LOG_MASK)!=0);

ACell result = Format.read(b,epos);
epos+=Format.getEncodingLength(result);
epos+=Cells.getEncodingLength(result);

AVector<AVector<ACell>> log=null;
if (hasLog) {
log = Vectors.read(b, epos);
if ((log==null)||(log.isEmpty())) throw new BadFormatException("Expected non-empty log");
epos+=Format.getEncodingLength(log);
epos+=Cells.getEncodingLength(log);
}

Receipt receipt=new Receipt(isError,result,log);
Expand Down
8 changes: 4 additions & 4 deletions convex-core/src/main/java/convex/core/cvm/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,19 @@ public static State read(Blob b, int pos) throws BadFormatException {
int epos=pos+1; // skip tag
AVector<AccountStatus> accounts = Format.read(b,epos);
if (accounts==null) throw new BadFormatException("Null accounts!");
epos+=Format.getEncodingLength(accounts);
epos+=Cells.getEncodingLength(accounts);

Index<AccountKey, PeerStatus> peers = Format.read(b,epos);
if (peers==null) throw new BadFormatException("Null peers!");
epos+=Format.getEncodingLength(peers);
epos+=Cells.getEncodingLength(peers);

AVector<ACell> globals = Format.read(b,epos);
if (globals==null) throw new BadFormatException("Null globals!");
epos+=Format.getEncodingLength(globals);
epos+=Cells.getEncodingLength(globals);

Index<ABlob, AVector<ACell>> schedule = Format.read(b,epos);
if (schedule==null) throw new BadFormatException("Null schedule!");
epos+=Format.getEncodingLength(schedule);
epos+=Cells.getEncodingLength(schedule);

State result=create(accounts, peers, globals, schedule);
result.attachEncoding(b.slice(pos,epos));
Expand Down
3 changes: 2 additions & 1 deletion convex-core/src/main/java/convex/core/cvm/ops/Cond.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import convex.core.data.ASequence;
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.IRefFunction;
import convex.core.data.Vectors;
Expand Down Expand Up @@ -110,7 +111,7 @@ public static <T extends ACell> Cond<T> read(Blob b, int pos) throws BadFormatEx
int epos=pos+Ops.OP_DATA_OFFSET; // skip tag and opcode to get to data

AVector<AOp<ACell>> ops = Format.read(b,epos);
epos+=Format.getEncodingLength(ops);
epos+=Cells.getEncodingLength(ops);

Cond<T> result=create(ops);
result.attachEncoding(b.slice(pos, epos));
Expand Down
3 changes: 2 additions & 1 deletion convex-core/src/main/java/convex/core/cvm/ops/Def.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import convex.core.cvm.Ops;
import convex.core.data.ACell;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.IRefFunction;
import convex.core.data.Ref;
Expand Down Expand Up @@ -156,7 +157,7 @@ public static <T extends ACell> Def<T> read(Blob b, int pos) throws BadFormatExc
int epos=pos+Ops.OP_DATA_OFFSET; // skip tag and opcode to get to data

ACell symbol = Format.read(b,epos);
epos+=Format.getEncodingLength(symbol);
epos+=Cells.getEncodingLength(symbol);

Ref<AOp<T>> ref = Format.readRef(b,epos);
if (!validKey(symbol)) throw new BadFormatException("Symbol not valid for Def op");
Expand Down
3 changes: 2 additions & 1 deletion convex-core/src/main/java/convex/core/cvm/ops/Do.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import convex.core.data.ASequence;
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.Vectors;
import convex.core.data.util.BlobBuilder;
Expand Down Expand Up @@ -91,7 +92,7 @@ public static <T extends ACell> Do<T> read(Blob b, int pos) throws BadFormatExce
int epos=pos+Ops.OP_DATA_OFFSET; // skip tag and opcode to get to data

AVector<AOp<ACell>> ops = Format.read(b,epos);
epos+=Format.getEncodingLength(ops);
epos+=Cells.getEncodingLength(ops);

Do<T> result=create(ops);
result.attachEncoding(b.slice(pos, epos));
Expand Down
3 changes: 2 additions & 1 deletion convex-core/src/main/java/convex/core/cvm/ops/Invoke.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import convex.core.data.ASequence;
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.Vectors;
import convex.core.data.type.Types;
Expand Down Expand Up @@ -131,7 +132,7 @@ public byte opCode() {
public static<T extends ACell> Invoke<T> read(Blob b, int pos) throws BadFormatException {
int epos=pos+Ops.OP_DATA_OFFSET; // skip tag and opcode to get to data
AVector<AOp<ACell>> ops = Format.read(b,epos);
epos+=Format.getEncodingLength(ops);
epos+=Cells.getEncodingLength(ops);

Invoke<T> result=create(ops);
result.attachEncoding(b.slice(pos, epos));
Expand Down
5 changes: 3 additions & 2 deletions convex-core/src/main/java/convex/core/cvm/ops/Let.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import convex.core.data.ASequence;
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.IRefFunction;
import convex.core.data.Ref;
Expand Down Expand Up @@ -183,9 +184,9 @@ public static <T extends ACell> Let<T> read(Blob b, int pos, boolean isLoop) thr
int epos=pos+Ops.OP_DATA_OFFSET; // skip tag and opcode to get to data

AVector<ACell> syms = Format.read(b,epos);
epos+=Format.getEncodingLength(syms);
epos+=Cells.getEncodingLength(syms);
AVector<AOp<ACell>> ops = Format.read(b,epos);
epos+=Format.getEncodingLength(ops);
epos+=Cells.getEncodingLength(ops);

Let<T> result= create(syms, ops.toVector(),isLoop);
result.attachEncoding(b.slice(pos, epos));
Expand Down
5 changes: 3 additions & 2 deletions convex-core/src/main/java/convex/core/cvm/ops/Lookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import convex.core.data.ACell;
import convex.core.data.Address;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.IRefFunction;
import convex.core.data.Ref;
Expand Down Expand Up @@ -107,10 +108,10 @@ public static <T extends ACell> Lookup<T> read(Blob b,int pos) throws BadFormatE

Symbol sym = Format.read(b,epos);
if (sym==null) throw new BadFormatException("Lookup symbol cannot be null");
epos+=Format.getEncodingLength(sym);
epos+=Cells.getEncodingLength(sym);

AOp<Address> address = Format.read(b,epos);
epos+=Format.getEncodingLength(address);
epos+=Cells.getEncodingLength(address);

Lookup<T> result= create(address,sym);
result.attachEncoding(b.slice(pos, epos));
Expand Down
3 changes: 2 additions & 1 deletion convex-core/src/main/java/convex/core/cvm/ops/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import convex.core.data.ASequence;
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.Vectors;
import convex.core.data.util.BlobBuilder;
Expand Down Expand Up @@ -98,7 +99,7 @@ public static <T extends ACell> Query<T> read(Blob b, int pos) throws BadFormatE
int epos=pos+Ops.OP_DATA_OFFSET; // skip tag and opcode to get to data

AVector<AOp<ACell>> ops = Format.read(b,epos);
epos+=Format.getEncodingLength(ops);
epos+=Cells.getEncodingLength(ops);

Query<T> result= create(ops);
result.attachEncoding(b.slice(pos, epos));
Expand Down
3 changes: 2 additions & 1 deletion convex-core/src/main/java/convex/core/cvm/ops/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import convex.core.data.ACell;
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.IRefFunction;
import convex.core.data.Ref;
Expand Down Expand Up @@ -95,7 +96,7 @@ public static <R extends ACell> Set<R> read(Blob b, int pos) throws BadFormatExc
epos+=Format.getVLQLongLength(position);

AOp<R> op = Format.read(b,epos);
epos+=Format.getEncodingLength(op);
epos+=Cells.getEncodingLength(op);

Set<R> result= create(position, op);
result.attachEncoding(b.slice(pos, epos));
Expand Down
3 changes: 2 additions & 1 deletion convex-core/src/main/java/convex/core/cvm/ops/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import convex.core.data.ASequence;
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.Vectors;
import convex.core.data.util.BlobBuilder;
Expand Down Expand Up @@ -103,7 +104,7 @@ public static <T extends ACell> Try<T> read(Blob b, int pos) throws BadFormatExc
int epos=pos+Ops.OP_DATA_OFFSET; // skip tag and opcode to get to data

AVector<AOp<ACell>> ops = Format.read(b,epos);
epos+=Format.getEncodingLength(ops);
epos+=Cells.getEncodingLength(ops);

Try<T> result=create(ops);
result.attachEncoding(b.slice(pos, epos));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import convex.core.data.AVector;
import convex.core.data.Address;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.IRefFunction;
import convex.core.data.Keyword;
Expand Down Expand Up @@ -87,18 +88,18 @@ public static Call read(Blob b, int pos) throws BadFormatException {
epos+=Format.getVLQCountLength(sequence);

Address target=Format.read(b, epos);
epos+=Format.getEncodingLength(target);
epos+=Cells.getEncodingLength(target);

long offer=Format.readVLQCount(b,epos);
epos+=Format.getVLQCountLength(offer);
if (!Coin.isValidAmount(offer)) throw new BadFormatException("Invalid offer in Call");


Symbol functionName=Format.read(b,epos);
epos+=Format.getEncodingLength(functionName);
epos+=Cells.getEncodingLength(functionName);

AVector<ACell> args = Format.read(b,epos);
epos+=Format.getEncodingLength(args);
epos+=Cells.getEncodingLength(args);

Call result=create(origin,sequence, target, offer, functionName,args);
result.attachEncoding(b.slice(pos,epos));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static Invoke read(Blob b, int pos) throws BadFormatException {
epos+=Format.getVLQCountLength(sequence);

ACell args=Format.read(b, epos);
epos+=Format.getEncodingLength(args);
epos+=Cells.getEncodingLength(args);

Invoke result=create(address, sequence, args);
result.attachEncoding(b.slice(pos, epos));
Expand Down
8 changes: 5 additions & 3 deletions convex-core/src/main/java/convex/core/data/CAD3Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
/**
* Base Encoder for CAD3 data / stores
*/
public abstract class CAD3Encoder extends AEncoder<ACell> {
public class CAD3Encoder extends AEncoder<ACell> {

public Blob encode(ACell a) {
return Format.encodedBlob(a);
return Cells.encode(a);
}

public ACell decode(Blob encoding) throws BadFormatException {
Expand All @@ -22,5 +22,7 @@ public ACell decode(Blob encoding) throws BadFormatException {
* @throws BadFormatException If CAD3 encoding format is invalid
*/
@Override
public abstract ACell decodeMultiCell(Blob enc) throws BadFormatException;
public ACell decodeMultiCell(Blob enc) throws BadFormatException {
return Format.decodeMultiCell(enc);
}
}
7 changes: 1 addition & 6 deletions convex-core/src/main/java/convex/core/data/CVMEncoder.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package convex.core.data;

import convex.core.exceptions.BadFormatException;

public class CVMEncoder extends CAD3Encoder {

public static final CVMEncoder INSTANCE = new CVMEncoder();

@Override
public ACell decodeMultiCell(Blob enc) throws BadFormatException {
return Format.decodeMultiCell(enc);
}


}
18 changes: 17 additions & 1 deletion convex-core/src/main/java/convex/core/data/Cells.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static boolean isCVM(ACell a) {
}

/**
* Checks if a Cell is completely encoded, i.e. has not external Refs
* Checks if a Cell is completely encoded, i.e. has no external Refs
* @param a Cell to check
* @return True if completely encoded, false otherwise
*/
Expand Down Expand Up @@ -345,4 +345,20 @@ public static boolean isEmbedded(ACell cell) {
return cell.isEmbedded();
}

/**
* Gets the encoded Blob for an object in CAD3 format
*
* @param o The object to encode
* @return Encoded data as a blob
*/
public static Blob encode(ACell o) {
if (o==null) return Blob.NULL_ENCODING;
return o.getEncoding();
}

public static int getEncodingLength(ACell value) {
if (value==null) return 1;
return value.getEncodingLength();
}

}
Loading

0 comments on commit b7a6371

Please sign in to comment.