Skip to content

Commit d6f8ec3

Browse files
committed
Implement mempool RPC commands and fix API compatibility
This commit implements three previously stubbed mempool RPC commands and includes API compatibility fixes similar to PR #65. New RPC implementations: - GetMempoolInfo: Returns mempool statistics (transaction count, total size) - GetRawMempool: Returns all transaction hashes from the entire mempool - GetRawMempoolShard: Returns transaction hashes filtered by chain_id/shard API compatibility fixes: - Update mempool access to use tx_map and current_size_bytes fields - Update shard method calls to use backend accessor - Handle BlockHeader.hash() now returning Option<&Hash256> - Update BlockData field access (transactions renamed to txs) - Update BlockHeader field access (pos renamed to height) - Remove timestamp field usage (removed from BlockHeader in protocol update) - Remove transaction fee field usage (no longer stored in blocks) New struct added: - RawMempool: Contains transaction_hashes vector and total_transactions count
1 parent ecbf1e6 commit d6f8ec3

File tree

1 file changed

+62
-23
lines changed

1 file changed

+62
-23
lines changed

src/node/rpc.rs

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ pub struct BlockStats {
8080
pub shard_id: u8,
8181
}
8282

83+
#[derive(Debug, Serialize, Deserialize, Clone)]
84+
pub struct RawMempool {
85+
pub transaction_hashes: Vec<String>,
86+
pub total_transactions: u32,
87+
}
88+
8389
#[tarpc::service]
8490
pub trait RpcServerDefinition {
8591
/// Returns information about the Blockchain
@@ -101,13 +107,13 @@ pub trait RpcServerDefinition {
101107
async fn get_shard_info(chain_id: u8) -> String;
102108

103109
/// Returns info about the mempool
104-
async fn get_mempool_info() -> String;
110+
async fn get_mempool_info() -> Result<MempoolSummary, RpcErr>;
105111

106112
/// Returns the raw mempool data for a shard
107-
async fn get_raw_mempool_shard(chain_id: u8) -> String;
113+
async fn get_raw_mempool_shard(chain_id: u8) -> Result<RawMempool, RpcErr>;
108114

109115
/// Returns the raw mempool data for all shards
110-
async fn get_raw_mempool() -> String;
116+
async fn get_raw_mempool() -> Result<RawMempool, RpcErr>;
111117

112118
/// Marks the block with the given hash as precious
113119
async fn precious_block(block_hash: String) -> String;
@@ -289,12 +295,8 @@ impl<B: PowChainBackend + ShardBackend + DBInterface + Send + Sync + 'static> Rp
289295
// Get mempool information
290296
let mempool = self.chain.mempool.read();
291297
let mempool_summary = MempoolSummary {
292-
total_transactions: mempool.transactions.len() as u32,
293-
total_size_bytes: mempool
294-
.transactions
295-
.values()
296-
.map(|tx| tx.len())
297-
.sum::<usize>() as u64,
298+
total_transactions: mempool.tx_map.len() as u32,
299+
total_size_bytes: mempool.current_size_bytes,
298300
};
299301

300302
// Get node information
@@ -332,12 +334,31 @@ impl<B: PowChainBackend + ShardBackend + DBInterface + Send + Sync + 'static> Rp
332334
})
333335
}
334336

335-
async fn get_mempool_info(self, _: context::Context) -> String {
336-
"Hello world!".to_string()
337+
async fn get_mempool_info(self, _: context::Context) -> Result<MempoolSummary, RpcErr> {
338+
// Get mempool information
339+
let mempool = self.chain.mempool.read();
340+
let mempool_summary = MempoolSummary {
341+
total_transactions: mempool.tx_map.len() as u32,
342+
total_size_bytes: mempool.current_size_bytes,
343+
};
344+
345+
Ok(mempool_summary)
337346
}
338347

339-
async fn get_raw_mempool(self, _: context::Context) -> String {
340-
"Hello world!".to_string()
348+
async fn get_raw_mempool(self, _: context::Context) -> Result<RawMempool, RpcErr> {
349+
// Get all transactions from mempool
350+
let mempool = self.chain.mempool.read();
351+
let transaction_hashes: Vec<String> = mempool
352+
.tx_map
353+
.keys()
354+
.map(|hash| hash.to_hex())
355+
.collect();
356+
let total_transactions = transaction_hashes.len() as u32;
357+
358+
Ok(RawMempool {
359+
transaction_hashes,
360+
total_transactions,
361+
})
341362
}
342363

343364
async fn get_sector_height(self, _: context::Context, sector_id: u8) -> Result<u64, RpcErr> {
@@ -366,8 +387,25 @@ impl<B: PowChainBackend + ShardBackend + DBInterface + Send + Sync + 'static> Rp
366387
"Hello world!".to_string()
367388
}
368389

369-
async fn get_raw_mempool_shard(self, _: context::Context, chain_id: u8) -> String {
370-
"Hello world!".to_string()
390+
async fn get_raw_mempool_shard(
391+
self,
392+
_: context::Context,
393+
chain_id: u8,
394+
) -> Result<RawMempool, RpcErr> {
395+
// Filter transactions by shard/chain_id
396+
let mempool = self.chain.mempool.read();
397+
let transaction_hashes: Vec<String> = mempool
398+
.tx_map
399+
.iter()
400+
.filter(|(_, tx)| tx.tx.tx.chain_id == chain_id)
401+
.map(|(hash, _)| hash.to_hex())
402+
.collect();
403+
let total_transactions = transaction_hashes.len() as u32;
404+
405+
Ok(RawMempool {
406+
transaction_hashes,
407+
total_transactions,
408+
})
371409
}
372410

373411
async fn get_block_hash(
@@ -384,8 +422,8 @@ impl<B: PowChainBackend + ShardBackend + DBInterface + Send + Sync + 'static> Rp
384422
.ok_or(RpcErr::ShardNotInitialised)?;
385423

386424
// Get the canonical block header at the specified height
387-
match shard.get_canonical_block_at_height(height) {
388-
Ok(Some(block_header)) => Ok(block_header.hash().to_hex()),
425+
match shard.backend.get_canonical_block_at_height(height) {
426+
Ok(Some(block_header)) => Ok(block_header.hash().unwrap().to_hex()),
389427
Ok(None) => {
390428
// No block at this height
391429
Err(RpcErr::ShardBackendErr)
@@ -412,16 +450,17 @@ impl<B: PowChainBackend + ShardBackend + DBInterface + Send + Sync + 'static> Rp
412450
// Search through all active shards to find the block
413451
for (shard_id, shard) in &self.chain.chain_states {
414452
// Try to get the canonical block with this hash
415-
match shard.get_canonical_block(&target_hash) {
453+
match shard.backend.get_canonical_block(&target_hash) {
416454
Ok(Some(block_header)) => {
417455
// Get the block data to calculate stats
418456
let block_data = shard
419-
.get_block_data(&target_hash)
457+
.backend.get_block_data(&target_hash)
420458
.map_err(|_| RpcErr::ShardBackendErr)?;
421459

422460
let (transaction_count, total_fees) = if let Some(data) = block_data {
423-
let tx_count = data.transactions.len() as u32;
424-
let fees = data.transactions.iter().map(|tx| tx.fee).sum::<u64>();
461+
let tx_count = data.txs.len() as u32;
462+
// Note: Transaction fees no longer stored in blocks after protocol update
463+
let fees = 0u64;
425464
(tx_count, fees)
426465
} else {
427466
(0, 0)
@@ -434,8 +473,8 @@ impl<B: PowChainBackend + ShardBackend + DBInterface + Send + Sync + 'static> Rp
434473

435474
return Ok(BlockStats {
436475
hash: hash.clone(),
437-
height: block_header.pos,
438-
timestamp: block_header.timestamp,
476+
height: block_header.height,
477+
timestamp: 0, // Timestamp removed from block headers in protocol update
439478
transaction_count,
440479
block_size_bytes: block_size,
441480
total_fees,

0 commit comments

Comments
 (0)