Skip to content

Commit 8d0c26c

Browse files
committed
Merge branch 'main' into cuprated-config
2 parents 5236ede + f9b847b commit 8d0c26c

File tree

111 files changed

+5664
-13405
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+5664
-13405
lines changed

Cargo.lock

Lines changed: 120 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ rayon = { version = "1.10.0", default-features = false }
7878
serde_bytes = { version = "0.11.15", default-features = false }
7979
serde_json = { version = "1.0.128", default-features = false }
8080
serde = { version = "1.0.210", default-features = false }
81+
strum = { version = "0.26.3", default-features = false }
8182
thiserror = { version = "1.0.63", default-features = false }
8283
thread_local = { version = "1.1.8", default-features = false }
8384
tokio-util = { version = "0.7.12", default-features = false }

binaries/cuprated/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cuprate-p2p-core = { path = "../../p2p/p2p-core", features = ["serde"] }
2323
cuprate-dandelion-tower = { path = "../../p2p/dandelion-tower" }
2424
cuprate-async-buffer = { path = "../../p2p/async-buffer" }
2525
cuprate-address-book = { path = "../../p2p/address-book", features = ["serde_config"] }
26-
cuprate-blockchain = { path = "../../storage/blockchain" }
26+
cuprate-blockchain = { path = "../../storage/blockchain", features = ["service"] }
2727
cuprate-database-service = { path = "../../storage/service" }
2828
cuprate-txpool = { path = "../../storage/txpool" }
2929
cuprate-database = { path = "../../storage/database" }
@@ -70,7 +70,7 @@ tokio-util = { workspace = true }
7070
tokio-stream = { workspace = true }
7171
tokio = { workspace = true }
7272
tower = { workspace = true }
73-
tracing-subscriber = { workspace = true }
73+
tracing-subscriber = { workspace = true, features = ["std", "fmt", "default"] }
7474
tracing = { workspace = true }
7575

7676
[lints]
Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,101 @@
11
//! Blockchain
22
//!
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;
45

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;
524
mod manager;
625
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

Comments
 (0)