Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subsystems configuration #277

Merged
merged 31 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d1e2481
Raw prototype of subsystems configuration
stanislav-tkach Jul 5, 2022
e3de6d7
Remove clap dependency from subsystem
stanislav-tkach Jul 7, 2022
7df933c
Add config struct
stanislav-tkach Jul 8, 2022
6559338
(Mostly) implement p2p config
stanislav-tkach Jul 8, 2022
dcc980c
Add example of option override
stanislav-tkach Jul 9, 2022
01c07d5
Fix p2p address
stanislav-tkach Jul 10, 2022
5187977
Add chain config to the configuration
stanislav-tkach Jul 11, 2022
9f98689
Add some assert_cmd tests
stanislav-tkach Jul 12, 2022
dd78bb9
Two configs
stanislav-tkach Jul 12, 2022
59f1144
Configs refactoring
stanislav-tkach Jul 12, 2022
1dd1d68
Use debug formatting for error
stanislav-tkach Jul 12, 2022
06ef0e4
Change fields order
stanislav-tkach Jul 12, 2022
be3b61a
Update the create_config test
stanislav-tkach Jul 12, 2022
e8de616
Rename configs
stanislav-tkach Jul 13, 2022
d78cefd
Revert chain config changes
stanislav-tkach Jul 13, 2022
01ed6df
Fix compilation everywhere
stanislav-tkach Jul 13, 2022
85e880e
Merge branch 'master' into subsystems-configuration
stanislav-tkach Jul 13, 2022
ffcf85a
Expand the chainstate config
stanislav-tkach Jul 13, 2022
bceecb3
Expand the p2p config
stanislav-tkach Jul 13, 2022
353a4c5
Add comand line tests
stanislav-tkach Jul 13, 2022
fb77d5a
Fix the rpc example
stanislav-tkach Jul 13, 2022
c86991e
Fix test node
stanislav-tkach Jul 13, 2022
d92e234
Rvert/fix test node initialization
stanislav-tkach Jul 14, 2022
8165b6b
Use run command to start node in the functional tests
stanislav-tkach Jul 14, 2022
06147c5
Add config to the functional tests
stanislav-tkach Jul 14, 2022
839481c
Remove ports from the chain config
stanislav-tkach Jul 14, 2022
80a8d9e
Rename p2p config parameters
stanislav-tkach Jul 14, 2022
aa99255
Update functional tests config
stanislav-tkach Jul 15, 2022
aeaa5b4
Node config initialization refactoring
stanislav-tkach Jul 15, 2022
e204cbc
Apply review comments
stanislav-tkach Jul 15, 2022
809e9e7
Apply review comments (typos and better description)
stanislav-tkach Jul 18, 2022
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
29 changes: 29 additions & 0 deletions 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 chainstate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1"
chainstate-storage = {path = '../chainstate-storage'}
chainstate-types = {path = '../chainstate-types'}
common = {path = '../common'}
Expand All @@ -18,12 +17,14 @@ serialization = {path = "../serialization"}
subsystem = {path = '../subsystem'}
utils = {path = '../utils'}

async-trait = "0.1"
hex = "0.4"
itertools = "0.10"
jsonrpsee = {version = "0.14", features = ["macros"]}
num = "0.4.0"
replace_with = "0.1"
thiserror = "1.0"
serde = { version = "1", features = ["derive"] }

[dev-dependencies]
mockall = "0.11"
Expand Down
35 changes: 35 additions & 0 deletions chainstate/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2022 RBB S.r.l
// [email protected]
// SPDX-License-Identifier: MIT
// Licensed under the MIT License;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://spdx.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use serde::{Deserialize, Serialize};

/// The chainstate subsystem configuration.
#[derive(Serialize, Deserialize, Debug)]
pub struct ChainstateConfig {
/// The number of maximum attempts to process a block.
pub max_db_commit_attempts: usize,
stanislav-tkach marked this conversation as resolved.
Show resolved Hide resolved
/// The maximum capacity of the orphan blocks pool.
pub max_orphan_blocks: usize,
stanislav-tkach marked this conversation as resolved.
Show resolved Hide resolved
}

impl ChainstateConfig {
/// Creates a new chainstate configuration isntance.
pub fn new() -> Self {
Self {
max_db_commit_attempts: 10,
max_orphan_blocks: 512,
}
}
}
13 changes: 10 additions & 3 deletions chainstate/src/detail/block_index_history_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,24 @@ mod tests {
primitives::{time, Idable, H256},
};

use crate::{BlockSource, Chainstate};
use crate::{BlockSource, Chainstate, ChainstateConfig};

use super::*;

#[test]
fn history_iteration() {
common::concurrency::model(|| {
let chain_config = Arc::new(create_unit_test_config());
let chainstte_config = ChainstateConfig::new();
stanislav-tkach marked this conversation as resolved.
Show resolved Hide resolved
let storage = Store::new_empty().unwrap();
let mut chainstate =
Chainstate::new(chain_config.clone(), storage, None, Default::default()).unwrap();
let mut chainstate = Chainstate::new(
chain_config.clone(),
chainstte_config,
storage,
None,
Default::default(),
)
.unwrap();

// put three blocks in a chain after genesis
let block1 = Block::new(
Expand Down
7 changes: 6 additions & 1 deletion chainstate/src/detail/chainstateref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use common::{
use logging::log;
use utils::ensure;

use crate::{BlockError, BlockSource};
use crate::{BlockError, BlockSource, ChainstateConfig};

use super::{
consensus_validator::{self, BlockIndexHandle},
Expand All @@ -44,6 +44,7 @@ use super::{

pub(crate) struct ChainstateRef<'a, S, O> {
chain_config: &'a ChainConfig,
_chainstate_config: &'a ChainstateConfig,
db_tx: S,
orphan_blocks: O,
time_getter: &'a TimeGetterFn,
Expand Down Expand Up @@ -92,12 +93,14 @@ impl<'a, S: TransactionRw<Error = chainstate_storage::Error>, O> ChainstateRef<'
impl<'a, S: BlockchainStorageRead, O: OrphanBlocks> ChainstateRef<'a, S, O> {
pub fn new_rw(
chain_config: &'a ChainConfig,
chainstate_config: &'a ChainstateConfig,
db_tx: S,
orphan_blocks: O,
time_getter: &'a TimeGetterFn,
) -> ChainstateRef<'a, S, O> {
ChainstateRef {
chain_config,
_chainstate_config: chainstate_config,
db_tx,
orphan_blocks,
time_getter,
Expand All @@ -106,12 +109,14 @@ impl<'a, S: BlockchainStorageRead, O: OrphanBlocks> ChainstateRef<'a, S, O> {

pub fn new_ro(
chain_config: &'a ChainConfig,
chainstate_config: &'a ChainstateConfig,
db_tx: S,
orphan_blocks: O,
time_getter: &'a TimeGetterFn,
) -> ChainstateRef<'a, S, O> {
ChainstateRef {
chain_config,
_chainstate_config: chainstate_config,
db_tx,
orphan_blocks,
time_getter,
Expand Down
18 changes: 14 additions & 4 deletions chainstate/src/detail/median_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn calculate_median_time_past<H: BlockIndexHandle>(

#[cfg(test)]
mod test {
use crate::{detail::time_getter::TimeGetter, BlockSource, Chainstate};
use crate::{detail::time_getter::TimeGetter, BlockSource, Chainstate, ChainstateConfig};

use super::*;
use chainstate_storage::Store;
Expand Down Expand Up @@ -86,9 +86,16 @@ mod test {
fn blocks_median_time() {
common::concurrency::model(|| {
let chain_config = Arc::new(create_unit_test_config());
let chainstate_config = ChainstateConfig::new();
let storage = Store::new_empty().unwrap();
let mut chainstate =
Chainstate::new(chain_config, storage, None, Default::default()).unwrap();
let mut chainstate = Chainstate::new(
chain_config,
chainstate_config,
storage,
None,
Default::default(),
)
.unwrap();

let block_count = 500;

Expand Down Expand Up @@ -151,7 +158,10 @@ mod test {
}));

let storage = Store::new_empty().unwrap();
let mut chainstate = Chainstate::new(chain_config, storage, None, time_getter).unwrap();
let chainstate_config = ChainstateConfig::new();
let mut chainstate =
Chainstate::new(chain_config, chainstate_config, storage, None, time_getter)
.unwrap();

// we use unordered block times, and ensure that the median will be in the right spot
let block1_time = current_time.load(Ordering::SeqCst) as u32 + 1;
Expand Down
20 changes: 12 additions & 8 deletions chainstate/src/detail/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
//
// Author(s): S. Afach, A. Sinitsyn

use crate::detail::orphan_blocks::OrphanBlocksPool;
use crate::ChainstateEvent;
use crate::{detail::orphan_blocks::OrphanBlocksPool, ChainstateConfig, ChainstateEvent};
use chainstate_storage::Transactional;
use chainstate_types::block_index::BlockIndex;
use common::chain::block::{Block, BlockHeader};
Expand Down Expand Up @@ -58,6 +57,7 @@ use time_getter::TimeGetter;
#[must_use]
pub struct Chainstate {
chain_config: Arc<ChainConfig>,
chainstate_config: ChainstateConfig,
chainstate_storage: chainstate_storage::Store,
orphan_blocks: OrphanBlocksPool,
custom_orphan_error_hook: Option<Arc<OrphanErrorHandler>>,
Expand All @@ -81,6 +81,7 @@ impl Chainstate {
let db_tx = self.chainstate_storage.transaction_rw();
chainstateref::ChainstateRef::new_rw(
&self.chain_config,
&self.chainstate_config,
db_tx,
self.orphan_blocks.as_rw_ref(),
self.time_getter.getter(),
Expand All @@ -92,6 +93,7 @@ impl Chainstate {
let db_tx = self.chainstate_storage.transaction_ro();
chainstateref::ChainstateRef::new_ro(
&self.chain_config,
&self.chainstate_config,
db_tx,
self.orphan_blocks.as_ro_ref(),
self.time_getter.getter(),
Expand All @@ -104,6 +106,7 @@ impl Chainstate {

pub fn new(
chain_config: Arc<ChainConfig>,
chainstate_config: ChainstateConfig,
chainstate_storage: chainstate_storage::Store,
custom_orphan_error_hook: Option<Arc<OrphanErrorHandler>>,
time_getter: TimeGetter,
Expand All @@ -112,6 +115,7 @@ impl Chainstate {

let mut cons = Self::new_no_genesis(
chain_config,
chainstate_config,
chainstate_storage,
custom_orphan_error_hook,
time_getter,
Expand All @@ -138,14 +142,17 @@ impl Chainstate {

fn new_no_genesis(
chain_config: Arc<ChainConfig>,
chainstate_config: ChainstateConfig,
chainstate_storage: chainstate_storage::Store,
custom_orphan_error_hook: Option<Arc<OrphanErrorHandler>>,
time_getter: TimeGetter,
) -> Result<Self, crate::ChainstateError> {
let orphan_blocks = OrphanBlocksPool::new(chainstate_config.max_orphan_blocks);
let cons = Self {
chain_config,
chainstate_config,
chainstate_storage,
orphan_blocks: OrphanBlocksPool::new_default(),
orphan_blocks,
custom_orphan_error_hook,
events_controller: EventsController::new(),
time_getter,
Expand Down Expand Up @@ -188,13 +195,10 @@ impl Chainstate {
block_source: BlockSource,
attempt_number: usize,
) -> Result<Option<BlockIndex>, BlockError> {
// TODO: move to a configuration object that loads from command line arguments
const MAX_DB_COMMIT_COUNT: usize = 10;

if attempt_number >= MAX_DB_COMMIT_COUNT {
if attempt_number >= self.chainstate_config.max_db_commit_attempts {
Err(BlockError::DatabaseCommitError(
block.get_id(),
MAX_DB_COMMIT_COUNT,
self.chainstate_config.max_db_commit_attempts,
db_error,
))
} else {
Expand Down
Loading