Skip to content

Commit

Permalink
Revert upgradeStateToCapella
Browse files Browse the repository at this point in the history
  • Loading branch information
dgcoffman committed Nov 7, 2022
1 parent 87ecaaf commit 89c43bf
Showing 1 changed file with 51 additions and 38 deletions.
89 changes: 51 additions & 38 deletions packages/state-transition/src/slot/upgradeStateToCapella.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
import {ssz} from "@lodestar/types";

import {CachedBeaconStateCapella} from "../types.js";
import {CachedBeaconStateBellatrix, CachedBeaconStateCapella} from "../types.js";
import {getCachedBeaconState} from "../cache/stateCache.js";
import {CachedBeaconStateBellatrix} from "../types.js";

/**
* Upgrade a state from Bellatrix to Capella.
*/
export function upgradeStateToCapella(stateBellatrix: CachedBeaconStateBellatrix): CachedBeaconStateCapella {
const {config} = stateBellatrix;

// Get underlying node and cast bellatrix tree to capella tree
//
// An bellatrix BeaconState tree can be safely casted to a capella BeaconState tree because:
// - Deprecated fields are replaced by new fields at the exact same indexes
// - All new fields are appended at the end
//
// bellatrix | op | capella
// -------------------------------- | ---- | ------------
// genesis_time | - | genesis_time
// genesis_validators_root | - | genesis_validators_root
// slot | - | slot
// fork | - | fork
// latest_block_header | - | latest_block_header
// block_roots | - | block_roots
// state_roots | - | state_roots
// historical_roots | - | historical_roots
// eth1_data | - | eth1_data
// eth1_data_votes | - | eth1_data_votes
// eth1_deposit_index | - | eth1_deposit_index
// validators | diff | validators
// balances | - | balances
// randao_mixes | - | randao_mixes
// slashings | - | slashings
// previous_epoch_participation | - | previous_epoch_participation
// current_epoch_participation | - | current_epoch_participation
// justification_bits | - | justification_bits
// previous_justified_checkpoint | - | previous_justified_checkpoint
// current_justified_checkpoint | - | current_justified_checkpoint
// finalized_checkpoint | - | finalized_checkpoint
// inactivity_scores | - | inactivity_scores
// current_sync_committee | - | current_sync_committee
// next_sync_committee | - | next_sync_committee
// latest_execution_payload_header | diff | latest_execution_payload_header
// - | new | withdrawal_queue
// - | new | next_withdrawal_index
// - | new | next_partial_withdrawal_validator_index

const stateBellatrixNode = ssz.bellatrix.BeaconState.commitViewDU(stateBellatrix);
const stateCapellaView = ssz.capella.BeaconState.getViewDU(stateBellatrixNode);

// Attach existing BeaconStateCache from stateBellatrix to new stateCapellaView object
const stateCapella = getCachedBeaconState(stateCapellaView, stateBellatrix);

stateCapella.fork = ssz.phase0.Fork.toViewDU({
Expand All @@ -21,42 +56,20 @@ export function upgradeStateToCapella(stateBellatrix: CachedBeaconStateBellatrix
epoch: stateBellatrix.epochCtx.epoch,
});

const {
parentHash,
feeRecipient,
stateRoot,
receiptsRoot,
logsBloom,
prevRandao,
blockNumber,
gasLimit,
gasUsed,
timestamp,
extraData,
baseFeePerGas,
blockHash,
transactionsRoot,
} = stateBellatrix.latestExecutionPayloadHeader;

stateCapella.latestExecutionPayloadHeader = ssz.capella.ExecutionPayloadHeader.toViewDU({
parentHash,
feeRecipient,
stateRoot,
receiptsRoot,
logsBloom,
prevRandao,
blockNumber,
gasLimit,
gasUsed,
timestamp,
extraData,
baseFeePerGas,
blockHash,
transactionsRoot,
withdrawalsRoot: ssz.Root.defaultValue(),
});
// Upgrade the validators, this validator change is not present in latest specs but in 1.2.0
// so just set it to 0 and cleanup later
for (let i = 0; i < stateCapella.validators.length; i++) {
const validator = stateCapella.validators.get(i);
validator.fullyWithdrawnEpoch = Infinity;
}
// Nothing to do for latestExecutionPayloadHeader as the root is set to 0 by default
stateCapella.withdrawalQueue = ssz.capella.WithdrawalQueue.defaultViewDU();
// nextWithdrawalIndex and nextPartialWithdrawalValidatorIndex are also set to 0 by default

// Commit new added fields ViewDU to the root node
stateCapella.commit();
// Clear cache to ensure the cache of bellatrix fields is not used by new capella fields
stateCapella["clearCache"]();

return stateCapella;
}

0 comments on commit 89c43bf

Please sign in to comment.