|
2 | 2 |
|
3 | 3 | #![cfg(not(debug_assertions))] |
4 | 4 |
|
5 | | -use beacon_chain::observed_operations::ObservationOutcome; |
6 | | -use beacon_chain::test_utils::{ |
7 | | - test_spec, AttestationStrategy, BeaconChainHarness, BlockStrategy, DiskHarnessType, |
| 5 | +use beacon_chain::{ |
| 6 | + observed_operations::ObservationOutcome, |
| 7 | + test_utils::{ |
| 8 | + test_spec, AttestationStrategy, BeaconChainHarness, BlockStrategy, DiskHarnessType, |
| 9 | + }, |
| 10 | + BeaconChainError, |
8 | 11 | }; |
9 | 12 | use lazy_static::lazy_static; |
10 | 13 | use sloggers::{null::NullLoggerBuilder, Build}; |
| 14 | +use state_processing::per_block_processing::errors::{ |
| 15 | + AttesterSlashingInvalid, BlockOperationError, ExitInvalid, ProposerSlashingInvalid, |
| 16 | +}; |
11 | 17 | use std::sync::Arc; |
12 | 18 | use store::{LevelDB, StoreConfig}; |
13 | 19 | use tempfile::{tempdir, TempDir}; |
@@ -119,6 +125,75 @@ async fn voluntary_exit() { |
119 | 125 | )); |
120 | 126 | } |
121 | 127 |
|
| 128 | +#[tokio::test] |
| 129 | +async fn voluntary_exit_duplicate_in_state() { |
| 130 | + let db_path = tempdir().unwrap(); |
| 131 | + let store = get_store(&db_path); |
| 132 | + let harness = get_harness(store.clone(), VALIDATOR_COUNT); |
| 133 | + let spec = &harness.chain.spec; |
| 134 | + |
| 135 | + harness |
| 136 | + .extend_chain( |
| 137 | + (E::slots_per_epoch() * (spec.shard_committee_period + 1)) as usize, |
| 138 | + BlockStrategy::OnCanonicalHead, |
| 139 | + AttestationStrategy::AllValidators, |
| 140 | + ) |
| 141 | + .await; |
| 142 | + harness.advance_slot(); |
| 143 | + |
| 144 | + // Exit a validator. |
| 145 | + let exited_validator = 0; |
| 146 | + let exit = |
| 147 | + harness.make_voluntary_exit(exited_validator, Epoch::new(spec.shard_committee_period)); |
| 148 | + let ObservationOutcome::New(verified_exit) = harness |
| 149 | + .chain |
| 150 | + .verify_voluntary_exit_for_gossip(exit.clone()) |
| 151 | + .unwrap() |
| 152 | + else { |
| 153 | + panic!("exit should verify"); |
| 154 | + }; |
| 155 | + harness.chain.import_voluntary_exit(verified_exit); |
| 156 | + |
| 157 | + // Make a new block to include the exit. |
| 158 | + harness |
| 159 | + .extend_chain( |
| 160 | + 1, |
| 161 | + BlockStrategy::OnCanonicalHead, |
| 162 | + AttestationStrategy::AllValidators, |
| 163 | + ) |
| 164 | + .await; |
| 165 | + |
| 166 | + // Verify validator is actually exited. |
| 167 | + assert_ne!( |
| 168 | + harness |
| 169 | + .get_current_state() |
| 170 | + .validators() |
| 171 | + .get(exited_validator as usize) |
| 172 | + .unwrap() |
| 173 | + .exit_epoch, |
| 174 | + spec.far_future_epoch |
| 175 | + ); |
| 176 | + |
| 177 | + // Clear the in-memory gossip cache & try to verify the same exit on gossip. |
| 178 | + // It should still fail because gossip verification should check the validator's `exit_epoch` |
| 179 | + // field in the head state. |
| 180 | + harness |
| 181 | + .chain |
| 182 | + .observed_voluntary_exits |
| 183 | + .lock() |
| 184 | + .__reset_for_testing_only(); |
| 185 | + |
| 186 | + assert!(matches!( |
| 187 | + harness |
| 188 | + .chain |
| 189 | + .verify_voluntary_exit_for_gossip(exit) |
| 190 | + .unwrap_err(), |
| 191 | + BeaconChainError::ExitValidationError(BlockOperationError::Invalid( |
| 192 | + ExitInvalid::AlreadyExited(index) |
| 193 | + )) if index == exited_validator |
| 194 | + )); |
| 195 | +} |
| 196 | + |
122 | 197 | #[test] |
123 | 198 | fn proposer_slashing() { |
124 | 199 | let db_path = tempdir().unwrap(); |
@@ -171,6 +246,63 @@ fn proposer_slashing() { |
171 | 246 | )); |
172 | 247 | } |
173 | 248 |
|
| 249 | +#[tokio::test] |
| 250 | +async fn proposer_slashing_duplicate_in_state() { |
| 251 | + let db_path = tempdir().unwrap(); |
| 252 | + let store = get_store(&db_path); |
| 253 | + let harness = get_harness(store.clone(), VALIDATOR_COUNT); |
| 254 | + |
| 255 | + // Slash a validator. |
| 256 | + let slashed_validator = 0; |
| 257 | + let slashing = harness.make_proposer_slashing(slashed_validator); |
| 258 | + let ObservationOutcome::New(verified_slashing) = harness |
| 259 | + .chain |
| 260 | + .verify_proposer_slashing_for_gossip(slashing.clone()) |
| 261 | + .unwrap() |
| 262 | + else { |
| 263 | + panic!("slashing should verify"); |
| 264 | + }; |
| 265 | + harness.chain.import_proposer_slashing(verified_slashing); |
| 266 | + |
| 267 | + // Make a new block to include the slashing. |
| 268 | + harness |
| 269 | + .extend_chain( |
| 270 | + 1, |
| 271 | + BlockStrategy::OnCanonicalHead, |
| 272 | + AttestationStrategy::AllValidators, |
| 273 | + ) |
| 274 | + .await; |
| 275 | + |
| 276 | + // Verify validator is actually slashed. |
| 277 | + assert!( |
| 278 | + harness |
| 279 | + .get_current_state() |
| 280 | + .validators() |
| 281 | + .get(slashed_validator as usize) |
| 282 | + .unwrap() |
| 283 | + .slashed |
| 284 | + ); |
| 285 | + |
| 286 | + // Clear the in-memory gossip cache & try to verify the same slashing on gossip. |
| 287 | + // It should still fail because gossip verification should check the validator's `slashed` field |
| 288 | + // in the head state. |
| 289 | + harness |
| 290 | + .chain |
| 291 | + .observed_proposer_slashings |
| 292 | + .lock() |
| 293 | + .__reset_for_testing_only(); |
| 294 | + |
| 295 | + assert!(matches!( |
| 296 | + harness |
| 297 | + .chain |
| 298 | + .verify_proposer_slashing_for_gossip(slashing) |
| 299 | + .unwrap_err(), |
| 300 | + BeaconChainError::ProposerSlashingValidationError(BlockOperationError::Invalid( |
| 301 | + ProposerSlashingInvalid::ProposerNotSlashable(index) |
| 302 | + )) if index == slashed_validator |
| 303 | + )); |
| 304 | +} |
| 305 | + |
174 | 306 | #[test] |
175 | 307 | fn attester_slashing() { |
176 | 308 | let db_path = tempdir().unwrap(); |
@@ -241,3 +373,60 @@ fn attester_slashing() { |
241 | 373 | ObservationOutcome::AlreadyKnown |
242 | 374 | )); |
243 | 375 | } |
| 376 | + |
| 377 | +#[tokio::test] |
| 378 | +async fn attester_slashing_duplicate_in_state() { |
| 379 | + let db_path = tempdir().unwrap(); |
| 380 | + let store = get_store(&db_path); |
| 381 | + let harness = get_harness(store.clone(), VALIDATOR_COUNT); |
| 382 | + |
| 383 | + // Slash a validator. |
| 384 | + let slashed_validator = 0; |
| 385 | + let slashing = harness.make_attester_slashing(vec![slashed_validator]); |
| 386 | + let ObservationOutcome::New(verified_slashing) = harness |
| 387 | + .chain |
| 388 | + .verify_attester_slashing_for_gossip(slashing.clone()) |
| 389 | + .unwrap() |
| 390 | + else { |
| 391 | + panic!("slashing should verify"); |
| 392 | + }; |
| 393 | + harness.chain.import_attester_slashing(verified_slashing); |
| 394 | + |
| 395 | + // Make a new block to include the slashing. |
| 396 | + harness |
| 397 | + .extend_chain( |
| 398 | + 1, |
| 399 | + BlockStrategy::OnCanonicalHead, |
| 400 | + AttestationStrategy::AllValidators, |
| 401 | + ) |
| 402 | + .await; |
| 403 | + |
| 404 | + // Verify validator is actually slashed. |
| 405 | + assert!( |
| 406 | + harness |
| 407 | + .get_current_state() |
| 408 | + .validators() |
| 409 | + .get(slashed_validator as usize) |
| 410 | + .unwrap() |
| 411 | + .slashed |
| 412 | + ); |
| 413 | + |
| 414 | + // Clear the in-memory gossip cache & try to verify the same slashing on gossip. |
| 415 | + // It should still fail because gossip verification should check the validator's `slashed` field |
| 416 | + // in the head state. |
| 417 | + harness |
| 418 | + .chain |
| 419 | + .observed_attester_slashings |
| 420 | + .lock() |
| 421 | + .__reset_for_testing_only(); |
| 422 | + |
| 423 | + assert!(matches!( |
| 424 | + harness |
| 425 | + .chain |
| 426 | + .verify_attester_slashing_for_gossip(slashing) |
| 427 | + .unwrap_err(), |
| 428 | + BeaconChainError::AttesterSlashingValidationError(BlockOperationError::Invalid( |
| 429 | + AttesterSlashingInvalid::NoSlashableIndices |
| 430 | + )) |
| 431 | + )); |
| 432 | +} |
0 commit comments