@ensdomains/merkle-builder 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -404,6 +404,9 @@ var Coder = class {
404
404
  return c.pos;
405
405
  }
406
406
  pos = 0;
407
+ get remaining() {
408
+ return Math.max(0, this.buf.length - this.pos);
409
+ }
407
410
  reset() {
408
411
  this.pos = 0;
409
412
  }
@@ -807,96 +810,58 @@ function mimcHash(v) {
807
810
  }
808
811
 
809
812
  // src/surgery.ts
810
- function pluckLimbs(node, depth, exact) {
811
- if (!node || depth <= 0) return { trunk: node, limbs: [] };
812
- const queue = [];
813
- if (isBranch(node)) {
814
- queue.push([node, []]);
815
- } else if (isExtension(node)) {
816
- if (node.path.length >= depth) {
817
- const extension = node.path.length === depth ? node.child : { path: node.path.subarray(depth), child: node.child };
818
- return {
819
- trunk: void 0,
820
- limbs: [[node.path.subarray(0, depth), extension]]
821
- };
822
- }
823
- queue.push([node.child, [...node.path]]);
824
- } else if (node.path.length >= depth) {
825
- const leaf = newLeaf(node.path.subarray(depth), node.data);
826
- return {
827
- trunk: void 0,
828
- limbs: [[node.path.subarray(0, depth), leaf]]
829
- };
830
- }
813
+ function pluckLimbs(node, depth) {
831
814
  const limbs = [];
832
- while (queue.length) {
833
- const [parent, path] = queue.pop();
834
- parent.children.forEach((child, i) => {
835
- if (!child) return;
836
- if (path.length + 1 === depth) {
837
- parent.children[i] = void 0;
838
- limbs.push([Uint8Array.of(...path, i), child]);
839
- } else if (isBranch(child)) {
840
- queue.push([child, [...path, i]]);
841
- } else if (isExtension(child) && path.length + child.path.length <= depth) {
842
- queue.push([child.child, [...path, i, ...child.path]]);
843
- } else if (exact) {
844
- parent.children[i] = void 0;
845
- const full = [...path, i, ...child.path];
846
- const rest = new Uint8Array(full.slice(depth));
847
- limbs.push([
848
- new Uint8Array(full.slice(0, depth)),
849
- isLeaf(child) ? newLeaf(rest, child.data) : rest.length ? { path: rest, child: child.child } : child.child
850
- ]);
815
+ if (node && depth > 0) pluck(node, []);
816
+ return limbs;
817
+ function pluck(node2, path, ext = false) {
818
+ if (path.length >= depth) {
819
+ if (ext && "cache" in node2) {
820
+ node2 = { children: node2.children };
851
821
  }
852
- });
853
- }
854
- return { trunk: node, limbs };
855
- }
856
- function graftLimb(trunk, path, limb) {
857
- if (!path.length) return limb;
858
- if (!trunk) {
859
- if (isBranch(limb)) {
860
- return { path: path.slice(), child: limb };
861
- } else {
862
- const copy = { ...limb };
863
- copy.path = concat(path, copy.path);
864
- return copy;
822
+ limbs.push([new Uint8Array(path.slice(0, depth)), node2]);
823
+ return true;
824
+ } else if (isBranch(node2)) {
825
+ node2.children.forEach((x, i, v) => {
826
+ if (x && pluck(x, [...path, i])) {
827
+ v[i] = void 0;
828
+ }
829
+ });
830
+ } else if (isExtension(node2) && pluck(node2.child, [...path, ...node2.path], true)) {
831
+ node2.child = { ...node2.child, ...newBranch() };
865
832
  }
866
833
  }
867
- let start = 0;
868
- let index = 0;
869
- let node = trunk;
870
- while (index < path.length - 1) {
871
- if (isBranch(node)) {
872
- const child = node.children[path[index]];
873
- if (!child) {
874
- start = index;
875
- index = path.length - 1;
876
- break;
834
+ }
835
+ function graftLimb(trunk, [path, limb]) {
836
+ if (!trunk || !path.length) throw new Error("invalid graft");
837
+ let part = [];
838
+ let parent = void 0;
839
+ let cursor = trunk;
840
+ while (part.length < path.length) {
841
+ if (isBranch(cursor)) {
842
+ const i = path[part.length];
843
+ part.push(i);
844
+ parent = cursor;
845
+ cursor = cursor.children[i];
846
+ if (isExtension(cursor)) {
847
+ parent = cursor;
848
+ part.push(...cursor.path);
849
+ cursor = cursor.child;
877
850
  }
878
- start = ++index;
879
- node = child;
880
- } else if (isExtension(node)) {
881
- ++start;
882
- index += node.path.length;
883
- node = node.child;
851
+ } else if (isExtension(cursor)) {
852
+ parent = cursor;
853
+ part.push(...cursor.path);
854
+ cursor = cursor.child;
884
855
  } else {
885
856
  break;
886
857
  }
887
858
  }
888
- if (!isBranch(node)) throw new RangeError("invalid graft");
889
- if (start === index) {
890
- node.children[path[start]] = limb;
891
- } else if (isBranch(limb)) {
892
- node.children[path[start]] = {
893
- path: path.slice(start + 1),
894
- child: limb
895
- };
896
- } else {
897
- const copy = { ...limb };
898
- copy.path = concat(path.subarray(start + 1), copy.path);
899
- node.children[path[start]] = copy;
859
+ if (!path.every((x, i) => x === part[i])) throw new Error("invalid path");
860
+ if (isBranch(parent)) {
861
+ parent.children[path[path.length - 1]] = limb;
862
+ } else if (isExtension(parent)) {
863
+ if (!isBranch(limb)) throw new Error("invalid limb");
864
+ parent.child.children = limb.children;
900
865
  }
901
866
  return trunk;
902
867
  }
package/dist/index.d.cts CHANGED
@@ -41,6 +41,7 @@ declare class Coder {
41
41
  static getByteCount(node: MaybeNode): number;
42
42
  pos: number;
43
43
  constructor(buf?: Uint8Array);
44
+ get remaining(): number;
44
45
  reset(): void;
45
46
  expand(need: number): void;
46
47
  require(need: number): void;
@@ -74,11 +75,8 @@ declare function insertBytes(node: MaybeNode, slot: Uint8Array, value: Uint8Arra
74
75
  declare function mimcHash(v: bigint[]): bigint;
75
76
 
76
77
  type Limb = [path: Uint8Array, node: Node];
77
- declare function pluckLimbs(node: MaybeNode, depth: number, exact?: boolean): {
78
- trunk: MaybeNode;
79
- limbs: Limb[];
80
- };
81
- declare function graftLimb(trunk: MaybeNode, path: Uint8Array, limb: Node): Node;
78
+ declare function pluckLimbs(node: MaybeNode, depth: number): Limb[];
79
+ declare function graftLimb(trunk: MaybeNode, [path, limb]: Limb): Node;
82
80
 
83
81
  type Hex = `0x${string}`;
84
82
  declare function keccak256(v: Uint8Array): Uint8Array;
package/dist/index.d.ts CHANGED
@@ -41,6 +41,7 @@ declare class Coder {
41
41
  static getByteCount(node: MaybeNode): number;
42
42
  pos: number;
43
43
  constructor(buf?: Uint8Array);
44
+ get remaining(): number;
44
45
  reset(): void;
45
46
  expand(need: number): void;
46
47
  require(need: number): void;
@@ -74,11 +75,8 @@ declare function insertBytes(node: MaybeNode, slot: Uint8Array, value: Uint8Arra
74
75
  declare function mimcHash(v: bigint[]): bigint;
75
76
 
76
77
  type Limb = [path: Uint8Array, node: Node];
77
- declare function pluckLimbs(node: MaybeNode, depth: number, exact?: boolean): {
78
- trunk: MaybeNode;
79
- limbs: Limb[];
80
- };
81
- declare function graftLimb(trunk: MaybeNode, path: Uint8Array, limb: Node): Node;
78
+ declare function pluckLimbs(node: MaybeNode, depth: number): Limb[];
79
+ declare function graftLimb(trunk: MaybeNode, [path, limb]: Limb): Node;
82
80
 
83
81
  type Hex = `0x${string}`;
84
82
  declare function keccak256(v: Uint8Array): Uint8Array;
package/dist/index.js CHANGED
@@ -342,6 +342,9 @@ var Coder = class {
342
342
  return c.pos;
343
343
  }
344
344
  pos = 0;
345
+ get remaining() {
346
+ return Math.max(0, this.buf.length - this.pos);
347
+ }
345
348
  reset() {
346
349
  this.pos = 0;
347
350
  }
@@ -745,96 +748,58 @@ function mimcHash(v) {
745
748
  }
746
749
 
747
750
  // src/surgery.ts
748
- function pluckLimbs(node, depth, exact) {
749
- if (!node || depth <= 0) return { trunk: node, limbs: [] };
750
- const queue = [];
751
- if (isBranch(node)) {
752
- queue.push([node, []]);
753
- } else if (isExtension(node)) {
754
- if (node.path.length >= depth) {
755
- const extension = node.path.length === depth ? node.child : { path: node.path.subarray(depth), child: node.child };
756
- return {
757
- trunk: void 0,
758
- limbs: [[node.path.subarray(0, depth), extension]]
759
- };
760
- }
761
- queue.push([node.child, [...node.path]]);
762
- } else if (node.path.length >= depth) {
763
- const leaf = newLeaf(node.path.subarray(depth), node.data);
764
- return {
765
- trunk: void 0,
766
- limbs: [[node.path.subarray(0, depth), leaf]]
767
- };
768
- }
751
+ function pluckLimbs(node, depth) {
769
752
  const limbs = [];
770
- while (queue.length) {
771
- const [parent, path] = queue.pop();
772
- parent.children.forEach((child, i) => {
773
- if (!child) return;
774
- if (path.length + 1 === depth) {
775
- parent.children[i] = void 0;
776
- limbs.push([Uint8Array.of(...path, i), child]);
777
- } else if (isBranch(child)) {
778
- queue.push([child, [...path, i]]);
779
- } else if (isExtension(child) && path.length + child.path.length <= depth) {
780
- queue.push([child.child, [...path, i, ...child.path]]);
781
- } else if (exact) {
782
- parent.children[i] = void 0;
783
- const full = [...path, i, ...child.path];
784
- const rest = new Uint8Array(full.slice(depth));
785
- limbs.push([
786
- new Uint8Array(full.slice(0, depth)),
787
- isLeaf(child) ? newLeaf(rest, child.data) : rest.length ? { path: rest, child: child.child } : child.child
788
- ]);
753
+ if (node && depth > 0) pluck(node, []);
754
+ return limbs;
755
+ function pluck(node2, path, ext = false) {
756
+ if (path.length >= depth) {
757
+ if (ext && "cache" in node2) {
758
+ node2 = { children: node2.children };
789
759
  }
790
- });
791
- }
792
- return { trunk: node, limbs };
793
- }
794
- function graftLimb(trunk, path, limb) {
795
- if (!path.length) return limb;
796
- if (!trunk) {
797
- if (isBranch(limb)) {
798
- return { path: path.slice(), child: limb };
799
- } else {
800
- const copy = { ...limb };
801
- copy.path = concat(path, copy.path);
802
- return copy;
760
+ limbs.push([new Uint8Array(path.slice(0, depth)), node2]);
761
+ return true;
762
+ } else if (isBranch(node2)) {
763
+ node2.children.forEach((x, i, v) => {
764
+ if (x && pluck(x, [...path, i])) {
765
+ v[i] = void 0;
766
+ }
767
+ });
768
+ } else if (isExtension(node2) && pluck(node2.child, [...path, ...node2.path], true)) {
769
+ node2.child = { ...node2.child, ...newBranch() };
803
770
  }
804
771
  }
805
- let start = 0;
806
- let index = 0;
807
- let node = trunk;
808
- while (index < path.length - 1) {
809
- if (isBranch(node)) {
810
- const child = node.children[path[index]];
811
- if (!child) {
812
- start = index;
813
- index = path.length - 1;
814
- break;
772
+ }
773
+ function graftLimb(trunk, [path, limb]) {
774
+ if (!trunk || !path.length) throw new Error("invalid graft");
775
+ let part = [];
776
+ let parent = void 0;
777
+ let cursor = trunk;
778
+ while (part.length < path.length) {
779
+ if (isBranch(cursor)) {
780
+ const i = path[part.length];
781
+ part.push(i);
782
+ parent = cursor;
783
+ cursor = cursor.children[i];
784
+ if (isExtension(cursor)) {
785
+ parent = cursor;
786
+ part.push(...cursor.path);
787
+ cursor = cursor.child;
815
788
  }
816
- start = ++index;
817
- node = child;
818
- } else if (isExtension(node)) {
819
- ++start;
820
- index += node.path.length;
821
- node = node.child;
789
+ } else if (isExtension(cursor)) {
790
+ parent = cursor;
791
+ part.push(...cursor.path);
792
+ cursor = cursor.child;
822
793
  } else {
823
794
  break;
824
795
  }
825
796
  }
826
- if (!isBranch(node)) throw new RangeError("invalid graft");
827
- if (start === index) {
828
- node.children[path[start]] = limb;
829
- } else if (isBranch(limb)) {
830
- node.children[path[start]] = {
831
- path: path.slice(start + 1),
832
- child: limb
833
- };
834
- } else {
835
- const copy = { ...limb };
836
- copy.path = concat(path.subarray(start + 1), copy.path);
837
- node.children[path[start]] = copy;
797
+ if (!path.every((x, i) => x === part[i])) throw new Error("invalid path");
798
+ if (isBranch(parent)) {
799
+ parent.children[path[path.length - 1]] = limb;
800
+ } else if (isExtension(parent)) {
801
+ if (!isBranch(limb)) throw new Error("invalid limb");
802
+ parent.child.children = limb.children;
838
803
  }
839
804
  return trunk;
840
805
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ensdomains/merkle-builder",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Low-level Merkle-Patricia storage trie implementation",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",