openethereum/src/queue.rs

60 lines
1.7 KiB
Rust
Raw Normal View History

2016-01-10 23:37:09 +01:00
use util::*;
use verification::*;
use error::*;
use engine::Engine;
2016-01-13 23:15:44 +01:00
use sync::*;
2016-01-14 19:03:48 +01:00
use views::*;
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.
pub struct BlockQueue {
engine: Arc<Box<Engine>>,
2016-01-15 12:26:04 +01:00
message_channel: IoChannel<NetSyncMessage>,
bad: HashSet<H256>,
}
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-14 01:28:37 +01:00
pub fn new(engine: Arc<Box<Engine>>, message_channel: IoChannel<NetSyncMessage>) -> BlockQueue {
BlockQueue {
engine: engine,
2016-01-15 12:26:04 +01:00
message_channel: message_channel,
bad: HashSet::new(),
}
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.
pub fn import_block(&mut self, bytes: &[u8]) -> ImportResult {
2016-01-15 12:26:04 +01:00
let header = BlockView::new(bytes).header();
if self.bad.contains(&header.hash()) {
return Err(ImportError::Bad(None));
}
if self.bad.contains(&header.parent_hash) {
self.bad.insert(header.hash());
return Err(ImportError::Bad(None));
}
try!(verify_block_basic(&header, bytes, self.engine.deref().deref()).map_err(|e| {
2016-01-14 19:03:48 +01:00
warn!(target: "client", "Stage 1 block verification failed for {}\nError: {:?}", BlockView::new(&bytes).header_view().sha3(), e);
e
}));
2016-01-15 12:26:04 +01:00
try!(verify_block_unordered(&header, bytes, self.engine.deref().deref()).map_err(|e| {
2016-01-14 19:03:48 +01:00
warn!(target: "client", "Stage 2 block verification failed for {}\nError: {:?}", BlockView::new(&bytes).header_view().sha3(), e);
e
}));
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)));
Ok(())
2016-01-09 10:16:35 +01:00
}
2016-01-15 12:26:04 +01:00
pub fn mark_as_bad(&mut self, hash: &H256) {
self.bad.insert(hash.clone());
//TODO: walk the queue
}
2016-01-09 10:16:35 +01:00
}