no need for write lock on txhashset for chain.is_unspent() (#2066)

* no need for write lock on txhashset

* commit
This commit is contained in:
Antioch Peverell 2018-12-03 09:35:40 +00:00 committed by GitHub
parent 8bad93c8c9
commit a24d613b05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 5 deletions

View file

@ -438,7 +438,7 @@ impl Chain {
/// current chain state, specifically the current winning (valid, most
/// work) fork.
pub fn is_unspent(&self, output_ref: &OutputIdentifier) -> Result<Hash, Error> {
let mut txhashset = self.txhashset.write();
let txhashset = self.txhashset.read();
let res = txhashset.is_unspent(output_ref);
match res {
Err(e) => Err(e),
@ -1096,7 +1096,7 @@ impl Chain {
output_ref: &OutputIdentifier,
) -> Result<BlockHeader, Error> {
let pos = {
let mut txhashset = self.txhashset.write();
let txhashset = self.txhashset.read();
let (_, pos) = txhashset.is_unspent(output_ref)?;
pos
};

View file

@ -160,11 +160,11 @@ impl TxHashSet {
/// Check if an output is unspent.
/// We look in the index to find the output MMR pos.
/// Then we check the entry in the output MMR and confirm the hash matches.
pub fn is_unspent(&mut self, output_id: &OutputIdentifier) -> Result<(Hash, u64), Error> {
pub fn is_unspent(&self, output_id: &OutputIdentifier) -> Result<(Hash, u64), Error> {
match self.commit_index.get_output_pos(&output_id.commit) {
Ok(pos) => {
let output_pmmr: PMMR<Output, _> =
PMMR::at(&mut self.output_pmmr_h.backend, self.output_pmmr_h.last_pos);
let output_pmmr: ReadonlyPMMR<Output, _> =
ReadonlyPMMR::at(&self.output_pmmr_h.backend, self.output_pmmr_h.last_pos);
if let Some(hash) = output_pmmr.get_hash(pos) {
if hash == output_id.hash_with_index(pos - 1) {
Ok((hash, pos))

View file

@ -16,6 +16,7 @@
use std::marker;
use core::hash::Hash;
use core::pmmr::{is_leaf, Backend};
use ser::PMMRable;
@ -70,4 +71,17 @@ where
None
}
}
/// Get the hash at provided position in the MMR.
pub fn get_hash(&self, pos: u64) -> Option<Hash> {
if pos > self.last_pos {
None
} else if is_leaf(pos) {
// If we are a leaf then get hash from the backend.
self.backend.get_hash(pos)
} else {
// If we are not a leaf get hash ignoring the remove log.
self.backend.get_from_file(pos)
}
}
}