Skip to content

Commit 7075241

Browse files
committed
feat: feat: Add total recent reject num to Terminal RPC txpool.
1 parent 7abc17b commit 7075241

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

rpc/src/module/terminal.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,17 @@ impl TerminalRpcImpl {
163163
RPCError::from_any_error(err)
164164
})?;
165165

166+
let total_recent_reject_num = tx_pool.get_total_recent_reject_num().map_err(|err| {
167+
error!("Get_total_recent_reject_num result error {}", err);
168+
RPCError::from_any_error(err)
169+
})?;
170+
166171
let tx_pool_info = TerminalPoolInfo {
167172
pending: (info.pending_size as u64).into(),
168173
proposed: (info.proposed_size as u64).into(),
169174
orphan: (info.orphan_size as u64).into(),
170175
committing: (block_template.transactions.len() as u64).into(),
176+
total_recent_reject_num: total_recent_reject_num.unwrap_or(0).into(),
171177
total_tx_size: (info.total_tx_size as u64).into(),
172178
total_tx_cycles: info.total_tx_cycles.into(),
173179
last_txs_updated_at: info.last_txs_updated_at.into(),

tx-pool/src/component/recent_reject.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,25 @@ impl RecentReject {
7777
Ok(ret.map(|bytes| unsafe { String::from_utf8_unchecked(bytes.to_vec()) }))
7878
}
7979

80+
pub fn get_estimate_total_keys_num(&self) -> u64 {
81+
self.total_keys_num
82+
}
83+
84+
fn estimate_total_keys_num(&self) -> Result<u64, AnyError> {
85+
let estimate_keys_num = (0..self.shard_num)
86+
.map(|num| self.db.estimate_num_keys_cf(&num.to_string()))
87+
.collect::<Result<Vec<_>, _>>()?;
88+
89+
Ok(estimate_keys_num.iter().map(|num| num.unwrap_or(0)).sum())
90+
}
91+
8092
fn shrink(&mut self) -> Result<u64, AnyError> {
8193
let mut rng = thread_rng();
8294
let shard = rng.sample(Uniform::new(0, self.shard_num)).to_string();
8395
self.db.drop_cf(&shard)?;
8496
self.db.create_cf_with_ttl(&shard, self.ttl)?;
8597

86-
let estimate_keys_num = (0..self.shard_num)
87-
.map(|num| self.db.estimate_num_keys_cf(&num.to_string()))
88-
.collect::<Result<Vec<_>, _>>()?;
89-
90-
let total_keys_num = estimate_keys_num.iter().map(|num| num.unwrap_or(0)).sum();
98+
let total_keys_num = self.estimate_total_keys_num()?;
9199
self.total_keys_num = total_keys_num;
92100
Ok(total_keys_num)
93101
}

tx-pool/src/service.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pub(crate) enum Message {
118118
GetAllIds(Request<(), TxPoolIds>),
119119
SavePool(Request<(), ()>),
120120
GetPoolTxDetails(Request<Byte32, PoolTxDetailInfo>),
121+
GetTotalRecentRejectNum(Request<(), Option<u64>>),
121122

122123
UpdateIBDState(Request<bool, ()>),
123124
EstimateFeeRate(Request<(EstimateMode, bool), FeeEstimatesResult>),
@@ -425,6 +426,11 @@ impl TxPoolController {
425426
pub fn submit_local_test_tx(&self, tx: TransactionView) -> Result<SubmitTxResult, AnyError> {
426427
send_message!(self, SubmitLocalTestTx, tx)
427428
}
429+
430+
/// get total recent reject num
431+
pub fn get_total_recent_reject_num(&self) -> Result<Option<u64>, AnyError> {
432+
send_message!(self, GetTotalRecentRejectNum, ())
433+
}
428434
}
429435

430436
/// A builder used to create TxPoolService.
@@ -1024,6 +1030,12 @@ async fn process(mut service: TxPoolService, message: Message) {
10241030
error!("Responder sending plug_entry failed {:?}", e);
10251031
};
10261032
}
1033+
Message::GetTotalRecentRejectNum(Request { responder, .. }) => {
1034+
let total_recent_reject_num = service.get_total_recent_reject_num().await;
1035+
if let Err(e) = responder.send(total_recent_reject_num) {
1036+
error!("Responder sending total_recent_reject_num failed {:?}", e)
1037+
};
1038+
}
10271039
}
10281040
}
10291041

@@ -1051,6 +1063,14 @@ impl TxPoolService {
10511063
}
10521064
}
10531065

1066+
async fn get_total_recent_reject_num(&self) -> Option<u64> {
1067+
let tx_pool = self.tx_pool.read().await;
1068+
tx_pool
1069+
.recent_reject
1070+
.as_ref()
1071+
.map(|r| r.get_estimate_total_keys_num())
1072+
}
1073+
10541074
/// Get Live Cell Status
10551075
async fn get_live_cell(&self, out_point: OutPoint, eager_load: bool) -> CellStatus {
10561076
let tx_pool = self.tx_pool.read().await;

util/jsonrpc-types/src/terminal.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ pub struct TerminalPoolInfo {
125125
/// The Committing transactions refer to transactions that have been packaged into the
126126
/// block_template and are awaiting mining into a block.
127127
pub committing: Uint64,
128+
/// Total count of recent reject transactions by pool
129+
pub total_recent_reject_num: Uint64,
128130
/// Total count of transactions in the pool of all the different kinds of states (excluding orphan transactions).
129131
pub total_tx_size: Uint64,
130132
/// Total consumed VM cycles of all the transactions in the pool (excluding orphan transactions).

0 commit comments

Comments
 (0)