From 969261695831236b6eddc30b2af28e9714e0d2a7 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Wed, 8 Mar 2017 18:01:41 +0100 Subject: [PATCH] re-do cost table --- ethcore/light/src/client/mod.rs | 2 +- ethcore/light/src/net/context.rs | 1 - ethcore/light/src/net/mod.rs | 11 +++-- ethcore/light/src/net/request_credits.rs | 55 ++++++++++++++++++++++-- ethcore/light/src/net/request_set.rs | 2 +- 5 files changed, 59 insertions(+), 12 deletions(-) diff --git a/ethcore/light/src/client/mod.rs b/ethcore/light/src/client/mod.rs index 34f7ed990..c791caed1 100644 --- a/ethcore/light/src/client/mod.rs +++ b/ethcore/light/src/client/mod.rs @@ -31,7 +31,7 @@ use ethcore::service::ClientIoMessage; use ethcore::encoded; use io::IoChannel; -use util::{Bytes, DBValue, H256, Mutex, RwLock}; +use util::{H256, Mutex, RwLock}; use self::header_chain::{AncestryIter, HeaderChain}; diff --git a/ethcore/light/src/net/context.rs b/ethcore/light/src/net/context.rs index 659c117af..513388a92 100644 --- a/ethcore/light/src/net/context.rs +++ b/ethcore/light/src/net/context.rs @@ -20,7 +20,6 @@ use network::{NetworkContext, PeerId, NodeId}; use super::{Announcement, LightProtocol, ReqId}; use super::error::Error; -use request::{self, Request}; use request_builder::Requests; /// An I/O context which allows sending and receiving packets as well as diff --git a/ethcore/light/src/net/mod.rs b/ethcore/light/src/net/mod.rs index 57459ec01..0241cf3f1 100644 --- a/ethcore/light/src/net/mod.rs +++ b/ethcore/light/src/net/mod.rs @@ -18,14 +18,13 @@ //! //! This uses a "Provider" to answer requests. -use ethcore::transaction::{Action, UnverifiedTransaction}; -use ethcore::receipt::Receipt; +use ethcore::transaction::UnverifiedTransaction; use io::TimerToken; use network::{NetworkProtocolHandler, NetworkContext, PeerId}; use rlp::{RlpStream, Stream, UntrustedRlp, View}; use util::hash::H256; -use util::{Bytes, DBValue, Mutex, RwLock, U256}; +use util::{DBValue, Mutex, RwLock, U256}; use time::{Duration, SteadyTime}; use std::collections::HashMap; @@ -34,7 +33,7 @@ use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; use provider::Provider; -use request::{self, HashOrNumber, Request, Response}; +use request::{Request, Response}; use request_builder::Requests; use self::request_credits::{Credits, FlowParams}; @@ -48,8 +47,8 @@ mod error; mod status; mod request_set; -#[cfg(test)] -mod tests; +// #[cfg(test)] +// mod tests; pub mod request_credits; diff --git a/ethcore/light/src/net/request_credits.rs b/ethcore/light/src/net/request_credits.rs index e3821e05a..abeb7e569 100644 --- a/ethcore/light/src/net/request_credits.rs +++ b/ethcore/light/src/net/request_credits.rs @@ -27,7 +27,6 @@ //! on any empirical timings or mathematical models. use request::{self, Request}; -use super::packet; use super::error::Error; use rlp::*; @@ -107,13 +106,63 @@ impl Default for CostTable { impl RlpEncodable for CostTable { fn rlp_append(&self, s: &mut RlpStream) { - unimplemented!() + fn append_cost(s: &mut RlpStream, cost: &U256, kind: request::Kind) { + s.begin_list(2).append(&kind).append(cost); + } + + s.begin_list(9).append(&self.base); + append_cost(s, &self.headers, request::Kind::Headers); + append_cost(s, &self.body, request::Kind::Body); + append_cost(s, &self.receipts, request::Kind::Receipts); + append_cost(s, &self.account, request::Kind::Account); + append_cost(s, &self.storage, request::Kind::Storage); + append_cost(s, &self.code, request::Kind::Code); + append_cost(s, &self.header_proof, request::Kind::HeaderProof); + append_cost(s, &self.transaction_proof, request::Kind::Execution); } } impl RlpDecodable for CostTable { fn decode(decoder: &D) -> Result where D: Decoder { - unimplemented!() + let rlp = decoder.as_rlp(); + let base = rlp.val_at(0)?; + + let mut headers = None; + let mut body = None; + let mut receipts = None; + let mut account = None; + let mut storage = None; + let mut code = None; + let mut header_proof = None; + let mut transaction_proof = None; + + for cost_list in rlp.iter().skip(1) { + let cost = cost_list.val_at(1)?; + match cost_list.val_at(0)? { + request::Kind::Headers => headers = Some(cost), + request::Kind::Body => body = Some(cost), + request::Kind::Receipts => receipts = Some(cost), + request::Kind::Account => account = Some(cost), + request::Kind::Storage => storage = Some(cost), + request::Kind::Code => code = Some(cost), + request::Kind::HeaderProof => header_proof = Some(cost), + request::Kind::Execution => transaction_proof = Some(cost), + } + } + + let unwrap_cost = |cost: Option| cost.ok_or(DecoderError::Custom("Not all costs specified in cost table.")); + + Ok(CostTable { + base: base, + headers: unwrap_cost(headers)?, + body: unwrap_cost(body)?, + receipts: unwrap_cost(receipts)?, + account: unwrap_cost(account)?, + storage: unwrap_cost(storage)?, + code: unwrap_cost(code)?, + header_proof: unwrap_cost(header_proof)?, + transaction_proof: unwrap_cost(transaction_proof)?, + }) } } diff --git a/ethcore/light/src/net/request_set.rs b/ethcore/light/src/net/request_set.rs index eefc6dfd5..8405b8c89 100644 --- a/ethcore/light/src/net/request_set.rs +++ b/ethcore/light/src/net/request_set.rs @@ -24,7 +24,7 @@ use std::collections::{BTreeMap, HashMap}; use std::iter::FromIterator; -use request::{self, Request}; +use request::Request; use request_builder::Requests; use net::{timeout, ReqId};