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

View File

@ -0,0 +1,57 @@
package org.bdware.sc.test;
import org.bdware.sc.util.DHTUtil;
import org.junit.Test;
public class DHTUtilTest extends DHTUtil {
@Test
public void test() {
String[] nodes = {
"0403ff00fb90380691d1175e55e838c140477cdce1591bf3b78e48e301407fd8f1b373e3b15dfe8349cd3627a9a483889d1d526e63db2e4aa3be8a97bd0b65f87d",
"040587bd88fe62b1c8ee85fbb5710d6f347be042af2bf75fdb159399569dda1a717db74504cbee855a7e13a52590b4b46050e66b9bcf957262cc19b3015f11e439",
"0414c9391ee3cccd66af4b04de3e6499b825cd1c79b9b709a9493c2f2b5320126aaf485e21f009dccf7589fca561b6bc534ad55d92d5511b025a541bc17b0cd448",
"04195fa13f9f6764e97f5fcae062c61f397370b529d1ae4da6ee6f0f28e87880112ce7f591718a328e72abfb658e83a9fabc22228cf4c4f7f255b921509480e3f1",
"041ad74f05c8edd668c130728e33993ae1d132239097c0da4e64148c7bbbe94d6d35615ac21b8c8d2f4be3ade0fba7ba4b59be5cb62d591f3d840f0426a81b986e",
"0430b4a610b7fbd6e5004d31462a47aca14c9b13c9f03224b1fd633c86d6d075412d11ac8f3fcafb973caf6f9587ce1d203dbd550d5760b3a733ed289dcc4b408f",
"045c9fc52fcd3d146bed43b2ea72ab735c6ae3b07481f7101b0ca9273cdad804e0e5b8489d8a87914ed1275fbc98a04ba38c2a8d0841d53a5009f98c8bfd98fdbe",
"04667101a3c59f8b6ac04d549757be791453afa569e865d265f66e273e1dc18e011ac164aec9e85f7ecb808a77470376ef71f8043ebba43bc2a04f0cd5bc094a47",
"046bd316801f86005b2c6a0277307b179faa15b1baa4b22fd3cfbaeb07dd481a952dece9f72dc5b59ca7437f646a6d48d3acb3b0338923b06c7a3bc013f5eeef23",
"046dcc290899ea28987f63d908219eb7a59be5e2f02c2c651187e99ff4160e75f65e7475d697d64541dfd61828523d202ac0f8c075ec8612b92f1095f3aeac2f97",
"046f7df9861bbbf17ac6091f83ed7e1836b97ef9b678ffda3e974cb4ff28f9a4f84266404ad340a3888e9a489b63a2a144092befa83a2032ef216d2c51fe9edb3f",
"047f7f96e76454ccc94d11ca4cc33950d768adc79abb41bbd5a0c129cd4cb42284da2d02867dc1472033550a4e91b60495b79e023b10027926a285eb60dbf8ff71",
"047ffe5237126429a6ef9b65c467f665e686873db89d782166eedf24df5d66fecfba060529c535f4d98095003e760c12af9c2eff71f7412e717fe687b3ff82e27b",
"048162a16b9f999c4af2c5a127cfe0672710f98cf530379f7f0c57b3eea64ded13282afad24e003845f00bb323f19c63b0bf007dc5057639ceab9db83267c4f0cf",
"048f4120650a09872d994c410e70eb1773ae56d6de42fb776ec7ee396d34d851f35497b7355bb963e4ba2701a7d3c2f370f0a7c82c57b29ca4f2941f5fcfaddea9",
"04949a4fa71a8d203a8d121d036cae97de09d1eac9218dcb94f5abfd76028160761f686e07841880067238015b74300c497d715087ec98abaeffd890faa3face77",
"0496f2881a00a1f6d0f184039156a0d1e268f1c497c419e0d9e1a48929bf7cdbdf8a4d8b1bab4a64a09552747972590548b5f04fa7f8002c6af53337c6032c0fab",
"049d2da913c2368dbba4db198f543cca2a68c092c544c2d94ea49edbb3901a7925ca876d27658b862b00c7e92d0b60d18a9eba2814a09d1e6f40565b788423743c",
"04a9e0bb49322c81826706fc2571ec530bbe4cb89166e9f03292bd825700aef5ded95da2db63aaefc413518aba81911d848cb92064508c693b934ad9001fd7ab1b",
"04b93f611bb51f7b07e84f812c16decbb21c6f4e21d4ea98d571c1cc7b1a93e22f594d698202995580d14bf02cb9f207fa3904520193854da6450994f67aa4d72c",
"04bac75a339e287deaa8c5e1cee47cf24233ca9e9b6ffdf943d43e9d43a08eda51663983ff1376cedf0286d0b66b22ede9d5f3dc92c10f74414436814e58c85f74",
"04c751ebe8d4d8449421539488e18969098f1bdb589ffd06549d225c45f9c2e3429c3b85b9666d9533d9fb3acc8cf14f8a1e612f5ad391ba5ad818f56627e6f9c0",
"04d1ff0fdb6b92b909b55a4b69ef0e9f6f63e33e5cce106a258d5085225b2a827e40ef725913b3df0bcf1ffdbdba48b6db73253ad388bb083b6924121ed4bb38c3",
"04d30189ad472de0cd4e3828eea337f643eb3baaceaa70349f1455b8137f155ceb5d961b51f7f43a8a66ca0bf2fb6afe1a142b0a1513c55965251d10475d10cec4",
"04d5186262ff5849ae2382968c94ff14792449cd10c28f2da3e6d6bd3b9d35484affb79d707e54327d657a832f69f6f322f573a647d074882be44758042a0de312",
"04e921f29e7407dc3011a0e292ec19c15d929b1eef26ea5c4f2ebb64c2144476be0d9c5722a0be5aa0eadf954b331334d63e1e478ed42694c8b91f0603091482e6",
"04e98c588c7e58784f4e68997639a911a62248bd7ce82bd52f40a126582657ca6d5a2fc41b60e4a6cbfb7a63580b43ffb64d60464b3660e4c5e22149be754ec39e",
"04ea0e5ddc6a8525795edaaaa6e776a9c8fa8d1e1a34d7956862dd7f132ef100d106f243c4ec10a9884019f58e64942c77e8a0609e6e17c973602b6df0fe7d65dc",
"04f4233bd922dbbd41c648f4118a2950058e11a5ab9bbeeb3807a96f71f325848516a2f7c0b69ecbabe8937f0d16fcff751976f08d4d5d6e03b9e8396d481be46f",
"04f954bd59636ee00d39bea5b1bf1b6e3e04ac259280647faec3050ab488984e020b9da2489efa10bc3d018ee1afae58fdcea8f4255c036ba9678567bab5262420"};
System.out.println(DHTUtil.getClusterByKey("bdcoin0", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoin1", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoin2", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoin3", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoin4", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoin5", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoin6", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoin7", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoin8", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoin9", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoina", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoinb", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoinc", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoind", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoine", 1, nodes)[0]);
System.out.println(DHTUtil.getClusterByKey("bdcoinf", 1, nodes)[0]);
}
}