Rename Requests to Batch

This commit is contained in:
Lorenzo Manacorda 2017-09-24 19:18:17 +02:00
parent 684b142711
commit 5f025cc1b8
No known key found for this signature in database
GPG Key ID: CF2797A94C182810
7 changed files with 39 additions and 39 deletions

View File

@ -899,7 +899,7 @@ impl LightProtocol {
// the maximum amount of requests we'll fill in a single packet. // the maximum amount of requests we'll fill in a single packet.
const MAX_REQUESTS: usize = 256; const MAX_REQUESTS: usize = 256;
use ::request::RequestBuilder; use ::request::Builder;
use ::request::CompleteRequest; use ::request::CompleteRequest;
let peers = self.peers.read(); let peers = self.peers.read();
@ -914,7 +914,7 @@ impl LightProtocol {
let peer: &mut Peer = &mut *peer; let peer: &mut Peer = &mut *peer;
let req_id: u64 = raw.val_at(0)?; let req_id: u64 = raw.val_at(0)?;
let mut request_builder = RequestBuilder::default(); let mut request_builder = Builder::default();
trace!(target: "pip", "Received requests (id: {}) from peer {}", req_id, peer_id); trace!(target: "pip", "Received requests (id: {}) from peer {}", req_id, peer_id);

View File

@ -147,7 +147,7 @@ fn compute_timeout(reqs: &Requests) -> Duration {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use net::ReqId; use net::ReqId;
use request::RequestBuilder; use request::Builder;
use time::{SteadyTime, Duration}; use time::{SteadyTime, Duration};
use super::{RequestSet, compute_timeout}; use super::{RequestSet, compute_timeout};
@ -156,7 +156,7 @@ mod tests {
let test_begin = SteadyTime::now(); let test_begin = SteadyTime::now();
let mut req_set = RequestSet::default(); let mut req_set = RequestSet::default();
let the_req = RequestBuilder::default().build(); let the_req = Builder::default().build();
let req_time = compute_timeout(&the_req); let req_time = compute_timeout(&the_req);
req_set.insert(ReqId(0), the_req.clone(), 0.into(), test_begin); req_set.insert(ReqId(0), the_req.clone(), 0.into(), test_begin);
req_set.insert(ReqId(1), the_req, 0.into(), test_begin + Duration::seconds(1)); req_set.insert(ReqId(1), the_req, 0.into(), test_begin + Duration::seconds(1));
@ -173,7 +173,7 @@ mod tests {
#[test] #[test]
fn cumulative_cost() { fn cumulative_cost() {
let the_req = RequestBuilder::default().build(); let the_req = Builder::default().build();
let test_begin = SteadyTime::now(); let test_begin = SteadyTime::now();
let test_end = test_begin + Duration::seconds(1); let test_end = test_begin + Duration::seconds(1);
let mut req_set = RequestSet::default(); let mut req_set = RequestSet::default();

View File

@ -41,7 +41,7 @@ use std::sync::Arc;
// helper for encoding a single request into a packet. // helper for encoding a single request into a packet.
// panics on bad backreference. // panics on bad backreference.
fn encode_single(request: Request) -> NetworkRequests { fn encode_single(request: Request) -> NetworkRequests {
let mut builder = RequestBuilder::default(); let mut builder = Builder::default();
builder.push(request).unwrap(); builder.push(request).unwrap();
builder.build() builder.build()
} }
@ -344,7 +344,7 @@ fn get_block_bodies() {
proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &my_status); proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &my_status);
} }
let mut builder = RequestBuilder::default(); let mut builder = Builder::default();
let mut bodies = Vec::new(); let mut bodies = Vec::new();
for i in 0..10 { for i in 0..10 {
@ -400,7 +400,7 @@ fn get_block_receipts() {
.take(10) .take(10)
.collect(); .collect();
let mut builder = RequestBuilder::default(); let mut builder = Builder::default();
let mut receipts = Vec::new(); let mut receipts = Vec::new();
for hash in block_hashes.iter().cloned() { for hash in block_hashes.iter().cloned() {
builder.push(Request::Receipts(IncompleteReceiptsRequest { hash: hash.into() })).unwrap(); builder.push(Request::Receipts(IncompleteReceiptsRequest { hash: hash.into() })).unwrap();
@ -448,7 +448,7 @@ fn get_state_proofs() {
let key1: H256 = U256::from(11223344).into(); let key1: H256 = U256::from(11223344).into();
let key2: H256 = U256::from(99988887).into(); let key2: H256 = U256::from(99988887).into();
let mut builder = RequestBuilder::default(); let mut builder = Builder::default();
builder.push(Request::Account(IncompleteAccountRequest { builder.push(Request::Account(IncompleteAccountRequest {
block_hash: H256::default().into(), block_hash: H256::default().into(),
address_hash: key1.into(), address_hash: key1.into(),

View File

@ -74,8 +74,8 @@ impl Peer {
// Attempted request info and sender to put received value. // Attempted request info and sender to put received value.
struct Pending { struct Pending {
requests: basic_request::Requests<CheckedRequest>, requests: basic_request::Batch<CheckedRequest>,
net_requests: basic_request::Requests<NetworkRequest>, net_requests: basic_request::Batch<NetworkRequest>,
required_capabilities: Capabilities, required_capabilities: Capabilities,
responses: Vec<Response>, responses: Vec<Response>,
sender: oneshot::Sender<Vec<Response>>, sender: oneshot::Sender<Vec<Response>>,
@ -151,7 +151,7 @@ impl Pending {
fn update_net_requests(&mut self) { fn update_net_requests(&mut self) {
use request::IncompleteRequest; use request::IncompleteRequest;
let mut builder = basic_request::RequestBuilder::default(); let mut builder = basic_request::Builder::default();
let num_answered = self.requests.num_answered(); let num_answered = self.requests.num_answered();
let mut mapping = move |idx| idx - num_answered; let mut mapping = move |idx| idx - num_answered;
@ -281,7 +281,7 @@ impl OnDemand {
return Ok(receiver); return Ok(receiver);
} }
let mut builder = basic_request::RequestBuilder::default(); let mut builder = basic_request::Builder::default();
let responses = Vec::with_capacity(requests.len()); let responses = Vec::with_capacity(requests.len());

View File

@ -25,23 +25,23 @@ use request::{
}; };
/// Build chained requests. Push them onto the series with `push`, /// Build chained requests. Push them onto the series with `push`,
/// and produce a `Requests` object with `build`. Outputs are checked for consistency. /// and produce a `Batch` object with `build`. Outputs are checked for consistency.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct RequestBuilder<T> { pub struct Builder<T> {
output_kinds: HashMap<(usize, usize), OutputKind>, output_kinds: HashMap<(usize, usize), OutputKind>,
requests: Vec<T>, requests: Vec<T>,
} }
impl<T> Default for RequestBuilder<T> { impl<T> Default for Builder<T> {
fn default() -> Self { fn default() -> Self {
RequestBuilder { Builder {
output_kinds: HashMap::new(), output_kinds: HashMap::new(),
requests: Vec::new(), requests: Vec::new(),
} }
} }
} }
impl<T: IncompleteRequest> RequestBuilder<T> { impl<T: IncompleteRequest> Builder<T> {
/// Attempt to push a request onto the request chain. Fails if the request /// Attempt to push a request onto the request chain. Fails if the request
/// references a non-existent output of a prior request. /// references a non-existent output of a prior request.
pub fn push(&mut self, request: T) -> Result<(), NoSuchOutput> { pub fn push(&mut self, request: T) -> Result<(), NoSuchOutput> {
@ -62,9 +62,9 @@ impl<T: IncompleteRequest> RequestBuilder<T> {
&self.output_kinds &self.output_kinds
} }
/// Convert this into a "requests" object. /// Convert this into a "batch" object.
pub fn build(self) -> Requests<T> { pub fn build(self) -> Batch<T> {
Requests { Batch {
outputs: HashMap::new(), outputs: HashMap::new(),
requests: self.requests, requests: self.requests,
answered: 0, answered: 0,
@ -74,13 +74,13 @@ impl<T: IncompleteRequest> RequestBuilder<T> {
/// Requests pending responses. /// Requests pending responses.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Requests<T> { pub struct Batch<T> {
outputs: HashMap<(usize, usize), Output>, outputs: HashMap<(usize, usize), Output>,
requests: Vec<T>, requests: Vec<T>,
answered: usize, answered: usize,
} }
impl<T> Requests<T> { impl<T> Batch<T> {
/// Get access to the underlying slice of requests. /// Get access to the underlying slice of requests.
// TODO: unimplemented -> Vec<Request>, // do we _have to_ allocate? // TODO: unimplemented -> Vec<Request>, // do we _have to_ allocate?
pub fn requests(&self) -> &[T] { &self.requests } pub fn requests(&self) -> &[T] { &self.requests }
@ -94,10 +94,10 @@ impl<T> Requests<T> {
} }
/// Map requests from one type into another. /// Map requests from one type into another.
pub fn map_requests<F, U>(self, f: F) -> Requests<U> pub fn map_requests<F, U>(self, f: F) -> Batch<U>
where F: FnMut(T) -> U, U: IncompleteRequest where F: FnMut(T) -> U, U: IncompleteRequest
{ {
Requests { Batch {
outputs: self.outputs, outputs: self.outputs,
requests: self.requests.into_iter().map(f).collect(), requests: self.requests.into_iter().map(f).collect(),
answered: self.answered, answered: self.answered,
@ -105,7 +105,7 @@ impl<T> Requests<T> {
} }
} }
impl<T: IncompleteRequest + Clone> Requests<T> { impl<T: IncompleteRequest + Clone> Batch<T> {
/// Get the next request as a filled request. Returns `None` when all requests answered. /// Get the next request as a filled request. Returns `None` when all requests answered.
pub fn next_complete(&self) -> Option<T::Complete> { pub fn next_complete(&self) -> Option<T::Complete> {
if self.is_complete() { if self.is_complete() {
@ -113,7 +113,7 @@ impl<T: IncompleteRequest + Clone> Requests<T> {
} else { } else {
Some(self.requests[self.answered].clone() Some(self.requests[self.answered].clone()
.complete() .complete()
.expect("All outputs checked as invariant of `Requests` object; qed")) .expect("All outputs checked as invariant of `Batch` object; qed"))
} }
} }
@ -149,7 +149,7 @@ impl<T: IncompleteRequest + Clone> Requests<T> {
} }
} }
impl<T: super::CheckedRequest + Clone> Requests<T> { impl<T: super::CheckedRequest + Clone> Batch<T> {
/// Supply a response for the next request. /// Supply a response for the next request.
/// Fails on: wrong request kind, all requests answered already. /// Fails on: wrong request kind, all requests answered already.
pub fn supply_response(&mut self, env: &T::Environment, response: &T::Response) pub fn supply_response(&mut self, env: &T::Environment, response: &T::Response)
@ -170,7 +170,7 @@ impl<T: super::CheckedRequest + Clone> Requests<T> {
} }
} }
impl Requests<super::Request> { impl Batch<super::Request> {
/// For each request, produce a response. /// For each request, produce a response.
/// The responses vector produced goes up to the point where the responder /// The responses vector produced goes up to the point where the responder
/// first returns `None`, an invalid response, or until all requests have been responded to. /// first returns `None`, an invalid response, or until all requests have been responded to.
@ -193,7 +193,7 @@ impl Requests<super::Request> {
} }
} }
impl<T: IncompleteRequest> Deref for Requests<T> { impl<T: IncompleteRequest> Deref for Batch<T> {
type Target = [T]; type Target = [T];
fn deref(&self) -> &[T] { fn deref(&self) -> &[T] {
@ -201,7 +201,7 @@ impl<T: IncompleteRequest> Deref for Requests<T> {
} }
} }
impl<T: IncompleteRequest> DerefMut for Requests<T> { impl<T: IncompleteRequest> DerefMut for Batch<T> {
fn deref_mut(&mut self) -> &mut [T] { fn deref_mut(&mut self) -> &mut [T] {
&mut self.requests[..] &mut self.requests[..]
} }
@ -210,12 +210,12 @@ impl<T: IncompleteRequest> DerefMut for Requests<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use request::*; use request::*;
use super::RequestBuilder; use super::Builder;
use bigint::hash::H256; use bigint::hash::H256;
#[test] #[test]
fn all_scalar() { fn all_scalar() {
let mut builder = RequestBuilder::default(); let mut builder = Builder::default();
builder.push(Request::HeaderProof(IncompleteHeaderProofRequest { builder.push(Request::HeaderProof(IncompleteHeaderProofRequest {
num: 100.into(), num: 100.into(),
})).unwrap(); })).unwrap();
@ -227,7 +227,7 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn missing_backref() { fn missing_backref() {
let mut builder = RequestBuilder::default(); let mut builder = Builder::default();
builder.push(Request::HeaderProof(IncompleteHeaderProofRequest { builder.push(Request::HeaderProof(IncompleteHeaderProofRequest {
num: Field::BackReference(100, 3), num: Field::BackReference(100, 3),
})).unwrap(); })).unwrap();
@ -236,7 +236,7 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn wrong_kind() { fn wrong_kind() {
let mut builder = RequestBuilder::default(); let mut builder = Builder::default();
assert!(builder.push(Request::HeaderProof(IncompleteHeaderProofRequest { assert!(builder.push(Request::HeaderProof(IncompleteHeaderProofRequest {
num: 100.into(), num: 100.into(),
})).is_ok()); })).is_ok());
@ -247,7 +247,7 @@ mod tests {
#[test] #[test]
fn good_backreference() { fn good_backreference() {
let mut builder = RequestBuilder::default(); let mut builder = Builder::default();
builder.push(Request::HeaderProof(IncompleteHeaderProofRequest { builder.push(Request::HeaderProof(IncompleteHeaderProofRequest {
num: 100.into(), // header proof puts hash at output 0. num: 100.into(), // header proof puts hash at output 0.
})).unwrap(); })).unwrap();

View File

@ -73,7 +73,7 @@ pub use self::epoch_signal::{
Response as SignalResponse, Response as SignalResponse,
}; };
pub use self::builder::{RequestBuilder, Requests}; pub use self::builder::{Builder, Requests};
/// Error indicating a reference to a non-existent or wrongly-typed output. /// Error indicating a reference to a non-existent or wrongly-typed output.
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -241,7 +241,7 @@ impl Encodable for HashOrNumber {
} }
/// Type alias for "network requests". /// Type alias for "network requests".
pub type NetworkRequests = Requests<Request>; pub type NetworkRequests = Batch<Request>;
/// All request types, as they're sent over the network. /// All request types, as they're sent over the network.
/// They may be incomplete, with back-references to outputs /// They may be incomplete, with back-references to outputs

View File

@ -522,7 +522,7 @@ impl<L: AsLightClient> LightSync<L> {
rng.shuffle(&mut peer_ids); rng.shuffle(&mut peer_ids);
let request = { let request = {
let mut builder = request::RequestBuilder::default(); let mut builder = request::Builder::default();
builder.push(request::Request::Headers(request::IncompleteHeadersRequest { builder.push(request::Request::Headers(request::IncompleteHeadersRequest {
start: req.start.into(), start: req.start.into(),
skip: req.skip, skip: req.skip,