Skip to content

Commit

Permalink
Make BlockResult work as generic record
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 2, 2024
1 parent 14b54e2 commit 27ac242
Showing 1 changed file with 27 additions and 75 deletions.
102 changes: 27 additions & 75 deletions convex-core/src/main/java/convex/core/cpos/BlockResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import convex.core.ErrorCodes;
import convex.core.Result;
import convex.core.cvm.ACVMRecord;
import convex.core.cvm.ARecordGeneric;
import convex.core.cvm.CVMTag;
import convex.core.cvm.Keywords;
import convex.core.cvm.RecordFormat;
Expand All @@ -12,14 +12,13 @@
import convex.core.data.AVector;
import convex.core.data.Blob;
import convex.core.data.Cells;
import convex.core.data.Format;
import convex.core.data.Hash;
import convex.core.data.IRefFunction;
import convex.core.data.Keyword;
import convex.core.data.Ref;
import convex.core.data.Vectors;
import convex.core.exceptions.BadFormatException;
import convex.core.exceptions.InvalidDataException;
import convex.core.lang.RT;
import convex.core.util.ErrorMessages;
import convex.core.util.Utils;

/**
Expand All @@ -29,34 +28,31 @@
* either be a valid result or an error.
*
*/
public class BlockResult extends ACVMRecord {
public class BlockResult extends ARecordGeneric {
private State state;
private AVector<Result> results;

private static final Keyword[] BLOCKRESULT_KEYS = new Keyword[] { Keywords.STATE, Keywords.RESULTS};

private static final RecordFormat FORMAT = RecordFormat.of(BLOCKRESULT_KEYS);


private BlockResult(State state, AVector<Result> results) {
super(CVMTag.BLOCK_RESULT,FORMAT.count());
super(CVMTag.BLOCK_RESULT,FORMAT,Vectors.create(state,results));
this.state = state;
this.results = results;
}

public BlockResult(AVector<ACell> values) {
super(CVMTag.BLOCK_RESULT,FORMAT,values);
}

/**
* Create a BlockResult
* @param state Resulting State
* @param results Results of transactions in Block
* @return BlockResult instance
*/
public static BlockResult create(State state, Result[] results) {
int n=results.length;
Object[] rs=new Object[n];
for (int i=0; i<n; i++) {
rs[i]=results[i];
}
return new BlockResult(state, Vectors.of(rs));
return new BlockResult(state, Vectors.create(results));
}

/**
Expand All @@ -74,6 +70,7 @@ public static BlockResult create(State state, AVector<Result> results) {
* @return State after Block is executed
*/
public State getState() {
if (state==null) state=(State)values.get(0);
return state;
}

Expand All @@ -82,6 +79,7 @@ public State getState() {
* @return Vector of Results
*/
public AVector<Result> getResults() {
if (results==null) results=RT.ensureVector(values.get(1));
return results;
}

Expand All @@ -100,6 +98,7 @@ public boolean isError(long i) {
* @return Result at specified index for the current Block, or null if not available
*/
public Result getResult(long i) {
AVector<Result> results=getResults();
if ((i<0)||(i>=results.count())) return null;
return results.get(i);
}
Expand All @@ -121,17 +120,9 @@ public ACell get(Keyword key) {
return null;
}

@Override
public BlockResult updateRefs(IRefFunction func) {
State newState=(State)state.updateRefs(func);
AVector<Result> newResults=results.updateRefs(func);
return create(newState,newResults);
}

@Override
public void validateCell() throws InvalidDataException {
// TODO Auto-generated method stub

}

@Override
Expand All @@ -142,30 +133,11 @@ public void validate() throws InvalidDataException {

long n=results.count();
for (long i=0; i<n; i++) {
Object r=results.get(i);
ACell r=results.get(i);
if (!(r instanceof Result)) throw new InvalidDataException("Not a Result at position "+i+" - found "+Utils.getClassName(r),this);
}
}

@Override
public int encode(byte[] bs, int pos) {
bs[pos++]=getTag();
// generic record writeRaw, handles all fields in declared order
return encodeRaw(bs,pos);
}

@Override
public int encodeRaw(byte[] bs, int pos) {
pos=state.encode(bs,pos);
pos=results.encode(bs,pos);
return pos;
}

@Override
public int estimatedEncodingSize() {
return 1+state.estimatedEncodingSize()+results.estimatedEncodingSize();
}

/**
* Decodes a BlockResult from a Blob
* @param b Blob to read from
Expand All @@ -174,27 +146,21 @@ public int estimatedEncodingSize() {
* @throws BadFormatException If encoding format has errors
*/
public static BlockResult read(Blob b, int pos) throws BadFormatException {
int epos=pos+1; // skip tag
AVector<ACell> values=Vectors.read(b, pos);
int epos=pos+values.getEncodingLength();

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

AVector<Result> newResults=Format.read(b,epos);
if (newResults==null) throw new BadFormatException("Null results");
epos+=Cells.getEncodingLength(newResults);
if (values.count()!=BLOCKRESULT_KEYS.length) throw new BadFormatException(ErrorMessages.RECORD_VALUE_NUMBER);

BlockResult result=create(newState,newResults);
result.attachEncoding(b.slice(pos, epos));
BlockResult result=new BlockResult(values);
result.attachEncoding(b.slice(pos,epos));
return result;
}


@Override
public boolean equals(ACell a) {
if (!(a instanceof BlockResult)) return false;
BlockResult as=(BlockResult)a;
return equals(as);
if (a instanceof BlockResult)return equals((BlockResult)a);
return Cells.equalsGeneric(this,a);
}

/**
Expand All @@ -216,26 +182,6 @@ public boolean equals(BlockResult a) {
return true;
}

@Override
public int getRefCount() {
return state.getRefCount()+results.getRefCount();
}

@Override
public <R extends ACell> Ref<R> getRef(int i) {
int sc=Cells.refCount(state);
if (i<sc) {
return state.getRef(i);
} else {
return results.getRef(i-sc);
}
}

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

/**
* Creates a BlockResult for an invalid Block (i.e. no peer)
* @param state State at time of creation
Expand All @@ -250,6 +196,12 @@ public static BlockResult createInvalidBlock(State state, Block block, AString m
return new BlockResult(state,rs);
}

@Override
protected ARecordGeneric withValues(AVector<ACell> newValues) {
if (values==newValues) return this;
return new BlockResult(newValues);
}




Expand Down

0 comments on commit 27ac242

Please sign in to comment.