Skip to content

Commit

Permalink
Add Result extra info for transaction receipts
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Mar 20, 2024
1 parent c1535e2 commit d9a8c33
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
19 changes: 19 additions & 0 deletions convex-core/src/main/java/convex/core/Result.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package convex.core;

import java.util.Map;
import java.util.concurrent.TimeoutException;

import convex.core.data.ACell;
Expand Down Expand Up @@ -44,6 +45,7 @@ public final class Result extends ARecordGeneric {

private static final RecordFormat RESULT_FORMAT=RecordFormat.of(Keywords.ID,Keywords.RESULT,Keywords.ERROR,Keywords.INFO);
private static final long FIELD_COUNT=RESULT_FORMAT.count();
private static final long INFO_POS=RESULT_FORMAT.indexFor(Keywords.INFO);

private Result(AVector<ACell> values) {
super(RESULT_FORMAT, values);
Expand Down Expand Up @@ -225,6 +227,7 @@ public boolean isError() {
* @param ctx Context
* @return New Result instance
*/

public static Result fromContext(CVMLong id,ResultContext rc) {
Context ctx=rc.context;
Object result=ctx.getValue();
Expand All @@ -246,9 +249,25 @@ public static Result fromContext(CVMLong id,ResultContext rc) {
if (rc.memUsed>0) info=info.assoc(Keywords.MEM, CVMLong.create(rc.memUsed));
if (rc.totalFees>0) info=info.assoc(Keywords.FEES, CVMLong.create(rc.totalFees));
if (rc.juiceUsed>0) info=info.assoc(Keywords.JUICE, CVMLong.create(rc.juiceUsed));



return create(id,(ACell)result,errorCode,info);
}

public Result withExtraInfo(Map<Keyword,ACell> extInfo) {
if ((extInfo!=null)&&(!extInfo.isEmpty())) {
AMap<Keyword,ACell> info=getInfo();
if (info==null) info=Maps.empty();
for (Map.Entry<Keyword,ACell> me: extInfo.entrySet()) {

info=info.assoc(me.getKey(), me.getValue());
}
return new Result(values.assoc(INFO_POS, info));
}
return this;
}

/**
* Constructs a Result from a Context. No ResultContext implies we are not in a top level transaction, so minimise work
* @param ctx Context
Expand Down
5 changes: 5 additions & 0 deletions convex-core/src/main/java/convex/core/data/Keywords.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class Keywords {

// Result keywords
public static final Keyword ID = Keyword.create("id");
public static final Keyword TX = Keyword.create("tx");
public static final Keyword LOC = Keyword.create("loc");
public static final Keyword RESULT = Keyword.create("result");
public static final Keyword ERROR = Keyword.create("error");
public static final Keyword ADDRESS = Keyword.create("address");
Expand All @@ -83,6 +85,9 @@ public class Keywords {
public static final Keyword MEM = Keyword.create("mem");
public static final Keyword FEES = Keyword.create("fees");
public static final Keyword JUICE = Keyword.create("juice");

public static final Keyword CVM = Keyword.create("cvm");


public static final Keyword EXPANDER_Q = Keyword.create("expander?");
public static final Keyword MACRO = Keyword.create("macro");
Expand Down
11 changes: 11 additions & 0 deletions convex-core/src/test/java/convex/core/ResultTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package convex.core;

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -30,6 +31,16 @@ public void testBasicResult() {

}

@Test
public void testExtraInfo() {
Result r1=Result.create(CVMLong.ZERO,Vectors.of(1,2,3));
Result r2=r1.withExtraInfo(Maps.of(Keywords.FOO,CVMLong.ONE));
assertNotEquals(r1,r2);

Result r3=r2.withExtraInfo(null);
assertSame(r2,r3);
}

@Test
public void testResultCreation() {
AHashMap<Keyword,ACell> info=Maps.of(Keywords.TRACE,Vectors.empty());
Expand Down
11 changes: 9 additions & 2 deletions convex-peer/src/main/java/convex/peer/TransactionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import convex.core.data.PeerStatus;
import convex.core.data.SignedData;
import convex.core.data.Strings;
import convex.core.data.Vectors;
import convex.core.data.prim.CVMLong;
import convex.core.lang.Reader;
import convex.core.transactions.ATransaction;
Expand Down Expand Up @@ -178,16 +179,17 @@ public void maybeReportTransactions(Peer peer) {
// only report our own transactions!
if (block.getAccountKey().equals(peer.getPeerKey())) {
BlockResult br = peer.getBlockResult(i);
reportTransactions(block.getValue(), br);
reportTransactions(block.getValue(), br,i);
}
}
reportedConsensusPoint=newConsensusPoint;
}
}

private void reportTransactions(Block block, BlockResult br) {
private void reportTransactions(Block block, BlockResult br, long blockNum) {
// TODO: consider culling old interests after some time period
int nTrans = block.length();
HashMap<Keyword,ACell> extInfo=new HashMap<>(5);
for (long j = 0; j < nTrans; j++) {
try {
SignedData<ATransaction> t = block.getTransactions().get(j);
Expand All @@ -197,6 +199,11 @@ private void reportTransactions(Block block, BlockResult br) {
ACell id = m.getID();
log.trace("Returning transaction result ID {} to {}", id,m.getOriginString());
Result res = br.getResults().get(j);

extInfo.put(Keywords.LOC,Vectors.of(blockNum,j));
extInfo.put(Keywords.TX,t.getHash());

res=res.withExtraInfo(extInfo);

boolean reported = m.reportResult(res);
if (!reported) {
Expand Down

0 comments on commit d9a8c33

Please sign in to comment.