Skip to content

Commit 982e087

Browse files
committed
Refactor to include TransactionContext in Context execution
1 parent d9da6b7 commit 982e087

File tree

4 files changed

+69
-16
lines changed

4 files changed

+69
-16
lines changed

convex-core/src/main/java/convex/core/lang/Context.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import convex.core.data.AccountKey;
1717
import convex.core.data.AccountStatus;
1818
import convex.core.data.Address;
19-
import convex.core.data.Index;
2019
import convex.core.data.Hash;
20+
import convex.core.data.Index;
2121
import convex.core.data.Keyword;
2222
import convex.core.data.Keywords;
2323
import convex.core.data.MapEntry;
@@ -43,6 +43,7 @@
4343
import convex.core.lang.exception.RollbackValue;
4444
import convex.core.lang.exception.TailcallValue;
4545
import convex.core.lang.impl.CoreFn;
46+
import convex.core.lang.impl.TransactionContext;
4647
import convex.core.util.Economics;
4748
import convex.core.util.Errors;
4849
import convex.core.util.Utils;
@@ -141,6 +142,8 @@ public CVMLong getPosition(Symbol sym) {
141142
return mappings.get(sym);
142143
}
143144
}
145+
146+
144147

145148
/**
146149
* Immutable inner class for less-frequently changing CVM state
@@ -152,7 +155,7 @@ public CVMLong getPosition(Symbol sym) {
152155
*/
153156
protected static final class ChainState {
154157
private final State state;
155-
private final Address origin;
158+
private final TransactionContext txContext;
156159
private final Address caller;
157160
private final Address address;
158161
private final ACell scope;
@@ -163,30 +166,30 @@ protected static final class ChainState {
163166
*/
164167
private final AccountStatus account;
165168

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) {
167170
this.state=state;
168-
this.origin=origin;
171+
this.txContext=transactionContext;
169172
this.caller=caller;
170173
this.address=address;
171174
this.account=account;
172175
this.offer=offer;
173176
this.scope=scope;
174177
}
175178

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) {
177180
AccountStatus as=state.getAccount(address);
178181
if (as==null) return null;
179182
return new ChainState(state,origin,caller,address,as,offer,scope);
180183
}
181184

182185
public ChainState withStateOffer(State newState,long newOffer) {
183186
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);
185188
}
186189

187190
private ChainState withState(State newState) {
188191
if (state==newState) return this;
189-
return create(newState,origin,caller,address,offer,scope);
192+
return create(newState,txContext,caller,address,offer,scope);
190193
}
191194

192195
protected long getOffer() {
@@ -230,16 +233,25 @@ public AHashMap<Symbol, AHashMap<ACell, ACell>> getMetadata() {
230233

231234
public ChainState withScope(ACell newScope) {
232235
if (scope==newScope) return this;
233-
return create(state,origin,caller,address,offer,newScope);
236+
return create(state,txContext,caller,address,offer,newScope);
234237
}
235238

236239
public AccountStatus getAccount() {
237240
return account;
238241
}
239-
242+
243+
public Address getOrigin() {
244+
return txContext.origin;
245+
}
246+
240247
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();
243255
}
244256

245257
}
@@ -264,7 +276,8 @@ private static <T extends ACell> Context create(ChainState cs, long juice,long j
264276
}
265277

266278
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);
268281
if (chainState==null) throw new Error("Attempting to create context with invalid Address");
269282
return create(chainState,juice,juiceLimit,localBindings,result,depth,log,comp);
270283
}
@@ -1140,7 +1153,7 @@ public int getDepth() {
11401153
}
11411154

11421155
public Address getOrigin() {
1143-
return chainState.origin;
1156+
return chainState.getOrigin();
11441157
}
11451158

11461159
/**
@@ -1346,7 +1359,7 @@ public Context evalAs(Address target, ACell form) {
13461359
public Context queryAs(Address address, ACell form) {
13471360
// chainstate with the target address as origin.
13481361
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);
13501363
if (cs==null) return withError(ErrorCodes.NOBODY,"Address does not exist: "+address);
13511364
Context ctx=Context.create(cs, juice,juiceLimit, EMPTY_BINDINGS, NO_RESULT, depth,log,NO_COMPILER_STATE);
13521365
ctx=ctx.eval(form);
@@ -2343,4 +2356,13 @@ public AFn<ACell> lookupExpander(ACell form) {
23432356
return null;
23442357
}
23452358

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+
23462368
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package convex.core.lang.impl;
2+
3+
import convex.core.cpos.Block;
4+
import convex.core.cvm.State;
5+
import convex.core.data.AccountKey;
6+
import convex.core.data.Address;
7+
import convex.core.data.SignedData;
8+
import convex.core.transactions.ATransaction;
9+
10+
public final class TransactionContext {
11+
public SignedData<ATransaction> tx;
12+
public SignedData<Block> block;
13+
public Address origin;
14+
public State initialState;
15+
16+
17+
public static TransactionContext createQuery(State initialState, Address origin) {
18+
TransactionContext ctx=new TransactionContext();
19+
ctx.origin=origin;
20+
ctx.initialState=initialState;
21+
return ctx;
22+
}
23+
24+
public AccountKey getPeer() {
25+
if (block==null) return null;
26+
return block.getAccountKey();
27+
}
28+
}

convex-core/src/main/java/convex/core/lang/ops/Special.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public Context execute(Context ctx) {
137137
case S_NOP: break; // unchanged context, propagates *result*
138138
case S_MEMORY_PRICE: ctx=ctx.withResult(CVMDouble.create(ctx.getState().getMemoryPrice())); break ;
139139
case S_SIGNER: ctx=ctx.withResult(null); break; // TODO
140-
case S_PEER: ctx=ctx.withResult(null); break ; // TODO
140+
case S_PEER: ctx=ctx.withResult(ctx.getPeer()); break ; // TODO
141141

142142
default:
143143
throw new Error("Bad Opcode"+specialCode);

convex-core/src/test/java/convex/core/lang/CoreTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5130,7 +5130,10 @@ public void testSpecialKey() {
51305130
assertEquals(InitTest.HERO_KEYPAIR.getAccountKey(), eval("*key*"));
51315131
}
51325132

5133-
5133+
@Test
5134+
public void testSpecialPeer() {
5135+
assertNull(eval("*peer*"));
5136+
}
51345137

51355138
@Test
51365139
public void testSpecialJuice() {

0 commit comments

Comments
 (0)