reintroduce credits recharging

This commit is contained in:
Robert Habermeier 2017-03-23 04:36:49 +01:00
parent 0d110ed47c
commit 4eb69dc0fe

View File

@ -303,10 +303,18 @@ impl LightProtocol {
match peer.remote_flow { match peer.remote_flow {
None => Err(Error::NotServer), None => Err(Error::NotServer),
Some((ref mut creds, ref params)) => { Some((ref mut creds, ref params)) => {
// apply recharge to credits if there's no pending requests.
if peer.pending_requests.is_empty() {
params.recharge(creds);
}
// compute and deduct cost. // compute and deduct cost.
let cost = params.compute_cost_multi(requests.requests()); let cost = params.compute_cost_multi(requests.requests());
creds.deduct_cost(cost)?; creds.deduct_cost(cost)?;
trace!(target: "pip", "requesting from peer {}. Cost: {}; Available: {}",
peer_id, cost, creds.current());
let req_id = ReqId(self.req_id.fetch_add(1, Ordering::SeqCst)); 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); let mut stream = RlpStream::new_list(2);
@ -686,6 +694,8 @@ impl LightProtocol {
trace!(target: "pip", "Received requests (id: {}) from peer {}", req_id, peer_id); trace!(target: "pip", "Received requests (id: {}) from peer {}", req_id, peer_id);
// deserialize requests, check costs and request validity. // deserialize requests, check costs and request validity.
self.flow_params.recharge(&mut peer.local_credits);
peer.local_credits.deduct_cost(self.flow_params.base_cost())?; peer.local_credits.deduct_cost(self.flow_params.base_cost())?;
for request_rlp in raw.at(1)?.iter().take(MAX_REQUESTS) { for request_rlp in raw.at(1)?.iter().take(MAX_REQUESTS) {
let request: Request = request_rlp.as_val()?; let request: Request = request_rlp.as_val()?;
@ -712,6 +722,7 @@ impl LightProtocol {
}); });
trace!(target: "pip", "Responded to {}/{} requests in packet {}", responses.len(), num_requests, req_id); trace!(target: "pip", "Responded to {}/{} requests in packet {}", responses.len(), num_requests, req_id);
trace!(target: "pip", "Peer {} has {} credits remaining.", peer_id, peer.local_credits.current());
io.respond(packet::RESPONSE, { io.respond(packet::RESPONSE, {
let mut stream = RlpStream::new_list(3); let mut stream = RlpStream::new_list(3);