Skip to content

Commit

Permalink
Update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ensi321 committed Oct 23, 2024
1 parent 2d88eb8 commit e2ea96d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/beacon-node/src/chain/lightClient/proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function getSyncCommitteesWitness(fork: ForkName, state: BeaconStateAllFo
n21.left.root, // 42
n10.left.root, // 20
n5.right.root, // 11
n2.right.root, // 4
n2.left.root, // 4
n1.right.root, // 3
];
} else {
Expand Down
105 changes: 89 additions & 16 deletions packages/beacon-node/test/unit/chain/lightclient/proof.test.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,146 @@
import {describe, it, expect, beforeAll} from "vitest";
import {BeaconStateAltair} from "@lodestar/state-transition";
import {BeaconStateAltair, BeaconStateElectra} from "@lodestar/state-transition";
import {ForkName, SYNC_COMMITTEE_SIZE} from "@lodestar/params";
import {altair, ssz} from "@lodestar/types";
import {verifyMerkleBranch, hash} from "@lodestar/utils";
import {getNextSyncCommitteeBranch, getSyncCommitteesWitness} from "../../../../src/chain/lightClient/proofs.js";
import {NUM_WITNESS, NUM_WITNESS_ELECTRA} from "../../../../src/db/repositories/lightclientSyncCommitteeWitness.js";

const currentSyncCommitteeGindex = 54;
const nextSyncCommitteeGindex = 55;
const syncCommitteesGindex = 27;
const currentSyncCommitteeGindexElectra = 86;
const nextSyncCommitteeGindexElectra = 87;
const syncCommitteesGindexElectra = 43;

describe("chain / lightclient / proof", () => {
let state: BeaconStateAltair;
let stateRoot: Uint8Array;
let stateAltair: BeaconStateAltair;
let stateElectra: BeaconStateElectra;
let stateRootAltair: Uint8Array;
let stateRootElectra: Uint8Array;

const currentSyncCommittee = fillSyncCommittee(Buffer.alloc(48, 0xbb));
const nextSyncCommittee = fillSyncCommittee(Buffer.alloc(48, 0xcc));

beforeAll(() => {
state = ssz.altair.BeaconState.defaultViewDU();
state.currentSyncCommittee = ssz.altair.SyncCommittee.toViewDU(currentSyncCommittee);
state.nextSyncCommittee = ssz.altair.SyncCommittee.toViewDU(nextSyncCommittee);
stateAltair = ssz.altair.BeaconState.defaultViewDU();
stateAltair.currentSyncCommittee = ssz.altair.SyncCommittee.toViewDU(currentSyncCommittee);
stateAltair.nextSyncCommittee = ssz.altair.SyncCommittee.toViewDU(nextSyncCommittee);
// Note: .hashTreeRoot() automatically commits()
stateRoot = state.hashTreeRoot();
stateRootAltair = stateAltair.hashTreeRoot();

stateElectra = ssz.electra.BeaconState.defaultViewDU();
stateElectra.currentSyncCommittee = ssz.altair.SyncCommittee.toViewDU(currentSyncCommittee);
stateElectra.nextSyncCommittee = ssz.altair.SyncCommittee.toViewDU(nextSyncCommittee);
stateRootElectra = stateElectra.hashTreeRoot();
});

it("SyncCommittees proof", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.altair, state);
it("SyncCommittees proof altair", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.altair, stateAltair);
const syncCommitteesLeaf = hash(
syncCommitteesWitness.currentSyncCommitteeRoot,
syncCommitteesWitness.nextSyncCommitteeRoot
);

expect(syncCommitteesWitness.witness.length).toBe(NUM_WITNESS);
expect(
verifyMerkleBranch(
syncCommitteesLeaf,
syncCommitteesWitness.witness,
...fromGindex(syncCommitteesGindex),
stateRoot
stateRootAltair
)
).toBe(true);
});

it("currentSyncCommittee proof", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.altair, state);
it("currentSyncCommittee proof altair", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.altair, stateAltair);
const currentSyncCommitteeBranch = [syncCommitteesWitness.nextSyncCommitteeRoot, ...syncCommitteesWitness.witness];

expect(syncCommitteesWitness.witness.length).toBe(NUM_WITNESS);
expect(
verifyMerkleBranch(
ssz.altair.SyncCommittee.hashTreeRoot(currentSyncCommittee),
currentSyncCommitteeBranch,
...fromGindex(currentSyncCommitteeGindex),
stateRoot
stateRootAltair
)
).toBe(true);
});

it("nextSyncCommittee proof", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.altair, state);
it("nextSyncCommittee proof altair", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.altair, stateAltair);
const nextSyncCommitteeBranch = getNextSyncCommitteeBranch(syncCommitteesWitness);

expect(syncCommitteesWitness.witness.length).toBe(NUM_WITNESS);
expect(
verifyMerkleBranch(
ssz.altair.SyncCommittee.hashTreeRoot(nextSyncCommittee),
nextSyncCommitteeBranch,
...fromGindex(nextSyncCommitteeGindex),
stateRoot
stateRootAltair
)
).toBe(true);
});

it("SyncCommittees proof electra", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.electra, stateElectra);
const syncCommitteesLeaf = hash(
syncCommitteesWitness.currentSyncCommitteeRoot,
syncCommitteesWitness.nextSyncCommitteeRoot
);

expect(syncCommitteesWitness.witness.length).toBe(NUM_WITNESS_ELECTRA);
expect(
verifyMerkleBranch(
syncCommitteesLeaf,
syncCommitteesWitness.witness,
...fromGindex(syncCommitteesGindexElectra),
stateRootElectra
)
).toBe(true);
});

it("currentSyncCommittee proof electra", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.electra, stateElectra);
const currentSyncCommitteeBranch = [syncCommitteesWitness.nextSyncCommitteeRoot, ...syncCommitteesWitness.witness];

expect(syncCommitteesWitness.witness.length).toBe(NUM_WITNESS_ELECTRA);
expect(
verifyMerkleBranch(
ssz.altair.SyncCommittee.hashTreeRoot(currentSyncCommittee),
currentSyncCommitteeBranch,
...fromGindex(currentSyncCommitteeGindexElectra),
stateRootElectra
)
).toBe(true);
});

it("nextSyncCommittee proof electra", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.electra, stateElectra);
const nextSyncCommitteeBranch = getNextSyncCommitteeBranch(syncCommitteesWitness);

expect(
verifyMerkleBranch(
ssz.altair.SyncCommittee.hashTreeRoot(nextSyncCommittee),
nextSyncCommitteeBranch,
...fromGindex(nextSyncCommitteeGindexElectra),
stateRootElectra
)
).toBe(true);
});

it("getSyncCommitteesWitness returns correct number of witness altair", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.altair, stateAltair);

expect(syncCommitteesWitness.witness.length).toBe(NUM_WITNESS);
});

it("getSyncCommitteesWitness returns correct number of witness electra", () => {
const syncCommitteesWitness = getSyncCommitteesWitness(ForkName.electra, stateElectra);

expect(syncCommitteesWitness.witness.length).toBe(NUM_WITNESS_ELECTRA);
});
});

function fillSyncCommittee(pubkey: Uint8Array): altair.SyncCommittee {
Expand Down

0 comments on commit e2ea96d

Please sign in to comment.