fix BlockAttributes encoding (#6281)

This commit is contained in:
Svyatoslav Nikolsky
2020-06-16 23:51:45 +03:00
committed by GitHub
parent 7d30ae7ba8
commit 194b3dfb17
3 changed files with 74 additions and 24 deletions
@@ -27,9 +27,10 @@
use bytes::Bytes;
use codec::{self, Encode, Decode};
use crate::{
block_requests::build_protobuf_block_request,
chain::Client,
config::ProtocolId,
protocol::message::BlockAttributes,
protocol::message::{BlockAttributes, Direction, FromBlock},
schema,
};
use futures::{channel::oneshot, future::BoxFuture, prelude::*, stream::FuturesUnordered};
@@ -1062,13 +1063,13 @@ fn retries<B: Block>(request: &Request<B>) -> usize {
fn serialize_request<B: Block>(request: &Request<B>) -> Result<Vec<u8>, prost::EncodeError> {
let request = match request {
Request::Body { request, .. } => {
let rq = schema::v1::BlockRequest {
fields: u32::from(BlockAttributes::BODY.bits()),
from_block: Some(schema::v1::block_request::FromBlock::Hash(request.header.hash().encode())),
to_block: Vec::new(),
direction: schema::v1::Direction::Ascending as i32,
max_blocks: 1,
};
let rq = build_protobuf_block_request::<_, NumberFor<B>>(
BlockAttributes::BODY,
FromBlock::Hash(request.header.hash()),
None,
Direction::Ascending,
Some(1),
);
let mut buf = Vec::with_capacity(rq.encoded_len());
rq.encode(&mut buf)?;
return Ok(buf);
@@ -2036,4 +2037,22 @@ mod tests {
assert_eq!(vec![(100, 2)], task::block_on(chan.1).unwrap().unwrap());
// ^--- from `DummyFetchChecker::check_changes_proof`
}
#[test]
fn body_request_fields_encoded_properly() {
let (sender, _) = oneshot::channel();
let serialized_request = serialize_request::<Block>(&Request::Body {
request: RemoteBodyRequest {
header: dummy_header(),
retry_count: None,
},
sender,
}).unwrap();
let deserialized_request = schema::v1::BlockRequest::decode(&serialized_request[..]).unwrap();
assert!(
BlockAttributes::from_be_u32(deserialized_request.fields)
.unwrap()
.contains(BlockAttributes::BODY)
);
}
}