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

Add RPC get_pending_tx_hashes #772

Merged
merged 2 commits into from
Aug 9, 2022
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
10 changes: 10 additions & 0 deletions crates/rpc-server/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ impl Registry {
.with_method("gw_get_transaction", get_transaction)
.with_method("gw_get_transaction_receipt", get_transaction_receipt)
.with_method("gw_get_withdrawal", get_withdrawal)
.with_method("gw_get_pending_tx_hashes", get_pending_tx_hashes)
.with_method("gw_execute_l2transaction", execute_l2transaction)
.with_method("gw_execute_raw_l2transaction", execute_raw_l2transaction)
.with_method(
Expand Down Expand Up @@ -741,6 +742,15 @@ async fn get_transaction(
}))
}

async fn get_pending_tx_hashes(store: Data<Store>) -> Result<Vec<JsonH256>, RpcError> {
let snap = store.get_snapshot();
let tx_hashes = snap
.iter_mem_pool_transactions()
.map(|hash| JsonH256::from_slice(&hash))
.collect::<Result<Vec<_>, _>>()?;
Ok(tx_hashes)
}

async fn is_request_in_queue(
Params((hash,)): Params<(JsonH256,)>,
in_queue_request_map: Data<Option<Arc<InQueueRequestMap>>>,
Expand Down
14 changes: 13 additions & 1 deletion crates/store/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use gw_db::{schema::Col, RocksDBSnapshot};
use gw_db::{
schema::{Col, COLUMN_MEM_POOL_TRANSACTION},
DBIterator, RocksDBSnapshot,
};

use crate::traits::{chain_store::ChainStore, kv_store::KVStoreRead};

Expand All @@ -22,3 +25,12 @@ impl KVStoreRead for StoreSnapshot {
.map(|v| Box::<[u8]>::from(v.as_ref()))
}
}

impl StoreSnapshot {
pub fn iter_mem_pool_transactions(&self) -> impl Iterator<Item = Box<[u8]>> + '_ {
self.inner
.iter(COLUMN_MEM_POOL_TRANSACTION, gw_db::IteratorMode::Start)
.expect("db read should not fail")
.map(|(k, _)| k)
}
}
33 changes: 33 additions & 0 deletions docs/RPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [Method `gw_get_fee_config`](#method-gw_get_fee_config)
* [Method `gw_get_mem_pool_state_root`](#method-gw_get_mem_pool_state_root)
* [Method `gw_get_mem_pool_state_ready`](#method-gw_get_mem_pool_state_ready)
* [Method `gw_get_pending_tx_hashes`](#method-gw_get_pending_tx_hashes)
* [Method `gw_get_node_info`](#method-gw_get_node_info)
* [Method `gw_reload_config`](#method-gw_reload_config)
* [Method `gw_submit_l2transaction`](#method-gw_submit_l2transaction)
Expand Down Expand Up @@ -1309,7 +1310,39 @@ Response
}
```

### Method `gw_get_pending_tx_hashes`
* params: None
* result: [`H256[]`](#type-h256)

Get pending transaction hashes.

#### Examples

Request

```json
{
"id": 42,
"jsonrpc": "2.0",
"method": "gw_get_pending_tx_hashes",
"params": []
}
```

Response:

```json
{
"id": 42,
"jsonrpc": "2.0",
"result": [
"0x37c705fbbe2660b6cec619fbfc7847752e0111044742a78e1b394f8da285baa3",
"0x4126f01bfaf17ffcbb1745c6e33830e66e2490e884c9f9c2d1e14bdbc99545de",
"0x7dcd3ddd4fbe1b6ee0129b3a9a4874dd909490da9952f0e17d2ee8d88ec49a2a",
"0xb0d96d3d73738f0dd4a66a03fb48fe11ea4d2195d8cb138885bb3a90fd29c7be"
],
}
```

## RPC Types

Expand Down