2016-01-09 10:16:35 +01:00
|
|
|
use std::sync::Arc;
|
2016-01-10 23:37:09 +01:00
|
|
|
use util::*;
|
2016-01-09 10:16:35 +01:00
|
|
|
use blockchain::BlockChain;
|
|
|
|
use client::{QueueStatus, ImportResult};
|
|
|
|
use views::{BlockView};
|
|
|
|
|
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;
|
2016-01-09 10:16:35 +01:00
|
|
|
|
|
|
|
impl BlockQueue {
|
2016-01-10 23:37:09 +01:00
|
|
|
/// Creates a new queue instance.
|
|
|
|
pub fn new() -> BlockQueue {
|
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], bc: &mut BlockChain) -> ImportResult {
|
2016-01-09 10:16:35 +01:00
|
|
|
//TODO: verify block
|
|
|
|
{
|
|
|
|
let block = BlockView::new(bytes);
|
|
|
|
let header = block.header_view();
|
|
|
|
let hash = header.sha3();
|
|
|
|
if self.chain.is_known(&hash) {
|
|
|
|
return ImportResult::Bad;
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 23:37:09 +01:00
|
|
|
bc.insert_block(bytes);
|
2016-01-09 10:16:35 +01:00
|
|
|
ImportResult::Queued(QueueStatus::Known)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|