beacon_node, consensus: fix possible deadlocks when comparing with itself (#1241)

This commit is contained in:
Boqin Qin 2020-06-09 05:08:54 +08:00 committed by GitHub
parent 208f1da81b
commit 7baac70056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 0 deletions

View File

@ -20,6 +20,7 @@ use state_processing::per_block_processing::{
}; };
use std::collections::{hash_map, HashMap, HashSet}; use std::collections::{hash_map, HashMap, HashSet};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::ptr;
use types::{ use types::{
typenum::Unsigned, Attestation, AttesterSlashing, BeaconState, BeaconStateError, ChainSpec, typenum::Unsigned, Attestation, AttesterSlashing, BeaconState, BeaconStateError, ChainSpec,
EthSpec, Fork, Hash256, ProposerSlashing, RelativeEpoch, SignedVoluntaryExit, Validator, EthSpec, Fork, Hash256, ProposerSlashing, RelativeEpoch, SignedVoluntaryExit, Validator,
@ -408,6 +409,9 @@ fn prune_validator_hash_map<T, F, E: EthSpec>(
/// Compare two operation pools. /// Compare two operation pools.
impl<T: EthSpec + Default> PartialEq for OperationPool<T> { impl<T: EthSpec + Default> PartialEq for OperationPool<T> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
if ptr::eq(self, other) {
return true;
}
*self.attestations.read() == *other.attestations.read() *self.attestations.read() == *other.attestations.read()
&& *self.attester_slashings.read() == *other.attester_slashings.read() && *self.attester_slashings.read() == *other.attester_slashings.read()
&& *self.proposer_slashings.read() == *other.proposer_slashings.read() && *self.proposer_slashings.read() == *other.proposer_slashings.read()

View File

@ -5,6 +5,7 @@ use parking_lot::{RwLock, RwLockReadGuard};
use ssz::{Decode, Encode}; use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode}; use ssz_derive::{Decode, Encode};
use std::collections::HashMap; use std::collections::HashMap;
use std::ptr;
use types::{Epoch, Hash256, Slot}; use types::{Epoch, Hash256, Slot};
pub const DEFAULT_PRUNE_THRESHOLD: usize = 256; pub const DEFAULT_PRUNE_THRESHOLD: usize = 256;
@ -51,6 +52,9 @@ pub struct ProtoArrayForkChoice {
impl PartialEq for ProtoArrayForkChoice { impl PartialEq for ProtoArrayForkChoice {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
if ptr::eq(self, other) {
return true;
}
*self.proto_array.read() == *other.proto_array.read() *self.proto_array.read() == *other.proto_array.read()
&& *self.votes.read() == *other.votes.read() && *self.votes.read() == *other.votes.read()
&& *self.balances.read() == *other.balances.read() && *self.balances.read() == *other.balances.read()