Skip to content

Commit f67c2cf

Browse files
authored
Update eth_getLogs to utilize index across block transactions (0.3) (#325)
Update eth_getLogs to utilize index across block transactions (#314) * Update eth_getLogs to utilize index across block transactions Signed-off-by: Nana-EC <[email protected]>
1 parent a8ef37c commit f67c2cf

File tree

3 files changed

+102
-100
lines changed

3 files changed

+102
-100
lines changed

packages/relay/src/lib/eth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ export class EthImpl implements Eth {
11341134
blockHash: EthImpl.toHash32(detail.block_hash),
11351135
blockNumber: detail.block_number,
11361136
data: log.data,
1137-
logIndex: log.index,
1137+
logIndex: logIndex,
11381138
removed: false,
11391139
topics: log.topics,
11401140
transactionHash: EthImpl.toHash32(detail.hash),

packages/relay/tests/lib/eth.spec.ts

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,73 @@ describe('Eth calls using MirrorNode', async function () {
204204
}
205205
};
206206

207+
const defaultLogTopics = [
208+
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
209+
"0x0000000000000000000000000000000000000000000000000000000000000000",
210+
"0x000000000000000000000000000000000000000000000000000000000208fa13",
211+
"0x0000000000000000000000000000000000000000000000000000000000000005"
212+
];
213+
214+
215+
const logBloom1 = '0x1111';
216+
const logBloom2 = '0x2222';
217+
const logBloom3 = '0x3333';
218+
const logBloom4 = '0x4444';
219+
220+
const defaultLogs1 = [
221+
{
222+
"address": "0x0000000000000000000000000000000002131951",
223+
"bloom": logBloom1,
224+
"contract_id": contractId1,
225+
"data": "0x",
226+
"index": 0,
227+
"topics": defaultLogTopics,
228+
"root_contract_id": "0.0.34806097",
229+
"timestamp": contractTimestamp1
230+
},
231+
{
232+
"address": "0x0000000000000000000000000000000002131951",
233+
"bloom": logBloom2,
234+
"contract_id": contractId1,
235+
"data": "0x",
236+
"index": 1,
237+
"topics": defaultLogTopics,
238+
"root_contract_id": "0.0.34806097",
239+
"timestamp": contractTimestamp1
240+
}
241+
];
242+
243+
const defaultLogs2 = [
244+
{
245+
"address": "0x0000000000000000000000000000000002131951",
246+
"bloom": logBloom3,
247+
"contract_id": contractId1,
248+
"data": "0x",
249+
"index": 0,
250+
"topics": [],
251+
"root_contract_id": "0.0.34806097",
252+
"timestamp": contractTimestamp2
253+
}
254+
];
255+
256+
const defaultLogs3 = [
257+
{
258+
"address": "0x0000000000000000000000000000000002131951",
259+
"bloom": logBloom4,
260+
"contract_id": contractId2,
261+
"data": "0x",
262+
"index": 0,
263+
"topics": [],
264+
"root_contract_id": "0.0.34806097",
265+
"timestamp": contractTimestamp3
266+
}
267+
];
268+
269+
const defaultLogsList = defaultLogs1.concat(defaultLogs2).concat(defaultLogs3);
270+
const defaultLogs = {
271+
"logs": defaultLogsList
272+
};
273+
207274
const defaultDetailedContractResults = {
208275
'access_list': '0x',
209276
'amount': 2000000000,
@@ -222,21 +289,7 @@ describe('Eth calls using MirrorNode', async function () {
222289
'gas_price': '0x4a817c80',
223290
'gas_used': 123,
224291
'hash': contractHash1,
225-
'logs': [
226-
{
227-
'address': contractAddress1,
228-
'bloom': '0x0123',
229-
'contract_id': contractId1,
230-
'data': '0x0123',
231-
'index': 0,
232-
'topics': [
233-
'0x97c1fc0a6ed5551bc831571325e9bdb365d06803100dc20648640ba24ce69750',
234-
'0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925',
235-
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
236-
'0xe8d47b56e8cdfa95f871b19d4f50a857217c44a95502b0811a350fec1500dd67'
237-
]
238-
},
239-
],
292+
'logs': defaultLogs1,
240293
'max_fee_per_gas': '0x',
241294
'max_priority_fee_per_gas': '0x',
242295
'nonce': 1,
@@ -265,7 +318,8 @@ describe('Eth calls using MirrorNode', async function () {
265318
'timestamp': contractTimestamp2,
266319
'block_hash': blockHash2,
267320
'block_number': blockNumber2,
268-
'hash': contractHash2
321+
'hash': contractHash2,
322+
'logs': defaultLogs2
269323
}
270324
};
271325

@@ -276,6 +330,7 @@ describe('Eth calls using MirrorNode', async function () {
276330
'block_number': blockNumber3,
277331
'hash': contractHash3,
278332
'contract_id': contractId2,
333+
'logs': defaultLogs3
279334
}
280335
};
281336

@@ -290,63 +345,6 @@ describe('Eth calls using MirrorNode', async function () {
290345
const results = defaultContractResults.results;
291346
const totalGasUsed = EthImpl.numberTo0x(results[0].gas_used + results[1].gas_used);
292347

293-
const logBloom1 = '0x1111';
294-
const logBloom2 = '0x2222';
295-
const logBloom3 = '0x3333';
296-
const logBloom4 = '0x4444';
297-
298-
const defaultLogTopics = [
299-
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
300-
"0x0000000000000000000000000000000000000000000000000000000000000000",
301-
"0x000000000000000000000000000000000000000000000000000000000208fa13",
302-
"0x0000000000000000000000000000000000000000000000000000000000000005"
303-
];
304-
305-
const defaultLogs = {
306-
"logs": [
307-
{
308-
"address": "0x0000000000000000000000000000000002131951",
309-
"bloom": logBloom1,
310-
"contract_id": contractId1,
311-
"data": "0x",
312-
"index": 0,
313-
"topics": defaultLogTopics,
314-
"root_contract_id": "0.0.34806097",
315-
"timestamp": contractTimestamp1
316-
},
317-
{
318-
"address": "0x0000000000000000000000000000000002131951",
319-
"bloom": logBloom2,
320-
"contract_id": contractId1,
321-
"data": "0x",
322-
"index": 1,
323-
"topics": defaultLogTopics,
324-
"root_contract_id": "0.0.34806097",
325-
"timestamp": contractTimestamp1
326-
},
327-
{
328-
"address": "0x0000000000000000000000000000000002131951",
329-
"bloom": logBloom3,
330-
"contract_id": contractId1,
331-
"data": "0x",
332-
"index": 0,
333-
"topics": [],
334-
"root_contract_id": "0.0.34806097",
335-
"timestamp": contractTimestamp2
336-
},
337-
{
338-
"address": "0x0000000000000000000000000000000002131951",
339-
"bloom": logBloom4,
340-
"contract_id": contractId2,
341-
"data": "0x",
342-
"index": 0,
343-
"topics": [],
344-
"root_contract_id": "0.0.34806097",
345-
"timestamp": contractTimestamp3
346-
}
347-
]
348-
};
349-
350348
const defaultNetworkFees = {
351349
'fees': [
352350
{
@@ -895,13 +893,13 @@ describe('Eth calls using MirrorNode', async function () {
895893

896894
describe('eth_getLogs', async function () {
897895

898-
const expectLogData = (res, log, tx) => {
896+
const expectLogData = (res, log, tx, blockLogIndexOffset) => {
899897
expect(res.address).to.eq(log.address);
900898
expect(res.blockHash).to.eq(EthImpl.toHash32(tx.block_hash));
901899
expect(res.blockHash.length).to.eq(66);
902900
expect(res.blockNumber).to.eq(tx.block_number);
903901
expect(res.data).to.eq(log.data);
904-
expect(res.logIndex).to.eq(log.index);
902+
expect(res.logIndex).to.eq(blockLogIndexOffset + log.index);
905903
expect(res.removed).to.eq(false);
906904
expect(res.topics).to.exist;
907905
expect(res.topics).to.deep.eq(log.topics);
@@ -911,19 +909,20 @@ describe('Eth calls using MirrorNode', async function () {
911909
};
912910

913911
const expectLogData1 = (res) => {
914-
expectLogData(res, defaultLogs.logs[0], defaultDetailedContractResults);
912+
expectLogData(res, defaultLogs.logs[0], defaultDetailedContractResults, 0);
915913
};
916914

917915
const expectLogData2 = (res) => {
918-
expectLogData(res, defaultLogs.logs[1], defaultDetailedContractResults);
916+
expectLogData(res, defaultLogs.logs[1], defaultDetailedContractResults, 0);
919917
};
920918

921919
const expectLogData3 = (res) => {
922-
expectLogData(res, defaultLogs.logs[2], defaultDetailedContractResults2);
920+
expectLogData(res, defaultLogs.logs[2], defaultDetailedContractResults2, defaultDetailedContractResults.logs.length);
923921
};
924922

923+
const transaction3LogOffset = defaultDetailedContractResults.logs.length + defaultDetailedContractResults2.logs.length;
925924
const expectLogData4 = (res) => {
926-
expectLogData(res, defaultLogs.logs[3], defaultDetailedContractResults3);
925+
expectLogData(res, defaultLogs.logs[3], defaultDetailedContractResults3, transaction3LogOffset);
927926
};
928927

929928
it('contract results details not found', async function () {

packages/server/tests/acceptance/rpc.spec.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
*/
2020

2121
// external resources
22-
import {expect} from 'chai';
23-
import {BigNumber, ethers} from 'ethers';
24-
import {AliasAccount} from '../clients/servicesClient';
22+
import { expect } from 'chai';
23+
import { BigNumber, ethers } from 'ethers';
24+
import { AliasAccount } from '../clients/servicesClient';
2525
import Assertions from '../helpers/assertions';
26-
import {Utils} from '../helpers/utils';
27-
import {AccountBalanceQuery, ContractFunctionParameters} from '@hashgraph/sdk';
26+
import { Utils } from '../helpers/utils';
27+
import { AccountBalanceQuery, ContractFunctionParameters } from '@hashgraph/sdk';
2828

2929
// local resources
3030
import parentContractJson from '../contracts/Parent.json';
@@ -38,7 +38,7 @@ describe('RPC Server Acceptance Tests', function () {
3838
const accounts: AliasAccount[] = [];
3939

4040
// @ts-ignore
41-
const {servicesNode, mirrorNode, relay, logger} = global;
41+
const { servicesNode, mirrorNode, relay, logger } = global;
4242

4343
// cached entities
4444
let tokenId;
@@ -126,10 +126,13 @@ describe('RPC Server Acceptance Tests', function () {
126126
const logs = await relay.call('eth_getLogs', [{}]);
127127
expect(logs.length).to.be.greaterThan(0);
128128
const txIndexLogIndexMapping: any[] = [];
129-
for(let i in logs) {
129+
for (const i in logs) {
130130
expect(logs[i]).to.have.property('address');
131131
expect(logs[i]).to.have.property('logIndex');
132132

133+
// verify logIndex represents index in block across transactions
134+
expect(logs[i].logIndex).to.equal(Number(i));
135+
133136
const key = `${logs[i].transactionHash}---${logs[i].logIndex}`;
134137
txIndexLogIndexMapping.push(key);
135138
}
@@ -152,7 +155,7 @@ describe('RPC Server Acceptance Tests', function () {
152155
expect(logs.length).to.be.greaterThan(0);
153156

154157
const log4BlockInt = parseInt(log4Block.blockNumber);
155-
for(let i in logs) {
158+
for (let i in logs) {
156159
expect(logs[i].blockNumber).to.be.greaterThanOrEqual(log4BlockInt);
157160
}
158161
});
@@ -164,7 +167,7 @@ describe('RPC Server Acceptance Tests', function () {
164167
expect(logs.length).to.be.greaterThan(0);
165168

166169
const log0BlockInt = parseInt(log0Block.blockNumber);
167-
for(let i in logs) {
170+
for (let i in logs) {
168171
expect(logs[i].blockNumber).to.be.lessThanOrEqual(log0BlockInt);
169172
}
170173
});
@@ -184,29 +187,29 @@ describe('RPC Server Acceptance Tests', function () {
184187
}
185188
});
186189

187-
it('should be able to use `address` param', async() => {
190+
it('should be able to use `address` param', async () => {
188191
const logs = await relay.call('eth_getLogs', [{
189192
'address': contractAddress
190193
}]);
191194
expect(logs.length).to.be.greaterThan(0);
192195

193-
for(let i in logs) {
196+
for (let i in logs) {
194197
expect(logs[i].address).to.equal(contractAddress);
195198
}
196199
});
197200

198-
it('should be able to use `blockHash` param', async() => {
201+
it('should be able to use `blockHash` param', async () => {
199202
const logs = await relay.call('eth_getLogs', [{
200203
'blockHash': log0Block.blockHash
201204
}]);
202205
expect(logs.length).to.be.greaterThan(0);
203206

204-
for(let i in logs) {
207+
for (let i in logs) {
205208
expect(logs[i].blockHash).to.equal(log0Block.blockHash);
206209
}
207210
});
208211

209-
it('should be able to use `topics` param', async() => {
212+
it('should be able to use `topics` param', async () => {
210213
const logs = await relay.call('eth_getLogs', [{
211214
'fromBlock': log0Block.blockNumber,
212215
'toBlock': log4Block.blockNumber,
@@ -221,7 +224,7 @@ describe('RPC Server Acceptance Tests', function () {
221224
}]);
222225
expect(logsWithTopic.length).to.be.greaterThan(0);
223226

224-
for(let i in logsWithTopic) {
227+
for (let i in logsWithTopic) {
225228
expect(logsWithTopic[i].topics.length).to.be.greaterThan(0);
226229
expect(logsWithTopic[i].topics[0]).to.be.equal(topic);
227230
}
@@ -450,7 +453,7 @@ describe('RPC Server Acceptance Tests', function () {
450453
await relay.sendRawTransaction(signedTx);
451454
Assertions.expectedError();
452455
}
453-
catch(e) {
456+
catch (e) {
454457
Assertions.jsonRpcError(e, predefined.UNSUPPORTED_CHAIN_ID(ethers.utils.hexValue(INCORRECT_CHAIN_ID), CHAIN_ID));
455458
}
456459
});
@@ -963,7 +966,7 @@ describe('RPC Server Acceptance Tests', function () {
963966
lastBlockAfterUpdate = (await mirrorNode.get(`/blocks?limit=1&order=desc`)).blocks[0];
964967
});
965968

966-
it('should call eth_feeHistory with updated fees', async function() {
969+
it('should call eth_feeHistory with updated fees', async function () {
967970
const blockCountNumber = lastBlockAfterUpdate.number - lastBlockBeforeUpdate.number;
968971
const blockCountHex = ethers.utils.hexValue(blockCountNumber);
969972
const datedGasPriceHex = ethers.utils.hexValue(Assertions.datedGasPrice);
@@ -973,14 +976,14 @@ describe('RPC Server Acceptance Tests', function () {
973976

974977
const res = await relay.call('eth_feeHistory', [blockCountHex, newestBlockNumberHex, [0]]);
975978

976-
Assertions.feeHistory(res, {resultCount: blockCountNumber, oldestBlock: oldestBlockNumberHex, chechReward: true});
979+
Assertions.feeHistory(res, { resultCount: blockCountNumber, oldestBlock: oldestBlockNumberHex, chechReward: true });
977980

978981
expect(res.baseFeePerGas[0]).to.equal(datedGasPriceHex);
979982
expect(res.baseFeePerGas[res.baseFeePerGas.length - 2]).to.equal(updatedGasPriceHex);
980983
expect(res.baseFeePerGas[res.baseFeePerGas.length - 1]).to.equal(updatedGasPriceHex);
981984
});
982985

983-
it('should call eth_feeHistory with newest block > latest', async function() {
986+
it('should call eth_feeHistory with newest block > latest', async function () {
984987
let latestBlock;
985988
const newestBlockNumber = lastBlockAfterUpdate.number + 10;
986989
const newestBlockNumberHex = ethers.utils.hexValue(newestBlockNumber);
@@ -992,7 +995,7 @@ describe('RPC Server Acceptance Tests', function () {
992995
}
993996
});
994997

995-
it('should call eth_feeHistory with zero block count', async function() {
998+
it('should call eth_feeHistory with zero block count', async function () {
996999
const res = await relay.call('eth_feeHistory', ['0x0', 'latest', null]);
9971000

9981001
expect(res.reward).to.not.exist;

0 commit comments

Comments
 (0)