2023-07-11 07:24:38 +00:00
|
|
|
import {
|
|
|
|
createBeaconConfig,
|
|
|
|
BeaconConfig,
|
|
|
|
ChainForkConfig,
|
|
|
|
} from "@lodestar/config";
|
|
|
|
import { allForks } from "@lodestar/types";
|
2023-07-10 20:44:58 +00:00
|
|
|
import { BEACON_SYNC_SUPER_MAJORITY, mainnetConfig } from "./constants.js";
|
|
|
|
import { networksChainConfig } from "@lodestar/config/networks";
|
|
|
|
import { fromHexString } from "@chainsafe/ssz";
|
|
|
|
import { capella, OptimisticUpdate, phase0, VerifyWithReason } from "#types.js";
|
|
|
|
import { assertValidSignedHeader } from "@lodestar/light-client/validation";
|
|
|
|
import bls from "@chainsafe/bls/switchable";
|
|
|
|
import axiosRetry from "axios-retry";
|
|
|
|
import axios from "axios";
|
|
|
|
import { digest } from "@chainsafe/as-sha256";
|
|
|
|
import { concatBytes } from "@noble/hashes/utils";
|
2023-07-11 07:24:38 +00:00
|
|
|
import {
|
|
|
|
deserializeSyncCommittee,
|
|
|
|
isValidMerkleBranch,
|
|
|
|
} from "@lodestar/light-client/utils";
|
|
|
|
import {
|
|
|
|
BLOCK_BODY_EXECUTION_PAYLOAD_DEPTH as EXECUTION_PAYLOAD_DEPTH,
|
|
|
|
BLOCK_BODY_EXECUTION_PAYLOAD_INDEX as EXECUTION_PAYLOAD_INDEX,
|
|
|
|
} from "@lodestar/params";
|
2023-07-10 20:44:58 +00:00
|
|
|
|
|
|
|
export function getDefaultClientConfig() {
|
|
|
|
const chainConfig = createBeaconConfig(
|
|
|
|
networksChainConfig.mainnet,
|
|
|
|
fromHexString(mainnetConfig.genesis_validator_root),
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
genesis: {
|
|
|
|
committee: mainnetConfig.committee_pk,
|
|
|
|
slot: parseInt(mainnetConfig.slot),
|
|
|
|
time: parseInt(mainnetConfig.genesis_time),
|
|
|
|
},
|
|
|
|
chainConfig,
|
|
|
|
n: 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function optimisticUpdateFromJSON(update: any): OptimisticUpdate {
|
|
|
|
return capella.ssz.LightClientOptimisticUpdate.fromJson(update);
|
|
|
|
}
|
2023-07-11 07:07:18 +00:00
|
|
|
|
2023-07-10 20:44:58 +00:00
|
|
|
export async function optimisticUpdateVerify(
|
|
|
|
committee: Uint8Array[],
|
|
|
|
update: OptimisticUpdate,
|
|
|
|
): Promise<VerifyWithReason> {
|
|
|
|
try {
|
|
|
|
const { attestedHeader: header, syncAggregate } = update;
|
|
|
|
const headerBlockRoot = phase0.ssz.BeaconBlockHeader.hashTreeRoot(
|
|
|
|
header.beacon,
|
|
|
|
);
|
2023-07-11 06:42:54 +00:00
|
|
|
|
2023-07-11 07:16:39 +00:00
|
|
|
const chainConfig = getDefaultClientConfig().chainConfig;
|
|
|
|
|
2023-07-11 06:42:54 +00:00
|
|
|
const committeeFast = deserializeSyncCommittee({
|
|
|
|
pubkeys: committee,
|
|
|
|
aggregatePubkey: bls.PublicKey.aggregate(
|
|
|
|
deserializePubkeys(committee),
|
|
|
|
).toBytes(),
|
|
|
|
});
|
|
|
|
|
2023-07-10 20:44:58 +00:00
|
|
|
try {
|
|
|
|
assertValidSignedHeader(
|
2023-07-11 07:16:39 +00:00
|
|
|
chainConfig,
|
2023-07-10 20:44:58 +00:00
|
|
|
committeeFast,
|
|
|
|
syncAggregate,
|
|
|
|
headerBlockRoot,
|
|
|
|
header.beacon.slot,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
return { correct: false, reason: "invalid signatures" };
|
|
|
|
}
|
|
|
|
|
|
|
|
const participation =
|
|
|
|
syncAggregate.syncCommitteeBits.getTrueBitIndexes().length;
|
|
|
|
if (participation < BEACON_SYNC_SUPER_MAJORITY) {
|
|
|
|
return { correct: false, reason: "insufficient signatures" };
|
|
|
|
}
|
|
|
|
|
2023-07-11 07:43:12 +00:00
|
|
|
if (!isValidLightClientHeader(chainConfig, header)) {
|
2023-07-10 20:44:58 +00:00
|
|
|
return { correct: false, reason: "invalid header" };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { correct: true };
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
return { correct: false, reason: (e as Error).message };
|
|
|
|
}
|
|
|
|
}
|
2023-07-11 07:07:18 +00:00
|
|
|
|
2023-07-10 20:44:58 +00:00
|
|
|
export function deserializePubkeys(pubkeys) {
|
|
|
|
return pubkeys.map((pk) => bls.PublicKey.fromBytes(pk));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCommitteeHash(committee: Uint8Array[]): Uint8Array {
|
|
|
|
return digest(concatBytes(...committee));
|
|
|
|
}
|
|
|
|
|
|
|
|
export const consensusClient = axios.create();
|
|
|
|
axiosRetry(consensusClient, { retries: 3 });
|
2023-07-11 05:50:42 +00:00
|
|
|
|
|
|
|
export async function getConsensusOptimisticUpdate() {
|
|
|
|
const resp = await consensusClient.get(
|
|
|
|
`/eth/v1/beacon/light_client/optimistic_update`,
|
|
|
|
);
|
|
|
|
|
2023-07-11 05:54:01 +00:00
|
|
|
const update = resp.data;
|
2023-07-11 05:50:42 +00:00
|
|
|
|
2023-07-11 05:54:01 +00:00
|
|
|
if (!update) {
|
2023-07-11 05:50:42 +00:00
|
|
|
throw Error(`fetching optimistic update failed`);
|
|
|
|
}
|
2023-07-11 05:54:01 +00:00
|
|
|
|
2023-07-11 06:32:41 +00:00
|
|
|
return update.data;
|
2023-07-11 05:50:42 +00:00
|
|
|
}
|
2023-07-11 07:24:38 +00:00
|
|
|
function 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,
|
|
|
|
);
|
|
|
|
}
|