take write lock on txhashet earlier when processing blocks (#1456)

* take write lock on txhashet earlier when processing blocks

* no txhashset lock in process_block_header
This commit is contained in:
Antioch Peverell 2018-09-03 11:09:53 +01:00 committed by GitHub
parent 9f7625e8aa
commit eae0ab6b2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -75,6 +75,14 @@ pub fn process_block(
b.outputs().len(), b.outputs().len(),
b.kernels().len(), b.kernels().len(),
); );
// First thing we do is take a write lock on the txhashset.
// We may receive the same block from multiple peers simultaneously.
// We want to process the first one fully to avoid redundant work
// processing the duplicates.
let txhashset = ctx.txhashset.clone();
let mut txhashset = txhashset.write().unwrap();
check_known(b.hash(), ctx)?; check_known(b.hash(), ctx)?;
validate_header(&b.header, ctx)?; validate_header(&b.header, ctx)?;
@ -99,14 +107,8 @@ pub fn process_block(
} }
// validate the block itself // validate the block itself
// we can do this now before interacting with the txhashset
let _sums = validate_block(b, ctx, verifier_cache)?; let _sums = validate_block(b, ctx, verifier_cache)?;
// header and block both valid, and we have a previous block
// so take the lock on the txhashset
let local_txhashset = ctx.txhashset.clone();
let mut txhashset = local_txhashset.write().unwrap();
// update head now that we're in the lock // update head now that we're in the lock
ctx.head = ctx.store.head()?; ctx.head = ctx.store.head()?;