Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jul 1, 2024
1 parent 5f1e756 commit b2b1390
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 103 deletions.
1 change: 1 addition & 0 deletions packages/persistent-merkle-tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"clean": "rm -rf lib",
"build": "tsc",
"lint": "eslint --color --ext .ts src/",
"lint:fix": "yarn run lint --fix",
"benchmark:files": "node --max-old-space-size=4096 --expose-gc -r ts-node/register ../../node_modules/.bin/benchmark",
"benchmark": "yarn benchmark:files 'test/perf/*.test.ts'",
"benchmark:local": "yarn benchmark --local",
Expand Down
15 changes: 12 additions & 3 deletions packages/persistent-merkle-tree/src/hasher/as-sha256.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import {digest2Bytes32, digest64HashObjectsInto, digest64HashObjects, HashObject, batchHash4HashObjectInputs, hashInto} from "@chainsafe/as-sha256";
import {
digest2Bytes32,
digest64HashObjectsInto,
digest64HashObjects,
HashObject,
batchHash4HashObjectInputs,
hashInto,
} from "@chainsafe/as-sha256";
import type {Hasher} from "./types";
import {HashComputation, Node} from "../node";

Expand All @@ -23,7 +30,9 @@ export const hasher: Hasher = {
throw new Error(`Invalid nLevel, expect to be greater than 0, got ${nLevel}`);
}
if (inputLength % bytesInBatch !== 0) {
throw new Error(`Invalid input length, expect to be multiple of ${bytesInBatch} for nLevel ${nLevel}, got ${inputLength}`);
throw new Error(
`Invalid input length, expect to be multiple of ${bytesInBatch} for nLevel ${nLevel}, got ${inputLength}`
);
}
if (inputLength > MAX_INPUT_SIZE) {
throw new Error(`Invalid input length, expect to be less than ${MAX_INPUT_SIZE}, got ${inputLength}`);
Expand All @@ -35,7 +44,7 @@ export const hasher: Hasher = {
const hashInput = buffer.subarray(0, inputLength);
const hashOutput = buffer.subarray(0, outputLength);
hashInto(hashInput, hashOutput);
inputLength = outputLength
inputLength = outputLength;
}

// the result is unsafe as it will be modified later, consumer should save the result if needed
Expand Down
10 changes: 6 additions & 4 deletions packages/persistent-merkle-tree/src/hasher/hashtree.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {hashInto} from "@chainsafe/hashtree";
import {Hasher, HashObject} from "./types";
import {HashComputation, Node} from "../node";
import { byteArrayToHashObject } from "@chainsafe/as-sha256";
import { byteArrayIntoHashObject } from "@chainsafe/as-sha256/lib/hashObject";
import {byteArrayToHashObject} from "@chainsafe/as-sha256";
import {byteArrayIntoHashObject} from "@chainsafe/as-sha256/lib/hashObject";

/**
* Best SIMD implementation is in 512 bits = 64 bytes
Expand Down Expand Up @@ -49,7 +49,9 @@ export const hasher: Hasher = {
throw new Error(`Invalid nLevel, expect to be greater than 0, got ${nLevel}`);
}
if (inputLength % bytesInBatch !== 0) {
throw new Error(`Invalid input length, expect to be multiple of ${bytesInBatch} for nLevel ${nLevel}, got ${inputLength}`);
throw new Error(
`Invalid input length, expect to be multiple of ${bytesInBatch} for nLevel ${nLevel}, got ${inputLength}`
);
}
if (inputLength > MAX_INPUT_SIZE) {
throw new Error(`Invalid input length, expect to be less than ${MAX_INPUT_SIZE}, got ${inputLength}`);
Expand Down Expand Up @@ -179,4 +181,4 @@ function hashObjectsToUint32Array(obj1: HashObject, obj2: HashObject, arr: Uint3
arr[13] = obj2.h5;
arr[14] = obj2.h6;
arr[15] = obj2.h7;
}
}
2 changes: 1 addition & 1 deletion packages/persistent-merkle-tree/src/hasher/noble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const hasher: Hasher = {
digest64HashObjects: (left, right, parent) => {
byteArrayIntoHashObject(digest64(hashObjectToUint8Array(left), hashObjectToUint8Array(right)), parent);
},
digestNLevelUnsafe(data: Uint8Array, nLevel: number): Uint8Array {
digestNLevelUnsafe(): Uint8Array {
throw new Error("Not implemented");
},
batchHashObjects: (inputs: HashObject[]) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/persistent-merkle-tree/src/hasher/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type Hasher = {
* With nLevel = 3, hash multiple of 256 bytes, return multiple of 32 bytes.
* The result is unsafe as it will be overwritten by the next call.
*/
digestNLevelUnsafe(data: Uint8Array, nLevel: number): Uint8Array
digestNLevelUnsafe(data: Uint8Array, nLevel: number): Uint8Array;
/**
* Batch hash 2 * n HashObjects, return n HashObjects output
*/
Expand Down
3 changes: 1 addition & 2 deletions packages/persistent-merkle-tree/src/tree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {zeroNode} from "./zeroNode";
import {Gindex, GindexBitstring, convertGindexToBitstring} from "./gindex";
import {Node, LeafNode, BranchNode, HashComputation, HashComputationGroup, arrayAtIndex} from "./node";
import {Node, LeafNode, BranchNode, HashComputationGroup, arrayAtIndex} from "./node";
import {createNodeFromProof, createProof, Proof, ProofInput} from "./proof";
import {createSingleProof} from "./proof/single";

Expand Down Expand Up @@ -799,7 +799,6 @@ export function findDiffDepthi(from: number, to: number): number {
return findDiffDepthi32Bits(from, to);
}


/**
* Returns true if the `index` at `depth` is a left node, false if it is a right node.
*
Expand Down
4 changes: 2 additions & 2 deletions packages/ssz/src/type/byteArray.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {concatGindices, Gindex, HashComputationGroup, Node, toGindex, Tree} from "@chainsafe/persistent-merkle-tree";
import {concatGindices, Gindex, Node, toGindex, Tree} from "@chainsafe/persistent-merkle-tree";
import {fromHexString, toHexString, byteArrayEquals} from "../util/byteArray";
import {splitIntoRootChunks} from "../util/merkleize";
import {ByteViews} from "./abstract";
Expand Down Expand Up @@ -38,7 +38,7 @@ export abstract class ByteArrayType extends CompositeType<ByteArray, ByteArray,
}

// TODO - batch
commitViewDU(view: ByteArray, hashComps: HashComputationGroup | null = null): Node {
commitViewDU(view: ByteArray): Node {
const uint8Array = new Uint8Array(this.value_serializedSize(view));
const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
this.value_serializeToBytes({uint8Array, dataView}, 0, view);
Expand Down
6 changes: 2 additions & 4 deletions packages/ssz/src/type/containerNodeStruct.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HashComputationGroup, Node, subtreeFillToContents} from "@chainsafe/persistent-merkle-tree";
import {Node} from "@chainsafe/persistent-merkle-tree";
import {Type, ByteViews} from "./abstract";
import {isCompositeType} from "./composite";
import {ContainerType, ContainerOptions, renderContainerTypeName} from "./container";
Expand Down Expand Up @@ -106,9 +106,7 @@ export class ContainerNodeStructType<Fields extends Record<string, Type<unknown>
return new BranchNodeStruct(this.valueToTree.bind(this), value);
}

private valueToTree(
value: ValueOfFields<Fields>,
): Node {
private valueToTree(value: ValueOfFields<Fields>): Node {
const uint8Array = new Uint8Array(this.value_serializedSize(value));
const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
this.value_serializeToBytes({uint8Array, dataView}, 0, value);
Expand Down
4 changes: 2 additions & 2 deletions packages/ssz/src/type/optional.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {concatGindices, Gindex, HashComputationGroup, Node, Tree, zeroNode} from "@chainsafe/persistent-merkle-tree";
import {concatGindices, Gindex, Node, Tree, zeroNode} from "@chainsafe/persistent-merkle-tree";
import {mixInLength} from "../util/merkleize";
import {Require} from "../util/types";
import {namedClass} from "../util/named";
Expand Down Expand Up @@ -76,7 +76,7 @@ export class OptionalType<ElementType extends Type<unknown>> extends CompositeTy

// TODO add an OptionalViewDU
// TODO - batch
commitViewDU(view: ValueOfType<ElementType>, hashComps: HashComputationGroup | null = null): Node {
commitViewDU(view: ValueOfType<ElementType>): Node {
return this.value_toTree(view);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ssz/src/type/union.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {concatGindices, getNode, Gindex, HashComputationGroup, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {concatGindices, getNode, Gindex, Node, Tree} from "@chainsafe/persistent-merkle-tree";
import {mixInLength} from "../util/merkleize";
import {Require} from "../util/types";
import {namedClass} from "../util/named";
Expand Down Expand Up @@ -107,7 +107,7 @@ export class UnionType<Types extends Type<unknown>[]> extends CompositeType<
}

// TODO - batch
commitViewDU(view: ValueOfTypes<Types>, hashComps: HashComputationGroup | null = null): Node {
commitViewDU(view: ValueOfTypes<Types>): Node {
return this.value_toTree(view);
}

Expand Down
10 changes: 9 additions & 1 deletion packages/ssz/src/viewDU/arrayBasic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import {getHashComputations, getNodeAtDepth, getNodesAtDepth, HashComputationGroup, LeafNode, Node, setNodesAtDepth} from "@chainsafe/persistent-merkle-tree";
import {
getHashComputations,
getNodeAtDepth,
getNodesAtDepth,
HashComputationGroup,
LeafNode,
Node,
setNodesAtDepth,
} from "@chainsafe/persistent-merkle-tree";
import {ValueOf} from "../type/abstract";
import {BasicType} from "../type/basic";
import {ArrayBasicType} from "../view/arrayBasic";
Expand Down
10 changes: 8 additions & 2 deletions packages/ssz/src/viewDU/container.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import {getHashComputations, getNodeAtDepth, HashComputationGroup, LeafNode, Node, setNodesAtDepth} from "@chainsafe/persistent-merkle-tree";
import {
getHashComputations,
getNodeAtDepth,
HashComputationGroup,
LeafNode,
Node,
setNodesAtDepth,
} from "@chainsafe/persistent-merkle-tree";
import {ByteViews, Type} from "../type/abstract";
import {BasicType, isBasicType} from "../type/basic";
import {CompositeType, isCompositeType, CompositeTypeAny} from "../type/composite";
import {ContainerTypeGeneric} from "../view/container";
import {TreeViewDU} from "./abstract";
import { isNullOrUndefined } from "util";

/* eslint-disable @typescript-eslint/member-ordering */

Expand Down
12 changes: 6 additions & 6 deletions packages/ssz/test/lodestarTypes/phase0/listValidator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ListCompositeType } from "../../../src/type/listComposite";
import { Node } from "@chainsafe/persistent-merkle-tree";
import { ListCompositeTreeViewDU } from "../../../src/viewDU/listComposite";
import { ValidatorNodeStructType } from "./validator";
import { ListValidatorTreeViewDU } from "./viewDU/listValidator";
import {ListCompositeType} from "../../../src/type/listComposite";
import {Node} from "@chainsafe/persistent-merkle-tree";
import {ListCompositeTreeViewDU} from "../../../src/viewDU/listComposite";
import {ValidatorNodeStructType} from "./validator";
import {ListValidatorTreeViewDU} from "./viewDU/listValidator";

export class ListValidatorType extends ListCompositeType<ValidatorNodeStructType> {
constructor(limit: number) {
Expand All @@ -12,4 +12,4 @@ export class ListValidatorType extends ListCompositeType<ValidatorNodeStructType
getViewDU(node: Node, cache?: unknown): ListCompositeTreeViewDU<ValidatorNodeStructType> {
return new ListValidatorTreeViewDU(this, node, cache as any);
}
}
}
2 changes: 1 addition & 1 deletion packages/ssz/test/lodestarTypes/phase0/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ByteViews} from "../../../src/type/abstract";
import {ContainerNodeStructType} from "../../../src/type/containerNodeStruct";
import {ValueOfFields} from "../../../src/view/container";
import * as primitiveSsz from "../primitive/sszTypes";
import { ValidatorTreeViewDU } from "./viewDU/validator";
import {ValidatorTreeViewDU} from "./viewDU/validator";
import {Node} from "@chainsafe/persistent-merkle-tree";

const {Boolean, Bytes32, UintNum64, BLSPubkey, EpochInf} = primitiveSsz;
Expand Down
16 changes: 9 additions & 7 deletions packages/ssz/test/lodestarTypes/phase0/viewDU/listValidator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {HashComputationGroup, Node, digestNLevelUnsafe, setNodesAtDepth} from "@chainsafe/persistent-merkle-tree";
import { ListCompositeType } from "../../../../src/type/listComposite";
import { ArrayCompositeTreeViewDUCache } from "../../../../src/viewDU/arrayComposite";
import { ListCompositeTreeViewDU } from "../../../../src/viewDU/listComposite";
import { ValidatorNodeStructType } from "../validator";
import { ValidatorTreeViewDU } from "./validator";
import { ByteViews } from "../../../../src";
import {ListCompositeType} from "../../../../src/type/listComposite";
import {ArrayCompositeTreeViewDUCache} from "../../../../src/viewDU/arrayComposite";
import {ListCompositeTreeViewDU} from "../../../../src/viewDU/listComposite";
import {ValidatorNodeStructType} from "../validator";
import {ValidatorTreeViewDU} from "./validator";
import {ByteViews} from "../../../../src";

/**
* hashtree has a MAX_SIZE of 1024 bytes = 32 chunks
Expand Down Expand Up @@ -71,7 +71,9 @@ export class ListValidatorTreeViewDU extends ListCompositeTreeViewDU<ValidatorNo
}
const validatorRoots = digestNLevelUnsafe(batchLevel3Bytes, 3);
if (validatorRoots.length !== PARALLEL_FACTOR * 32) {
throw new Error(`Invalid validatorRoots length, expect ${PARALLEL_FACTOR * 32}, got ${validatorRoots.length}`);
throw new Error(
`Invalid validatorRoots length, expect ${PARALLEL_FACTOR * 32}, got ${validatorRoots.length}`
);
}
// commit all validators in this batch
for (let j = PARALLEL_FACTOR - 1; j >= 0; j--) {
Expand Down
55 changes: 30 additions & 25 deletions packages/ssz/test/lodestarTypes/phase0/viewDU/validator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { byteArrayIntoHashObject } from "@chainsafe/as-sha256";
import { BranchNodeStruct } from "../../../../src/branchNodeStruct";
import { ContainerTypeGeneric } from "../../../../src/view/container";
import { TreeViewDU } from "../../../../src/viewDU/abstract";
import { ValidatorType } from "../validator";
import {
Node,
digestNLevelUnsafe,
} from "@chainsafe/persistent-merkle-tree";
import { ByteViews } from "../../../../src/type/abstract";
import {byteArrayIntoHashObject} from "@chainsafe/as-sha256";
import {BranchNodeStruct} from "../../../../src/branchNodeStruct";
import {ContainerTypeGeneric} from "../../../../src/view/container";
import {TreeViewDU} from "../../../../src/viewDU/abstract";
import {ValidatorType} from "../validator";
import {Node, digestNLevelUnsafe} from "@chainsafe/persistent-merkle-tree";
import {ByteViews} from "../../../../src/type/abstract";
type Validator = {
pubkey: Uint8Array;
withdrawalCredentials: Uint8Array;
Expand All @@ -19,7 +16,6 @@ type Validator = {
withdrawableEpoch: number;
};

const numFields = 8;
const NUMBER_2_POW_32 = 2 ** 32;
/*
* Below constants are respective to their ssz type in `ValidatorType`.
Expand Down Expand Up @@ -218,21 +214,30 @@ export class ValidatorTreeViewDU extends TreeViewDU<ContainerTypeGeneric<typeof
}
}

/**
* Write to level3 and level4 bytes to compute merkle root. Note that this is to compute
* merkle root and it's different from serialization (which is more compressed).
* pub0 + pub1 are at level4, they will be hashed to 1st chunked of level 3
* then use 8 chunks of level 3 to compute the root hash.
* reserved withdr eff sla actElig act exit with
* level 3 |----------|----------|----------|----------|----------|----------|----------|----------|
*
* pub0 pub1
* level4 |----------|----------|
*
*/
/**
* Write to level3 and level4 bytes to compute merkle root. Note that this is to compute
* merkle root and it's different from serialization (which is more compressed).
* pub0 + pub1 are at level4, they will be hashed to 1st chunked of level 3
* then use 8 chunks of level 3 to compute the root hash.
* reserved withdr eff sla actElig act exit with
* level 3 |----------|----------|----------|----------|----------|----------|----------|----------|
*
* pub0 pub1
* level4 |----------|----------|
*
*/
export function validatorToMerkleBytes(level3: ByteViews, level4: Uint8Array, value: Validator): void {
const { pubkey, withdrawalCredentials, effectiveBalance, slashed, activationEligibilityEpoch, activationEpoch, exitEpoch, withdrawableEpoch } = value;
const { uint8Array: outputLevel3, dataView } = level3;
const {
pubkey,
withdrawalCredentials,
effectiveBalance,
slashed,
activationEligibilityEpoch,
activationEpoch,
exitEpoch,
withdrawableEpoch,
} = value;
const {uint8Array: outputLevel3, dataView} = level3;

// pubkey = 48 bytes which is 2 * CHUNK_SIZE
level4.set(pubkey, 0);
Expand Down
2 changes: 1 addition & 1 deletion packages/ssz/test/perf/eth2/hashTreeRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
hash64,
} from "../../../src";
import {CompositeTypeAny} from "../../../src/type/composite";
import { HashObject } from "@chainsafe/as-sha256";
import {HashObject} from "@chainsafe/as-sha256";

describe("HashTreeRoot frequent eth2 objects", () => {
itBenchHashTreeRoot(sszPhase0.Attestation, getAttestation(0));
Expand Down
7 changes: 3 additions & 4 deletions packages/ssz/test/perf/eth2/validators.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {itBench, setBenchOpts} from "@dapplion/benchmark";
import {itBench} from "@dapplion/benchmark";
import {Validator} from "../../lodestarTypes/phase0/types";
import {ValidatorContainer, ValidatorNodeStruct, Validators} from "../../lodestarTypes/phase0/sszTypes";
import {BranchNodeStruct, CompositeViewDU} from "../../../src";
Expand Down Expand Up @@ -69,8 +69,7 @@ describe("ContainerNodeStructViewDU vs ValidatorViewDU hashtreeroot", () => {
fn: () => {
listValidator.commit();
},
})

});

// this needs to create validator tree every time
itBench({
Expand All @@ -81,7 +80,7 @@ describe("ContainerNodeStructViewDU vs ValidatorViewDU hashtreeroot", () => {
node.h0 = null as unknown as number;
}
},
fn: () => {
fn: () => {
for (const node of nodes) {
node.root;
}
Expand Down
Loading

0 comments on commit b2b1390

Please sign in to comment.