16
16
import convex .core .data .AccountKey ;
17
17
import convex .core .data .AccountStatus ;
18
18
import convex .core .data .Address ;
19
- import convex .core .data .Index ;
20
19
import convex .core .data .Hash ;
20
+ import convex .core .data .Index ;
21
21
import convex .core .data .Keyword ;
22
22
import convex .core .data .Keywords ;
23
23
import convex .core .data .MapEntry ;
43
43
import convex .core .lang .exception .RollbackValue ;
44
44
import convex .core .lang .exception .TailcallValue ;
45
45
import convex .core .lang .impl .CoreFn ;
46
+ import convex .core .lang .impl .TransactionContext ;
46
47
import convex .core .util .Economics ;
47
48
import convex .core .util .Errors ;
48
49
import convex .core .util .Utils ;
@@ -141,6 +142,8 @@ public CVMLong getPosition(Symbol sym) {
141
142
return mappings .get (sym );
142
143
}
143
144
}
145
+
146
+
144
147
145
148
/**
146
149
* Immutable inner class for less-frequently changing CVM state
@@ -152,7 +155,7 @@ public CVMLong getPosition(Symbol sym) {
152
155
*/
153
156
protected static final class ChainState {
154
157
private final State state ;
155
- private final Address origin ;
158
+ private final TransactionContext txContext ;
156
159
private final Address caller ;
157
160
private final Address address ;
158
161
private final ACell scope ;
@@ -163,30 +166,30 @@ protected static final class ChainState {
163
166
*/
164
167
private final AccountStatus account ;
165
168
166
- private ChainState (State state , Address origin ,Address caller , Address address ,AccountStatus account , long offer ,ACell scope ) {
169
+ private ChainState (State state , TransactionContext transactionContext ,Address caller , Address address ,AccountStatus account , long offer ,ACell scope ) {
167
170
this .state =state ;
168
- this .origin = origin ;
171
+ this .txContext = transactionContext ;
169
172
this .caller =caller ;
170
173
this .address =address ;
171
174
this .account =account ;
172
175
this .offer =offer ;
173
176
this .scope =scope ;
174
177
}
175
178
176
- public static ChainState create (State state , Address origin , Address caller , Address address , long offer , ACell scope ) {
179
+ public static ChainState create (State state , TransactionContext origin , Address caller , Address address , long offer , ACell scope ) {
177
180
AccountStatus as =state .getAccount (address );
178
181
if (as ==null ) return null ;
179
182
return new ChainState (state ,origin ,caller ,address ,as ,offer ,scope );
180
183
}
181
184
182
185
public ChainState withStateOffer (State newState ,long newOffer ) {
183
186
if ((state ==newState )&&(offer ==newOffer )) return this ;
184
- return create (newState ,origin ,caller ,address ,newOffer ,scope );
187
+ return create (newState ,txContext ,caller ,address ,newOffer ,scope );
185
188
}
186
189
187
190
private ChainState withState (State newState ) {
188
191
if (state ==newState ) return this ;
189
- return create (newState ,origin ,caller ,address ,offer ,scope );
192
+ return create (newState ,txContext ,caller ,address ,offer ,scope );
190
193
}
191
194
192
195
protected long getOffer () {
@@ -230,16 +233,25 @@ public AHashMap<Symbol, AHashMap<ACell, ACell>> getMetadata() {
230
233
231
234
public ChainState withScope (ACell newScope ) {
232
235
if (scope ==newScope ) return this ;
233
- return create (state ,origin ,caller ,address ,offer ,newScope );
236
+ return create (state ,txContext ,caller ,address ,offer ,newScope );
234
237
}
235
238
236
239
public AccountStatus getAccount () {
237
240
return account ;
238
241
}
239
-
242
+
243
+ public Address getOrigin () {
244
+ return txContext .origin ;
245
+ }
246
+
240
247
public AccountStatus getOriginAccount () {
241
- if (address .equals (origin )) return account ;
242
- return state .getAccount (origin );
248
+ Address o =getOrigin ();
249
+ if (address .equals (o )) return account ;
250
+ return state .getAccount (o );
251
+ }
252
+
253
+ public AccountKey getPeer () {
254
+ return txContext .getPeer ();
243
255
}
244
256
245
257
}
@@ -264,7 +276,8 @@ private static <T extends ACell> Context create(ChainState cs, long juice,long j
264
276
}
265
277
266
278
private static <T extends ACell > Context create (State state , long juice ,long juiceLimit ,AVector <ACell > localBindings , T result , int depth , Address origin ,Address caller , Address address , long offer , AVector <AVector <ACell >> log , CompilerState comp ) {
267
- ChainState chainState =ChainState .create (state ,origin ,caller ,address ,offer ,NULL_SCOPE );
279
+ TransactionContext tctx =TransactionContext .createQuery (state , origin );
280
+ ChainState chainState =ChainState .create (state ,tctx ,caller ,address ,offer ,NULL_SCOPE );
268
281
if (chainState ==null ) throw new Error ("Attempting to create context with invalid Address" );
269
282
return create (chainState ,juice ,juiceLimit ,localBindings ,result ,depth ,log ,comp );
270
283
}
@@ -1140,7 +1153,7 @@ public int getDepth() {
1140
1153
}
1141
1154
1142
1155
public Address getOrigin () {
1143
- return chainState .origin ;
1156
+ return chainState .getOrigin () ;
1144
1157
}
1145
1158
1146
1159
/**
@@ -1346,7 +1359,7 @@ public Context evalAs(Address target, ACell form) {
1346
1359
public Context queryAs (Address address , ACell form ) {
1347
1360
// chainstate with the target address as origin.
1348
1361
State s =getState ();
1349
- ChainState cs =ChainState .create (s ,getOrigin () ,getAddress (),address ,ZERO_OFFER ,NULL_SCOPE );
1362
+ ChainState cs =ChainState .create (s ,chainState . txContext ,getAddress (),address ,ZERO_OFFER ,NULL_SCOPE );
1350
1363
if (cs ==null ) return withError (ErrorCodes .NOBODY ,"Address does not exist: " +address );
1351
1364
Context ctx =Context .create (cs , juice ,juiceLimit , EMPTY_BINDINGS , NO_RESULT , depth ,log ,NO_COMPILER_STATE );
1352
1365
ctx =ctx .eval (form );
@@ -2343,4 +2356,13 @@ public AFn<ACell> lookupExpander(ACell form) {
2343
2356
return null ;
2344
2357
}
2345
2358
2359
+ /**
2360
+ * Gets the peer responsible for the current block
2361
+ * @return Peer key, or null if outside a peer created block
2362
+ */
2363
+ public AccountKey getPeer () {
2364
+ // TODO Auto-generated method stub
2365
+ return chainState .getPeer ();
2366
+ }
2367
+
2346
2368
}
0 commit comments