Skip to content

Commit 4c2c749

Browse files
hinto-janaiBoog900
andauthored
cuprated: CupratedRpcHandler, enable certain endpoints (#450)
* add rpc server * add init fn * add layers * docs * comments * move * warn * split config * split * fix toml * impl p2p port * fix tests * docs * doc * remove (de)compression * init rpc servers with handlers * add `rpc.md` * typo * sort * revert cargo.lock diff * 🟣 * not_available() * `advertise` * Update binaries/cuprated/src/config/rpc.rs Co-authored-by: Boog900 <boog900@tutanota.com> * update tracing * `tracing::field::display` * fix * typo * docs * clippy * remove comment_out * add test for `cuprate_helper::net::ip_is_local` * fix args * `/get_outs`, `/get_height` * enable methods * apply * block fn * fix * clippy * book * add warning * update * typos * Update binaries/cuprated/src/rpc/handlers/helper.rs Co-authored-by: Boog900 <boog900@tutanota.com> * fmt --------- Co-authored-by: Boog900 <boog900@tutanota.com>
1 parent cb41f20 commit 4c2c749

File tree

16 files changed

+309
-161
lines changed

16 files changed

+309
-161
lines changed

binaries/cuprated/src/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn main() {
133133
let tx_handler = IncomingTxHandler::init(
134134
network_interfaces.clearnet_network_interface.clone(),
135135
txpool_write_handle.clone(),
136-
txpool_read_handle,
136+
txpool_read_handle.clone(),
137137
context_svc.clone(),
138138
blockchain_read_handle.clone(),
139139
);
@@ -149,15 +149,20 @@ fn main() {
149149
blockchain::init_blockchain_manager(
150150
network_interfaces.clearnet_network_interface,
151151
blockchain_write_handle,
152-
blockchain_read_handle,
153-
txpool_write_handle,
152+
blockchain_read_handle.clone(),
153+
txpool_write_handle.clone(),
154154
context_svc.clone(),
155155
config.block_downloader_config(),
156156
)
157157
.await;
158158

159159
// Initialize the RPC server(s).
160-
rpc::init_rpc_servers(config.rpc);
160+
rpc::init_rpc_servers(
161+
config.rpc,
162+
blockchain_read_handle,
163+
context_svc.clone(),
164+
txpool_read_handle,
165+
);
161166

162167
// Start the command listener.
163168
if std::io::IsTerminal::is_terminal(&std::io::stdin()) {

binaries/cuprated/src/rpc/handlers/bin.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use cuprate_types::{
3030
};
3131

3232
use crate::rpc::{
33-
handlers::{helper, shared},
33+
handlers::{helper, shared, shared::not_available},
3434
service::{blockchain, txpool},
3535
CupratedRpcHandler,
3636
};
@@ -44,17 +44,13 @@ pub async fn map_request(
4444
use BinResponse as Resp;
4545

4646
Ok(match request {
47-
Req::GetBlocks(r) => Resp::GetBlocks(get_blocks(state, r).await?),
48-
Req::GetBlocksByHeight(r) => Resp::GetBlocksByHeight(get_blocks_by_height(state, r).await?),
49-
Req::GetHashes(r) => Resp::GetHashes(get_hashes(state, r).await?),
50-
Req::GetOutputIndexes(r) => Resp::GetOutputIndexes(get_output_indexes(state, r).await?),
51-
Req::GetOuts(r) => Resp::GetOuts(get_outs(state, r).await?),
52-
Req::GetTransactionPoolHashes(r) => {
53-
Resp::GetTransactionPoolHashes(get_transaction_pool_hashes(state, r).await?)
54-
}
55-
Req::GetOutputDistribution(r) => {
56-
Resp::GetOutputDistribution(get_output_distribution(state, r).await?)
57-
}
47+
Req::GetBlocks(r) => Resp::GetBlocks(not_available()?),
48+
Req::GetBlocksByHeight(r) => Resp::GetBlocksByHeight(not_available()?),
49+
Req::GetHashes(r) => Resp::GetHashes(not_available()?),
50+
Req::GetOutputIndexes(r) => Resp::GetOutputIndexes(not_available()?),
51+
Req::GetOuts(r) => Resp::GetOuts(not_available()?),
52+
Req::GetTransactionPoolHashes(r) => Resp::GetTransactionPoolHashes(not_available()?),
53+
Req::GetOutputDistribution(r) => Resp::GetOutputDistribution(not_available()?),
5854
})
5955
}
6056

binaries/cuprated/src/rpc/handlers/helper.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use cuprate_rpc_types::{
1515
base::{AccessResponseBase, ResponseBase},
1616
misc::BlockHeader,
1717
};
18-
use cuprate_types::HardFork;
18+
use cuprate_types::{Chain, HardFork};
1919
use monero_serai::transaction::Timelock;
2020

2121
use crate::rpc::{
@@ -55,18 +55,15 @@ pub(super) async fn block_header(
5555
let pow_hash = if fill_pow_hash {
5656
let seed_height =
5757
cuprate_consensus_rules::blocks::randomx_seed_height(u64_to_usize(height));
58-
let seed_hash = blockchain::block_hash(
59-
&mut state.blockchain_read,
60-
height,
61-
todo!("access to `cuprated`'s Chain"),
62-
)
63-
.await?;
58+
let seed_hash =
59+
blockchain::block_hash(&mut state.blockchain_read, height, Chain::Main).await?;
6460

6561
Some(
6662
blockchain_context::calculate_pow(
6763
&mut state.blockchain_context,
6864
hardfork,
69-
block,
65+
// TODO: expensive clone
66+
block.clone(),
7067
seed_hash,
7168
)
7269
.await?,

binaries/cuprated/src/rpc/handlers/json_rpc.rs

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use crate::{
6161
constants::VERSION_BUILD,
6262
rpc::{
6363
constants::{FIELD_NOT_SUPPORTED, UNSUPPORTED_RPC_CALL},
64-
handlers::{helper, shared},
64+
handlers::{helper, shared, shared::not_available},
6565
service::{address_book, blockchain, blockchain_context, blockchain_manager, txpool},
6666
CupratedRpcHandler,
6767
},
@@ -77,11 +77,11 @@ pub async fn map_request(
7777
use JsonRpcResponse as Resp;
7878

7979
Ok(match request {
80-
Req::GetBlockTemplate(r) => Resp::GetBlockTemplate(get_block_template(state, r).await?),
80+
Req::GetBlockTemplate(r) => Resp::GetBlockTemplate(not_available()?),
8181
Req::GetBlockCount(r) => Resp::GetBlockCount(get_block_count(state, r).await?),
8282
Req::OnGetBlockHash(r) => Resp::OnGetBlockHash(on_get_block_hash(state, r).await?),
83-
Req::SubmitBlock(r) => Resp::SubmitBlock(submit_block(state, r).await?),
84-
Req::GenerateBlocks(r) => Resp::GenerateBlocks(generate_blocks(state, r).await?),
83+
Req::SubmitBlock(r) => Resp::SubmitBlock(not_available()?),
84+
Req::GenerateBlocks(r) => Resp::GenerateBlocks(not_available()?),
8585
Req::GetLastBlockHeader(r) => {
8686
Resp::GetLastBlockHeader(get_last_block_header(state, r).await?)
8787
}
@@ -95,33 +95,25 @@ pub async fn map_request(
9595
Resp::GetBlockHeadersRange(get_block_headers_range(state, r).await?)
9696
}
9797
Req::GetBlock(r) => Resp::GetBlock(get_block(state, r).await?),
98-
Req::GetConnections(r) => Resp::GetConnections(get_connections(state, r).await?),
99-
Req::GetInfo(r) => Resp::GetInfo(get_info(state, r).await?),
100-
Req::HardForkInfo(r) => Resp::HardForkInfo(hard_fork_info(state, r).await?),
101-
Req::SetBans(r) => Resp::SetBans(set_bans(state, r).await?),
102-
Req::GetBans(r) => Resp::GetBans(get_bans(state, r).await?),
103-
Req::Banned(r) => Resp::Banned(banned(state, r).await?),
104-
Req::FlushTransactionPool(r) => {
105-
Resp::FlushTransactionPool(flush_transaction_pool(state, r).await?)
106-
}
107-
Req::GetOutputHistogram(r) => {
108-
Resp::GetOutputHistogram(get_output_histogram(state, r).await?)
109-
}
110-
Req::GetCoinbaseTxSum(r) => Resp::GetCoinbaseTxSum(get_coinbase_tx_sum(state, r).await?),
111-
Req::GetVersion(r) => Resp::GetVersion(get_version(state, r).await?),
112-
Req::GetFeeEstimate(r) => Resp::GetFeeEstimate(get_fee_estimate(state, r).await?),
113-
Req::GetAlternateChains(r) => {
114-
Resp::GetAlternateChains(get_alternate_chains(state, r).await?)
115-
}
116-
Req::RelayTx(r) => Resp::RelayTx(relay_tx(state, r).await?),
117-
Req::SyncInfo(r) => Resp::SyncInfo(sync_info(state, r).await?),
118-
Req::GetTransactionPoolBacklog(r) => {
119-
Resp::GetTransactionPoolBacklog(get_transaction_pool_backlog(state, r).await?)
120-
}
121-
Req::GetMinerData(r) => Resp::GetMinerData(get_miner_data(state, r).await?),
122-
Req::PruneBlockchain(r) => Resp::PruneBlockchain(prune_blockchain(state, r).await?),
123-
Req::CalcPow(r) => Resp::CalcPow(calc_pow(state, r).await?),
124-
Req::AddAuxPow(r) => Resp::AddAuxPow(add_aux_pow(state, r).await?),
98+
Req::GetConnections(r) => Resp::GetConnections(not_available()?),
99+
Req::GetInfo(r) => Resp::GetInfo(not_available()?),
100+
Req::HardForkInfo(r) => Resp::HardForkInfo(not_available()?),
101+
Req::SetBans(r) => Resp::SetBans(not_available()?),
102+
Req::GetBans(r) => Resp::GetBans(not_available()?),
103+
Req::Banned(r) => Resp::Banned(not_available()?),
104+
Req::FlushTransactionPool(r) => Resp::FlushTransactionPool(not_available()?),
105+
Req::GetOutputHistogram(r) => Resp::GetOutputHistogram(not_available()?),
106+
Req::GetCoinbaseTxSum(r) => Resp::GetCoinbaseTxSum(not_available()?),
107+
Req::GetVersion(r) => Resp::GetVersion(not_available()?),
108+
Req::GetFeeEstimate(r) => Resp::GetFeeEstimate(not_available()?),
109+
Req::GetAlternateChains(r) => Resp::GetAlternateChains(not_available()?),
110+
Req::RelayTx(r) => Resp::RelayTx(not_available()?),
111+
Req::SyncInfo(r) => Resp::SyncInfo(not_available()?),
112+
Req::GetTransactionPoolBacklog(r) => Resp::GetTransactionPoolBacklog(not_available()?),
113+
Req::GetMinerData(r) => Resp::GetMinerData(not_available()?),
114+
Req::PruneBlockchain(r) => Resp::PruneBlockchain(not_available()?),
115+
Req::CalcPow(r) => Resp::CalcPow(not_available()?),
116+
Req::AddAuxPow(r) => Resp::AddAuxPow(not_available()?),
125117

126118
// Unsupported RPC calls.
127119
Req::GetTxIdsLoose(_) | Req::FlushCache(_) => return Err(anyhow!(UNSUPPORTED_RPC_CALL)),
@@ -172,7 +164,7 @@ async fn get_block_template(
172164
seed_hash,
173165
next_seed_hash,
174166
} = *blockchain_manager::create_block_template(
175-
&mut state.blockchain_manager,
167+
todo!(),
176168
prev_block,
177169
request.wallet_address,
178170
request.extra_nonce.0,
@@ -242,7 +234,7 @@ async fn submit_block(
242234
let block_id = Hex(block.hash());
243235

244236
// Attempt to relay the block.
245-
blockchain_manager::relay_block(&mut state.blockchain_manager, Box::new(block)).await?;
237+
blockchain_manager::relay_block(todo!(), Box::new(block)).await?;
246238

247239
Ok(SubmitBlockResponse {
248240
base: helper::response_base(false),
@@ -269,7 +261,7 @@ async fn generate_blocks(
269261
};
270262

271263
let (blocks, height) = blockchain_manager::generate_blocks(
272-
&mut state.blockchain_manager,
264+
todo!(),
273265
request.amount_of_blocks,
274266
prev_block,
275267
request.starting_nonce,
@@ -479,7 +471,7 @@ async fn get_info(
479471
(String::new(), false)
480472
};
481473

482-
let busy_syncing = blockchain_manager::syncing(&mut state.blockchain_manager).await?;
474+
let busy_syncing = blockchain_manager::syncing(todo!()).await?;
483475

484476
let (cumulative_difficulty, cumulative_difficulty_top64) =
485477
split_u128_into_low_high_bits(cumulative_difficulty);
@@ -524,12 +516,10 @@ async fn get_info(
524516
let rpc_connections_count = if restricted { 0 } else { 0 };
525517

526518
let start_time = if restricted { 0 } else { *START_INSTANT_UNIX };
527-
let synchronized = blockchain_manager::synced(&mut state.blockchain_manager).await?;
519+
let synchronized = blockchain_manager::synced(todo!()).await?;
528520

529-
let target_height = blockchain_manager::target_height(&mut state.blockchain_manager).await?;
530-
let target = blockchain_manager::target(&mut state.blockchain_manager)
531-
.await?
532-
.as_secs();
521+
let target_height = blockchain_manager::target_height(todo!()).await?;
522+
let target = blockchain_manager::target(todo!()).await?.as_secs();
533523
let top_block_hash = Hex(c.top_hash);
534524

535525
let tx_count = blockchain::total_tx_count(&mut state.blockchain_read).await?;
@@ -738,7 +728,7 @@ async fn flush_transaction_pool(
738728
.map(|h| h.0)
739729
.collect::<Vec<[u8; 32]>>();
740730

741-
txpool::flush(&mut state.txpool_manager, tx_hashes).await?;
731+
txpool::flush(todo!(), tx_hashes).await?;
742732

743733
Ok(FlushTransactionPoolResponse { status: Status::Ok })
744734
}
@@ -807,7 +797,7 @@ async fn get_version(
807797
_: GetVersionRequest,
808798
) -> Result<GetVersionResponse, Error> {
809799
let current_height = helper::top_height(&mut state).await?.0;
810-
let target_height = blockchain_manager::target_height(&mut state.blockchain_manager).await?;
800+
let target_height = blockchain_manager::target_height(todo!()).await?;
811801

812802
let mut hard_forks = Vec::with_capacity(HardFork::COUNT);
813803

@@ -880,7 +870,7 @@ async fn relay_tx(
880870
.map(|h| h.0)
881871
.collect::<Vec<[u8; 32]>>();
882872

883-
txpool::relay(&mut state.txpool_manager, tx_hashes).await?;
873+
txpool::relay(todo!(), tx_hashes).await?;
884874

885875
Ok(RelayTxResponse { status: Status::Ok })
886876
}
@@ -892,20 +882,19 @@ async fn sync_info(
892882
) -> Result<SyncInfoResponse, Error> {
893883
let height = usize_to_u64(state.blockchain_context.blockchain_context().chain_height);
894884

895-
let target_height = blockchain_manager::target_height(&mut state.blockchain_manager).await?;
885+
let target_height = blockchain_manager::target_height(todo!()).await?;
896886

897887
let peers = address_book::connection_info::<ClearNet>(&mut DummyAddressBook)
898888
.await?
899889
.into_iter()
900890
.map(|info| SyncInfoPeer { info })
901891
.collect();
902892

903-
let next_needed_pruning_seed =
904-
blockchain_manager::next_needed_pruning_seed(&mut state.blockchain_manager)
905-
.await?
906-
.compress();
893+
let next_needed_pruning_seed = blockchain_manager::next_needed_pruning_seed(todo!())
894+
.await?
895+
.compress();
907896

908-
let spans = blockchain_manager::spans::<ClearNet>(&mut state.blockchain_manager).await?;
897+
let spans = blockchain_manager::spans::<ClearNet>(todo!()).await?;
909898

910899
// <https://github.com/Cuprate/cuprate/pull/320#discussion_r1811063772>
911900
let overview = String::from(FIELD_NOT_SUPPORTED);
@@ -994,10 +983,8 @@ async fn prune_blockchain(
994983
mut state: CupratedRpcHandler,
995984
request: PruneBlockchainRequest,
996985
) -> Result<PruneBlockchainResponse, Error> {
997-
let pruned = blockchain_manager::pruned(&mut state.blockchain_manager).await?;
998-
let pruning_seed = blockchain_manager::prune(&mut state.blockchain_manager)
999-
.await?
1000-
.compress();
986+
let pruned = blockchain_manager::pruned(todo!()).await?;
987+
let pruning_seed = blockchain_manager::prune(todo!()).await?.compress();
1001988

1002989
Ok(PruneBlockchainResponse {
1003990
base: helper::response_base(false),

binaries/cuprated/src/rpc/handlers/other_json.rs

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use cuprate_types::{
4949
use crate::{
5050
rpc::{
5151
constants::UNSUPPORTED_RPC_CALL,
52-
handlers::{helper, shared},
52+
handlers::{helper, shared, shared::not_available},
5353
service::{address_book, blockchain, blockchain_context, blockchain_manager, txpool},
5454
CupratedRpcHandler,
5555
},
@@ -66,36 +66,26 @@ pub async fn map_request(
6666

6767
Ok(match request {
6868
Req::GetHeight(r) => Resp::GetHeight(get_height(state, r).await?),
69-
Req::GetTransactions(r) => Resp::GetTransactions(get_transactions(state, r).await?),
70-
Req::GetAltBlocksHashes(r) => {
71-
Resp::GetAltBlocksHashes(get_alt_blocks_hashes(state, r).await?)
72-
}
73-
Req::IsKeyImageSpent(r) => Resp::IsKeyImageSpent(is_key_image_spent(state, r).await?),
74-
Req::SendRawTransaction(r) => {
75-
Resp::SendRawTransaction(send_raw_transaction(state, r).await?)
76-
}
77-
Req::SaveBc(r) => Resp::SaveBc(save_bc(state, r).await?),
78-
Req::GetPeerList(r) => Resp::GetPeerList(get_peer_list(state, r).await?),
79-
Req::SetLogLevel(r) => Resp::SetLogLevel(set_log_level(state, r).await?),
80-
Req::SetLogCategories(r) => Resp::SetLogCategories(set_log_categories(state, r).await?),
81-
Req::GetTransactionPool(r) => {
82-
Resp::GetTransactionPool(get_transaction_pool(state, r).await?)
83-
}
84-
Req::GetTransactionPoolStats(r) => {
85-
Resp::GetTransactionPoolStats(get_transaction_pool_stats(state, r).await?)
86-
}
87-
Req::StopDaemon(r) => Resp::StopDaemon(stop_daemon(state, r).await?),
88-
Req::GetLimit(r) => Resp::GetLimit(get_limit(state, r).await?),
89-
Req::SetLimit(r) => Resp::SetLimit(set_limit(state, r).await?),
90-
Req::OutPeers(r) => Resp::OutPeers(out_peers(state, r).await?),
91-
Req::InPeers(r) => Resp::InPeers(in_peers(state, r).await?),
92-
Req::GetNetStats(r) => Resp::GetNetStats(get_net_stats(state, r).await?),
93-
Req::GetOuts(r) => Resp::GetOuts(get_outs(state, r).await?),
94-
Req::PopBlocks(r) => Resp::PopBlocks(pop_blocks(state, r).await?),
95-
Req::GetTransactionPoolHashes(r) => {
96-
Resp::GetTransactionPoolHashes(get_transaction_pool_hashes(state, r).await?)
97-
}
98-
Req::GetPublicNodes(r) => Resp::GetPublicNodes(get_public_nodes(state, r).await?),
69+
Req::GetTransactions(r) => Resp::GetTransactions(not_available()?),
70+
Req::GetAltBlocksHashes(r) => Resp::GetAltBlocksHashes(not_available()?),
71+
Req::IsKeyImageSpent(r) => Resp::IsKeyImageSpent(not_available()?),
72+
Req::SendRawTransaction(r) => Resp::SendRawTransaction(not_available()?),
73+
Req::SaveBc(r) => Resp::SaveBc(not_available()?),
74+
Req::GetPeerList(r) => Resp::GetPeerList(not_available()?),
75+
Req::SetLogLevel(r) => Resp::SetLogLevel(not_available()?),
76+
Req::SetLogCategories(r) => Resp::SetLogCategories(not_available()?),
77+
Req::GetTransactionPool(r) => Resp::GetTransactionPool(not_available()?),
78+
Req::GetTransactionPoolStats(r) => Resp::GetTransactionPoolStats(not_available()?),
79+
Req::StopDaemon(r) => Resp::StopDaemon(not_available()?),
80+
Req::GetLimit(r) => Resp::GetLimit(not_available()?),
81+
Req::SetLimit(r) => Resp::SetLimit(not_available()?),
82+
Req::OutPeers(r) => Resp::OutPeers(not_available()?),
83+
Req::InPeers(r) => Resp::InPeers(not_available()?),
84+
Req::GetNetStats(r) => Resp::GetNetStats(not_available()?),
85+
Req::GetOuts(r) => Resp::GetOuts(not_available()?),
86+
Req::PopBlocks(r) => Resp::PopBlocks(not_available()?),
87+
Req::GetTransactionPoolHashes(r) => Resp::GetTransactionPoolHashes(not_available()?),
88+
Req::GetPublicNodes(r) => Resp::GetPublicNodes(not_available()?),
9989

10090
// Unsupported requests.
10191
Req::SetBootstrapDaemon(_)
@@ -452,9 +442,9 @@ async fn send_raw_transaction(
452442
}
453443
}
454444

445+
// TODO: handle to txpool service.
455446
let tx_relay_checks =
456-
txpool::check_maybe_relay_local(&mut state.txpool_manager, tx, !request.do_not_relay)
457-
.await?;
447+
txpool::check_maybe_relay_local(todo!(), tx, !request.do_not_relay).await?;
458448

459449
if tx_relay_checks.is_empty() {
460450
return Ok(resp);
@@ -496,7 +486,7 @@ async fn send_raw_transaction(
496486

497487
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L1525-L1535>
498488
async fn save_bc(mut state: CupratedRpcHandler, _: SaveBcRequest) -> Result<SaveBcResponse, Error> {
499-
blockchain_manager::sync(&mut state.blockchain_manager).await?;
489+
blockchain_manager::sync(todo!()).await?;
500490

501491
Ok(SaveBcResponse {
502492
base: ResponseBase::OK,
@@ -554,7 +544,7 @@ async fn stop_daemon(
554544
mut state: CupratedRpcHandler,
555545
_: StopDaemonRequest,
556546
) -> Result<StopDaemonResponse, Error> {
557-
blockchain_manager::stop(&mut state.blockchain_manager).await?;
547+
blockchain_manager::stop(todo!()).await?;
558548
Ok(StopDaemonResponse { status: Status::Ok })
559549
}
560550

@@ -658,8 +648,7 @@ async fn pop_blocks(
658648
mut state: CupratedRpcHandler,
659649
request: PopBlocksRequest,
660650
) -> Result<PopBlocksResponse, Error> {
661-
let height =
662-
blockchain_manager::pop_blocks(&mut state.blockchain_manager, request.nblocks).await?;
651+
let height = blockchain_manager::pop_blocks(todo!(), request.nblocks).await?;
663652

664653
Ok(PopBlocksResponse {
665654
base: helper::response_base(false),

0 commit comments

Comments
 (0)