From c5abf9bb98480f4d75bf0f063879366a90339269 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Fri, 13 Dec 2024 20:14:16 -0500 Subject: [PATCH] `/get_transaction_pool` --- binaries/cuprated/src/rpc/handler.rs | 3 + binaries/cuprated/src/rpc/other.rs | 105 +++++++++--------- .../src/rpc/request/blockchain_manager.rs | 14 +++ binaries/cuprated/src/rpc/request/txpool.rs | 28 +++++ rpc/types/src/from.rs | 11 +- storage/txpool/src/service/interface.rs | 14 ++- storage/txpool/src/service/read.rs | 11 ++ types/types/src/rpc/types.rs | 4 +- 8 files changed, 133 insertions(+), 57 deletions(-) diff --git a/binaries/cuprated/src/rpc/handler.rs b/binaries/cuprated/src/rpc/handler.rs index 66b8549..d52976f 100644 --- a/binaries/cuprated/src/rpc/handler.rs +++ b/binaries/cuprated/src/rpc/handler.rs @@ -95,6 +95,9 @@ pub enum BlockchainManagerRequest { account_public_address: String, extra_nonce: Vec, }, + + /// TODO + Stop, } /// TODO: use real type when public. diff --git a/binaries/cuprated/src/rpc/other.rs b/binaries/cuprated/src/rpc/other.rs index 29a5370..b6b97d7 100644 --- a/binaries/cuprated/src/rpc/other.rs +++ b/binaries/cuprated/src/rpc/other.rs @@ -483,7 +483,7 @@ async fn save_bc(mut state: CupratedRpcHandler, _: SaveBcRequest) -> Result async fn get_peer_list( - state: CupratedRpcHandler, + mut state: CupratedRpcHandler, request: GetPeerListRequest, ) -> Result { let (white_list, gray_list) = address_book::peerlist::(&mut DummyAddressBook).await?; @@ -495,54 +495,27 @@ async fn get_peer_list( }) } -/// -async fn set_log_hash_rate( - state: CupratedRpcHandler, - request: SetLogHashRateRequest, -) -> Result { - unreachable!(); - Ok(SetLogHashRateResponse { - base: helper::response_base(false), - }) -} - -/// -async fn set_log_level( - state: CupratedRpcHandler, - request: SetLogLevelRequest, -) -> Result { - todo!(); - Ok(SetLogLevelResponse { - base: helper::response_base(false), - }) -} - -/// -async fn set_log_categories( - state: CupratedRpcHandler, - request: SetLogCategoriesRequest, -) -> Result { - Ok(SetLogCategoriesResponse { - base: helper::response_base(false), - ..todo!() - }) -} - /// async fn get_transaction_pool( - state: CupratedRpcHandler, - request: GetTransactionPoolRequest, + mut state: CupratedRpcHandler, + _: GetTransactionPoolRequest, ) -> Result { + let include_sensitive_txs = !state.is_restricted(); + + let (transactions, spent_key_images) = + txpool::pool(&mut state.txpool_read, include_sensitive_txs).await?; + Ok(GetTransactionPoolResponse { base: helper::access_response_base(false), - ..todo!() + transactions, + spent_key_images, }) } /// async fn get_transaction_pool_stats( - state: CupratedRpcHandler, - request: GetTransactionPoolStatsRequest, + mut state: CupratedRpcHandler, + _: GetTransactionPoolStatsRequest, ) -> Result { Ok(GetTransactionPoolStatsResponse { base: helper::access_response_base(false), @@ -552,17 +525,17 @@ async fn get_transaction_pool_stats( /// async fn stop_daemon( - state: CupratedRpcHandler, - request: StopDaemonRequest, + mut state: CupratedRpcHandler, + _: StopDaemonRequest, ) -> Result { - todo!(); + blockchain_manager::stop(&mut state.blockchain_manager).await?; Ok(StopDaemonResponse { status: Status::Ok }) } /// async fn get_limit( - state: CupratedRpcHandler, - request: GetLimitRequest, + mut state: CupratedRpcHandler, + _: GetLimitRequest, ) -> Result { Ok(GetLimitResponse { base: helper::response_base(false), @@ -572,7 +545,7 @@ async fn get_limit( /// async fn set_limit( - state: CupratedRpcHandler, + mut state: CupratedRpcHandler, request: SetLimitRequest, ) -> Result { Ok(SetLimitResponse { @@ -583,7 +556,7 @@ async fn set_limit( /// async fn out_peers( - state: CupratedRpcHandler, + mut state: CupratedRpcHandler, request: OutPeersRequest, ) -> Result { Ok(OutPeersResponse { @@ -594,7 +567,7 @@ async fn out_peers( /// async fn in_peers( - state: CupratedRpcHandler, + mut state: CupratedRpcHandler, request: InPeersRequest, ) -> Result { Ok(InPeersResponse { @@ -605,8 +578,8 @@ async fn in_peers( /// async fn get_net_stats( - state: CupratedRpcHandler, - request: GetNetStatsRequest, + mut state: CupratedRpcHandler, + _: GetNetStatsRequest, ) -> Result { Ok(GetNetStatsResponse { base: helper::response_base(false), @@ -671,8 +644,8 @@ async fn pop_blocks( /// async fn get_transaction_pool_hashes( - state: CupratedRpcHandler, - request: GetTransactionPoolHashesRequest, + mut state: CupratedRpcHandler, + _: GetTransactionPoolHashesRequest, ) -> Result { Ok(GetTransactionPoolHashesResponse { base: helper::response_base(false), @@ -682,7 +655,7 @@ async fn get_transaction_pool_hashes( /// async fn get_public_nodes( - state: CupratedRpcHandler, + mut state: CupratedRpcHandler, request: GetPublicNodesRequest, ) -> Result { Ok(GetPublicNodesResponse { @@ -691,7 +664,7 @@ async fn get_public_nodes( }) } -//---------------------------------------------------------------------------------------------------- Unsupported RPC calls +//---------------------------------------------------------------------------------------------------- Unsupported RPC calls (for now) /// async fn set_bootstrap_daemon( @@ -709,6 +682,24 @@ async fn update( todo!(); } +/// +async fn set_log_level( + state: CupratedRpcHandler, + request: SetLogLevelRequest, +) -> Result { + todo!() +} + +/// +async fn set_log_categories( + state: CupratedRpcHandler, + request: SetLogCategoriesRequest, +) -> Result { + todo!() +} + +//---------------------------------------------------------------------------------------------------- Unsupported RPC calls (forever) + /// async fn start_mining( state: CupratedRpcHandler, @@ -732,3 +723,11 @@ async fn mining_status( ) -> Result { unreachable!(); } + +/// +async fn set_log_hash_rate( + state: CupratedRpcHandler, + request: SetLogHashRateRequest, +) -> Result { + unreachable!(); +} diff --git a/binaries/cuprated/src/rpc/request/blockchain_manager.rs b/binaries/cuprated/src/rpc/request/blockchain_manager.rs index ef4f2f4..0e7d3c2 100644 --- a/binaries/cuprated/src/rpc/request/blockchain_manager.rs +++ b/binaries/cuprated/src/rpc/request/blockchain_manager.rs @@ -256,3 +256,17 @@ pub(crate) async fn sync(blockchain_manager: &mut BlockchainManagerHandle) -> Re Ok(()) } + +/// [`BlockchainManagerRequest::Stop`] +pub(crate) async fn stop(blockchain_manager: &mut BlockchainManagerHandle) -> Result<(), Error> { + let BlockchainManagerResponse::Ok = blockchain_manager + .ready() + .await? + .call(BlockchainManagerRequest::Stop) + .await? + else { + unreachable!(); + }; + + Ok(()) +} diff --git a/binaries/cuprated/src/rpc/request/txpool.rs b/binaries/cuprated/src/rpc/request/txpool.rs index 95d49ef..ded73a7 100644 --- a/binaries/cuprated/src/rpc/request/txpool.rs +++ b/binaries/cuprated/src/rpc/request/txpool.rs @@ -7,6 +7,7 @@ use monero_serai::transaction::Transaction; use tower::{Service, ServiceExt}; use cuprate_helper::cast::usize_to_u64; +use cuprate_rpc_types::misc::{SpentKeyImageInfo, TxInfo}; use cuprate_txpool::{ service::{ interface::{TxpoolReadRequest, TxpoolReadResponse}, @@ -129,6 +130,33 @@ pub(crate) async fn key_images_spent( Ok(status) } +/// TODO +pub(crate) async fn pool( + txpool_read: &mut TxpoolReadHandle, + include_sensitive_txs: bool, +) -> Result<(Vec, Vec), Error> { + let TxpoolReadResponse::Pool { + txs, + spent_key_images, + } = txpool_read + .ready() + .await + .map_err(|e| anyhow!(e))? + .call(TxpoolReadRequest::Pool { + include_sensitive_txs, + }) + .await + .map_err(|e| anyhow!(e))? + else { + unreachable!(); + }; + + let txs = txs.into_iter().map(Into::into).collect(); + let spent_key_images = spent_key_images.into_iter().map(Into::into).collect(); + + Ok((txs, spent_key_images)) +} + /// TODO pub(crate) async fn flush( txpool_manager: &mut Infallible, diff --git a/rpc/types/src/from.rs b/rpc/types/src/from.rs index 1738a69..b5d94af 100644 --- a/rpc/types/src/from.rs +++ b/rpc/types/src/from.rs @@ -13,7 +13,7 @@ use cuprate_p2p_core::{ types::{ConnectionId, ConnectionInfo, SetBan, Span}, NetZoneAddress, }; -use cuprate_types::rpc::{BlockHeader, ChainInfo, HistogramEntry, TxInfo}; +use cuprate_types::rpc::{BlockHeader, ChainInfo, HistogramEntry, SpentKeyImageInfo, TxInfo}; impl From for crate::misc::BlockHeader { fn from(x: BlockHeader) -> Self { @@ -184,3 +184,12 @@ impl From for crate::misc::TxInfo { } } } + +impl From for crate::misc::SpentKeyImageInfo { + fn from(x: SpentKeyImageInfo) -> Self { + Self { + id_hash: Hex(x.id_hash), + txs_hashes: x.txs_hashes.into_iter().map(Hex).collect(), + } + } +} diff --git a/storage/txpool/src/service/interface.rs b/storage/txpool/src/service/interface.rs index a247923..047fd71 100644 --- a/storage/txpool/src/service/interface.rs +++ b/storage/txpool/src/service/interface.rs @@ -7,7 +7,10 @@ use std::{ sync::Arc, }; -use cuprate_types::{rpc::PoolInfo, TransactionVerificationData, TxInPool}; +use cuprate_types::{ + rpc::{PoolInfo, SpentKeyImageInfo, TxInfo}, + TransactionVerificationData, TxInPool, +}; use crate::{ tx::TxEntry, @@ -64,6 +67,9 @@ pub enum TxpoolReadRequest { key_images: Vec<[u8; 32]>, include_sensitive_txs: bool, }, + + /// TODO + Pool { include_sensitive_txs: bool }, } //---------------------------------------------------------------------------------------------------- TxpoolReadResponse @@ -112,6 +118,12 @@ pub enum TxpoolReadResponse { /// Response to [`TxpoolReadRequest::KeyImagesSpent`]. KeyImagesSpent(Vec), + + /// Response to [`TxpoolReadRequest::Pool`]. + Pool { + txs: Vec, + spent_key_images: Vec, + }, } //---------------------------------------------------------------------------------------------------- TxpoolWriteRequest diff --git a/storage/txpool/src/service/read.rs b/storage/txpool/src/service/read.rs index 1579808..8d393c9 100644 --- a/storage/txpool/src/service/read.rs +++ b/storage/txpool/src/service/read.rs @@ -89,6 +89,9 @@ fn map_request( key_images, include_sensitive_txs, } => key_images_spent(env, key_images, include_sensitive_txs), + TxpoolReadRequest::Pool { + include_sensitive_txs, + } => pool(env, include_sensitive_txs), } } @@ -249,3 +252,11 @@ fn key_images_spent( ) -> ReadResponseResult { Ok(TxpoolReadResponse::KeyImagesSpent(todo!())) } + +/// [`TxpoolReadRequest::Pool`]. +fn pool(env: &ConcreteEnv, include_sensitive_txs: bool) -> ReadResponseResult { + Ok(TxpoolReadResponse::Pool { + txs: todo!(), + spent_key_images: todo!(), + }) +} diff --git a/types/types/src/rpc/types.rs b/types/types/src/rpc/types.rs index 5515731..675d1fb 100644 --- a/types/types/src/rpc/types.rs +++ b/types/types/src/rpc/types.rs @@ -383,8 +383,8 @@ define_struct_and_impl_epee! { 1558..=1567 )] SpentKeyImageInfo { - id_hash: String, - txs_hashes: Vec, + id_hash: [u8; 32], + txs_hashes: Vec<[u8; 32]>, } #[doc = monero_definition_link!(