From a5ea8e5320bf3055756a323a57e9dd690ff663b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Ver=C5=A1i=C4=87?= Date: Tue, 15 Oct 2024 00:53:25 +0200 Subject: [PATCH] refactor!: remove `max` prefix from `blocks_in_memory` (#5145) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marin Veršić --- crates/iroha_config/src/parameters/actual.rs | 2 +- .../iroha_config/src/parameters/defaults.rs | 2 +- crates/iroha_config/src/parameters/user.rs | 10 +++---- crates/iroha_config/tests/fixtures.rs | 2 +- crates/iroha_config/tests/fixtures/full.env | 2 +- crates/iroha_config/tests/fixtures/full.toml | 1 + crates/iroha_core/benches/kura.rs | 4 +-- crates/iroha_core/src/kura.rs | 26 +++++++++---------- docs/source/references/peer.template.toml | 1 + 9 files changed, 26 insertions(+), 24 deletions(-) diff --git a/crates/iroha_config/src/parameters/actual.rs b/crates/iroha_config/src/parameters/actual.rs index bf80379e84..89fafe21f2 100644 --- a/crates/iroha_config/src/parameters/actual.rs +++ b/crates/iroha_config/src/parameters/actual.rs @@ -101,7 +101,7 @@ pub struct Queue { pub struct Kura { pub init_mode: InitMode, pub store_dir: WithOrigin, - pub max_blocks_in_memory: NonZeroUsize, + pub blocks_in_memory: NonZeroUsize, pub debug_output_new_blocks: bool, } diff --git a/crates/iroha_config/src/parameters/defaults.rs b/crates/iroha_config/src/parameters/defaults.rs index 8ff4965e1c..f03bb9fbc1 100644 --- a/crates/iroha_config/src/parameters/defaults.rs +++ b/crates/iroha_config/src/parameters/defaults.rs @@ -25,7 +25,7 @@ pub mod kura { use nonzero_ext::nonzero; pub const STORE_DIR: &str = "./storage"; - pub const MAX_BLOCKS_IN_MEMORY: NonZeroUsize = nonzero!(128_usize); + pub const BLOCKS_IN_MEMORY: NonZeroUsize = nonzero!(128_usize); } pub mod network { diff --git a/crates/iroha_config/src/parameters/user.rs b/crates/iroha_config/src/parameters/user.rs index 3c465efff5..550f702b97 100644 --- a/crates/iroha_config/src/parameters/user.rs +++ b/crates/iroha_config/src/parameters/user.rs @@ -182,10 +182,10 @@ pub struct Kura { )] pub store_dir: WithOrigin, #[config( - env = "KURA_MAX_BLOCKS_IN_MEMORY", - default = "defaults::kura::MAX_BLOCKS_IN_MEMORY" + env = "KURA_BLOCKS_IN_MEMORY", + default = "defaults::kura::BLOCKS_IN_MEMORY" )] - pub max_blocks_in_memory: NonZeroUsize, + pub blocks_in_memory: NonZeroUsize, #[config(nested)] pub debug: KuraDebug, } @@ -195,7 +195,7 @@ impl Kura { let Self { init_mode, store_dir, - max_blocks_in_memory, + blocks_in_memory, debug: KuraDebug { output_new_blocks: debug_output_new_blocks, @@ -205,7 +205,7 @@ impl Kura { actual::Kura { init_mode, store_dir, - max_blocks_in_memory, + blocks_in_memory, debug_output_new_blocks, } } diff --git a/crates/iroha_config/tests/fixtures.rs b/crates/iroha_config/tests/fixtures.rs index f267cbfd82..b772453c3b 100644 --- a/crates/iroha_config/tests/fixtures.rs +++ b/crates/iroha_config/tests/fixtures.rs @@ -127,7 +127,7 @@ fn minimal_config_snapshot() { id: ParameterId(kura.store_dir), }, }, - max_blocks_in_memory: 128, + blocks_in_memory: 128, debug_output_new_blocks: false, }, sumeragi: Sumeragi { diff --git a/crates/iroha_config/tests/fixtures/full.env b/crates/iroha_config/tests/fixtures/full.env index a07f0e0c11..d7b916be70 100644 --- a/crates/iroha_config/tests/fixtures/full.env +++ b/crates/iroha_config/tests/fixtures/full.env @@ -7,7 +7,7 @@ GENESIS=./genesis.signed.scale API_ADDRESS=127.0.0.1:8080 KURA_INIT_MODE=strict KURA_STORE_DIR=/store/path/from/env -KURA_MAX_BLOCKS_IN_MEMORY=128 +KURA_BLOCKS_IN_MEMORY=128 KURA_DEBUG_OUTPUT_NEW_BLOCKS=false LOG_LEVEL=DEBUG LOG_FORMAT=pretty diff --git a/crates/iroha_config/tests/fixtures/full.toml b/crates/iroha_config/tests/fixtures/full.toml index c21bc715ee..298d5582a8 100644 --- a/crates/iroha_config/tests/fixtures/full.toml +++ b/crates/iroha_config/tests/fixtures/full.toml @@ -26,6 +26,7 @@ query_store_capacity_per_user = 128 [kura] init_mode = "strict" store_dir = "./storage" +blocks_in_memory = 128 [kura.debug] output_new_blocks = true diff --git a/crates/iroha_core/benches/kura.rs b/crates/iroha_core/benches/kura.rs index b4acb7c254..420d65cdbf 100644 --- a/crates/iroha_core/benches/kura.rs +++ b/crates/iroha_core/benches/kura.rs @@ -4,7 +4,7 @@ use byte_unit::{Byte, UnitType}; use criterion::{criterion_group, criterion_main, Criterion}; use iroha_config::{ base::WithOrigin, - parameters::{actual::Kura as Config, defaults::kura::MAX_BLOCKS_IN_MEMORY}, + parameters::{actual::Kura as Config, defaults::kura::BLOCKS_IN_MEMORY}, }; use iroha_core::{ block::*, @@ -25,7 +25,7 @@ async fn measure_block_size_for_n_executors(n_executors: u32) { let cfg = Config { init_mode: iroha_config::kura::InitMode::Strict, debug_output_new_blocks: false, - max_blocks_in_memory: MAX_BLOCKS_IN_MEMORY, + blocks_in_memory: BLOCKS_IN_MEMORY, store_dir: WithOrigin::inline(dir.path().to_path_buf()), }; let chain_id = ChainId::from("00000000-0000-0000-0000-000000000000"); diff --git a/crates/iroha_core/src/kura.rs b/crates/iroha_core/src/kura.rs index 46df90f60a..2a1c03cc0c 100644 --- a/crates/iroha_core/src/kura.rs +++ b/crates/iroha_core/src/kura.rs @@ -13,7 +13,7 @@ use std::{ use iroha_config::{ kura::InitMode, - parameters::{actual::Kura as Config, defaults::kura::MAX_BLOCKS_IN_MEMORY}, + parameters::{actual::Kura as Config, defaults::kura::BLOCKS_IN_MEMORY}, }; use iroha_crypto::{Hash, HashOf}; use iroha_data_model::block::{BlockHeader, SignedBlock}; @@ -42,7 +42,7 @@ pub struct Kura { block_plain_text_path: Option, /// At most N last blocks will be stored in memory. /// Older blocks will be dropped from memory and loaded from the disk if they are needed. - max_blocks_in_memory: NonZeroUsize, + blocks_in_memory: NonZeroUsize, /// Amount of blocks loaded during initialization init_block_count: usize, } @@ -74,7 +74,7 @@ impl Kura { block_store: Mutex::new(block_store), block_data: Mutex::new(block_data), block_plain_text_path, - max_blocks_in_memory: config.max_blocks_in_memory, + blocks_in_memory: config.blocks_in_memory, init_block_count: block_count, }); @@ -88,7 +88,7 @@ impl Kura { block_store: Mutex::new(BlockStore::new(PathBuf::new())), block_data: Mutex::new(Vec::new()), block_plain_text_path: None, - max_blocks_in_memory: MAX_BLOCKS_IN_MEMORY, + blocks_in_memory: BLOCKS_IN_MEMORY, init_block_count: 0, }) } @@ -242,7 +242,7 @@ impl Kura { Self::drop_old_block( &mut block_data, written_block_count, - kura.max_blocks_in_memory.get(), + kura.blocks_in_memory.get(), ); written_block_count += 1; } @@ -332,7 +332,7 @@ impl Kura { let block_arc = Arc::new(block); // Only last N blocks should be kept in memory - if block_index + self.max_blocks_in_memory.get() >= data_array_guard.len() { + if block_index + self.blocks_in_memory.get() >= data_array_guard.len() { data_array_guard[block_index].1 = Some(Arc::clone(&block_arc)); } Some(block_arc) @@ -357,12 +357,12 @@ impl Kura { fn drop_old_block( block_data: &mut BlockData, written_block_count: usize, - max_blocks_in_memory: usize, + blocks_in_memory: usize, ) { // Keep last N blocks and genesis block. // (genesis block is used in metrics to get genesis timestamp) - if written_block_count > max_blocks_in_memory { - block_data[written_block_count - max_blocks_in_memory].1 = None; + if written_block_count > blocks_in_memory { + block_data[written_block_count - blocks_in_memory].1 = None; } } } @@ -810,7 +810,7 @@ impl AddErrContextExt for Result { mod tests { use std::{str::FromStr, thread, time::Duration}; - use iroha_config::parameters::defaults::kura::MAX_BLOCKS_IN_MEMORY; + use iroha_config::parameters::defaults::kura::BLOCKS_IN_MEMORY; use iroha_crypto::KeyPair; use iroha_data_model::{ account::Account, @@ -1009,7 +1009,7 @@ mod tests { store_dir: iroha_config::base::WithOrigin::inline( temp_dir.path().to_str().unwrap().into(), ), - max_blocks_in_memory: MAX_BLOCKS_IN_MEMORY, + blocks_in_memory: BLOCKS_IN_MEMORY, debug_output_new_blocks: false, }) .unwrap(); @@ -1039,7 +1039,7 @@ mod tests { store_dir: iroha_config::base::WithOrigin::inline( temp_dir.path().to_str().unwrap().into(), ), - max_blocks_in_memory: MAX_BLOCKS_IN_MEMORY, + blocks_in_memory: BLOCKS_IN_MEMORY, debug_output_new_blocks: false, }) .unwrap(); @@ -1091,7 +1091,7 @@ mod tests { store_dir: iroha_config::base::WithOrigin::inline( temp_dir.path().to_str().unwrap().into(), ), - max_blocks_in_memory: MAX_BLOCKS_IN_MEMORY, + blocks_in_memory: BLOCKS_IN_MEMORY, debug_output_new_blocks: false, }) .unwrap(); diff --git a/docs/source/references/peer.template.toml b/docs/source/references/peer.template.toml index af87ad3563..af6867671e 100644 --- a/docs/source/references/peer.template.toml +++ b/docs/source/references/peer.template.toml @@ -35,6 +35,7 @@ [kura] # init_mode = "strict" # store_dir = "./storage" +# blocks_in_memory = 128 ## Add more of this section for each trusted peer # [[sumeragi.trusted_peers]]