Skip to content

Commit 978d72b

Browse files
Move consensus context service into a subcrate. (#318)
Co-authored-by: Boog900 <boog900@tutanota.com>
1 parent f9b847b commit 978d72b

File tree

28 files changed

+218
-133
lines changed

28 files changed

+218
-133
lines changed

Cargo.lock

Lines changed: 22 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ members = [
55
"binaries/cuprated",
66
"constants",
77
"consensus",
8+
"consensus/context",
89
"consensus/fast-sync",
910
"consensus/rules",
1011
"cryptonight",
@@ -322,4 +323,4 @@ non_camel_case_types = "deny"
322323
# unused_results = "deny"
323324
# non_exhaustive_omitted_patterns = "deny"
324325
# missing_docs = "deny"
325-
# missing_copy_implementations = "deny"
326+
# missing_copy_implementations = "deny"

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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ use tracing::error;
88

99
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
1010
use cuprate_consensus::{
11-
context::RawBlockChainContext, BlockChainContextRequest, BlockChainContextResponse,
12-
BlockChainContextService, BlockVerifierService, ExtendedConsensusError, TxVerifierService,
13-
VerifyBlockRequest, VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
11+
BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService,
12+
BlockVerifierService, ExtendedConsensusError, TxVerifierService, VerifyBlockRequest,
13+
VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
1414
};
15+
use cuprate_consensus_context::RawBlockChainContext;
1516
use cuprate_p2p::{
1617
block_downloader::{BlockBatch, BlockDownloaderConfig},
1718
BroadcastSvc, NetworkInterface,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use tracing::info;
1010

1111
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
1212
use cuprate_consensus::{
13-
block::PreparedBlock, context::NewBlockData, transactions::new_tx_verification_data,
14-
BlockChainContextRequest, BlockChainContextResponse, BlockVerifierService,
15-
ExtendedConsensusError, VerifyBlockRequest, VerifyBlockResponse, VerifyTxRequest,
16-
VerifyTxResponse,
13+
block::PreparedBlock, transactions::new_tx_verification_data, BlockChainContextRequest,
14+
BlockChainContextResponse, BlockVerifierService, ExtendedConsensusError, VerifyBlockRequest,
15+
VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
1716
};
17+
use cuprate_consensus_context::NewBlockData;
1818
use cuprate_helper::cast::usize_to_u64;
1919
use cuprate_p2p::{block_downloader::BlockBatch, constants::LONG_BAN, BroadcastRequest};
2020
use cuprate_types::{

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
};

books/architecture/src/appendix/crates.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ cargo doc --open --package cuprate-blockchain
1616
| Crate | In-tree path | Purpose |
1717
|-------|--------------|---------|
1818
| [`cuprate-consensus`](https://doc.cuprate.org/cuprate_consensus) | [`consensus/`](https://github.com/Cuprate/cuprate/tree/main/consensus) | TODO
19-
| [`cuprate-consensus-rules`](https://doc.cuprate.org/cuprate_consensus_rules) | [`consensus/rules/`](https://github.com/Cuprate/cuprate/tree/main/consensus-rules) | TODO
19+
| [`cuprate-consensus-context`](https://doc.cuprate.org/cuprate_consensus_context) | [`consensus/context/`](https://github.com/Cuprate/cuprate/tree/main/consensus/context) | TODO
20+
| [`cuprate-consensus-rules`](https://doc.cuprate.org/cuprate_consensus_rules) | [`consensus/rules/`](https://github.com/Cuprate/cuprate/tree/main/consensus/rules) | TODO
2021
| [`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
2122

2223
## Networking

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name = "cuprate-consensus-context"
3+
version = "0.1.0"
4+
edition = "2021"
5+
license = "MIT"
6+
authors = ["SyntheticBird","Boog900"]
7+
8+
[dependencies]
9+
cuprate-consensus-rules = { path = "../rules", features = ["proptest"]}
10+
cuprate-helper = { path = "../../helper", default-features = false, features = ["std", "cast"] }
11+
cuprate-types = { path = "../../types", default-features = false }
12+
13+
futures = { workspace = true, features = ["std", "async-await"] }
14+
tokio = { workspace = true, features = ["rt-multi-thread", "macros"]}
15+
tokio-util = { workspace = true }
16+
tower = { workspace = true, features = ["util"] }
17+
tracing = { workspace = true, features = ["std", "attributes"] }
18+
thiserror = { workspace = true }
19+
20+
monero-serai = { workspace = true, features = ["std"] }
21+
randomx-rs = { workspace = true }
22+
rayon = { workspace = true }
23+
thread_local = { workspace = true }
24+
hex = { workspace = true }

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

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

1111
use crate::{
12-
ExtendedConsensusError,
13-
__private::Database,
14-
context::{difficulty::DifficultyCache, rx_vms::RandomXVm, weight::BlockWeightsCache},
12+
ContextCacheError, __private::Database, difficulty::DifficultyCache, rx_vms::RandomXVm,
13+
weight::BlockWeightsCache,
1514
};
1615

1716
pub(crate) mod sealed {
@@ -38,7 +37,7 @@ pub struct AltChainContextCache {
3837
pub chain_height: usize,
3938
/// The top hash of the alt chain.
4039
pub top_hash: [u8; 32],
41-
/// The [`ChainID`] of the alt chain.
40+
/// The [`ChainId`] of the alt chain.
4241
pub chain_id: Option<ChainId>,
4342
/// The parent [`Chain`] of this alt chain.
4443
pub parent_chain: Chain,
@@ -98,7 +97,7 @@ impl AltChainMap {
9897
&mut self,
9998
prev_id: [u8; 32],
10099
database: D,
101-
) -> Result<Box<AltChainContextCache>, ExtendedConsensusError> {
100+
) -> Result<Box<AltChainContextCache>, ContextCacheError> {
102101
if let Some(cache) = self.alt_cache_map.remove(&prev_id) {
103102
return Ok(cache);
104103
}
@@ -133,7 +132,7 @@ pub(crate) async fn get_alt_chain_difficulty_cache<D: Database + Clone>(
133132
prev_id: [u8; 32],
134133
main_chain_difficulty_cache: &DifficultyCache,
135134
mut database: D,
136-
) -> Result<DifficultyCache, ExtendedConsensusError> {
135+
) -> Result<DifficultyCache, ContextCacheError> {
137136
// find the block with hash == prev_id.
138137
let BlockchainResponse::FindBlock(res) = database
139138
.ready()
@@ -180,7 +179,7 @@ pub(crate) async fn get_alt_chain_weight_cache<D: Database + Clone>(
180179
prev_id: [u8; 32],
181180
main_chain_weight_cache: &BlockWeightsCache,
182181
mut database: D,
183-
) -> Result<BlockWeightsCache, ExtendedConsensusError> {
182+
) -> Result<BlockWeightsCache, ContextCacheError> {
184183
// find the block with hash == prev_id.
185184
let BlockchainResponse::FindBlock(res) = database
186185
.ready()

0 commit comments

Comments
 (0)