Skip to content

Commit

Permalink
todo
Browse files Browse the repository at this point in the history
  • Loading branch information
Boog900 committed Oct 6, 2024
1 parent dc3a3c5 commit cb960ab
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 33 deletions.
28 changes: 0 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion binaries/cuprated/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! cuprated config
use serde::{Deserialize, Serialize};
use cuprate_consensus::ContextConfig;
use cuprate_helper::network::Network;
use cuprate_p2p_core::ClearNet;

Expand All @@ -24,7 +25,11 @@ pub struct Config {
}

impl Config {
fn clearnet_p2p_config(&self) -> cuprate_p2p::P2PConfig<ClearNet> {
pub fn network(&self) -> Network {
self.network
}

pub fn clearnet_p2p_config(&self) -> cuprate_p2p::P2PConfig<ClearNet> {
cuprate_p2p::P2PConfig {
network: self.network,
outbound_connections: self.p2p.clear_net.general.outbound_connections,
Expand All @@ -37,6 +42,14 @@ impl Config {
address_book_config: self.p2p.clear_net.general.address_book_config.clone(),
}
}

pub fn context_config(&self) -> ContextConfig {
match self.network {
Network::Mainnet => ContextConfig::main_net(),
Network::Stagenet => ContextConfig::stage_net(),
Network::Testnet => ContextConfig::test_net()
}
}

pub fn blockchain_config(&self) -> cuprate_blockchain::config::Config {
self.storage.blockchain.clone()
Expand Down
19 changes: 15 additions & 4 deletions binaries/cuprated/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,27 @@ fn main() {
let async_rt = init_tokio_rt(&config);

async_rt.block_on(async move {
blockchain:: check_add_genesis(&mut bc_read_handle, &mut bc_write_handle, &config.network()).await;
blockchain:: check_add_genesis(&mut bc_read_handle, &mut bc_write_handle, config.network()).await;

let (block_verifier, _tx_verifier, context_svc) =
blockchain::init_consensus(bc_read_handle.clone(), config.context_config())
.await
.unwrap();
}

// TODO: everything else.
todo!()
let net = cuprate_p2p::initialize_network(
p2p::request_handler:: {
blockchain_read_handle: bc_read_handle.clone(),
},
p2p::core_sync_service::CoreSyncService(context_svc.clone()),
config.clearnet_p2p_config(),
)
.await
.unwrap();

// TODO: this can be removed as long as the main thread does not exit, so when command handling
// is added
futures::future::pending::<()>().await;
});
}

fn init_log(_config: &Config) {
Expand Down
1 change: 1 addition & 0 deletions binaries/cuprated/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
//! Will handle initiating the P2P and contains a protocol request handler.

pub mod request_handler;
pub mod core_sync_service;
51 changes: 51 additions & 0 deletions binaries/cuprated/src/p2p/core_sync_service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use cuprate_blockchain::cuprate_database::RuntimeError;
use cuprate_blockchain::service::BlockchainReadHandle;
use cuprate_consensus::{
BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService,
};
use cuprate_p2p_core::services::{CoreSyncDataRequest, CoreSyncDataResponse};
use cuprate_p2p_core::CoreSyncData;
use cuprate_types::blockchain::BlockchainReadRequest;
use futures::future::{BoxFuture, MapErr, MapOk};
use futures::{FutureExt, TryFutureExt};
use std::task::{Context, Poll};
use tower::Service;

#[derive(Clone)]
pub struct CoreSyncService(pub BlockChainContextService);

impl Service<CoreSyncDataRequest> for CoreSyncService {
type Response = CoreSyncDataResponse;
type Error = tower::BoxError;
type Future = MapOk<
<BlockChainContextService as Service<BlockChainContextRequest>>::Future,
fn(BlockChainContextResponse) -> CoreSyncDataResponse,
>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.0.poll_ready(cx)
}

fn call(&mut self, _: CoreSyncDataRequest) -> Self::Future {
self.0
.call(BlockChainContextRequest::GetContext)
.map_ok(|res| {
let BlockChainContextResponse::Context(ctx) = res else {
panic!("blockchain context service returned wrong response.");
};

let raw_ctx = ctx.unchecked_blockchain_context();

// TODO: the hardfork here should be the version of the top block not the current HF,
// on HF boundaries these will be different.
CoreSyncDataResponse(CoreSyncData::new(
raw_ctx.cumulative_difficulty,
// TODO:
raw_ctx.chain_height as u64,
0,
raw_ctx.top_hash,
raw_ctx.current_hf.as_u8(),
))
})
}
}

0 comments on commit cb960ab

Please sign in to comment.