Skip to content

Commit

Permalink
perf: remove fee estimator
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Jun 16, 2020
1 parent d909cde commit 317f8d1
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 232 deletions.
42 changes: 7 additions & 35 deletions rpc/src/module/experiment.rs
@@ -1,10 +1,8 @@
use crate::error::RPCError;
use ckb_dao::DaoCalculator;
use ckb_fee_estimator::MAX_CONFIRM_BLOCKS;
use ckb_jsonrpc_types::{
Capacity, DryRunResult, EstimateResult, OutPoint, Script, Transaction, Uint64,
};
use ckb_logger::error;
use ckb_shared::{shared::Shared, Snapshot};
use ckb_store::ChainStore;
use ckb_types::{
Expand All @@ -14,7 +12,7 @@ use ckb_types::{
H256,
};
use ckb_verification::ScriptVerifier;
use jsonrpc_core::{Error, Result};
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use std::collections::HashSet;

Expand Down Expand Up @@ -70,38 +68,12 @@ impl ExperimentRpc for ExperimentRpcImpl {
}
}

fn estimate_fee_rate(&self, expect_confirm_blocks: Uint64) -> Result<EstimateResult> {
let expect_confirm_blocks = expect_confirm_blocks.value() as usize;
// A tx need 1 block to propose, then 2 block to get confirmed
// so at least confirm blocks is 3 blocks.
if expect_confirm_blocks < 3 || expect_confirm_blocks > MAX_CONFIRM_BLOCKS {
return Err(RPCError::custom(
RPCError::Invalid,
format!(
"expect_confirm_blocks should between 3 and {}, got {}",
MAX_CONFIRM_BLOCKS, expect_confirm_blocks
),
));
}

let tx_pool = self.shared.tx_pool_controller();
let fee_rate = tx_pool.estimate_fee_rate(expect_confirm_blocks);
if let Err(e) = fee_rate {
error!("send estimate_fee_rate request error {}", e);
return Err(Error::internal_error());
};
let fee_rate = fee_rate.unwrap();

if fee_rate.as_u64() == 0 {
return Err(RPCError::custom(
RPCError::Invalid,
"collected samples are not enough, please make sure the node has peers and try later"
.into(),
));
}
Ok(EstimateResult {
fee_rate: fee_rate.as_u64().into(),
})
fn estimate_fee_rate(&self, _expect_confirm_blocks: Uint64) -> Result<EstimateResult> {
Err(RPCError::custom(
RPCError::Invalid,
"estimate_fee_rate have been deprecated due to it has availability and performance issue"
.into(),
))
}
}

Expand Down
1 change: 0 additions & 1 deletion test/src/main.rs
Expand Up @@ -316,7 +316,6 @@ fn all_specs() -> SpecMap {
Box::new(SendLargeCyclesTxToRelay::new()),
Box::new(TxsRelayOrder),
Box::new(SendArrowTxs),
Box::new(FeeEstimate),
Box::new(DifferentTxsWithSameInput),
Box::new(CompactBlockEmpty),
Box::new(CompactBlockEmptyParentUnknown),
Expand Down
140 changes: 0 additions & 140 deletions test/src/specs/tx_pool/fee_estimate.rs

This file was deleted.

2 changes: 0 additions & 2 deletions test/src/specs/tx_pool/mod.rs
Expand Up @@ -3,7 +3,6 @@ mod collision;
mod depend_tx_in_same_block;
mod descendant;
mod different_txs_with_same_input;
mod fee_estimate;
mod limit;
mod pool_reconcile;
mod pool_resurrect;
Expand All @@ -25,7 +24,6 @@ pub use collision::*;
pub use depend_tx_in_same_block::*;
pub use descendant::*;
pub use different_txs_with_same_input::*;
pub use fee_estimate::*;
pub use limit::*;
pub use pool_reconcile::*;
pub use pool_resurrect::*;
Expand Down
17 changes: 3 additions & 14 deletions tx-pool/src/pool.rs
Expand Up @@ -7,7 +7,6 @@ use crate::error::SubmitTxError;
use ckb_app_config::TxPoolConfig;
use ckb_dao::DaoCalculator;
use ckb_error::{Error, ErrorKind, InternalErrorKind};
use ckb_fee_estimator::Estimator as FeeEstimator;
use ckb_logger::{debug_target, error_target, trace_target};
use ckb_snapshot::Snapshot;
use ckb_store::ChainStore;
Expand Down Expand Up @@ -51,8 +50,6 @@ pub struct TxPool {
pub(crate) total_tx_size: usize,
// sum of all tx_pool tx's cycles.
pub(crate) total_tx_cycles: Cycle,
// tx fee estimator
pub(crate) fee_estimator: FeeEstimator,
pub snapshot: Arc<Snapshot>,
}

Expand Down Expand Up @@ -87,7 +84,6 @@ impl TxPool {
total_tx_size: 0,
total_tx_cycles: 0,
snapshot,
fee_estimator: FeeEstimator::default(),
}
}

Expand Down Expand Up @@ -683,38 +679,31 @@ impl TxPool {
) -> Option<(Byte32, CacheEntry)> {
let mut ret = None;
let tx_hash = tx.hash();
let mut readd_tx = false;
let cache_entry = txs_verify_cache.get(&tx_hash).cloned();
let tx_short_id = tx.proposal_short_id();
let tx_size = tx.data().serialized_size_in_block();
if snapshot.proposals().contains_proposed(&tx_short_id) {
if let Ok(new_cache_entry) = self.proposed_tx_and_descendants(cache_entry, tx_size, tx)
{
if cache_entry.is_none() {
ret = Some((tx_hash.clone(), new_cache_entry));
ret = Some((tx_hash, new_cache_entry));
}
self.update_statics_for_add_tx(tx_size, new_cache_entry.cycles);
readd_tx = true;
}
} else if snapshot.proposals().contains_gap(&tx_short_id) {
if let Ok(new_cache_entry) = self.gap_tx(cache_entry, tx_size, tx) {
if cache_entry.is_none() {
ret = Some((tx_hash.clone(), new_cache_entry));
ret = Some((tx_hash, new_cache_entry));
}
self.update_statics_for_add_tx(tx_size, cache_entry.map(|c| c.cycles).unwrap_or(0));
readd_tx = true;
}
} else if let Ok(new_cache_entry) = self.pending_tx(cache_entry, tx_size, tx) {
if cache_entry.is_none() {
ret = Some((tx_hash.clone(), new_cache_entry));
ret = Some((tx_hash, new_cache_entry));
}
self.update_statics_for_add_tx(tx_size, cache_entry.map(|c| c.cycles).unwrap_or(0));
readd_tx = true;
}

if !readd_tx {
self.fee_estimator.drop_tx(&tx_hash);
}
ret
}

Expand Down
16 changes: 1 addition & 15 deletions tx-pool/src/process.rs
Expand Up @@ -7,7 +7,6 @@ use crate::service::TxPoolService;
use ckb_app_config::BlockAssemblerConfig;
use ckb_dao::DaoCalculator;
use ckb_error::{Error, InternalErrorKind};
use ckb_fee_estimator::FeeRate;
use ckb_jsonrpc_types::BlockTemplate;
use ckb_logger::{debug_target, info};
use ckb_snapshot::Snapshot;
Expand Down Expand Up @@ -414,16 +413,7 @@ impl TxPoolService {
related_dep_out_points,
);
let inserted = match status {
TxStatus::Fresh => {
let tx_hash = entry.transaction.hash();
let inserted = tx_pool.add_pending(entry)?;
if inserted {
let height = tx_pool.snapshot().tip_number();
let fee_rate = FeeRate::calculate(fee, tx_size);
tx_pool.fee_estimator.track_tx(tx_hash, fee_rate, height);
}
inserted
}
TxStatus::Fresh => tx_pool.add_pending(entry)?,
TxStatus::Gap => tx_pool.add_gap(entry)?,
TxStatus::Proposed => tx_pool.add_proposed(entry)?,
};
Expand Down Expand Up @@ -640,10 +630,6 @@ fn _update_tx_pool_for_reorg(

for blk in attached_blocks {
attached.extend(blk.transactions().iter().skip(1).cloned());
tx_pool.fee_estimator.process_block(
blk.header().number(),
blk.transactions().iter().skip(1).map(|tx| tx.hash()),
);
}

let retain: Vec<TransactionView> = detached.difference(&attached).cloned().collect();
Expand Down
25 changes: 0 additions & 25 deletions tx-pool/src/service.rs
Expand Up @@ -6,7 +6,6 @@ use crate::process::PlugTarget;
use ckb_app_config::{BlockAssemblerConfig, TxPoolConfig};
use ckb_async_runtime::{new_runtime, Handle};
use ckb_error::Error;
use ckb_fee_estimator::FeeRate;
use ckb_jsonrpc_types::BlockTemplate;
use ckb_logger::error;
use ckb_snapshot::{Snapshot, SnapshotMgr};
Expand Down Expand Up @@ -78,7 +77,6 @@ pub enum Message {
FetchTxRPC(Request<ProposalShortId, Option<(bool, TransactionView)>>),
NewUncle(Notify<UncleBlockView>),
PlugEntry(Request<(Vec<TxEntry>, PlugTarget), ()>),
EstimateFeeRate(Request<usize, FeeRate>),
}

#[derive(Clone)]
Expand Down Expand Up @@ -254,19 +252,6 @@ impl TxPoolController {
})?;
response.recv().map_err(Into::into)
}

pub fn estimate_fee_rate(&self, expect_confirm_blocks: usize) -> Result<FeeRate, FailureError> {
let mut sender = self.sender.clone();
let (responder, response) = crossbeam_channel::bounded(1);
let request = Request::call(expect_confirm_blocks, responder);
sender
.try_send(Message::EstimateFeeRate(request))
.map_err(|e| {
let (_m, e) = handle_try_send_error(e);
e
})?;
response.recv().map_err(Into::into)
}
}

pub struct TxPoolServiceBuilder {
Expand Down Expand Up @@ -500,15 +485,5 @@ async fn process(service: TxPoolService, message: Message) {
error!("responder send plug_entry failed {:?}", e);
};
}
Message::EstimateFeeRate(Request {
responder,
arguments: expect_confirm_blocks,
}) => {
let tx_pool = service.tx_pool.read().await;
let fee_rate = tx_pool.fee_estimator.estimate(expect_confirm_blocks);
if let Err(e) = responder.send(fee_rate) {
error!("responder send estimate_fee_rate failed {:?}", e)
};
}
}
}

0 comments on commit 317f8d1

Please sign in to comment.