Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Rename Requests to Batch #6582

Merged
merged 2 commits into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ethcore/light/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions ethcore/light/src/net/request_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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));
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions ethcore/light/src/net/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions ethcore/light/src/on_demand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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,
Expand All @@ -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 }
Expand All @@ -94,26 +94,26 @@ 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,
}
}
}

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() {
None
} 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"))
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -193,15 +193,15 @@ 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] {
&self.requests[..]
}
}

impl<T: IncompleteRequest> DerefMut for Requests<T> {
impl<T: IncompleteRequest> DerefMut for Batch<T> {
fn deref_mut(&mut self) -> &mut [T] {
&mut self.requests[..]
}
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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());
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions ethcore/light/src/types/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sync/src/light_sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down