Skip to content

Commit

Permalink
Move consensus context service into a subcrate. (#318)
Browse files Browse the repository at this point in the history
Co-authored-by: Boog900 <[email protected]>
  • Loading branch information
SyntheticBird45 and Boog900 authored Oct 16, 2024
1 parent f9b847b commit 978d72b
Show file tree
Hide file tree
Showing 28 changed files with 218 additions and 133 deletions.
23 changes: 22 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"binaries/cuprated",
"constants",
"consensus",
"consensus/context",
"consensus/fast-sync",
"consensus/rules",
"cryptonight",
Expand Down Expand Up @@ -322,4 +323,4 @@ non_camel_case_types = "deny"
# unused_results = "deny"
# non_exhaustive_omitted_patterns = "deny"
# missing_docs = "deny"
# missing_copy_implementations = "deny"
# missing_copy_implementations = "deny"
1 change: 1 addition & 0 deletions binaries/cuprated/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/binaries/cuprated"
# TODO: after v1.0.0, remove unneeded dependencies.
cuprate-consensus = { path = "../../consensus" }
cuprate-fast-sync = { path = "../../consensus/fast-sync" }
cuprate-consensus-context = { path = "../../consensus/context" }
cuprate-consensus-rules = { path = "../../consensus/rules" }
cuprate-cryptonight = { path = "../../cryptonight" }
cuprate-helper = { path = "../../helper" }
Expand Down
7 changes: 4 additions & 3 deletions binaries/cuprated/src/blockchain/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use tracing::error;

use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
use cuprate_consensus::{
context::RawBlockChainContext, BlockChainContextRequest, BlockChainContextResponse,
BlockChainContextService, BlockVerifierService, ExtendedConsensusError, TxVerifierService,
VerifyBlockRequest, VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService,
BlockVerifierService, ExtendedConsensusError, TxVerifierService, VerifyBlockRequest,
VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
};
use cuprate_consensus_context::RawBlockChainContext;
use cuprate_p2p::{
block_downloader::{BlockBatch, BlockDownloaderConfig},
BroadcastSvc, NetworkInterface,
Expand Down
8 changes: 4 additions & 4 deletions binaries/cuprated/src/blockchain/manager/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use tracing::info;

use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
use cuprate_consensus::{
block::PreparedBlock, context::NewBlockData, transactions::new_tx_verification_data,
BlockChainContextRequest, BlockChainContextResponse, BlockVerifierService,
ExtendedConsensusError, VerifyBlockRequest, VerifyBlockResponse, VerifyTxRequest,
VerifyTxResponse,
block::PreparedBlock, transactions::new_tx_verification_data, BlockChainContextRequest,
BlockChainContextResponse, BlockVerifierService, ExtendedConsensusError, VerifyBlockRequest,
VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
};
use cuprate_consensus_context::NewBlockData;
use cuprate_helper::cast::usize_to_u64;
use cuprate_p2p::{block_downloader::BlockBatch, constants::LONG_BAN, BroadcastRequest};
use cuprate_types::{
Expand Down
2 changes: 1 addition & 1 deletion binaries/cuprated/src/rpc/request/blockchain_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::convert::Infallible;
use anyhow::Error;
use tower::{Service, ServiceExt};

use cuprate_consensus::context::{
use cuprate_consensus_context::{
BlockChainContext, BlockChainContextRequest, BlockChainContextResponse,
BlockChainContextService,
};
Expand Down
3 changes: 2 additions & 1 deletion books/architecture/src/appendix/crates.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ cargo doc --open --package cuprate-blockchain
| Crate | In-tree path | Purpose |
|-------|--------------|---------|
| [`cuprate-consensus`](https://doc.cuprate.org/cuprate_consensus) | [`consensus/`](https://github.com/Cuprate/cuprate/tree/main/consensus) | TODO
| [`cuprate-consensus-rules`](https://doc.cuprate.org/cuprate_consensus_rules) | [`consensus/rules/`](https://github.com/Cuprate/cuprate/tree/main/consensus-rules) | TODO
| [`cuprate-consensus-context`](https://doc.cuprate.org/cuprate_consensus_context) | [`consensus/context/`](https://github.com/Cuprate/cuprate/tree/main/consensus/context) | TODO
| [`cuprate-consensus-rules`](https://doc.cuprate.org/cuprate_consensus_rules) | [`consensus/rules/`](https://github.com/Cuprate/cuprate/tree/main/consensus/rules) | TODO
| [`cuprate-fast-sync`](https://doc.cuprate.org/cuprate_fast_sync) | [`consensus/fast-sync/`](https://github.com/Cuprate/cuprate/tree/main/consensus/fast-sync) | Fast block synchronization

## Networking
Expand Down
6 changes: 2 additions & 4 deletions consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/consensus"
cuprate-helper = { path = "../helper", default-features = false, features = ["std", "asynch", "num"] }
cuprate-consensus-rules = { path = "./rules", features = ["rayon"] }
cuprate-types = { path = "../types" }
cuprate-consensus-context = { path = "./context" }

cfg-if = { workspace = true }
thiserror = { workspace = true }
tower = { workspace = true, features = ["util"] }
tracing = { workspace = true, features = ["std", "attributes"] }
futures = { workspace = true, features = ["std", "async-await"] }

randomx-rs = { workspace = true }
monero-serai = { workspace = true, features = ["std"] }

rayon = { workspace = true }
thread_local = { workspace = true }
tokio = { workspace = true, features = ["rt"] }
tokio-util = { workspace = true }

hex = { workspace = true }
rand = { workspace = true }
Expand All @@ -42,4 +40,4 @@ proptest = { workspace = true }
proptest-derive = { workspace = true }

[lints]
workspace = true
workspace = true
24 changes: 24 additions & 0 deletions consensus/context/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "cuprate-consensus-context"
version = "0.1.0"
edition = "2021"
license = "MIT"
authors = ["SyntheticBird","Boog900"]

[dependencies]
cuprate-consensus-rules = { path = "../rules", features = ["proptest"]}
cuprate-helper = { path = "../../helper", default-features = false, features = ["std", "cast"] }
cuprate-types = { path = "../../types", default-features = false }

futures = { workspace = true, features = ["std", "async-await"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"]}
tokio-util = { workspace = true }
tower = { workspace = true, features = ["util"] }
tracing = { workspace = true, features = ["std", "attributes"] }
thiserror = { workspace = true }

monero-serai = { workspace = true, features = ["std"] }
randomx-rs = { workspace = true }
rayon = { workspace = true }
thread_local = { workspace = true }
hex = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use cuprate_types::{
};

use crate::{
ExtendedConsensusError,
__private::Database,
context::{difficulty::DifficultyCache, rx_vms::RandomXVm, weight::BlockWeightsCache},
ContextCacheError, __private::Database, difficulty::DifficultyCache, rx_vms::RandomXVm,
weight::BlockWeightsCache,
};

pub(crate) mod sealed {
Expand All @@ -38,7 +37,7 @@ pub struct AltChainContextCache {
pub chain_height: usize,
/// The top hash of the alt chain.
pub top_hash: [u8; 32],
/// The [`ChainID`] of the alt chain.
/// The [`ChainId`] of the alt chain.
pub chain_id: Option<ChainId>,
/// The parent [`Chain`] of this alt chain.
pub parent_chain: Chain,
Expand Down Expand Up @@ -98,7 +97,7 @@ impl AltChainMap {
&mut self,
prev_id: [u8; 32],
database: D,
) -> Result<Box<AltChainContextCache>, ExtendedConsensusError> {
) -> Result<Box<AltChainContextCache>, ContextCacheError> {
if let Some(cache) = self.alt_cache_map.remove(&prev_id) {
return Ok(cache);
}
Expand Down Expand Up @@ -133,7 +132,7 @@ pub(crate) async fn get_alt_chain_difficulty_cache<D: Database + Clone>(
prev_id: [u8; 32],
main_chain_difficulty_cache: &DifficultyCache,
mut database: D,
) -> Result<DifficultyCache, ExtendedConsensusError> {
) -> Result<DifficultyCache, ContextCacheError> {
// find the block with hash == prev_id.
let BlockchainResponse::FindBlock(res) = database
.ready()
Expand Down Expand Up @@ -180,7 +179,7 @@ pub(crate) async fn get_alt_chain_weight_cache<D: Database + Clone>(
prev_id: [u8; 32],
main_chain_weight_cache: &BlockWeightsCache,
mut database: D,
) -> Result<BlockWeightsCache, ExtendedConsensusError> {
) -> Result<BlockWeightsCache, ContextCacheError> {
// find the block with hash == prev_id.
let BlockchainResponse::FindBlock(res) = database
.ready()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use cuprate_types::{
Chain,
};

use crate::{Database, ExtendedConsensusError, HardFork};
use crate::{ContextCacheError, Database, HardFork};

/// The amount of blocks we account for to calculate difficulty
const DIFFICULTY_WINDOW: usize = 720;
Expand All @@ -33,9 +33,9 @@ const DIFFICULTY_LAG: usize = 15;
///
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct DifficultyCacheConfig {
pub(crate) window: usize,
pub(crate) cut: usize,
pub(crate) lag: usize,
pub window: usize,
pub cut: usize,
pub lag: usize,
}

impl DifficultyCacheConfig {
Expand Down Expand Up @@ -73,14 +73,13 @@ impl DifficultyCacheConfig {
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DifficultyCache {
/// The list of timestamps in the window.
/// len <= [`DIFFICULTY_BLOCKS_COUNT`]
pub(crate) timestamps: VecDeque<u64>,
pub timestamps: VecDeque<u64>,
/// The current cumulative difficulty of the chain.
pub(crate) cumulative_difficulties: VecDeque<u128>,
pub cumulative_difficulties: VecDeque<u128>,
/// The last height we accounted for.
pub(crate) last_accounted_height: usize,
pub last_accounted_height: usize,
/// The config
pub(crate) config: DifficultyCacheConfig,
pub config: DifficultyCacheConfig,
}

impl DifficultyCache {
Expand All @@ -91,7 +90,7 @@ impl DifficultyCache {
config: DifficultyCacheConfig,
database: D,
chain: Chain,
) -> Result<Self, ExtendedConsensusError> {
) -> Result<Self, ContextCacheError> {
tracing::info!("Initializing difficulty cache this may take a while.");

let mut block_start = chain_height.saturating_sub(config.total_block_count());
Expand Down Expand Up @@ -134,7 +133,7 @@ impl DifficultyCache {
&mut self,
numb_blocks: usize,
database: D,
) -> Result<(), ExtendedConsensusError> {
) -> Result<(), ContextCacheError> {
let Some(retained_blocks) = self.timestamps.len().checked_sub(numb_blocks) else {
// More blocks to pop than we have in the cache, so just restart a new cache.
*self = Self::init_from_chain_height(
Expand Down Expand Up @@ -361,7 +360,7 @@ async fn get_blocks_in_pow_info<D: Database + Clone>(
database: D,
block_heights: Range<usize>,
chain: Chain,
) -> Result<(VecDeque<u64>, VecDeque<u128>), ExtendedConsensusError> {
) -> Result<(VecDeque<u64>, VecDeque<u128>), ContextCacheError> {
tracing::info!("Getting blocks timestamps");

let BlockchainResponse::BlockExtendedHeaderInRange(ext_header) = database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cuprate_types::{
Chain,
};

use crate::{Database, ExtendedConsensusError};
use crate::{ContextCacheError, Database};

/// The default amount of hard-fork votes to track to decide on activation of a hard-fork.
///
Expand All @@ -21,9 +21,9 @@ const DEFAULT_WINDOW_SIZE: usize = 10080; // supermajority window check length -
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct HardForkConfig {
/// The network we are on.
pub(crate) info: HFsInfo,
pub info: HFsInfo,
/// The amount of votes we are taking into account to decide on a fork activation.
pub(crate) window: usize,
pub window: usize,
}

impl HardForkConfig {
Expand Down Expand Up @@ -54,17 +54,17 @@ impl HardForkConfig {

/// A struct that keeps track of the current hard-fork and current votes.
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct HardForkState {
pub struct HardForkState {
/// The current active hard-fork.
pub(crate) current_hardfork: HardFork,
pub current_hardfork: HardFork,

/// The hard-fork config.
pub(crate) config: HardForkConfig,
pub config: HardForkConfig,
/// The votes in the current window.
pub(crate) votes: HFVotes,
pub votes: HFVotes,

/// The last block height accounted for.
pub(crate) last_height: usize,
pub last_height: usize,
}

impl HardForkState {
Expand All @@ -74,7 +74,7 @@ impl HardForkState {
chain_height: usize,
config: HardForkConfig,
mut database: D,
) -> Result<Self, ExtendedConsensusError> {
) -> Result<Self, ContextCacheError> {
tracing::info!("Initializing hard-fork state this may take a while.");

let block_start = chain_height.saturating_sub(config.window);
Expand Down Expand Up @@ -122,11 +122,11 @@ impl HardForkState {
/// # Invariant
///
/// This _must_ only be used on a main-chain cache.
pub(crate) async fn pop_blocks_main_chain<D: Database + Clone>(
pub async fn pop_blocks_main_chain<D: Database + Clone>(
&mut self,
numb_blocks: usize,
database: D,
) -> Result<(), ExtendedConsensusError> {
) -> Result<(), ContextCacheError> {
let Some(retained_blocks) = self.votes.total_votes().checked_sub(self.config.window) else {
*self = Self::init_from_chain_height(
self.last_height + 1 - numb_blocks,
Expand Down Expand Up @@ -159,7 +159,7 @@ impl HardForkState {
}

/// Add a new block to the cache.
pub(crate) fn new_block(&mut self, vote: HardFork, height: usize) {
pub fn new_block(&mut self, vote: HardFork, height: usize) {
// We don't _need_ to take in `height` but it's for safety, so we don't silently loose track
// of blocks.
assert_eq!(self.last_height + 1, height);
Expand Down Expand Up @@ -194,7 +194,7 @@ impl HardForkState {
}

/// Returns the current hard-fork.
pub(crate) const fn current_hardfork(&self) -> HardFork {
pub const fn current_hardfork(&self) -> HardFork {
self.current_hardfork
}
}
Expand All @@ -205,7 +205,7 @@ async fn get_votes_in_range<D: Database>(
database: D,
block_heights: Range<usize>,
window_size: usize,
) -> Result<HFVotes, ExtendedConsensusError> {
) -> Result<HFVotes, ContextCacheError> {
let mut votes = HFVotes::new(window_size);

let BlockchainResponse::BlockExtendedHeaderInRange(vote_list) = database
Expand Down
Loading

0 comments on commit 978d72b

Please sign in to comment.