Skip to content

Commit

Permalink
Remove Handle and HandleFactory.
Browse files Browse the repository at this point in the history
Use an integer array instead of encoding the data into a long which added limitations and made the code more complicate.
  • Loading branch information
HenrikJannsen committed Feb 28, 2024
1 parent 9953f63 commit f7908cd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
@Slf4j
public class CatHash {
private static final int MAX_CACHE_SIZE = 10000;
private static final HandleFactory HANDLE_FACTORY = new HandleFactory();
private static final ConcurrentHashMap<ByteArray, Image> CACHE = new ConcurrentHashMap<>();

public static Image getImage(byte[] pubKeyHash) {
Expand All @@ -48,20 +47,16 @@ private static Image getImage(ByteArray pubKeyHash, boolean useCache) {
BigInteger bigInteger = new BigInteger(pubKeyHash.getBytes());
Configuration configuration = new Configuration();
VariableSizeHashing hashing = new VariableSizeHashing(configuration.getBucketSizes());
byte[] data = hashing.createBuckets(bigInteger);
Handle handle = HANDLE_FACTORY.calculateHandle(data);
Image image = imageForHandle(handle, configuration);
int[] integerBuckets = hashing.createIntegerBuckets(bigInteger);
Image image = imageFromIntegerBuckets(integerBuckets, configuration);
if (useCache && CACHE.size() < MAX_CACHE_SIZE) {
CACHE.put(pubKeyHash, image);
}
return image;
}

private static Image imageForHandle(Handle handle, Configuration configuration) {
long ts = System.currentTimeMillis();
byte[] bucketValues = handle.bucketValues();
String[] paths = configuration.convertToFacetParts(bucketValues);
log.debug("Generated paths for CatHash image in {} ms", System.currentTimeMillis() - ts); // typically <1ms
private static Image imageFromIntegerBuckets(int[] integerBuckets, Configuration configuration) {
String[] paths = configuration.integerBucketsToPaths(integerBuckets);
return ImageUtil.composeImage(paths, configuration.width(), configuration.height());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ public class Configuration {
};
}

public String[] integerBucketsToPaths(int[] integerBuckets) {
if (integerBuckets.length != BUCKET_COUNT) {
throw new IllegalArgumentException();
}

String[] paths = new String[FACET_COUNT];
for (int facet = 0; facet < FACET_COUNT; facet++) {
int bucketValue = integerBuckets[facet];
paths[facet] = generatePath(FACET_PATH_TEMPLATES[facet], bucketValue);
}
return paths;
}

public String[] convertToFacetParts(byte[] bucketValues) {
if (bucketValues.length != BUCKET_COUNT) {
throw new IllegalArgumentException();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@ public byte[] createBuckets(BigInteger hash) {

return ret;
}

public int[] createIntegerBuckets(BigInteger input) {
int currentBucket = 0;
int[] result = new int[bucketSizes.length];
while (currentBucket < bucketSizes.length) {
BigInteger[] divisorReminder = input.divideAndRemainder(BigInteger.valueOf(bucketSizes[currentBucket]));
input = divisorReminder[0];
long reminder = divisorReminder[1].longValue();
result[currentBucket] = (int) Math.abs(reminder % bucketSizes[currentBucket]);
currentBucket++;
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import bisq.desktop.common.threading.UIThread;
import bisq.desktop.common.view.Controller;
import bisq.desktop.common.view.Navigation;
import bisq.desktop.components.overlay.Popup;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.components.overlay.Popup;
import bisq.desktop.overlay.OverlayController;
import bisq.i18n.Res;
import bisq.identity.IdentityService;
Expand Down Expand Up @@ -153,7 +153,7 @@ private CompletableFuture<ProofOfWork> createProofOfWork(byte[] pubKeyHash) {
.thenApply(proofOfWork -> {
long powDuration = System.currentTimeMillis() - ts;
log.info("Proof of work creation completed after {} ms", powDuration);
createSimulatedDelay(powDuration);
//createSimulatedDelay(powDuration);
UIThread.run(() -> {
model.setProofOfWork(Optional.of(proofOfWork));
String nym = NymIdGenerator.fromHash(pubKeyHash);
Expand Down

0 comments on commit f7908cd

Please sign in to comment.