Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass through Cid Codec to buildCid method #65

Merged
merged 1 commit into from
Oct 20, 2023
Merged
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 @@ -42,23 +42,23 @@ public CompletableFuture<BlockMetadata> getBlockMetadata(Cid block) {
});
}

private void cacheBlockMetadata(byte[] block, boolean isRaw) {
Cid cid = hashToCid(block, isRaw);
private void cacheBlockMetadata(byte[] block, Cid.Codec codec) {
Cid cid = hashToCid(block, codec);
metadata.put(cid, block);
}

private Cid hashToCid(byte[] input, boolean isRaw) {
return buildCid(Hash.sha256(input), isRaw);
private Cid hashToCid(byte[] input, Cid.Codec codec) {
return buildCid(Hash.sha256(input), codec);
}

private Cid buildCid(byte[] sha256, boolean isRaw) {
return Cid.buildCidV1(isRaw ? Cid.Codec.Raw : Cid.Codec.DagCbor, Multihash.Type.sha2_256, sha256);
private Cid buildCid(byte[] sha256, Cid.Codec codec) {
return Cid.buildCidV1(codec, Multihash.Type.sha2_256, sha256);
}

@Override
public CompletableFuture<Optional<byte[]>> get(Cid hash) {
return target.get(hash).thenApply(bopt -> {
bopt.ifPresent(b -> cacheBlockMetadata(b, hash.codec == Cid.Codec.Raw));
bopt.ifPresent(b -> cacheBlockMetadata(b, hash.codec));
return bopt;
});
}
Expand Down