Skip to content

Commit 77d9ed7

Browse files
kariyclaude
andcommitted
refactor(rpc): use BTreeMap for deterministic txpool response ordering
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 859c720 commit 77d9ed7

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

crates/rpc/rpc-server/src/txpool.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::collections::BTreeMap;
22

33
use jsonrpsee::core::{async_trait, RpcResult};
44
use katana_pool::{PoolTransaction, TransactionPool};
@@ -25,7 +25,7 @@ impl<P> TxPoolApi<P> {
2525
impl<P: TransactionPool> TxPoolApi<P> {
2626
fn build_content(&self, filter: Option<ContractAddress>) -> TxPoolContent {
2727
let txs = self.pool.take_transactions_snapshot();
28-
let mut pending: HashMap<ContractAddress, HashMap<_, _>> = HashMap::new();
28+
let mut pending: BTreeMap<ContractAddress, BTreeMap<_, _>> = BTreeMap::new();
2929

3030
for tx in txs {
3131
let sender = tx.sender();
@@ -47,7 +47,7 @@ impl<P: TransactionPool> TxPoolApi<P> {
4747
pending.entry(sender).or_default().insert(tx.nonce(), entry);
4848
}
4949

50-
TxPoolContent { pending, queued: HashMap::new() }
50+
TxPoolContent { pending, queued: BTreeMap::new() }
5151
}
5252
}
5353

@@ -68,7 +68,7 @@ impl<P: TransactionPool + 'static> TxPoolApiServer for TxPoolApi<P> {
6868

6969
async fn txpool_inspect(&self) -> RpcResult<TxPoolInspect> {
7070
let txs = self.pool.take_transactions_snapshot();
71-
let mut pending: HashMap<ContractAddress, HashMap<_, _>> = HashMap::new();
71+
let mut pending: BTreeMap<ContractAddress, BTreeMap<_, _>> = BTreeMap::new();
7272

7373
for tx in txs {
7474
let summary = format!(
@@ -82,6 +82,6 @@ impl<P: TransactionPool + 'static> TxPoolApiServer for TxPoolApi<P> {
8282
pending.entry(tx.sender()).or_default().insert(tx.nonce(), summary);
8383
}
8484

85-
Ok(TxPoolInspect { pending, queued: HashMap::new() })
85+
Ok(TxPoolInspect { pending, queued: BTreeMap::new() })
8686
}
8787
}

crates/rpc/rpc-types/src/txpool.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::collections::BTreeMap;
22

33
use katana_primitives::contract::Nonce;
44
use katana_primitives::transaction::TxHash;
@@ -37,9 +37,9 @@ pub struct TxPoolTransaction {
3737
#[derive(Debug, Clone, Serialize, Deserialize)]
3838
pub struct TxPoolContent {
3939
/// Transactions ready for execution, keyed by sender then nonce.
40-
pub pending: HashMap<ContractAddress, HashMap<Nonce, TxPoolTransaction>>,
40+
pub pending: BTreeMap<ContractAddress, BTreeMap<Nonce, TxPoolTransaction>>,
4141
/// Transactions waiting on a nonce gap. Always empty for now.
42-
pub queued: HashMap<ContractAddress, HashMap<Nonce, TxPoolTransaction>>,
42+
pub queued: BTreeMap<ContractAddress, BTreeMap<Nonce, TxPoolTransaction>>,
4343
}
4444

4545
/// Response for `txpool_inspect`.
@@ -50,7 +50,7 @@ pub struct TxPoolContent {
5050
#[derive(Debug, Clone, Serialize, Deserialize)]
5151
pub struct TxPoolInspect {
5252
/// Textual summaries of pending transactions, keyed by sender then nonce.
53-
pub pending: HashMap<ContractAddress, HashMap<Nonce, String>>,
53+
pub pending: BTreeMap<ContractAddress, BTreeMap<Nonce, String>>,
5454
/// Textual summaries of queued transactions. Always empty for now.
55-
pub queued: HashMap<ContractAddress, HashMap<Nonce, String>>,
55+
pub queued: BTreeMap<ContractAddress, BTreeMap<Nonce, String>>,
5656
}

0 commit comments

Comments
 (0)