Skip to content
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
b4b73ec
add cuprated skeleton
Boog900 Aug 16, 2024
39d48fe
fmt and add deny exception
Boog900 Aug 16, 2024
a018469
add main chain batch handler
Boog900 Aug 20, 2024
f909c26
add blockchain init
Boog900 Aug 21, 2024
f25588d
very rough block manager
Boog900 Aug 23, 2024
1c93ea1
misc changes
Boog900 Aug 29, 2024
05d0cf2
move more config values
Boog900 Aug 29, 2024
d648871
add new tables & types
Boog900 Aug 29, 2024
e1ae848
add function to fully add an alt block
Boog900 Aug 30, 2024
ed887a7
resolve current todo!s
Boog900 Aug 30, 2024
bc619b6
add new requests
Boog900 Aug 31, 2024
029f439
WIP: starting re-orgs
Boog900 Sep 1, 2024
6927b05
add last service request
Boog900 Sep 5, 2024
21e4b3a
commit Cargo.lock
Boog900 Sep 5, 2024
a9d8eee
Merge branch 'main' into storage-alt-blocks
Boog900 Sep 5, 2024
123aedd
add test
Boog900 Sep 6, 2024
ba5c5ac
more docs + cleanup + alt blocks request
Boog900 Sep 7, 2024
f92375f
clippy + fmt
Boog900 Sep 7, 2024
a864f93
document types
Boog900 Sep 7, 2024
6119972
move tx_fee to helper
Boog900 Sep 8, 2024
b211210
more doc updates
Boog900 Sep 8, 2024
68807e7
fmt
Boog900 Sep 8, 2024
c03065b
fix imports
Boog900 Sep 8, 2024
1831fa6
remove config files
Boog900 Sep 9, 2024
20033ee
Merge branch 'main' into cuprated-blockchain
Boog900 Sep 9, 2024
da78cbd
fix merge errors
Boog900 Sep 9, 2024
b5f8475
Merge branch 'storage-alt-blocks' into cuprated-blockchain
Boog900 Sep 9, 2024
d4e0e30
fix generated coins
Boog900 Sep 9, 2024
915633f
handle more p2p requests + alt blocks
Boog900 Sep 12, 2024
01a3065
clean up handler code
Boog900 Sep 12, 2024
6ec5bc3
add function for incoming blocks
Boog900 Sep 12, 2024
90beed1
add docs to handler functions
Boog900 Sep 13, 2024
a16381e
broadcast new blocks + add commands
Boog900 Sep 14, 2024
d2ab8e2
add fluffy block handler
Boog900 Sep 15, 2024
291ffe3
fix new block handling
Boog900 Sep 15, 2024
c0a3f7a
small cleanup
Boog900 Sep 15, 2024
fa54df2
increase outbound peer count
Boog900 Sep 16, 2024
a7553c2
Merge branch 'main' into cuprated-blockchain
Boog900 Oct 1, 2024
69f9d84
fix merge
Boog900 Oct 3, 2024
caaeced
clean up the blockchain manger
Boog900 Oct 3, 2024
8cff481
add more docs + cleanup imports
Boog900 Oct 3, 2024
4af0f89
fix typo
Boog900 Oct 3, 2024
6702dbe
fix doc
Boog900 Oct 3, 2024
403964b
remove unrelated changes
Boog900 Oct 3, 2024
783f426
improve interface globals
Boog900 Oct 5, 2024
375a1e1
manger -> manager
Boog900 Oct 5, 2024
f50d921
enums instead of bools
Boog900 Oct 5, 2024
27a8acd
move chain service to separate file
Boog900 Oct 5, 2024
a21e489
more review fixes
Boog900 Oct 5, 2024
c87edf2
add link to issue
Boog900 Oct 6, 2024
e7aaf3c
fix syncer + update comment
Boog900 Oct 8, 2024
173f4f7
fmt
Boog900 Oct 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Cargo.lock

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

10 changes: 8 additions & 2 deletions 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 Expand Up @@ -70,8 +70,14 @@ tokio-util = { workspace = true }
tokio-stream = { workspace = true }
tokio = { workspace = true }
tower = { workspace = true }
tracing-subscriber = { workspace = true }
tracing-subscriber = { workspace = true, features = ["std", "fmt", "default"] }
tracing = { workspace = true }

[lints]
workspace = true

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
97 changes: 96 additions & 1 deletion binaries/cuprated/src/blockchain.rs
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to change for now but I still think cuprate_constants::genesis should exist - having the genesis block inlined in some async function in cuprated feels wrong. It's not about de-duplicatation but more about separating concerns and being clean, these constants deserve a name instead of being inlined.

Also, this is only mainnet right?

Copy link
Member Author

@Boog900 Boog900 Oct 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having the genesis block inlined in some async function in cuprated feels wrong

This is just mapping the genesis block got from generate_genesis_block to VerifiedBlockInformation.

Also, this is only mainnet right?

No this should work on all 3 current networks, although it does (like monerod) make some assumptions about the genesis blocks format

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapping

I realize this, I'm saying this mapping should exist in cuprate_constants::genesis as well, next to the Block.

}

/// 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))
}
72 changes: 72 additions & 0 deletions binaries/cuprated/src/blockchain/chain_service.rs
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(),
}
}
}
Loading
Loading