2
2
3
3
import convex .core .ErrorCodes ;
4
4
import convex .core .Result ;
5
- import convex .core .cvm .ACVMRecord ;
5
+ import convex .core .cvm .ARecordGeneric ;
6
6
import convex .core .cvm .CVMTag ;
7
7
import convex .core .cvm .Keywords ;
8
8
import convex .core .cvm .RecordFormat ;
12
12
import convex .core .data .AVector ;
13
13
import convex .core .data .Blob ;
14
14
import convex .core .data .Cells ;
15
- import convex .core .data .Format ;
16
15
import convex .core .data .Hash ;
17
- import convex .core .data .IRefFunction ;
18
16
import convex .core .data .Keyword ;
19
- import convex .core .data .Ref ;
20
17
import convex .core .data .Vectors ;
21
18
import convex .core .exceptions .BadFormatException ;
22
19
import convex .core .exceptions .InvalidDataException ;
20
+ import convex .core .lang .RT ;
21
+ import convex .core .util .ErrorMessages ;
23
22
import convex .core .util .Utils ;
24
23
25
24
/**
29
28
* either be a valid result or an error.
30
29
*
31
30
*/
32
- public class BlockResult extends ACVMRecord {
31
+ public class BlockResult extends ARecordGeneric {
33
32
private State state ;
34
33
private AVector <Result > results ;
35
34
36
35
private static final Keyword [] BLOCKRESULT_KEYS = new Keyword [] { Keywords .STATE , Keywords .RESULTS };
37
-
38
36
private static final RecordFormat FORMAT = RecordFormat .of (BLOCKRESULT_KEYS );
39
37
40
-
41
38
private BlockResult (State state , AVector <Result > results ) {
42
- super (CVMTag .BLOCK_RESULT ,FORMAT . count ( ));
39
+ super (CVMTag .BLOCK_RESULT ,FORMAT , Vectors . create ( state , results ));
43
40
this .state = state ;
44
41
this .results = results ;
45
42
}
46
43
44
+ public BlockResult (AVector <ACell > values ) {
45
+ super (CVMTag .BLOCK_RESULT ,FORMAT ,values );
46
+ }
47
+
47
48
/**
48
49
* Create a BlockResult
49
50
* @param state Resulting State
50
51
* @param results Results of transactions in Block
51
52
* @return BlockResult instance
52
53
*/
53
54
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 ));
60
56
}
61
57
62
58
/**
@@ -74,6 +70,7 @@ public static BlockResult create(State state, AVector<Result> results) {
74
70
* @return State after Block is executed
75
71
*/
76
72
public State getState () {
73
+ if (state ==null ) state =(State )values .get (0 );
77
74
return state ;
78
75
}
79
76
@@ -82,6 +79,7 @@ public State getState() {
82
79
* @return Vector of Results
83
80
*/
84
81
public AVector <Result > getResults () {
82
+ if (results ==null ) results =RT .ensureVector (values .get (1 ));
85
83
return results ;
86
84
}
87
85
@@ -100,6 +98,7 @@ public boolean isError(long i) {
100
98
* @return Result at specified index for the current Block, or null if not available
101
99
*/
102
100
public Result getResult (long i ) {
101
+ AVector <Result > results =getResults ();
103
102
if ((i <0 )||(i >=results .count ())) return null ;
104
103
return results .get (i );
105
104
}
@@ -121,17 +120,9 @@ public ACell get(Keyword key) {
121
120
return null ;
122
121
}
123
122
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
-
131
123
@ Override
132
124
public void validateCell () throws InvalidDataException {
133
125
// TODO Auto-generated method stub
134
-
135
126
}
136
127
137
128
@ Override
@@ -142,30 +133,11 @@ public void validate() throws InvalidDataException {
142
133
143
134
long n =results .count ();
144
135
for (long i =0 ; i <n ; i ++) {
145
- Object r =results .get (i );
136
+ ACell r =results .get (i );
146
137
if (!(r instanceof Result )) throw new InvalidDataException ("Not a Result at position " +i +" - found " +Utils .getClassName (r ),this );
147
138
}
148
139
}
149
140
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
-
169
141
/**
170
142
* Decodes a BlockResult from a Blob
171
143
* @param b Blob to read from
@@ -174,27 +146,21 @@ public int estimatedEncodingSize() {
174
146
* @throws BadFormatException If encoding format has errors
175
147
*/
176
148
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 ();
178
151
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 );
186
153
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 ));
189
156
return result ;
190
157
}
191
158
192
159
193
160
@ Override
194
161
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 );
198
164
}
199
165
200
166
/**
@@ -216,26 +182,6 @@ public boolean equals(BlockResult a) {
216
182
return true ;
217
183
}
218
184
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
-
239
185
/**
240
186
* Creates a BlockResult for an invalid Block (i.e. no peer)
241
187
* @param state State at time of creation
@@ -250,6 +196,12 @@ public static BlockResult createInvalidBlock(State state, Block block, AString m
250
196
return new BlockResult (state ,rs );
251
197
}
252
198
199
+ @ Override
200
+ protected ARecordGeneric withValues (AVector <ACell > newValues ) {
201
+ if (values ==newValues ) return this ;
202
+ return new BlockResult (newValues );
203
+ }
204
+
253
205
254
206
255
207
0 commit comments