Skip to content

Commit

Permalink
feat: RPC tx_pool_info add tx_size_limit and max_tx_pool_size
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad committed Mar 23, 2023
1 parent 72918fb commit 98678a8
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 65 deletions.
12 changes: 10 additions & 2 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4435,14 +4435,16 @@ Response
"jsonrpc": "2.0",
"result": {
"last_txs_updated_at": "0x0",
"min_fee_rate": "0x0",
"min_fee_rate": "0x3e8",
"max_tx_pool_size": "0xaba9500",
"orphan": "0x0",
"pending": "0x1",
"proposed": "0x0",
"tip_hash": "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40",
"tip_number": "0x400",
"total_tx_cycles": "0x219",
"total_tx_size": "0x112"
"total_tx_size": "0x112",
"tx_size_limit": "0x7d000"
}
}
```
Expand Down Expand Up @@ -6916,6 +6918,12 @@ Transaction pool information.

* `last_txs_updated_at`: [`Timestamp`](#type-timestamp) - Last updated time. This is the Unix timestamp in milliseconds.

* `tx_size_limit`: [`Uint64`](#type-uint64) - Limiting transactions to tx_size_limit

Transactions with a large size close to the block size limit may not be packaged, because the block header and cellbase are occupied, so the tx-pool is limited to accepting transaction up to tx_size_limit.

* `max_tx_pool_size`: [`Uint64`](#type-uint64) - Total limit on the size of transactions in the tx-pool


### Type `TxStatus`

Expand Down
21 changes: 5 additions & 16 deletions rpc/src/module/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,16 @@ pub trait PoolRpc {
/// "jsonrpc": "2.0",
/// "result": {
/// "last_txs_updated_at": "0x0",
/// "min_fee_rate": "0x0",
/// "min_fee_rate": "0x3e8",
/// "max_tx_pool_size": "0xaba9500",
/// "orphan": "0x0",
/// "pending": "0x1",
/// "proposed": "0x0",
/// "tip_hash": "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40",
/// "tip_number": "0x400",
/// "total_tx_cycles": "0x219",
/// "total_tx_size": "0x112"
/// "total_tx_size": "0x112",
/// "tx_size_limit": "0x7d000"
/// }
/// }
/// ```
Expand Down Expand Up @@ -276,15 +278,13 @@ pub trait PoolRpc {

pub(crate) struct PoolRpcImpl {
shared: Shared,
min_fee_rate: core::FeeRate,
well_known_lock_scripts: Vec<packed::Script>,
well_known_type_scripts: Vec<packed::Script>,
}

impl PoolRpcImpl {
pub fn new(
shared: Shared,
min_fee_rate: core::FeeRate,
mut extra_well_known_lock_scripts: Vec<packed::Script>,
mut extra_well_known_type_scripts: Vec<packed::Script>,
) -> PoolRpcImpl {
Expand All @@ -298,7 +298,6 @@ impl PoolRpcImpl {

PoolRpcImpl {
shared,
min_fee_rate,
well_known_lock_scripts,
well_known_type_scripts,
}
Expand Down Expand Up @@ -449,17 +448,7 @@ impl PoolRpc for PoolRpcImpl {

let tx_pool_info = get_tx_pool_info.unwrap();

Ok(TxPoolInfo {
tip_hash: tx_pool_info.tip_hash.unpack(),
tip_number: tx_pool_info.tip_number.into(),
pending: (tx_pool_info.pending_size as u64).into(),
proposed: (tx_pool_info.proposed_size as u64).into(),
orphan: (tx_pool_info.orphan_size as u64).into(),
total_tx_size: (tx_pool_info.total_tx_size as u64).into(),
total_tx_cycles: tx_pool_info.total_tx_cycles.into(),
min_fee_rate: self.min_fee_rate.as_u64().into(),
last_txs_updated_at: tx_pool_info.last_txs_updated_at.into(),
})
Ok(tx_pool_info.into())
}

fn clear_tx_pool(&self) -> Result<()> {
Expand Down
4 changes: 1 addition & 3 deletions rpc/src/service_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ckb_network_alert::{notifier::Notifier as AlertNotifier, verifier::Verifier
use ckb_pow::Pow;
use ckb_shared::shared::Shared;
use ckb_sync::SyncShared;
use ckb_types::{core::FeeRate, packed::Script};
use ckb_types::packed::Script;
use ckb_util::Mutex;
use jsonrpc_core::RemoteProcedure;
use std::sync::Arc;
Expand Down Expand Up @@ -52,13 +52,11 @@ impl<'a> ServiceBuilder<'a> {
pub fn enable_pool(
mut self,
shared: Shared,
min_fee_rate: FeeRate,
extra_well_known_lock_scripts: Vec<Script>,
extra_well_known_type_scripts: Vec<Script>,
) -> Self {
let rpc_methods = PoolRpcImpl::new(
shared,
min_fee_rate,
extra_well_known_lock_scripts,
extra_well_known_type_scripts,
)
Expand Down
6 changes: 3 additions & 3 deletions rpc/src/tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use ckb_sync::SyncShared;
use ckb_test_chain_utils::always_success_cell;
use ckb_types::{
core::{
capacity_bytes, BlockBuilder, Capacity, EpochNumberWithFraction, FeeRate, Ratio,
TransactionBuilder, TransactionView,
capacity_bytes, BlockBuilder, Capacity, EpochNumberWithFraction, Ratio, TransactionBuilder,
TransactionView,
},
h256,
packed::{AlertBuilder, CellDep, CellInput, CellOutputBuilder, OutPoint, RawAlertBuilder},
Expand Down Expand Up @@ -244,7 +244,7 @@ fn setup_rpc_test_suite(height: u64) -> RpcTestSuite {

let builder = ServiceBuilder::new(&rpc_config)
.enable_chain(shared.clone())
.enable_pool(shared.clone(), FeeRate::zero(), vec![], vec![])
.enable_pool(shared.clone(), vec![], vec![])
.enable_miner(
shared.clone(),
network_controller.clone(),
Expand Down
6 changes: 3 additions & 3 deletions rpc/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use ckb_test_chain_utils::{
};
use ckb_types::{
core::{
cell::resolve_transaction, BlockBuilder, BlockView, FeeRate, HeaderView,
TransactionBuilder, TransactionView,
cell::resolve_transaction, BlockBuilder, BlockView, HeaderView, TransactionBuilder,
TransactionView,
},
h256,
packed::{CellInput, OutPoint},
Expand Down Expand Up @@ -290,7 +290,7 @@ fn setup() -> RpcTestSuite {

let builder = ServiceBuilder::new(&rpc_config)
.enable_chain(shared.clone())
.enable_pool(shared.clone(), FeeRate::zero(), vec![], vec![])
.enable_pool(shared.clone(), vec![], vec![])
.enable_miner(
shared.clone(),
network_controller.clone(),
Expand Down
33 changes: 0 additions & 33 deletions tx-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use ckb_app_config::TxPoolConfig;
use ckb_logger::{debug, error, trace, warn};
use ckb_snapshot::Snapshot;
use ckb_store::ChainStore;
use ckb_types::core::BlockNumber;
use ckb_types::{
core::{
cell::{resolve_transaction, OverlayCellChecker, OverlayCellProvider, ResolvedTransaction},
Expand Down Expand Up @@ -75,38 +74,6 @@ pub struct TxPool {
pub(crate) expiry: u64,
}

/// Transaction pool information.
#[derive(Clone, Debug)]
pub struct TxPoolInfo {
/// The associated chain tip block hash.
///
/// Transaction pool is stateful. It manages the transactions which are valid to be commit
/// after this block.
pub tip_hash: Byte32,
/// The block number of the block `tip_hash`.
pub tip_number: BlockNumber,
/// Count of transactions in the pending state.
///
/// The pending transactions must be proposed in a new block first.
pub pending_size: usize,
/// Count of transactions in the proposed state.
///
/// The proposed transactions are ready to be commit in the new block after the block
/// `tip_hash`.
pub proposed_size: usize,
/// Count of orphan transactions.
///
/// An orphan transaction has an input cell from the transaction which is neither in the chain
/// nor in the transaction pool.
pub orphan_size: usize,
/// Total count of transactions in the pool of all the different kinds of states.
pub total_tx_size: usize,
/// Total consumed VM cycles of all the transactions in the pool.
pub total_tx_cycles: Cycle,
/// Last updated time. This is the Unix timestamp in milliseconds.
pub last_txs_updated_at: u64,
}

impl TxPool {
/// Create new TxPool
pub fn new(config: TxPoolConfig, snapshot: Arc<Snapshot>) -> TxPool {
Expand Down
7 changes: 5 additions & 2 deletions tx-pool/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::callback::{Callback, Callbacks, ProposedCallback, RejectCallback};
use crate::chunk_process::ChunkCommand;
use crate::component::{chunk::ChunkQueue, orphan::OrphanPool};
use crate::error::{handle_recv_error, handle_send_cmd_error, handle_try_send_error};
use crate::pool::{TxPool, TxPoolInfo};
use crate::pool::TxPool;
use ckb_app_config::{BlockAssemblerConfig, TxPoolConfig};
use ckb_async_runtime::Handle;
use ckb_chain_spec::consensus::Consensus;
Expand All @@ -20,7 +20,7 @@ use ckb_stop_handler::{SignalSender, StopHandler, WATCH_INIT};
use ckb_types::core::tx_pool::{TransactionWithStatus, TxStatus};
use ckb_types::{
core::{
tx_pool::{Reject, TxPoolEntryInfo, TxPoolIds},
tx_pool::{Reject, TxPoolEntryInfo, TxPoolIds, TxPoolInfo, TRANSACTION_SIZE_LIMIT},
BlockView, Cycle, TransactionView, UncleBlockView, Version,
},
packed::{Byte32, ProposalShortId},
Expand Down Expand Up @@ -1035,7 +1035,10 @@ impl TxPoolService {
orphan_size: orphan.len(),
total_tx_size: tx_pool.total_tx_size,
total_tx_cycles: tx_pool.total_tx_cycles,
min_fee_rate: self.tx_pool_config.min_fee_rate,
last_txs_updated_at: 0,
tx_size_limit: TRANSACTION_SIZE_LIMIT,
max_tx_pool_size: self.tx_pool_config.max_tx_pool_size as u64,
}
}

Expand Down
30 changes: 29 additions & 1 deletion util/jsonrpc-types/src/pool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{BlockNumber, Capacity, Cycle, Timestamp, TransactionView, Uint64};
use ckb_types::core::service::PoolTransactionEntry as CorePoolTransactionEntry;
use ckb_types::core::tx_pool::{Reject, TxEntryInfo, TxPoolEntryInfo, TxPoolIds as CoreTxPoolIds};
use ckb_types::core::tx_pool::{
Reject, TxEntryInfo, TxPoolEntryInfo, TxPoolIds as CoreTxPoolIds, TxPoolInfo as CoreTxPoolInfo,
};
use ckb_types::prelude::Unpack;
use ckb_types::H256;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -40,6 +42,32 @@ pub struct TxPoolInfo {
pub min_fee_rate: Uint64,
/// Last updated time. This is the Unix timestamp in milliseconds.
pub last_txs_updated_at: Timestamp,
/// Limiting transactions to tx_size_limit
///
/// Transactions with a large size close to the block size limit may not be packaged,
/// because the block header and cellbase are occupied,
/// so the tx-pool is limited to accepting transaction up to tx_size_limit.
pub tx_size_limit: Uint64,
/// Total limit on the size of transactions in the tx-pool
pub max_tx_pool_size: Uint64,
}

impl From<CoreTxPoolInfo> for TxPoolInfo {
fn from(tx_pool_info: CoreTxPoolInfo) -> Self {
TxPoolInfo {
tip_hash: tx_pool_info.tip_hash.unpack(),
tip_number: tx_pool_info.tip_number.into(),
pending: (tx_pool_info.pending_size as u64).into(),
proposed: (tx_pool_info.proposed_size as u64).into(),
orphan: (tx_pool_info.orphan_size as u64).into(),
total_tx_size: (tx_pool_info.total_tx_size as u64).into(),
total_tx_cycles: tx_pool_info.total_tx_cycles.into(),
min_fee_rate: tx_pool_info.min_fee_rate.as_u64().into(),
last_txs_updated_at: tx_pool_info.last_txs_updated_at.into(),
tx_size_limit: tx_pool_info.tx_size_limit.into(),
max_tx_pool_size: tx_pool_info.max_tx_pool_size.into(),
}
}
}

/// The transaction entry in the pool.
Expand Down
1 change: 0 additions & 1 deletion util/launcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ impl Launcher {
.enable_chain(shared.clone())
.enable_pool(
shared.clone(),
self.args.config.tx_pool.min_fee_rate,
rpc_config
.extra_well_known_lock_scripts
.iter()
Expand Down
46 changes: 45 additions & 1 deletion util/types/src/core/tx_pool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tx-pool shared type define.
use crate::core::{
error::{OutPointError, TransactionError},
Capacity, Cycle, FeeRate,
BlockNumber, Capacity, Cycle, FeeRate,
};
use crate::packed::Byte32;
use crate::{core, H256};
Expand Down Expand Up @@ -262,3 +262,47 @@ pub fn get_transaction_weight(tx_size: usize, cycles: u64) -> u64 {
/// but if the size of the transaction is close to the limit of the block,
/// it may cause the transaction to fail to be packed
pub const TRANSACTION_SIZE_LIMIT: u64 = 512 * 1_000;

/// Transaction pool information.
#[derive(Clone, Debug)]
pub struct TxPoolInfo {
/// The associated chain tip block hash.
///
/// Transaction pool is stateful. It manages the transactions which are valid to be commit
/// after this block.
pub tip_hash: Byte32,
/// The block number of the block `tip_hash`.
pub tip_number: BlockNumber,
/// Count of transactions in the pending state.
///
/// The pending transactions must be proposed in a new block first.
pub pending_size: usize,
/// Count of transactions in the proposed state.
///
/// The proposed transactions are ready to be commit in the new block after the block
/// `tip_hash`.
pub proposed_size: usize,
/// Count of orphan transactions.
///
/// An orphan transaction has an input cell from the transaction which is neither in the chain
/// nor in the transaction pool.
pub orphan_size: usize,
/// Total count of transactions in the pool of all the different kinds of states.
pub total_tx_size: usize,
/// Total consumed VM cycles of all the transactions in the pool.
pub total_tx_cycles: Cycle,
/// Fee rate threshold. The pool rejects transactions which fee rate is below this threshold.
///
/// The unit is Shannons per 1000 bytes transaction serialization size in the block.
pub min_fee_rate: FeeRate,
/// Last updated time. This is the Unix timestamp in milliseconds.
pub last_txs_updated_at: u64,
/// Limiting transactions to tx_size_limit
///
/// Transactions with a large size close to the block size limit may not be packaged,
/// because the block header and cellbase are occupied,
/// so the tx-pool is limited to accepting transaction up to tx_size_limit.
pub tx_size_limit: u64,
/// Total limit on the size of transactions in the tx-pool
pub max_tx_pool_size: u64,
}

0 comments on commit 98678a8

Please sign in to comment.