Merge pull request #6582 from asymmetric/batch

Rename Requests to Batch
This commit is contained in:
Robert Habermeier 2017-09-25 15:26:18 +02:00 committed by GitHub
commit 70be064aa5
7 changed files with 40 additions and 40 deletions

View File

@ -899,7 +899,7 @@ impl LightProtocol {
// the maximum amount of requests we'll fill in a single packet.
const MAX_REQUESTS: usize = 256;
use ::request::RequestBuilder;
use ::request::Builder;
use ::request::CompleteRequest;
let peers = self.peers.read();
@ -914,7 +914,7 @@ impl LightProtocol {
let peer: &mut Peer = &mut *peer;
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);

View File

@ -147,7 +147,7 @@ fn compute_timeout(reqs: &Requests) -> Duration {
#[cfg(test)]
mod tests {
use net::ReqId;
use request::RequestBuilder;
use request::Builder;
use time::{SteadyTime, Duration};
use super::{RequestSet, compute_timeout};
@ -156,7 +156,7 @@ mod tests {
let test_begin = SteadyTime::now();
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);
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));
@ -173,7 +173,7 @@ mod tests {
#[test]
fn cumulative_cost() {
let the_req = RequestBuilder::default().build();
let the_req = Builder::default().build();
let test_begin = SteadyTime::now();
let test_end = test_begin + Duration::seconds(1);
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.
// panics on bad backreference.
fn encode_single(request: Request) -> NetworkRequests {
let mut builder = RequestBuilder::default();
let mut builder = Builder::default();
builder.push(request).unwrap();
builder.build()
}
@ -344,7 +344,7 @@ fn get_block_bodies() {
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();
for i in 0..10 {
@ -400,7 +400,7 @@ fn get_block_receipts() {
.take(10)
.collect();
let mut builder = RequestBuilder::default();
let mut builder = Builder::default();
let mut receipts = Vec::new();
for hash in block_hashes.iter().cloned() {
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 key2: H256 = U256::from(99988887).into();
let mut builder = RequestBuilder::default();
let mut builder = Builder::default();
builder.push(Request::Account(IncompleteAccountRequest {
block_hash: H256::default().into(),
address_hash: key1.into(),

View File

@ -74,8 +74,8 @@ impl Peer {
// Attempted request info and sender to put received value.
struct Pending {
requests: basic_request::Requests<CheckedRequest>,
net_requests: basic_request::Requests<NetworkRequest>,
requests: basic_request::Batch<CheckedRequest>,
net_requests: basic_request::Batch<NetworkRequest>,
required_capabilities: Capabilities,
responses: Vec<Response>,
sender: oneshot::Sender<Vec<Response>>,
@ -151,7 +151,7 @@ impl Pending {
fn update_net_requests(&mut self) {
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 mut mapping = move |idx| idx - num_answered;
@ -281,7 +281,7 @@ impl OnDemand {
return Ok(receiver);
}
let mut builder = basic_request::RequestBuilder::default();
let mut builder = basic_request::Builder::default();
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`,
/// 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)]
pub struct RequestBuilder<T> {
pub struct Builder<T> {
output_kinds: HashMap<(usize, usize), OutputKind>,
requests: Vec<T>,
}
impl<T> Default for RequestBuilder<T> {
impl<T> Default for Builder<T> {
fn default() -> Self {
RequestBuilder {
Builder {
output_kinds: HashMap::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
/// references a non-existent output of a prior request.
pub fn push(&mut self, request: T) -> Result<(), NoSuchOutput> {
@ -62,9 +62,9 @@ impl<T: IncompleteRequest> RequestBuilder<T> {
&self.output_kinds
}
/// Convert this into a "requests" object.
pub fn build(self) -> Requests<T> {
Requests {
/// Convert this into a "batch" object.
pub fn build(self) -> Batch<T> {
Batch {
outputs: HashMap::new(),
requests: self.requests,
answered: 0,
@ -74,13 +74,13 @@ impl<T: IncompleteRequest> RequestBuilder<T> {
/// Requests pending responses.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Requests<T> {
pub struct Batch<T> {
outputs: HashMap<(usize, usize), Output>,
requests: Vec<T>,
answered: usize,
}
impl<T> Requests<T> {
impl<T> Batch<T> {
/// Get access to the underlying slice of requests.
// TODO: unimplemented -> Vec<Request>, // do we _have to_ allocate?
pub fn requests(&self) -> &[T] { &self.requests }
@ -94,10 +94,10 @@ impl<T> Requests<T> {
}
/// 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
{
Requests {
Batch {
outputs: self.outputs,
requests: self.requests.into_iter().map(f).collect(),
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.
pub fn next_complete(&self) -> Option<T::Complete> {
if self.is_complete() {
@ -113,7 +113,7 @@ impl<T: IncompleteRequest + Clone> Requests<T> {
} else {
Some(self.requests[self.answered].clone()
.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.
/// Fails on: wrong request kind, all requests answered already.
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.
/// 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.
@ -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];
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] {
&mut self.requests[..]
}
@ -210,12 +210,12 @@ impl<T: IncompleteRequest> DerefMut for Requests<T> {
#[cfg(test)]
mod tests {
use request::*;
use super::RequestBuilder;
use super::Builder;
use bigint::hash::H256;
#[test]
fn all_scalar() {
let mut builder = RequestBuilder::default();
let mut builder = Builder::default();
builder.push(Request::HeaderProof(IncompleteHeaderProofRequest {
num: 100.into(),
})).unwrap();
@ -227,7 +227,7 @@ mod tests {
#[test]
#[should_panic]
fn missing_backref() {
let mut builder = RequestBuilder::default();
let mut builder = Builder::default();
builder.push(Request::HeaderProof(IncompleteHeaderProofRequest {
num: Field::BackReference(100, 3),
})).unwrap();
@ -236,7 +236,7 @@ mod tests {
#[test]
#[should_panic]
fn wrong_kind() {
let mut builder = RequestBuilder::default();
let mut builder = Builder::default();
assert!(builder.push(Request::HeaderProof(IncompleteHeaderProofRequest {
num: 100.into(),
})).is_ok());
@ -247,7 +247,7 @@ mod tests {
#[test]
fn good_backreference() {
let mut builder = RequestBuilder::default();
let mut builder = Builder::default();
builder.push(Request::HeaderProof(IncompleteHeaderProofRequest {
num: 100.into(), // header proof puts hash at output 0.
})).unwrap();

View File

@ -19,7 +19,7 @@
use rlp::{Encodable, Decodable, DecoderError, RlpStream, UntrustedRlp};
use bigint::hash::H256;
mod builder;
mod batch;
// re-exports of request types.
pub use self::header::{
@ -73,7 +73,7 @@ pub use self::epoch_signal::{
Response as SignalResponse,
};
pub use self::builder::{RequestBuilder, Requests};
pub use self::batch::{Batch, Builder};
/// Error indicating a reference to a non-existent or wrongly-typed output.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -241,7 +241,7 @@ impl Encodable for HashOrNumber {
}
/// 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.
/// 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);
let request = {
let mut builder = request::RequestBuilder::default();
let mut builder = request::Builder::default();
builder.push(request::Request::Headers(request::IncompleteHeadersRequest {
start: req.start.into(),
skip: req.skip,