Skip to content

Commit

Permalink
test: fee_rate collector
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Nov 2, 2022
1 parent f45c62f commit 3d4e8f1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
88 changes: 88 additions & 0 deletions rpc/src/tests/fee_rate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use crate::util::{FeeRateCollector, FeeRateProvider};
use ckb_jsonrpc_types::FeeRateStatics;
use ckb_types::core::{BlockExt, BlockNumber, Capacity};
use std::collections::HashMap;

struct DummyFeeRateProvider {
tip_number: BlockNumber,
block_exts: HashMap<BlockNumber, BlockExt>,
}

impl DummyFeeRateProvider {
pub fn new() -> DummyFeeRateProvider {
DummyFeeRateProvider {
tip_number: 0,
block_exts: HashMap::new(),
}
}

pub fn append(&mut self, number: BlockNumber, ext: BlockExt) {
if number > self.tip_number {
self.tip_number = number;
}
self.block_exts.insert(number, ext);
}
}

impl FeeRateProvider for DummyFeeRateProvider {
fn get_tip_number(&self) -> BlockNumber {
self.tip_number
}

fn get_block_ext_by_number(&self, number: BlockNumber) -> Option<BlockExt> {
self.block_exts.get(&number).cloned()
}
}

#[test]
fn test_fee_rate_statics() {
let mut provider = DummyFeeRateProvider::new();
for i in 0..=21 {
let ext = BlockExt {
received_at: 0,
total_difficulty: 0u64.into(),
total_uncles_count: 0,
verified: None,
txs_fees: vec![Capacity::shannons(i * i * 100)],
cycles: Some(vec![i * 100]),
txs_sizes: Some(vec![i * 100]),
};
provider.append(i, ext);
}

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

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

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

let statistics = FeeRateCollector::new(&provider).statistics(Some(0));
assert_eq!(
statistics,
Some(FeeRateStatics {
mean: 21.0,
median: 21.0
})
);
}
1 change: 1 addition & 0 deletions rpc/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::{cmp, collections::HashSet, fmt, sync::Arc};

mod error;
mod examples;
mod fee_rate;
mod module;

#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq, Default)]
Expand Down
3 changes: 3 additions & 0 deletions rpc/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub(crate) mod fee_rate;

pub(crate) use fee_rate::FeeRateCollector;

#[cfg(test)]
pub(crate) use fee_rate::FeeRateProvider;
2 changes: 1 addition & 1 deletion util/jsonrpc-types/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ impl HardForkFeature {
}

/// The fee_rate statistics information, includes mean and median
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub struct FeeRateStatics {
/// mean
pub mean: f64,
Expand Down

0 comments on commit 3d4e8f1

Please sign in to comment.