diff --git a/chain/src/chain.rs b/chain/src/chain.rs index a48a5bf38..18321d96c 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -53,7 +53,7 @@ pub struct Chain { head: Arc>, block_process_lock: Arc>, - orphans: Arc>>, + orphans: Arc>>, test_mode: bool, } @@ -67,10 +67,11 @@ impl Chain { /// on the current chain head to make sure it exists and creates one based /// on /// the genesis block if necessary. - pub fn init(test_mode: bool, - db_root: String, - adapter: Arc) - -> Result { + pub fn init( + test_mode: bool, + db_root: String, + adapter: Arc, + ) -> Result { let chain_store = store::ChainKVStore::new(db_root)?; // check if we have a head in store, otherwise the genesis block is it @@ -105,7 +106,7 @@ impl Chain { adapter: adapter, head: Arc::new(Mutex::new(head)), block_process_lock: Arc::new(Mutex::new(true)), - orphans: Arc::new(Mutex::new(VecDeque::with_capacity(MAX_ORPHANS+1))), + orphans: Arc::new(Mutex::new(VecDeque::with_capacity(MAX_ORPHANS + 1))), test_mode: test_mode, }) } @@ -120,32 +121,33 @@ impl Chain { let res = pipe::process_block(&b, ctx); - match res { - Ok(Some(ref tip)) => { - // block got accepted and extended the head, updating our head - let chain_head = self.head.clone(); - let mut head = chain_head.lock().unwrap(); - *head = tip.clone(); + match res { + Ok(Some(ref tip)) => { + // block got accepted and extended the head, updating our head + let chain_head = self.head.clone(); + let mut head = chain_head.lock().unwrap(); + *head = tip.clone(); - self.check_orphans(); - } - Err(Error::Orphan) => { - let mut orphans = self.orphans.lock().unwrap(); - orphans.push_front(b); - orphans.truncate(MAX_ORPHANS); - } - _ => {} - } + self.check_orphans(); + } + Err(Error::Orphan) => { + let mut orphans = self.orphans.lock().unwrap(); + orphans.push_front(b); + orphans.truncate(MAX_ORPHANS); + } + _ => {} + } res } /// Attempt to add a new header to the header chain. Only necessary during /// sync. - pub fn process_block_header(&self, - bh: &BlockHeader, - opts: Options) - -> Result, Error> { + pub fn process_block_header( + &self, + bh: &BlockHeader, + opts: Options, + ) -> Result, Error> { let head = self.store.get_header_head().map_err(&Error::StoreErr)?; let ctx = self.ctx_from_head(head, opts); @@ -167,32 +169,32 @@ impl Chain { } } - /// Pop orphans out of the queue and check if we can now accept them. - fn check_orphans(&self) { - // first check how many we have to retry, unfort. we can't extend the lock - // in the loop as it needs to be freed before going in process_block - let mut orphan_count = 0; - { - let orphans = self.orphans.lock().unwrap(); - orphan_count = orphans.len(); - } + /// Pop orphans out of the queue and check if we can now accept them. + fn check_orphans(&self) { + // first check how many we have to retry, unfort. we can't extend the lock + // in the loop as it needs to be freed before going in process_block + let mut orphan_count = 0; + { + let orphans = self.orphans.lock().unwrap(); + orphan_count = orphans.len(); + } - // pop each orphan and retry, if still orphaned, will be pushed again + // pop each orphan and retry, if still orphaned, will be pushed again let mut opts = NONE; if self.test_mode { opts = opts | EASY_POW; } - for _ in 0..orphan_count { - let mut popped = None; - { - let mut orphans = self.orphans.lock().unwrap(); - popped = orphans.pop_back(); - } - if let Some(orphan) = popped { - self.process_block(orphan, opts); - } - } - } + for _ in 0..orphan_count { + let mut popped = None; + { + let mut orphans = self.orphans.lock().unwrap(); + popped = orphans.pop_back(); + } + if let Some(orphan) = popped { + self.process_block(orphan, opts); + } + } + } /// Gets an unspent output from its commitment. With return None if the /// output @@ -252,7 +254,9 @@ impl Chain { /// Gets the block header at the provided height pub fn get_header_by_height(&self, height: u64) -> Result { - self.store.get_header_by_height(height).map_err(&Error::StoreErr) + self.store.get_header_by_height(height).map_err( + &Error::StoreErr, + ) } /// Get the tip of the header chain diff --git a/chain/src/pipe.rs b/chain/src/pipe.rs index 64c2fed37..818ea9ff4 100644 --- a/chain/src/pipe.rs +++ b/chain/src/pipe.rs @@ -46,11 +46,13 @@ pub fn process_block(b: &Block, mut ctx: BlockContext) -> Result, Er // TODO should just take a promise for a block with a full header so we don't // spend resources reading the full block when its header is invalid - info!("Starting validation pipeline for block {} at {} with {} inputs and {} outputs.", - b.hash(), - b.header.height, - b.inputs.len(), - b.outputs.len()); + info!( + "Starting validation pipeline for block {} at {} with {} inputs and {} outputs.", + b.hash(), + b.header.height, + b.inputs.len(), + b.outputs.len() + ); check_known(b.hash(), &mut ctx)?; if !ctx.opts.intersects(SYNC) { @@ -58,9 +60,11 @@ pub fn process_block(b: &Block, mut ctx: BlockContext) -> Result, Er validate_header(&b.header, &mut ctx)?; } validate_block(b, &mut ctx)?; - debug!("Block at {} with hash {} is valid, going to save and append.", - b.header.height, - b.hash()); + debug!( + "Block at {} with hash {} is valid, going to save and append.", + b.header.height, + b.hash() + ); ctx.lock.lock(); add_block(b, &mut ctx)?; @@ -69,9 +73,11 @@ pub fn process_block(b: &Block, mut ctx: BlockContext) -> Result, Er pub fn process_block_header(bh: &BlockHeader, mut ctx: BlockContext) -> Result, Error> { - info!("Starting validation pipeline for block header {} at {}.", - bh.hash(), - bh.height); + info!( + "Starting validation pipeline for block header {} at {}.", + bh.hash(), + bh.height + ); check_known(bh.hash(), &mut ctx)?; validate_header(&bh, &mut ctx)?; add_block_header(bh, &mut ctx)?; @@ -106,7 +112,9 @@ fn validate_header(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), E return Err(Error::Orphan); } - let prev = try!(ctx.store.get_block_header(&header.previous).map_err(&Error::StoreErr)); + let prev = try!(ctx.store.get_block_header(&header.previous).map_err( + &Error::StoreErr, + )); if header.height != prev.height + 1 { return Err(Error::InvalidBlockHeight); @@ -117,7 +125,8 @@ fn validate_header(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), E return Err(Error::InvalidBlockTime); } if header.timestamp > - time::now() + time::Duration::seconds(12 * (consensus::BLOCK_TIME_SEC as i64)) { + time::now() + time::Duration::seconds(12 * (consensus::BLOCK_TIME_SEC as i64)) + { // refuse blocks more than 12 blocks intervals in future (as in bitcoin) // TODO add warning in p2p code if local time is too different from peers return Err(Error::InvalidBlockTime); @@ -131,8 +140,9 @@ fn validate_header(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), E } let diff_iter = store::DifficultyIter::from(header.previous, ctx.store.clone()); - let difficulty = - consensus::next_difficulty(diff_iter).map_err(|e| Error::Other(e.to_string()))?; + let difficulty = consensus::next_difficulty(diff_iter).map_err(|e| { + Error::Other(e.to_string()) + })?; if header.difficulty < difficulty { return Err(Error::DifficultyTooLow); } @@ -224,9 +234,11 @@ fn update_header_head(bh: &BlockHeader, ctx: &mut BlockContext) -> Result