Skip to content

Commit

Permalink
Consider state slot in getDomain
Browse files Browse the repository at this point in the history
  • Loading branch information
wemeetagain committed Aug 17, 2022
1 parent 7703607 commit 8345d05
Show file tree
Hide file tree
Showing 23 changed files with 47 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getAggregateAndProofSignatureSet(
aggregateAndProof: phase0.SignedAggregateAndProof
): ISignatureSet {
const slot = computeStartSlotAtEpoch(epoch);
const aggregatorDomain = state.config.getDomain(DOMAIN_AGGREGATE_AND_PROOF, slot);
const aggregatorDomain = state.config.getDomain(state.slot, DOMAIN_AGGREGATE_AND_PROOF, slot);
const signingRoot = computeSigningRoot(ssz.phase0.AggregateAndProof, aggregateAndProof.message, aggregatorDomain);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function getContributionAndProofSignatureSet(
): ISignatureSet {
const {epochCtx} = state;
const domain = state.config.getDomain(
state.slot,
DOMAIN_CONTRIBUTION_AND_PROOF,
signedContributionAndProof.message.contribution.slot
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function getSelectionProofSignatureSet(
aggregator: PublicKey,
aggregateAndProof: phase0.SignedAggregateAndProof
): ISignatureSet {
const selectionProofDomain = state.config.getDomain(DOMAIN_SELECTION_PROOF, slot);
const selectionProofDomain = state.config.getDomain(state.slot, DOMAIN_SELECTION_PROOF, slot);

return {
type: SignatureSetType.single,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function getSyncCommitteeSignatureSet(
state: CachedBeaconStateAllForks,
syncCommittee: altair.SyncCommitteeMessage
): ISignatureSet {
const domain = state.config.getDomain(DOMAIN_SYNC_COMMITTEE, syncCommittee.slot);
const domain = state.config.getDomain(state.slot, DOMAIN_SYNC_COMMITTEE, syncCommittee.slot);

return {
type: SignatureSetType.single,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function getSyncCommitteeContributionSignatureSet(
contribution: altair.SyncCommitteeContribution,
pubkeys: PublicKey[]
): ISignatureSet {
const domain = state.config.getDomain(DOMAIN_SYNC_COMMITTEE, contribution.slot);
const domain = state.config.getDomain(state.slot, DOMAIN_SYNC_COMMITTEE, contribution.slot);
return {
type: SignatureSetType.aggregate,
pubkeys,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function getSyncCommitteeSelectionProofSignatureSet(
): ISignatureSet {
const {epochCtx, config} = state;
const slot = contributionAndProof.contribution.slot;
const domain = config.getDomain(DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF, slot);
const domain = config.getDomain(state.slot, DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF, slot);
const signingData: altair.SyncAggregatorSelectionData = {
slot,
subcommitteeIndex: contributionAndProof.contribution.subcommitteeIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function getAggregateAndProofValidData(
(chain as {seenAggregators: IBeaconChain["seenAggregators"]}).seenAggregators = new SeenAggregators();

const aggregatorIndex = validatorIndex;
const proofDomain = state.config.getDomain(DOMAIN_SELECTION_PROOF, attSlot);
const proofDomain = state.config.getDomain(state.slot, DOMAIN_SELECTION_PROOF, attSlot);
const proofSigningRoot = computeSigningRoot(ssz.Slot, attSlot, proofDomain);

const aggregateAndProof: phase0.AggregateAndProof = {
Expand All @@ -39,7 +39,7 @@ export function getAggregateAndProofValidData(
selectionProof: signCached(sk, proofSigningRoot),
};

const aggDomain = state.config.getDomain(DOMAIN_AGGREGATE_AND_PROOF, attSlot);
const aggDomain = state.config.getDomain(state.slot, DOMAIN_AGGREGATE_AND_PROOF, attSlot);
const aggSigningRoot = computeSigningRoot(ssz.phase0.AggregateAndProof, aggregateAndProof, aggDomain);

const signedAggregateAndProof: phase0.SignedAggregateAndProof = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function getAttestationValidData(
};

const slot = computeStartSlotAtEpoch(attestationData.target.epoch);
const domain = state.config.getDomain(DOMAIN_BEACON_ATTESTER, slot);
const domain = state.config.getDomain(state.slot, DOMAIN_BEACON_ATTESTER, slot);
const signingRoot = computeSigningRoot(ssz.phase0.AttestationData, attestationData, domain);
const sk = getSecretKeyFromIndexCached(validatorIndex);

Expand Down
2 changes: 0 additions & 2 deletions packages/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ import {config as chainConfig} from "@lodestar/config/default";
let genesisValidatorsRoot: Uint8Array = new Uint8Array();

const config: IBeaconConfig = createIBeaconConfig(chainConfig, genesisValidatorsRoot);

const domain = config.getDomain(DOMAIN_DEPOSIT, GENESIS_SLOT);
```

## License
Expand Down
11 changes: 8 additions & 3 deletions packages/config/src/genesisConfig/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ForkName} from "@lodestar/params";
import {ForkName, SLOTS_PER_EPOCH} from "@lodestar/params";
import {DomainType, ForkDigest, phase0, Root, Slot, ssz, Version} from "@lodestar/types";
import {toHexString} from "@chainsafe/ssz";
import {IChainForkConfig} from "../beaconConfig.js";
Expand All @@ -22,8 +22,13 @@ export function createICachedGenesis(chainForkConfig: IChainForkConfig, genesisV
}

return {
getDomain(domainType: DomainType, slot: Slot): Uint8Array {
const forkInfo = chainForkConfig.getForkInfo(slot);
getDomain(stateSlot: Slot, domainType: DomainType, messageSlot?: Slot): Uint8Array {
const stateForkInfo = chainForkConfig.getForkInfo(stateSlot);
const epoch = Math.floor(messageSlot ?? stateSlot / SLOTS_PER_EPOCH);
const forkInfo =
epoch < stateForkInfo.epoch
? chainForkConfig.forksAscendingEpochOrder[Math.max(stateForkInfo.seq - 1, 0)] // previous
: stateForkInfo; // current
let domainByType = domainCache.get(forkInfo.name);
if (!domainByType) {
domainByType = new Map<DomainType, Uint8Array>();
Expand Down
7 changes: 6 additions & 1 deletion packages/config/src/genesisConfig/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ export interface IForkDigestContext {
}

export interface ICachedGenesis extends IForkDigestContext {
getDomain(domainType: DomainType, slot: Slot): Uint8Array;
/**
* Return the signature domain (fork version concatenated with domain type) of a message.
*
* Note: The configured fork schedule is always used rather than on-chain fork schedule.
*/
getDomain(stateSlot: Slot, domainType: DomainType, messageSlot?: Slot): Uint8Array;
}
2 changes: 1 addition & 1 deletion packages/flare/src/cmds/selfSlashAttester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function signAttestationDataBigint(
data: phase0.AttestationDataBigint
): Uint8Array {
const slot = Number(data.slot as bigint);
const proposerDomain = config.getDomain(DOMAIN_BEACON_ATTESTER, slot);
const proposerDomain = config.getDomain(slot, DOMAIN_BEACON_ATTESTER);
const signingRoot = computeSigningRoot(ssz.phase0.AttestationDataBigint, data, proposerDomain);

const sigs = sks.map((sk) => sk.sign(signingRoot));
Expand Down
2 changes: 1 addition & 1 deletion packages/flare/src/cmds/selfSlashProposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export async function selfSlashProposerHandler(args: SelfSlashArgs): Promise<voi

function signHeaderBigint(config: IBeaconConfig, sk: SecretKey, header: phase0.BeaconBlockHeaderBigint): Uint8Array {
const slot = Number(header.slot as bigint);
const proposerDomain = config.getDomain(DOMAIN_BEACON_PROPOSER, slot);
const proposerDomain = config.getDomain(slot, DOMAIN_BEACON_PROPOSER);
const signingRoot = computeSigningRoot(ssz.phase0.BeaconBlockHeaderBigint, header, proposerDomain);
return sk.sign(signingRoot).toBytes();
}
2 changes: 1 addition & 1 deletion packages/light-client/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export function assertValidSignedHeader(

const signingRoot = ssz.phase0.SigningData.hashTreeRoot({
objectRoot: signedHeaderRoot,
domain: config.getDomain(DOMAIN_SYNC_COMMITTEE, signedHeaderSlot),
domain: config.getDomain(signedHeaderSlot, DOMAIN_SYNC_COMMITTEE),
});

if (!isValidBlsAggregate(participantPubkeys, signingRoot, syncAggregate.syncCommitteeSignature)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/light-client/test/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function getSyncAggregateSigningRoot(
config: IBeaconConfig,
syncAttestedBlockHeader: phase0.BeaconBlockHeader
): Uint8Array {
const domain = config.getDomain(DOMAIN_SYNC_COMMITTEE, syncAttestedBlockHeader.slot);
const domain = config.getDomain(syncAttestedBlockHeader.slot, DOMAIN_SYNC_COMMITTEE);
return computeSigningRoot(ssz.phase0.BeaconBlockHeader, syncAttestedBlockHeader, domain);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function getSyncCommitteeSignatureSet(
}
}

const domain = state.config.getDomain(DOMAIN_SYNC_COMMITTEE, previousSlot);
const domain = state.config.getDomain(state.slot, DOMAIN_SYNC_COMMITTEE, previousSlot);

return {
type: SignatureSetType.aggregate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function getIndexedAttestationBigintSignatureSet(
): ISignatureSet {
const {index2pubkey} = state.epochCtx;
const slot = computeStartSlotAtEpoch(Number(indexedAttestation.data.target.epoch as bigint));
const domain = state.config.getDomain(DOMAIN_BEACON_ATTESTER, slot);
const domain = state.config.getDomain(state.slot, DOMAIN_BEACON_ATTESTER, slot);

return {
type: SignatureSetType.aggregate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function getAttestationWithIndicesSignatureSet(
): ISignatureSet {
const {epochCtx} = state;
const slot = computeStartSlotAtEpoch(attestation.data.target.epoch);
const domain = state.config.getDomain(DOMAIN_BEACON_ATTESTER, slot);
const domain = state.config.getDomain(state.slot, DOMAIN_BEACON_ATTESTER, slot);

return {
type: SignatureSetType.aggregate,
Expand Down
2 changes: 1 addition & 1 deletion packages/state-transition/src/signatureSets/proposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getProposerSignatureSet(
signedBlock: allForks.FullOrBlindedSignedBeaconBlock
): ISignatureSet {
const {config, epochCtx} = state;
const domain = state.config.getDomain(DOMAIN_BEACON_PROPOSER, signedBlock.message.slot);
const domain = state.config.getDomain(state.slot, DOMAIN_BEACON_PROPOSER, signedBlock.message.slot);

const blockType = isBlindedBeaconBlock(signedBlock.message)
? ssz.bellatrix.BlindedBeaconBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export function getProposerSlashingSignatureSets(
// clock and the slashing would still be valid. Must use bigint variants to hash correctly to all possible values
return [proposerSlashing.signedHeader1, proposerSlashing.signedHeader2].map(
(signedHeader): ISignatureSet => {
const domain = state.config.getDomain(DOMAIN_BEACON_PROPOSER, Number(signedHeader.message.slot as bigint));
const domain = state.config.getDomain(
state.slot,
DOMAIN_BEACON_PROPOSER,
Number(signedHeader.message.slot as bigint)
);

return {
type: SignatureSetType.single,
Expand Down
2 changes: 1 addition & 1 deletion packages/state-transition/src/signatureSets/randao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function getRandaoRevealSignatureSet(
const {epochCtx} = state;
// should not get epoch from epochCtx
const epoch = computeEpochAtSlot(block.slot);
const domain = state.config.getDomain(DOMAIN_RANDAO, block.slot);
const domain = state.config.getDomain(state.slot, DOMAIN_RANDAO, block.slot);

return {
type: SignatureSetType.single,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function getVoluntaryExitSignatureSet(
): ISignatureSet {
const {epochCtx} = state;
const slot = computeStartSlotAtEpoch(signedVoluntaryExit.message.epoch);
const domain = state.config.getDomain(DOMAIN_VOLUNTARY_EXIT, slot);
const domain = state.config.getDomain(state.slot, DOMAIN_VOLUNTARY_EXIT, slot);

return {
type: SignatureSetType.single,
Expand Down
18 changes: 9 additions & 9 deletions packages/validator/src/services/validatorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class ValidatorStore {
// Duties are filtered before-hard by doppelganger-safe, this assert should never throw
this.assertDoppelgangerSafe(pubkey);

const proposerDomain = this.config.getDomain(DOMAIN_BEACON_PROPOSER, blindedOrFull.slot);
const proposerDomain = this.config.getDomain(currentSlot, DOMAIN_BEACON_PROPOSER, blindedOrFull.slot);
const blockType =
(blindedOrFull.body as bellatrix.BlindedBeaconBlockBody).executionPayloadHeader !== undefined
? ssz.bellatrix.BlindedBeaconBlock
Expand All @@ -200,7 +200,7 @@ export class ValidatorStore {

async signRandao(pubkey: BLSPubkey, slot: Slot): Promise<BLSSignature> {
const epoch = computeEpochAtSlot(slot);
const randaoDomain = this.config.getDomain(DOMAIN_RANDAO, slot);
const randaoDomain = this.config.getDomain(slot, DOMAIN_RANDAO);
const randaoSigningRoot = computeSigningRoot(ssz.Epoch, epoch, randaoDomain);

return await this.getSignature(pubkey, randaoSigningRoot);
Expand All @@ -223,7 +223,7 @@ export class ValidatorStore {

this.validateAttestationDuty(duty, attestationData);
const slot = computeStartSlotAtEpoch(attestationData.target.epoch);
const domain = this.config.getDomain(DOMAIN_BEACON_ATTESTER, slot);
const domain = this.config.getDomain(slot, DOMAIN_BEACON_ATTESTER);
const signingRoot = computeSigningRoot(ssz.phase0.AttestationData, attestationData, domain);

try {
Expand Down Expand Up @@ -257,7 +257,7 @@ export class ValidatorStore {
selectionProof,
};

const domain = this.config.getDomain(DOMAIN_AGGREGATE_AND_PROOF, aggregate.data.slot);
const domain = this.config.getDomain(duty.slot, DOMAIN_AGGREGATE_AND_PROOF);
const signingRoot = computeSigningRoot(ssz.phase0.AggregateAndProof, aggregateAndProof, domain);

return {
Expand All @@ -272,7 +272,7 @@ export class ValidatorStore {
slot: Slot,
beaconBlockRoot: Root
): Promise<altair.SyncCommitteeMessage> {
const domain = this.config.getDomain(DOMAIN_SYNC_COMMITTEE, slot);
const domain = this.config.getDomain(slot, DOMAIN_SYNC_COMMITTEE);
const signingRoot = computeSigningRoot(ssz.Root, beaconBlockRoot, domain);

return {
Expand All @@ -294,7 +294,7 @@ export class ValidatorStore {
selectionProof,
};

const domain = this.config.getDomain(DOMAIN_CONTRIBUTION_AND_PROOF, contribution.slot);
const domain = this.config.getDomain(contribution.slot, DOMAIN_CONTRIBUTION_AND_PROOF);
const signingRoot = computeSigningRoot(ssz.altair.ContributionAndProof, contributionAndProof, domain);

return {
Expand All @@ -304,7 +304,7 @@ export class ValidatorStore {
}

async signAttestationSelectionProof(pubkey: BLSPubkeyMaybeHex, slot: Slot): Promise<BLSSignature> {
const domain = this.config.getDomain(DOMAIN_SELECTION_PROOF, slot);
const domain = this.config.getDomain(slot, DOMAIN_SELECTION_PROOF);
const signingRoot = computeSigningRoot(ssz.Slot, slot, domain);

return await this.getSignature(pubkey, signingRoot);
Expand All @@ -315,7 +315,7 @@ export class ValidatorStore {
slot: Slot,
subcommitteeIndex: number
): Promise<BLSSignature> {
const domain = this.config.getDomain(DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF, slot);
const domain = this.config.getDomain(slot, DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF);
const signingData: altair.SyncAggregatorSelectionData = {
slot,
subcommitteeIndex,
Expand All @@ -331,7 +331,7 @@ export class ValidatorStore {
validatorIndex: number,
exitEpoch: Epoch
): Promise<phase0.SignedVoluntaryExit> {
const domain = this.config.getDomain(DOMAIN_VOLUNTARY_EXIT, computeStartSlotAtEpoch(exitEpoch));
const domain = this.config.getDomain(computeStartSlotAtEpoch(exitEpoch), DOMAIN_VOLUNTARY_EXIT);

const voluntaryExit: phase0.VoluntaryExit = {epoch: exitEpoch, validatorIndex};
const signingRoot = computeSigningRoot(ssz.phase0.VoluntaryExit, voluntaryExit, domain);
Expand Down

0 comments on commit 8345d05

Please sign in to comment.