From 537e56c1a6b11edcdddb93ec96bde002c2afd706 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Thu, 27 Apr 2023 13:38:25 +0800 Subject: [PATCH 1/2] rpc(ChainRpc): add `only_committed` param for `get_transaction` --- rpc/README.md | 5 ++++- rpc/src/module/chain.rs | 31 ++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/rpc/README.md b/rpc/README.md index 32cc6f3eb3..112b924b57 100644 --- a/rpc/README.md +++ b/rpc/README.md @@ -785,9 +785,10 @@ The response looks like below when the block have block filter. #### Method `get_transaction` -* `get_transaction(tx_hash, verbosity)` +* `get_transaction(tx_hash, verbosity, only_committed)` * `tx_hash`: [`H256`](#type-h256) * `verbosity`: [`Uint32`](#type-uint32) `|` `null` + * `only_committed`: `boolean` `|` `null` * result: [`TransactionWithStatusResponse`](#type-transactionwithstatusresponse) Returns the information about a transaction requested by transaction hash. @@ -804,6 +805,8 @@ If the transaction is in the chain, the block hash is also returned. * `verbosity` - result format which allows 0, 1 and 2. (**Optional**, the defaults to 2.) +* `only_committed` - whether to query committed transaction only. (**Optional**, if not set, it will query all status of transactions.) + ###### Returns When verbosity=0, it’s response value is as same as verbosity=2, but it return a 0x-prefixed hex encoded molecule packed::Transaction on `transaction` field diff --git a/rpc/src/module/chain.rs b/rpc/src/module/chain.rs index 9700e5d17f..0e6d223369 100644 --- a/rpc/src/module/chain.rs +++ b/rpc/src/module/chain.rs @@ -540,6 +540,7 @@ pub trait ChainRpc { /// /// * `tx_hash` - Hash of a transaction /// * `verbosity` - result format which allows 0, 1 and 2. (**Optional**, the defaults to 2.) + /// * `only_committed` - whether to query committed transaction only. (**Optional**, if not set, it will query all status of transactions.) /// /// ## Returns /// @@ -648,6 +649,7 @@ pub trait ChainRpc { &self, tx_hash: H256, verbosity: Option, + only_committed: Option, ) -> Result; /// Returns the hash of a block in the [canonical chain](#canonical-chain) with the specified @@ -1729,26 +1731,29 @@ impl ChainRpc for ChainRpcImpl { &self, tx_hash: H256, verbosity: Option, + only_committed: Option, ) -> Result { let tx_hash = tx_hash.pack(); let verbosity = verbosity .map(|v| v.value()) .unwrap_or(DEFAULT_GET_TRANSACTION_VERBOSITY_LEVEL); + let only_committed: bool = only_committed.unwrap_or(false); + if verbosity == 0 { // when verbosity=0, it's response value is as same as verbosity=2, but it // return a 0x-prefixed hex encoded molecule packed::Transaction` on `transaction` field - self.get_transaction_verbosity2(tx_hash) + self.get_transaction_verbosity2(tx_hash, only_committed) .map(|tws| TransactionWithStatusResponse::from(tws, ResponseFormatInnerType::Hex)) } else if verbosity == 1 { // The RPC does not return the transaction content and the field transaction must be null. - self.get_transaction_verbosity1(tx_hash) + self.get_transaction_verbosity1(tx_hash, only_committed) .map(|tws| TransactionWithStatusResponse::from(tws, ResponseFormatInnerType::Json)) } else if verbosity == 2 { // if tx_status.status is pending, proposed, or committed, // the RPC returns the transaction content as field transaction, // otherwise the field is null. - self.get_transaction_verbosity2(tx_hash) + self.get_transaction_verbosity2(tx_hash, only_committed) .map(|tws| TransactionWithStatusResponse::from(tws, ResponseFormatInnerType::Json)) } else { Err(RPCError::invalid_params("invalid verbosity level")) @@ -2103,7 +2108,11 @@ impl ChainRpc for ChainRpcImpl { } impl ChainRpcImpl { - fn get_transaction_verbosity1(&self, tx_hash: packed::Byte32) -> Result { + fn get_transaction_verbosity1( + &self, + tx_hash: packed::Byte32, + only_committed: bool, + ) -> Result { let snapshot = self.shared.snapshot(); if let Some(tx_info) = snapshot.get_transaction_info(&tx_hash) { let cycles = if tx_info.is_cellbase() { @@ -2125,6 +2134,10 @@ impl ChainRpcImpl { )); } + if only_committed { + return Ok(TransactionWithStatus::with_unknown()); + } + let tx_pool = self.shared.tx_pool_controller(); let tx_status = tx_pool.get_tx_status(tx_hash); if let Err(e) = tx_status { @@ -2141,7 +2154,11 @@ impl ChainRpcImpl { Ok(TransactionWithStatus::omit_transaction(tx_status, cycles)) } - fn get_transaction_verbosity2(&self, tx_hash: packed::Byte32) -> Result { + fn get_transaction_verbosity2( + &self, + tx_hash: packed::Byte32, + only_committed: bool, + ) -> Result { let snapshot = self.shared.snapshot(); if let Some((tx, tx_info)) = snapshot.get_transaction_with_info(&tx_hash) { let cycles = if tx_info.is_cellbase() { @@ -2163,6 +2180,10 @@ impl ChainRpcImpl { )); } + if only_committed { + return Ok(TransactionWithStatus::with_unknown()); + } + let tx_pool = self.shared.tx_pool_controller(); let transaction_with_status = tx_pool.get_transaction_with_status(tx_hash); if let Err(e) = transaction_with_status { From 4145095e64f940be138a7d3f0850191157ad05fa Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Thu, 27 Apr 2023 13:39:19 +0800 Subject: [PATCH 2/2] test: add `only_committed` param for `test::rpc::RpcClient::get_transaction` Signed-off-by: Eval EXEC --- test/src/rpc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/src/rpc.rs b/test/src/rpc.rs index 2247fe5593..a4c7aa4697 100644 --- a/test/src/rpc.rs +++ b/test/src/rpc.rs @@ -85,7 +85,7 @@ impl RpcClient { verbosity: u32, ) -> TransactionWithStatusResponse { self.inner - .get_transaction(hash.unpack(), Some(verbosity.into())) + .get_transaction(hash.unpack(), Some(verbosity.into()), None) .expect("rpc call get_transaction") } @@ -306,7 +306,7 @@ jsonrpc!(pub struct Inner { pub fn get_header(&self, _hash: H256) -> Option; pub fn get_header_by_number(&self, _number: BlockNumber) -> Option; pub fn get_block_filter(&self, _hash: H256) -> Option; - pub fn get_transaction(&self, _hash: H256, verbosity: Option) -> TransactionWithStatusResponse; + pub fn get_transaction(&self, _hash: H256, verbosity: Option, only_commited: Option) -> TransactionWithStatusResponse; pub fn get_block_hash(&self, _number: BlockNumber) -> Option; pub fn get_tip_header(&self) -> HeaderView; pub fn get_live_cell(&self, _out_point: OutPoint, _with_data: bool) -> CellWithStatus;