Skip to content

Commit 3da676e

Browse files
committed
fix the bug of eth_getLogs
1 parent 0f39d7a commit 3da676e

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

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: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,17 +347,16 @@ private int[] getBloomIndex(String s) {
347347
Bloom bloom = Bloom.create(Hash.sha3(ByteArray.fromHexString(s)));
348348
BitSet bs = BitSet.valueOf(bloom.getData());
349349

350-
int[] bitIndex = new int[3]; //must same as the number of hash function in Bloom
351-
int nonZeroCount = 0;
350+
List<Integer> bitIndexList = new ArrayList<>();
352351
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
353352
// operate on index i here
354353
if (i == Integer.MAX_VALUE) {
355354
break; // or (i+1) would overflow
356355
}
357-
bitIndex[nonZeroCount++] = i;
356+
bitIndexList.add(i);
358357
}
359358

360-
return bitIndex;
359+
return bitIndexList.stream().mapToInt(Integer::intValue).toArray();
361360
}
362361

363362
@Test
@@ -416,4 +415,43 @@ public void testGetConditions() {
416415
Assert.fail();
417416
}
418417
}
418+
419+
@Test
420+
public void testGetConditionWithHashCollision() {
421+
try {
422+
List<String> addressList = new ArrayList<>();
423+
addressList.add("0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85");
424+
addressList.add("0x3038114c1a1e72c5bfa8b003bc3650ad2ba254a0");
425+
426+
Object[] topics = new Object[0];
427+
428+
LogFilterWrapper logFilterWrapper =
429+
new LogFilterWrapper(new FilterRequest(null,
430+
null,
431+
addressList,
432+
topics,
433+
null),
434+
100,
435+
null);
436+
437+
LogBlockQuery logBlockQuery = new LogBlockQuery(logFilterWrapper, null, 100, null);
438+
int[][][] conditions = logBlockQuery.getConditions();
439+
//level = depth(address) + depth(topics), skip null
440+
Assert.assertEquals(1, conditions.length);
441+
//elements number
442+
Assert.assertEquals(2, conditions[0].length);
443+
444+
Assert.assertEquals(3, conditions[0][0].length);
445+
//Hash collision, only two nonZero position
446+
Assert.assertEquals(2, conditions[0][1].length);
447+
448+
Assert.assertArrayEquals(conditions[0][0],
449+
getBloomIndex("0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85"));
450+
Assert.assertArrayEquals(conditions[0][1],
451+
getBloomIndex("0x3038114c1a1e72c5bfa8b003bc3650ad2ba254a0"));
452+
453+
} catch (JsonRpcInvalidParamsException e) {
454+
Assert.fail();
455+
}
456+
}
419457
}

0 commit comments

Comments
 (0)