@lodestar/state-transition 1.39.0-dev.b6bba4cb8c → 1.39.0-dev.b6d377a93c
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.
- package/lib/block/processWithdrawals.d.ts +3 -3
- package/lib/block/processWithdrawals.d.ts.map +1 -1
- package/lib/block/processWithdrawals.js +152 -105
- package/lib/block/processWithdrawals.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/rewards/attestationsRewards.d.ts +6 -0
- package/lib/rewards/attestationsRewards.d.ts.map +1 -0
- package/lib/rewards/attestationsRewards.js +113 -0
- package/lib/rewards/attestationsRewards.js.map +1 -0
- package/lib/rewards/blockRewards.d.ts +13 -0
- package/lib/rewards/blockRewards.d.ts.map +1 -0
- package/lib/rewards/blockRewards.js +95 -0
- package/lib/rewards/blockRewards.js.map +1 -0
- package/lib/rewards/index.d.ts +4 -0
- package/lib/rewards/index.d.ts.map +1 -0
- package/lib/rewards/index.js +4 -0
- package/lib/rewards/index.js.map +1 -0
- package/lib/rewards/syncCommitteeRewards.d.ts +6 -0
- package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -0
- package/lib/rewards/syncCommitteeRewards.js +36 -0
- package/lib/rewards/syncCommitteeRewards.js.map +1 -0
- package/lib/signatureSets/proposer.d.ts +3 -4
- package/lib/signatureSets/proposer.d.ts.map +1 -1
- package/lib/signatureSets/proposer.js +5 -6
- package/lib/signatureSets/proposer.js.map +1 -1
- package/package.json +14 -11
- package/src/block/processWithdrawals.ts +230 -135
- package/src/index.ts +1 -0
- package/src/rewards/attestationsRewards.ts +200 -0
- package/src/rewards/blockRewards.ts +147 -0
- package/src/rewards/index.ts +3 -0
- package/src/rewards/syncCommitteeRewards.ts +59 -0
- package/src/signatureSets/proposer.ts +6 -7
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import {BeaconConfig} from "@lodestar/config";
|
|
2
|
+
import {
|
|
3
|
+
ForkName,
|
|
4
|
+
WHISTLEBLOWER_REWARD_QUOTIENT,
|
|
5
|
+
WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA,
|
|
6
|
+
isForkPostElectra,
|
|
7
|
+
} from "@lodestar/params";
|
|
8
|
+
import {BeaconBlock, altair, phase0, rewards} from "@lodestar/types";
|
|
9
|
+
import {processAttestationsAltair} from "../block/processAttestationsAltair.js";
|
|
10
|
+
import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStatePhase0} from "../cache/stateCache.js";
|
|
11
|
+
import {getAttesterSlashableIndices} from "../util/attestation.js";
|
|
12
|
+
|
|
13
|
+
type SubRewardValue = number; // All reward values should be integer
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Calculate total proposer block rewards given block and the beacon state of the same slot before the block is applied (preState)
|
|
17
|
+
* postState can be passed in to read reward cache if available
|
|
18
|
+
* Standard (Non MEV) rewards for proposing a block consists of:
|
|
19
|
+
* 1) Including attestations from (beacon) committee
|
|
20
|
+
* 2) Including attestations from sync committee
|
|
21
|
+
* 3) Reporting slashable behaviours from proposer and attester
|
|
22
|
+
*/
|
|
23
|
+
export async function computeBlockRewards(
|
|
24
|
+
config: BeaconConfig,
|
|
25
|
+
block: BeaconBlock,
|
|
26
|
+
preState: CachedBeaconStateAllForks,
|
|
27
|
+
postState?: CachedBeaconStateAllForks
|
|
28
|
+
): Promise<rewards.BlockRewards> {
|
|
29
|
+
const fork = config.getForkName(block.slot);
|
|
30
|
+
const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} =
|
|
31
|
+
postState?.proposerRewards ?? {};
|
|
32
|
+
let blockAttestationReward = cachedAttestationsReward;
|
|
33
|
+
let syncAggregateReward = cachedSyncAggregateReward;
|
|
34
|
+
|
|
35
|
+
if (blockAttestationReward === 0) {
|
|
36
|
+
blockAttestationReward =
|
|
37
|
+
fork === ForkName.phase0
|
|
38
|
+
? computeBlockAttestationRewardPhase0(block as phase0.BeaconBlock, preState as CachedBeaconStatePhase0)
|
|
39
|
+
: computeBlockAttestationRewardAltair(config, block as altair.BeaconBlock, preState as CachedBeaconStateAltair);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (syncAggregateReward === 0) {
|
|
43
|
+
syncAggregateReward = computeSyncAggregateReward(block as altair.BeaconBlock, preState as CachedBeaconStateAltair);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const blockProposerSlashingReward = computeBlockProposerSlashingReward(fork, block, preState);
|
|
47
|
+
const blockAttesterSlashingReward = computeBlockAttesterSlashingReward(fork, block, preState);
|
|
48
|
+
|
|
49
|
+
const total =
|
|
50
|
+
blockAttestationReward + syncAggregateReward + blockProposerSlashingReward + blockAttesterSlashingReward;
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
proposerIndex: block.proposerIndex,
|
|
54
|
+
total,
|
|
55
|
+
attestations: blockAttestationReward,
|
|
56
|
+
syncAggregate: syncAggregateReward,
|
|
57
|
+
proposerSlashings: blockProposerSlashingReward,
|
|
58
|
+
attesterSlashings: blockAttesterSlashingReward,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* TODO: Calculate rewards received by block proposer for including attestations.
|
|
64
|
+
*/
|
|
65
|
+
function computeBlockAttestationRewardPhase0(
|
|
66
|
+
_block: phase0.BeaconBlock,
|
|
67
|
+
_preState: CachedBeaconStatePhase0
|
|
68
|
+
): SubRewardValue {
|
|
69
|
+
throw new Error("Unsupported fork! Block attestation reward calculation is not available in phase0");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Calculate rewards received by block proposer for including attestations since Altair.
|
|
74
|
+
* Reuses `processAttestationsAltair()`. Has dependency on RewardCache
|
|
75
|
+
*/
|
|
76
|
+
function computeBlockAttestationRewardAltair(
|
|
77
|
+
config: BeaconConfig,
|
|
78
|
+
block: altair.BeaconBlock,
|
|
79
|
+
preState: CachedBeaconStateAltair
|
|
80
|
+
): SubRewardValue {
|
|
81
|
+
const fork = config.getForkSeq(block.slot);
|
|
82
|
+
const {attestations} = block.body;
|
|
83
|
+
|
|
84
|
+
processAttestationsAltair(fork, preState, attestations, false);
|
|
85
|
+
|
|
86
|
+
return preState.proposerRewards.attestations;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function computeSyncAggregateReward(block: altair.BeaconBlock, preState: CachedBeaconStateAltair): SubRewardValue {
|
|
90
|
+
if (block.body.syncAggregate !== undefined) {
|
|
91
|
+
const {syncCommitteeBits} = block.body.syncAggregate;
|
|
92
|
+
const {syncProposerReward} = preState.epochCtx;
|
|
93
|
+
|
|
94
|
+
return syncCommitteeBits.getTrueBitIndexes().length * Math.floor(syncProposerReward); // syncProposerReward should already be integer
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return 0; // phase0 block does not have syncAggregate
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Calculate rewards received by block proposer for including proposer slashings.
|
|
102
|
+
* All proposer slashing rewards go to block proposer and none to whistleblower as of Deneb
|
|
103
|
+
*/
|
|
104
|
+
function computeBlockProposerSlashingReward(
|
|
105
|
+
fork: ForkName,
|
|
106
|
+
block: BeaconBlock,
|
|
107
|
+
state: CachedBeaconStateAllForks
|
|
108
|
+
): SubRewardValue {
|
|
109
|
+
let proposerSlashingReward = 0;
|
|
110
|
+
|
|
111
|
+
for (const proposerSlashing of block.body.proposerSlashings) {
|
|
112
|
+
const offendingProposerIndex = proposerSlashing.signedHeader1.message.proposerIndex;
|
|
113
|
+
const offendingProposerBalance = state.validators.getReadonly(offendingProposerIndex).effectiveBalance;
|
|
114
|
+
const whistleblowerRewardQuotient = isForkPostElectra(fork)
|
|
115
|
+
? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
|
|
116
|
+
: WHISTLEBLOWER_REWARD_QUOTIENT;
|
|
117
|
+
|
|
118
|
+
proposerSlashingReward += Math.floor(offendingProposerBalance / whistleblowerRewardQuotient);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return proposerSlashingReward;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Calculate rewards received by block proposer for including attester slashings.
|
|
126
|
+
* All attester slashing rewards go to block proposer and none to whistleblower as of Deneb
|
|
127
|
+
*/
|
|
128
|
+
function computeBlockAttesterSlashingReward(
|
|
129
|
+
fork: ForkName,
|
|
130
|
+
block: BeaconBlock,
|
|
131
|
+
preState: CachedBeaconStateAllForks
|
|
132
|
+
): SubRewardValue {
|
|
133
|
+
let attesterSlashingReward = 0;
|
|
134
|
+
|
|
135
|
+
for (const attesterSlashing of block.body.attesterSlashings) {
|
|
136
|
+
for (const offendingAttesterIndex of getAttesterSlashableIndices(attesterSlashing)) {
|
|
137
|
+
const offendingAttesterBalance = preState.validators.getReadonly(offendingAttesterIndex).effectiveBalance;
|
|
138
|
+
const whistleblowerRewardQuotient = isForkPostElectra(fork)
|
|
139
|
+
? WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA
|
|
140
|
+
: WHISTLEBLOWER_REWARD_QUOTIENT;
|
|
141
|
+
|
|
142
|
+
attesterSlashingReward += Math.floor(offendingAttesterBalance / whistleblowerRewardQuotient);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return attesterSlashingReward;
|
|
147
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {BeaconConfig} from "@lodestar/config";
|
|
2
|
+
import {ForkName, SYNC_COMMITTEE_SIZE} from "@lodestar/params";
|
|
3
|
+
import {BeaconBlock, ValidatorIndex, altair, rewards} from "@lodestar/types";
|
|
4
|
+
import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
|
|
5
|
+
import {CachedBeaconStateAllForks, CachedBeaconStateAltair} from "../cache/stateCache.js";
|
|
6
|
+
|
|
7
|
+
type BalanceRecord = {val: number}; // Use val for convenient way to increment/decrement balance
|
|
8
|
+
|
|
9
|
+
export async function computeSyncCommitteeRewards(
|
|
10
|
+
config: BeaconConfig,
|
|
11
|
+
index2pubkey: Index2PubkeyCache,
|
|
12
|
+
block: BeaconBlock,
|
|
13
|
+
preState: CachedBeaconStateAllForks,
|
|
14
|
+
validatorIds: (ValidatorIndex | string)[] = []
|
|
15
|
+
): Promise<rewards.SyncCommitteeRewards> {
|
|
16
|
+
const fork = config.getForkName(block.slot);
|
|
17
|
+
if (fork === ForkName.phase0) {
|
|
18
|
+
throw Error("Cannot get sync rewards as phase0 block does not have sync committee");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const altairBlock = block as altair.BeaconBlock;
|
|
22
|
+
const preStateAltair = preState as CachedBeaconStateAltair;
|
|
23
|
+
|
|
24
|
+
// Bound syncCommitteeValidatorIndices in case it goes beyond SYNC_COMMITTEE_SIZE just to be safe
|
|
25
|
+
const syncCommitteeValidatorIndices = preStateAltair.epochCtx.currentSyncCommitteeIndexed.validatorIndices.slice(
|
|
26
|
+
0,
|
|
27
|
+
SYNC_COMMITTEE_SIZE
|
|
28
|
+
);
|
|
29
|
+
const {syncParticipantReward} = preStateAltair.epochCtx;
|
|
30
|
+
const {syncCommitteeBits} = altairBlock.body.syncAggregate;
|
|
31
|
+
|
|
32
|
+
// Use balance of each committee as starting point such that we cap the penalty to avoid balance dropping below 0
|
|
33
|
+
const balances: Map<ValidatorIndex, BalanceRecord> = new Map();
|
|
34
|
+
for (const i of syncCommitteeValidatorIndices) {
|
|
35
|
+
balances.set(i, {val: preStateAltair.balances.get(i)});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
for (const i of syncCommitteeValidatorIndices) {
|
|
39
|
+
const balanceRecord = balances.get(i) as BalanceRecord;
|
|
40
|
+
if (syncCommitteeBits.get(i)) {
|
|
41
|
+
// Positive rewards for participants
|
|
42
|
+
balanceRecord.val += syncParticipantReward;
|
|
43
|
+
} else {
|
|
44
|
+
// Negative rewards for non participants
|
|
45
|
+
balanceRecord.val = Math.max(0, balanceRecord.val - syncParticipantReward);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const rewards = Array.from(balances, ([validatorIndex, v]) => ({validatorIndex, reward: v.val}));
|
|
50
|
+
|
|
51
|
+
if (validatorIds.length) {
|
|
52
|
+
const filtersSet = new Set(validatorIds);
|
|
53
|
+
return rewards.filter(
|
|
54
|
+
(reward) => filtersSet.has(reward.validatorIndex) || filtersSet.has(index2pubkey[reward.validatorIndex].toHex())
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return rewards;
|
|
59
|
+
}
|
|
@@ -2,7 +2,6 @@ import {BeaconConfig} from "@lodestar/config";
|
|
|
2
2
|
import {DOMAIN_BEACON_PROPOSER} from "@lodestar/params";
|
|
3
3
|
import {SignedBeaconBlock, SignedBlindedBeaconBlock, Slot, isBlindedBeaconBlock, phase0, ssz} from "@lodestar/types";
|
|
4
4
|
import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
|
|
5
|
-
import {CachedBeaconStateAllForks} from "../types.js";
|
|
6
5
|
import {computeSigningRoot} from "../util/index.js";
|
|
7
6
|
import {ISignatureSet, SignatureSetType, verifySignatureSet} from "../util/signatureSets.js";
|
|
8
7
|
|
|
@@ -38,28 +37,28 @@ export function getBlockProposerSignatureSet(
|
|
|
38
37
|
}
|
|
39
38
|
|
|
40
39
|
export function getBlockHeaderProposerSignatureSetByParentStateSlot(
|
|
40
|
+
config: BeaconConfig,
|
|
41
41
|
index2pubkey: Index2PubkeyCache,
|
|
42
|
-
|
|
42
|
+
parentStateSlot: Slot,
|
|
43
43
|
signedBlockHeader: phase0.SignedBeaconBlockHeader
|
|
44
44
|
) {
|
|
45
|
-
return getBlockHeaderProposerSignatureSet(
|
|
45
|
+
return getBlockHeaderProposerSignatureSet(config, index2pubkey, signedBlockHeader, parentStateSlot);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export function getBlockHeaderProposerSignatureSetByHeaderSlot(
|
|
49
|
+
config: BeaconConfig,
|
|
49
50
|
index2pubkey: Index2PubkeyCache,
|
|
50
|
-
headState: CachedBeaconStateAllForks,
|
|
51
51
|
signedBlockHeader: phase0.SignedBeaconBlockHeader
|
|
52
52
|
) {
|
|
53
|
-
return getBlockHeaderProposerSignatureSet(
|
|
53
|
+
return getBlockHeaderProposerSignatureSet(config, index2pubkey, signedBlockHeader, signedBlockHeader.message.slot);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
function getBlockHeaderProposerSignatureSet(
|
|
57
|
+
config: BeaconConfig,
|
|
57
58
|
index2pubkey: Index2PubkeyCache,
|
|
58
|
-
state: CachedBeaconStateAllForks,
|
|
59
59
|
signedBlockHeader: phase0.SignedBeaconBlockHeader,
|
|
60
60
|
domainSlot: Slot
|
|
61
61
|
): ISignatureSet {
|
|
62
|
-
const {config} = state;
|
|
63
62
|
const domain = config.getDomain(domainSlot, DOMAIN_BEACON_PROPOSER, signedBlockHeader.message.slot);
|
|
64
63
|
|
|
65
64
|
return {
|