-
Notifications
You must be signed in to change notification settings - Fork 49
cuprated: Blockchain Manager #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 50 commits
b4b73ec
39d48fe
a018469
f909c26
f25588d
1c93ea1
05d0cf2
d648871
e1ae848
ed887a7
bc619b6
029f439
6927b05
21e4b3a
a9d8eee
123aedd
ba5c5ac
f92375f
a864f93
6119972
b211210
68807e7
c03065b
1831fa6
20033ee
da78cbd
b5f8475
d4e0e30
915633f
01a3065
6ec5bc3
90beed1
a16381e
d2ab8e2
291ffe3
c0a3f7a
fa54df2
a7553c2
69f9d84
caaeced
8cff481
4af0f89
6702dbe
403964b
783f426
375a1e1
f50d921
27a8acd
a21e489
c87edf2
e7aaf3c
173f4f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,101 @@ | ||
//! Blockchain | ||
//! | ||
//! Will contain the chain manager and syncer. | ||
//! Contains the blockchain manager, syncer and an interface to mutate the blockchain. | ||
use std::sync::Arc; | ||
|
||
use futures::FutureExt; | ||
use tokio::sync::{mpsc, Notify}; | ||
use tower::{BoxError, Service, ServiceExt}; | ||
|
||
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle}; | ||
use cuprate_consensus::{generate_genesis_block, BlockChainContextService, ContextConfig}; | ||
use cuprate_cryptonight::cryptonight_hash_v0; | ||
use cuprate_p2p::{block_downloader::BlockDownloaderConfig, NetworkInterface}; | ||
use cuprate_p2p_core::{ClearNet, Network}; | ||
use cuprate_types::{ | ||
blockchain::{BlockchainReadRequest, BlockchainWriteRequest}, | ||
VerifiedBlockInformation, | ||
}; | ||
|
||
use crate::constants::PANIC_CRITICAL_SERVICE_ERROR; | ||
|
||
mod chain_service; | ||
pub mod interface; | ||
mod manager; | ||
mod syncer; | ||
mod types; | ||
|
||
use types::{ | ||
ConcreteBlockVerifierService, ConcreteTxVerifierService, ConsensusBlockchainReadHandle, | ||
}; | ||
|
||
/// Checks if the genesis block is in the blockchain and adds it if not. | ||
pub async fn check_add_genesis( | ||
blockchain_read_handle: &mut BlockchainReadHandle, | ||
blockchain_write_handle: &mut BlockchainWriteHandle, | ||
network: Network, | ||
) { | ||
// Try to get the chain height, will fail if the genesis block is not in the DB. | ||
if blockchain_read_handle | ||
.ready() | ||
.await | ||
.expect(PANIC_CRITICAL_SERVICE_ERROR) | ||
.call(BlockchainReadRequest::ChainHeight) | ||
.await | ||
.is_ok() | ||
{ | ||
return; | ||
} | ||
|
||
let genesis = generate_genesis_block(network); | ||
|
||
assert_eq!(genesis.miner_transaction.prefix().outputs.len(), 1); | ||
assert!(genesis.transactions.is_empty()); | ||
|
||
blockchain_write_handle | ||
.ready() | ||
.await | ||
.expect(PANIC_CRITICAL_SERVICE_ERROR) | ||
.call(BlockchainWriteRequest::WriteBlock( | ||
VerifiedBlockInformation { | ||
block_blob: genesis.serialize(), | ||
txs: vec![], | ||
block_hash: genesis.hash(), | ||
pow_hash: cryptonight_hash_v0(&genesis.serialize_pow_hash()), | ||
height: 0, | ||
generated_coins: genesis.miner_transaction.prefix().outputs[0] | ||
.amount | ||
.unwrap(), | ||
weight: genesis.miner_transaction.weight(), | ||
long_term_weight: genesis.miner_transaction.weight(), | ||
cumulative_difficulty: 1, | ||
block: genesis, | ||
}, | ||
)) | ||
.await | ||
.expect(PANIC_CRITICAL_SERVICE_ERROR); | ||
Comment on lines
+55
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't need to change for now but I still think Also, this is only mainnet right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is just mapping the genesis block got from
No this should work on all 3 current networks, although it does (like monerod) make some assumptions about the genesis blocks format There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I realize this, I'm saying this mapping should exist in |
||
} | ||
|
||
/// Initializes the consensus services. | ||
pub async fn init_consensus( | ||
blockchain_read_handle: BlockchainReadHandle, | ||
context_config: ContextConfig, | ||
) -> Result< | ||
( | ||
ConcreteBlockVerifierService, | ||
ConcreteTxVerifierService, | ||
BlockChainContextService, | ||
), | ||
BoxError, | ||
> { | ||
let read_handle = ConsensusBlockchainReadHandle::new(blockchain_read_handle, BoxError::from); | ||
|
||
let ctx_service = | ||
cuprate_consensus::initialize_blockchain_context(context_config, read_handle.clone()) | ||
.await?; | ||
|
||
let (block_verifier_svc, tx_verifier_svc) = | ||
cuprate_consensus::initialize_verifier(read_handle, ctx_service.clone()); | ||
|
||
Ok((block_verifier_svc, tx_verifier_svc, ctx_service)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::task::{Context, Poll}; | ||
|
||
use futures::{future::BoxFuture, FutureExt, TryFutureExt}; | ||
use tower::Service; | ||
|
||
use cuprate_blockchain::service::BlockchainReadHandle; | ||
use cuprate_p2p::block_downloader::{ChainSvcRequest, ChainSvcResponse}; | ||
use cuprate_types::blockchain::{BlockchainReadRequest, BlockchainResponse}; | ||
|
||
/// That service that allows retrieving the chain state to give to the P2P crates, so we can figure out | ||
/// what blocks we need. | ||
/// | ||
/// This has a more minimal interface than [`BlockchainReadRequest`] to make using the p2p crates easier. | ||
#[derive(Clone)] | ||
pub struct ChainService(pub BlockchainReadHandle); | ||
|
||
impl Service<ChainSvcRequest> for ChainService { | ||
type Response = ChainSvcResponse; | ||
type Error = tower::BoxError; | ||
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.0.poll_ready(cx).map_err(Into::into) | ||
} | ||
|
||
fn call(&mut self, req: ChainSvcRequest) -> Self::Future { | ||
let map_res = |res: BlockchainResponse| match res { | ||
BlockchainResponse::CompactChainHistory { | ||
block_ids, | ||
cumulative_difficulty, | ||
} => ChainSvcResponse::CompactHistory { | ||
block_ids, | ||
cumulative_difficulty, | ||
}, | ||
BlockchainResponse::FindFirstUnknown(res) => ChainSvcResponse::FindFirstUnknown(res), | ||
_ => unreachable!(), | ||
}; | ||
|
||
match req { | ||
ChainSvcRequest::CompactHistory => self | ||
.0 | ||
.call(BlockchainReadRequest::CompactChainHistory) | ||
.map_ok(map_res) | ||
.map_err(Into::into) | ||
.boxed(), | ||
ChainSvcRequest::FindFirstUnknown(req) => self | ||
.0 | ||
.call(BlockchainReadRequest::FindFirstUnknown(req)) | ||
.map_ok(map_res) | ||
.map_err(Into::into) | ||
.boxed(), | ||
ChainSvcRequest::CumulativeDifficulty => self | ||
.0 | ||
.call(BlockchainReadRequest::CompactChainHistory) | ||
.map_ok(|res| { | ||
// TODO create a custom request instead of hijacking this one. | ||
// TODO: use the context cache. | ||
let BlockchainResponse::CompactChainHistory { | ||
cumulative_difficulty, | ||
.. | ||
} = res | ||
else { | ||
unreachable!() | ||
}; | ||
|
||
ChainSvcResponse::CumulativeDifficulty(cumulative_difficulty) | ||
}) | ||
.map_err(Into::into) | ||
.boxed(), | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.