Skip to content

Commit

Permalink
Merge pull request nervosnetwork#4173 from chenyukang/yukang-add-api-…
Browse files Browse the repository at this point in the history
…for-tx-pool

Add troubleshoot and metric API for tx-pool
  • Loading branch information
doitian authored Nov 21, 2023
2 parents b551499 + 5d307a6 commit 3bb18c0
Show file tree
Hide file tree
Showing 19 changed files with 378 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ clippy: setup-ckb-test ## Run linter to examine Rust source codes.
cargo clippy ${VERBOSE} --all --all-targets --features ${ALL_FEATURES} -- ${CLIPPY_OPTS} -D missing_docs
cd test && cargo clippy ${VERBOSE} --all --all-targets --all-features -- ${CLIPPY_OPTS}

.PHONY: bless
bless: setup-ckb-test
cargo clippy --fix --allow-dirty ${VERBOSE} --all --all-targets --features ${ALL_FEATURES} -- ${CLIPPY_OPTS} -D missing_docs
cd test && cargo clippy --fix --allow-dirty ${VERBOSE} --all --all-targets --all-features -- ${CLIPPY_OPTS}
cargo fmt ${VERBOSE} --all
cd test && cargo fmt ${VERBOSE} --all

.PHONY: security-audit
security-audit: ## Use cargo-deny to audit Cargo.lock for crates with security vulnerabilities.
cargo deny check --hide-inclusion-graph --show-stats advisories sources
Expand Down
76 changes: 76 additions & 0 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ The crate `ckb-rpc`'s minimum supported rustc version is 1.71.1.
* [Method `tx_pool_info`](#method-tx_pool_info)
* [Method `clear_tx_pool`](#method-clear_tx_pool)
* [Method `get_raw_tx_pool`](#method-get_raw_tx_pool)
* [Method `get_pool_tx_detail_info`](#method-get_pool_tx_detail_info)
* [Method `tx_pool_ready`](#method-tx_pool_ready)
* [Module Stats](#module-stats)
* [Method `get_blockchain_info`](#method-get_blockchain_info)
Expand Down Expand Up @@ -163,6 +164,7 @@ The crate `ckb-rpc`'s minimum supported rustc version is 1.71.1.
* [Type `PeerSyncState`](#type-peersyncstate)
* [Type `PoolTransactionEntry`](#type-pooltransactionentry)
* [Type `PoolTransactionReject`](#type-pooltransactionreject)
* [Type `PoolTxDetailInfo`](#type-pooltxdetailinfo)
* [Type `ProposalShortId`](#type-proposalshortid)
* [Type `ProposalWindow`](#type-proposalwindow)
* [Type `Ratio`](#type-ratio)
Expand Down Expand Up @@ -4659,6 +4661,55 @@ Response
```


#### Method `get_pool_tx_detail_info`
* `get_pool_tx_detail_info(tx_hash)`
* `tx_hash`: [`H256`](#type-h256)
* result: [`PoolTxDetailInfo`](#type-pooltxdetailinfo)

Query and returns the details of a transaction in the pool, only for trouble shooting

###### Params

* `tx_hash` - Hash of a transaction

###### Examples

Request


```
{
"id": 42,
"jsonrpc": "2.0",
"method": "get_pool_tx_detail_info",
"params": [
"0xa0ef4eb5f4ceeb08a4c8524d84c5da95dce2f608e0ca2ec8091191b0f330c6e3"
]
}
```


Response


```
{
"jsonrpc": "2.0",
"result": {
"ancestors_count": "0x0",
"descendants_count": "0x0",
"entry_status": "pending",
"pending_count": "0x1",
"proposed_count": "0x0",
"rank_in_pending": "0x1",
"score_sortkey": "fee: 0x16923F7DCF, ancestors_fee: 0x16923F7DCF, weight: 0x112, ancestors_weight: 0x112",
"timestamp": "0x18aa1baa54c"
},
"id": 42
}
```


#### Method `tx_pool_ready`
* `tx_pool_ready()`
* result: `boolean`
Expand Down Expand Up @@ -6506,6 +6557,31 @@ Different reject types:
* `RBFRejected`: RBF rejected


### Type `PoolTxDetailInfo`

A Tx details info in tx-pool.

#### Fields

`PoolTxDetailInfo` is a JSON object with the following fields.

* `timestamp`: [`Uint64`](#type-uint64) - The time added into tx-pool

* `entry_status`: `string` - The detailed status in tx-pool, `pending`, `gap`, `proposed`

* `rank_in_pending`: [`Uint64`](#type-uint64) - The rank in pending, starting from 0

* `pending_count`: [`Uint64`](#type-uint64) - The pending(`pending` and `gap`) count

* `proposed_count`: [`Uint64`](#type-uint64) - The proposed count

* `descendants_count`: [`Uint64`](#type-uint64) - The descendants count of tx

* `ancestors_count`: [`Uint64`](#type-uint64) - The ancestors count of tx

* `score_sortkey`: `string` - The score key details, useful to debug


### Type `ProposalShortId`

The 10-byte fixed-length binary encoded as a 0x-prefixed hex string in JSON.
Expand Down
53 changes: 52 additions & 1 deletion rpc/src/module/pool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::error::RPCError;
use ckb_chain_spec::consensus::Consensus;
use ckb_constant::hardfork::{mainnet, testnet};
use ckb_jsonrpc_types::{OutputsValidator, RawTxPool, Script, Transaction, TxPoolInfo};
use ckb_jsonrpc_types::{
OutputsValidator, PoolTxDetailInfo, RawTxPool, Script, Transaction, TxPoolInfo,
};
use ckb_logger::error;
use ckb_shared::shared::Shared;
use ckb_types::{core, packed, prelude::*, H256};
Expand Down Expand Up @@ -253,6 +255,47 @@ pub trait PoolRpc {
#[rpc(name = "get_raw_tx_pool")]
fn get_raw_tx_pool(&self, verbose: Option<bool>) -> Result<RawTxPool>;

/// Query and returns the details of a transaction in the pool, only for trouble shooting
/// ## Params
///
/// * `tx_hash` - Hash of a transaction
///
/// ## Examples
///
/// Request
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "method": "get_pool_tx_detail_info",
/// "params": [
/// "0xa0ef4eb5f4ceeb08a4c8524d84c5da95dce2f608e0ca2ec8091191b0f330c6e3"
/// ]
/// }
/// ```
///
/// Response
///
/// ```json
/// {
/// "jsonrpc": "2.0",
/// "result": {
/// "ancestors_count": "0x0",
/// "descendants_count": "0x0",
/// "entry_status": "pending",
/// "pending_count": "0x1",
/// "proposed_count": "0x0",
/// "rank_in_pending": "0x1",
/// "score_sortkey": "fee: 0x16923F7DCF, ancestors_fee: 0x16923F7DCF, weight: 0x112, ancestors_weight: 0x112",
/// "timestamp": "0x18aa1baa54c"
/// },
/// "id": 42
/// }
/// ```
#[rpc(name = "get_pool_tx_detail_info")]
fn get_pool_tx_detail_info(&self, tx_hash: H256) -> Result<PoolTxDetailInfo>;

/// Returns whether tx-pool service is started, ready for request.
///
/// ## Examples
Expand Down Expand Up @@ -482,6 +525,14 @@ impl PoolRpc for PoolRpcImpl {
};
Ok(raw)
}

fn get_pool_tx_detail_info(&self, tx_hash: H256) -> Result<PoolTxDetailInfo> {
let tx_pool = self.shared.tx_pool_controller();
let tx_detail = tx_pool
.get_tx_detail(tx_hash.pack())
.map_err(|err| RPCError::custom(RPCError::CKBInternalError, err.to_string()))?;
Ok(tx_detail.into())
}
}

pub(crate) struct WellKnownScriptsOnlyValidator<'a> {
Expand Down
3 changes: 3 additions & 0 deletions rpc/src/tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,9 @@ fn mock_rpc_response(example: &RpcTestExample, response: &mut RpcTestResponse) {
"generate_block" => replace_rpc_response::<H256>(example, response),
"process_block_without_verify" => replace_rpc_response::<H256>(example, response),
"notify_transaction" => replace_rpc_response::<H256>(example, response),
"get_pool_tx_detail_info" => {
response.result["timestamp"] = example.response.result["timestamp"].clone()
}
_ => {}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ fn all_specs() -> Vec<Box<dyn Spec>> {
Box::new(TxsRelayOrder),
Box::new(SendTxChain),
Box::new(SendTxChainRevOrder),
Box::new(TxPoolEntryStatus),
Box::new(DifferentTxsWithSameInputWithOutRBF),
Box::new(RbfEnable),
Box::new(RbfBasic),
Expand Down
11 changes: 10 additions & 1 deletion test/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use ckb_app_config::CKBAppConfig;
use ckb_chain_spec::consensus::Consensus;
use ckb_chain_spec::ChainSpec;
use ckb_error::AnyError;
use ckb_jsonrpc_types::TxStatus;
use ckb_jsonrpc_types::{BlockFilter, BlockTemplate, TxPoolInfo};
use ckb_jsonrpc_types::{PoolTxDetailInfo, TxStatus};
use ckb_logger::{debug, error};
use ckb_resource::Resource;
use ckb_types::{
Expand Down Expand Up @@ -424,6 +424,10 @@ impl Node {
.expect("block filter exists")
}

pub fn get_pool_tx_detail_info(&self, hash: Byte32) -> PoolTxDetailInfo {
self.rpc_client().get_pool_tx_detail_info(hash)
}

/// The states of chain and txpool are updated asynchronously. Which means that the chain has
/// updated to the newest tip but txpool not.
/// get_tip_tx_pool_info wait to ensure the txpool update to the newest tip as well.
Expand Down Expand Up @@ -607,6 +611,11 @@ impl Node {
assert_eq!(tx_pool_info.total_tx_cycles.value(), total_tx_cycles);
}

pub fn assert_pool_entry_status(&self, hash: Byte32, expect_status: &str) {
let response = self.get_pool_tx_detail_info(hash);
assert_eq!(response.entry_status, expect_status);
}

pub fn assert_tx_pool_cycles(&self, total_tx_cycles: u64) {
let tx_pool_info = self.get_tip_tx_pool_info();
assert_eq!(tx_pool_info.total_tx_cycles.value(), total_tx_cycles);
Expand Down
12 changes: 10 additions & 2 deletions test/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use ckb_error::AnyError;
use ckb_jsonrpc_types::{
Alert, BannedAddr, Block, BlockEconomicState, BlockFilter, BlockNumber, BlockTemplate,
BlockView, Capacity, CellWithStatus, ChainInfo, EpochNumber, EpochView, EstimateCycles,
HeaderView, LocalNode, OutPoint, RawTxPool, RemoteNode, Timestamp, Transaction,
TransactionProof, TransactionWithStatusResponse, TxPoolInfo, Uint32, Uint64, Version,
HeaderView, LocalNode, OutPoint, PoolTxDetailInfo, RawTxPool, RemoteNode, Timestamp,
Transaction, TransactionProof, TransactionWithStatusResponse, TxPoolInfo, Uint32, Uint64,
Version,
};
use ckb_types::core::{
BlockNumber as CoreBlockNumber, Capacity as CoreCapacity, EpochNumber as CoreEpochNumber,
Expand Down Expand Up @@ -89,6 +90,12 @@ impl RpcClient {
.expect("rpc call get_transaction")
}

pub fn get_pool_tx_detail_info(&self, hash: Byte32) -> PoolTxDetailInfo {
self.inner
.get_pool_tx_detail_info(hash.unpack())
.expect("rpc call get_transaction_tx_pool_details")
}

pub fn get_block_hash(&self, number: CoreBlockNumber) -> Option<Byte32> {
self.inner
.get_block_hash(number.into())
Expand Down Expand Up @@ -357,4 +364,5 @@ jsonrpc!(pub struct Inner {
pub fn verify_transaction_proof(&self, tx_proof: TransactionProof) -> Vec<H256>;
pub fn notify_transaction(&self, tx: Transaction) -> H256;
pub fn tx_pool_ready(&self) -> bool;
pub fn get_pool_tx_detail_info(&self, _hash: H256) -> PoolTxDetailInfo;
});
20 changes: 20 additions & 0 deletions test/src/specs/rpc/get_pool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::{Node, Spec};

pub struct TxPoolEntryStatus;

impl Spec for TxPoolEntryStatus {
fn run(&self, nodes: &mut Vec<Node>) {
let node0 = &nodes[0];

node0.mine_until_out_bootstrap_period();
node0.new_block_with_blocking(|template| template.number.value() != 13);
let tx_hash_0 = node0.generate_transaction();
let tx = node0.new_transaction(tx_hash_0.clone());
node0.rpc_client().send_transaction(tx.data().into());
node0.assert_pool_entry_status(tx_hash_0.clone(), "pending");
node0.mine(1);
node0.assert_pool_entry_status(tx_hash_0.clone(), "gap");
node0.mine(1);
node0.assert_pool_entry_status(tx_hash_0, "proposed");
}
}
2 changes: 2 additions & 0 deletions test/src/specs/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod get_block_median_time;
mod get_block_template;
mod get_blockchain_info;
mod get_pool;
#[cfg(target_os = "linux")]
mod set_ban;
mod submit_block;
Expand All @@ -10,6 +11,7 @@ mod truncate;
pub use get_block_median_time::*;
pub use get_block_template::*;
pub use get_blockchain_info::*;
pub use get_pool::*;
#[cfg(target_os = "linux")]
pub use set_ban::*;
pub use submit_block::*;
Expand Down
2 changes: 2 additions & 0 deletions tx-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ ckb-logger = { path = "../util/logger", version = "= 0.113.0-pre" }
ckb-verification = { path = "../verification", version = "= 0.113.0-pre" }
ckb-systemtime = { path = "../util/systemtime", version = "= 0.113.0-pre" }
lru = "0.7.1"

ckb-dao = { path = "../util/dao", version = "= 0.113.0-pre" }
ckb-reward-calculator = { path = "../util/reward-calculator", version = "= 0.113.0-pre" }
ckb-store = { path = "../store", version = "= 0.113.0-pre" }
ckb-util = { path = "../util", version = "= 0.113.0-pre" }
ckb-jsonrpc-types = { path = "../util/jsonrpc-types", version = "= 0.113.0-pre" }
ckb-chain-spec = { path = "../spec", version = "= 0.113.0-pre" }
ckb-snapshot = { path = "../util/snapshot", version = "= 0.113.0-pre" }
ckb-metrics = {path = "../util/metrics", version = "= 0.113.0-pre"}
ckb-error = { path = "../error", version = "= 0.113.0-pre" }
tokio = { version = "1", features = ["sync", "process"] }
ckb-async-runtime = { path = "../util/runtime", version = "= 0.113.0-pre" }
Expand Down
Loading

0 comments on commit 3bb18c0

Please sign in to comment.