Skip to content

Commit 22e0aa3

Browse files
authored
feat(jsonrpc): add blockTimestamp to logs and receipts (tronprotocol#6671)
1 parent 314f130 commit 22e0aa3

6 files changed

Lines changed: 79 additions & 29 deletions

File tree

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

Lines changed: 10 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 blockTimestampMs) {
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,7 @@ 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 = ByteArray.toJsonHex(blockTimestampMs / 1000);
480483
}
481484

482485
@Override
@@ -500,12 +503,16 @@ public boolean equals(Object o) {
500503
if (!Objects.equals(logIndex, item.logIndex)) {
501504
return false;
502505
}
503-
return removed == item.removed;
506+
if (removed != item.removed) {
507+
return false;
508+
}
509+
return Objects.equals(blockTimestamp, item.blockTimestamp);
504510
}
505511

506512
@Override
507513
public int hashCode() {
508-
return Objects.hash(blockHash, transactionHash, transactionIndex, logIndex, removed);
514+
return Objects.hash(blockHash, transactionHash, transactionIndex,
515+
logIndex, removed, blockTimestamp);
509516
}
510517

511518
}

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(blockCapsule.getTimeStamp() / 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/JsonrpcServiceTest.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ public void init() {
116116
blockCapsule0 = BlockUtil.newGenesisBlockCapsule();
117117
blockCapsule1 = new BlockCapsule(LATEST_BLOCK_NUM, Sha256Hash.wrap(ByteString.copyFrom(
118118
ByteArray.fromHexString(
119-
"0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))), 1,
119+
"0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))), 1000000,
120120
ByteString.copyFromUtf8("testAddress"));
121121
blockCapsule2 = new BlockCapsule(LATEST_SOLIDIFIED_BLOCK_NUM, Sha256Hash.wrap(
122122
ByteString.copyFrom(ByteArray.fromHexString(
123-
"9938a342238077182498b464ac029222ae169360e540d1fd6aee7c2ae9575a06"))), 1,
123+
"9938a342238077182498b464ac029222ae169360e540d1fd6aee7c2ae9575a06"))), 2000000,
124124
ByteString.copyFromUtf8("testAddress"));
125125

126126
TransferContract transferContract1 = TransferContract.newBuilder().setAmount(1L)
@@ -142,13 +142,15 @@ public void init() {
142142

143143
transactionCapsule1 = new TransactionCapsule(transferContract1, ContractType.TransferContract);
144144
transactionCapsule1.setBlockNum(blockCapsule1.getNum());
145+
transactionCapsule1.setTimestamp(blockCapsule1.getTimeStamp());
145146
TransactionCapsule transactionCapsule2 = new TransactionCapsule(transferContract2,
146147
ContractType.TransferContract);
147148
transactionCapsule2.setBlockNum(blockCapsule1.getNum());
149+
transactionCapsule2.setTimestamp(blockCapsule1.getTimeStamp());
148150
TransactionCapsule transactionCapsule3 = new TransactionCapsule(transferContract3,
149151
ContractType.TransferContract);
150152
transactionCapsule3.setBlockNum(blockCapsule2.getNum());
151-
153+
transactionCapsule3.setTimestamp(blockCapsule2.getTimeStamp());
152154
blockCapsule1.addTransaction(transactionCapsule1);
153155
blockCapsule1.addTransaction(transactionCapsule2);
154156
blockCapsule2.addTransaction(transactionCapsule3);
@@ -188,6 +190,7 @@ public void init() {
188190
TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule();
189191
transactionInfoCapsule.setId(tx.getTransactionId().getBytes());
190192
transactionInfoCapsule.setBlockNumber(blockCapsule1.getNum());
193+
transactionInfoCapsule.setBlockTimeStamp(blockCapsule1.getTimeStamp());
191194
transactionInfoCapsule.addAllLog(logs);
192195
transactionRetCapsule1.addTransactionInfo(transactionInfoCapsule.getInstance());
193196
});
@@ -199,6 +202,7 @@ public void init() {
199202
TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule();
200203
transactionInfoCapsule.setId(tx.getTransactionId().getBytes());
201204
transactionInfoCapsule.setBlockNumber(blockCapsule2.getNum());
205+
transactionInfoCapsule.setBlockTimeStamp(blockCapsule2.getTimeStamp());
202206
transactionRetCapsule2.addTransactionInfo(transactionInfoCapsule.getInstance());
203207
});
204208
dbManager.getTransactionRetStore()
@@ -1112,6 +1116,30 @@ public void testMethodBlockRange() {
11121116
}
11131117
}
11141118

1119+
@Test
1120+
public void testGetLogs() {
1121+
try {
1122+
LogFilterElement[] logs = tronJsonRpc.getLogs(
1123+
new FilterRequest("0x2710", "0x2710", null, null, null));
1124+
Assert.assertTrue(logs.length > 0);
1125+
LogFilterElement log = logs[0];
1126+
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getNum()), log.getBlockNumber());
1127+
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getBlockId().toString()),
1128+
log.getBlockHash());
1129+
Assert.assertEquals("0x0", log.getLogIndex());
1130+
Assert.assertFalse(log.isRemoved());
1131+
Assert.assertEquals(1, log.getTopics().length);
1132+
Assert.assertEquals(
1133+
"0x0000000000000000000000000000000000000000000000000000746f70696331",
1134+
log.getTopics()[0]);
1135+
Assert.assertEquals(ByteArray.toJsonHex("data1".getBytes()), log.getData());
1136+
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getTimeStamp() / 1000),
1137+
log.getBlockTimestamp());
1138+
} catch (Exception e) {
1139+
Assert.fail();
1140+
}
1141+
}
1142+
11151143
@Test
11161144
public void testNewFilterFinalizedBlock() {
11171145

@@ -1239,6 +1267,10 @@ public void testGetBlockReceipts() {
12391267

12401268
Assert.assertEquals(
12411269
JSON.toJSONString(transactionReceipt), JSON.toJSONString(transactionReceipt1));
1270+
1271+
Assert.assertTrue(transactionReceipt1.getLogs().length > 0);
1272+
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getTimeStamp() / 1000),
1273+
transactionReceipt1.getLogs()[0].getBlockTimestamp());
12421274
}
12431275
} catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) {
12441276
throw new RuntimeException(e);

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: 26 additions & 22 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)
@@ -53,8 +54,11 @@ public void testTransactionReceipt() throws JsonRpcInternalException {
5354

5455
Protocol.Block block = Protocol.Block.newBuilder().setBlockHeader(
5556
Protocol.BlockHeader.newBuilder().setRawData(
56-
Protocol.BlockHeader.raw.newBuilder().setNumber(1))).addTransactions(
57-
transaction).build();
57+
Protocol.BlockHeader.raw.newBuilder()
58+
.setNumber(1)
59+
.setTimestamp(1000000L)))
60+
.addTransactions(transaction)
61+
.build();
5862

5963
BlockCapsule blockCapsule = new BlockCapsule(block);
6064
long energyFee = wallet.getEnergyFee(blockCapsule.getTimeStamp());
@@ -65,35 +69,35 @@ public void testTransactionReceipt() throws JsonRpcInternalException {
6569
new TransactionReceipt(blockCapsule, transactionInfo, context, energyFee);
6670

6771
Assert.assertNotNull(transactionReceipt);
68-
String blockHash = "0x0000000000000001464f071c8a336fd22eb5145dff1b245bda013ec89add8497";
72+
String blockHash = "0x0000000000000001ba51f50f562758a449ff4a98df4febef89e122c1bb7e1a0c";
6973

7074
// assert basic fields
71-
Assert.assertEquals(transactionReceipt.getBlockHash(), blockHash);
72-
Assert.assertEquals(transactionReceipt.getBlockNumber(), "0x1");
73-
Assert.assertEquals(transactionReceipt.getTransactionHash(), "0x31");
74-
Assert.assertEquals(transactionReceipt.getTransactionIndex(), "0x0");
75-
Assert.assertEquals(transactionReceipt.getCumulativeGasUsed(), ByteArray.toJsonHex(102));
76-
Assert.assertEquals(transactionReceipt.getGasUsed(), ByteArray.toJsonHex(100));
77-
Assert.assertEquals(transactionReceipt.getEffectiveGasPrice(), ByteArray.toJsonHex(energyFee));
78-
Assert.assertEquals(transactionReceipt.getStatus(), "0x1");
75+
Assert.assertEquals(blockHash, transactionReceipt.getBlockHash());
76+
Assert.assertEquals("0x1", transactionReceipt.getBlockNumber());
77+
Assert.assertEquals("0x31", transactionReceipt.getTransactionHash());
78+
Assert.assertEquals("0x0", transactionReceipt.getTransactionIndex());
79+
Assert.assertEquals(ByteArray.toJsonHex(102), transactionReceipt.getCumulativeGasUsed());
80+
Assert.assertEquals(ByteArray.toJsonHex(100), transactionReceipt.getGasUsed());
81+
Assert.assertEquals(ByteArray.toJsonHex(energyFee), transactionReceipt.getEffectiveGasPrice());
82+
Assert.assertEquals("0x1", transactionReceipt.getStatus());
7983

8084
// assert contract fields
81-
Assert.assertEquals(transactionReceipt.getFrom(), ByteArray.toJsonHexAddress(new byte[0]));
82-
Assert.assertEquals(transactionReceipt.getTo(), ByteArray.toJsonHexAddress(new byte[0]));
85+
Assert.assertEquals(ByteArray.toJsonHexAddress(new byte[0]), transactionReceipt.getFrom());
86+
Assert.assertEquals(ByteArray.toJsonHexAddress(new byte[0]), transactionReceipt.getTo());
8387
Assert.assertNull(transactionReceipt.getContractAddress());
8488

8589
// assert logs fields
86-
Assert.assertEquals(transactionReceipt.getLogs().length, 1);
87-
Assert.assertEquals(transactionReceipt.getLogs()[0].getLogIndex(), "0x3");
88-
Assert.assertEquals(
89-
transactionReceipt.getLogs()[0].getBlockHash(), blockHash);
90-
Assert.assertEquals(transactionReceipt.getLogs()[0].getBlockNumber(), "0x1");
91-
Assert.assertEquals(transactionReceipt.getLogs()[0].getTransactionHash(), "0x31");
92-
Assert.assertEquals(transactionReceipt.getLogs()[0].getTransactionIndex(), "0x0");
90+
Assert.assertEquals(1, transactionReceipt.getLogs().length);
91+
Assert.assertEquals("0x3", transactionReceipt.getLogs()[0].getLogIndex());
92+
Assert.assertEquals(blockHash, transactionReceipt.getLogs()[0].getBlockHash());
93+
Assert.assertEquals("0x1", transactionReceipt.getLogs()[0].getBlockNumber());
94+
Assert.assertEquals("0x31", transactionReceipt.getLogs()[0].getTransactionHash());
95+
Assert.assertEquals("0x0", transactionReceipt.getLogs()[0].getTransactionIndex());
96+
Assert.assertEquals("0x3e8", transactionReceipt.getLogs()[0].getBlockTimestamp());
9397

9498
// assert default fields
9599
Assert.assertNull(transactionReceipt.getRoot());
96-
Assert.assertEquals(transactionReceipt.getType(), "0x0");
97-
Assert.assertEquals(transactionReceipt.getLogsBloom(), ByteArray.toJsonHex(new byte[256]));
100+
Assert.assertEquals("0x0", transactionReceipt.getType());
101+
Assert.assertEquals(ByteArray.toJsonHex(new byte[256]), transactionReceipt.getLogsBloom());
98102
}
99103
}

0 commit comments

Comments
 (0)