|
19 | 19 | import org.tron.common.utils.ByteUtil; |
20 | 20 | import org.tron.common.utils.Commons; |
21 | 21 | import org.tron.core.exception.JsonRpcInvalidParamsException; |
| 22 | +import org.tron.core.services.jsonrpc.JsonRpcApiUtil; |
22 | 23 | import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; |
23 | 24 | import org.tron.core.services.jsonrpc.filters.LogBlockQuery; |
24 | 25 | import org.tron.core.services.jsonrpc.filters.LogFilter; |
@@ -245,17 +246,16 @@ private int[] getBloomIndex(String s) { |
245 | 246 | Bloom bloom = Bloom.create(Hash.sha3(ByteArray.fromHexString(s))); |
246 | 247 | BitSet bs = BitSet.valueOf(bloom.getData()); |
247 | 248 |
|
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<>(); |
250 | 250 | for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) { |
251 | 251 | // operate on index i here |
252 | 252 | if (i == Integer.MAX_VALUE) { |
253 | 253 | break; // or (i+1) would overflow |
254 | 254 | } |
255 | | - bitIndex[nonZeroCount++] = i; |
| 255 | + bitIndexList.add(i); |
256 | 256 | } |
257 | 257 |
|
258 | | - return bitIndex; |
| 258 | + return bitIndexList.stream().mapToInt(Integer::intValue).toArray(); |
259 | 259 | } |
260 | 260 |
|
261 | 261 | @Test |
@@ -314,4 +314,48 @@ public void testGetConditions() { |
314 | 314 | Assert.fail(); |
315 | 315 | } |
316 | 316 | } |
| 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 | + } |
317 | 361 | } |
0 commit comments