Merge pull request #58 from ethcore/inventory_type

intentory_type, fixed #39
This commit is contained in:
Svyatoslav Nikolsky 2016-10-30 21:11:00 +00:00 committed by GitHub
commit 1695880529
3 changed files with 19 additions and 12 deletions

View File

@ -30,9 +30,22 @@ impl From<InventoryType> for u32 {
}
}
impl Serializable for InventoryType {
fn serialize(&self, stream: &mut Stream) {
stream.append(&u32::from(*self));
}
}
impl Deserializable for InventoryType {
fn deserialize(reader: &mut Reader) -> Result<Self, ReaderError> where Self: Sized {
let t: u32 = try!(reader.read());
InventoryType::from_u32(t).ok_or(ReaderError::MalformedData)
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct InventoryVector {
pub inv_type: u32, // TODO: change to InventoryType as discussed in #37
pub inv_type: InventoryType,
pub hash: H256,
}
@ -55,12 +68,6 @@ impl Deserializable for InventoryVector {
}
}
impl InventoryVector {
pub fn inventory_type(&self) -> Option<InventoryType> {
InventoryType::from_u32(self.inv_type)
}
}
#[cfg(test)]
mod tests {
use bytes::Bytes;
@ -72,7 +79,7 @@ mod tests {
let expected = "020000000000000000000000000000000000000000000000000000000000000000000004".into();
let inventory = InventoryVector {
inv_type: 2,
inv_type: InventoryType::MessageBlock,
hash: 4u8.into(),
};
@ -84,7 +91,7 @@ mod tests {
let raw: Bytes = "020000000000000000000000000000000000000000000000000000000000000000000004".into();
let expected = InventoryVector {
inv_type: 2,
inv_type: InventoryType::MessageBlock,
hash: 4u8.into(),
};

View File

@ -64,7 +64,7 @@ impl LocalNode {
// request inventory from peer
self.executor.lock().execute(SynchronizationTask::RequestInventory(peer_index));
}
}
pub fn on_peer_inventory(&self, peer_index: usize, message: types::Inv) {
trace!(target: "sync", "Got `inventory` message from peer#{}. Inventory len: {}", peer_index, message.inventory.len());
@ -78,7 +78,7 @@ impl LocalNode {
let unknown_blocks: Vec<_> = {
let chain = self.chain.read();
message.inventory.iter()
.filter(|item| InventoryType::from_u32(item.inv_type) == Some(InventoryType::MessageBlock))
.filter(|item| item.inv_type == InventoryType::MessageBlock)
.filter(|item| chain.block_state(&item.hash) == BlockState::Unknown)
.map(|item| item.hash.clone())
.collect()

View File

@ -40,7 +40,7 @@ impl SynchronizationTaskExecutor for LocalSynchronizationTaskExecutor {
let getdata = types::GetData {
inventory: blocks_hashes.into_iter()
.map(|hash| InventoryVector {
inv_type: InventoryType::MessageBlock.into(),
inv_type: InventoryType::MessageBlock,
hash: hash,
}).collect()
};