client/api: fix possible deadlock when comparing with itself (#6277)

This commit is contained in:
Boqin Qin
2020-06-08 19:39:50 +08:00
committed by GitHub
parent 15ecac5cb6
commit d884848625
+9
View File
@@ -19,6 +19,7 @@
//! In memory client backend
use std::collections::HashMap;
use std::ptr;
use std::sync::Arc;
use parking_lot::RwLock;
use sp_core::{
@@ -191,11 +192,19 @@ impl<Block: BlockT> Blockchain<Block> {
/// Compare this blockchain with another in-mem blockchain
pub fn equals_to(&self, other: &Self) -> bool {
// Check ptr equality first to avoid double read locks.
if ptr::eq(self, other) {
return true;
}
self.canon_equals_to(other) && self.storage.read().blocks == other.storage.read().blocks
}
/// Compare canonical chain to other canonical chain.
pub fn canon_equals_to(&self, other: &Self) -> bool {
// Check ptr equality first to avoid double read locks.
if ptr::eq(self, other) {
return true;
}
let this = self.storage.read();
let other = other.storage.read();
this.hashes == other.hashes