From 7baac7005670ac05ad31b7400b3efa1e6b15cded Mon Sep 17 00:00:00 2001 From: Boqin Qin Date: Tue, 9 Jun 2020 05:08:54 +0800 Subject: [PATCH] beacon_node, consensus: fix possible deadlocks when comparing with itself (#1241) --- beacon_node/operation_pool/src/lib.rs | 4 ++++ .../proto_array_fork_choice/src/proto_array_fork_choice.rs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/beacon_node/operation_pool/src/lib.rs b/beacon_node/operation_pool/src/lib.rs index 79eb3f890a..d91aa2cc56 100644 --- a/beacon_node/operation_pool/src/lib.rs +++ b/beacon_node/operation_pool/src/lib.rs @@ -20,6 +20,7 @@ use state_processing::per_block_processing::{ }; use std::collections::{hash_map, HashMap, HashSet}; use std::marker::PhantomData; +use std::ptr; use types::{ typenum::Unsigned, Attestation, AttesterSlashing, BeaconState, BeaconStateError, ChainSpec, EthSpec, Fork, Hash256, ProposerSlashing, RelativeEpoch, SignedVoluntaryExit, Validator, @@ -408,6 +409,9 @@ fn prune_validator_hash_map( /// Compare two operation pools. impl PartialEq for OperationPool { fn eq(&self, other: &Self) -> bool { + if ptr::eq(self, other) { + return true; + } *self.attestations.read() == *other.attestations.read() && *self.attester_slashings.read() == *other.attester_slashings.read() && *self.proposer_slashings.read() == *other.proposer_slashings.read() diff --git a/consensus/proto_array_fork_choice/src/proto_array_fork_choice.rs b/consensus/proto_array_fork_choice/src/proto_array_fork_choice.rs index 8fc390003f..2ce28483be 100644 --- a/consensus/proto_array_fork_choice/src/proto_array_fork_choice.rs +++ b/consensus/proto_array_fork_choice/src/proto_array_fork_choice.rs @@ -5,6 +5,7 @@ use parking_lot::{RwLock, RwLockReadGuard}; use ssz::{Decode, Encode}; use ssz_derive::{Decode, Encode}; use std::collections::HashMap; +use std::ptr; use types::{Epoch, Hash256, Slot}; pub const DEFAULT_PRUNE_THRESHOLD: usize = 256; @@ -51,6 +52,9 @@ pub struct ProtoArrayForkChoice { impl PartialEq for ProtoArrayForkChoice { fn eq(&self, other: &Self) -> bool { + if ptr::eq(self, other) { + return true; + } *self.proto_array.read() == *other.proto_array.read() && *self.votes.read() == *other.votes.read() && *self.balances.read() == *other.balances.read()