|
1 | 1 | //! Blockchain
|
2 | 2 | //!
|
3 |
| -//! Will contain the chain manager and syncer. |
| 3 | +//! Contains the blockchain manager, syncer and an interface to mutate the blockchain. |
| 4 | +use std::sync::Arc; |
4 | 5 |
|
| 6 | +use futures::FutureExt; |
| 7 | +use tokio::sync::{mpsc, Notify}; |
| 8 | +use tower::{BoxError, Service, ServiceExt}; |
| 9 | + |
| 10 | +use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle}; |
| 11 | +use cuprate_consensus::{generate_genesis_block, BlockChainContextService, ContextConfig}; |
| 12 | +use cuprate_cryptonight::cryptonight_hash_v0; |
| 13 | +use cuprate_p2p::{block_downloader::BlockDownloaderConfig, NetworkInterface}; |
| 14 | +use cuprate_p2p_core::{ClearNet, Network}; |
| 15 | +use cuprate_types::{ |
| 16 | + blockchain::{BlockchainReadRequest, BlockchainWriteRequest}, |
| 17 | + VerifiedBlockInformation, |
| 18 | +}; |
| 19 | + |
| 20 | +use crate::constants::PANIC_CRITICAL_SERVICE_ERROR; |
| 21 | + |
| 22 | +mod chain_service; |
| 23 | +pub mod interface; |
5 | 24 | mod manager;
|
6 | 25 | mod syncer;
|
| 26 | +mod types; |
| 27 | + |
| 28 | +use types::{ |
| 29 | + ConcreteBlockVerifierService, ConcreteTxVerifierService, ConsensusBlockchainReadHandle, |
| 30 | +}; |
| 31 | + |
| 32 | +/// Checks if the genesis block is in the blockchain and adds it if not. |
| 33 | +pub async fn check_add_genesis( |
| 34 | + blockchain_read_handle: &mut BlockchainReadHandle, |
| 35 | + blockchain_write_handle: &mut BlockchainWriteHandle, |
| 36 | + network: Network, |
| 37 | +) { |
| 38 | + // Try to get the chain height, will fail if the genesis block is not in the DB. |
| 39 | + if blockchain_read_handle |
| 40 | + .ready() |
| 41 | + .await |
| 42 | + .expect(PANIC_CRITICAL_SERVICE_ERROR) |
| 43 | + .call(BlockchainReadRequest::ChainHeight) |
| 44 | + .await |
| 45 | + .is_ok() |
| 46 | + { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + let genesis = generate_genesis_block(network); |
| 51 | + |
| 52 | + assert_eq!(genesis.miner_transaction.prefix().outputs.len(), 1); |
| 53 | + assert!(genesis.transactions.is_empty()); |
| 54 | + |
| 55 | + blockchain_write_handle |
| 56 | + .ready() |
| 57 | + .await |
| 58 | + .expect(PANIC_CRITICAL_SERVICE_ERROR) |
| 59 | + .call(BlockchainWriteRequest::WriteBlock( |
| 60 | + VerifiedBlockInformation { |
| 61 | + block_blob: genesis.serialize(), |
| 62 | + txs: vec![], |
| 63 | + block_hash: genesis.hash(), |
| 64 | + pow_hash: cryptonight_hash_v0(&genesis.serialize_pow_hash()), |
| 65 | + height: 0, |
| 66 | + generated_coins: genesis.miner_transaction.prefix().outputs[0] |
| 67 | + .amount |
| 68 | + .unwrap(), |
| 69 | + weight: genesis.miner_transaction.weight(), |
| 70 | + long_term_weight: genesis.miner_transaction.weight(), |
| 71 | + cumulative_difficulty: 1, |
| 72 | + block: genesis, |
| 73 | + }, |
| 74 | + )) |
| 75 | + .await |
| 76 | + .expect(PANIC_CRITICAL_SERVICE_ERROR); |
| 77 | +} |
| 78 | + |
| 79 | +/// Initializes the consensus services. |
| 80 | +pub async fn init_consensus( |
| 81 | + blockchain_read_handle: BlockchainReadHandle, |
| 82 | + context_config: ContextConfig, |
| 83 | +) -> Result< |
| 84 | + ( |
| 85 | + ConcreteBlockVerifierService, |
| 86 | + ConcreteTxVerifierService, |
| 87 | + BlockChainContextService, |
| 88 | + ), |
| 89 | + BoxError, |
| 90 | +> { |
| 91 | + let read_handle = ConsensusBlockchainReadHandle::new(blockchain_read_handle, BoxError::from); |
| 92 | + |
| 93 | + let ctx_service = |
| 94 | + cuprate_consensus::initialize_blockchain_context(context_config, read_handle.clone()) |
| 95 | + .await?; |
| 96 | + |
| 97 | + let (block_verifier_svc, tx_verifier_svc) = |
| 98 | + cuprate_consensus::initialize_verifier(read_handle, ctx_service.clone()); |
| 99 | + |
| 100 | + Ok((block_verifier_svc, tx_verifier_svc, ctx_service)) |
| 101 | +} |
0 commit comments