network: clean up GetHeaders, GetBlocks modeling
This commit is contained in:
parent
3c993f33b1
commit
b289cb9164
|
|
@ -433,10 +433,7 @@ where
|
||||||
}),
|
}),
|
||||||
(AwaitingRequest, FindBlocks { known_blocks, stop }) => self
|
(AwaitingRequest, FindBlocks { known_blocks, stop }) => self
|
||||||
.peer_tx
|
.peer_tx
|
||||||
.send(Message::GetBlocks {
|
.send(Message::GetBlocks { known_blocks, stop })
|
||||||
block_locator_hashes: known_blocks,
|
|
||||||
hash_stop: stop.unwrap_or(block::Hash([0; 32])),
|
|
||||||
})
|
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.into())
|
.map_err(|e| e.into())
|
||||||
.map(|()| AwaitingResponse {
|
.map(|()| AwaitingResponse {
|
||||||
|
|
|
||||||
|
|
@ -227,21 +227,17 @@ impl Codec {
|
||||||
Message::Addr(addrs) => addrs.zcash_serialize(&mut writer)?,
|
Message::Addr(addrs) => addrs.zcash_serialize(&mut writer)?,
|
||||||
Message::GetAddr => { /* Empty payload -- no-op */ }
|
Message::GetAddr => { /* Empty payload -- no-op */ }
|
||||||
Message::Block(block) => block.zcash_serialize(&mut writer)?,
|
Message::Block(block) => block.zcash_serialize(&mut writer)?,
|
||||||
Message::GetBlocks {
|
Message::GetBlocks { known_blocks, stop } => {
|
||||||
block_locator_hashes,
|
|
||||||
hash_stop,
|
|
||||||
} => {
|
|
||||||
writer.write_u32::<LittleEndian>(self.builder.version.0)?;
|
writer.write_u32::<LittleEndian>(self.builder.version.0)?;
|
||||||
block_locator_hashes.zcash_serialize(&mut writer)?;
|
known_blocks.zcash_serialize(&mut writer)?;
|
||||||
hash_stop.zcash_serialize(&mut writer)?;
|
stop.unwrap_or(block::Hash([0; 32]))
|
||||||
|
.zcash_serialize(&mut writer)?;
|
||||||
}
|
}
|
||||||
Message::GetHeaders {
|
Message::GetHeaders { known_blocks, stop } => {
|
||||||
block_locator_hashes,
|
|
||||||
hash_stop,
|
|
||||||
} => {
|
|
||||||
writer.write_u32::<LittleEndian>(self.builder.version.0)?;
|
writer.write_u32::<LittleEndian>(self.builder.version.0)?;
|
||||||
block_locator_hashes.zcash_serialize(&mut writer)?;
|
known_blocks.zcash_serialize(&mut writer)?;
|
||||||
hash_stop.zcash_serialize(&mut writer)?;
|
stop.unwrap_or(block::Hash([0; 32]))
|
||||||
|
.zcash_serialize(&mut writer)?;
|
||||||
}
|
}
|
||||||
Message::Headers(headers) => headers.zcash_serialize(&mut writer)?,
|
Message::Headers(headers) => headers.zcash_serialize(&mut writer)?,
|
||||||
Message::Inv(hashes) => hashes.zcash_serialize(&mut writer)?,
|
Message::Inv(hashes) => hashes.zcash_serialize(&mut writer)?,
|
||||||
|
|
@ -483,10 +479,14 @@ impl Codec {
|
||||||
|
|
||||||
fn read_getblocks<R: Read>(&self, mut reader: R) -> Result<Message, Error> {
|
fn read_getblocks<R: Read>(&self, mut reader: R) -> Result<Message, Error> {
|
||||||
if self.builder.version == Version(reader.read_u32::<LittleEndian>()?) {
|
if self.builder.version == Version(reader.read_u32::<LittleEndian>()?) {
|
||||||
Ok(Message::GetBlocks {
|
let known_blocks = Vec::zcash_deserialize(&mut reader)?;
|
||||||
block_locator_hashes: Vec::zcash_deserialize(&mut reader)?,
|
let stop_hash = block::Hash::zcash_deserialize(&mut reader)?;
|
||||||
hash_stop: block::Hash::zcash_deserialize(&mut reader)?,
|
let stop = if stop_hash != block::Hash([0; 32]) {
|
||||||
})
|
Some(stop_hash)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
Ok(Message::GetBlocks { known_blocks, stop })
|
||||||
} else {
|
} else {
|
||||||
Err(Error::Parse("getblocks version did not match negotiation"))
|
Err(Error::Parse("getblocks version did not match negotiation"))
|
||||||
}
|
}
|
||||||
|
|
@ -503,10 +503,14 @@ impl Codec {
|
||||||
|
|
||||||
fn read_getheaders<R: Read>(&self, mut reader: R) -> Result<Message, Error> {
|
fn read_getheaders<R: Read>(&self, mut reader: R) -> Result<Message, Error> {
|
||||||
if self.builder.version == Version(reader.read_u32::<LittleEndian>()?) {
|
if self.builder.version == Version(reader.read_u32::<LittleEndian>()?) {
|
||||||
Ok(Message::GetHeaders {
|
let known_blocks = Vec::zcash_deserialize(&mut reader)?;
|
||||||
block_locator_hashes: Vec::zcash_deserialize(&mut reader)?,
|
let stop_hash = block::Hash::zcash_deserialize(&mut reader)?;
|
||||||
hash_stop: block::Hash::zcash_deserialize(&mut reader)?,
|
let stop = if stop_hash != block::Hash([0; 32]) {
|
||||||
})
|
Some(stop_hash)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
Ok(Message::GetHeaders { known_blocks, stop })
|
||||||
} else {
|
} else {
|
||||||
Err(Error::Parse("getblocks version did not match negotiation"))
|
Err(Error::Parse("getblocks version did not match negotiation"))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -138,37 +138,21 @@ pub enum Message {
|
||||||
|
|
||||||
/// A `getblocks` message.
|
/// A `getblocks` message.
|
||||||
///
|
///
|
||||||
/// Requests the list of blocks starting right after the last
|
/// `known_blocks` is a series of known block hashes spaced out along the
|
||||||
/// known hash in `block_locator_hashes`, up to `hash_stop` or 500
|
/// peer's best chain. The remote peer uses them to compute the intersection
|
||||||
/// blocks, whichever comes first.
|
/// of its best chain and determine the blocks following the intersection
|
||||||
|
/// point.
|
||||||
///
|
///
|
||||||
/// You can send in fewer known hashes down to a minimum of just
|
/// The peer responds with an `inv` packet with the hashes of subsequent blocks.
|
||||||
/// one hash. However, the purpose of the block locator object is
|
/// If supplied, the `stop` parameter specifies the last header to request.
|
||||||
/// to detect a wrong branch in the caller's main chain. If the
|
/// Otherwise, an inv packet with the maximum number (500) are sent.
|
||||||
/// peer detects that you are off the main chain, it will send in
|
|
||||||
/// block hashes which are earlier than your last known block. So
|
|
||||||
/// if you just send in your last known hash and it is off the
|
|
||||||
/// main chain, the peer starts over at block #1.
|
|
||||||
///
|
///
|
||||||
/// [Bitcoin reference](https://en.bitcoin.it/wiki/Protocol_documentation#getblocks)
|
/// [Bitcoin reference](https://en.bitcoin.it/wiki/Protocol_documentation#getheaders)
|
||||||
// The locator hashes are processed by a node in the order as they
|
|
||||||
// appear in the message. If a block hash is found in the node's
|
|
||||||
// main chain, the list of its children is returned back via the
|
|
||||||
// inv message and the remaining locators are ignored, no matter
|
|
||||||
// if the requested limit was reached, or not.
|
|
||||||
//
|
|
||||||
// The 500 headers number is from the Bitcoin docs, we are not
|
|
||||||
// certain (yet) that other implementations of Zcash obey this
|
|
||||||
// restriction, or if they don't, what happens if we send them too
|
|
||||||
// many results.
|
|
||||||
GetBlocks {
|
GetBlocks {
|
||||||
/// Block locators, from newest back to genesis block.
|
/// Hashes of known blocks, ordered from highest height to lowest height.
|
||||||
block_locator_hashes: Vec<block::Hash>,
|
known_blocks: Vec<block::Hash>,
|
||||||
|
/// Optionally, the last header to request.
|
||||||
/// `block::Hash` of the last desired block.
|
stop: Option<block::Hash>,
|
||||||
///
|
|
||||||
/// Set to zero to get as many blocks as possible (500).
|
|
||||||
hash_stop: block::Hash,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/// A `headers` message.
|
/// A `headers` message.
|
||||||
|
|
@ -184,31 +168,21 @@ pub enum Message {
|
||||||
|
|
||||||
/// A `getheaders` message.
|
/// A `getheaders` message.
|
||||||
///
|
///
|
||||||
/// Requests a series of block headers starting right after the
|
/// `known_blocks` is a series of known block hashes spaced out along the
|
||||||
/// last known hash in `block_locator_hashes`, up to `hash_stop`
|
/// peer's best chain. The remote peer uses them to compute the intersection
|
||||||
/// or 2000 blocks, whichever comes first.
|
/// of its best chain and determine the blocks following the intersection
|
||||||
|
/// point.
|
||||||
///
|
///
|
||||||
/// You can send in fewer known hashes down to a minimum of just
|
/// The peer responds with an `headers` packet with the hashes of subsequent blocks.
|
||||||
/// one hash. However, the purpose of the block locator object is
|
/// If supplied, the `stop` parameter specifies the last header to request.
|
||||||
/// to detect a wrong branch in the caller's main chain. If the
|
/// Otherwise, the maximum number of block headers (160) are sent.
|
||||||
/// peer detects that you are off the main chain, it will send in
|
|
||||||
/// block hashes which are earlier than your last known block. So
|
|
||||||
/// if you just send in your last known hash and it is off the
|
|
||||||
/// main chain, the peer starts over at block #1.
|
|
||||||
///
|
///
|
||||||
/// [Bitcoin reference](https://en.bitcoin.it/wiki/Protocol_documentation#getheaders)
|
/// [Bitcoin reference](https://en.bitcoin.it/wiki/Protocol_documentation#getheaders)
|
||||||
// The 2000 headers number is from the Bitcoin docs, we are not
|
|
||||||
// certain (yet) that other implementations of Zcash obey this
|
|
||||||
// restriction, or if they don't, what happens if we send them too
|
|
||||||
// many results.
|
|
||||||
GetHeaders {
|
GetHeaders {
|
||||||
/// Block locators, from newest back to genesis block.
|
/// Hashes of known blocks, ordered from highest height to lowest height.
|
||||||
block_locator_hashes: Vec<block::Hash>,
|
known_blocks: Vec<block::Hash>,
|
||||||
|
/// Optionally, the last header to request.
|
||||||
/// `block::Hash` of the last desired block header.
|
stop: Option<block::Hash>,
|
||||||
///
|
|
||||||
/// Set to zero to get as many block headers as possible (2000).
|
|
||||||
hash_stop: block::Hash,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/// An `inv` message.
|
/// An `inv` message.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue