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

feat: add electra support for remote signer #7100

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
10 changes: 5 additions & 5 deletions packages/validator/src/services/validatorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,13 +562,13 @@ export class ValidatorStore {

const signingSlot = aggregate.data.slot;
const domain = this.config.getDomain(signingSlot, DOMAIN_AGGREGATE_AND_PROOF);
const signingRoot =
this.config.getForkSeq(duty.slot) >= ForkSeq.electra
? computeSigningRoot(ssz.electra.AggregateAndProof, aggregateAndProof, domain)
: computeSigningRoot(ssz.phase0.AggregateAndProof, aggregateAndProof, domain);
const isPostElectra = this.config.getForkSeq(duty.slot) >= ForkSeq.electra;
nflaig marked this conversation as resolved.
Show resolved Hide resolved
const signingRoot = isPostElectra
? computeSigningRoot(ssz.electra.AggregateAndProof, aggregateAndProof, domain)
: computeSigningRoot(ssz.phase0.AggregateAndProof, aggregateAndProof, domain);

const signableMessage: SignableMessage = {
type: SignableMessageType.AGGREGATE_AND_PROOF,
type: isPostElectra ? SignableMessageType.AGGREGATE_AND_PROOF_V2 : SignableMessageType.AGGREGATE_AND_PROOF,
data: aggregateAndProof,
};

Expand Down
33 changes: 29 additions & 4 deletions packages/validator/src/util/externalSignerClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import {ContainerType, ValueOf} from "@chainsafe/ssz";
import {fetch} from "@lodestar/api";
import {phase0, altair, capella, BeaconBlock, BlindedBeaconBlock} from "@lodestar/types";
import {ForkSeq} from "@lodestar/params";
import {
phase0,
altair,
capella,
BeaconBlock,
BlindedBeaconBlock,
AggregateAndProof,
sszTypesFor,
ssz,
Slot,
Epoch,
RootHex,
Root,
} from "@lodestar/types";
import {ForkPreExecution, ForkSeq} from "@lodestar/params";
import {ValidatorRegistrationV1} from "@lodestar/types/bellatrix";
import {BeaconConfig} from "@lodestar/config";
import {computeEpochAtSlot, blindedOrFullBlockToHeader} from "@lodestar/state-transition";
import {Epoch, Root, RootHex, Slot, ssz} from "@lodestar/types";
import {toHex, toRootHex} from "@lodestar/utils";
import {PubkeyHex} from "../types.js";

Expand All @@ -14,6 +26,7 @@ import {PubkeyHex} from "../types.js";
export enum SignableMessageType {
AGGREGATION_SLOT = "AGGREGATION_SLOT",
AGGREGATE_AND_PROOF = "AGGREGATE_AND_PROOF",
AGGREGATE_AND_PROOF_V2 = "AGGREGATE_AND_PROOF_V2",
ATTESTATION = "ATTESTATION",
BLOCK_V2 = "BLOCK_V2",
DEPOSIT = "DEPOSIT",
Expand Down Expand Up @@ -63,8 +76,9 @@ const SyncAggregatorSelectionDataType = new ContainerType(
export type SignableMessage =
| {type: SignableMessageType.AGGREGATION_SLOT; data: {slot: Slot}}
| {type: SignableMessageType.AGGREGATE_AND_PROOF; data: phase0.AggregateAndProof}
| {type: SignableMessageType.AGGREGATE_AND_PROOF_V2; data: AggregateAndProof}
| {type: SignableMessageType.ATTESTATION; data: phase0.AttestationData}
| {type: SignableMessageType.BLOCK_V2; data: BeaconBlock | BlindedBeaconBlock}
| {type: SignableMessageType.BLOCK_V2; data: BeaconBlock<ForkPreExecution> | BlindedBeaconBlock}
| {type: SignableMessageType.DEPOSIT; data: ValueOf<typeof DepositType>}
| {type: SignableMessageType.RANDAO_REVEAL; data: {epoch: Epoch}}
| {type: SignableMessageType.VOLUNTARY_EXIT; data: phase0.VoluntaryExit}
Expand All @@ -77,6 +91,7 @@ export type SignableMessage =
const requiresForkInfo: Record<SignableMessageType, boolean> = {
[SignableMessageType.AGGREGATION_SLOT]: true,
[SignableMessageType.AGGREGATE_AND_PROOF]: true,
[SignableMessageType.AGGREGATE_AND_PROOF_V2]: true,
[SignableMessageType.ATTESTATION]: true,
[SignableMessageType.BLOCK_V2]: true,
[SignableMessageType.DEPOSIT]: false,
Expand Down Expand Up @@ -203,6 +218,16 @@ function serializerSignableMessagePayload(config: BeaconConfig, payload: Signabl
case SignableMessageType.AGGREGATE_AND_PROOF:
return {aggregate_and_proof: ssz.phase0.AggregateAndProof.toJson(payload.data)};

case SignableMessageType.AGGREGATE_AND_PROOF_V2: {
const fork = config.getForkName(payload.data.aggregate.data.slot);
return {
aggregate_and_proof: {
version: fork.toUpperCase(),
data: sszTypesFor(fork).AggregateAndProof.toJson(payload.data),
},
};
}

case SignableMessageType.ATTESTATION:
return {attestation: ssz.phase0.AttestationData.toJson(payload.data)};

Expand Down
34 changes: 22 additions & 12 deletions packages/validator/test/e2e/web3signer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {computeStartSlotAtEpoch, interopSecretKey, interopSecretKeys} from "@lod
import {createBeaconConfig} from "@lodestar/config";
import {genesisData} from "@lodestar/config/networks";
import {getClient, routes} from "@lodestar/api";
import {ssz} from "@lodestar/types";
import {ssz, sszTypesFor} from "@lodestar/types";
import {ForkSeq} from "@lodestar/params";
import {getKeystoresStr, StartedExternalSigner, startExternalSigner} from "@lodestar/test-utils";
import {Interchange, ISlashingProtection, Signer, SignerType, ValidatorStore} from "../../src/index.js";
Expand Down Expand Up @@ -93,17 +93,27 @@ describe("web3signer signature test", function () {
await assertSameSignature("signAttestation", duty, attestationData, epoch);
});

it("signAggregateAndProof", async () => {
const aggregateAndProof = ssz.phase0.AggregateAndProof.defaultValue();
aggregateAndProof.aggregate.data.slot = duty.slot;
aggregateAndProof.aggregate.data.index = duty.committeeIndex;
await assertSameSignature(
"signAggregateAndProof",
duty,
aggregateAndProof.selectionProof,
aggregateAndProof.aggregate
);
});
for (const fork of config.forksAscendingEpochOrder) {
it(`signAggregateAndProof ${fork.name}`, async ({skip}) => {
// Only test till the fork the signer version supports
if (ForkSeq[fork.name] > externalSigner.supportedForkSeq) {
skip();
return;
}

const aggregateAndProof = sszTypesFor(fork.name).AggregateAndProof.defaultValue();
const slot = computeStartSlotAtEpoch(fork.epoch);
aggregateAndProof.aggregate.data.slot = slot;
aggregateAndProof.aggregate.data.index = duty.committeeIndex;

await assertSameSignature(
"signAggregateAndProof",
{...duty, slot},
aggregateAndProof.selectionProof,
aggregateAndProof.aggregate
);
});
}

it("signSyncCommitteeSignature", async () => {
const beaconBlockRoot = ssz.phase0.BeaconBlockHeader.defaultValue().bodyRoot;
Expand Down
Loading