Skip to content

Commit 71c0929

Browse files
Merge pull request #351 from mintlayer/move-consensus-from-chainstate-to-separate-crate
Move the consensus related logic from chainstate to a separate crate
2 parents 1c84e78 + 9295766 commit 71c0929

File tree

32 files changed

+360
-238
lines changed

32 files changed

+360
-238
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,29 @@ edition = "2021"
1111

1212
[workspace]
1313
members = [
14-
"common", # everything else, until it's moved to another crate
15-
"crypto", # cryptographic primitives and their interfaces
16-
"chainstate", # code on chainstate of blocks and transactions
17-
"script", # bitcoin script and its interfaces
18-
"logging", # logging engine and its interfaces
19-
"mempool", # mempool interface and implementation
20-
"p2p", # p2p communication interfaces and protocols
21-
"rpc", # rpc abstraction and implementation
22-
"serialization", # full featured serialization interfaces and implementations
23-
"serialization/core", # serialization core tools
24-
"serialization/tagged", # serialization for direct/tagged encoding style
25-
"serialization/tagged/derive", # direct/tagged encoding style derive macros
26-
"storage", # storage abstraction layer and its implementation
27-
"storage/core", # Core backend-agnostic storage abstraction
28-
"storage/inmemory", # In-memory storage backend implementation
29-
"subsystem", # Utilities for working with concurrent subsystems
30-
"node", # node executable
31-
"wallet", # wallet executable
32-
"utils", # various utilities
33-
"utxo", # utxo and related utilities (cache, undo, etc.)
34-
"test", # integration tests
35-
"test-utils", # various utilities for tests
14+
"common", # Everything else, until it's moved to another crate.
15+
"crypto", # Cryptographic primitives and their interfaces.
16+
"consensus", # Consensus related logic.
17+
"chainstate", # Code on chainstate of blocks and transactions.
18+
"script", # Bitcoin script and its interfaces.
19+
"logging", # Logging engine and its interfaces.
20+
"mempool", # Mempool interface and implementation.
21+
"p2p", # P2p communication interfaces and protocols.
22+
"rpc", # Rpc abstraction and implementation.
23+
"serialization", # Full featured serialization interfaces and implementations.
24+
"serialization/core", # Serialization core tools.
25+
"serialization/tagged", # Serialization for direct/tagged encoding style.
26+
"serialization/tagged/derive", # direct/tagged encoding style derive macros.
27+
"storage", # storage abstraction layer and its implementation.
28+
"storage/core", # Core backend-agnostic storage abstraction.
29+
"storage/inmemory", # In-memory storage backend implementation.
30+
"subsystem", # Utilities for working with concurrent subsystems.
31+
"node", # Node executable.
32+
"wallet", # Wallet executable.
33+
"utils", # Various utilities.
34+
"utxo", # Utxo and related utilities (cache, undo, etc.).
35+
"test", # Integration tests.
36+
"test-utils", # Various utilities for tests.
3637
]
3738

3839
default-members = [

chainstate-storage/src/internal/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515

1616
pub mod utxo_db;
1717

18-
use chainstate_types::block_index::BlockIndex;
19-
use common::chain::transaction::{Transaction, TxMainChainIndex, TxMainChainPosition};
20-
use common::chain::{Block, GenBlock, OutPoint, OutPointSourceId};
21-
use common::primitives::{BlockHeight, Id, Idable};
18+
use chainstate_types::BlockIndex;
19+
use common::{
20+
chain::{
21+
transaction::{Transaction, TxMainChainIndex, TxMainChainPosition},
22+
Block, GenBlock, OutPoint, OutPointSourceId,
23+
},
24+
primitives::{BlockHeight, Id, Idable},
25+
};
2226
use serialization::{Codec, Decode, DecodeAll, Encode};
2327
use storage::traits::{self, MapMut, MapRef, TransactionRo, TransactionRw};
24-
use utxo::utxo_storage::{UtxosStorageRead, UtxosStorageWrite};
25-
use utxo::{BlockUndo, Utxo};
28+
use utxo::{
29+
utxo_storage::{UtxosStorageRead, UtxosStorageWrite},
30+
BlockUndo, Utxo,
31+
};
2632

2733
use crate::{BlockchainStorage, BlockchainStorageRead, BlockchainStorageWrite, Transactional};
2834

chainstate-storage/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
//! Application-level interface for the persistent blockchain storage.
1717
18-
use chainstate_types::block_index::BlockIndex;
18+
use chainstate_types::BlockIndex;
1919
use common::chain::transaction::{Transaction, TxMainChainIndex, TxMainChainPosition};
2020
use common::chain::OutPointSourceId;
2121
use common::chain::{Block, GenBlock};

chainstate-storage/src/mock.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515

1616
//! A mock version of the blockchain storage.
1717
18-
use chainstate_types::block_index::BlockIndex;
19-
use common::chain::transaction::{
20-
OutPointSourceId, Transaction, TxMainChainIndex, TxMainChainPosition,
18+
use chainstate_types::BlockIndex;
19+
use common::{
20+
chain::{
21+
transaction::{OutPointSourceId, Transaction, TxMainChainIndex, TxMainChainPosition},
22+
Block, GenBlock, OutPoint,
23+
},
24+
primitives::{BlockHeight, Id},
25+
};
26+
use utxo::{
27+
utxo_storage::{UtxosStorageRead, UtxosStorageWrite},
28+
BlockUndo, Utxo,
2129
};
22-
use common::chain::{Block, GenBlock, OutPoint};
23-
use common::primitives::{BlockHeight, Id};
24-
use utxo::utxo_storage::{UtxosStorageRead, UtxosStorageWrite};
25-
use utxo::{BlockUndo, Utxo};
2630

2731
mockall::mock! {
2832
/// A mock object for blockchain storage

chainstate-types/src/error.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2022 RBB S.r.l
2+
3+
// SPDX-License-Identifier: MIT
4+
// Licensed under the MIT License;
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
use thiserror::Error;
17+
18+
use common::{
19+
chain::{Block, GenBlock},
20+
primitives::{BlockHeight, Id},
21+
};
22+
23+
#[derive(Error, Debug, PartialEq, Eq, Clone)]
24+
pub enum PropertyQueryError {
25+
#[error("Blockchain storage error: {0}")]
26+
StorageError(#[from] crate::storage_result::Error),
27+
#[error("Best block not found")]
28+
BestBlockNotFound,
29+
#[error("Best block index not found")]
30+
BestBlockIndexNotFound,
31+
#[error("Block not found {0}")]
32+
BlockNotFound(Id<Block>),
33+
#[error("Previous block index not found {0}")]
34+
PrevBlockIndexNotFound(Id<GenBlock>),
35+
#[error("Block for height {0} not found")]
36+
BlockForHeightNotFound(BlockHeight),
37+
#[error("Provided an empty list")]
38+
InvalidInputEmpty,
39+
#[error("Invalid ancestor height: sought ancestor with height {ancestor_height} for block with height {block_height}")]
40+
InvalidAncestorHeight {
41+
block_height: BlockHeight,
42+
ancestor_height: BlockHeight,
43+
},
44+
#[error("Genesis block has no header")]
45+
GenesisHeaderRequested,
46+
}

chainstate/src/detail/gen_block_index.rs renamed to chainstate-types/src/gen_block_index.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use chainstate_types::block_index::BlockIndex;
17-
use common::chain::block::timestamp::BlockTimestamp;
18-
use common::chain::{GenBlock, Genesis};
19-
use common::primitives::{id::WithId, BlockHeight, Id, Idable};
20-
use common::Uint256;
2116
use std::sync::Arc;
2217

18+
use common::{
19+
chain::{block::timestamp::BlockTimestamp, GenBlock, Genesis},
20+
primitives::{id::WithId, BlockHeight, Id, Idable},
21+
Uint256,
22+
};
23+
24+
use crate::block_index::BlockIndex;
25+
2326
/// Generalized block index
2427
#[derive(Clone, Debug)]
2528
#[allow(clippy::large_enum_variant)]

chainstate-types/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
pub mod block_index;
17-
pub mod height_skip;
18-
pub mod locator;
1916
pub mod storage_result;
17+
18+
pub use crate::{
19+
block_index::BlockIndex, error::PropertyQueryError, gen_block_index::GenBlockIndex,
20+
height_skip::get_skip_height, locator::Locator,
21+
};
22+
23+
mod block_index;
24+
mod error;
25+
mod gen_block_index;
26+
mod height_skip;
27+
mod locator;

chainstate/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ serialization = {path = "../serialization"}
1717
subsystem = {path = '../subsystem'}
1818
utxo = {path = '../utxo'}
1919
utils = {path = '../utils'}
20+
consensus = {path = "../consensus"}
2021

2122
async-trait = "0.1"
2223
hex = "0.4"
2324
itertools = "0.10"
2425
jsonrpsee = {version = "0.14", features = ["macros"]}
25-
num = "0.4.0"
2626
replace_with = "0.1"
2727
thiserror = "1.0"
2828
serde = { version = "1", features = ["derive"] }

chainstate/src/detail/ban_score.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use crate::BlockError;
16+
use consensus::{ConsensusPoWError, ConsensusVerificationError};
1717

1818
use super::{
19-
pow::error::ConsensusPoWError, transaction_verifier::error::ConnectTransactionError,
20-
BlockSizeError, CheckBlockError, CheckBlockTransactionsError, ConsensusVerificationError,
21-
OrphanCheckError,
19+
transaction_verifier::error::ConnectTransactionError, BlockSizeError, CheckBlockError,
20+
CheckBlockTransactionsError, OrphanCheckError,
2221
};
22+
use crate::BlockError;
2323

2424
// TODO: use a ban_score macro in a form similar to thiserror::Error in order to define the ban score
2525
// value of an error on the error enum arms instead of separately like in this file

chainstate/src/detail/block_index_history_iter.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use super::{consensus_validator::BlockIndexHandle, GenBlockIndex};
1716
use common::{chain::GenBlock, primitives::Id};
17+
use consensus::BlockIndexHandle;
1818
use logging::log;
1919

20+
use super::GenBlockIndex;
21+
2022
/// An iterator that starts at some block starting from a given it, and at every `next()` member call will provide the previous block index,
2123
/// The last viable block index is of the genesis block
2224
pub struct BlockIndexHistoryIterator<'a, H> {

chainstate/src/detail/chainstateref.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515

1616
use std::{collections::BTreeSet, sync::Arc};
1717

18-
use super::{
19-
consensus_validator::TransactionIndexHandle, gen_block_index::GenBlockIndex,
20-
median_time::calculate_median_time_past, time_getter::TimeGetterFn,
21-
};
2218
use chainstate_storage::{BlockchainStorageRead, BlockchainStorageWrite, TransactionRw};
23-
use chainstate_types::{block_index::BlockIndex, height_skip::get_skip_height};
19+
use chainstate_types::{get_skip_height, BlockIndex, GenBlockIndex, PropertyQueryError};
2420
use common::{
2521
chain::{
2622
block::{calculate_tx_merkle_root, calculate_witness_merkle_root, BlockHeader},
@@ -29,17 +25,17 @@ use common::{
2925
primitives::{BlockDistance, BlockHeight, Id, Idable},
3026
Uint256,
3127
};
28+
use consensus::{BlockIndexHandle, TransactionIndexHandle};
3229
use logging::log;
3330
use utils::ensure;
3431

32+
use super::{median_time::calculate_median_time_past, time_getter::TimeGetterFn};
3533
use crate::{BlockError, BlockSource, ChainstateConfig};
3634

3735
use super::{
38-
consensus_validator::{self, BlockIndexHandle},
3936
orphan_blocks::{OrphanBlocks, OrphanBlocksMut},
4037
transaction_verifier::{BlockTransactableRef, TransactionVerifier},
4138
BlockSizeError, CheckBlockError, CheckBlockTransactionsError, OrphanCheckError,
42-
PropertyQueryError,
4339
};
4440

4541
pub(crate) struct ChainstateRef<'a, S, O> {
@@ -486,7 +482,7 @@ impl<'a, S: BlockchainStorageRead, O: OrphanBlocks> ChainstateRef<'a, S, O> {
486482
}
487483

488484
pub fn check_block(&self, block: &Block) -> Result<(), CheckBlockError> {
489-
consensus_validator::validate_consensus(self.chain_config, block.header(), self)
485+
consensus::validate_consensus(self.chain_config, block.header(), self)
490486
.map_err(CheckBlockError::ConsensusVerificationFailed)?;
491487
self.check_block_detail(block)?;
492488
Ok(())

0 commit comments

Comments
 (0)