Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc(ChainRpc): Add only_committed param for get_transaction #3963

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
31 changes: 26 additions & 5 deletions rpc/src/module/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -648,6 +649,7 @@ pub trait ChainRpc {
&self,
tx_hash: H256,
verbosity: Option<Uint32>,
only_committed: Option<bool>,
) -> Result<TransactionWithStatusResponse>;

/// Returns the hash of a block in the [canonical chain](#canonical-chain) with the specified
Expand Down Expand Up @@ -1729,26 +1731,29 @@ impl ChainRpc for ChainRpcImpl {
&self,
tx_hash: H256,
verbosity: Option<Uint32>,
only_committed: Option<bool>,
) -> Result<TransactionWithStatusResponse> {
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"))
Expand Down Expand Up @@ -2103,7 +2108,11 @@ impl ChainRpc for ChainRpcImpl {
}

impl ChainRpcImpl {
fn get_transaction_verbosity1(&self, tx_hash: packed::Byte32) -> Result<TransactionWithStatus> {
fn get_transaction_verbosity1(
&self,
tx_hash: packed::Byte32,
only_committed: bool,
) -> Result<TransactionWithStatus> {
let snapshot = self.shared.snapshot();
if let Some(tx_info) = snapshot.get_transaction_info(&tx_hash) {
let cycles = if tx_info.is_cellbase() {
Expand All @@ -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 {
Expand All @@ -2141,7 +2154,11 @@ impl ChainRpcImpl {
Ok(TransactionWithStatus::omit_transaction(tx_status, cycles))
}

fn get_transaction_verbosity2(&self, tx_hash: packed::Byte32) -> Result<TransactionWithStatus> {
fn get_transaction_verbosity2(
&self,
tx_hash: packed::Byte32,
only_committed: bool,
) -> Result<TransactionWithStatus> {
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() {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions test/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -306,7 +306,7 @@ jsonrpc!(pub struct Inner {
pub fn get_header(&self, _hash: H256) -> Option<HeaderView>;
pub fn get_header_by_number(&self, _number: BlockNumber) -> Option<HeaderView>;
pub fn get_block_filter(&self, _hash: H256) -> Option<BlockFilter>;
pub fn get_transaction(&self, _hash: H256, verbosity: Option<Uint32>) -> TransactionWithStatusResponse;
pub fn get_transaction(&self, _hash: H256, verbosity: Option<Uint32>, only_commited: Option<bool>) -> TransactionWithStatusResponse;
pub fn get_block_hash(&self, _number: BlockNumber) -> Option<H256>;
pub fn get_tip_header(&self) -> HeaderView;
pub fn get_live_cell(&self, _out_point: OutPoint, _with_data: bool) -> CellWithStatus;
Expand Down