Skip to content

Commit 96b0801

Browse files
committed
feat(jsonrpc): add blockTimestamp to logs and receipts
1 parent 039821c commit 96b0801

5 files changed

Lines changed: 21 additions & 4 deletions

File tree

framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,12 @@ class LogFilterElement {
461461
private final String[] topics;
462462
@Getter
463463
private final boolean removed;
464+
@Getter
465+
private final String blockTimestamp;
464466

465467
public LogFilterElement(String blockHash, Long blockNum, String txId, Integer txIndex,
466468
String contractAddress, List<DataWord> topicList, String logData, int logIdx,
467-
boolean removed) {
469+
boolean removed, Long blockTimestamp) {
468470
logIndex = ByteArray.toJsonHex(logIdx);
469471
this.blockNumber = blockNum == null ? null : ByteArray.toJsonHex(blockNum);
470472
this.blockHash = blockHash == null ? null : ByteArray.toJsonHex(blockHash);
@@ -477,6 +479,8 @@ public LogFilterElement(String blockHash, Long blockNum, String txId, Integer tx
477479
topics[i] = ByteArray.toJsonHex(topicList.get(i).getData());
478480
}
479481
this.removed = removed;
482+
this.blockTimestamp = blockTimestamp == null ? null
483+
: ByteArray.toJsonHex(blockTimestamp / 1000);
480484
}
481485

482486
@Override
@@ -500,12 +504,16 @@ public boolean equals(Object o) {
500504
if (!Objects.equals(logIndex, item.logIndex)) {
501505
return false;
502506
}
503-
return removed == item.removed;
507+
if (removed != item.removed) {
508+
return false;
509+
}
510+
return Objects.equals(blockTimestamp, item.blockTimestamp);
504511
}
505512

506513
@Override
507514
public int hashCode() {
508-
return Objects.hash(blockHash, transactionHash, transactionIndex, logIndex, removed);
515+
return Objects.hash(blockHash, transactionHash, transactionIndex,
516+
logIndex, removed, blockTimestamp);
509517
}
510518

511519
}

framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogMatch.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public static List<LogFilterElement> matchBlock(LogFilter logFilter, long blockN
6666
topicList,
6767
ByteArray.toHexString(log.getData().toByteArray()),
6868
logIndexInBlock,
69-
removed
69+
removed,
70+
transactionInfo.getBlockTimeStamp()
7071
);
7172
matchedLog.add(logFilterElement);
7273
}

framework/src/main/java/org/tron/core/services/jsonrpc/types/TransactionReceipt.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static class TransactionLog {
3535
private String data;
3636
private String[] topics;
3737
private boolean removed = false;
38+
private String blockTimestamp;
3839

3940
public TransactionLog() {}
4041
}
@@ -108,6 +109,7 @@ public TransactionReceipt(
108109

109110
// Set logs
110111
List<TransactionLog> logList = new ArrayList<>();
112+
String blockTimestamp = ByteArray.toJsonHex(txInfo.getBlockTimeStamp() / 1000);
111113
for (int logIndex = 0; logIndex < txInfo.getLogCount(); logIndex++) {
112114
TransactionInfo.Log log = txInfo.getLogList().get(logIndex);
113115
TransactionLog transactionLog = new TransactionLog();
@@ -116,6 +118,7 @@ public TransactionReceipt(
116118
transactionLog.setTransactionIndex(this.transactionIndex);
117119
transactionLog.setBlockHash(this.blockHash);
118120
transactionLog.setBlockNumber(this.blockNumber);
121+
transactionLog.setBlockTimestamp(blockTimestamp);
119122

120123
byte[] addressByte = convertToTronAddress(log.getAddress().toByteArray());
121124
transactionLog.setAddress(ByteArray.toJsonHexAddress(addressByte));

framework/src/test/java/org/tron/core/jsonrpc/LogMatchExactlyTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ private TransactionInfo createTransactionInfo(byte[] address, byte[][] topicArra
3737
LogInfo logInfo = new LogInfo(address, topics, data);
3838
logList.add(LogInfo.buildLog(logInfo));
3939
builder.addAllLog(logList);
40+
builder.setBlockTimeStamp(1000000L);
4041

4142
return builder.build();
4243
}
@@ -230,6 +231,8 @@ public void testMatchBlock() {
230231
LogFilterElement logFilterElement1 = elementList.get(0);
231232
LogFilterElement logFilterElement2 = elementList2.get(0);
232233

234+
Assert.assertEquals("0x3e8", logFilterElement1.getBlockTimestamp());
235+
Assert.assertEquals("0x3e8", logFilterElement2.getBlockTimestamp());
233236
Assert.assertEquals(logFilterElement1.hashCode(), logFilterElement2.hashCode());
234237
Assert.assertEquals(logFilterElement1, logFilterElement2);
235238

framework/src/test/java/org/tron/core/services/jsonrpc/TransactionReceiptTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public void testTransactionReceipt() throws JsonRpcInternalException {
3232
Protocol.TransactionInfo transactionInfo = Protocol.TransactionInfo.newBuilder()
3333
.setId(ByteString.copyFrom("1".getBytes()))
3434
.setContractAddress(ByteString.copyFrom("address1".getBytes()))
35+
.setBlockTimeStamp(1000000L)
3536
.setReceipt(Protocol.ResourceReceipt.newBuilder()
3637
.setEnergyUsageTotal(100L)
3738
.setResult(Protocol.Transaction.Result.contractResult.DEFAULT)
@@ -90,6 +91,7 @@ public void testTransactionReceipt() throws JsonRpcInternalException {
9091
Assert.assertEquals(transactionReceipt.getLogs()[0].getBlockNumber(), "0x1");
9192
Assert.assertEquals(transactionReceipt.getLogs()[0].getTransactionHash(), "0x31");
9293
Assert.assertEquals(transactionReceipt.getLogs()[0].getTransactionIndex(), "0x0");
94+
Assert.assertEquals(transactionReceipt.getLogs()[0].getBlockTimestamp(), "0x3e8");
9395

9496
// assert default fields
9597
Assert.assertNull(transactionReceipt.getRoot());

0 commit comments

Comments
 (0)