Skip to content

Commit 27ac242

Browse files
committed
Make BlockResult work as generic record
1 parent 14b54e2 commit 27ac242

File tree

1 file changed

+27
-75
lines changed

1 file changed

+27
-75
lines changed

convex-core/src/main/java/convex/core/cpos/BlockResult.java

Lines changed: 27 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import convex.core.ErrorCodes;
44
import convex.core.Result;
5-
import convex.core.cvm.ACVMRecord;
5+
import convex.core.cvm.ARecordGeneric;
66
import convex.core.cvm.CVMTag;
77
import convex.core.cvm.Keywords;
88
import convex.core.cvm.RecordFormat;
@@ -12,14 +12,13 @@
1212
import convex.core.data.AVector;
1313
import convex.core.data.Blob;
1414
import convex.core.data.Cells;
15-
import convex.core.data.Format;
1615
import convex.core.data.Hash;
17-
import convex.core.data.IRefFunction;
1816
import convex.core.data.Keyword;
19-
import convex.core.data.Ref;
2017
import convex.core.data.Vectors;
2118
import convex.core.exceptions.BadFormatException;
2219
import convex.core.exceptions.InvalidDataException;
20+
import convex.core.lang.RT;
21+
import convex.core.util.ErrorMessages;
2322
import convex.core.util.Utils;
2423

2524
/**
@@ -29,34 +28,31 @@
2928
* either be a valid result or an error.
3029
*
3130
*/
32-
public class BlockResult extends ACVMRecord {
31+
public class BlockResult extends ARecordGeneric {
3332
private State state;
3433
private AVector<Result> results;
3534

3635
private static final Keyword[] BLOCKRESULT_KEYS = new Keyword[] { Keywords.STATE, Keywords.RESULTS};
37-
3836
private static final RecordFormat FORMAT = RecordFormat.of(BLOCKRESULT_KEYS);
3937

40-
4138
private BlockResult(State state, AVector<Result> results) {
42-
super(CVMTag.BLOCK_RESULT,FORMAT.count());
39+
super(CVMTag.BLOCK_RESULT,FORMAT,Vectors.create(state,results));
4340
this.state = state;
4441
this.results = results;
4542
}
4643

44+
public BlockResult(AVector<ACell> values) {
45+
super(CVMTag.BLOCK_RESULT,FORMAT,values);
46+
}
47+
4748
/**
4849
* Create a BlockResult
4950
* @param state Resulting State
5051
* @param results Results of transactions in Block
5152
* @return BlockResult instance
5253
*/
5354
public static BlockResult create(State state, Result[] results) {
54-
int n=results.length;
55-
Object[] rs=new Object[n];
56-
for (int i=0; i<n; i++) {
57-
rs[i]=results[i];
58-
}
59-
return new BlockResult(state, Vectors.of(rs));
55+
return new BlockResult(state, Vectors.create(results));
6056
}
6157

6258
/**
@@ -74,6 +70,7 @@ public static BlockResult create(State state, AVector<Result> results) {
7470
* @return State after Block is executed
7571
*/
7672
public State getState() {
73+
if (state==null) state=(State)values.get(0);
7774
return state;
7875
}
7976

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

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

124-
@Override
125-
public BlockResult updateRefs(IRefFunction func) {
126-
State newState=(State)state.updateRefs(func);
127-
AVector<Result> newResults=results.updateRefs(func);
128-
return create(newState,newResults);
129-
}
130-
131123
@Override
132124
public void validateCell() throws InvalidDataException {
133125
// TODO Auto-generated method stub
134-
135126
}
136127

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

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

150-
@Override
151-
public int encode(byte[] bs, int pos) {
152-
bs[pos++]=getTag();
153-
// generic record writeRaw, handles all fields in declared order
154-
return encodeRaw(bs,pos);
155-
}
156-
157-
@Override
158-
public int encodeRaw(byte[] bs, int pos) {
159-
pos=state.encode(bs,pos);
160-
pos=results.encode(bs,pos);
161-
return pos;
162-
}
163-
164-
@Override
165-
public int estimatedEncodingSize() {
166-
return 1+state.estimatedEncodingSize()+results.estimatedEncodingSize();
167-
}
168-
169141
/**
170142
* Decodes a BlockResult from a Blob
171143
* @param b Blob to read from
@@ -174,27 +146,21 @@ public int estimatedEncodingSize() {
174146
* @throws BadFormatException If encoding format has errors
175147
*/
176148
public static BlockResult read(Blob b, int pos) throws BadFormatException {
177-
int epos=pos+1; // skip tag
149+
AVector<ACell> values=Vectors.read(b, pos);
150+
int epos=pos+values.getEncodingLength();
178151

179-
State newState=Format.read(b,epos);
180-
if (newState==null) throw new BadFormatException("Null state");
181-
epos+=Cells.getEncodingLength(newState);
182-
183-
AVector<Result> newResults=Format.read(b,epos);
184-
if (newResults==null) throw new BadFormatException("Null results");
185-
epos+=Cells.getEncodingLength(newResults);
152+
if (values.count()!=BLOCKRESULT_KEYS.length) throw new BadFormatException(ErrorMessages.RECORD_VALUE_NUMBER);
186153

187-
BlockResult result=create(newState,newResults);
188-
result.attachEncoding(b.slice(pos, epos));
154+
BlockResult result=new BlockResult(values);
155+
result.attachEncoding(b.slice(pos,epos));
189156
return result;
190157
}
191158

192159

193160
@Override
194161
public boolean equals(ACell a) {
195-
if (!(a instanceof BlockResult)) return false;
196-
BlockResult as=(BlockResult)a;
197-
return equals(as);
162+
if (a instanceof BlockResult)return equals((BlockResult)a);
163+
return Cells.equalsGeneric(this,a);
198164
}
199165

200166
/**
@@ -216,26 +182,6 @@ public boolean equals(BlockResult a) {
216182
return true;
217183
}
218184

219-
@Override
220-
public int getRefCount() {
221-
return state.getRefCount()+results.getRefCount();
222-
}
223-
224-
@Override
225-
public <R extends ACell> Ref<R> getRef(int i) {
226-
int sc=Cells.refCount(state);
227-
if (i<sc) {
228-
return state.getRef(i);
229-
} else {
230-
return results.getRef(i-sc);
231-
}
232-
}
233-
234-
@Override
235-
public RecordFormat getFormat() {
236-
return FORMAT;
237-
}
238-
239185
/**
240186
* Creates a BlockResult for an invalid Block (i.e. no peer)
241187
* @param state State at time of creation
@@ -250,6 +196,12 @@ public static BlockResult createInvalidBlock(State state, Block block, AString m
250196
return new BlockResult(state,rs);
251197
}
252198

199+
@Override
200+
protected ARecordGeneric withValues(AVector<ACell> newValues) {
201+
if (values==newValues) return this;
202+
return new BlockResult(newValues);
203+
}
204+
253205

254206

255207

0 commit comments

Comments
 (0)