diff --git a/binaries/cuprated/src/rpc/helper.rs b/binaries/cuprated/src/rpc/helper.rs index 31e2171..8f7c671 100644 --- a/binaries/cuprated/src/rpc/helper.rs +++ b/binaries/cuprated/src/rpc/helper.rs @@ -6,15 +6,13 @@ //! These build on-top of [`crate::rpc::request`] functions. use anyhow::{anyhow, Error}; -use monero_serai::block::Block; -use cuprate_consensus::{BlockChainContext, BlockChainContextService}; use cuprate_helper::{ cast::{u64_to_usize, usize_to_u64}, map::split_u128_into_low_high_bits, }; use cuprate_rpc_types::misc::{BlockHeader, KeyImageSpentStatus}; -use cuprate_types::{ExtendedBlockHeader, HardFork}; +use cuprate_types::HardFork; use crate::{ rpc::request::{blockchain, blockchain_context}, @@ -150,6 +148,7 @@ pub(super) async fn check_height( } /// Parse a hexadecimal [`String`] as a 32-byte hash. +#[expect(clippy::needless_pass_by_value)] pub(super) fn hex_to_hash(hex: String) -> Result<[u8; 32], Error> { let error = || anyhow!("Failed to parse hex representation of hash. Hex = {hex}."); diff --git a/binaries/cuprated/src/rpc/json.rs b/binaries/cuprated/src/rpc/json.rs index 134e05a..8b78d79 100644 --- a/binaries/cuprated/src/rpc/json.rs +++ b/binaries/cuprated/src/rpc/json.rs @@ -5,6 +5,7 @@ //! use std::{ + net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4}, num::NonZero, time::{Duration, Instant}, }; @@ -12,9 +13,7 @@ use std::{ use anyhow::{anyhow, Error}; use monero_serai::block::Block; use strum::{EnumCount, VariantArray}; -use tower::{Service, ServiceExt}; -use cuprate_consensus::{BlockchainReadRequest, BlockchainResponse}; use cuprate_constants::{ build::RELEASE, rpc::{RESTRICTED_BLOCK_COUNT, RESTRICTED_BLOCK_HEADER_RANGE}, @@ -129,7 +128,7 @@ pub(super) async fn map_request( /// async fn get_block_count( mut state: CupratedRpcHandler, - request: GetBlockCountRequest, + _: GetBlockCountRequest, ) -> Result { Ok(GetBlockCountResponse { base: ResponseBase::OK, @@ -367,7 +366,7 @@ async fn get_block( /// async fn get_connections( state: CupratedRpcHandler, - request: GetConnectionsRequest, + _: GetConnectionsRequest, ) -> Result { let connections = address_book::connection_info::(&mut DummyAddressBook).await?; @@ -380,7 +379,7 @@ async fn get_connections( /// async fn get_info( mut state: CupratedRpcHandler, - request: GetInfoRequest, + _: GetInfoRequest, ) -> Result { let restricted = state.is_restricted(); let context = blockchain_context::context(&mut state.blockchain_context).await?; @@ -403,7 +402,7 @@ async fn get_info( let (bootstrap_daemon_address, was_bootstrap_ever_used) = if restricted { (String::new(), false) } else { - todo!() + todo!("support bootstrap daemon") }; let busy_syncing = blockchain_manager::syncing(&mut state.blockchain_manager).await?; let (cumulative_difficulty, cumulative_difficulty_top64) = @@ -548,7 +547,12 @@ async fn set_bans( request: SetBansRequest, ) -> Result { for peer in request.bans { - let address = todo!(); + // TODO: support non-clearnet addresses. + + // + let [a, b, c, d] = peer.ip.to_le_bytes(); + let ip = Ipv4Addr::new(a, b, c, d); + let address = SocketAddr::V4(SocketAddrV4::new(ip, todo!("p2p port?"))); let ban = if peer.ban { Some(Duration::from_secs(peer.seconds.into())) @@ -567,12 +571,11 @@ async fn set_bans( } /// -async fn get_bans( - state: CupratedRpcHandler, - request: GetBansRequest, -) -> Result { +async fn get_bans(state: CupratedRpcHandler, _: GetBansRequest) -> Result { let now = Instant::now(); + // TODO: support non-clearnet addresses. + let bans = address_book::get_bans::(&mut DummyAddressBook) .await? .into_iter() @@ -588,9 +591,15 @@ async fn get_bans( 0 }; + // + let ip = match ban.address.ip() { + IpAddr::V4(v4) => u32::from_le_bytes(v4.octets()), + IpAddr::V6(v6) => todo!(), + }; + GetBan { host: ban.address.to_string(), - ip: todo!(), + ip, seconds, } }) @@ -607,7 +616,7 @@ async fn banned( state: CupratedRpcHandler, request: BannedRequest, ) -> Result { - let peer = match request.address.parse::() { + let peer = match request.address.parse::() { Ok(p) => p, Err(e) => { return Err(anyhow!( @@ -716,7 +725,7 @@ async fn get_coinbase_tx_sum( /// async fn get_version( mut state: CupratedRpcHandler, - request: GetVersionRequest, + _: GetVersionRequest, ) -> Result { let current_height = helper::top_height(&mut state).await?.0; let target_height = blockchain_manager::target_height(&mut state.blockchain_manager).await?; @@ -766,7 +775,7 @@ async fn get_fee_estimate( /// async fn get_alternate_chains( mut state: CupratedRpcHandler, - request: GetAlternateChainsRequest, + _: GetAlternateChainsRequest, ) -> Result { let chains = blockchain::alt_chains(&mut state.blockchain_read) .await? @@ -813,7 +822,7 @@ async fn relay_tx( /// async fn sync_info( mut state: CupratedRpcHandler, - request: SyncInfoRequest, + _: SyncInfoRequest, ) -> Result { let height = usize_to_u64( blockchain_context::context(&mut state.blockchain_context) @@ -854,7 +863,7 @@ async fn sync_info( /// async fn get_transaction_pool_backlog( mut state: CupratedRpcHandler, - request: GetTransactionPoolBacklogRequest, + _: GetTransactionPoolBacklogRequest, ) -> Result { let backlog = txpool::backlog(&mut state.txpool_read) .await? @@ -908,7 +917,7 @@ async fn get_output_distribution( /// async fn get_miner_data( mut state: CupratedRpcHandler, - request: GetMinerDataRequest, + _: GetMinerDataRequest, ) -> Result { let context = blockchain_context::context(&mut state.blockchain_context).await?; let c = context.unchecked_blockchain_context(); @@ -963,10 +972,10 @@ async fn prune_blockchain( /// async fn calc_pow( mut state: CupratedRpcHandler, - mut request: CalcPowRequest, + request: CalcPowRequest, ) -> Result { let hardfork = HardFork::from_version(request.major_version)?; - let mut block_blob: Vec = hex::decode(request.block_blob)?; + let block_blob: Vec = hex::decode(request.block_blob)?; let block = Block::read(&mut block_blob.as_slice())?; let seed_hash = helper::hex_to_hash(request.seed_hash)?; @@ -1020,7 +1029,7 @@ async fn add_aux_pow( /// fn add_aux_pow_inner( - mut state: CupratedRpcHandler, + state: CupratedRpcHandler, request: AddAuxPowRequest, ) -> Result { let Some(non_zero_len) = NonZero::::new(request.aux_pow.len()) else { diff --git a/binaries/cuprated/src/rpc/request/blockchain_context.rs b/binaries/cuprated/src/rpc/request/blockchain_context.rs index 6a8c315..4fd650e 100644 --- a/binaries/cuprated/src/rpc/request/blockchain_context.rs +++ b/binaries/cuprated/src/rpc/request/blockchain_context.rs @@ -8,7 +8,6 @@ use cuprate_consensus_context::{ BlockChainContext, BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService, }; -use cuprate_helper::cast::u64_to_usize; use cuprate_types::{FeeEstimate, HardFork, HardForkInfo}; // FIXME: use `anyhow::Error` over `tower::BoxError` in blockchain context.