feat: Update to capella fork

This commit is contained in:
Derrick Hammer 2023-06-18 03:42:56 -04:00
parent f5099a4546
commit 40d652fa22
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 579 additions and 554 deletions

View File

@ -1,5 +1,4 @@
import { import {
Bytes32,
ClientConfig, ClientConfig,
ExecutionInfo, ExecutionInfo,
LightClientUpdate, LightClientUpdate,
@ -22,17 +21,20 @@ import {
assertValidSignedHeader, assertValidSignedHeader,
} from "@lodestar/light-client/validation"; } from "@lodestar/light-client/validation";
import { SyncCommitteeFast } from "@lodestar/light-client"; import { SyncCommitteeFast } from "@lodestar/light-client";
import bls from "@chainsafe/bls/switchable"; import bls, { init } from "@chainsafe/bls/switchable";
import { PublicKey } from "@chainsafe/bls/types.js"; import { PublicKey } from "@chainsafe/bls/types.js";
import { fromHexString, toHexString } from "@chainsafe/ssz"; import { fromHexString } from "@chainsafe/ssz";
import { AsyncOrSync } from "ts-essentials";
import * as altair from "@lodestar/types/altair";
import * as phase0 from "@lodestar/types/phase0"; import * as phase0 from "@lodestar/types/phase0";
import * as bellatrix from "@lodestar/types/bellatrix"; import * as capella from "@lodestar/types/capella";
import { init } from "@chainsafe/bls/switchable";
import NodeCache from "node-cache"; import NodeCache from "node-cache";
import { Mutex } from "async-mutex"; import { Mutex } from "async-mutex";
import { VerifyingProvider } from "./rpc/index.js"; import { VerifyingProvider } from "./rpc/index.js";
import { ChainForkConfig } from "@lodestar/config";
import { allForks } from "@lodestar/types";
import {
BLOCK_BODY_EXECUTION_PAYLOAD_DEPTH as EXECUTION_PAYLOAD_DEPTH,
BLOCK_BODY_EXECUTION_PAYLOAD_INDEX as EXECUTION_PAYLOAD_INDEX,
} from "@lodestar/params";
export default class Client { export default class Client {
latestCommittee?: Uint8Array[]; latestCommittee?: Uint8Array[];
@ -163,36 +165,63 @@ export default class Client {
} }
optimisticUpdateFromJSON(update: any): OptimisticUpdate { optimisticUpdateFromJSON(update: any): OptimisticUpdate {
return altair.ssz.LightClientOptimisticUpdate.fromJson(update); return capella.ssz.LightClientOptimisticUpdate.fromJson(update);
} }
async optimisticUpdateVerify( async optimisticUpdateVerify(
committee: Uint8Array[], committee: Uint8Array[],
update: OptimisticUpdate update: OptimisticUpdate
): Promise<VerifyWithReason> { ): Promise<VerifyWithReason> {
const { attestedHeader: header, syncAggregate } = update;
const headerBlockRoot = phase0.ssz.BeaconBlockHeader.hashTreeRoot(
header.beacon
);
const committeeFast = this.deserializeSyncCommittee(committee);
try { try {
await assertValidSignedHeader( const { attestedHeader: header, syncAggregate } = update;
this.config.chainConfig, const headerBlockRoot = phase0.ssz.BeaconBlockHeader.hashTreeRoot(
committeeFast, header.beacon
syncAggregate,
headerBlockRoot,
header.beacon.slot
); );
} catch (e) { const committeeFast = this.deserializeSyncCommittee(committee);
return { correct: false, reason: "invalid signatures" }; try {
} assertValidSignedHeader(
this.config.chainConfig,
committeeFast,
syncAggregate,
headerBlockRoot,
header.beacon.slot
);
} catch (e) {
return { correct: false, reason: "invalid signatures" };
}
const participation = const participation =
syncAggregate.syncCommitteeBits.getTrueBitIndexes().length; syncAggregate.syncCommitteeBits.getTrueBitIndexes().length;
if (participation < BEACON_SYNC_SUPER_MAJORITY) { if (participation < BEACON_SYNC_SUPER_MAJORITY) {
return { correct: false, reason: "insufficient signatures" }; return { correct: false, reason: "insufficient signatures" };
}
if (!this.isValidLightClientHeader(this.config.chainConfig, header)) {
return { correct: false, reason: "invalid header" };
}
return { correct: true };
} catch (e) {
console.error(e);
return { correct: false, reason: (e as Error).message };
} }
return { correct: true }; }
private isValidLightClientHeader(
config: ChainForkConfig,
header: allForks.LightClientHeader
): boolean {
return isValidMerkleBranch(
config
.getExecutionForkTypes(header.beacon.slot)
.ExecutionPayloadHeader.hashTreeRoot(
(header as capella.LightClientHeader).execution
),
(header as capella.LightClientHeader).executionBranch,
EXECUTION_PAYLOAD_DEPTH,
EXECUTION_PAYLOAD_INDEX,
header.beacon.bodyRoot
);
} }
public async getNextValidExecutionInfo( public async getNextValidExecutionInfo(

File diff suppressed because it is too large Load Diff

View File

@ -1,25 +1,22 @@
import { import {
ContainerType,
VectorCompositeType,
ByteVectorType, ByteVectorType,
BooleanType,
UintNumberType,
ListCompositeType, ListCompositeType,
} from '@chainsafe/ssz'; VectorCompositeType,
import * as altair from '@lodestar/types/altair'; } from "@chainsafe/ssz";
import { BEACON_SYNC_COMMITTEE_SIZE } from './constants.js'; import * as capella from "@lodestar/types/capella";
import { BEACON_SYNC_COMMITTEE_SIZE } from "./constants.js";
const MAX_BATCHSIZE = 10000; const MAX_BATCHSIZE = 10000;
export const LightClientUpdateSSZ = altair.ssz.LightClientUpdate; export const LightClientUpdateSSZ = capella.ssz.LightClientUpdate;
export const LightClientUpdatesSSZ = new ListCompositeType( export const LightClientUpdatesSSZ = new ListCompositeType(
LightClientUpdateSSZ as any, LightClientUpdateSSZ as any,
MAX_BATCHSIZE, MAX_BATCHSIZE
); );
export const CommitteeSSZ = new VectorCompositeType( export const CommitteeSSZ = new VectorCompositeType(
new ByteVectorType(48), new ByteVectorType(48),
BEACON_SYNC_COMMITTEE_SIZE, BEACON_SYNC_COMMITTEE_SIZE
); );
const HashSSZ = new ByteVectorType(32); const HashSSZ = new ByteVectorType(32);

View File

@ -1,13 +1,12 @@
import { routes } from "@lodestar/api";
import { BeaconConfig } from "@lodestar/config"; import { BeaconConfig } from "@lodestar/config";
import * as altair from "@lodestar/types/altair"; import * as capella from "@lodestar/types/capella";
export type PubKeyString = string; export type PubKeyString = string;
export type Slot = number; export type Slot = number;
export type Bytes32 = string; export type Bytes32 = string;
export type OptimisticUpdate = altair.LightClientOptimisticUpdate; export type OptimisticUpdate = capella.LightClientOptimisticUpdate;
export type LightClientUpdate = altair.LightClientUpdate; export type LightClientUpdate = capella.LightClientUpdate;
export type GenesisData = { export type GenesisData = {
committee: PubKeyString[]; committee: PubKeyString[];