Skip to content

Commit

Permalink
Some Blob refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 5, 2024
1 parent bb5b81f commit 802fdd1
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
7 changes: 6 additions & 1 deletion convex-core/src/main/java/convex/core/data/ABlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ public final Hash computeHash(MessageDigest digest) {
return Hash.wrap(digest.digest());
}

protected abstract void updateDigest(MessageDigest digest);
/**
* Updates a MessageDigest with the contents of this Blob
*
* @param digest MessageDigest instance
*/
public abstract void updateDigest(MessageDigest digest);

/**
* Gets the byte at the specified position
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/BlobTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public Blob toFlatBlob() {
}

@Override
protected void updateDigest(MessageDigest digest) {
public void updateDigest(MessageDigest digest) {
int n = children.length;
for (int i = 0; i < n; i++) {
getChild(i).updateDigest(digest);
Expand Down
7 changes: 4 additions & 3 deletions convex-core/src/main/java/convex/core/data/LongBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public ABlob slice(long start, long end) {


@Override
protected void updateDigest(MessageDigest digest) {
byte[] bs = getEncoding().getInternalArray();
digest.update(bs, 2, (int) LENGTH);
public void updateDigest(MessageDigest digest) {
Blob b=getEncoding();
byte[] bs = b.getInternalArray();
digest.update(bs, b.getInternalOffset()+2, (int) LENGTH);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package convex.core.data;
package convex.core.data.impl;

import java.nio.ByteBuffer;
import java.security.MessageDigest;

import convex.core.data.ABlob;
import convex.core.data.ABlobLike;
import convex.core.data.Blob;
import convex.core.util.Utils;

/**
Expand Down Expand Up @@ -51,7 +54,7 @@ public Blob toFlatBlob() {
}

@Override
protected void updateDigest(MessageDigest digest) {
public void updateDigest(MessageDigest digest) {
ABlob can=getCanonical();
can.updateDigest(digest);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package convex.core.data.impl;

import org.bouncycastle.util.Arrays;

public class RepeatByteBlob extends ADerivedBlob {
private final byte b;

protected RepeatByteBlob(long count, byte b) {
super(count);
this.b=b;
}

@Override
public RepeatByteBlob sliceImpl(long start, long end) {
return new RepeatByteBlob(end-start,b);
}

@Override
public int getBytes(byte[] dest, int destOffset) {
int end=destOffset+size();
Arrays.fill(dest, destOffset,end,(byte)0);
return end;
}

@Override
public byte byteAtUnchecked(long i) {
return b;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.bouncycastle.util.Arrays;

import convex.core.data.ABlob;
import convex.core.data.ADerivedBlob;
import convex.core.data.Cells;

/**
Expand Down
13 changes: 13 additions & 0 deletions convex-core/src/test/java/convex/core/data/BlobsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,19 @@ public void testLongBlobBroken() throws BadFormatException, IOException {
assertEquals(b, Format.encodedBlob(o));
assertEquals(pref.getValue(), o);
}

@Test
public void testLongBlobDecoded() throws BadFormatException {
long V=0xFF1234567899L;
// LongBlob has custom hash implementation, we want to make sure it reads from encoding correctly
LongBlob l0=LongBlob.create(V);
Blob enc=(Blob.fromHex("F000").append(l0.getEncoding()).toFlatBlob().slice(2));

LongBlob l1=LongBlob.create(V);
l1.attachEncoding(enc);

assertEquals(l0.getHash(),l1.getHash());
}

/**
* Generic tests for an arbitrary ABlob instance
Expand Down

0 comments on commit 802fdd1

Please sign in to comment.