Skip to content

Commit 5f1bd10

Browse files
author
SyntheticBird45
committed
split
1 parent f7548ff commit 5f1bd10

File tree

28 files changed

+606
-536
lines changed

28 files changed

+606
-536
lines changed

Cargo.lock

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

binaries/cuprated/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/binaries/cuprated"
1111
# TODO: after v1.0.0, remove unneeded dependencies.
1212
cuprate-consensus = { path = "../../consensus" }
1313
cuprate-fast-sync = { path = "../../consensus/fast-sync" }
14+
cuprate-consensus-context = { path = "../../consensus/context" }
1415
cuprate-consensus-rules = { path = "../../consensus/rules" }
1516
cuprate-cryptonight = { path = "../../cryptonight" }
1617
cuprate-helper = { path = "../../helper" }

binaries/cuprated/src/blockchain/manager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use tower::{Service, ServiceExt};
77
use tracing::error;
88

99
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
10+
use cuprate_consensus_context::RawBlockChainContext;
1011
use cuprate_consensus::{
11-
context::RawBlockChainContext, BlockChainContextRequest, BlockChainContextResponse,
12+
BlockChainContextRequest, BlockChainContextResponse,
1213
BlockChainContextService, BlockVerifierService, ExtendedConsensusError, TxVerifierService,
1314
VerifyBlockRequest, VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
1415
};

binaries/cuprated/src/blockchain/manager/handler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use tower::{Service, ServiceExt};
99
use tracing::info;
1010

1111
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
12+
use cuprate_consensus_context::NewBlockData;
1213
use cuprate_consensus::{
13-
block::PreparedBlock, context::NewBlockData, transactions::new_tx_verification_data,
14+
block::PreparedBlock, transactions::new_tx_verification_data,
1415
BlockChainContextRequest, BlockChainContextResponse, BlockVerifierService,
1516
ExtendedConsensusError, VerifyBlockRequest, VerifyBlockResponse, VerifyTxRequest,
1617
VerifyTxResponse,

binaries/cuprated/src/rpc/request/blockchain_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::convert::Infallible;
55
use anyhow::Error;
66
use tower::{Service, ServiceExt};
77

8-
use cuprate_consensus::context::{
8+
use cuprate_consensus_context::{
99
BlockChainContext, BlockChainContextRequest, BlockChainContextResponse,
1010
BlockChainContextService,
1111
};

consensus/Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/consensus"
1111
cuprate-helper = { path = "../helper", default-features = false, features = ["std", "asynch", "num"] }
1212
cuprate-consensus-rules = { path = "./rules", features = ["rayon"] }
1313
cuprate-types = { path = "../types" }
14+
cuprate-consensus-context = { path = "./context" }
1415

1516
cfg-if = { workspace = true }
1617
thiserror = { workspace = true }
1718
tower = { workspace = true, features = ["util"] }
1819
tracing = { workspace = true, features = ["std", "attributes"] }
1920
futures = { workspace = true, features = ["std", "async-await"] }
2021

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

2424
rayon = { workspace = true }
2525
thread_local = { workspace = true }
26-
tokio = { workspace = true, features = ["rt"] }
27-
tokio-util = { workspace = true }
2826

2927
hex = { workspace = true }
3028
rand = { workspace = true }
@@ -42,4 +40,4 @@ proptest = { workspace = true }
4240
proptest-derive = { workspace = true }
4341

4442
[lints]
45-
workspace = true
43+
workspace = true

consensus/context/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,22 @@
22
name = "cuprate-consensus-context"
33
version = "0.1.0"
44
edition = "2021"
5+
license = "MIT"
6+
authors = ["SyntheticBird","Boog900"]
57

68
[dependencies]
9+
futures = { workspace = true, features = ["std", "async-await"] }
10+
tokio = { workspace = true, features = ["rt-multi-thread", "macros"]}
11+
tokio-util = { workspace = true }
12+
tower = { workspace = true, features = ["util"] }
13+
tracing = { workspace = true, features = ["std", "attributes"] }
14+
thiserror = { workspace = true }
15+
16+
randomx-rs = { workspace = true }
17+
rayon = { workspace = true }
18+
thread_local = { workspace = true }
19+
hex = { workspace = true }
20+
21+
cuprate-consensus-rules = { path = "../rules", features = ["proptest"]}
22+
cuprate-helper = { path = "../../helper", default-features = false, features = ["std", "cast"] }
23+
cuprate-types = { path = "../../types", default-features = false }

consensus/src/context/alt_chains.rs renamed to consensus/context/src/alt_chains.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ use cuprate_types::{
99
};
1010

1111
use crate::{
12-
ExtendedConsensusError,
12+
ContextCacheError,
1313
__private::Database,
14-
context::{difficulty::DifficultyCache, rx_vms::RandomXVm, weight::BlockWeightsCache},
14+
difficulty::DifficultyCache,
15+
rx_vms::RandomXVm,
16+
weight::BlockWeightsCache
1517
};
1618

1719
pub(crate) mod sealed {
@@ -98,7 +100,7 @@ impl AltChainMap {
98100
&mut self,
99101
prev_id: [u8; 32],
100102
database: D,
101-
) -> Result<Box<AltChainContextCache>, ExtendedConsensusError> {
103+
) -> Result<Box<AltChainContextCache>, ContextCacheError> {
102104
if let Some(cache) = self.alt_cache_map.remove(&prev_id) {
103105
return Ok(cache);
104106
}
@@ -133,7 +135,7 @@ pub(crate) async fn get_alt_chain_difficulty_cache<D: Database + Clone>(
133135
prev_id: [u8; 32],
134136
main_chain_difficulty_cache: &DifficultyCache,
135137
mut database: D,
136-
) -> Result<DifficultyCache, ExtendedConsensusError> {
138+
) -> Result<DifficultyCache, ContextCacheError> {
137139
// find the block with hash == prev_id.
138140
let BlockchainResponse::FindBlock(res) = database
139141
.ready()
@@ -180,7 +182,7 @@ pub(crate) async fn get_alt_chain_weight_cache<D: Database + Clone>(
180182
prev_id: [u8; 32],
181183
main_chain_weight_cache: &BlockWeightsCache,
182184
mut database: D,
183-
) -> Result<BlockWeightsCache, ExtendedConsensusError> {
185+
) -> Result<BlockWeightsCache, ContextCacheError> {
184186
// find the block with hash == prev_id.
185187
let BlockchainResponse::FindBlock(res) = database
186188
.ready()

consensus/src/context/difficulty.rs renamed to consensus/context/src/difficulty.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use cuprate_types::{
1717
Chain,
1818
};
1919

20-
use crate::{Database, ExtendedConsensusError, HardFork};
20+
use crate::{Database, ContextCacheError, HardFork};
2121

2222
/// The amount of blocks we account for to calculate difficulty
2323
const DIFFICULTY_WINDOW: usize = 720;
@@ -33,9 +33,9 @@ const DIFFICULTY_LAG: usize = 15;
3333
///
3434
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
3535
pub struct DifficultyCacheConfig {
36-
pub(crate) window: usize,
37-
pub(crate) cut: usize,
38-
pub(crate) lag: usize,
36+
pub window: usize,
37+
pub cut: usize,
38+
pub lag: usize,
3939
}
4040

4141
impl DifficultyCacheConfig {
@@ -74,13 +74,13 @@ impl DifficultyCacheConfig {
7474
pub struct DifficultyCache {
7575
/// The list of timestamps in the window.
7676
/// len <= [`DIFFICULTY_BLOCKS_COUNT`]
77-
pub(crate) timestamps: VecDeque<u64>,
77+
pub timestamps: VecDeque<u64>,
7878
/// The current cumulative difficulty of the chain.
79-
pub(crate) cumulative_difficulties: VecDeque<u128>,
79+
pub cumulative_difficulties: VecDeque<u128>,
8080
/// The last height we accounted for.
81-
pub(crate) last_accounted_height: usize,
81+
pub last_accounted_height: usize,
8282
/// The config
83-
pub(crate) config: DifficultyCacheConfig,
83+
pub config: DifficultyCacheConfig,
8484
}
8585

8686
impl DifficultyCache {
@@ -91,7 +91,7 @@ impl DifficultyCache {
9191
config: DifficultyCacheConfig,
9292
database: D,
9393
chain: Chain,
94-
) -> Result<Self, ExtendedConsensusError> {
94+
) -> Result<Self, ContextCacheError> {
9595
tracing::info!("Initializing difficulty cache this may take a while.");
9696

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

367367
let BlockchainResponse::BlockExtendedHeaderInRange(ext_header) = database

consensus/src/context/hardforks.rs renamed to consensus/context/src/hardforks.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use cuprate_types::{
99
Chain,
1010
};
1111

12-
use crate::{Database, ExtendedConsensusError};
12+
use crate::{Database, ContextCacheError};
1313

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

2929
impl HardForkConfig {
@@ -54,17 +54,17 @@ impl HardForkConfig {
5454

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

6161
/// The hard-fork config.
62-
pub(crate) config: HardForkConfig,
62+
pub config: HardForkConfig,
6363
/// The votes in the current window.
64-
pub(crate) votes: HFVotes,
64+
pub votes: HFVotes,
6565

6666
/// The last block height accounted for.
67-
pub(crate) last_height: usize,
67+
pub last_height: usize,
6868
}
6969

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

8080
let block_start = chain_height.saturating_sub(config.window);
@@ -122,11 +122,11 @@ impl HardForkState {
122122
/// # Invariant
123123
///
124124
/// This _must_ only be used on a main-chain cache.
125-
pub(crate) async fn pop_blocks_main_chain<D: Database + Clone>(
125+
pub async fn pop_blocks_main_chain<D: Database + Clone>(
126126
&mut self,
127127
numb_blocks: usize,
128128
database: D,
129-
) -> Result<(), ExtendedConsensusError> {
129+
) -> Result<(), ContextCacheError> {
130130
let Some(retained_blocks) = self.votes.total_votes().checked_sub(self.config.window) else {
131131
*self = Self::init_from_chain_height(
132132
self.last_height + 1 - numb_blocks,
@@ -159,7 +159,7 @@ impl HardForkState {
159159
}
160160

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

196196
/// Returns the current hard-fork.
197-
pub(crate) const fn current_hardfork(&self) -> HardFork {
197+
pub const fn current_hardfork(&self) -> HardFork {
198198
self.current_hardfork
199199
}
200200
}
@@ -205,7 +205,7 @@ async fn get_votes_in_range<D: Database>(
205205
database: D,
206206
block_heights: Range<usize>,
207207
window_size: usize,
208-
) -> Result<HFVotes, ExtendedConsensusError> {
208+
) -> Result<HFVotes, ContextCacheError> {
209209
let mut votes = HFVotes::new(window_size);
210210

211211
let BlockchainResponse::BlockExtendedHeaderInRange(vote_list) = database

0 commit comments

Comments
 (0)