Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jul 2, 2024
1 parent 110116d commit 5775c61
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
24 changes: 13 additions & 11 deletions src/MerkleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class MerkleTree extends Base {

if (options.concatenator) {
this.concatenator = options.concatenator
}else{
} else {
this.concatenator = Buffer.concat
}

Expand Down Expand Up @@ -283,15 +283,21 @@ export class MerkleTree extends Base {
return this.leaves
}

removeLeaf(index: number): Buffer {
if (!this.isValidIdx(index)) this.invalidIdxErr(index)
removeLeaf (index: number): Buffer {
if (!this.isValidLeafIndex(index)) {
throw new Error(`"${index}" is not a valid leaf index. Expected to be [0, ${this.getLeafCount() - 1}]`)
}

const result = this.leaves.splice(index, 1)
this.processLeaves(this.leaves)
this.processLeaves(this.leaves)
return result[0]
}

updateLeaf(index: number, value: Buffer, shouldHash: boolean = false): void {
if (!this.isValidIdx(index)) this.invalidIdxErr(index)
updateLeaf (index: number, value: Buffer, shouldHash: boolean = false): void {
if (!this.isValidLeafIndex(index)) {
throw new Error(`"${index}" is not a valid leaf index. Expected to be [0, ${this.getLeafCount() - 1}]`)
}

if (shouldHash) value = this.hashFn(value)
this.leaves[index] = value
this.processLeaves(this.leaves)
Expand Down Expand Up @@ -1423,11 +1429,7 @@ export class MerkleTree extends Base {
return v && !(v & (v - 1))
}

private invalidIdxErr(idx: number): void {
throw new Error(`"${idx}" is not a valid leaf index. Expected to be [0, ${this.getLeafCount() - 1}]`)
}

private isValidIdx(idx): boolean {
isValidLeafIndex (idx: number): boolean {
return idx >= 0 && idx < this.getLeafCount()
}

Expand Down
2 changes: 1 addition & 1 deletion test/Base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('bufferify', t => {
t.deepEqual(base.bufferify(BigInt('0x123')), Buffer.from('0123', 'hex'))
t.deepEqual(base.bufferify(BigInt('0x1234')), Buffer.from('1234', 'hex'))
t.deepEqual(base.bufferify(new Uint8Array([123])), Buffer.from([123]))
t.deepEqual(base.bufferify(new Uint8Array((new Uint8Array([0,123])).buffer, 1, 1)), Buffer.from([123]))
t.deepEqual(base.bufferify(new Uint8Array((new Uint8Array([0, 123])).buffer, 1, 1)), Buffer.from([123]))
})

test('bufferifyFn', t => {
Expand Down
2 changes: 2 additions & 0 deletions test/MerkleTree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,7 @@ test('addLeaves', t => {

test('removeLeaf', t => {
t.plan(6)

const leaves = ['c', 'a', 'b'].map(keccak256)
const tree = new MerkleTree(leaves, sha256)

Expand All @@ -1262,6 +1263,7 @@ test('removeLeaf', t => {

test('removeLeaf - invalid index error', (t) => {
t.plan(1)

const leaves = ['c', 'a', 'b'].map(keccak256)
const tree = new MerkleTree(leaves, sha256)
t.throws(() => tree.removeLeaf(10), /"10" is not a valid leaf index. Expected to be \[0, 2\]/)
Expand Down

0 comments on commit 5775c61

Please sign in to comment.