fix: fix bugs in DHTUtil

fix the bug in DHTUtil which is wrong arguments when calling constructor of BigInteger
This commit is contained in:
Frank.R.Wu
2021-12-09 15:32:36 +08:00
parent 14e918fa18
commit a9ba6fea6e
2 changed files with 64 additions and 4 deletions

View File

@@ -12,7 +12,10 @@ public class DHTUtil {
private static final Map<String, BigInteger> ID_INTEGER_CACHE = new HashMap<>();
public static String[] getClusterByKey(String key, int k) {
String[] nodes = ContractManager.instance.masterStub.listNodes();
return getClusterByKey(key, k, ContractManager.instance.masterStub.listNodes());
}
protected static String[] getClusterByKey(String key, int k, String[] nodes) {
if (nodes.length == 0) {
return null;
}
@@ -41,7 +44,7 @@ public class DHTUtil {
if (l + 1 == r) {
BigInteger bigL = getBigInteger(nodes[l]),
bigR = getBigInteger(nodes[r]);
bigH = new BigInteger(hash);
bigH = new BigInteger(hash, 16);
if (bigR.subtract(bigH).compareTo(bigH.subtract(bigL)) > -1) {
selected = nodes[l];
m = l;
@@ -76,7 +79,7 @@ public class DHTUtil {
ret.add(nodes[l--]);
} else {
if (null == bigH) {
bigH = new BigInteger(hash);
bigH = new BigInteger(hash, 16);
}
if (getBigInteger(nodes[r]).subtract(bigH)
.compareTo(bigH.subtract(getBigInteger(nodes[l]))) > -1) {
@@ -93,7 +96,7 @@ public class DHTUtil {
private static BigInteger getBigInteger(String id) {
String bigInt = id.substring(2);
if (!ID_INTEGER_CACHE.containsKey(bigInt)) {
ID_INTEGER_CACHE.put(bigInt, new BigInteger(bigInt));
ID_INTEGER_CACHE.put(bigInt, new BigInteger(bigInt, 16));
}
return ID_INTEGER_CACHE.get(bigInt);
}