mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-12-23 03:59:37 +00:00
Compare commits
No commits in common. "a3fa638d91f171fda151eafc23dd46b9b5f2a939" and "de7abfc3f004673f4e1fb0fd38545af78ccad90d" have entirely different histories.
a3fa638d91
...
de7abfc3f0
18 changed files with 190 additions and 110 deletions
|
@ -31,7 +31,7 @@ cuprate-txpool = { workspace = true }
|
|||
cuprate-database = { workspace = true }
|
||||
cuprate-pruning = { workspace = true }
|
||||
cuprate-test-utils = { workspace = true }
|
||||
cuprate-types = { workspace = true, features = ["json"] }
|
||||
cuprate-types = { workspace = true }
|
||||
cuprate-json-rpc = { workspace = true }
|
||||
cuprate-rpc-interface = { workspace = true }
|
||||
cuprate-rpc-types = { workspace = true }
|
||||
|
|
|
@ -140,7 +140,7 @@ pub type BlockchainManagerHandle = cuprate_database_service::DatabaseReadService
|
|||
/// TODO
|
||||
#[derive(Clone)]
|
||||
pub struct CupratedRpcHandler {
|
||||
/// Should this RPC server be [restricted](RpcHandler::is_restricted)?
|
||||
/// Should this RPC server be [restricted](RpcHandler::restricted)?
|
||||
///
|
||||
/// This is not `pub` on purpose, as it should not be mutated after [`Self::new`].
|
||||
restricted: bool,
|
||||
|
|
|
@ -6,13 +6,15 @@
|
|||
//! 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::HardFork;
|
||||
use cuprate_types::{ExtendedBlockHeader, HardFork};
|
||||
|
||||
use crate::{
|
||||
rpc::request::{blockchain, blockchain_context},
|
||||
|
@ -140,7 +142,7 @@ pub(super) async fn check_height(
|
|||
|
||||
if height > top_height {
|
||||
return Err(anyhow!(
|
||||
"Requested block height: {height} greater than top block height: {top_height}",
|
||||
"Requested block height: {height} greater than current top block height: {top_height}",
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -148,7 +150,6 @@ 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}.");
|
||||
|
||||
|
@ -163,7 +164,7 @@ pub(super) fn hex_to_hash(hex: String) -> Result<[u8; 32], Error> {
|
|||
Ok(hash)
|
||||
}
|
||||
|
||||
/// [`cuprate_types::blockchain::BlockchainResponse::ChainHeight`] minus 1.
|
||||
/// [`BlockchainResponse::ChainHeight`] minus 1.
|
||||
pub(super) async fn top_height(state: &mut CupratedRpcHandler) -> Result<(u64, [u8; 32]), Error> {
|
||||
let (chain_height, hash) = blockchain::chain_height(&mut state.blockchain_read).await?;
|
||||
let height = chain_height.saturating_sub(1);
|
||||
|
|
|
@ -1,19 +1,17 @@
|
|||
//! RPC request handler functions (JSON-RPC).
|
||||
//!
|
||||
//! TODO:
|
||||
//! Many handlers have `todo!()`s for other Cuprate internals that must be completed, see:
|
||||
//! Many handlers have `todo!()`s for internals that must be completed, see:
|
||||
//! <https://github.com/Cuprate/cuprate/pull/308>
|
||||
|
||||
use std::{
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4},
|
||||
num::NonZero,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
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},
|
||||
|
@ -116,6 +114,9 @@ pub(super) async fn map_request(
|
|||
Req::GetTransactionPoolBacklog(r) => {
|
||||
Resp::GetTransactionPoolBacklog(get_transaction_pool_backlog(state, r).await?)
|
||||
}
|
||||
Req::GetOutputDistribution(r) => {
|
||||
Resp::GetOutputDistribution(get_output_distribution(state, r).await?)
|
||||
}
|
||||
Req::GetMinerData(r) => Resp::GetMinerData(get_miner_data(state, r).await?),
|
||||
Req::PruneBlockchain(r) => Resp::PruneBlockchain(prune_blockchain(state, r).await?),
|
||||
Req::CalcPow(r) => Resp::CalcPow(calc_pow(state, r).await?),
|
||||
|
@ -128,7 +129,7 @@ pub(super) async fn map_request(
|
|||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L1790-L1804>
|
||||
async fn get_block_count(
|
||||
mut state: CupratedRpcHandler,
|
||||
_: GetBlockCountRequest,
|
||||
request: GetBlockCountRequest,
|
||||
) -> Result<GetBlockCountResponse, Error> {
|
||||
Ok(GetBlockCountResponse {
|
||||
base: ResponseBase::OK,
|
||||
|
@ -366,7 +367,7 @@ async fn get_block(
|
|||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2729-L2738>
|
||||
async fn get_connections(
|
||||
state: CupratedRpcHandler,
|
||||
_: GetConnectionsRequest,
|
||||
request: GetConnectionsRequest,
|
||||
) -> Result<GetConnectionsResponse, Error> {
|
||||
let connections = address_book::connection_info::<ClearNet>(&mut DummyAddressBook).await?;
|
||||
|
||||
|
@ -379,7 +380,7 @@ async fn get_connections(
|
|||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L501-L582>
|
||||
async fn get_info(
|
||||
mut state: CupratedRpcHandler,
|
||||
_: GetInfoRequest,
|
||||
request: GetInfoRequest,
|
||||
) -> Result<GetInfoResponse, Error> {
|
||||
let restricted = state.is_restricted();
|
||||
let context = blockchain_context::context(&mut state.blockchain_context).await?;
|
||||
|
@ -402,7 +403,7 @@ async fn get_info(
|
|||
let (bootstrap_daemon_address, was_bootstrap_ever_used) = if restricted {
|
||||
(String::new(), false)
|
||||
} else {
|
||||
todo!("support bootstrap daemon")
|
||||
todo!()
|
||||
};
|
||||
let busy_syncing = blockchain_manager::syncing(&mut state.blockchain_manager).await?;
|
||||
let (cumulative_difficulty, cumulative_difficulty_top64) =
|
||||
|
@ -547,12 +548,7 @@ async fn set_bans(
|
|||
request: SetBansRequest,
|
||||
) -> Result<SetBansResponse, Error> {
|
||||
for peer in request.bans {
|
||||
// TODO: support non-clearnet addresses.
|
||||
|
||||
// <https://architecture.cuprate.org/oddities/le-ipv4.html>
|
||||
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, 0));
|
||||
let address = todo!();
|
||||
|
||||
let ban = if peer.ban {
|
||||
Some(Duration::from_secs(peer.seconds.into()))
|
||||
|
@ -571,15 +567,16 @@ async fn set_bans(
|
|||
}
|
||||
|
||||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2768-L2801>
|
||||
async fn get_bans(state: CupratedRpcHandler, _: GetBansRequest) -> Result<GetBansResponse, Error> {
|
||||
async fn get_bans(
|
||||
state: CupratedRpcHandler,
|
||||
request: GetBansRequest,
|
||||
) -> Result<GetBansResponse, Error> {
|
||||
let now = Instant::now();
|
||||
|
||||
// TODO: support non-clearnet addresses.
|
||||
|
||||
let bans = address_book::get_bans::<ClearNet>(&mut DummyAddressBook)
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter_map(|ban| {
|
||||
.map(|ban| {
|
||||
let seconds = if let Some(instant) = ban.unban_instant {
|
||||
instant
|
||||
.checked_duration_since(now)
|
||||
|
@ -591,17 +588,11 @@ async fn get_bans(state: CupratedRpcHandler, _: GetBansRequest) -> Result<GetBan
|
|||
0
|
||||
};
|
||||
|
||||
// <https://architecture.cuprate.org/oddities/le-ipv4.html>
|
||||
let ip = match ban.address.ip() {
|
||||
IpAddr::V4(v4) => u32::from_le_bytes(v4.octets()),
|
||||
IpAddr::V6(v6) => return None,
|
||||
};
|
||||
|
||||
Some(GetBan {
|
||||
GetBan {
|
||||
host: ban.address.to_string(),
|
||||
ip,
|
||||
ip: todo!(),
|
||||
seconds,
|
||||
})
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
@ -616,7 +607,7 @@ async fn banned(
|
|||
state: CupratedRpcHandler,
|
||||
request: BannedRequest,
|
||||
) -> Result<BannedResponse, Error> {
|
||||
let peer = match request.address.parse::<SocketAddr>() {
|
||||
let peer = match request.address.parse::<std::net::SocketAddr>() {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
return Err(anyhow!(
|
||||
|
@ -725,7 +716,7 @@ async fn get_coinbase_tx_sum(
|
|||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2981-L2996>
|
||||
async fn get_version(
|
||||
mut state: CupratedRpcHandler,
|
||||
_: GetVersionRequest,
|
||||
request: GetVersionRequest,
|
||||
) -> Result<GetVersionResponse, Error> {
|
||||
let current_height = helper::top_height(&mut state).await?.0;
|
||||
let target_height = blockchain_manager::target_height(&mut state.blockchain_manager).await?;
|
||||
|
@ -775,7 +766,7 @@ async fn get_fee_estimate(
|
|||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L3033-L3064>
|
||||
async fn get_alternate_chains(
|
||||
mut state: CupratedRpcHandler,
|
||||
_: GetAlternateChainsRequest,
|
||||
request: GetAlternateChainsRequest,
|
||||
) -> Result<GetAlternateChainsResponse, Error> {
|
||||
let chains = blockchain::alt_chains(&mut state.blockchain_read)
|
||||
.await?
|
||||
|
@ -822,7 +813,7 @@ async fn relay_tx(
|
|||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L3306-L3330>
|
||||
async fn sync_info(
|
||||
mut state: CupratedRpcHandler,
|
||||
_: SyncInfoRequest,
|
||||
request: SyncInfoRequest,
|
||||
) -> Result<SyncInfoResponse, Error> {
|
||||
let height = usize_to_u64(
|
||||
blockchain_context::context(&mut state.blockchain_context)
|
||||
|
@ -863,7 +854,7 @@ async fn sync_info(
|
|||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L3332-L3350>
|
||||
async fn get_transaction_pool_backlog(
|
||||
mut state: CupratedRpcHandler,
|
||||
_: GetTransactionPoolBacklogRequest,
|
||||
request: GetTransactionPoolBacklogRequest,
|
||||
) -> Result<GetTransactionPoolBacklogResponse, Error> {
|
||||
let backlog = txpool::backlog(&mut state.txpool_read)
|
||||
.await?
|
||||
|
@ -917,7 +908,7 @@ async fn get_output_distribution(
|
|||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L1998-L2033>
|
||||
async fn get_miner_data(
|
||||
mut state: CupratedRpcHandler,
|
||||
_: GetMinerDataRequest,
|
||||
request: GetMinerDataRequest,
|
||||
) -> Result<GetMinerDataResponse, Error> {
|
||||
let context = blockchain_context::context(&mut state.blockchain_context).await?;
|
||||
let c = context.unchecked_blockchain_context();
|
||||
|
@ -972,28 +963,27 @@ async fn prune_blockchain(
|
|||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2035-L2070>
|
||||
async fn calc_pow(
|
||||
mut state: CupratedRpcHandler,
|
||||
request: CalcPowRequest,
|
||||
mut request: CalcPowRequest,
|
||||
) -> Result<CalcPowResponse, Error> {
|
||||
let hardfork = HardFork::from_version(request.major_version)?;
|
||||
let block_blob: Vec<u8> = hex::decode(request.block_blob)?;
|
||||
let mut block_blob: Vec<u8> = hex::decode(request.block_blob)?;
|
||||
let block = Block::read(&mut block_blob.as_slice())?;
|
||||
let seed_hash = helper::hex_to_hash(request.seed_hash)?;
|
||||
|
||||
// let block_weight = todo!();
|
||||
let block_weight = todo!("calculate block weight");
|
||||
|
||||
// let median_for_block_reward = blockchain_context::context(&mut state.blockchain_context)
|
||||
// .await?
|
||||
// .unchecked_blockchain_context()
|
||||
// .context_to_verify_block
|
||||
// .median_weight_for_block_reward;
|
||||
let median_for_block_reward = blockchain_context::context(&mut state.blockchain_context)
|
||||
.await?
|
||||
.unchecked_blockchain_context()
|
||||
.context_to_verify_block
|
||||
.median_weight_for_block_reward;
|
||||
|
||||
// if cuprate_consensus_rules::blocks::check_block_weight(block_weight, median_for_block_reward)
|
||||
// .is_err()
|
||||
// {
|
||||
// return Err(anyhow!("Block blob size is too big, rejecting block"));
|
||||
// }
|
||||
if cuprate_consensus_rules::blocks::check_block_weight(block_weight, median_for_block_reward)
|
||||
.is_err()
|
||||
{
|
||||
return Err(anyhow!("Block blob size is too big, rejecting block"));
|
||||
}
|
||||
|
||||
// TODO: will `CalculatePow` do the above checks?
|
||||
let pow_hash = blockchain_context::calculate_pow(
|
||||
&mut state.blockchain_context,
|
||||
hardfork,
|
||||
|
@ -1029,12 +1019,12 @@ async fn add_aux_pow(
|
|||
|
||||
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2072-L2207>
|
||||
fn add_aux_pow_inner(
|
||||
state: CupratedRpcHandler,
|
||||
mut state: CupratedRpcHandler,
|
||||
request: AddAuxPowRequest,
|
||||
) -> Result<AddAuxPowResponse, Error> {
|
||||
let Some(non_zero_len) = NonZero::<usize>::new(request.aux_pow.len()) else {
|
||||
if request.aux_pow.is_empty() {
|
||||
return Err(anyhow!("Empty `aux_pow` vector"));
|
||||
};
|
||||
}
|
||||
|
||||
let aux_pow = request
|
||||
.aux_pow
|
||||
|
@ -1050,41 +1040,21 @@ fn add_aux_pow_inner(
|
|||
// Boxed slices are used over `Vec` to slightly
|
||||
// safe-guard against accidently pushing to it.
|
||||
|
||||
// --- BEGIN AUX POW IMPL ---
|
||||
|
||||
// Original impl:
|
||||
// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2110-L2206>
|
||||
|
||||
let len = aux_pow.len();
|
||||
|
||||
// TODO: why is this here? it does nothing:
|
||||
// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2110-L2112>
|
||||
// let mut path_domain = 1_usize;
|
||||
// while 1 << path_domain < len {
|
||||
// path_domain += 1;
|
||||
// }
|
||||
|
||||
fn find_nonce(
|
||||
aux_pow: &[cuprate_types::AuxPow],
|
||||
non_zero_len: NonZero<usize>,
|
||||
aux_pow_len: usize,
|
||||
) -> Result<(u32, Box<[u32]>), Error> {
|
||||
/// <https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a/src/cryptonote_basic/merge_mining.cpp#L48>
|
||||
fn get_aux_slot(id: &[u8; 32], nonce: u32, n_aux_chains: NonZero<u32>) -> u32 {
|
||||
const HASH_SIZE: usize = 32;
|
||||
|
||||
let mut buf = [0; HASH_SIZE + size_of::<u32>() + 1];
|
||||
buf[..HASH_SIZE].copy_from_slice(id);
|
||||
|
||||
let v: [u8; 4] = nonce.to_le_bytes();
|
||||
buf[HASH_SIZE..HASH_SIZE + size_of::<u32>()].copy_from_slice(&v);
|
||||
|
||||
const HASH_KEY_MM_SLOT: u8 = b'm';
|
||||
buf[HASH_SIZE + size_of::<u32>()] = HASH_KEY_MM_SLOT;
|
||||
|
||||
fn sha256sum(buf: &[u8]) -> [u8; 32] {
|
||||
todo!()
|
||||
}
|
||||
let res = sha256sum(&buf);
|
||||
|
||||
let v = u32::from_le_bytes(res[..4].try_into().unwrap());
|
||||
|
||||
v % n_aux_chains.get()
|
||||
}
|
||||
let mut path_domain = 1_usize;
|
||||
while 1 << path_domain < len {
|
||||
path_domain += 1;
|
||||
}
|
||||
|
||||
fn find_nonce(aux_pow_len: usize) -> Result<(u32, Box<[u32]>), Error> {
|
||||
// INVARIANT: this must be the same `.len()` as `aux_pow`
|
||||
let mut slots: Box<[u32]> = vec![u32::MAX; aux_pow_len].into_boxed_slice();
|
||||
let mut slot_seen: Box<[bool]> = vec![false; aux_pow_len].into_boxed_slice();
|
||||
|
@ -1093,12 +1063,7 @@ fn add_aux_pow_inner(
|
|||
|
||||
for nonce in 0..=MAX_NONCE {
|
||||
for i in &mut slots {
|
||||
let slot_u32 = get_aux_slot(
|
||||
&aux_pow[u32_to_usize(*i)].id,
|
||||
nonce,
|
||||
non_zero_len.try_into().unwrap(),
|
||||
);
|
||||
|
||||
let slot_u32: u32 = todo!("const uint32_t slot = cryptonote::get_aux_slot(aux_pow[idx].first, nonce, aux_pow.size());");
|
||||
let slot = u32_to_usize(slot_u32);
|
||||
|
||||
if slot >= aux_pow_len {
|
||||
|
@ -1119,8 +1084,7 @@ fn add_aux_pow_inner(
|
|||
Err(anyhow!("Failed to find a suitable nonce"))
|
||||
}
|
||||
|
||||
let len = non_zero_len.get();
|
||||
let (nonce, slots) = find_nonce(&aux_pow, non_zero_len, len)?;
|
||||
let (nonce, slots) = find_nonce(len)?;
|
||||
|
||||
// FIXME: use iterator version.
|
||||
let (aux_pow_id_raw, aux_pow_raw) = {
|
||||
|
@ -1171,7 +1135,7 @@ fn add_aux_pow_inner(
|
|||
};
|
||||
|
||||
fn tree_hash(aux_pow_raw: &[[u8; 32]]) -> [u8; 32] {
|
||||
todo!("https://github.com/serai-dex/serai/pull/629")
|
||||
todo!("https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2163")
|
||||
}
|
||||
|
||||
fn encode_mm_depth(aux_pow_len: usize, nonce: u32) -> u64 {
|
||||
|
@ -1225,6 +1189,8 @@ fn add_aux_pow_inner(
|
|||
})
|
||||
.collect::<Vec<AuxPow>>();
|
||||
|
||||
// --- END AUX POW IMPL ---
|
||||
|
||||
Ok(AddAuxPowResponse {
|
||||
base: ResponseBase::OK,
|
||||
blocktemplate_blob,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//! Functions to send [`AddressBookRequest`]s.
|
||||
//! Functions for TODO: doc enum message.
|
||||
|
||||
use anyhow::{anyhow, Error};
|
||||
use tower::ServiceExt;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//! Functions to send [`BlockchainReadRequest`]s.
|
||||
//! Functions for [`BlockchainReadRequest`].
|
||||
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//! Functions to send [`BlockChainContextRequest`]s.
|
||||
//! Functions for [`BlockChainContextRequest`] and [`BlockChainContextResponse`].
|
||||
|
||||
use anyhow::{anyhow, Error};
|
||||
use monero_serai::block::Block;
|
||||
|
@ -8,6 +8,7 @@ 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.
|
||||
|
@ -76,7 +77,7 @@ pub(crate) async fn calculate_pow(
|
|||
seed_hash: [u8; 32],
|
||||
) -> Result<[u8; 32], Error> {
|
||||
let Some(height) = block.number() else {
|
||||
return Err(anyhow!("Block is missing height"));
|
||||
return Err(anyhow!("block is missing height"));
|
||||
};
|
||||
|
||||
let block = Box::new(block);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//! Functions to send [`BlockchainManagerRequest`]s.
|
||||
//! Functions for [`BlockchainManagerRequest`] & [`BlockchainManagerResponse`].
|
||||
|
||||
use anyhow::Error;
|
||||
use monero_serai::block::Block;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//! Functions to send [`TxpoolReadRequest`]s.
|
||||
//! Functions for [`TxpoolReadRequest`].
|
||||
|
||||
use std::convert::Infallible;
|
||||
|
||||
|
|
|
@ -424,7 +424,8 @@ impl<Z: BorshNetworkZone> Service<AddressBookRequest<Z>> for AddressBook<Z> {
|
|||
| AddressBookRequest::ConnectionCount
|
||||
| AddressBookRequest::SetBan(_)
|
||||
| AddressBookRequest::GetBans
|
||||
| AddressBookRequest::ConnectionInfo => {
|
||||
| AddressBookRequest::ConnectionInfo
|
||||
| AddressBookRequest::NextNeededPruningSeed => {
|
||||
todo!("finish https://github.com/Cuprate/cuprate/pull/297")
|
||||
}
|
||||
};
|
||||
|
|
|
@ -112,7 +112,8 @@ impl<N: NetworkZone> Service<AddressBookRequest<N>> for DummyAddressBook {
|
|||
| AddressBookRequest::ConnectionCount
|
||||
| AddressBookRequest::SetBan(_)
|
||||
| AddressBookRequest::GetBans
|
||||
| AddressBookRequest::ConnectionInfo => {
|
||||
| AddressBookRequest::ConnectionInfo
|
||||
| AddressBookRequest::NextNeededPruningSeed => {
|
||||
todo!("finish https://github.com/Cuprate/cuprate/pull/297")
|
||||
}
|
||||
}))
|
||||
|
|
|
@ -132,6 +132,9 @@ pub enum AddressBookRequest<Z: NetworkZone> {
|
|||
|
||||
/// Get the state of all bans.
|
||||
GetBans,
|
||||
|
||||
/// Get the next [`PruningSeed`] needed for a pruned sync.
|
||||
NextNeededPruningSeed,
|
||||
}
|
||||
|
||||
/// A response from the address book service.
|
||||
|
@ -169,4 +172,7 @@ pub enum AddressBookResponse<Z: NetworkZone> {
|
|||
|
||||
/// Response to [`AddressBookRequest::GetBans`].
|
||||
GetBans(Vec<BanState<Z::Addr>>),
|
||||
|
||||
/// Response to [`AddressBookRequest::NextNeededPruningSeed`].
|
||||
NextNeededPruningSeed(PruningSeed),
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ The error type must always be [`anyhow::Error`].
|
|||
The `RpcHandler` must also hold some state that is required
|
||||
for RPC server operation.
|
||||
|
||||
The only state currently needed is [`RpcHandler::is_restricted`], which determines if an RPC
|
||||
The only state currently needed is [`RpcHandler::restricted`], which determines if an RPC
|
||||
server is restricted or not, and thus, if some endpoints/methods are allowed or not.
|
||||
|
||||
# Unknown endpoint behavior
|
||||
|
|
|
@ -31,7 +31,7 @@ use crate::rpc_handler::RpcHandler;
|
|||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
|
||||
pub struct RpcHandlerDummy {
|
||||
/// Should this RPC server be [restricted](RpcHandler::is_restricted)?
|
||||
/// Should this RPC server be [restricted](RpcHandler::restricted)?
|
||||
///
|
||||
/// The dummy will honor this [`bool`]
|
||||
/// on restricted methods/endpoints.
|
||||
|
@ -85,6 +85,7 @@ impl Service<JsonRpcRequest> for RpcHandlerDummy {
|
|||
Req::GetTransactionPoolBacklog(_) => {
|
||||
Resp::GetTransactionPoolBacklog(Default::default())
|
||||
}
|
||||
Req::GetOutputDistribution(_) => Resp::GetOutputDistribution(Default::default()),
|
||||
Req::GetMinerData(_) => Resp::GetMinerData(Default::default()),
|
||||
Req::PruneBlockchain(_) => Resp::PruneBlockchain(Default::default()),
|
||||
Req::CalcPow(_) => Resp::CalcPow(Default::default()),
|
||||
|
|
|
@ -1623,6 +1623,7 @@ pub enum JsonRpcRequest {
|
|||
RelayTx(RelayTxRequest),
|
||||
SyncInfo(SyncInfoRequest),
|
||||
GetTransactionPoolBacklog(GetTransactionPoolBacklogRequest),
|
||||
GetOutputDistribution(GetOutputDistributionRequest),
|
||||
GetMinerData(GetMinerDataRequest),
|
||||
PruneBlockchain(PruneBlockchainRequest),
|
||||
CalcPow(CalcPowRequest),
|
||||
|
@ -1648,6 +1649,7 @@ impl RpcCallValue for JsonRpcRequest {
|
|||
Self::GetVersion(x) => x.is_restricted(),
|
||||
Self::GetFeeEstimate(x) => x.is_restricted(),
|
||||
Self::GetTransactionPoolBacklog(x) => x.is_restricted(),
|
||||
Self::GetOutputDistribution(x) => x.is_restricted(),
|
||||
Self::GetMinerData(x) => x.is_restricted(),
|
||||
Self::AddAuxPow(x) => x.is_restricted(),
|
||||
Self::GetTxIdsLoose(x) => x.is_restricted(),
|
||||
|
@ -1683,6 +1685,7 @@ impl RpcCallValue for JsonRpcRequest {
|
|||
Self::GetVersion(x) => x.is_empty(),
|
||||
Self::GetFeeEstimate(x) => x.is_empty(),
|
||||
Self::GetTransactionPoolBacklog(x) => x.is_empty(),
|
||||
Self::GetOutputDistribution(x) => x.is_empty(),
|
||||
Self::GetMinerData(x) => x.is_empty(),
|
||||
Self::AddAuxPow(x) => x.is_empty(),
|
||||
Self::GetTxIdsLoose(x) => x.is_empty(),
|
||||
|
@ -1755,6 +1758,7 @@ pub enum JsonRpcResponse {
|
|||
RelayTx(RelayTxResponse),
|
||||
SyncInfo(SyncInfoResponse),
|
||||
GetTransactionPoolBacklog(GetTransactionPoolBacklogResponse),
|
||||
GetOutputDistribution(GetOutputDistributionResponse),
|
||||
GetMinerData(GetMinerDataResponse),
|
||||
PruneBlockchain(PruneBlockchainResponse),
|
||||
CalcPow(CalcPowResponse),
|
||||
|
|
97
rpc/types/src/misc/address_type.rs
Normal file
97
rpc/types/src/misc/address_type.rs
Normal file
|
@ -0,0 +1,97 @@
|
|||
//! Types of network addresses; used in P2P.
|
||||
|
||||
use cuprate_epee_encoding::Marker;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[cfg(feature = "epee")]
|
||||
use cuprate_epee_encoding::{
|
||||
error,
|
||||
macros::bytes::{Buf, BufMut},
|
||||
EpeeValue,
|
||||
};
|
||||
|
||||
/// Used in [`crate::misc::ConnectionInfo::address_type`].
|
||||
#[doc = crate::macros::monero_definition_link!(
|
||||
cc73fe71162d564ffda8e549b79a350bca53c454,
|
||||
"epee/include/net/enums.h",
|
||||
39..=47
|
||||
)]
|
||||
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde", serde(untagged))]
|
||||
#[repr(u8)]
|
||||
pub enum AddressType {
|
||||
#[default]
|
||||
Invalid,
|
||||
Ipv4,
|
||||
Ipv6,
|
||||
I2p,
|
||||
Tor,
|
||||
}
|
||||
|
||||
impl AddressType {
|
||||
/// Convert [`Self`] to a [`u8`].
|
||||
///
|
||||
/// ```rust
|
||||
/// use cuprate_rpc_types::misc::AddressType as A;
|
||||
///
|
||||
/// assert_eq!(A::Invalid.to_u8(), 0);
|
||||
/// assert_eq!(A::Ipv4.to_u8(), 1);
|
||||
/// assert_eq!(A::Ipv6.to_u8(), 2);
|
||||
/// assert_eq!(A::I2p.to_u8(), 3);
|
||||
/// assert_eq!(A::Tor.to_u8(), 4);
|
||||
/// ```
|
||||
pub const fn to_u8(self) -> u8 {
|
||||
self as u8
|
||||
}
|
||||
|
||||
/// Convert a [`u8`] to a [`Self`].
|
||||
///
|
||||
/// # Errors
|
||||
/// This returns [`None`] if `u > 4`.
|
||||
///
|
||||
/// ```rust
|
||||
/// use cuprate_rpc_types::misc::AddressType as A;
|
||||
///
|
||||
/// assert_eq!(A::from_u8(0), Some(A::Invalid));
|
||||
/// assert_eq!(A::from_u8(1), Some(A::Ipv4));
|
||||
/// assert_eq!(A::from_u8(2), Some(A::Ipv6));
|
||||
/// assert_eq!(A::from_u8(3), Some(A::I2p));
|
||||
/// assert_eq!(A::from_u8(4), Some(A::Tor));
|
||||
/// assert_eq!(A::from_u8(5), None);
|
||||
/// ```
|
||||
pub const fn from_u8(u: u8) -> Option<Self> {
|
||||
Some(match u {
|
||||
0 => Self::Invalid,
|
||||
1 => Self::Ipv4,
|
||||
2 => Self::Ipv6,
|
||||
3 => Self::I2p,
|
||||
4 => Self::Tor,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AddressType> for u8 {
|
||||
fn from(value: AddressType) -> Self {
|
||||
value.to_u8()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "epee")]
|
||||
impl EpeeValue for AddressType {
|
||||
const MARKER: Marker = u8::MARKER;
|
||||
|
||||
fn read<B: Buf>(r: &mut B, marker: &Marker) -> error::Result<Self> {
|
||||
let u = u8::read(r, marker)?;
|
||||
Self::from_u8(u).ok_or(error::Error::Format("u8 was greater than 4"))
|
||||
}
|
||||
|
||||
fn write<B: BufMut>(self, w: &mut B) -> error::Result<()> {
|
||||
let u = self.to_u8();
|
||||
u8::write(u, w)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@
|
|||
)]
|
||||
|
||||
//---------------------------------------------------------------------------------------------------- Mod
|
||||
mod address_type;
|
||||
mod binary_string;
|
||||
mod distribution;
|
||||
mod key_image_spent_status;
|
||||
|
@ -21,6 +22,7 @@ mod pool_info_extent;
|
|||
mod status;
|
||||
mod tx_entry;
|
||||
|
||||
pub use address_type::AddressType;
|
||||
pub use binary_string::BinaryString;
|
||||
pub use distribution::{Distribution, DistributionCompressedBinary, DistributionUncompressed};
|
||||
pub use key_image_spent_status::KeyImageSpentStatus;
|
||||
|
|
|
@ -16,7 +16,7 @@ use strum::{
|
|||
|
||||
/// An enumeration of address types.
|
||||
///
|
||||
/// Used in `cuprate-p2p`, `cuprate-rpc-types`.
|
||||
/// Used in `cuprate_p2p` and `cuprate_types`
|
||||
///
|
||||
/// Original definition:
|
||||
/// - <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/epee/include/net/enums.h/#L49>
|
||||
|
|
Loading…
Reference in a new issue