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

Commit

Permalink
Merge pull request #3527 from ethcore/client-provider
Browse files Browse the repository at this point in the history
LES Part 2
  • Loading branch information
rphmeier committed Dec 7, 2016
2 parents 97e60a6 + 5db93cd commit 085b8ad
Show file tree
Hide file tree
Showing 23 changed files with 962 additions and 142 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions ethcore/light/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ license = "GPL-3.0"
name = "ethcore-light"
version = "1.5.0"
authors = ["Ethcore <admin@ethcore.io>"]
build = "build.rs"

[build-dependencies]
"ethcore-ipc-codegen" = { path = "../../ipc/codegen" }

[dependencies]
log = "0.3"
ethcore = { path = ".." }
ethcore-util = { path = "../../util" }
ethcore-network = { path = "../../util/network" }
ethcore-io = { path = "../../util/io" }
ethcore-ipc = { path = "../../ipc/rpc" }
rlp = { path = "../../util/rlp" }
time = "0.1"
21 changes: 21 additions & 0 deletions ethcore/light/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

extern crate ethcore_ipc_codegen;

fn main() {
ethcore_ipc_codegen::derive_binary("src/types/mod.rs.in").unwrap();
}
2 changes: 1 addition & 1 deletion ethcore/light/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Provider for Client {
Vec::new()
}

fn code(&self, _req: request::ContractCodes) -> Vec<Bytes> {
fn contract_code(&self, _req: request::ContractCodes) -> Vec<Bytes> {
Vec::new()
}

Expand Down
19 changes: 12 additions & 7 deletions ethcore/light/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,25 @@
//! It starts by performing a header-only sync, verifying random samples
//! of members of the chain to varying degrees.

// TODO: remove when integrating with parity.
// TODO: remove when integrating with the rest of parity.
#![allow(dead_code)]

pub mod client;
pub mod net;
pub mod provider;
pub mod request;

mod types;

pub use self::provider::Provider;
pub use types::les_request as request;

#[macro_use]
extern crate log;

extern crate ethcore;
extern crate ethcore_util as util;
extern crate ethcore_network as network;
extern crate ethcore_io as io;
extern crate ethcore;
extern crate ethcore_ipc as ipc;
extern crate rlp;
extern crate time;

#[macro_use]
extern crate log;
extern crate time;
43 changes: 43 additions & 0 deletions ethcore/light/src/net/buffer_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,39 @@ impl FlowParams {
cost.0 + (amount * cost.1)
}

/// Compute the maximum number of costs of a specific kind which can be made
/// with the given buffer.
/// Saturates at `usize::max()`. This is not a problem in practice because
/// this amount of requests is already prohibitively large.
pub fn max_amount(&self, buffer: &Buffer, kind: request::Kind) -> usize {
use util::Uint;
use std::usize;

let cost = match kind {
request::Kind::Headers => &self.costs.headers,
request::Kind::Bodies => &self.costs.bodies,
request::Kind::Receipts => &self.costs.receipts,
request::Kind::StateProofs => &self.costs.state_proofs,
request::Kind::Codes => &self.costs.contract_codes,
request::Kind::HeaderProofs => &self.costs.header_proofs,
};

let start = buffer.current();

if start <= cost.0 {
return 0;
} else if cost.1 == U256::zero() {
return usize::MAX;
}

let max = (start - cost.0) / cost.1;
if max >= usize::MAX.into() {
usize::MAX
} else {
max.as_u64() as usize
}
}

/// Create initial buffer parameter.
pub fn create_buffer(&self) -> Buffer {
Buffer {
Expand All @@ -228,6 +261,16 @@ impl FlowParams {

buf.estimate = ::std::cmp::min(self.limit, buf.estimate + (elapsed * self.recharge));
}

/// Refund some buffer which was previously deducted.
/// Does not update the recharge timestamp.
pub fn refund(&self, buf: &mut Buffer, refund_amount: U256) {
buf.estimate = buf.estimate + refund_amount;

if buf.estimate > self.limit {
buf.estimate = self.limit
}
}
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions ethcore/light/src/net/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub enum Error {
UnexpectedHandshake,
/// Peer on wrong network (wrong NetworkId or genesis hash)
WrongNetwork,
/// Unknown peer.
UnknownPeer,
}

impl Error {
Expand All @@ -64,6 +66,7 @@ impl Error {
Error::UnrecognizedPacket(_) => Punishment::Disconnect,
Error::UnexpectedHandshake => Punishment::Disconnect,
Error::WrongNetwork => Punishment::Disable,
Error::UnknownPeer => Punishment::Disconnect,
}
}
}
Expand All @@ -89,6 +92,7 @@ impl fmt::Display for Error {
Error::UnrecognizedPacket(code) => write!(f, "Unrecognized packet: 0x{:x}", code),
Error::UnexpectedHandshake => write!(f, "Unexpected handshake"),
Error::WrongNetwork => write!(f, "Wrong network"),
Error::UnknownPeer => write!(f, "unknown peer"),
}
}
}
Loading

0 comments on commit 085b8ad

Please sign in to comment.