Skip to content

Commit

Permalink
Add altair merkle test
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Sep 29, 2021
1 parent dabb97a commit e503eba
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/spec-test-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@chainsafe/lodestar-utils": "^0.30.0",
"@chainsafe/lodestar-validator": "^0.30.0",
"@chainsafe/ssz": "^0.8.17",
"@chainsafe/persistent-merkle-tree": "^0.3.7",
"@types/yargs": "^17.0.2"
},
"keywords": [
Expand Down
61 changes: 61 additions & 0 deletions packages/spec-test-runner/test/spec/altair/merkle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {join} from "path";
import {altair, ssz} from "@chainsafe/lodestar-types";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {ProofType, SingleProof} from "@chainsafe/persistent-merkle-tree";
import {ACTIVE_PRESET} from "@chainsafe/lodestar-params";
import {fromHexString, toHexString, TreeBacked} from "@chainsafe/ssz";
import {SPEC_TEST_LOCATION} from "../../specTestVersioning";
import {IBaseSpecTest} from "../type";
import {verifyMerkleBranch} from "@chainsafe/lodestar-utils";
import {expect} from "chai";

describeDirectorySpecTest<IMerkleTestCase, IProof>(
`${ACTIVE_PRESET}/altair/transition`,
join(SPEC_TEST_LOCATION, `/tests/${ACTIVE_PRESET}/altair/merkle/single_proof/pyspec_tests`),
(testcase) => {
const {proof: specTestProof, state} = testcase;
const stateTB = state as TreeBacked<altair.BeaconState>;
const stateRoot = stateTB.hashTreeRoot();
const leaf = fromHexString(specTestProof.leaf);
const branch = specTestProof.branch.map((item) => fromHexString(item));
const depth = Math.floor(Math.log2(Number(specTestProof.leafIndex)));
const verified = verifyMerkleBranch(leaf, branch, depth, Number(specTestProof.leafIndex) % 2 ** depth, stateRoot);
expect(verified, "cannot verify merkle branch").to.be.true;
const lodestarProof = stateTB.tree.getProof({
gindex: specTestProof.leafIndex,
type: ProofType.single,
}) as SingleProof;
return {
leaf: toHexString(lodestarProof.leaf),
leafIndex: lodestarProof.gindex,
branch: lodestarProof.witnesses.map(toHexString),
};
},
{
inputTypes: {
state: {type: InputType.SSZ_SNAPPY as const, treeBacked: true as const},
proof: InputType.YAML as const,
},
getSszTypes: () => {
return {
state: ssz.altair.BeaconState,
};
},
timeout: 10000,
getExpected: (testCase) => testCase.proof,
expectFunc: (testCase, expected, actual) => {
expect(actual).to.be.deep.equal(expected, "incorrect proof");
},
}
);

interface IMerkleTestCase extends IBaseSpecTest {
state: altair.BeaconState;
proof: IProof;
}

interface IProof {
leaf: string;
leafIndex: bigint;
branch: string[];
}

0 comments on commit e503eba

Please sign in to comment.