Skip to main content

ckb_app_config/configs/
tx_pool.rs

1use ckb_jsonrpc_types::{FeeRateDef, JsonBytes, ScriptHashType};
2use ckb_types::H256;
3use ckb_types::core::{Cycle, FeeRate};
4use serde::{Deserialize, Serialize};
5use std::path::{Path, PathBuf};
6use url::Url;
7
8// The default values are set in the legacy version.
9/// Transaction pool configuration
10#[derive(Clone, Debug, Serialize)]
11pub struct TxPoolConfig {
12    /// Keep the transaction pool below <max_tx_pool_size> mb
13    pub max_tx_pool_size: usize,
14    /// txs with lower fee rate than this will not be relayed or be mined
15    #[serde(with = "FeeRateDef")]
16    pub min_fee_rate: FeeRate,
17    /// txs need to pay larger fee rate than this for RBF
18    #[serde(with = "FeeRateDef")]
19    pub min_rbf_rate: FeeRate,
20    /// tx pool rejects txs that cycles greater than max_tx_verify_cycles
21    pub max_tx_verify_cycles: Cycle,
22    /// max tx verify workers, default is 3/4 of cpu cores
23    #[serde(default = "default_max_tx_verify_workers")]
24    pub max_tx_verify_workers: usize,
25    /// max ancestors size limit for a single tx
26    pub max_ancestors_count: usize,
27    /// rejected tx time to live by days
28    pub keep_rejected_tx_hashes_days: u8,
29    /// rejected tx count limit
30    pub keep_rejected_tx_hashes_count: u64,
31    /// The file to persist the tx pool on the disk when tx pool have been shutdown.
32    ///
33    /// By default, it is a subdirectory of 'tx-pool' subdirectory under the data directory.
34    #[serde(default)]
35    pub persisted_data: PathBuf,
36    /// The recent reject record database directory path.
37    ///
38    /// By default, it is a subdirectory of 'tx-pool' subdirectory under the data directory.
39    #[serde(default)]
40    pub recent_reject: PathBuf,
41    /// The expiration time for pool transactions in hours
42    pub expiry_hours: u8,
43}
44
45/// default max tx verify workers is 3/4 of cpu cores
46pub fn default_max_tx_verify_workers() -> usize {
47    std::cmp::max(num_cpus::get() * 3 / 4, 1)
48}
49
50/// Block assembler config options.
51///
52/// The block assembler section tells CKB how to claim the miner rewards.
53#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Eq)]
54#[serde(deny_unknown_fields)]
55pub struct BlockAssemblerConfig {
56    /// The miner lock script code hash.
57    pub code_hash: H256,
58    /// The miner lock script args.
59    pub args: JsonBytes,
60    /// An arbitrary message to be added into the cellbase transaction.
61    pub message: JsonBytes,
62    /// The miner lock script hash type.
63    pub hash_type: ScriptHashType,
64    /// Use ckb binary version as message prefix to identify the block miner client (default true, false to disable it).
65    #[serde(default = "default_use_binary_version_as_message_prefix")]
66    pub use_binary_version_as_message_prefix: bool,
67    /// A field to store the block miner client version, non-configurable options.
68    #[serde(skip)]
69    pub binary_version: String,
70    /// A field to control update interval millis
71    #[serde(default = "default_update_interval_millis")]
72    pub update_interval_millis: u64,
73    /// Notify url
74    #[serde(default)]
75    pub notify: Vec<Url>,
76    /// Notify scripts
77    #[serde(default)]
78    pub notify_scripts: Vec<String>,
79    /// Notify timeout
80    #[serde(default = "default_notify_timeout_millis")]
81    pub notify_timeout_millis: u64,
82    /// Optional bearer token to authenticate block-template notifications.
83    ///
84    /// When `notify` URLs are configured and this token is set, the node will
85    /// send the header `Authorization: Bearer <token>` with every notify
86    /// request. The receiving ckb-miner must be configured with the same token
87    /// in `miner.client.auth_token`, otherwise notifications will be rejected.
88    ///
89    /// Must be non-empty and free of leading/trailing whitespace; the node
90    /// refuses to start otherwise.
91    #[serde(default)]
92    pub notify_auth_token: Option<String>,
93}
94
95const fn default_use_binary_version_as_message_prefix() -> bool {
96    true
97}
98
99const fn default_update_interval_millis() -> u64 {
100    800
101}
102
103const fn default_notify_timeout_millis() -> u64 {
104    800
105}
106
107impl TxPoolConfig {
108    /// Canonicalizes paths in the config options.
109    ///
110    /// If `self.persisted_data` is not set, set it to `data_dir / tx_pool_persisted_data`.
111    ///
112    /// If `self.path` is relative, convert them to absolute path using
113    /// `root_dir` as current working directory.
114    pub fn adjust<P: AsRef<Path>>(&mut self, root_dir: &Path, tx_pool_dir: P) {
115        _adjust(
116            root_dir,
117            tx_pool_dir.as_ref(),
118            &mut self.persisted_data,
119            "persisted_data",
120        );
121        _adjust(
122            root_dir,
123            tx_pool_dir.as_ref(),
124            &mut self.recent_reject,
125            "recent_reject",
126        );
127    }
128}
129
130fn _adjust(root_dir: &Path, tx_pool_dir: &Path, target: &mut PathBuf, sub: &str) {
131    if target.to_str().is_none() || target.to_str() == Some("") {
132        *target = tx_pool_dir.to_path_buf().join(sub);
133    } else if target.is_relative() {
134        *target = root_dir.to_path_buf().join(&target)
135    }
136}