Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: integrate peerdas-kzg #6923

Draft
wants to merge 25 commits into
base: peerDAS
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
08884f8
feat: placeholder PR for electra
g11tech Jan 24, 2024
b1481f1
feat: implement peerDAS on electra
g11tech Jan 24, 2024
7dab01e
fix: docker build issue for c-kzg
matthewkeil Jun 21, 2024
ae1a03e
rebase fixes
g11tech Jun 26, 2024
38fc1b4
fix: update c-zkg install workflow
matthewkeil Jun 26, 2024
2b26a4e
feat: add trustedSetupPrecompute cli flag
matthewkeil Jun 26, 2024
33e8ba2
fix: update trusted-setup for testing
matthewkeil Jun 26, 2024
c210176
fix: update c-zkg install workflow to remove sudo
matthewkeil Jun 26, 2024
952b65f
add dep
kevaundray Jun 29, 2024
ca67a07
update peerdas lib
kevaundray Jun 30, 2024
1cb31a4
add rough idea
kevaundray Jun 30, 2024
f2d2917
modify code to call the exported computeCellsAndProofs method
kevaundray Jun 30, 2024
83a137c
add false to use ckzg
kevaundray Jun 30, 2024
ae0c46c
add todo re lib switching
kevaundray Jun 30, 2024
50dfe26
revert yarn.lock
kevaundray Jun 30, 2024
89ca708
yarn add "@crate-crypto/[email protected]" --exact
kevaundray Jun 30, 2024
02f7c67
use tsc --define to set the library to use
kevaundray Jun 30, 2024
039f170
Merge branch 'peerDAS' into kw/peerdas-rust-lib-integration
kevaundray Aug 16, 2024
313af86
use v0.4.1 plus name-change
kevaundray Aug 16, 2024
5e9c747
refactor: package name has changed
kevaundray Aug 16, 2024
f8f5bf8
yarn lint
kevaundray Aug 16, 2024
5557728
Fix: github has problems merging branch due to force merge
kevaundray Aug 16, 2024
c03f297
revert lockfile and regenerate by running `yarn`
kevaundray Aug 16, 2024
3b69703
change import to node-eth-kzg
kevaundray Aug 16, 2024
0f04d36
update API and add notes on how to upgrade
kevaundray Aug 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/beacon-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"@chainsafe/prometheus-gc-stats": "^1.0.0",
"@chainsafe/ssz": "^0.17.0",
"@chainsafe/threads": "^1.11.1",
"@crate-crypto/node-eth-kzg": "0.4.1",
"@ethersproject/abi": "^5.7.0",
"@fastify/bearer-auth": "^9.0.0",
"@fastify/cors": "^8.2.1",
Expand Down Expand Up @@ -167,4 +168,4 @@
"beacon",
"blockchain"
]
}
}
7 changes: 7 additions & 0 deletions packages/beacon-node/src/network/peers/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const MAX_CACHED_ENRS = 100;
/** Max age a cached ENR will be considered for dial */
const MAX_CACHED_ENR_AGE_MS = 5 * 60 * 1000;

const MAX_CACHED_NODEIDS = 10000;

kevaundray marked this conversation as resolved.
Show resolved Hide resolved
export type PeerDiscoveryOpts = {
maxPeers: number;
discv5FirstQueryDelayMs: number;
Expand Down Expand Up @@ -354,6 +356,11 @@ export class PeerDiscovery {
}
// async due to some crypto that's no longer necessary
const peerId = await enr.peerId();

const nodeId = fromHexString(enr.nodeId);
this.peerIdToNodeId.set(peerId.toString(), nodeId);
pruneSetToMax(this.peerIdToNodeId, MAX_CACHED_NODEIDS);

kevaundray marked this conversation as resolved.
Show resolved Hide resolved
// tcp multiaddr is known to be be present, checked inside the worker
const multiaddrTCP = enr.getLocationMultiaddr(ENRKey.tcp);
if (!multiaddrTCP) {
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/network/peers/peerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ export class PeerManager {
agentVersion: null,
agentClient: null,
encodingPreference: null,
custodySubnetCount,
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
};
this.connectedPeers.set(remotePeer.toString(), peerData);

Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/network/peers/peersData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type PeerData = {
agentVersion: string | null;
agentClient: ClientKind | null;
encodingPreference: Encoding | null;
custodySubnetCount: number | null;
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand Down
41 changes: 41 additions & 0 deletions packages/beacon-node/src/util/kzg.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import path from "node:path";
import fs from "node:fs";
import {fileURLToPath} from "node:url";
import {ProverContextJs, VerifierContextJs} from "@crate-crypto/peerdas-kzg";
import {fromHex, toHex} from "@lodestar/utils";

/* eslint-disable @typescript-eslint/naming-convention */

// "c-kzg" has hardcoded the mainnet value, do not use params
export const FIELD_ELEMENTS_PER_BLOB_MAINNET = 4096;

// A constant to switch between the two libraries at compile time
declare let USE_PEERDAS_KZG: boolean | undefined;
if (typeof USE_PEERDAS_KZG === "undefined") {
USE_PEERDAS_KZG = false; // use c-kzg by default, if the constant is not set
}

function ckzgNotLoaded(): never {
throw Error("c-kzg library not loaded");
}
Expand All @@ -19,9 +26,12 @@ export let ckzg: {
computeBlobKzgProof(blob: Uint8Array, commitment: Uint8Array): Uint8Array;
verifyBlobKzgProof(blob: Uint8Array, commitment: Uint8Array, proof: Uint8Array): boolean;
verifyBlobKzgProofBatch(blobs: Uint8Array[], expectedKzgCommitments: Uint8Array[], kzgProofs: Uint8Array[]): boolean;
// TODO:This is deprecated
computeCells(blob: Uint8Array): Uint8Array[];
computeCellsAndKzgProofs(blob: Uint8Array): [Uint8Array[], Uint8Array[]];
// TODO:This is deprecated
cellsToBlob(cells: Uint8Array[]): Uint8Array;
// TODO:This is deprecated
recoverAllCells(cellIds: number[], cells: Uint8Array[]): Uint8Array[];
verifyCellKzgProof(commitmentBytes: Uint8Array, cellId: number, cell: Uint8Array, proofBytes: Uint8Array): boolean;
verifyCellKzgProofBatch(
Expand Down Expand Up @@ -60,6 +70,10 @@ const G1POINT_COUNT = FIELD_ELEMENTS_PER_BLOB_MAINNET;
const G2POINT_COUNT = 65;
const TOTAL_SIZE = 2 * POINT_COUNT_BYTES + G1POINT_BYTES * G1POINT_COUNT + G2POINT_BYTES * G2POINT_COUNT;

// TODO: We can think of a better way to initialize this
let proverContext: ProverContextJs;
let verifierContext: VerifierContextJs;

export async function initCKZG(): Promise<void> {
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
Expand All @@ -83,6 +97,8 @@ export function loadEthereumTrustedSetup(
filePath?: string
): void {
try {
proverContext = new ProverContextJs();
verifierContext = new VerifierContextJs();
let setupFilePath;
if (mode === TrustedFileMode.Bin) {
const binPath = filePath ?? TRUSTED_SETUP_BIN_FILEPATH;
Expand Down Expand Up @@ -183,3 +199,28 @@ function strip0xPrefix(hex: string): string {
return hex;
}
}

export function computeCellsAndKzgProofs(blob: Uint8Array): [Uint8Array[], Uint8Array[]] {
if (USE_PEERDAS_KZG) {
const cellsAndProofs = proverContext.computeCellsAndKzgProofs(blob);
return [cellsAndProofs.cells, cellsAndProofs.proofs];
} else {
return ckzg.computeCellsAndKzgProofs(blob);
}
}

// This API is not correct imo, its concatenating all of the proofs and cells, which is a c-kzg
// specific thing.
// export function verifyCellKzgProofBatch(
// commitmentsBytes: Uint8Array[],
// rowIndices: number[],
// columnIndices: number[],
// cells: Uint8Array[],
// proofsBytes: Uint8Array[]
// ): boolean {
// if (USE_PEERDAS_KZG) {
// return verifierContext.verifyCellKzgProofBatch(commitmentsBytes, rowIndices, columnIndices, cells, proofsBytes)
// } else {
// return ckzg.verifyCellKzgProofBatch(commitmentsBytes, rowIndices, columnIndices, cells, proofsBytes);
// }
// }
1 change: 1 addition & 0 deletions packages/cli/src/cmds/beacon/initPeerIdAndEnr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function overwriteEnrWithCliArgs(
maybeUpdateEnr(enr, "tcp", args["enr.tcp"] ?? port ?? enr.tcp);
maybeUpdateEnr(enr, "tcp6", args["enr.tcp6"] ?? port6 ?? enr.tcp6);
}
enr.set("custody_subnet_count", ssz.UintNum64.serialize(config.CUSTODY_REQUIREMENT));
kevaundray marked this conversation as resolved.
Show resolved Hide resolved

// csc is big ending but since 1 bytes suffices for now so its the same
enr.set("csc", ssz.Uint8.serialize(Math.max(config.CUSTODY_REQUIREMENT, config.NODE_CUSTODY_REQUIREMENT)));
Expand Down
34 changes: 34 additions & 0 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,40 @@ type TypesByFork = {
SyncCommittee: altair.SyncCommittee;
SyncAggregate: altair.SyncAggregate;
};
[ForkName.electra]: {
BeaconBlockHeader: phase0.BeaconBlockHeader;
SignedBeaconBlockHeader: phase0.SignedBeaconBlockHeader;
BeaconBlock: electra.BeaconBlock;
BeaconBlockBody: electra.BeaconBlockBody;
BeaconState: electra.BeaconState;
SignedBeaconBlock: electra.SignedBeaconBlock;
Metadata: altair.Metadata;
LightClientHeader: electra.LightClientHeader;
LightClientBootstrap: electra.LightClientBootstrap;
LightClientUpdate: electra.LightClientUpdate;
LightClientFinalityUpdate: electra.LightClientFinalityUpdate;
LightClientOptimisticUpdate: electra.LightClientOptimisticUpdate;
LightClientStore: electra.LightClientStore;
BlindedBeaconBlock: electra.BlindedBeaconBlock;
BlindedBeaconBlockBody: electra.BlindedBeaconBlockBody;
SignedBlindedBeaconBlock: electra.SignedBlindedBeaconBlock;
ExecutionPayload: electra.ExecutionPayload;
ExecutionPayloadHeader: electra.ExecutionPayloadHeader;
BuilderBid: electra.BuilderBid;
SignedBuilderBid: electra.SignedBuilderBid;
SSEPayloadAttributes: electra.SSEPayloadAttributes;
BlockContents: {block: BeaconBlock<ForkName.electra>; kzgProofs: deneb.KZGProofs; blobs: deneb.Blobs};
SignedBlockContents: {
signedBlock: SignedBeaconBlock<ForkName.electra>;
kzgProofs: deneb.KZGProofs;
blobs: deneb.Blobs;
};
ExecutionPayloadAndBlobsBundle: deneb.ExecutionPayloadAndBlobsBundle;
BlobsBundle: deneb.BlobsBundle;
Contents: deneb.Contents;
SyncCommittee: altair.SyncCommittee;
SyncAggregate: altair.SyncAggregate;
};
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
};

export type TypesFor<F extends ForkName, K extends keyof TypesByFork[F] | void = void> = K extends void
Expand Down
Loading