Skip to content

Commit b3c6ccb

Browse files
committed
refactor(jsonrpc): use blockCapsule as timestamp source and add API-level tests
1 parent 96b0801 commit b3c6ccb

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public TransactionReceipt(
109109

110110
// Set logs
111111
List<TransactionLog> logList = new ArrayList<>();
112-
String blockTimestamp = ByteArray.toJsonHex(txInfo.getBlockTimeStamp() / 1000);
112+
String blockTimestamp = ByteArray.toJsonHex(blockCapsule.getTimeStamp() / 1000);
113113
for (int logIndex = 0; logIndex < txInfo.getLogCount(); logIndex++) {
114114
TransactionInfo.Log log = txInfo.getLogList().get(logIndex);
115115
TransactionLog transactionLog = new TransactionLog();

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.tron.core.services.interfaceJsonRpcOnSolidity.JsonRpcServiceOnSolidity;
4646
import org.tron.core.services.jsonrpc.FullNodeJsonRpcHttpService;
4747
import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest;
48+
import org.tron.core.services.jsonrpc.TronJsonRpc.LogFilterElement;
4849
import org.tron.core.services.jsonrpc.TronJsonRpcImpl;
4950
import org.tron.core.services.jsonrpc.filters.LogFilterWrapper;
5051
import org.tron.core.services.jsonrpc.types.BlockResult;
@@ -109,11 +110,11 @@ public void init() {
109110
blockCapsule0 = BlockUtil.newGenesisBlockCapsule();
110111
blockCapsule1 = new BlockCapsule(LATEST_BLOCK_NUM, Sha256Hash.wrap(ByteString.copyFrom(
111112
ByteArray.fromHexString(
112-
"0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))), 1,
113+
"0304f784e4e7bae517bcab94c3e0c9214fb4ac7ff9d7d5a937d1f40031f87b81"))), 1000000,
113114
ByteString.copyFromUtf8("testAddress"));
114115
blockCapsule2 = new BlockCapsule(LATEST_SOLIDIFIED_BLOCK_NUM, Sha256Hash.wrap(
115116
ByteString.copyFrom(ByteArray.fromHexString(
116-
"9938a342238077182498b464ac029222ae169360e540d1fd6aee7c2ae9575a06"))), 1,
117+
"9938a342238077182498b464ac029222ae169360e540d1fd6aee7c2ae9575a06"))), 2000000,
117118
ByteString.copyFromUtf8("testAddress"));
118119

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

136137
transactionCapsule1 = new TransactionCapsule(transferContract1, ContractType.TransferContract);
137138
transactionCapsule1.setBlockNum(blockCapsule1.getNum());
139+
transactionCapsule1.setTimestamp(blockCapsule1.getTimeStamp());
138140
TransactionCapsule transactionCapsule2 = new TransactionCapsule(transferContract2,
139141
ContractType.TransferContract);
140142
transactionCapsule2.setBlockNum(blockCapsule1.getNum());
143+
transactionCapsule2.setTimestamp(blockCapsule1.getTimeStamp());
141144
TransactionCapsule transactionCapsule3 = new TransactionCapsule(transferContract3,
142145
ContractType.TransferContract);
143146
transactionCapsule3.setBlockNum(blockCapsule2.getNum());
144-
147+
transactionCapsule3.setTimestamp(blockCapsule2.getTimeStamp());
145148
blockCapsule1.addTransaction(transactionCapsule1);
146149
blockCapsule1.addTransaction(transactionCapsule2);
147150
blockCapsule2.addTransaction(transactionCapsule3);
@@ -181,6 +184,7 @@ public void init() {
181184
TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule();
182185
transactionInfoCapsule.setId(tx.getTransactionId().getBytes());
183186
transactionInfoCapsule.setBlockNumber(blockCapsule1.getNum());
187+
transactionInfoCapsule.setBlockTimeStamp(blockCapsule1.getTimeStamp());
184188
transactionInfoCapsule.addAllLog(logs);
185189
transactionRetCapsule1.addTransactionInfo(transactionInfoCapsule.getInstance());
186190
});
@@ -192,6 +196,7 @@ public void init() {
192196
TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule();
193197
transactionInfoCapsule.setId(tx.getTransactionId().getBytes());
194198
transactionInfoCapsule.setBlockNumber(blockCapsule2.getNum());
199+
transactionInfoCapsule.setBlockTimeStamp(blockCapsule2.getTimeStamp());
195200
transactionRetCapsule2.addTransactionInfo(transactionInfoCapsule.getInstance());
196201
});
197202
dbManager.getTransactionRetStore()
@@ -969,6 +974,30 @@ public void testMethodBlockRange() {
969974
}
970975
}
971976

977+
@Test
978+
public void testGetLogs() {
979+
try {
980+
LogFilterElement[] logs = tronJsonRpc.getLogs(
981+
new FilterRequest("0x2710", "0x2710", null, null, null));
982+
Assert.assertTrue(logs.length > 0);
983+
LogFilterElement log = logs[0];
984+
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getNum()), log.getBlockNumber());
985+
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getBlockId().toString()),
986+
log.getBlockHash());
987+
Assert.assertEquals("0x0", log.getLogIndex());
988+
Assert.assertFalse(log.isRemoved());
989+
Assert.assertEquals(1, log.getTopics().length);
990+
Assert.assertEquals(
991+
"0x0000000000000000000000000000000000000000000000000000746f70696331",
992+
log.getTopics()[0]);
993+
Assert.assertEquals(ByteArray.toJsonHex("data1".getBytes()), log.getData());
994+
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getTimeStamp() / 1000),
995+
log.getBlockTimestamp());
996+
} catch (Exception e) {
997+
Assert.fail();
998+
}
999+
}
1000+
9721001
@Test
9731002
public void testNewFilterFinalizedBlock() {
9741003

@@ -1026,6 +1055,10 @@ public void testGetBlockReceipts() {
10261055

10271056
Assert.assertEquals(
10281057
JSON.toJSONString(transactionReceipt), JSON.toJSONString(transactionReceipt1));
1058+
1059+
Assert.assertTrue(transactionReceipt1.getLogs().length > 0);
1060+
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule1.getTimeStamp() / 1000),
1061+
transactionReceipt1.getLogs()[0].getBlockTimestamp());
10291062
}
10301063
} catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) {
10311064
throw new RuntimeException(e);

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ public void testTransactionReceipt() throws JsonRpcInternalException {
5454

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

6063
BlockCapsule blockCapsule = new BlockCapsule(block);
6164
long energyFee = wallet.getEnergyFee(blockCapsule.getTimeStamp());
@@ -66,36 +69,35 @@ public void testTransactionReceipt() throws JsonRpcInternalException {
6669
new TransactionReceipt(blockCapsule, transactionInfo, context, energyFee);
6770

6871
Assert.assertNotNull(transactionReceipt);
69-
String blockHash = "0x0000000000000001464f071c8a336fd22eb5145dff1b245bda013ec89add8497";
72+
String blockHash = "0x0000000000000001ba51f50f562758a449ff4a98df4febef89e122c1bb7e1a0c";
7073

7174
// assert basic fields
72-
Assert.assertEquals(transactionReceipt.getBlockHash(), blockHash);
73-
Assert.assertEquals(transactionReceipt.getBlockNumber(), "0x1");
74-
Assert.assertEquals(transactionReceipt.getTransactionHash(), "0x31");
75-
Assert.assertEquals(transactionReceipt.getTransactionIndex(), "0x0");
76-
Assert.assertEquals(transactionReceipt.getCumulativeGasUsed(), ByteArray.toJsonHex(102));
77-
Assert.assertEquals(transactionReceipt.getGasUsed(), ByteArray.toJsonHex(100));
78-
Assert.assertEquals(transactionReceipt.getEffectiveGasPrice(), ByteArray.toJsonHex(energyFee));
79-
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());
8083

8184
// assert contract fields
82-
Assert.assertEquals(transactionReceipt.getFrom(), ByteArray.toJsonHexAddress(new byte[0]));
83-
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());
8487
Assert.assertNull(transactionReceipt.getContractAddress());
8588

8689
// assert logs fields
87-
Assert.assertEquals(transactionReceipt.getLogs().length, 1);
88-
Assert.assertEquals(transactionReceipt.getLogs()[0].getLogIndex(), "0x3");
89-
Assert.assertEquals(
90-
transactionReceipt.getLogs()[0].getBlockHash(), blockHash);
91-
Assert.assertEquals(transactionReceipt.getLogs()[0].getBlockNumber(), "0x1");
92-
Assert.assertEquals(transactionReceipt.getLogs()[0].getTransactionHash(), "0x31");
93-
Assert.assertEquals(transactionReceipt.getLogs()[0].getTransactionIndex(), "0x0");
94-
Assert.assertEquals(transactionReceipt.getLogs()[0].getBlockTimestamp(), "0x3e8");
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());
9597

9698
// assert default fields
9799
Assert.assertNull(transactionReceipt.getRoot());
98-
Assert.assertEquals(transactionReceipt.getType(), "0x0");
99-
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());
100102
}
101103
}

0 commit comments

Comments
 (0)