Skip to content

Commit

Permalink
handle more p2p requests + alt blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Boog900 committed Sep 12, 2024
1 parent d4e0e30 commit 915633f
Show file tree
Hide file tree
Showing 15 changed files with 601 additions and 109 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion binaries/cuprated/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cuprate-p2p-core = { path = "../../p2p/p2p-core" }
cuprate-dandelion-tower = { path = "../../p2p/dandelion-tower" }
cuprate-async-buffer = { path = "../../p2p/async-buffer" }
cuprate-address-book = { path = "../../p2p/address-book" }
cuprate-blockchain = { path = "../../storage/blockchain" }
cuprate-blockchain = { path = "../../storage/blockchain", features = ["service"] }
cuprate-database-service = { path = "../../storage/service" }
cuprate-txpool = { path = "../../storage/txpool" }
cuprate-database = { path = "../../storage/database" }
Expand Down
90 changes: 83 additions & 7 deletions binaries/cuprated/src/blockchain/manager.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
mod batch_handler;
mod handler;

use crate::blockchain::manager::batch_handler::handle_incoming_block_batch;
use crate::blockchain::types::ConsensusBlockchainReadHandle;
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
use cuprate_consensus::{BlockChainContextService, BlockVerifierService, TxVerifierService};
use cuprate_consensus::context::RawBlockChainContext;
use cuprate_consensus::{
BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService,
BlockVerifierService, ExtendedConsensusError, TxVerifierService, VerifyBlockRequest,
VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
};
use cuprate_p2p::block_downloader::BlockBatch;
use cuprate_types::blockchain::{BlockchainReadRequest, BlockchainResponse};
use cuprate_types::Chain;
use futures::StreamExt;
use tokio::sync::mpsc::Receiver;
use tower::{Service, ServiceExt};
use tracing::error;

pub struct BlockchainManager {
blockchain_write_handle: BlockchainWriteHandle,
blockchain_read_handle: BlockchainReadHandle,
blockchain_context_service: BlockChainContextService,
cached_blockchain_context: RawBlockChainContext,
block_verifier_service: BlockVerifierService<
BlockChainContextService,
TxVerifierService<ConsensusBlockchainReadHandle>,
Expand All @@ -34,20 +44,86 @@ impl BlockchainManager {
blockchain_write_handle,
blockchain_read_handle,
blockchain_context_service,
cached_blockchain_context: todo!(),
block_verifier_service,
}
}

async fn handle_incoming_main_chain_batch(
&mut self,
batch: BlockBatch,
) -> Result<(), anyhow::Error> {
let VerifyBlockResponse::MainChainBatchPrepped(prepped) = self
.block_verifier_service
.ready()
.await
.expect("TODO")
.call(VerifyBlockRequest::MainChainBatchPrepareBlocks {
blocks: batch.blocks,
})
.await?
else {
panic!("Incorrect response!");
};

for (block, txs) in prepped {
let VerifyBlockResponse::MainChain(verified_block) = block_verifier_service
.ready()
.await
.expect("TODO")
.call(VerifyBlockRequest::MainChainPrepped { block, txs })
.await
.unwrap()
else {
panic!("Incorrect response!");
};

blockchain_context_service
.ready()
.await
.expect("TODO")
.call(BlockChainContextRequest::Update(NewBlockData {
block_hash: verified_block.block_hash,
height: verified_block.height,
timestamp: verified_block.block.header.timestamp,
weight: verified_block.weight,
long_term_weight: verified_block.long_term_weight,
generated_coins: verified_block.generated_coins,
vote: HardFork::from_vote(verified_block.block.header.hardfork_signal),
cumulative_difficulty: verified_block.cumulative_difficulty,
}))
.await
.expect("TODO");

blockchain_write_handle
.ready()
.await
.expect("TODO")
.call(BlockchainWriteRequest::WriteBlock(verified_block))
.await
.expect("TODO");
}
}

async fn handle_incoming_block_batch(&mut self, batch: BlockBatch) {
let (first_block, _) = batch
.blocks
.first()
.expect("Block batch should not be empty");

if first_block.header.previous == self.cached_blockchain_context.top_hash {
todo!("Main chain")
} else {
todo!("Alt chain")
}
}

pub async fn run(mut self, mut batch_rx: Receiver<BlockBatch>) {
loop {
tokio::select! {
Some(batch) = batch_rx.recv() => {
handle_incoming_block_batch(
self.handle_incoming_block_batch(
batch,
&mut self.block_verifier_service,
&mut self.blockchain_context_service,
&mut self.blockchain_read_handle,
&mut self.blockchain_write_handle
).await;
}
else => {
Expand Down
105 changes: 33 additions & 72 deletions binaries/cuprated/src/blockchain/manager/batch_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::blockchain::types::ConsensusBlockchainReadHandle;
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
use cuprate_consensus::context::NewBlockData;
use cuprate_consensus::transactions::new_tx_verification_data;
use cuprate_consensus::{
BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService,
BlockVerifierService, BlockchainReadRequest, BlockchainResponse, ExtendedConsensusError,
Expand All @@ -11,82 +12,17 @@ use cuprate_consensus::{
use cuprate_p2p::block_downloader::BlockBatch;
use cuprate_types::blockchain::BlockchainWriteRequest;
use cuprate_types::{Chain, HardFork};
use rayon::prelude::*;
use tower::{Service, ServiceExt};
use tracing::{debug, error, info};

pub async fn handle_incoming_block_batch<C, TxV>(
batch: BlockBatch,
block_verifier_service: &mut BlockVerifierService<C, TxV, ConsensusBlockchainReadHandle>,
blockchain_context_service: &mut C,
blockchain_read_handle: &mut BlockchainReadHandle,
blockchain_write_handle: &mut BlockchainWriteHandle,
) where
C: Service<
BlockChainContextRequest,
Response = BlockChainContextResponse,
Error = tower::BoxError,
> + Clone
+ Send
+ 'static,
C::Future: Send + 'static,

TxV: Service<VerifyTxRequest, Response = VerifyTxResponse, Error = ExtendedConsensusError>
+ Clone
+ Send
+ 'static,
TxV::Future: Send + 'static,
{
let (first_block, _) = batch
.blocks
.first()
.expect("Block batch should not be empty");

handle_incoming_block_batch_main_chain(
batch,
block_verifier_service,
blockchain_context_service,
blockchain_write_handle,
)
.await;

// TODO: alt block to the DB
/*
match blockchain_read_handle
.oneshot(BlockchainReadRequest::FindBlock(
first_block.header.previous,
))
.await
{
Err(_) | Ok(BlockchainResponse::FindBlock(None)) => {
// The block downloader shouldn't be downloading orphan blocks
error!("Failed to find parent block for first block in batch.");
return;
}
Ok(BlockchainResponse::FindBlock(Some((chain, _)))) => match chain {
Chain::Main => {
handle_incoming_block_batch_main_chain(
batch,
block_verifier_service,
blockchain_context_service,
blockchain_write_handle,
)
.await;
}
Chain::Alt(_) => todo!(),
},
Ok(_) => panic!("Blockchain service returned incorrect response"),
}
*/
}

async fn handle_incoming_block_batch_main_chain<C, TxV>(
batch: BlockBatch,
block_verifier_service: &mut BlockVerifierService<C, TxV, ConsensusBlockchainReadHandle>,
blockchain_context_service: &mut C,
blockchain_write_handle: &mut BlockchainWriteHandle,
) where
) -> Result<(), anyhow::Error>
where
C: Service<
BlockChainContextRequest,
Response = BlockChainContextResponse,
Expand Down Expand Up @@ -114,8 +50,7 @@ async fn handle_incoming_block_batch_main_chain<C, TxV>(
.call(VerifyBlockRequest::MainChainBatchPrepareBlocks {
blocks: batch.blocks,
})
.await
.unwrap()
.await?
else {
panic!("Incorrect response!");
};
Expand All @@ -126,8 +61,7 @@ async fn handle_incoming_block_batch_main_chain<C, TxV>(
.await
.expect("TODO")
.call(VerifyBlockRequest::MainChainPrepped { block, txs })
.await
.unwrap()
.await?
else {
panic!("Incorrect response!");
};
Expand Down Expand Up @@ -158,3 +92,30 @@ async fn handle_incoming_block_batch_main_chain<C, TxV>(
.expect("TODO");
}
}

async fn handle_incoming_block_batch_alt_chain<C, TxV>(
batch: BlockBatch,
block_verifier_service: &mut BlockVerifierService<C, TxV, ConsensusBlockchainReadHandle>,
blockchain_context_service: &mut C,
blockchain_write_handle: &mut BlockchainWriteHandle,
) -> Result<(), anyhow::Error>
where
C: Service<
BlockChainContextRequest,
Response = BlockChainContextResponse,
Error = tower::BoxError,
> + Clone
+ Send
+ 'static,
C::Future: Send + 'static,

TxV: Service<VerifyTxRequest, Response = VerifyTxResponse, Error = ExtendedConsensusError>
+ Clone
+ Send
+ 'static,
TxV::Future: Send + 'static,
{
for (block, txs) in batch.blocks {
alt_block_info.cumulative_difficulty
}
}
Loading

0 comments on commit 915633f

Please sign in to comment.