Skip to content

Commit

Permalink
fix: get_fee_rate_statics compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Dec 8, 2022
1 parent aa6380d commit 2601e28
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
9 changes: 6 additions & 3 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,10 @@ pub trait ChainRpc {
///
/// ## Returns
///
/// If the query has data records, it returns statistics, if not, it returns null.
/// If the query finds the corresponding historical data,
/// the corresponding statistics are returned,
/// containing the mean and median, in shannons per kilo-weight.
/// If not, it returns null.
///
/// ## Examples
///
Expand All @@ -1520,8 +1523,8 @@ pub trait ChainRpc {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "result": {
/// "mean":59.29387293275573,
/// "median":5.288207297726071
/// "mean": 59293,
/// "median": 5288
/// }
/// }
/// ```
Expand Down
16 changes: 8 additions & 8 deletions rpc/src/tests/fee_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,35 @@ fn test_fee_rate_statics() {
assert_eq!(
statistics,
Some(FeeRateStatics {
mean: 11.0,
median: 11.0
mean: 11_000,
median: 11_000,
})
);

let statistics = FeeRateCollector::new(&provider).statistics(Some(9));
assert_eq!(
statistics,
Some(FeeRateStatics {
mean: 17.0,
median: 17.0
mean: 17_000,
median: 17_000
})
);

let statistics = FeeRateCollector::new(&provider).statistics(Some(30));
assert_eq!(
statistics,
Some(FeeRateStatics {
mean: 11.0,
median: 11.0
mean: 11_000,
median: 11_000
})
);

let statistics = FeeRateCollector::new(&provider).statistics(Some(0));
assert_eq!(
statistics,
Some(FeeRateStatics {
mean: 21.0,
median: 21.0
mean: 21_000,
median: 21_000
})
);
}
20 changes: 10 additions & 10 deletions rpc/src/util/fee_rate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ckb_jsonrpc_types::FeeRateStatics;
use ckb_shared::Snapshot;
use ckb_store::ChainStore;
use ckb_types::core::{tx_pool::get_transaction_weight, BlockExt, BlockNumber};
use ckb_types::core::{tx_pool::get_transaction_weight, BlockExt, BlockNumber, FeeRate};

const DEFAULT_TARGET: u64 = 21;
const MIN_TARGET: u64 = 1;
Expand All @@ -11,16 +11,16 @@ fn is_even(n: u64) -> bool {
n & 1 == 0
}

fn mean(numbers: &[f64]) -> f64 {
let sum: f64 = numbers.iter().sum();
sum / numbers.len() as f64
fn mean(numbers: &[u64]) -> u64 {
let sum: u64 = numbers.iter().sum();
sum / numbers.len() as u64
}

fn median(numbers: &mut [f64]) -> f64 {
numbers.sort_unstable_by(|a, b| a.partial_cmp(b).expect("slice does not contain NaN"));
fn median(numbers: &mut [u64]) -> u64 {
numbers.sort_unstable();
let mid = numbers.len() / 2;
if numbers.len() % 2 == 0 {
mean(&[numbers[mid - 1], numbers[mid]]) as f64
mean(&[numbers[mid - 1], numbers[mid]])
} else {
numbers[mid]
}
Expand All @@ -30,9 +30,9 @@ pub(crate) trait FeeRateProvider {
fn get_tip_number(&self) -> BlockNumber;
fn get_block_ext_by_number(&self, number: BlockNumber) -> Option<BlockExt>;

fn collect<F>(&self, target: u64, f: F) -> Vec<f64>
fn collect<F>(&self, target: u64, f: F) -> Vec<u64>
where
F: FnMut(Vec<f64>, BlockExt) -> Vec<f64>,
F: FnMut(Vec<u64>, BlockExt) -> Vec<u64>,
{
let tip_number = self.get_tip_number();
let start = std::cmp::max(
Expand Down Expand Up @@ -88,7 +88,7 @@ where
) {
let weight = get_transaction_weight(size as usize, cycles);
if weight > 0 {
fee_rates.push(fee.as_u64() as f64 / weight as f64);
fee_rates.push(FeeRate::calculate(fee, weight).as_u64());
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion tx-pool/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ pub(crate) fn check_tx_fee(
let fee = DaoCalculator::new(snapshot.consensus(), &snapshot.as_data_provider())
.transaction_fee(rtx)
.map_err(|err| Reject::Malformed(format!("{}", err)))?;
let min_fee = tx_pool.config.min_fee_rate.fee(tx_size);
// Theoretically we cannot use size as weight directly to calculate fee_rate,
// here min fee rate is used as a cheap check,
// so we will use size to calculate fee_rate directly
let min_fee = tx_pool.config.min_fee_rate.fee(tx_size as u64);
// reject txs which fee lower than min fee rate
if fee < min_fee {
let reject =
Expand Down
6 changes: 3 additions & 3 deletions util/jsonrpc-types/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,11 +1398,11 @@ impl HardForkFeature {
}
}

/// The fee_rate statistics information, includes mean and median
/// The fee_rate statistics information, includes mean and median, unit: shannons per kilo-weight
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub struct FeeRateStatics {
/// mean
pub mean: f64,
pub mean: u64,
/// median
pub median: f64,
pub median: u64,
}
16 changes: 8 additions & 8 deletions util/types/src/core/fee_rate.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use crate::core::Capacity;

/// shannons per kilobytes
/// shannons per kilo-weight
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct FeeRate(pub u64);

const KB: u64 = 1000;
const KW: u64 = 1000;

impl FeeRate {
/// TODO(doc): @doitian
pub fn calculate(fee: Capacity, weight: usize) -> Self {
pub fn calculate(fee: Capacity, weight: u64) -> Self {
if weight == 0 {
return FeeRate::zero();
}
FeeRate::from_u64(fee.as_u64().saturating_mul(KB) / (weight as u64))
FeeRate::from_u64(fee.as_u64().saturating_mul(KW) / weight)
}

/// TODO(doc): @doitian
pub const fn from_u64(fee_per_kb: u64) -> Self {
FeeRate(fee_per_kb)
pub const fn from_u64(fee_per_kw: u64) -> Self {
FeeRate(fee_per_kw)
}

/// TODO(doc): @doitian
Expand All @@ -31,8 +31,8 @@ impl FeeRate {
}

/// TODO(doc): @doitian
pub fn fee(self, size: usize) -> Capacity {
let fee = self.0.saturating_mul(size as u64) / KB;
pub fn fee(self, weight: u64) -> Capacity {
let fee = self.0.saturating_mul(weight) / KW;
Capacity::shannons(fee)
}
}
Expand Down

0 comments on commit 2601e28

Please sign in to comment.