Light clippy(fy) (#9473)
* wasm tests * `clippyfy` light-client * Revert inefficient change `collect_ready()`
This commit is contained in:
committed by
Afri Schoedon
parent
4e8e5bbb86
commit
6888a968f9
@@ -126,7 +126,7 @@ impl<'a> BasicContext for TickCtx<'a> {
|
||||
}
|
||||
|
||||
fn request_from(&self, peer: PeerId, requests: Requests) -> Result<ReqId, Error> {
|
||||
self.proto.request_from(self.io, &peer, requests)
|
||||
self.proto.request_from(self.io, peer, requests)
|
||||
}
|
||||
|
||||
fn make_announcement(&self, announcement: Announcement) {
|
||||
@@ -159,7 +159,7 @@ impl<'a> BasicContext for Ctx<'a> {
|
||||
}
|
||||
|
||||
fn request_from(&self, peer: PeerId, requests: Requests) -> Result<ReqId, Error> {
|
||||
self.proto.request_from(self.io, &peer, requests)
|
||||
self.proto.request_from(self.io, peer, requests)
|
||||
}
|
||||
|
||||
fn make_announcement(&self, announcement: Announcement) {
|
||||
|
||||
@@ -108,9 +108,9 @@ impl LoadDistribution {
|
||||
|
||||
LoadTimer {
|
||||
start: Instant::now(),
|
||||
n: n,
|
||||
n,
|
||||
dist: self,
|
||||
kind: kind,
|
||||
kind,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ const UPDATE_INTERVAL: Duration = Duration::from_millis(5000);
|
||||
const PACKET_COUNT_V1: u8 = 9;
|
||||
|
||||
/// Supported protocol versions.
|
||||
pub const PROTOCOL_VERSIONS: &'static [(u8, u8)] = &[
|
||||
pub const PROTOCOL_VERSIONS: &[(u8, u8)] = &[
|
||||
(1, PACKET_COUNT_V1),
|
||||
];
|
||||
|
||||
@@ -309,9 +309,9 @@ mod id_guard {
|
||||
/// (for forming responses, triggering handlers) until defused
|
||||
pub fn new(peers: RwLockReadGuard<'a, PeerMap>, peer_id: PeerId, req_id: ReqId) -> Self {
|
||||
IdGuard {
|
||||
peers: peers,
|
||||
peer_id: peer_id,
|
||||
req_id: req_id,
|
||||
peers,
|
||||
peer_id,
|
||||
req_id,
|
||||
active: true,
|
||||
}
|
||||
}
|
||||
@@ -375,9 +375,9 @@ impl LightProtocol {
|
||||
);
|
||||
|
||||
LightProtocol {
|
||||
provider: provider,
|
||||
provider,
|
||||
config: params.config,
|
||||
genesis_hash: genesis_hash,
|
||||
genesis_hash,
|
||||
network_id: params.network_id,
|
||||
pending_peers: RwLock::new(HashMap::new()),
|
||||
peers: RwLock::new(HashMap::new()),
|
||||
@@ -385,13 +385,13 @@ impl LightProtocol {
|
||||
flow_params: RwLock::new(Arc::new(flow_params)),
|
||||
handlers: Vec::new(),
|
||||
req_id: AtomicUsize::new(0),
|
||||
sample_store: sample_store,
|
||||
load_distribution: load_distribution,
|
||||
sample_store,
|
||||
load_distribution,
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempt to get peer status.
|
||||
pub fn peer_status(&self, peer: &PeerId) -> Option<Status> {
|
||||
pub fn peer_status(&self, peer: PeerId) -> Option<Status> {
|
||||
self.peers.read().get(&peer)
|
||||
.map(|peer| peer.lock().status.clone())
|
||||
}
|
||||
@@ -412,9 +412,9 @@ impl LightProtocol {
|
||||
/// insufficient credits. Does not check capabilities before sending.
|
||||
/// On success, returns a request id which can later be coordinated
|
||||
/// with an event.
|
||||
pub fn request_from(&self, io: &IoContext, peer_id: &PeerId, requests: Requests) -> Result<ReqId, Error> {
|
||||
pub fn request_from(&self, io: &IoContext, peer_id: PeerId, requests: Requests) -> Result<ReqId, Error> {
|
||||
let peers = self.peers.read();
|
||||
let peer = match peers.get(peer_id) {
|
||||
let peer = match peers.get(&peer_id) {
|
||||
Some(peer) => peer,
|
||||
None => return Err(Error::UnknownPeer),
|
||||
};
|
||||
@@ -442,7 +442,7 @@ impl LightProtocol {
|
||||
peer_id, cost, pre_creds);
|
||||
|
||||
let req_id = ReqId(self.req_id.fetch_add(1, Ordering::SeqCst));
|
||||
io.send(*peer_id, packet::REQUEST, {
|
||||
io.send(peer_id, packet::REQUEST, {
|
||||
let mut stream = RlpStream::new_list(2);
|
||||
stream.append(&req_id.0).append_list(&requests.requests());
|
||||
stream.out()
|
||||
@@ -471,7 +471,7 @@ impl LightProtocol {
|
||||
// TODO: "urgent" announcements like new blocks?
|
||||
// the timer approach will skip 1 (possibly 2) in rare occasions.
|
||||
if peer_info.sent_head == announcement.head_hash ||
|
||||
peer_info.status.head_num >= announcement.head_num ||
|
||||
peer_info.status.head_num >= announcement.head_num ||
|
||||
now - peer_info.last_update < UPDATE_INTERVAL {
|
||||
continue
|
||||
}
|
||||
@@ -528,18 +528,18 @@ impl LightProtocol {
|
||||
// - check whether peer exists
|
||||
// - check whether request was made
|
||||
// - check whether request kinds match
|
||||
fn pre_verify_response(&self, peer: &PeerId, raw: &Rlp) -> Result<IdGuard, Error> {
|
||||
fn pre_verify_response(&self, peer: PeerId, raw: &Rlp) -> Result<IdGuard, Error> {
|
||||
let req_id = ReqId(raw.val_at(0)?);
|
||||
let cur_credits: U256 = raw.val_at(1)?;
|
||||
|
||||
trace!(target: "pip", "pre-verifying response for {} from peer {}", req_id, peer);
|
||||
|
||||
let peers = self.peers.read();
|
||||
let res = match peers.get(peer) {
|
||||
let res = match peers.get(&peer) {
|
||||
Some(peer_info) => {
|
||||
let mut peer_info = peer_info.lock();
|
||||
let peer_info: &mut Peer = &mut *peer_info;
|
||||
let req_info = peer_info.pending_requests.remove(&req_id, Instant::now());
|
||||
let req_info = peer_info.pending_requests.remove(req_id, Instant::now());
|
||||
let last_batched = peer_info.pending_requests.is_empty();
|
||||
let flow_info = peer_info.remote_flow.as_mut();
|
||||
|
||||
@@ -565,29 +565,29 @@ impl LightProtocol {
|
||||
None => Err(Error::UnknownPeer), // probably only occurs in a race of some kind.
|
||||
};
|
||||
|
||||
res.map(|_| IdGuard::new(peers, *peer, req_id))
|
||||
res.map(|_| IdGuard::new(peers, peer, req_id))
|
||||
}
|
||||
|
||||
/// Handle a packet using the given io context.
|
||||
/// Packet data is _untrusted_, which means that invalid data won't lead to
|
||||
/// issues.
|
||||
pub fn handle_packet(&self, io: &IoContext, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
pub fn handle_packet(&self, io: &IoContext, peer: PeerId, packet_id: u8, data: &[u8]) {
|
||||
let rlp = Rlp::new(data);
|
||||
|
||||
trace!(target: "pip", "Incoming packet {} from peer {}", packet_id, peer);
|
||||
|
||||
// handle the packet
|
||||
let res = match packet_id {
|
||||
packet::STATUS => self.status(peer, io, rlp),
|
||||
packet::ANNOUNCE => self.announcement(peer, io, rlp),
|
||||
packet::STATUS => self.status(peer, io, &rlp),
|
||||
packet::ANNOUNCE => self.announcement(peer, io, &rlp),
|
||||
|
||||
packet::REQUEST => self.request(peer, io, rlp),
|
||||
packet::RESPONSE => self.response(peer, io, rlp),
|
||||
packet::REQUEST => self.request(peer, io, &rlp),
|
||||
packet::RESPONSE => self.response(peer, io, &rlp),
|
||||
|
||||
packet::UPDATE_CREDITS => self.update_credits(peer, io, rlp),
|
||||
packet::ACKNOWLEDGE_UPDATE => self.acknowledge_update(peer, io, rlp),
|
||||
packet::UPDATE_CREDITS => self.update_credits(peer, io, &rlp),
|
||||
packet::ACKNOWLEDGE_UPDATE => self.acknowledge_update(peer, io, &rlp),
|
||||
|
||||
packet::SEND_TRANSACTIONS => self.relay_transactions(peer, io, rlp),
|
||||
packet::SEND_TRANSACTIONS => self.relay_transactions(peer, io, &rlp),
|
||||
|
||||
other => {
|
||||
Err(Error::UnrecognizedPacket(other))
|
||||
@@ -595,7 +595,7 @@ impl LightProtocol {
|
||||
};
|
||||
|
||||
if let Err(e) = res {
|
||||
punish(*peer, io, e);
|
||||
punish(peer, io, &e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -682,14 +682,14 @@ impl LightProtocol {
|
||||
}
|
||||
|
||||
/// called when a peer connects.
|
||||
pub fn on_connect(&self, peer: &PeerId, io: &IoContext) {
|
||||
let proto_version = match io.protocol_version(*peer).ok_or(Error::WrongNetwork) {
|
||||
pub fn on_connect(&self, peer: PeerId, io: &IoContext) {
|
||||
let proto_version = match io.protocol_version(peer).ok_or(Error::WrongNetwork) {
|
||||
Ok(pv) => pv,
|
||||
Err(e) => { punish(*peer, io, e); return }
|
||||
Err(e) => { punish(peer, io, &e); return }
|
||||
};
|
||||
|
||||
if PROTOCOL_VERSIONS.iter().find(|x| x.0 == proto_version).is_none() {
|
||||
punish(*peer, io, Error::UnsupportedProtocolVersion(proto_version));
|
||||
punish(peer, io, &Error::UnsupportedProtocolVersion(proto_version));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -705,17 +705,17 @@ impl LightProtocol {
|
||||
last_head: None,
|
||||
};
|
||||
|
||||
let capabilities = self.capabilities.read().clone();
|
||||
let capabilities = self.capabilities.read();
|
||||
let local_flow = self.flow_params.read();
|
||||
let status_packet = status::write_handshake(&status, &capabilities, Some(&**local_flow));
|
||||
|
||||
self.pending_peers.write().insert(*peer, PendingPeer {
|
||||
self.pending_peers.write().insert(peer, PendingPeer {
|
||||
sent_head: chain_info.best_block_hash,
|
||||
last_update: Instant::now(),
|
||||
});
|
||||
|
||||
trace!(target: "pip", "Sending status to peer {}", peer);
|
||||
io.send(*peer, packet::STATUS, status_packet);
|
||||
io.send(peer, packet::STATUS, status_packet);
|
||||
}
|
||||
|
||||
/// called when a peer disconnects.
|
||||
@@ -736,8 +736,8 @@ impl LightProtocol {
|
||||
|
||||
for handler in &self.handlers {
|
||||
handler.on_disconnect(&Ctx {
|
||||
peer: peer,
|
||||
io: io,
|
||||
peer,
|
||||
io,
|
||||
proto: self,
|
||||
}, &unfulfilled)
|
||||
}
|
||||
@@ -748,7 +748,7 @@ impl LightProtocol {
|
||||
where F: FnOnce(&BasicContext) -> T
|
||||
{
|
||||
f(&TickCtx {
|
||||
io: io,
|
||||
io,
|
||||
proto: self,
|
||||
})
|
||||
}
|
||||
@@ -756,7 +756,7 @@ impl LightProtocol {
|
||||
fn tick_handlers(&self, io: &IoContext) {
|
||||
for handler in &self.handlers {
|
||||
handler.tick(&TickCtx {
|
||||
io: io,
|
||||
io,
|
||||
proto: self,
|
||||
})
|
||||
}
|
||||
@@ -787,15 +787,15 @@ impl LightProtocol {
|
||||
let mut peer_info = peer_info.lock();
|
||||
|
||||
io.send(*peer_id, packet::UPDATE_CREDITS, packet_body.clone());
|
||||
peer_info.awaiting_acknowledge = Some((now.clone(), new_params.clone()));
|
||||
peer_info.awaiting_acknowledge = Some((now, new_params.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LightProtocol {
|
||||
// Handle status message from peer.
|
||||
fn status(&self, peer: &PeerId, io: &IoContext, data: Rlp) -> Result<(), Error> {
|
||||
let pending = match self.pending_peers.write().remove(peer) {
|
||||
fn status(&self, peer: PeerId, io: &IoContext, data: &Rlp) -> Result<(), Error> {
|
||||
let pending = match self.pending_peers.write().remove(&peer) {
|
||||
Some(pending) => pending,
|
||||
None => {
|
||||
return Err(Error::UnexpectedHandshake);
|
||||
@@ -813,33 +813,33 @@ impl LightProtocol {
|
||||
return Err(Error::WrongNetwork);
|
||||
}
|
||||
|
||||
if Some(status.protocol_version as u8) != io.protocol_version(*peer) {
|
||||
if Some(status.protocol_version as u8) != io.protocol_version(peer) {
|
||||
return Err(Error::BadProtocolVersion);
|
||||
}
|
||||
|
||||
let remote_flow = flow_params.map(|params| (params.create_credits(), params));
|
||||
let local_flow = self.flow_params.read().clone();
|
||||
|
||||
self.peers.write().insert(*peer, Mutex::new(Peer {
|
||||
self.peers.write().insert(peer, Mutex::new(Peer {
|
||||
local_credits: local_flow.create_credits(),
|
||||
status: status.clone(),
|
||||
capabilities,
|
||||
remote_flow: remote_flow,
|
||||
remote_flow,
|
||||
sent_head: pending.sent_head,
|
||||
last_update: pending.last_update,
|
||||
pending_requests: RequestSet::default(),
|
||||
failed_requests: Vec::new(),
|
||||
propagated_transactions: HashSet::new(),
|
||||
skip_update: false,
|
||||
local_flow: local_flow,
|
||||
local_flow,
|
||||
awaiting_acknowledge: None,
|
||||
}));
|
||||
|
||||
let any_kept = self.handlers.iter().map(
|
||||
|handler| handler.on_connect(
|
||||
&Ctx {
|
||||
peer: *peer,
|
||||
io: io,
|
||||
peer,
|
||||
io,
|
||||
proto: self,
|
||||
},
|
||||
&status,
|
||||
@@ -855,8 +855,8 @@ impl LightProtocol {
|
||||
}
|
||||
|
||||
// Handle an announcement.
|
||||
fn announcement(&self, peer: &PeerId, io: &IoContext, data: Rlp) -> Result<(), Error> {
|
||||
if !self.peers.read().contains_key(peer) {
|
||||
fn announcement(&self, peer: PeerId, io: &IoContext, data: &Rlp) -> Result<(), Error> {
|
||||
if !self.peers.read().contains_key(&peer) {
|
||||
debug!(target: "pip", "Ignoring announcement from unknown peer");
|
||||
return Ok(())
|
||||
}
|
||||
@@ -866,7 +866,7 @@ impl LightProtocol {
|
||||
// scope to ensure locks are dropped before moving into handler-space.
|
||||
{
|
||||
let peers = self.peers.read();
|
||||
let peer_info = match peers.get(peer) {
|
||||
let peer_info = match peers.get(&peer) {
|
||||
Some(info) => info,
|
||||
None => return Ok(()),
|
||||
};
|
||||
@@ -890,8 +890,8 @@ impl LightProtocol {
|
||||
|
||||
for handler in &self.handlers {
|
||||
handler.on_announcement(&Ctx {
|
||||
peer: *peer,
|
||||
io: io,
|
||||
peer,
|
||||
io,
|
||||
proto: self,
|
||||
}, &announcement);
|
||||
}
|
||||
@@ -900,7 +900,7 @@ impl LightProtocol {
|
||||
}
|
||||
|
||||
// Receive requests from a peer.
|
||||
fn request(&self, peer_id: &PeerId, io: &IoContext, raw: Rlp) -> Result<(), Error> {
|
||||
fn request(&self, peer_id: PeerId, io: &IoContext, raw: &Rlp) -> Result<(), Error> {
|
||||
// the maximum amount of requests we'll fill in a single packet.
|
||||
const MAX_REQUESTS: usize = 256;
|
||||
|
||||
@@ -908,7 +908,7 @@ impl LightProtocol {
|
||||
use ::request::CompleteRequest;
|
||||
|
||||
let peers = self.peers.read();
|
||||
let peer = match peers.get(peer_id) {
|
||||
let peer = match peers.get(&peer_id) {
|
||||
Some(peer) => peer,
|
||||
None => {
|
||||
debug!(target: "pip", "Ignoring request from unknown peer");
|
||||
@@ -968,7 +968,7 @@ impl LightProtocol {
|
||||
}
|
||||
|
||||
// handle a packet with responses.
|
||||
fn response(&self, peer: &PeerId, io: &IoContext, raw: Rlp) -> Result<(), Error> {
|
||||
fn response(&self, peer: PeerId, io: &IoContext, raw: &Rlp) -> Result<(), Error> {
|
||||
let (req_id, responses) = {
|
||||
let id_guard = self.pre_verify_response(peer, &raw)?;
|
||||
let responses: Vec<Response> = raw.list_at(2)?;
|
||||
@@ -977,9 +977,9 @@ impl LightProtocol {
|
||||
|
||||
for handler in &self.handlers {
|
||||
handler.on_responses(&Ctx {
|
||||
io: io,
|
||||
io,
|
||||
proto: self,
|
||||
peer: *peer,
|
||||
peer,
|
||||
}, req_id, &responses);
|
||||
}
|
||||
|
||||
@@ -987,10 +987,10 @@ impl LightProtocol {
|
||||
}
|
||||
|
||||
// handle an update of request credits parameters.
|
||||
fn update_credits(&self, peer_id: &PeerId, io: &IoContext, raw: Rlp) -> Result<(), Error> {
|
||||
fn update_credits(&self, peer_id: PeerId, io: &IoContext, raw: &Rlp) -> Result<(), Error> {
|
||||
let peers = self.peers.read();
|
||||
|
||||
let peer = peers.get(peer_id).ok_or(Error::UnknownPeer)?;
|
||||
let peer = peers.get(&peer_id).ok_or(Error::UnknownPeer)?;
|
||||
let mut peer = peer.lock();
|
||||
|
||||
trace!(target: "pip", "Received an update to request credit params from peer {}", peer_id);
|
||||
@@ -1022,9 +1022,9 @@ impl LightProtocol {
|
||||
}
|
||||
|
||||
// handle an acknowledgement of request credits update.
|
||||
fn acknowledge_update(&self, peer_id: &PeerId, _io: &IoContext, _raw: Rlp) -> Result<(), Error> {
|
||||
fn acknowledge_update(&self, peer_id: PeerId, _io: &IoContext, _raw: &Rlp) -> Result<(), Error> {
|
||||
let peers = self.peers.read();
|
||||
let peer = peers.get(peer_id).ok_or(Error::UnknownPeer)?;
|
||||
let peer = peers.get(&peer_id).ok_or(Error::UnknownPeer)?;
|
||||
let mut peer = peer.lock();
|
||||
|
||||
trace!(target: "pip", "Received an acknowledgement for new request credit params from peer {}", peer_id);
|
||||
@@ -1041,7 +1041,7 @@ impl LightProtocol {
|
||||
}
|
||||
|
||||
// Receive a set of transactions to relay.
|
||||
fn relay_transactions(&self, peer: &PeerId, io: &IoContext, data: Rlp) -> Result<(), Error> {
|
||||
fn relay_transactions(&self, peer: PeerId, io: &IoContext, data: &Rlp) -> Result<(), Error> {
|
||||
const MAX_TRANSACTIONS: usize = 256;
|
||||
|
||||
let txs: Vec<_> = data.iter()
|
||||
@@ -1053,8 +1053,8 @@ impl LightProtocol {
|
||||
|
||||
for handler in &self.handlers {
|
||||
handler.on_transactions(&Ctx {
|
||||
peer: *peer,
|
||||
io: io,
|
||||
peer,
|
||||
io,
|
||||
proto: self,
|
||||
}, &txs);
|
||||
}
|
||||
@@ -1064,7 +1064,7 @@ impl LightProtocol {
|
||||
}
|
||||
|
||||
// if something went wrong, figure out how much to punish the peer.
|
||||
fn punish(peer: PeerId, io: &IoContext, e: Error) {
|
||||
fn punish(peer: PeerId, io: &IoContext, e: &Error) {
|
||||
match e.punishment() {
|
||||
Punishment::None => {}
|
||||
Punishment::Disconnect => {
|
||||
@@ -1091,11 +1091,11 @@ impl NetworkProtocolHandler for LightProtocol {
|
||||
}
|
||||
|
||||
fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) {
|
||||
self.handle_packet(&io, peer, packet_id, data);
|
||||
self.handle_packet(&io, *peer, packet_id, data);
|
||||
}
|
||||
|
||||
fn connected(&self, io: &NetworkContext, peer: &PeerId) {
|
||||
self.on_connect(peer, &io);
|
||||
self.on_connect(*peer, &io);
|
||||
}
|
||||
|
||||
fn disconnected(&self, io: &NetworkContext, peer: &PeerId) {
|
||||
|
||||
@@ -46,7 +46,7 @@ pub struct Credits {
|
||||
|
||||
impl Credits {
|
||||
/// Get the current amount of credits..
|
||||
pub fn current(&self) -> U256 { self.estimate.clone() }
|
||||
pub fn current(&self) -> U256 { self.estimate }
|
||||
|
||||
/// Make a definitive update.
|
||||
/// This will be the value obtained after receiving
|
||||
@@ -68,12 +68,11 @@ impl Credits {
|
||||
/// If unsuccessful, the structure will be unaltered an an
|
||||
/// error will be produced.
|
||||
pub fn deduct_cost(&mut self, cost: U256) -> Result<(), Error> {
|
||||
match cost > self.estimate {
|
||||
true => Err(Error::NoCredits),
|
||||
false => {
|
||||
self.estimate = self.estimate - cost;
|
||||
Ok(())
|
||||
}
|
||||
if cost > self.estimate {
|
||||
Err(Error::NoCredits)
|
||||
} else {
|
||||
self.estimate = self.estimate - cost;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -121,7 +120,7 @@ impl Default for CostTable {
|
||||
fn default() -> Self {
|
||||
// arbitrarily chosen constants.
|
||||
CostTable {
|
||||
base: 100000.into(),
|
||||
base: 100_000.into(),
|
||||
headers: Some(10000.into()),
|
||||
transaction_index: Some(10000.into()),
|
||||
body: Some(15000.into()),
|
||||
@@ -193,17 +192,17 @@ impl Decodable for CostTable {
|
||||
}
|
||||
|
||||
let table = CostTable {
|
||||
base: base,
|
||||
headers: headers,
|
||||
transaction_index: transaction_index,
|
||||
body: body,
|
||||
receipts: receipts,
|
||||
account: account,
|
||||
storage: storage,
|
||||
code: code,
|
||||
header_proof: header_proof,
|
||||
transaction_proof: transaction_proof,
|
||||
epoch_signal: epoch_signal,
|
||||
base,
|
||||
headers,
|
||||
transaction_index,
|
||||
body,
|
||||
receipts,
|
||||
account,
|
||||
storage,
|
||||
code,
|
||||
header_proof,
|
||||
transaction_proof,
|
||||
epoch_signal,
|
||||
};
|
||||
|
||||
if table.costs_set() == 0 {
|
||||
@@ -227,9 +226,9 @@ impl FlowParams {
|
||||
/// credit limit, and (minimum) rate of recharge.
|
||||
pub fn new(limit: U256, costs: CostTable, recharge: U256) -> Self {
|
||||
FlowParams {
|
||||
costs: costs,
|
||||
limit: limit,
|
||||
recharge: recharge,
|
||||
costs,
|
||||
limit,
|
||||
recharge,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,7 +282,7 @@ impl FlowParams {
|
||||
};
|
||||
|
||||
FlowParams {
|
||||
costs: costs,
|
||||
costs,
|
||||
limit: max.into(),
|
||||
recharge: recharge.into(),
|
||||
}
|
||||
@@ -293,19 +292,19 @@ impl FlowParams {
|
||||
pub fn free() -> Self {
|
||||
let free_cost: Option<U256> = Some(0.into());
|
||||
FlowParams {
|
||||
limit: (!0u64).into(),
|
||||
limit: (!0_u64).into(),
|
||||
recharge: 1.into(),
|
||||
costs: CostTable {
|
||||
base: 0.into(),
|
||||
headers: free_cost.clone(),
|
||||
transaction_index: free_cost.clone(),
|
||||
body: free_cost.clone(),
|
||||
receipts: free_cost.clone(),
|
||||
account: free_cost.clone(),
|
||||
storage: free_cost.clone(),
|
||||
code: free_cost.clone(),
|
||||
header_proof: free_cost.clone(),
|
||||
transaction_proof: free_cost.clone(),
|
||||
headers: free_cost,
|
||||
transaction_index: free_cost,
|
||||
body: free_cost,
|
||||
receipts: free_cost,
|
||||
account: free_cost,
|
||||
storage: free_cost,
|
||||
code: free_cost,
|
||||
header_proof: free_cost,
|
||||
transaction_proof: free_cost,
|
||||
epoch_signal: free_cost,
|
||||
}
|
||||
}
|
||||
@@ -370,7 +369,7 @@ impl FlowParams {
|
||||
// recompute and update only in terms of full seconds elapsed
|
||||
// in order to keep the estimate as an underestimate.
|
||||
let elapsed = (now - credits.recharge_point).as_secs();
|
||||
credits.recharge_point = credits.recharge_point + Duration::from_secs(elapsed);
|
||||
credits.recharge_point += Duration::from_secs(elapsed);
|
||||
|
||||
let elapsed: U256 = elapsed.into();
|
||||
|
||||
@@ -418,7 +417,7 @@ mod tests {
|
||||
use std::time::Duration;
|
||||
|
||||
let flow_params = FlowParams::new(100.into(), Default::default(), 20.into());
|
||||
let mut credits = flow_params.create_credits();
|
||||
let mut credits = flow_params.create_credits();
|
||||
|
||||
assert!(credits.deduct_cost(101.into()).is_err());
|
||||
assert!(credits.deduct_cost(10.into()).is_ok());
|
||||
|
||||
@@ -73,7 +73,7 @@ impl RequestSet {
|
||||
}
|
||||
|
||||
/// Remove a set of requests from the stack.
|
||||
pub fn remove(&mut self, req_id: &ReqId, now: Instant) -> Option<Requests> {
|
||||
pub fn remove(&mut self, req_id: ReqId, now: Instant) -> Option<Requests> {
|
||||
let id = match self.ids.remove(&req_id) {
|
||||
Some(id) => id,
|
||||
None => return None,
|
||||
@@ -165,7 +165,7 @@ mod tests {
|
||||
let test_end = test_begin + req_time;
|
||||
assert!(req_set.check_timeout(test_end));
|
||||
|
||||
req_set.remove(&ReqId(0), test_begin + Duration::from_secs(1)).unwrap();
|
||||
req_set.remove(ReqId(0), test_begin + Duration::from_secs(1)).unwrap();
|
||||
assert!(!req_set.check_timeout(test_end));
|
||||
assert!(req_set.check_timeout(test_end + Duration::from_secs(1)));
|
||||
}
|
||||
@@ -183,7 +183,7 @@ mod tests {
|
||||
}
|
||||
|
||||
for i in (0..5).rev() {
|
||||
assert!(req_set.remove(&ReqId(i), test_end).is_some());
|
||||
assert!(req_set.remove(ReqId(i), test_end).is_some());
|
||||
assert_eq!(req_set.cumulative_cost, i.into());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,8 +43,8 @@ enum Key {
|
||||
|
||||
impl Key {
|
||||
// get the string value of this key.
|
||||
fn as_str(&self) -> &'static str {
|
||||
match *self {
|
||||
fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Key::ProtocolVersion => "protocolVersion",
|
||||
Key::NetworkId => "networkId",
|
||||
Key::HeadTD => "headTd",
|
||||
@@ -85,7 +85,7 @@ impl Key {
|
||||
// helper for decoding key-value pairs in the handshake or an announcement.
|
||||
struct Parser<'a> {
|
||||
pos: usize,
|
||||
rlp: Rlp<'a>,
|
||||
rlp: &'a Rlp<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
@@ -208,10 +208,10 @@ impl Capabilities {
|
||||
/// - chain status
|
||||
/// - serving capabilities
|
||||
/// - request credit parameters
|
||||
pub fn parse_handshake(rlp: Rlp) -> Result<(Status, Capabilities, Option<FlowParams>), DecoderError> {
|
||||
pub fn parse_handshake(rlp: &Rlp) -> Result<(Status, Capabilities, Option<FlowParams>), DecoderError> {
|
||||
let mut parser = Parser {
|
||||
pos: 0,
|
||||
rlp: rlp,
|
||||
rlp,
|
||||
};
|
||||
|
||||
let status = Status {
|
||||
@@ -304,7 +304,7 @@ pub struct Announcement {
|
||||
}
|
||||
|
||||
/// Parse an announcement.
|
||||
pub fn parse_announcement(rlp: Rlp) -> Result<Announcement, DecoderError> {
|
||||
pub fn parse_announcement(rlp: &Rlp) -> Result<Announcement, DecoderError> {
|
||||
let mut last_key = None;
|
||||
|
||||
let mut announcement = Announcement {
|
||||
@@ -320,7 +320,7 @@ pub fn parse_announcement(rlp: Rlp) -> Result<Announcement, DecoderError> {
|
||||
|
||||
let mut parser = Parser {
|
||||
pos: 4,
|
||||
rlp: rlp,
|
||||
rlp,
|
||||
};
|
||||
|
||||
while let Some((key, item)) = parser.get_next()? {
|
||||
@@ -404,7 +404,7 @@ mod tests {
|
||||
let handshake = write_handshake(&status, &capabilities, Some(&flow_params));
|
||||
|
||||
let (read_status, read_capabilities, read_flow)
|
||||
= parse_handshake(Rlp::new(&handshake)).unwrap();
|
||||
= parse_handshake(&Rlp::new(&handshake)).unwrap();
|
||||
|
||||
assert_eq!(read_status, status);
|
||||
assert_eq!(read_capabilities, capabilities);
|
||||
@@ -439,7 +439,7 @@ mod tests {
|
||||
let handshake = write_handshake(&status, &capabilities, Some(&flow_params));
|
||||
|
||||
let (read_status, read_capabilities, read_flow)
|
||||
= parse_handshake(Rlp::new(&handshake)).unwrap();
|
||||
= parse_handshake(&Rlp::new(&handshake)).unwrap();
|
||||
|
||||
assert_eq!(read_status, status);
|
||||
assert_eq!(read_capabilities, capabilities);
|
||||
@@ -489,7 +489,7 @@ mod tests {
|
||||
};
|
||||
|
||||
let (read_status, read_capabilities, read_flow)
|
||||
= parse_handshake(Rlp::new(&interleaved)).unwrap();
|
||||
= parse_handshake(&Rlp::new(&interleaved)).unwrap();
|
||||
|
||||
assert_eq!(read_status, status);
|
||||
assert_eq!(read_capabilities, capabilities);
|
||||
@@ -510,7 +510,7 @@ mod tests {
|
||||
};
|
||||
|
||||
let serialized = write_announcement(&announcement);
|
||||
let read = parse_announcement(Rlp::new(&serialized)).unwrap();
|
||||
let read = parse_announcement(&Rlp::new(&serialized)).unwrap();
|
||||
|
||||
assert_eq!(read, announcement);
|
||||
}
|
||||
@@ -522,26 +522,26 @@ mod tests {
|
||||
let mut stream = RlpStream::new_list(6);
|
||||
stream
|
||||
.append(&H256::zero())
|
||||
.append(&10u64)
|
||||
.append(&100_000u64)
|
||||
.append(&2u64)
|
||||
.append_raw(&encode_pair(Key::ServeStateSince, &44u64), 1)
|
||||
.append(&10_u64)
|
||||
.append(&100_000_u64)
|
||||
.append(&2_u64)
|
||||
.append_raw(&encode_pair(Key::ServeStateSince, &44_u64), 1)
|
||||
.append_raw(&encode_flag(Key::ServeHeaders), 1);
|
||||
|
||||
let out = stream.drain();
|
||||
assert!(parse_announcement(Rlp::new(&out)).is_err());
|
||||
assert!(parse_announcement(&Rlp::new(&out)).is_err());
|
||||
|
||||
let mut stream = RlpStream::new_list(6);
|
||||
stream
|
||||
.append(&H256::zero())
|
||||
.append(&10u64)
|
||||
.append(&100_000u64)
|
||||
.append(&2u64)
|
||||
.append(&10_u64)
|
||||
.append(&100_000_u64)
|
||||
.append(&2_u64)
|
||||
.append_raw(&encode_flag(Key::ServeHeaders), 1)
|
||||
.append_raw(&encode_pair(Key::ServeStateSince, &44u64), 1);
|
||||
.append_raw(&encode_pair(Key::ServeStateSince, &44_u64), 1);
|
||||
|
||||
let out = stream.drain();
|
||||
assert!(parse_announcement(Rlp::new(&out)).is_ok());
|
||||
assert!(parse_announcement(&Rlp::new(&out)).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -566,7 +566,7 @@ mod tests {
|
||||
let handshake = write_handshake(&status, &capabilities, None);
|
||||
|
||||
let (read_status, read_capabilities, read_flow)
|
||||
= parse_handshake(Rlp::new(&handshake)).unwrap();
|
||||
= parse_handshake(&Rlp::new(&handshake)).unwrap();
|
||||
|
||||
assert_eq!(read_status, status);
|
||||
assert_eq!(read_capabilities, capabilities);
|
||||
|
||||
@@ -228,7 +228,7 @@ fn handshake_expected() {
|
||||
|
||||
let packet_body = write_handshake(&status, &capabilities, &proto);
|
||||
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -243,7 +243,7 @@ fn genesis_mismatch() {
|
||||
|
||||
let packet_body = write_handshake(&status, &capabilities, &proto);
|
||||
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -256,12 +256,12 @@ fn credit_overflow() {
|
||||
|
||||
{
|
||||
let packet_body = write_handshake(&status, &capabilities, &proto);
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
}
|
||||
|
||||
{
|
||||
let my_status = write_handshake(&status, &capabilities, &proto);
|
||||
proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &my_status);
|
||||
proto.handle_packet(&Expect::Nothing, 1, packet::STATUS, &my_status);
|
||||
}
|
||||
|
||||
// 1 billion requests is far too many for the default flow params.
|
||||
@@ -273,7 +273,7 @@ fn credit_overflow() {
|
||||
}));
|
||||
let request = make_packet(111, &requests);
|
||||
|
||||
proto.handle_packet(&Expect::Punish(1), &1, packet::REQUEST, &request);
|
||||
proto.handle_packet(&Expect::Punish(1), 1, packet::REQUEST, &request);
|
||||
}
|
||||
|
||||
// test the basic request types -- these just make sure that requests are parsed
|
||||
@@ -295,8 +295,8 @@ fn get_block_headers() {
|
||||
|
||||
{
|
||||
let packet_body = write_handshake(&cur_status, &capabilities, &proto);
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &my_status);
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
proto.handle_packet(&Expect::Nothing, 1, packet::STATUS, &my_status);
|
||||
}
|
||||
|
||||
let request = Request::Headers(IncompleteHeadersRequest {
|
||||
@@ -317,9 +317,7 @@ fn get_block_headers() {
|
||||
|
||||
let new_creds = *flow_params.limit() - flow_params.compute_cost_multi(requests.requests()).unwrap();
|
||||
|
||||
let response = vec![Response::Headers(HeadersResponse {
|
||||
headers: headers,
|
||||
})];
|
||||
let response = vec![Response::Headers(HeadersResponse { headers })];
|
||||
|
||||
let mut stream = RlpStream::new_list(3);
|
||||
stream.append(&req_id).append(&new_creds).append_list(&response);
|
||||
@@ -328,7 +326,7 @@ fn get_block_headers() {
|
||||
};
|
||||
|
||||
let expected = Expect::Respond(packet::RESPONSE, response);
|
||||
proto.handle_packet(&expected, &1, packet::REQUEST, &request_body);
|
||||
proto.handle_packet(&expected, 1, packet::REQUEST, &request_body);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -347,8 +345,8 @@ fn get_block_bodies() {
|
||||
|
||||
{
|
||||
let packet_body = write_handshake(&cur_status, &capabilities, &proto);
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &my_status);
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
proto.handle_packet(&Expect::Nothing, 1, packet::STATUS, &my_status);
|
||||
}
|
||||
|
||||
let mut builder = Builder::default();
|
||||
@@ -376,7 +374,7 @@ fn get_block_bodies() {
|
||||
};
|
||||
|
||||
let expected = Expect::Respond(packet::RESPONSE, response);
|
||||
proto.handle_packet(&expected, &1, packet::REQUEST, &request_body);
|
||||
proto.handle_packet(&expected, 1, packet::REQUEST, &request_body);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -395,8 +393,8 @@ fn get_block_receipts() {
|
||||
|
||||
{
|
||||
let packet_body = write_handshake(&cur_status, &capabilities, &proto);
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &my_status);
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body));
|
||||
proto.handle_packet(&Expect::Nothing, 1, packet::STATUS, &my_status);
|
||||
}
|
||||
|
||||
// find the first 10 block hashes starting with `f` because receipts are only provided
|
||||
@@ -431,7 +429,7 @@ fn get_block_receipts() {
|
||||
};
|
||||
|
||||
let expected = Expect::Respond(packet::RESPONSE, response);
|
||||
proto.handle_packet(&expected, &1, packet::REQUEST, &request_body);
|
||||
proto.handle_packet(&expected, 1, packet::REQUEST, &request_body);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -447,8 +445,8 @@ fn get_state_proofs() {
|
||||
|
||||
{
|
||||
let packet_body = write_handshake(&cur_status, &capabilities, &proto);
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body.clone()));
|
||||
proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &packet_body);
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body.clone()));
|
||||
proto.handle_packet(&Expect::Nothing, 1, packet::STATUS, &packet_body);
|
||||
}
|
||||
|
||||
let req_id = 112;
|
||||
@@ -490,7 +488,7 @@ fn get_state_proofs() {
|
||||
};
|
||||
|
||||
let expected = Expect::Respond(packet::RESPONSE, response);
|
||||
proto.handle_packet(&expected, &1, packet::REQUEST, &request_body);
|
||||
proto.handle_packet(&expected, 1, packet::REQUEST, &request_body);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -504,8 +502,8 @@ fn get_contract_code() {
|
||||
|
||||
{
|
||||
let packet_body = write_handshake(&cur_status, &capabilities, &proto);
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body.clone()));
|
||||
proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &packet_body);
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body.clone()));
|
||||
proto.handle_packet(&Expect::Nothing, 1, packet::STATUS, &packet_body);
|
||||
}
|
||||
|
||||
let req_id = 112;
|
||||
@@ -533,7 +531,7 @@ fn get_contract_code() {
|
||||
};
|
||||
|
||||
let expected = Expect::Respond(packet::RESPONSE, response);
|
||||
proto.handle_packet(&expected, &1, packet::REQUEST, &request_body);
|
||||
proto.handle_packet(&expected, 1, packet::REQUEST, &request_body);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -547,8 +545,8 @@ fn epoch_signal() {
|
||||
|
||||
{
|
||||
let packet_body = write_handshake(&cur_status, &capabilities, &proto);
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body.clone()));
|
||||
proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &packet_body);
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body.clone()));
|
||||
proto.handle_packet(&Expect::Nothing, 1, packet::STATUS, &packet_body);
|
||||
}
|
||||
|
||||
let req_id = 112;
|
||||
@@ -576,7 +574,7 @@ fn epoch_signal() {
|
||||
};
|
||||
|
||||
let expected = Expect::Respond(packet::RESPONSE, response);
|
||||
proto.handle_packet(&expected, &1, packet::REQUEST, &request_body);
|
||||
proto.handle_packet(&expected, 1, packet::REQUEST, &request_body);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -590,8 +588,8 @@ fn proof_of_execution() {
|
||||
|
||||
{
|
||||
let packet_body = write_handshake(&cur_status, &capabilities, &proto);
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body.clone()));
|
||||
proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &packet_body);
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body.clone()));
|
||||
proto.handle_packet(&Expect::Nothing, 1, packet::STATUS, &packet_body);
|
||||
}
|
||||
|
||||
let req_id = 112;
|
||||
@@ -622,7 +620,7 @@ fn proof_of_execution() {
|
||||
};
|
||||
|
||||
let expected = Expect::Respond(packet::RESPONSE, response);
|
||||
proto.handle_packet(&expected, &1, packet::REQUEST, &request_body);
|
||||
proto.handle_packet(&expected, 1, packet::REQUEST, &request_body);
|
||||
|
||||
// next: way too much requested gas.
|
||||
if let Request::Execution(ref mut req) = request {
|
||||
@@ -633,7 +631,7 @@ fn proof_of_execution() {
|
||||
let request_body = make_packet(req_id, &requests);
|
||||
|
||||
let expected = Expect::Punish(1);
|
||||
proto.handle_packet(&expected, &1, packet::REQUEST, &request_body);
|
||||
proto.handle_packet(&expected, 1, packet::REQUEST, &request_body);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -682,33 +680,33 @@ fn id_guard() {
|
||||
{
|
||||
let mut stream = RlpStream::new_list(3);
|
||||
stream.append(&req_id_1.0);
|
||||
stream.append(&4_000_000usize);
|
||||
stream.begin_list(2).append(&125usize).append(&3usize);
|
||||
stream.append(&4_000_000_usize);
|
||||
stream.begin_list(2).append(&125_usize).append(&3_usize);
|
||||
|
||||
let packet = stream.out();
|
||||
assert!(proto.response(&peer_id, &Expect::Nothing, Rlp::new(&packet)).is_err());
|
||||
assert!(proto.response(peer_id, &Expect::Nothing, &Rlp::new(&packet)).is_err());
|
||||
}
|
||||
|
||||
// next, do an unexpected response.
|
||||
{
|
||||
let mut stream = RlpStream::new_list(3);
|
||||
stream.append(&10000usize);
|
||||
stream.append(&3_000_000usize);
|
||||
stream.append(&10000_usize);
|
||||
stream.append(&3_000_000_usize);
|
||||
stream.begin_list(0);
|
||||
|
||||
let packet = stream.out();
|
||||
assert!(proto.response(&peer_id, &Expect::Nothing, Rlp::new(&packet)).is_err());
|
||||
assert!(proto.response(peer_id, &Expect::Nothing, &Rlp::new(&packet)).is_err());
|
||||
}
|
||||
|
||||
// lastly, do a valid (but empty) response.
|
||||
{
|
||||
let mut stream = RlpStream::new_list(3);
|
||||
stream.append(&req_id_2.0);
|
||||
stream.append(&3_000_000usize);
|
||||
stream.append(&3_000_000_usize);
|
||||
stream.begin_list(0);
|
||||
|
||||
let packet = stream.out();
|
||||
assert!(proto.response(&peer_id, &Expect::Nothing, Rlp::new(&packet)).is_ok());
|
||||
assert!(proto.response(peer_id, &Expect::Nothing, &Rlp::new(&packet)).is_ok());
|
||||
}
|
||||
|
||||
let peers = proto.peers.read();
|
||||
@@ -730,8 +728,8 @@ fn get_transaction_index() {
|
||||
|
||||
{
|
||||
let packet_body = write_handshake(&cur_status, &capabilities, &proto);
|
||||
proto.on_connect(&1, &Expect::Send(1, packet::STATUS, packet_body.clone()));
|
||||
proto.handle_packet(&Expect::Nothing, &1, packet::STATUS, &packet_body);
|
||||
proto.on_connect(1, &Expect::Send(1, packet::STATUS, packet_body.clone()));
|
||||
proto.handle_packet(&Expect::Nothing, 1, packet::STATUS, &packet_body);
|
||||
}
|
||||
|
||||
let req_id = 112;
|
||||
@@ -759,5 +757,5 @@ fn get_transaction_index() {
|
||||
};
|
||||
|
||||
let expected = Expect::Respond(packet::RESPONSE, response);
|
||||
proto.handle_packet(&expected, &1, packet::REQUEST, &request_body);
|
||||
proto.handle_packet(&expected, 1, packet::REQUEST, &request_body);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user