feat: Update to capella fork

This commit is contained in:
Derrick Hammer 2023-06-18 03:19:54 -04:00
parent 5a03605ac8
commit e62eadd436
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 644 additions and 649 deletions

View File

@ -9,10 +9,10 @@
"build": "npm run compile && node ./dist-build/build.mjs dev"
},
"dependencies": {
"@chainsafe/as-sha256": "^0.3.1",
"@chainsafe/as-sha256": "0.4.1",
"@chainsafe/bls": "git+https://git.lumeweb.com/LumeWeb/chainsafe-bls.git",
"@chainsafe/blst": "^0.2.8",
"@chainsafe/ssz": "^0.10.2",
"@chainsafe/ssz": "0.11.1",
"@ethereumjs/block": "^4.2.1",
"@ethereumjs/blockchain": "^6.2.1",
"@ethereumjs/common": "^3.1.1",
@ -20,9 +20,10 @@
"@ethereumjs/tx": "^4.1.1",
"@ethereumjs/util": "^8.0.5",
"@ethereumjs/vm": "^6.4.1",
"@lodestar/api": "^1.7.2",
"@lodestar/config": "^1.7.2",
"@lodestar/api": "1.7.2",
"@lodestar/config": "1.7.2",
"@lodestar/light-client": "^1.7.2",
"@lodestar/params": "^1.8.0",
"@lodestar/types": "^1.7.2",
"@lumeweb/kernel-rpc-client": "git+https://git.lumeweb.com/LumeWeb/kernel-rpc-client.git",
"decimal.js": "^10.4.3",
@ -59,10 +60,5 @@
"crypto": "crypto-browserify",
"stream": "stream-browserify",
"path": "path-browserify"
},
"pnpm": {
"overrides": {
"@chainsafe/bls": "git+https://git.lumeweb.com/LumeWeb/chainsafe-bls.git"
}
}
}

View File

@ -1,8 +1,6 @@
import {
Bytes32,
ClientConfig,
ExecutionInfo,
LightClientUpdate,
OptimisticUpdate,
VerifyWithReason,
} from "./types.js";
@ -20,21 +18,23 @@ import {
import {
computeSyncPeriodAtSlot,
getCurrentSlot,
isValidMerkleBranch,
} from "@lodestar/light-client/utils";
import {
assertValidLightClientUpdate,
assertValidSignedHeader,
} from "@lodestar/light-client/validation";
import { assertValidSignedHeader } from "@lodestar/light-client/validation";
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 { fromHexString, toHexString } from "@chainsafe/ssz";
import * as altair from "@lodestar/types/altair";
import { fromHexString } from "@chainsafe/ssz";
import * as capella from "@lodestar/types/capella";
import * as phase0 from "@lodestar/types/phase0";
import * as bellatrix from "@lodestar/types/bellatrix";
import { init } from "@chainsafe/bls/switchable";
import { VerifyingProvider } from "./rpc/provider.js";
import { digest } from "@chainsafe/as-sha256";
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 {
latestCommittee?: Uint8Array[];
@ -117,13 +117,14 @@ export default class Client {
committee: Uint8Array[],
update: OptimisticUpdate
): Promise<VerifyWithReason> {
try {
const { attestedHeader: header, syncAggregate } = update;
const headerBlockRoot = phase0.ssz.BeaconBlockHeader.hashTreeRoot(
header.beacon
);
const committeeFast = this.deserializeSyncCommittee(committee);
try {
await assertValidSignedHeader(
assertValidSignedHeader(
this.config.chainConfig,
committeeFast,
syncAggregate,
@ -139,7 +140,33 @@ export default class Client {
if (participation < BEACON_SYNC_SUPER_MAJORITY) {
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 };
}
}
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(
@ -247,33 +274,9 @@ export default class Client {
console.log(
`Optimistic update verified for slot ${updateJSON.attested_header.beacon.slot}`
);
return this.getExecutionFromBlockRoot(
updateJSON.attested_header.beacon.slot,
updateJSON.attested_header.beacon.body_root
);
}
private async getExecutionFromBlockRoot(
slot: bigint,
expectedBlockRoot: Bytes32
): Promise<ExecutionInfo> {
const res = await this.prover.callback("consensus_block", {
block: slot,
});
const blockJSON = res.message.body;
const block = bellatrix.ssz.BeaconBlockBody.fromJson(blockJSON);
const blockRoot = toHexString(
bellatrix.ssz.BeaconBlockBody.hashTreeRoot(block)
);
if (blockRoot !== expectedBlockRoot) {
throw Error(
`block provided by the beacon chain api doesn't match the expected block root`
);
}
return {
blockhash: blockJSON.execution_payload.block_hash,
blockNumber: blockJSON.execution_payload.block_number,
blockhash: updateJSON.attested_header.execution.block_hash,
blockNumber: updateJSON.attested_header.execution.block_number,
};
}
@ -294,6 +297,6 @@ export default class Client {
return digest(concatUint8Array(committee));
}
private optimisticUpdateFromJSON(update: any): OptimisticUpdate {
return altair.ssz.LightClientOptimisticUpdate.fromJson(update);
return capella.ssz.LightClientOptimisticUpdate.fromJson(update);
}
}

File diff suppressed because it is too large Load Diff

View File

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

View File

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