Abstract the common case of a message with a Vec<InventoryHash>
This commit is contained in:
parent
9699ef2fa1
commit
7632d5b8cc
|
|
@ -17,7 +17,7 @@ use zebra_chain::{
|
||||||
|
|
||||||
use crate::{constants, Network};
|
use crate::{constants, Network};
|
||||||
|
|
||||||
use super::{message::Message, types::*};
|
use super::{inv::InventoryHash, message::Message, types::*};
|
||||||
|
|
||||||
/// The length of a Bitcoin message header.
|
/// The length of a Bitcoin message header.
|
||||||
const HEADER_LEN: usize = 24usize;
|
const HEADER_LEN: usize = 24usize;
|
||||||
|
|
@ -452,9 +452,10 @@ impl Codec {
|
||||||
bail!("unimplemented message type")
|
bail!("unimplemented message type")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_inv<R: Read>(&self, mut reader: R) -> Result<Message, Error> {
|
fn _read_generic_inventory_hash_vector<R: Read>(
|
||||||
use super::inv::InventoryHash;
|
&self,
|
||||||
|
mut reader: R,
|
||||||
|
) -> Result<Vec<InventoryHash>, Error> {
|
||||||
let count = reader.read_compactsize()? as usize;
|
let count = reader.read_compactsize()? as usize;
|
||||||
// Preallocate a buffer, performing a single allocation in the honest
|
// Preallocate a buffer, performing a single allocation in the honest
|
||||||
// case. Although the size of the recieved data buffer is bounded by the
|
// case. Although the size of the recieved data buffer is bounded by the
|
||||||
|
|
@ -473,17 +474,22 @@ impl Codec {
|
||||||
hashes.push(InventoryHash::zcash_deserialize(&mut reader)?);
|
hashes.push(InventoryHash::zcash_deserialize(&mut reader)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(hashes);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_inv<R: Read>(&self, reader: R) -> Result<Message, Error> {
|
||||||
|
let hashes = self._read_generic_inventory_hash_vector(reader)?;
|
||||||
Ok(Message::Inv(hashes))
|
Ok(Message::Inv(hashes))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_getdata<R: Read>(&self, mut _reader: R) -> Result<Message, Error> {
|
fn read_getdata<R: Read>(&self, reader: R) -> Result<Message, Error> {
|
||||||
trace!("getdata");
|
let hashes = self._read_generic_inventory_hash_vector(reader)?;
|
||||||
bail!("unimplemented message type")
|
Ok(Message::GetData(hashes))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_notfound<R: Read>(&self, mut _reader: R) -> Result<Message, Error> {
|
fn read_notfound<R: Read>(&self, reader: R) -> Result<Message, Error> {
|
||||||
trace!("notfound");
|
let hashes = self._read_generic_inventory_hash_vector(reader)?;
|
||||||
bail!("unimplemented message type")
|
Ok(Message::GetData(hashes))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_tx<R: Read>(&self, mut reader: R) -> Result<Message, Error> {
|
fn read_tx<R: Read>(&self, mut reader: R) -> Result<Message, Error> {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue