Skip to content

Commit

Permalink
testing insertions in offchain
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed May 28, 2024
1 parent ba40af6 commit e51d219
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
68 changes: 65 additions & 3 deletions offchain/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,72 @@ import { Leaf, SparseMerkleTree } from "./index.js";

test("Test 1", () => {
const x = new SparseMerkleTree();

console.log("Root Hash", Buffer.from(x.branchHash).toString("hex"));
let rootList = [x.branchHash];
const expectedList = [
"ae671c31cece3444d2dd9240939e5c63c9b6ec79e6710b7a777f09d539a29d42",
"18c8dbcc059576ec251c90862baee3387c4ea916ef72e6f7dbf15502521ebed2",
"49148fa603b03d70fff1733cb9a70a35b9057637e11cde68e30f6f624b001504",
"9ea42e89c070ee103f27ae748c0aa9ce268d2f44e66568423409fa4f659cdb7a",
"8333b7d8020aa5101ae770c2b8ea84ad84834ea106413d309fbbf226816b6a17",
"e53502fcf8e19ddd4130fb7430c89f7092e96493d7081486c8b9f452217096df",
"d3161d3695bfb1ddedb5217a712350661bb8042ee7a73deec3686730ac872600",
"913b8548e5bd6531610e5eacce933ffec3a29575f5e384fb795320d15ea28c9b",
"5a812ad3240bc532aea6f0b8427ccf929a20bdd0a08bcad062207b14a65e9be8",
"5cd33d77b3e53cd5433d5c56fb0cc3190a97a09e8e43382dac90883154f75885",
"ce21ae7b870c1012db2b9d469e95a05540ad74640c236776139e52118d39f2fc",
"0170b41f8f90f96eb95a0dfc66b959fb4e7060ed738ee162076d03597a0f468f",
"92cc9d3ed08668c5d71243ccac72b76b46c924e5cee13583665a13920b244e23",
].map((x) => Buffer.from(x, "hex").toString("hex"));

x.insert("apple (0)");

console.log("Root Hash", Buffer.from(x.branchHash).toString("hex"));
rootList.push(x.branchHash);

x.insert("apricot (0)");

rootList.push(x.branchHash);

x.insert("banana (328)");

rootList.push(x.branchHash);

x.insert("blackberry (0)");

rootList.push(x.branchHash);

x.insert("blueberry (92383)");

rootList.push(x.branchHash);

x.insert("cherry (0)");

rootList.push(x.branchHash);

x.insert("coconut (0)");

rootList.push(x.branchHash);

x.insert("cranberry (0)");

rootList.push(x.branchHash);

x.insert("durian (0)");

rootList.push(x.branchHash);

x.insert("fig (0)");

rootList.push(x.branchHash);

x.insert("grape (110606)");

rootList.push(x.branchHash);

x.insert("grapefruit (0)");

rootList.push(x.branchHash);

expect(rootList.map((x) => Buffer.from(x).toString("hex"))).toStrictEqual(
expectedList
);
});
12 changes: 10 additions & 2 deletions offchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export class Leaf {
? new TextEncoder().encode(value)
: new Uint8Array(value);

this.key = new BitSet(blake2b(bufferValue, undefined, 32));
this.key = new BitSet(
Buffer.from(blake2b(bufferValue, undefined, 32)).reverse()
);

this.value = bufferValue;

Expand Down Expand Up @@ -95,10 +97,12 @@ export class Branch {
const heightDiff = childTwo instanceof Leaf ? 0 : childTwo.height + 1;

keyOne = keyOne.slice(heightDiff);
currentHeight = heightDiff - 1;
} else if (childTwo instanceof Leaf) {
const heightDiff = childOne instanceof Leaf ? 0 : childOne.height + 1;

keyTwo = keyTwo.slice(heightDiff);
currentHeight = heightDiff - 1;
} else {
currentHeight = Math.max(childOne.height, childTwo.height);
if (childOne.height > childTwo.height) {
Expand Down Expand Up @@ -277,7 +281,11 @@ export class SparseMerkleTree extends Branch {
? new TextEncoder().encode(value)
: new Uint8Array(value);

const initialKey = new BitSet(blake2b(bufferValue, undefined, 32));
console.log(blake2bHex(bufferValue, undefined, 32));

const initialKey = new BitSet(
Buffer.from(blake2b(bufferValue, undefined, 32)).reverse()
);

super.doInsert(initialKey, value);
}
Expand Down

0 comments on commit e51d219

Please sign in to comment.