Skip to content

Commit

Permalink
Add test_tx_pool_accept subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
eval-exec committed Apr 28, 2024
1 parent bd3945a commit f992dae
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/subcommands/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ impl<'a> RpcSubCommand<'a> {
.about("Hash of a transaction"),
),
App::new("tx_pool_info").about("Get transaction pool information"),
App::new("test_tx_pool_accept")
.about("Test if transaction can be accepted by Tx Pool")
.arg(
Arg::with_name("tx-file").long("tx-file").takes_value(true).required(true).about("transaction data file(format json)")
),
App::new("clear_tx_pool").about("Removes all transactions from the transaction pool"),
App::new("get_raw_tx_pool")
.about("Returns all transaction ids in tx pool as a json array of string transaction ids")
Expand Down Expand Up @@ -1064,6 +1069,25 @@ impl<'a> CliSubCommand for RpcSubCommand<'a> {
Ok(Output::new_output(resp))
}
}
("test_tx_pool_accept", Some(m)) => {
let path: PathBuf = FilePathParser::new(true).from_matches(m, "tx-path")?;
let content = fs::read_to_string(path).map_err(|err| err.to_string())?;

let tx: rpc_types::Transaction =
serde_json::from_str(&content).map_err(|err| err.to_string())?;

let is_raw_data = is_raw_data || m.is_present("raw-data");
if is_raw_data {
let resp = self
.raw_rpc_client
.test_tx_pool_accept(tx, None)
.map_err(|err| err.to_string())?;
Ok(Output::new_output(resp))
} else {
let resp = self.rpc_client.test_tx_pool_accept(tx.into(), None)?;
Ok(Output::new_output(resp))
}
}
("clear_tx_pool", _) => {
self.rpc_client.clear_tx_pool()?;
Ok(Output::new_success())
Expand Down
14 changes: 12 additions & 2 deletions src/utils/rpc/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::convert::TryInto;

use ckb_jsonrpc_types::{
Alert, BlockNumber, CellWithStatus, EpochNumber, EpochNumberWithFraction, JsonBytes,
OutputsValidator, Uint32,
Alert, BlockNumber, CellWithStatus, EntryCompleted, EpochNumber, EpochNumberWithFraction,
JsonBytes, OutputsValidator, Uint32,
};
pub use ckb_sdk::{
rpc::ckb_indexer::{Order, Pagination, SearchKey},
Expand Down Expand Up @@ -368,6 +368,16 @@ impl HttpRpcClient {
.map(Into::into)
.map_err(|err| err.to_string())
}
pub fn test_tx_pool_accept(
&mut self,
tx: packed::Transaction,
outputs_validator: Option<OutputsValidator>,
) -> Result<types::EntryCompleted, String> {
self.client
.test_tx_pool_accept(tx.into(), outputs_validator)
.map(Into::into)
.map_err(|err| err.to_string())
}
pub fn clear_tx_pool(&mut self) -> Result<(), String> {
self.client.clear_tx_pool().map_err(|err| err.to_string())
}
Expand Down
16 changes: 16 additions & 0 deletions src/utils/rpc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,22 @@ impl From<rpc_types::HardForks> for HardForks {
}
}

/// Response type of the RPC method `test_tx_pool_accept`.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct EntryCompleted {
pub block_hash: H256,
pub block_number: BlockNumber,
}

impl From<ckb_jsonrpc_types::EntryCompleted> for EntryCompleted {
fn from(value: ckb_jsonrpc_types::EntryCompleted) -> Self {
Self {
block_hash: value.block_hash,
block_number: value.block_number.into(),

}
}

/// SoftFork information
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
Expand Down

0 comments on commit f992dae

Please sign in to comment.