Skip to content

Commit

Permalink
feat: add inclusion lists for censorship resistance
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Mar 16, 2024
1 parent 1abc896 commit 8b053ca
Show file tree
Hide file tree
Showing 17 changed files with 378 additions and 81 deletions.
3 changes: 3 additions & 0 deletions packages/beacon-node/src/api/impl/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF,
DOMAIN_CONTRIBUTION_AND_PROOF,
DOMAIN_BLS_TO_EXECUTION_CHANGE,
DOMAIN_INCLUSION_LIST_SUMMARY,
DOMAIN_APPLICATION_BUILDER,
TIMELY_SOURCE_FLAG_INDEX,
TIMELY_TARGET_FLAG_INDEX,
Expand Down Expand Up @@ -100,4 +101,6 @@ export const specConstants = {
// Deneb types
BLOB_TX_TYPE,
VERSIONED_HASH_VERSION_KZG,

DOMAIN_INCLUSION_LIST_SUMMARY,
};
3 changes: 2 additions & 1 deletion packages/beacon-node/test/spec/presets/fork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CachedBeaconStateAltair,
CachedBeaconStatePhase0,
CachedBeaconStateCapella,
CachedBeaconStateDeneb,
} from "@lodestar/state-transition";
import * as slotFns from "@lodestar/state-transition/slot";
import {phase0, ssz} from "@lodestar/types";
Expand Down Expand Up @@ -36,7 +37,7 @@ const fork: TestRunnerFn<ForkStateCase, BeaconStateAllForks> = (forkNext) => {
case ForkName.deneb:
return slotFns.upgradeStateToDeneb(preState as CachedBeaconStateCapella);
case ForkName.electra:
throw Error("not Implemented");
return slotFns.upgradeStateToElectra(preState as CachedBeaconStateDeneb);
}
},
options: {
Expand Down
6 changes: 6 additions & 0 deletions packages/params/src/forkName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ export type ForkBlobs = Exclude<ForkName, ForkPreBlobs>;
export function isForkBlobs(fork: ForkName): fork is ForkBlobs {
return isForkWithdrawals(fork) && fork !== ForkName.capella;
}

export type ForkPreILs = ForkPreBlobs | ForkName.deneb;
export type ForkILs = Exclude<ForkName, ForkPreILs>;
export function isForkILs(fork: ForkName): fork is ForkILs {
return isForkBlobs(fork) && fork !== ForkName.deneb;
}
5 changes: 4 additions & 1 deletion packages/params/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const DOMAIN_SYNC_COMMITTEE = Uint8Array.from([7, 0, 0, 0]);
export const DOMAIN_SYNC_COMMITTEE_SELECTION_PROOF = Uint8Array.from([8, 0, 0, 0]);
export const DOMAIN_CONTRIBUTION_AND_PROOF = Uint8Array.from([9, 0, 0, 0]);
export const DOMAIN_BLS_TO_EXECUTION_CHANGE = Uint8Array.from([10, 0, 0, 0]);
export const DOMAIN_BLOB_SIDECAR = Uint8Array.from([11, 0, 0, 0]);
export const DOMAIN_INCLUSION_LIST_SUMMARY = Uint8Array.from([11, 0, 0, 0]);

// Application specific domains

Expand Down Expand Up @@ -244,3 +244,6 @@ export const KZG_COMMITMENT_SUBTREE_INDEX0 = KZG_COMMITMENT_GINDEX0 - 2 ** KZG_C

// ssz.deneb.BlobSidecars.elementType.fixedSize
export const BLOBSIDECAR_FIXED_SIZE = ACTIVE_PRESET === PresetName.minimal ? 131672 : 131928;

// might be rounded to nearest power of 2
export const MAX_TRANSACTIONS_PER_INCLUSION_LIST = 143;
2 changes: 2 additions & 0 deletions packages/state-transition/src/cache/stateCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
BeaconStateBellatrix,
BeaconStateCapella,
BeaconStateDeneb,
BeaconStateElectra,
} from "./types.js";
import {RewardCache, createEmptyRewardCache} from "./rewardCache.js";

Expand Down Expand Up @@ -131,6 +132,7 @@ export type CachedBeaconStateAltair = CachedBeaconState<BeaconStateAltair>;
export type CachedBeaconStateBellatrix = CachedBeaconState<BeaconStateBellatrix>;
export type CachedBeaconStateCapella = CachedBeaconState<BeaconStateCapella>;
export type CachedBeaconStateDeneb = CachedBeaconState<BeaconStateDeneb>;
export type CachedBeaconStateElectra = CachedBeaconState<BeaconStateElectra>;

export type CachedBeaconStateAllForks = CachedBeaconState<BeaconStateAllForks>;
export type CachedBeaconStateExecutions = CachedBeaconState<BeaconStateExecutions>;
Expand Down
6 changes: 4 additions & 2 deletions packages/state-transition/src/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type BeaconStateAltair = CompositeViewDU<typeof ssz.altair.BeaconState>;
export type BeaconStateBellatrix = CompositeViewDU<typeof ssz.bellatrix.BeaconState>;
export type BeaconStateCapella = CompositeViewDU<typeof ssz.capella.BeaconState>;
export type BeaconStateDeneb = CompositeViewDU<typeof ssz.deneb.BeaconState>;
export type BeaconStateElectra = CompositeViewDU<typeof ssz.electra.BeaconState>;

// Union at the TreeViewDU level
// - Works well as function argument and as generic type for allForks functions
Expand All @@ -18,8 +19,9 @@ export type BeaconStateAllForks =
| BeaconStateAltair
| BeaconStateBellatrix
| BeaconStateCapella
| BeaconStateDeneb;
| BeaconStateDeneb
| BeaconStateElectra;

export type BeaconStateExecutions = BeaconStateBellatrix | BeaconStateCapella | BeaconStateDeneb;
export type BeaconStateExecutions = BeaconStateBellatrix | BeaconStateCapella | BeaconStateDeneb | BeaconStateElectra;

export type ShufflingGetter = (shufflingEpoch: Epoch, dependentRoot: RootHex) => EpochShuffling | null;
1 change: 1 addition & 0 deletions packages/state-transition/src/slot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {upgradeStateToAltair} from "./upgradeStateToAltair.js";
export {upgradeStateToBellatrix} from "./upgradeStateToBellatrix.js";
export {upgradeStateToCapella} from "./upgradeStateToCapella.js";
export {upgradeStateToDeneb} from "./upgradeStateToDeneb.js";
export {upgradeStateToElectra} from "./upgradeStateToElectra.js";

/**
* Dial state to next slot. Common for all forks
Expand Down
36 changes: 36 additions & 0 deletions packages/state-transition/src/slot/upgradeStateToElectra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {ssz} from "@lodestar/types";
import {getCachedBeaconState} from "../cache/stateCache.js";
import {CachedBeaconStateDeneb} from "../types.js";
import {CachedBeaconStateElectra} from "../types.js";

/**
* Upgrade a state from Capella to Deneb.
*/
export function upgradeStateToElectra(stateDeneb: CachedBeaconStateDeneb): CachedBeaconStateElectra {
const {config} = stateDeneb;

const stateDenebNode = ssz.deneb.BeaconState.commitViewDU(stateDeneb);
const stateElectraView = ssz.electra.BeaconState.getViewDU(stateDenebNode);

const stateElectra = getCachedBeaconState(stateElectraView, stateDeneb);

stateElectra.fork = ssz.phase0.Fork.toViewDU({
previousVersion: stateDeneb.fork.currentVersion,
currentVersion: config.ELECTRA_FORK_VERSION,
epoch: stateDeneb.epochCtx.epoch,
});

// TODO ELECTRA: check if this is following is required, since it seemed to be an issue in deneb state u[grade
// (see upgradeStateToDeneb)
//
// stateElectra.latestExecutionPayloadHeader = ssz.electra.BeaconState.fields.latestExecutionPayloadHeader.toViewDU({
// ...stateDeneb.latestExecutionPayloadHeader.toValue(),
// previousInclusionListSummaryRoot: ssz.Root.defaultValue(),
// });

stateElectra.commit();
// Clear cache to ensure the cache of capella fields is not used by new deneb fields
stateElectra["clearCache"]();

return stateElectra;
}
5 changes: 5 additions & 0 deletions packages/state-transition/src/stateTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CachedBeaconStateAltair,
CachedBeaconStateBellatrix,
CachedBeaconStateCapella,
CachedBeaconStateDeneb,
} from "./types.js";
import {computeEpochAtSlot} from "./util/index.js";
import {verifyProposerSignature} from "./signatureSets/index.js";
Expand All @@ -18,6 +19,7 @@ import {
upgradeStateToBellatrix,
upgradeStateToCapella,
upgradeStateToDeneb,
upgradeStateToElectra,
} from "./slot/index.js";
import {processBlock} from "./block/index.js";
import {EpochTransitionStep, processEpoch} from "./epoch/index.js";
Expand Down Expand Up @@ -230,6 +232,9 @@ function processSlotsWithTransientCache(
if (stateSlot === config.DENEB_FORK_EPOCH) {
postState = upgradeStateToDeneb(postState as CachedBeaconStateCapella) as CachedBeaconStateAllForks;
}
if (stateSlot === config.ELECTRA_FORK_EPOCH) {
postState = upgradeStateToElectra(postState as CachedBeaconStateDeneb) as CachedBeaconStateAllForks;
}
} else {
postState.slot++;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/state-transition/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type {
CachedBeaconStateBellatrix,
CachedBeaconStateCapella,
CachedBeaconStateDeneb,
CachedBeaconStateElectra,
} from "./cache/stateCache.js";

export type {
Expand All @@ -19,4 +20,5 @@ export type {
BeaconStateBellatrix,
BeaconStateCapella,
BeaconStateDeneb,
BeaconStateElectra,
} from "./cache/types.js";
10 changes: 10 additions & 0 deletions packages/state-transition/src/util/genesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export function initializeBeaconStateFromEth1(
| typeof ssz.bellatrix.ExecutionPayloadHeader
| typeof ssz.capella.ExecutionPayloadHeader
| typeof ssz.deneb.ExecutionPayloadHeader
| typeof ssz.electra.ExecutionPayloadHeader
>
): CachedBeaconStateAllForks {
const stateView = getGenesisBeaconState(
Expand Down Expand Up @@ -284,6 +285,15 @@ export function initializeBeaconStateFromEth1(
ssz.deneb.ExecutionPayloadHeader.defaultViewDU();
}

if (GENESIS_SLOT >= config.ELECTRA_FORK_EPOCH) {
const stateElectra = state as CompositeViewDU<typeof ssz.electra.BeaconState>;
stateElectra.fork.previousVersion = config.ELECTRA_FORK_VERSION;
stateElectra.fork.currentVersion = config.ELECTRA_FORK_VERSION;
stateElectra.latestExecutionPayloadHeader =
(executionPayloadHeader as CompositeViewDU<typeof ssz.electra.ExecutionPayloadHeader>) ??
ssz.electra.ExecutionPayloadHeader.defaultViewDU();
}

state.commit();

return state;
Expand Down
16 changes: 16 additions & 0 deletions packages/state-transition/test/unit/upgradeState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {createBeaconConfig, ChainForkConfig, createChainForkConfig} from "@lodes
import {config as chainConfig} from "@lodestar/config/default";

import {upgradeStateToDeneb} from "../../src/slot/upgradeStateToDeneb.js";
import {upgradeStateToElectra} from "../../src/slot/upgradeStateToElectra.js";
import {createCachedBeaconState} from "../../src/cache/stateCache.js";
import {PubkeyIndexMap} from "../../src/cache/pubkeyCache.js";

Expand All @@ -24,6 +25,21 @@ describe("upgradeState", () => {
const newState = upgradeStateToDeneb(stateView);
expect(() => newState.toValue()).not.toThrow();
});
it("upgradeStateToElectra", () => {
const denebState = ssz.deneb.BeaconState.defaultViewDU();
const config = getConfig(ForkName.deneb);
const stateView = createCachedBeaconState(
denebState,
{
config: createBeaconConfig(config, denebState.genesisValidatorsRoot),
pubkey2index: new PubkeyIndexMap(),
index2pubkey: [],
},
{skipSyncCommitteeCache: true}
);
const newState = upgradeStateToElectra(stateView);
expect(() => newState.toValue()).not.toThrow();
});
});

const ZERO_HASH = Buffer.alloc(32, 0);
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/allForks/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export const allForksBlobs = {
ExecutionPayloadAndBlobsBundle: deneb.ExecutionPayloadAndBlobsBundle,
},
electra: {
BlobSidecar: electra.BlobSidecar,
BlobSidecar: deneb.BlobSidecar,
ExecutionPayloadAndBlobsBundle: electra.ExecutionPayloadAndBlobsBundle,
},
};
Loading

0 comments on commit 8b053ca

Please sign in to comment.