2016-01-10 23:37:09 +01:00
|
|
|
use util::*;
|
2016-01-12 13:14:01 +01:00
|
|
|
use blockchain::*;
|
2016-01-09 10:16:35 +01:00
|
|
|
use views::{BlockView};
|
2016-01-11 13:42:32 +01:00
|
|
|
use verification::*;
|
|
|
|
use error::*;
|
|
|
|
use engine::Engine;
|
2016-01-13 23:15:44 +01:00
|
|
|
use sync::*;
|
2016-01-09 10:16:35 +01:00
|
|
|
|
2016-01-10 23:37:09 +01:00
|
|
|
/// A queue of blocks. Sits between network or other I/O and the BlockChain.
|
|
|
|
/// Sorts them ready for blockchain insertion.
|
2016-01-11 13:42:32 +01:00
|
|
|
pub struct BlockQueue {
|
|
|
|
bc: Arc<RwLock<BlockChain>>,
|
|
|
|
engine: Arc<Box<Engine>>,
|
2016-01-13 23:15:44 +01:00
|
|
|
message_channel: IoChannel<NetSyncMessage>
|
2016-01-11 13:42:32 +01:00
|
|
|
}
|
2016-01-09 10:16:35 +01:00
|
|
|
|
|
|
|
impl BlockQueue {
|
2016-01-10 23:37:09 +01:00
|
|
|
/// Creates a new queue instance.
|
2016-01-13 23:15:44 +01:00
|
|
|
pub fn new(bc: Arc<RwLock<BlockChain>>, engine: Arc<Box<Engine>>, message_channel: IoChannel<NetSyncMessage>) -> BlockQueue {
|
2016-01-11 13:42:32 +01:00
|
|
|
BlockQueue {
|
|
|
|
bc: bc,
|
|
|
|
engine: engine,
|
2016-01-13 23:15:44 +01:00
|
|
|
message_channel: message_channel
|
2016-01-11 13:42:32 +01:00
|
|
|
}
|
2016-01-09 10:16:35 +01:00
|
|
|
}
|
|
|
|
|
2016-01-10 23:37:09 +01:00
|
|
|
/// Clear the queue and stop verification activity.
|
2016-01-09 10:16:35 +01:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
}
|
|
|
|
|
2016-01-10 23:37:09 +01:00
|
|
|
/// Add a block to the queue.
|
2016-01-11 13:42:32 +01:00
|
|
|
pub fn import_block(&mut self, bytes: &[u8]) -> ImportResult {
|
|
|
|
let header = BlockView::new(bytes).header();
|
|
|
|
if self.bc.read().unwrap().is_known(&header.hash()) {
|
|
|
|
return Err(ImportError::AlreadyInChain);
|
2016-01-09 10:16:35 +01:00
|
|
|
}
|
2016-01-11 13:42:32 +01:00
|
|
|
try!(verify_block_basic(bytes, self.engine.deref().deref()));
|
|
|
|
try!(verify_block_unordered(bytes, self.engine.deref().deref()));
|
|
|
|
try!(verify_block_final(bytes, self.engine.deref().deref(), self.bc.read().unwrap().deref()));
|
2016-01-13 23:15:44 +01:00
|
|
|
try!(self.message_channel.send(UserMessage(SyncMessage::BlockVerified(bytes.to_vec()))).map_err(|e| Error::from(e)));
|
2016-01-11 13:42:32 +01:00
|
|
|
Ok(())
|
2016-01-09 10:16:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|