-
-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
94 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 10 additions & 12 deletions
22
packages/state-transition/src/epoch/processNetExcessPenalties.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import { | ||
EFFECTIVE_BALANCE_INCREMENT, | ||
ForkSeq, | ||
HYSTERESIS_DOWNWARD_MULTIPLIER, | ||
HYSTERESIS_QUOTIENT, | ||
HYSTERESIS_UPWARD_MULTIPLIER, | ||
MAX_EFFECTIVE_BALANCE, | ||
TIMELY_TARGET_FLAG_INDEX, | ||
} from "@lodestar/params"; | ||
import {EpochTransitionCache, CachedBeaconStateAllForks, BeaconStateAltair} from "../types.js"; | ||
import {PARTICIPATION_FLAG_WEIGHTS, SLOTS_PER_EPOCH} from "@lodestar/params"; | ||
import {CachedBeaconStateEIP7716} from "../types.js"; | ||
import {getPreviousEpoch} from "../util/epoch.js"; | ||
import {computePenaltyFactor} from "../util/eip7716.js"; | ||
|
||
export function processNetExcessPenalties(state: CachedBeaconStateAllForks): void { | ||
// TODO | ||
export function processNetExcessPenalties(state: CachedBeaconStateEIP7716): void { | ||
const lastSlotPrevEpoch = getPreviousEpoch(state) + SLOTS_PER_EPOCH - 1; | ||
for (let flagIndex = 0; flagIndex < PARTICIPATION_FLAG_WEIGHTS.length; flagIndex++) { | ||
const {netExcessPenalty} = computePenaltyFactor(state, lastSlotPrevEpoch, flagIndex); | ||
state.netExcessPenalties.set(flagIndex, netExcessPenalty); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,79 @@ | ||
import {digest} from "@chainsafe/as-sha256"; | ||
import {BLSSignature} from "@lodestar/types"; | ||
import {intDiv, bytesToBigInt} from "@lodestar/utils"; | ||
import {Epoch, Slot, ValidatorIndex} from "@lodestar/types"; | ||
import { | ||
TARGET_AGGREGATORS_PER_COMMITTEE, | ||
SYNC_COMMITTEE_SIZE, | ||
SYNC_COMMITTEE_SUBNET_COUNT, | ||
TARGET_AGGREGATORS_PER_SYNC_SUBCOMMITTEE, | ||
SLOTS_PER_EPOCH, | ||
} from "@lodestar/params"; | ||
import {CachedBeaconStateEIP7716} from "../types.js"; | ||
import {computeEpochAtSlot, computeStartSlotAtEpoch} from "./epoch.js"; | ||
|
||
export function computePenaltyFactor() { | ||
export function computePenaltyFactor( | ||
state: CachedBeaconStateEIP7716, | ||
atSlot: Slot, | ||
flagIndex: number | ||
): {penaltyFactor: number; netExcessPenalty: number} { | ||
let netExcessPenalty = state.netExcessPenalties.get(flagIndex); | ||
const epoch = computeEpochAtSlot(atSlot); | ||
const {PENALTY_ADJUSTMENT_FACTOR, MAX_PENALTY_FACTOR, PENALTY_RECOVERY_RATE} = state.config; | ||
let penaltyFactor = 1; | ||
|
||
for (let slot = computeStartSlotAtEpoch(epoch); slot < atSlot; slot++) { | ||
const totalBalance = getSlotCommitteeBalance(state, slot); | ||
const participatingBalance = participatingBalanceSlot(state, slot, flagIndex); | ||
penaltyFactor = Math.min( | ||
Math.floor( | ||
((totalBalance - participatingBalance) * PENALTY_ADJUSTMENT_FACTOR) / (netExcessPenalty * totalBalance + 1) | ||
), | ||
MAX_PENALTY_FACTOR | ||
); | ||
netExcessPenalty = Math.max(PENALTY_RECOVERY_RATE, netExcessPenalty + penaltyFactor) - PENALTY_RECOVERY_RATE; | ||
} | ||
|
||
return {penaltyFactor, netExcessPenalty}; | ||
} | ||
|
||
export function committeeSlotOfValidator(state: CachedBeaconStateEIP7716, index: ValidatorIndex, epoch: Epoch) { | ||
for (let slot = epoch * SLOTS_PER_EPOCH; slot < (epoch + 1) * SLOTS_PER_EPOCH; slot++) { | ||
if (index in getSlotCommittees(state, slot)) { | ||
return slot; | ||
} | ||
} | ||
throw new Error(`Validator with index ${index} is not active`); | ||
} | ||
|
||
export function committeeSlotOfValidator() { | ||
export function participatingBalanceSlot(state: CachedBeaconStateEIP7716, slot: Slot, flagIndex: number) { | ||
const inCurrentEpoch = computeEpochAtSlot(slot) === state.epochCtx.epoch; | ||
const epochParticipation = inCurrentEpoch ? state.currentEpochParticipation : state.previousEpochParticipation; | ||
|
||
const flagBit = 1 << flagIndex; | ||
const participatingIndices = getSlotCommittees(state, slot).filter( | ||
(index) => (epochParticipation.get(index) & flagBit) === flagBit | ||
); | ||
|
||
return participatingIndices | ||
.map((participatingIndex) => state.balances.get(participatingIndex)) | ||
.reduce((total, balance) => total + balance, 0); | ||
} | ||
|
||
export function participatingBalanceSlot() { | ||
export function getSlotCommittees(state: CachedBeaconStateEIP7716, slot: Slot): Uint32Array { | ||
const committees = state.epochCtx.getShufflingAtSlot(slot).committees[slot % SLOTS_PER_EPOCH].flat(); | ||
// Create a new Uint32Array to flatten `committees` | ||
const totalLength = committees.reduce((acc, curr) => acc + curr.length, 0); | ||
const result = new Uint32Array(totalLength); | ||
|
||
let offset = 0; | ||
for (const committee of committees) { | ||
result.set(committee, offset); | ||
offset += committee.length; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function getSlotCommittees() { | ||
export function getSlotCommitteeBalance(state: CachedBeaconStateEIP7716, slot: Slot): number { | ||
const validatorIndices = getSlotCommittees(state, slot); | ||
|
||
} | ||
// 32eth * 1mil / 32 is still within number range | ||
// WIth maxEB = 2048, total max balance could be 2^46 | ||
return validatorIndices | ||
.map((validatorIndex) => state.balances.get(validatorIndex)) | ||
.reduce((total, balance) => total + balance, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
import {ContainerType, VectorBasicType} from "@chainsafe/ssz"; | ||
import { | ||
TIMELY_HEAD_FLAG_INDEX, | ||
} from "@lodestar/params"; | ||
import {TIMELY_HEAD_FLAG_INDEX} from "@lodestar/params"; | ||
import {ssz as primitiveSsz} from "../primitive/index.js"; | ||
import {ssz as denebSsz} from "../capella/index.js"; | ||
|
||
const {UintNum64} = | ||
primitiveSsz; | ||
const {UintNum64} = primitiveSsz; | ||
|
||
export const BeaconState = new ContainerType( | ||
{ | ||
...denebSsz.BeaconState.fields, | ||
netExcessPenalties: new VectorBasicType(UintNum64, TIMELY_HEAD_FLAG_INDEX) | ||
netExcessPenalties: new VectorBasicType(UintNum64, TIMELY_HEAD_FLAG_INDEX), | ||
}, | ||
{typeName: "BeaconState", jsonCase: "eth2"} | ||
); | ||
|
||
export const SignedBeaconBlock = denebSsz.SignedBeaconBlock; | ||
export const BeaconBlock = denebSsz.BeaconBlock; | ||
export const BeaconBlockBody = denebSsz.BeaconBlockBody; | ||
export const ExecutionPayload = denebSsz.ExecutionPayload; | ||
export const ExecutionPayload = denebSsz.ExecutionPayload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters