Skip to content

Commit

Permalink
Change types as path and nextPath are bigint and not number
Browse files Browse the repository at this point in the history
  • Loading branch information
ejMina226 committed Oct 31, 2024
1 parent c1dc6ee commit 1c3e27e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/common/src/trees/InMemoryLinkedMerkleTreeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class InMemoryLinkedMerkleTreeStorage implements LinkedMerkleTreeStore {
this.leaves[index.toString()] = value;
}

public getLeafIndex(path: number): bigint | undefined {
public getLeafIndex(path: bigint): bigint | undefined {
const leafIndex = Object.keys(this.leaves).find((key) => {
return this.leaves[key].path === path;
});
Expand All @@ -42,7 +42,7 @@ export class InMemoryLinkedMerkleTreeStorage implements LinkedMerkleTreeStore {
}

// This gets the leaf with the closest path.
public getPathLessOrEqual(path: number): LinkedLeaf {
public getPathLessOrEqual(path: bigint): LinkedLeaf {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
let largestLeaf = this.getLeaf(0n) as LinkedLeaf;
while (largestLeaf.nextPath <= path) {
Expand Down
22 changes: 11 additions & 11 deletions packages/common/src/trees/LinkedMerkleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ export interface AbstractLinkedMerkleTree {
* @param path of the leaf node.
* @param value New value.
*/
setValue(path: number, value: bigint): void;
setValue(path: bigint, value: bigint): void;

/**
* Returns a leaf which lives at a given path.
* @param path Index of the node.
* @returns The data of the leaf.
*/
getLeaf(path: number): LinkedLeaf;
getLeaf(path: bigint): LinkedLeaf;

/**
* Returns a leaf which is closest to a given path.
* @param path Index of the node.
* @returns The data of the leaf.
*/
getPathLessOrEqual(path: number): LinkedLeaf;
getPathLessOrEqual(path: bigint): LinkedLeaf;

/**
* Returns the witness (also known as
Expand All @@ -81,7 +81,7 @@ export interface AbstractLinkedMerkleTree {
* @param path Position of the leaf node.
* @returns The witness that belongs to the leaf.
*/
getWitness(path: number): AbstractLinkedMerkleWitness;
getWitness(path: bigint): AbstractLinkedMerkleWitness;
}

export interface AbstractLinkedMerkleTreeClass {
Expand Down Expand Up @@ -210,7 +210,7 @@ export function createLinkedMerkleTree(
* @param path path of the node.
* @returns The data of the node.
*/
public getLeaf(path: number): LinkedLeaf {
public getLeaf(path: bigint): LinkedLeaf {
const index = this.store.getLeafIndex(path);
if (index === undefined) {
throw new Error("Path does not exist in tree.");
Expand All @@ -230,7 +230,7 @@ export function createLinkedMerkleTree(
* Returns the leaf with a path either equal to or less than the path specified.
* @param path Position of the leaf node.
* */
public getPathLessOrEqual(path: number): LinkedLeaf {
public getPathLessOrEqual(path: bigint): LinkedLeaf {
const closestLeaf = this.store.getPathLessOrEqual(path);
return {
value: Field(closestLeaf.value),
Expand Down Expand Up @@ -284,7 +284,7 @@ export function createLinkedMerkleTree(
* @param path Position of the leaf node.
* @param value New value.
*/
public setValue(path: number, value: bigint) {
public setValue(path: bigint, value: bigint) {
let index = this.store.getLeafIndex(path);
const prevLeaf = this.store.getPathLessOrEqual(path);
if (index === undefined) {
Expand Down Expand Up @@ -323,13 +323,13 @@ export function createLinkedMerkleTree(
* i.e. {vale: 0, path: 0, nextPath: Field.Max}
*/
private setLeafInitialisation() {
const MAX_FIELD_VALUE = 2 ** 1000000;
const MAX_FIELD_VALUE: bigint = BigInt(2 ** 53 - 1);
this.store.setLeaf(0n, {
value: 0n,
path: 0,
path: 0n,
nextPath: MAX_FIELD_VALUE,
});
const initialLeaf = this.getLeaf(0);
const initialLeaf = this.getLeaf(0n);
this.setLeaf(0n, initialLeaf);
}

Expand All @@ -340,7 +340,7 @@ export function createLinkedMerkleTree(
* @param path of the leaf node.
* @returns The witness that belongs to the leaf.
*/
public getWitness(path: number): LinkedMerkleWitness {
public getWitness(path: bigint): LinkedMerkleWitness {
const index = this.store.getLeafIndex(path);
if (index === undefined) {
throw new Error("Path does not exist in tree.");
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/trees/LinkedMerkleTreeStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export interface LinkedMerkleTreeStore {

getLeaf: (index: bigint) => LinkedLeaf | undefined;

getLeafIndex: (path: number) => bigint | undefined;
getLeafIndex: (path: bigint) => bigint | undefined;

getPathLessOrEqual: (path: number) => LinkedLeaf;
getPathLessOrEqual: (path: bigint) => LinkedLeaf;

getMaximumIndex: () => bigint;
}

export type LinkedLeaf = { value: bigint; path: number; nextPath: number };
export type LinkedLeaf = { value: bigint; path: bigint; nextPath: bigint };

0 comments on commit 1c3e27e

Please sign in to comment.