2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-11-06 19:04:30 +01:00
|
|
|
// 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/>.
|
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
//! Request credit management.
|
2016-11-06 19:04:30 +01:00
|
|
|
//!
|
2017-03-08 14:39:44 +01:00
|
|
|
//! Every request in the light protocol leads to a reduction
|
|
|
|
//! of the requester's amount of credits as a rate-limiting mechanism.
|
|
|
|
//! The amount of credits will recharge at a set rate.
|
2016-11-06 19:04:30 +01:00
|
|
|
//!
|
2017-03-08 14:39:44 +01:00
|
|
|
//! This module provides an interface for configuration of
|
|
|
|
//! costs and recharge rates of request credits.
|
2016-12-08 23:21:47 +01:00
|
|
|
//!
|
2017-01-11 14:39:03 +01:00
|
|
|
//! Current default costs are picked completely arbitrarily, not based
|
2016-12-08 23:21:47 +01:00
|
|
|
//! on any empirical timings or mathematical models.
|
2016-11-06 19:04:30 +01:00
|
|
|
|
2017-03-23 13:17:05 +01:00
|
|
|
use request::{self, Request};
|
2016-11-09 18:05:00 +01:00
|
|
|
use super::error::Error;
|
2016-11-06 19:04:30 +01:00
|
|
|
|
2016-11-07 15:40:34 +01:00
|
|
|
use rlp::*;
|
|
|
|
use util::U256;
|
|
|
|
use time::{Duration, SteadyTime};
|
2016-11-06 19:04:30 +01:00
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
/// Credits value.
|
2016-11-07 15:40:34 +01:00
|
|
|
///
|
|
|
|
/// Produced and recharged using `FlowParams`.
|
|
|
|
/// Definitive updates can be made as well -- these will reset the recharge
|
|
|
|
/// point to the time of the update.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2017-03-08 14:39:44 +01:00
|
|
|
pub struct Credits {
|
2016-11-07 15:40:34 +01:00
|
|
|
estimate: U256,
|
|
|
|
recharge_point: SteadyTime,
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
impl Credits {
|
|
|
|
/// Get the current amount of credits..
|
2016-11-09 18:05:00 +01:00
|
|
|
pub fn current(&self) -> U256 { self.estimate.clone() }
|
|
|
|
|
2016-11-07 15:40:34 +01:00
|
|
|
/// Make a definitive update.
|
|
|
|
/// This will be the value obtained after receiving
|
|
|
|
/// a response to a request.
|
|
|
|
pub fn update_to(&mut self, value: U256) {
|
|
|
|
self.estimate = value;
|
|
|
|
self.recharge_point = SteadyTime::now();
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
/// Attempt to apply the given cost to the amount of credits.
|
2016-11-09 18:05:00 +01:00
|
|
|
///
|
2016-11-07 15:40:34 +01:00
|
|
|
/// If successful, the cost will be deducted successfully.
|
2016-11-09 18:05:00 +01:00
|
|
|
///
|
2016-11-07 15:40:34 +01:00
|
|
|
/// If unsuccessful, the structure will be unaltered an an
|
|
|
|
/// error will be produced.
|
2016-11-09 18:05:00 +01:00
|
|
|
pub fn deduct_cost(&mut self, cost: U256) -> Result<(), Error> {
|
2016-11-07 15:40:34 +01:00
|
|
|
match cost > self.estimate {
|
2017-03-08 14:39:44 +01:00
|
|
|
true => Err(Error::NoCredits),
|
2016-11-07 15:40:34 +01:00
|
|
|
false => {
|
|
|
|
self.estimate = self.estimate - cost;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A cost table, mapping requests to base and per-request costs.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct CostTable {
|
2017-03-23 13:17:05 +01:00
|
|
|
base: U256, // cost per packet.
|
|
|
|
headers: U256, // cost per header
|
|
|
|
body: U256,
|
|
|
|
receipts: U256,
|
|
|
|
account: U256,
|
|
|
|
storage: U256,
|
|
|
|
code: U256,
|
|
|
|
header_proof: U256,
|
|
|
|
transaction_proof: U256, // cost per gas.
|
2016-11-07 15:40:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CostTable {
|
|
|
|
fn default() -> Self {
|
|
|
|
// arbitrarily chosen constants.
|
|
|
|
CostTable {
|
2017-03-23 13:17:05 +01:00
|
|
|
base: 100000.into(),
|
|
|
|
headers: 10000.into(),
|
|
|
|
body: 15000.into(),
|
|
|
|
receipts: 5000.into(),
|
|
|
|
account: 25000.into(),
|
|
|
|
storage: 25000.into(),
|
|
|
|
code: 20000.into(),
|
|
|
|
header_proof: 15000.into(),
|
|
|
|
transaction_proof: 2.into(),
|
2016-11-07 15:40:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 19:14:29 +01:00
|
|
|
impl Encodable for CostTable {
|
2016-11-07 15:40:34 +01:00
|
|
|
fn rlp_append(&self, s: &mut RlpStream) {
|
2017-03-23 13:17:05 +01:00
|
|
|
fn append_cost(s: &mut RlpStream, cost: &U256, kind: request::Kind) {
|
|
|
|
s.begin_list(2);
|
2016-11-07 15:40:34 +01:00
|
|
|
|
2017-03-29 15:17:27 +02:00
|
|
|
// hack around https://github.com/paritytech/parity/issues/4356
|
2017-03-23 13:17:05 +01:00
|
|
|
Encodable::rlp_append(&kind, s);
|
|
|
|
s.append(cost);
|
|
|
|
}
|
2016-11-07 15:40:34 +01:00
|
|
|
|
2017-03-23 13:17:05 +01:00
|
|
|
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);
|
2016-11-06 19:04:30 +01:00
|
|
|
}
|
2016-11-07 15:40:34 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 14:41:46 +01:00
|
|
|
impl Decodable for CostTable {
|
|
|
|
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
|
2017-03-23 13:17:05 +01:00
|
|
|
let base = rlp.val_at(0)?;
|
|
|
|
|
2016-11-07 15:40:34 +01:00
|
|
|
let mut headers = None;
|
2017-03-23 13:17:05 +01:00
|
|
|
let mut body = None;
|
2016-11-07 15:40:34 +01:00
|
|
|
let mut receipts = None;
|
2017-03-23 13:17:05 +01:00
|
|
|
let mut account = None;
|
|
|
|
let mut storage = None;
|
|
|
|
let mut code = None;
|
|
|
|
let mut header_proof = None;
|
2017-03-08 14:39:44 +01:00
|
|
|
let mut transaction_proof = None;
|
2016-11-07 15:40:34 +01:00
|
|
|
|
2017-03-23 13:17:05 +01:00
|
|
|
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),
|
2016-11-07 15:40:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:17:05 +01:00
|
|
|
let unwrap_cost = |cost: Option<U256>| cost.ok_or(DecoderError::Custom("Not all costs specified in cost table."));
|
|
|
|
|
2016-11-07 15:40:34 +01:00
|
|
|
Ok(CostTable {
|
2017-03-23 13:17:05 +01:00
|
|
|
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)?,
|
2016-11-07 15:40:34 +01:00
|
|
|
})
|
2016-11-06 19:04:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
/// Handles costs, recharge, limits of request credits.
|
2016-11-07 15:40:34 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct FlowParams {
|
|
|
|
costs: CostTable,
|
|
|
|
limit: U256,
|
|
|
|
recharge: U256,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FlowParams {
|
|
|
|
/// Create new flow parameters from a request cost table,
|
2017-03-08 14:39:44 +01:00
|
|
|
/// credit limit, and (minimum) rate of recharge.
|
2016-11-08 16:57:10 +01:00
|
|
|
pub fn new(limit: U256, costs: CostTable, recharge: U256) -> Self {
|
2016-11-07 15:40:34 +01:00
|
|
|
FlowParams {
|
|
|
|
costs: costs,
|
|
|
|
limit: limit,
|
|
|
|
recharge: recharge,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:39:03 +01:00
|
|
|
/// Create effectively infinite flow params.
|
|
|
|
pub fn free() -> Self {
|
2017-03-23 13:17:05 +01:00
|
|
|
let free_cost: U256 = 0.into();
|
2017-01-11 14:39:03 +01:00
|
|
|
FlowParams {
|
|
|
|
limit: (!0u64).into(),
|
|
|
|
recharge: 1.into(),
|
|
|
|
costs: CostTable {
|
2017-03-23 13:17:05 +01:00
|
|
|
base: free_cost.clone(),
|
2017-01-11 14:39:03 +01:00
|
|
|
headers: free_cost.clone(),
|
2017-03-23 13:17:05 +01:00
|
|
|
body: free_cost.clone(),
|
2017-01-11 14:39:03 +01:00
|
|
|
receipts: free_cost.clone(),
|
2017-03-23 13:17:05 +01:00
|
|
|
account: free_cost.clone(),
|
|
|
|
storage: free_cost.clone(),
|
|
|
|
code: free_cost.clone(),
|
|
|
|
header_proof: free_cost.clone(),
|
2017-03-08 14:39:44 +01:00
|
|
|
transaction_proof: free_cost,
|
2017-01-11 14:39:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
/// Get a reference to the credit limit.
|
2016-11-08 16:57:10 +01:00
|
|
|
pub fn limit(&self) -> &U256 { &self.limit }
|
|
|
|
|
|
|
|
/// Get a reference to the cost table.
|
|
|
|
pub fn cost_table(&self) -> &CostTable { &self.costs }
|
|
|
|
|
2017-03-23 13:17:05 +01:00
|
|
|
/// Get the base cost of a request.
|
|
|
|
pub fn base_cost(&self) -> U256 { self.costs.base }
|
|
|
|
|
2016-11-08 16:57:10 +01:00
|
|
|
/// Get a reference to the recharge rate.
|
|
|
|
pub fn recharge_rate(&self) -> &U256 { &self.recharge }
|
|
|
|
|
2016-11-07 15:40:34 +01:00
|
|
|
/// Compute the actual cost of a request, given the kind of request
|
|
|
|
/// and number of requests made.
|
2017-03-23 13:17:05 +01:00
|
|
|
pub fn compute_cost(&self, request: &Request) -> U256 {
|
|
|
|
match *request {
|
|
|
|
Request::Headers(ref req) => self.costs.headers * req.max.into(),
|
|
|
|
Request::HeaderProof(_) => self.costs.header_proof,
|
|
|
|
Request::Body(_) => self.costs.body,
|
|
|
|
Request::Receipts(_) => self.costs.receipts,
|
|
|
|
Request::Account(_) => self.costs.account,
|
|
|
|
Request::Storage(_) => self.costs.storage,
|
|
|
|
Request::Code(_) => self.costs.code,
|
|
|
|
Request::Execution(ref req) => self.costs.transaction_proof * req.gas,
|
2016-11-18 19:12:20 +01:00
|
|
|
}
|
2017-03-23 13:17:05 +01:00
|
|
|
}
|
2016-11-18 19:12:20 +01:00
|
|
|
|
2017-03-23 13:17:05 +01:00
|
|
|
/// Compute the cost of a set of requests.
|
|
|
|
/// This is the base cost plus the cost of each individual request.
|
|
|
|
pub fn compute_cost_multi(&self, requests: &[Request]) -> U256 {
|
|
|
|
requests.iter().fold(self.costs.base, |cost, req| cost + self.compute_cost(req))
|
2016-11-18 19:12:20 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 13:17:05 +01:00
|
|
|
/// Create initial credits.
|
2017-03-08 14:39:44 +01:00
|
|
|
pub fn create_credits(&self) -> Credits {
|
|
|
|
Credits {
|
2016-11-07 15:40:34 +01:00
|
|
|
estimate: self.limit,
|
|
|
|
recharge_point: SteadyTime::now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
/// Recharge the given credits based on time passed since last
|
2016-11-07 15:40:34 +01:00
|
|
|
/// update.
|
2017-03-08 14:39:44 +01:00
|
|
|
pub fn recharge(&self, credits: &mut Credits) {
|
2016-11-07 15:40:34 +01:00
|
|
|
let now = SteadyTime::now();
|
|
|
|
|
|
|
|
// recompute and update only in terms of full seconds elapsed
|
|
|
|
// in order to keep the estimate as an underestimate.
|
2017-03-08 14:39:44 +01:00
|
|
|
let elapsed = (now - credits.recharge_point).num_seconds();
|
|
|
|
credits.recharge_point = credits.recharge_point + Duration::seconds(elapsed);
|
2016-11-07 15:40:34 +01:00
|
|
|
|
|
|
|
let elapsed: U256 = elapsed.into();
|
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
credits.estimate = ::std::cmp::min(self.limit, credits.estimate + (elapsed * self.recharge));
|
2016-11-07 15:40:34 +01:00
|
|
|
}
|
2016-11-15 18:19:16 +01:00
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
/// Refund some credits which were previously deducted.
|
2016-11-15 18:19:16 +01:00
|
|
|
/// Does not update the recharge timestamp.
|
2017-03-08 14:39:44 +01:00
|
|
|
pub fn refund(&self, credits: &mut Credits, refund_amount: U256) {
|
|
|
|
credits.estimate = credits.estimate + refund_amount;
|
2016-11-15 18:19:16 +01:00
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
if credits.estimate > self.limit {
|
|
|
|
credits.estimate = self.limit
|
2016-11-15 18:19:16 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-07 15:40:34 +01:00
|
|
|
}
|
|
|
|
|
2016-12-08 23:21:47 +01:00
|
|
|
impl Default for FlowParams {
|
|
|
|
fn default() -> Self {
|
|
|
|
FlowParams {
|
|
|
|
limit: 50_000_000.into(),
|
|
|
|
costs: CostTable::default(),
|
|
|
|
recharge: 100_000.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 15:40:34 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_serialize_cost_table() {
|
|
|
|
let costs = CostTable::default();
|
|
|
|
let serialized = ::rlp::encode(&costs);
|
|
|
|
|
|
|
|
let new_costs: CostTable = ::rlp::decode(&*serialized);
|
|
|
|
|
|
|
|
assert_eq!(costs, new_costs);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-03-08 14:39:44 +01:00
|
|
|
fn credits_mechanism() {
|
2016-11-07 15:40:34 +01:00
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2016-11-08 16:57:10 +01:00
|
|
|
let flow_params = FlowParams::new(100.into(), Default::default(), 20.into());
|
2017-03-08 14:39:44 +01:00
|
|
|
let mut credits = flow_params.create_credits();
|
2016-11-07 15:40:34 +01:00
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
assert!(credits.deduct_cost(101.into()).is_err());
|
|
|
|
assert!(credits.deduct_cost(10.into()).is_ok());
|
2016-11-07 15:40:34 +01:00
|
|
|
|
|
|
|
thread::sleep(Duration::from_secs(1));
|
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
flow_params.recharge(&mut credits);
|
2016-11-07 15:40:34 +01:00
|
|
|
|
2017-03-08 14:39:44 +01:00
|
|
|
assert_eq!(credits.estimate, 100.into());
|
2016-11-07 15:40:34 +01:00
|
|
|
}
|
2017-01-11 14:39:03 +01:00
|
|
|
}
|