Skip to content

Commit 6b0aed7

Browse files
Merge pull request #6176 from 317787106/hotfix/eth_getLogs
feat(api): fix the bug of generating query conditions in `eth_getLogs`
2 parents 55cd17f + b9a4912 commit 6b0aed7

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,17 @@ public int[][][] getConditions() {
191191
Bloom bloom = Bloom.create(hash);
192192
BitSet bs = BitSet.valueOf(bloom.getData());
193193

194-
int[] bitIndex = new int[3]; //must same as the number of hash function in Bloom
195-
int nonZeroCount = 0;
194+
//number of nonZero positions may be equal or less than number(3) of hash function in Bloom
195+
List<Integer> bitIndexList = new ArrayList<>();
196196
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
197197
// operate on index i here
198198
if (i == Integer.MAX_VALUE) {
199199
break; // or (i+1) would overflow
200200
}
201-
bitIndex[nonZeroCount++] = i;
201+
bitIndexList.add(i);
202202
}
203203

204-
bitIndexes[j] = bitIndex;
204+
bitIndexes[j] = bitIndexList.stream().mapToInt(Integer::intValue).toArray();
205205
}
206206
allConditionsIndex[k] = bitIndexes;
207207
}

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.tron.common.utils.ByteUtil;
2020
import org.tron.common.utils.Commons;
2121
import org.tron.core.exception.JsonRpcInvalidParamsException;
22+
import org.tron.core.services.jsonrpc.JsonRpcApiUtil;
2223
import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest;
2324
import org.tron.core.services.jsonrpc.filters.LogBlockQuery;
2425
import org.tron.core.services.jsonrpc.filters.LogFilter;
@@ -245,17 +246,16 @@ private int[] getBloomIndex(String s) {
245246
Bloom bloom = Bloom.create(Hash.sha3(ByteArray.fromHexString(s)));
246247
BitSet bs = BitSet.valueOf(bloom.getData());
247248

248-
int[] bitIndex = new int[3]; //must same as the number of hash function in Bloom
249-
int nonZeroCount = 0;
249+
List<Integer> bitIndexList = new ArrayList<>();
250250
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
251251
// operate on index i here
252252
if (i == Integer.MAX_VALUE) {
253253
break; // or (i+1) would overflow
254254
}
255-
bitIndex[nonZeroCount++] = i;
255+
bitIndexList.add(i);
256256
}
257257

258-
return bitIndex;
258+
return bitIndexList.stream().mapToInt(Integer::intValue).toArray();
259259
}
260260

261261
@Test
@@ -314,4 +314,48 @@ public void testGetConditions() {
314314
Assert.fail();
315315
}
316316
}
317+
318+
@Test
319+
public void testGetConditionWithHashCollision() {
320+
try {
321+
List<String> addressList = new ArrayList<>();
322+
addressList.add("0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85");
323+
addressList.add("0x3038114c1a1e72c5bfa8b003bc3650ad2ba254a0");
324+
325+
Object[] topics = new Object[0];
326+
327+
LogFilterWrapper logFilterWrapper =
328+
new LogFilterWrapper(new FilterRequest(null,
329+
null,
330+
addressList,
331+
topics,
332+
null),
333+
100,
334+
null);
335+
336+
LogBlockQuery logBlockQuery = new LogBlockQuery(logFilterWrapper, null, 100, null);
337+
int[][][] conditions = logBlockQuery.getConditions();
338+
//level = depth(address) + depth(topics), skip null
339+
Assert.assertEquals(1, conditions.length);
340+
//elements number
341+
Assert.assertEquals(2, conditions[0].length);
342+
343+
Assert.assertEquals(3, conditions[0][0].length);
344+
//Hash collision, only two nonZero position
345+
Assert.assertEquals(2, conditions[0][1].length);
346+
347+
Assert.assertArrayEquals(conditions[0][0],
348+
getBloomIndex("0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85"));
349+
Assert.assertArrayEquals(conditions[0][1],
350+
getBloomIndex("0x3038114c1a1e72c5bfa8b003bc3650ad2ba254a0"));
351+
352+
} catch (JsonRpcInvalidParamsException e) {
353+
Assert.fail();
354+
}
355+
}
356+
357+
@Test
358+
public void testGenerateFilterId() {
359+
Assert.assertEquals(32, JsonRpcApiUtil.generateFilterId().length());
360+
}
317361
}

0 commit comments

Comments
 (0)