-
-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathvalidatorStatus.ts
196 lines (183 loc) Β· 6.81 KB
/
validatorStatus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @module chain/stateTransition/util
*/
import {ValidatorIndex, phase0, allForks} from "@chainsafe/lodestar-types";
import {IBeaconConfig} from "@chainsafe/lodestar-config";
import bls from "@chainsafe/bls";
import {FAR_FUTURE_EPOCH} from "../constants";
import {computeActivationExitEpoch, getCurrentEpoch, computeEpochAtSlot} from "./epoch";
import {getValidatorChurnLimit, isSlashableValidator, isActiveValidator} from "./validator";
import {decreaseBalance, increaseBalance} from "./balance";
import {getBeaconProposerIndex} from "./proposer";
import {isSlashableAttestationData, isValidIndexedAttestation, getDomain} from ".";
import {computeSigningRoot} from "./signingRoot";
/**
* Initiate exit for the validator with the given index.
*
* Note: that this function mutates state.
*/
export function initiateValidatorExit(config: IBeaconConfig, state: allForks.BeaconState, index: ValidatorIndex): void {
const validator = state.validators[index];
// Return if validator already initiated exit
if (validator.exitEpoch !== FAR_FUTURE_EPOCH) {
return;
}
// Compute exit queue epoch
let exitQueueEpoch = computeActivationExitEpoch(config, getCurrentEpoch(config, state));
for (const v of state.validators) {
if (v.exitEpoch !== FAR_FUTURE_EPOCH) {
exitQueueEpoch = Math.max(v.exitEpoch, exitQueueEpoch);
}
}
const exitQueueChurn = Array.from(state.validators).filter((v: phase0.Validator) => v.exitEpoch === exitQueueEpoch)
.length;
if (exitQueueChurn >= getValidatorChurnLimit(config, state)) {
exitQueueEpoch += 1;
}
// Set validator exit epoch and withdrawable epoch
state.validators[index] = {
...validator,
exitEpoch: exitQueueEpoch,
withdrawableEpoch: exitQueueEpoch + config.params.MIN_VALIDATOR_WITHDRAWABILITY_DELAY,
};
}
/**
* Slash the validator with index ``slashedIndex``.
*
* Note that this function mutates ``state``.
*/
export function slashValidator(
config: IBeaconConfig,
state: allForks.BeaconState,
slashedIndex: ValidatorIndex,
whistleblowerIndex: ValidatorIndex | null = null
): void {
const currentEpoch = getCurrentEpoch(config, state);
initiateValidatorExit(config, state, slashedIndex);
state.validators[slashedIndex].slashed = true;
state.validators[slashedIndex].withdrawableEpoch = Math.max(
state.validators[slashedIndex].withdrawableEpoch,
currentEpoch + config.params.EPOCHS_PER_SLASHINGS_VECTOR
);
const slashedBalance = state.validators[slashedIndex].effectiveBalance;
state.slashings[currentEpoch % config.params.EPOCHS_PER_SLASHINGS_VECTOR] += slashedBalance;
decreaseBalance(
state,
slashedIndex,
state.validators[slashedIndex].effectiveBalance / BigInt(config.params.MIN_SLASHING_PENALTY_QUOTIENT)
);
const proposerIndex = getBeaconProposerIndex(config, state);
if (whistleblowerIndex === undefined || whistleblowerIndex === null) {
whistleblowerIndex = proposerIndex;
}
const whistleblowingReward = slashedBalance / BigInt(config.params.WHISTLEBLOWER_REWARD_QUOTIENT);
const proposerReward = whistleblowingReward / BigInt(config.params.PROPOSER_REWARD_QUOTIENT);
increaseBalance(state, proposerIndex, proposerReward);
increaseBalance(state, whistleblowerIndex, whistleblowingReward - proposerReward);
}
export function isValidAttesterSlashing(
config: IBeaconConfig,
state: allForks.BeaconState,
attesterSlashing: phase0.AttesterSlashing,
verifySignatures = true
): boolean {
const attestation1 = attesterSlashing.attestation1;
const attestation2 = attesterSlashing.attestation2;
return (
isSlashableAttestationData(config, attestation1.data, attestation2.data) &&
isValidIndexedAttestation(config, state, attestation1, verifySignatures) &&
isValidIndexedAttestation(config, state, attestation2, verifySignatures)
);
}
export function isValidProposerSlashing(
config: IBeaconConfig,
state: allForks.BeaconState,
proposerSlashing: phase0.ProposerSlashing,
verifySignatures = true
): boolean {
const header1 = proposerSlashing.signedHeader1.message;
const header2 = proposerSlashing.signedHeader2.message;
// Verify the header slots match
if (header1.slot !== header2.slot) {
return false;
}
// Verify header proposer indices match
if (header1.proposerIndex !== header2.proposerIndex) {
return false;
}
// Verify the headers are different
if (config.types.phase0.BeaconBlockHeader.equals(header1, header2)) {
return false;
}
const proposer = state.validators[header1.proposerIndex];
// Check proposer is slashable
if (!isSlashableValidator(proposer, getCurrentEpoch(config, state))) {
return false;
}
// Verify signatures
if (!verifySignatures) {
return true;
}
const domain = getDomain(
config,
state,
config.params.DOMAIN_BEACON_PROPOSER,
computeEpochAtSlot(config, header1.slot)
);
const signingRoot = computeSigningRoot(
config,
config.types.phase0.BeaconBlockHeader,
proposerSlashing.signedHeader1.message,
domain
);
const proposalData1Verified = bls.verify(
proposer.pubkey.valueOf() as Uint8Array,
signingRoot,
proposerSlashing.signedHeader1.signature.valueOf() as Uint8Array
);
if (!proposalData1Verified) {
return false;
}
const domain2 = getDomain(
config,
state,
config.params.DOMAIN_BEACON_PROPOSER,
computeEpochAtSlot(config, header2.slot)
);
const signingRoot2 = computeSigningRoot(
config,
config.types.phase0.BeaconBlockHeader,
proposerSlashing.signedHeader2.message,
domain2
);
const proposalData2Verified = bls.verify(
proposer.pubkey.valueOf() as Uint8Array,
signingRoot2,
proposerSlashing.signedHeader2.signature.valueOf() as Uint8Array
);
return proposalData2Verified;
}
export function isValidVoluntaryExit(
config: IBeaconConfig,
state: allForks.BeaconState,
signedExit: phase0.SignedVoluntaryExit,
verifySignature = true
): boolean {
const validator = state.validators[signedExit.message.validatorIndex];
const currentEpoch = getCurrentEpoch(config, state);
const domain = getDomain(config, state, config.params.DOMAIN_VOLUNTARY_EXIT, signedExit.message.epoch);
const signingRoot = computeSigningRoot(config, config.types.phase0.VoluntaryExit, signedExit.message, domain);
// Verify the validator is active
return (
isActiveValidator(validator, currentEpoch) &&
// Verify the validator has not yet exited
validator.exitEpoch === FAR_FUTURE_EPOCH &&
// Exits must specify an epoch when they become valid; they are not valid before then
currentEpoch >= signedExit.message.epoch &&
// Verify the validator has been active long enough
currentEpoch >= validator.activationEpoch + config.params.SHARD_COMMITTEE_PERIOD &&
// Verify signature
(!verifySignature ||
bls.verify(validator.pubkey.valueOf() as Uint8Array, signingRoot, signedExit.signature.valueOf() as Uint8Array))
);
}