@lodestar/beacon-node 1.39.0-dev.075956b855 → 1.39.0-dev.100ab480bb

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,60 +0,0 @@
1
- import {routes} from "@lodestar/api";
2
- import {BeaconConfig} from "@lodestar/config";
3
- import {ForkName, SYNC_COMMITTEE_SIZE} from "@lodestar/params";
4
- import {CachedBeaconStateAllForks, CachedBeaconStateAltair, Index2PubkeyCache} from "@lodestar/state-transition";
5
- import {BeaconBlock, ValidatorIndex, altair} from "@lodestar/types";
6
-
7
- export type SyncCommitteeRewards = routes.beacon.SyncCommitteeRewards;
8
- type BalanceRecord = {val: number}; // Use val for convenient way to increment/decrement balance
9
-
10
- export async function computeSyncCommitteeRewards(
11
- config: BeaconConfig,
12
- index2pubkey: Index2PubkeyCache,
13
- block: BeaconBlock,
14
- preState: CachedBeaconStateAllForks,
15
- validatorIds: (ValidatorIndex | string)[] = []
16
- ): Promise<SyncCommitteeRewards> {
17
- const fork = config.getForkName(block.slot);
18
- if (fork === ForkName.phase0) {
19
- throw Error("Cannot get sync rewards as phase0 block does not have sync committee");
20
- }
21
-
22
- const altairBlock = block as altair.BeaconBlock;
23
- const preStateAltair = preState as CachedBeaconStateAltair;
24
-
25
- // Bound syncCommitteeValidatorIndices in case it goes beyond SYNC_COMMITTEE_SIZE just to be safe
26
- const syncCommitteeValidatorIndices = preStateAltair.epochCtx.currentSyncCommitteeIndexed.validatorIndices.slice(
27
- 0,
28
- SYNC_COMMITTEE_SIZE
29
- );
30
- const {syncParticipantReward} = preStateAltair.epochCtx;
31
- const {syncCommitteeBits} = altairBlock.body.syncAggregate;
32
-
33
- // Use balance of each committee as starting point such that we cap the penalty to avoid balance dropping below 0
34
- const balances: Map<ValidatorIndex, BalanceRecord> = new Map();
35
- for (const i of syncCommitteeValidatorIndices) {
36
- balances.set(i, {val: preStateAltair.balances.get(i)});
37
- }
38
-
39
- for (const i of syncCommitteeValidatorIndices) {
40
- const balanceRecord = balances.get(i) as BalanceRecord;
41
- if (syncCommitteeBits.get(i)) {
42
- // Positive rewards for participants
43
- balanceRecord.val += syncParticipantReward;
44
- } else {
45
- // Negative rewards for non participants
46
- balanceRecord.val = Math.max(0, balanceRecord.val - syncParticipantReward);
47
- }
48
- }
49
-
50
- const rewards = Array.from(balances, ([validatorIndex, v]) => ({validatorIndex, reward: v.val}));
51
-
52
- if (validatorIds.length) {
53
- const filtersSet = new Set(validatorIds);
54
- return rewards.filter(
55
- (reward) => filtersSet.has(reward.validatorIndex) || filtersSet.has(index2pubkey[reward.validatorIndex].toHex())
56
- );
57
- }
58
-
59
- return rewards;
60
- }