Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,12 @@ class LogFilterElement {
private final String[] topics;
@Getter
private final boolean removed;
@Getter
private final String blockTimestamp;

public LogFilterElement(String blockHash, Long blockNum, String txId, Integer txIndex,
String contractAddress, List<DataWord> topicList, String logData, int logIdx,
boolean removed) {
boolean removed, Long blockTimestamp) {
logIndex = ByteArray.toJsonHex(logIdx);
this.blockNumber = blockNum == null ? null : ByteArray.toJsonHex(blockNum);
this.blockHash = blockHash == null ? null : ByteArray.toJsonHex(blockHash);
Expand All @@ -477,6 +479,8 @@ public LogFilterElement(String blockHash, Long blockNum, String txId, Integer tx
topics[i] = ByteArray.toJsonHex(topicList.get(i).getData());
}
this.removed = removed;
this.blockTimestamp = blockTimestamp == null ? null
: ByteArray.toJsonHex(blockTimestamp / 1000);
}

@Override
Expand All @@ -500,12 +504,16 @@ public boolean equals(Object o) {
if (!Objects.equals(logIndex, item.logIndex)) {
return false;
}
return removed == item.removed;
if (removed != item.removed) {
return false;
}
return Objects.equals(blockTimestamp, item.blockTimestamp);
}

@Override
public int hashCode() {
return Objects.hash(blockHash, transactionHash, transactionIndex, logIndex, removed);
return Objects.hash(blockHash, transactionHash, transactionIndex,
logIndex, removed, blockTimestamp);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public static List<LogFilterElement> matchBlock(LogFilter logFilter, long blockN
topicList,
ByteArray.toHexString(log.getData().toByteArray()),
logIndexInBlock,
removed
removed,
transactionInfo.getBlockTimeStamp()
);
matchedLog.add(logFilterElement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class TransactionLog {
private String data;
private String[] topics;
private boolean removed = false;
private String blockTimestamp;

public TransactionLog() {}
}
Expand Down Expand Up @@ -108,6 +109,7 @@ public TransactionReceipt(

// Set logs
List<TransactionLog> logList = new ArrayList<>();
String blockTimestamp = ByteArray.toJsonHex(blockCapsule.getTimeStamp() / 1000);
for (int logIndex = 0; logIndex < txInfo.getLogCount(); logIndex++) {
TransactionInfo.Log log = txInfo.getLogList().get(logIndex);
TransactionLog transactionLog = new TransactionLog();
Expand All @@ -116,6 +118,7 @@ public TransactionReceipt(
transactionLog.setTransactionIndex(this.transactionIndex);
transactionLog.setBlockHash(this.blockHash);
transactionLog.setBlockNumber(this.blockNumber);
transactionLog.setBlockTimestamp(blockTimestamp);

byte[] addressByte = convertToTronAddress(log.getAddress().toByteArray());
transactionLog.setAddress(ByteArray.toJsonHexAddress(addressByte));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.tron.core.services.interfaceJsonRpcOnSolidity.JsonRpcServiceOnSolidity;
import org.tron.core.services.jsonrpc.FullNodeJsonRpcHttpService;
import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest;
import org.tron.core.services.jsonrpc.TronJsonRpc.LogFilterElement;
import org.tron.core.services.jsonrpc.TronJsonRpcImpl;
import org.tron.core.services.jsonrpc.filters.LogFilterWrapper;
import org.tron.core.services.jsonrpc.types.BlockResult;
Expand Down Expand Up @@ -109,11 +110,11 @@ public void init() {
blockCapsule0 = BlockUtil.newGenesisBlockCapsule();
blockCapsule1 = new BlockCapsule(LATEST_BLOCK_NUM, Sha256Hash.wrap(ByteString.copyFrom(
ByteArray.fromHexString(
"0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))), 1,
"0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))), 1000000,
ByteString.copyFromUtf8("testAddress"));
blockCapsule2 = new BlockCapsule(LATEST_SOLIDIFIED_BLOCK_NUM, Sha256Hash.wrap(
ByteString.copyFrom(ByteArray.fromHexString(
"9938a342238077182498b464ac029222ae169360e540d1fd6aee7c2ae9575a06"))), 1,
"9938a342238077182498b464ac029222ae169360e540d1fd6aee7c2ae9575a06"))), 2000000,
ByteString.copyFromUtf8("testAddress"));

TransferContract transferContract1 = TransferContract.newBuilder().setAmount(1L)
Expand All @@ -135,13 +136,15 @@ public void init() {

transactionCapsule1 = new TransactionCapsule(transferContract1, ContractType.TransferContract);
transactionCapsule1.setBlockNum(blockCapsule1.getNum());
transactionCapsule1.setTimestamp(blockCapsule1.getTimeStamp());
TransactionCapsule transactionCapsule2 = new TransactionCapsule(transferContract2,
ContractType.TransferContract);
transactionCapsule2.setBlockNum(blockCapsule1.getNum());
transactionCapsule2.setTimestamp(blockCapsule1.getTimeStamp());
TransactionCapsule transactionCapsule3 = new TransactionCapsule(transferContract3,
ContractType.TransferContract);
transactionCapsule3.setBlockNum(blockCapsule2.getNum());

transactionCapsule3.setTimestamp(blockCapsule2.getTimeStamp());
blockCapsule1.addTransaction(transactionCapsule1);
blockCapsule1.addTransaction(transactionCapsule2);
blockCapsule2.addTransaction(transactionCapsule3);
Expand Down Expand Up @@ -181,6 +184,7 @@ public void init() {
TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule();
transactionInfoCapsule.setId(tx.getTransactionId().getBytes());
transactionInfoCapsule.setBlockNumber(blockCapsule1.getNum());
transactionInfoCapsule.setBlockTimeStamp(blockCapsule1.getTimeStamp());
transactionInfoCapsule.addAllLog(logs);
transactionRetCapsule1.addTransactionInfo(transactionInfoCapsule.getInstance());
});
Expand All @@ -192,6 +196,7 @@ public void init() {
TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule();
transactionInfoCapsule.setId(tx.getTransactionId().getBytes());
transactionInfoCapsule.setBlockNumber(blockCapsule2.getNum());
transactionInfoCapsule.setBlockTimeStamp(blockCapsule2.getTimeStamp());
transactionRetCapsule2.addTransactionInfo(transactionInfoCapsule.getInstance());
});
dbManager.getTransactionRetStore()
Expand Down Expand Up @@ -969,6 +974,30 @@ public void testMethodBlockRange() {
}
}

@Test
public void testGetLogs() {
try {
LogFilterElement[] logs = tronJsonRpc.getLogs(
new FilterRequest("0x2710", "0x2710", null, null, null));
Assert.assertTrue(logs.length > 0);
LogFilterElement log = logs[0];
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getNum()), log.getBlockNumber());
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getBlockId().toString()),
log.getBlockHash());
Assert.assertEquals("0x0", log.getLogIndex());
Assert.assertFalse(log.isRemoved());
Assert.assertEquals(1, log.getTopics().length);
Assert.assertEquals(
"0x0000000000000000000000000000000000000000000000000000746f70696331",
log.getTopics()[0]);
Assert.assertEquals(ByteArray.toJsonHex("data1".getBytes()), log.getData());
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getTimeStamp() / 1000),
log.getBlockTimestamp());
} catch (Exception e) {
Assert.fail();
}
}

@Test
public void testNewFilterFinalizedBlock() {

Expand Down Expand Up @@ -1026,6 +1055,10 @@ public void testGetBlockReceipts() {

Assert.assertEquals(
JSON.toJSONString(transactionReceipt), JSON.toJSONString(transactionReceipt1));

Assert.assertTrue(transactionReceipt1.getLogs().length > 0);
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getTimeStamp() / 1000),
transactionReceipt1.getLogs()[0].getBlockTimestamp());
}
} catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private TransactionInfo createTransactionInfo(byte[] address, byte[][] topicArra
LogInfo logInfo = new LogInfo(address, topics, data);
logList.add(LogInfo.buildLog(logInfo));
builder.addAllLog(logList);
builder.setBlockTimeStamp(1000000L);

return builder.build();
}
Expand Down Expand Up @@ -230,6 +231,8 @@ public void testMatchBlock() {
LogFilterElement logFilterElement1 = elementList.get(0);
LogFilterElement logFilterElement2 = elementList2.get(0);

Assert.assertEquals("0x3e8", logFilterElement1.getBlockTimestamp());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right kind of value assertion, but it only verifies the construction helper.

Since the compatibility change is on the external JSON-RPC response model, we should also add endpoint-level coverage for eth_getLogs, eth_getFilterLogs, and log-type eth_getFilterChanges to prove that the new field is actually present on the API boundary, not just on the intermediate object.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. The existing getLogs-related tests in JsonrpcServiceTest only covered error paths (block range exceeded, max topics exceeded) without any positive-path field validation. I've added a new testGetLogs that covers the positive path for eth_getLogs, including field-level assertions for blockNumber, blockHash, logIndex, removed, topics,data, and blockTimestamp.
As for eth_getFilterLogs and eth_getFilterChanges, they share the same underlying
getLogsByLogFilterWrapper()LogMatch.matchBlock() path with eth_getLogs, differing only in how the filter is sourced. testGetLogs already covers the shared code path, so no additional tests are needed.

Assert.assertEquals("0x3e8", logFilterElement2.getBlockTimestamp());
Assert.assertEquals(logFilterElement1.hashCode(), logFilterElement2.hashCode());
Assert.assertEquals(logFilterElement1, logFilterElement2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void testTransactionReceipt() throws JsonRpcInternalException {
Protocol.TransactionInfo transactionInfo = Protocol.TransactionInfo.newBuilder()
.setId(ByteString.copyFrom("1".getBytes()))
.setContractAddress(ByteString.copyFrom("address1".getBytes()))
.setBlockTimeStamp(1000000L)
.setReceipt(Protocol.ResourceReceipt.newBuilder()
.setEnergyUsageTotal(100L)
.setResult(Protocol.Transaction.Result.contractResult.DEFAULT)
Expand Down Expand Up @@ -90,6 +91,7 @@ public void testTransactionReceipt() throws JsonRpcInternalException {
Assert.assertEquals(transactionReceipt.getLogs()[0].getBlockNumber(), "0x1");
Assert.assertEquals(transactionReceipt.getLogs()[0].getTransactionHash(), "0x31");
Assert.assertEquals(transactionReceipt.getLogs()[0].getTransactionIndex(), "0x0");
Assert.assertEquals(transactionReceipt.getLogs()[0].getBlockTimestamp(), "0x3e8");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to assert the concrete value here.

However, this is still a constructor-level unit test, while the feature request is specifically about the public JSON-RPC response shape. Please also add an API-facing assertion in JsonrpcServiceTest that eth_getTransactionReceipt and eth_getBlockReceipts expose blockTimestamp with the expected hex-seconds value on the serialized response. The existing receipt equality check is useful, but it can still miss a false positive if both paths shape the same incomplete object.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I've added an explicit blockTimestamp assertion in testGetBlockReceipts, verifying the field value from both eth_getBlockReceipts and eth_getTransactionReceipt responses.


// assert default fields
Assert.assertNull(transactionReceipt.getRoot());
Expand Down
Loading