Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,17 @@ public int[][][] getConditions() {
Bloom bloom = Bloom.create(hash);
BitSet bs = BitSet.valueOf(bloom.getData());

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

bitIndexes[j] = bitIndex;
bitIndexes[j] = bitIndexList.stream().mapToInt(Integer::intValue).toArray();
}
allConditionsIndex[k] = bitIndexes;
}
Expand Down
52 changes: 48 additions & 4 deletions framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.tron.common.utils.ByteUtil;
import org.tron.common.utils.Commons;
import org.tron.core.exception.JsonRpcInvalidParamsException;
import org.tron.core.services.jsonrpc.JsonRpcApiUtil;
import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest;
import org.tron.core.services.jsonrpc.filters.LogBlockQuery;
import org.tron.core.services.jsonrpc.filters.LogFilter;
Expand Down Expand Up @@ -245,17 +246,16 @@ private int[] getBloomIndex(String s) {
Bloom bloom = Bloom.create(Hash.sha3(ByteArray.fromHexString(s)));
BitSet bs = BitSet.valueOf(bloom.getData());

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

return bitIndex;
return bitIndexList.stream().mapToInt(Integer::intValue).toArray();
}

@Test
Expand Down Expand Up @@ -314,4 +314,48 @@ public void testGetConditions() {
Assert.fail();
}
}

@Test
public void testGetConditionWithHashCollision() {
try {
List<String> addressList = new ArrayList<>();
addressList.add("0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85");
addressList.add("0x3038114c1a1e72c5bfa8b003bc3650ad2ba254a0");

Object[] topics = new Object[0];

LogFilterWrapper logFilterWrapper =
new LogFilterWrapper(new FilterRequest(null,
null,
addressList,
topics,
null),
100,
null);

LogBlockQuery logBlockQuery = new LogBlockQuery(logFilterWrapper, null, 100, null);
int[][][] conditions = logBlockQuery.getConditions();
//level = depth(address) + depth(topics), skip null
Assert.assertEquals(1, conditions.length);
//elements number
Assert.assertEquals(2, conditions[0].length);

Assert.assertEquals(3, conditions[0][0].length);
//Hash collision, only two nonZero position
Assert.assertEquals(2, conditions[0][1].length);

Assert.assertArrayEquals(conditions[0][0],
getBloomIndex("0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85"));
Assert.assertArrayEquals(conditions[0][1],
getBloomIndex("0x3038114c1a1e72c5bfa8b003bc3650ad2ba254a0"));

} catch (JsonRpcInvalidParamsException e) {
Assert.fail();
}
}

@Test
public void testGenerateFilterId() {
Assert.assertEquals(32, JsonRpcApiUtil.generateFilterId().length());
}
}