From fce5a376579cee495fe25e77dd88221b4480e3a9 Mon Sep 17 00:00:00 2001 From: librelois <elois@ifee.fr> Date: Sat, 1 Jun 2019 02:34:39 +0200 Subject: [PATCH] [ref] whole: Externalize currency params & add FORK_WINDOW_SIZE to them --- Cargo.lock | 9 ++ Cargo.toml | 1 + .../blockchain/blockchain-dal/Cargo.toml | 1 + .../blockchain-dal/src/constants.rs | 9 -- .../blockchain-dal/src/entities/fork_tree.rs | 97 ++++++++++--------- .../blockchain-dal/src/entities/mod.rs | 3 - .../src/readers/currency_params.rs | 2 +- .../src/writers/certification.rs | 2 +- .../blockchain-dal/src/writers/fork_tree.rs | 53 ++++------ .../blockchain-dal/src/writers/identity.rs | 2 +- .../blockchain-dal/src/writers/requests.rs | 5 +- lib/modules/blockchain/blockchain/Cargo.toml | 1 + lib/modules/blockchain/blockchain/clippy.toml | 3 +- .../blockchain/blockchain/src/dubp/mod.rs | 2 +- .../blockchain/src/dunp/receiver.rs | 8 +- .../blockchain/src/fork/fork_algo.rs | 27 ++++-- .../blockchain/src/fork/rollback.rs | 14 ++- .../blockchain/src/fork/stackable_blocks.rs | 7 +- lib/modules/blockchain/blockchain/src/lib.rs | 6 +- .../src/sync/apply/blocks_worker.rs | 2 + .../blockchain/blockchain/src/sync/mod.rs | 7 +- lib/tools/currency-params/Cargo.toml | 15 +++ lib/tools/currency-params/src/constants.rs | 27 ++++++ .../currency-params/src/lib.rs} | 26 +++-- 24 files changed, 210 insertions(+), 119 deletions(-) create mode 100644 lib/tools/currency-params/Cargo.toml create mode 100644 lib/tools/currency-params/src/constants.rs rename lib/{modules/blockchain/blockchain-dal/src/entities/currency_params.rs => tools/currency-params/src/lib.rs} (88%) diff --git a/Cargo.lock b/Cargo.lock index 17d53f2e..97e18d90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -341,6 +341,13 @@ dependencies = [ "dup-crypto 0.6.0", ] +[[package]] +name = "dup-currency-params" +version = "0.1.0" +dependencies = [ + "dubp-documents 0.12.0", +] + [[package]] name = "durs" version = "0.2.0-a" @@ -365,6 +372,7 @@ dependencies = [ "dubp-documents-tests-tools 0.1.0", "dup-crypto 0.6.0", "dup-crypto-tests-tools 0.1.0", + "dup-currency-params 0.1.0", "durs-blockchain-dal 0.2.0-a", "durs-common-tools 0.1.0", "durs-conf 0.2.0-a", @@ -392,6 +400,7 @@ dependencies = [ "dubp-documents-tests-tools 0.1.0", "dup-crypto 0.6.0", "dup-crypto-tests-tools 0.1.0", + "dup-currency-params 0.1.0", "durs-common-tests-tools 0.1.0", "durs-common-tools 0.1.0", "durs-module 0.2.0-a", diff --git a/Cargo.toml b/Cargo.toml index c05aacdb..fbb0f4d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ members = [ "lib/tests-tools/common-tests-tools", "lib/tools/crypto", "lib/tools/common-tools", + "lib/tools/currency-params", "lib/tools/documents", "lib/tools/json-pest-parser", "lib/tools/network-documents", diff --git a/lib/modules/blockchain/blockchain-dal/Cargo.toml b/lib/modules/blockchain/blockchain-dal/Cargo.toml index 6437999e..3c1e073b 100644 --- a/lib/modules/blockchain/blockchain-dal/Cargo.toml +++ b/lib/modules/blockchain/blockchain-dal/Cargo.toml @@ -11,6 +11,7 @@ path = "src/lib.rs" [dependencies] dup-crypto = { path = "../../../tools/crypto" } +dup-currency-params = { path = "../../../tools/currency-params" } dubp-documents= { path = "../../../tools/documents" } durs-module = { path = "../../../core/module" } durs-common-tools = { path = "../../../tools/common-tools" } diff --git a/lib/modules/blockchain/blockchain-dal/src/constants.rs b/lib/modules/blockchain/blockchain-dal/src/constants.rs index 48eaf7ba..b463e122 100644 --- a/lib/modules/blockchain/blockchain-dal/src/constants.rs +++ b/lib/modules/blockchain/blockchain-dal/src/constants.rs @@ -13,14 +13,5 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -/// Default value for sig_renew_period parameter -pub static DEFAULT_SIG_RENEW_PERIOD: &'static u64 = &5_259_600; -/// Default value for ms_period parameter -pub static DEFAULT_MS_PERIOD: &'static u64 = &5_259_600; -/// Default value for tx_window parameter -pub static DEFAULT_TX_WINDOW: &'static u64 = &604_800; -/// Maximum roolback length -pub static FORK_WINDOW_SIZE: &'static usize = &200; - /// Default page size for requests responses pub static DEFAULT_PAGE_SIZE: &'static usize = &50; diff --git a/lib/modules/blockchain/blockchain-dal/src/entities/fork_tree.rs b/lib/modules/blockchain/blockchain-dal/src/entities/fork_tree.rs index 5264d0b5..f5afd8c6 100644 --- a/lib/modules/blockchain/blockchain-dal/src/entities/fork_tree.rs +++ b/lib/modules/blockchain/blockchain-dal/src/entities/fork_tree.rs @@ -112,26 +112,39 @@ impl TreeNode { #[derive(Debug, Clone, Serialize, Deserialize)] /// Tree store all forks branchs pub struct ForkTree { - root: Option<TreeNodeId>, - nodes: Vec<Option<TreeNode>>, main_branch: HashMap<BlockNumber, TreeNodeId>, - sheets: HashSet<TreeNodeId>, + max_depth: usize, + nodes: Vec<Option<TreeNode>>, removed_blockstamps: Vec<Blockstamp>, + root: Option<TreeNodeId>, + sheets: HashSet<TreeNodeId>, } impl Default for ForkTree { + #[inline] fn default() -> Self { + ForkTree::new(*dup_currency_params::constants::DEFAULT_FORK_WINDOW_SIZE) + } +} + +impl ForkTree { + /// Instanciate new fork tree + #[inline] + pub fn new(max_depth: usize) -> Self { ForkTree { - nodes: Vec::with_capacity((*crate::constants::FORK_WINDOW_SIZE) * 2), + main_branch: HashMap::with_capacity(max_depth + 1), + max_depth, + nodes: Vec::with_capacity(max_depth * 2), + removed_blockstamps: Vec::with_capacity(max_depth), root: None, - main_branch: HashMap::with_capacity(*crate::constants::FORK_WINDOW_SIZE + 1), sheets: HashSet::new(), - removed_blockstamps: Vec::with_capacity(*crate::constants::FORK_WINDOW_SIZE), } } -} - -impl ForkTree { + /// Set max depth + #[inline] + pub fn set_max_depth(&mut self, max_depth: usize) { + self.max_depth = max_depth; + } /// Get tree size #[inline] pub fn size(&self) -> usize { @@ -214,7 +227,7 @@ impl ForkTree { } /// Get fork branch nodes ids pub fn get_fork_branch_nodes_ids(&self, node_id: TreeNodeId) -> Vec<TreeNodeId> { - let mut branch = Vec::with_capacity(*crate::constants::FORK_WINDOW_SIZE); + let mut branch = Vec::with_capacity(self.max_depth); let node = self.get_ref_node(node_id); if !self.main_branch.contains_key(&node.data.id) @@ -251,7 +264,7 @@ impl ForkTree { } /// Get fork branch pub fn get_fork_branch(&self, node_id: TreeNodeId) -> Vec<Blockstamp> { - let mut branch = Vec::with_capacity(*crate::constants::FORK_WINDOW_SIZE); + let mut branch = Vec::with_capacity(self.max_depth); let node = self.get_ref_node(node_id); branch.push(node.data); @@ -364,7 +377,7 @@ impl ForkTree { self.removed_blockstamps.clear(); if main_branch { self.main_branch.insert(data.id, new_node_id); - if self.main_branch.len() > *crate::constants::FORK_WINDOW_SIZE { + if self.main_branch.len() > self.max_depth { self.pruning(); } } @@ -438,6 +451,7 @@ impl ForkTree { mod tests { use super::*; + use dup_currency_params::constants::DEFAULT_FORK_WINDOW_SIZE; #[test] fn insert_root_nodes() { @@ -605,78 +619,74 @@ mod tests { #[test] fn insert_more_fork_window_size_nodes() { let mut tree = ForkTree::default(); - let blockstamps: Vec<Blockstamp> = dubp_documents_tests_tools::mocks::generate_blockstamps( - *crate::constants::FORK_WINDOW_SIZE + 2, - ); + let blockstamps: Vec<Blockstamp> = + dubp_documents_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 2); - // Fill tree with FORK_WINDOW_SIZE nodes + // Fill tree with MAX_DEPTH nodes tree.insert_new_node(blockstamps[0], None, true); - for i in 1..*crate::constants::FORK_WINDOW_SIZE { + for i in 1..*DEFAULT_FORK_WINDOW_SIZE { tree.insert_new_node(blockstamps[i], Some(TreeNodeId(i - 1)), true); } // The tree-root must not have been shifted yet - assert_eq!(*crate::constants::FORK_WINDOW_SIZE, tree.size()); + assert_eq!(*DEFAULT_FORK_WINDOW_SIZE, tree.size()); assert_eq!(Some(TreeNodeId(0)), tree.get_root_id()); // Inserting a node that exceeds FORK_WIN_SIZE, // the tree size must not be increased and the root must shift tree.insert_new_node( - blockstamps[*crate::constants::FORK_WINDOW_SIZE], - Some(TreeNodeId(*crate::constants::FORK_WINDOW_SIZE - 1)), + blockstamps[*DEFAULT_FORK_WINDOW_SIZE], + Some(TreeNodeId(*DEFAULT_FORK_WINDOW_SIZE - 1)), true, ); - assert_eq!(*crate::constants::FORK_WINDOW_SIZE, tree.size()); + assert_eq!(*DEFAULT_FORK_WINDOW_SIZE, tree.size()); assert_eq!(Some(TreeNodeId(1)), tree.get_root_id()); // Repeating the insertion of a node that exceeds FORK_WIN_SIZE, // the tree size must still not be increased and the root must still shift tree.insert_new_node( - blockstamps[*crate::constants::FORK_WINDOW_SIZE + 1], - Some(TreeNodeId(*crate::constants::FORK_WINDOW_SIZE)), + blockstamps[*DEFAULT_FORK_WINDOW_SIZE + 1], + Some(TreeNodeId(*DEFAULT_FORK_WINDOW_SIZE)), true, ); - assert_eq!(*crate::constants::FORK_WINDOW_SIZE, tree.size()); + assert_eq!(*DEFAULT_FORK_WINDOW_SIZE, tree.size()); assert_eq!(Some(TreeNodeId(2)), tree.get_root_id()); } #[test] fn test_change_main_branch() { let mut tree = ForkTree::default(); - let blockstamps: Vec<Blockstamp> = dubp_documents_tests_tools::mocks::generate_blockstamps( - *crate::constants::FORK_WINDOW_SIZE + 2, - ); + let blockstamps: Vec<Blockstamp> = + dubp_documents_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 2); - // Fill tree with FORK_WINDOW_SIZE nodes + // Fill tree with MAX_DEPTH nodes tree.insert_new_node(blockstamps[0], None, true); - for i in 1..*crate::constants::FORK_WINDOW_SIZE { + for i in 1..*DEFAULT_FORK_WINDOW_SIZE { tree.insert_new_node(blockstamps[i], Some(TreeNodeId(i - 1)), true); } - // Insert 2 forks blocks after block (FORK_WINDOW_SIZE - 2) + // Insert 2 forks blocks after block (MAX_DEPTH - 2) let fork_blockstamp = Blockstamp { - id: BlockNumber(*crate::constants::FORK_WINDOW_SIZE as u32 - 1), + id: BlockNumber(*DEFAULT_FORK_WINDOW_SIZE as u32 - 1), hash: BlockHash(dup_crypto_tests_tools::mocks::hash('A')), }; tree.insert_new_node( fork_blockstamp, - tree.get_main_branch_node_id(BlockNumber( - *crate::constants::FORK_WINDOW_SIZE as u32 - 2, - )), + tree.get_main_branch_node_id(BlockNumber(*DEFAULT_FORK_WINDOW_SIZE as u32 - 2)), false, ); let fork_blockstamp_2 = Blockstamp { - id: BlockNumber(*crate::constants::FORK_WINDOW_SIZE as u32), + id: BlockNumber(*DEFAULT_FORK_WINDOW_SIZE as u32), hash: BlockHash(dup_crypto_tests_tools::mocks::hash('B')), }; tree.insert_new_node( fork_blockstamp_2, - Some(TreeNodeId(*crate::constants::FORK_WINDOW_SIZE)), + Some(TreeNodeId(*DEFAULT_FORK_WINDOW_SIZE)), false, ); // Check tree size - assert_eq!(*crate::constants::FORK_WINDOW_SIZE + 2, tree.size()); + assert_eq!(*DEFAULT_FORK_WINDOW_SIZE + 2, tree.size()); // Check that the root of the shaft has not shifted assert_eq!(Some(TreeNodeId(0)), tree.get_root_id()); @@ -688,13 +698,10 @@ mod tests { // Check sheets content let expected_sheets = vec![ ( - TreeNodeId(*crate::constants::FORK_WINDOW_SIZE - 1), - blockstamps[*crate::constants::FORK_WINDOW_SIZE - 1], - ), - ( - TreeNodeId(*crate::constants::FORK_WINDOW_SIZE + 1), - fork_blockstamp_2, + TreeNodeId(*DEFAULT_FORK_WINDOW_SIZE - 1), + blockstamps[*DEFAULT_FORK_WINDOW_SIZE - 1], ), + (TreeNodeId(*DEFAULT_FORK_WINDOW_SIZE + 1), fork_blockstamp_2), ]; println!("{:?}", sheets); assert!(durs_common_tests_tools::collections::slice_same_elems( @@ -704,7 +711,7 @@ mod tests { // Switch to fork branch tree.change_main_branch( - blockstamps[*crate::constants::FORK_WINDOW_SIZE - 1], + blockstamps[*DEFAULT_FORK_WINDOW_SIZE - 1], fork_blockstamp_2, ); @@ -715,7 +722,7 @@ mod tests { )); // Check that tree size decrease - assert_eq!(*crate::constants::FORK_WINDOW_SIZE + 1, tree.size()); + assert_eq!(*DEFAULT_FORK_WINDOW_SIZE + 1, tree.size()); // Check that the root of the tree has shifted assert_eq!(Some(TreeNodeId(1)), tree.get_root_id()); diff --git a/lib/modules/blockchain/blockchain-dal/src/entities/mod.rs b/lib/modules/blockchain/blockchain-dal/src/entities/mod.rs index f3ab1363..03cd65cb 100644 --- a/lib/modules/blockchain/blockchain-dal/src/entities/mod.rs +++ b/lib/modules/blockchain/blockchain-dal/src/entities/mod.rs @@ -16,9 +16,6 @@ /// Block pub mod block; -/// Currency paramenters -pub mod currency_params; - /// Forks tree pub mod fork_tree; diff --git a/lib/modules/blockchain/blockchain-dal/src/readers/currency_params.rs b/lib/modules/blockchain/blockchain-dal/src/readers/currency_params.rs index 59800aed..f3bd13df 100644 --- a/lib/modules/blockchain/blockchain-dal/src/readers/currency_params.rs +++ b/lib/modules/blockchain/blockchain-dal/src/readers/currency_params.rs @@ -13,8 +13,8 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -use crate::entities::currency_params::CurrencyParameters; use crate::*; +use dup_currency_params::CurrencyParameters; /// Get currency parameters pub fn get_currency_params( diff --git a/lib/modules/blockchain/blockchain-dal/src/writers/certification.rs b/lib/modules/blockchain/blockchain-dal/src/writers/certification.rs index bf3c1947..bf154756 100644 --- a/lib/modules/blockchain/blockchain-dal/src/writers/certification.rs +++ b/lib/modules/blockchain/blockchain-dal/src/writers/certification.rs @@ -13,11 +13,11 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -use crate::entities::currency_params::CurrencyParameters; use crate::{BinDB, CertsExpirV10Datas, DALError, IdentitiesV10Datas}; use dubp_documents::documents::certification::CompactCertificationDocument; use dubp_documents::BlockNumber; use dup_crypto::keys::*; +use dup_currency_params::CurrencyParameters; use durs_wot::NodeId; /// Apply "certification" event in databases diff --git a/lib/modules/blockchain/blockchain-dal/src/writers/fork_tree.rs b/lib/modules/blockchain/blockchain-dal/src/writers/fork_tree.rs index a4e702fe..8885d216 100644 --- a/lib/modules/blockchain/blockchain-dal/src/writers/fork_tree.rs +++ b/lib/modules/blockchain/blockchain-dal/src/writers/fork_tree.rs @@ -90,13 +90,13 @@ mod test { use super::*; use crate::entities::fork_tree::TreeNodeId; + use dup_currency_params::constants::DEFAULT_FORK_WINDOW_SIZE; #[test] fn test_insert_new_head_block() -> Result<(), DALError> { // Create mock datas - let blockstamps = dubp_documents_tests_tools::mocks::generate_blockstamps( - *crate::constants::FORK_WINDOW_SIZE + 2, - ); + let blockstamps = + dubp_documents_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 2); let fork_tree_db = open_db::<ForksTreeV10Datas>(None, "")?; // Insert genesis block @@ -113,7 +113,7 @@ mod test { ); // Insert FORK_WINDOW_SIZE blocks - for i in 1..*crate::constants::FORK_WINDOW_SIZE { + for i in 1..*DEFAULT_FORK_WINDOW_SIZE { assert_eq!( Ok(vec![]), insert_new_head_block(&fork_tree_db, blockstamps[i]) @@ -122,13 +122,13 @@ mod test { // Check tree state assert_eq!( - *crate::constants::FORK_WINDOW_SIZE, + *DEFAULT_FORK_WINDOW_SIZE, fork_tree_db.read(|tree| tree.size())? ); assert_eq!( vec![( - TreeNodeId(*crate::constants::FORK_WINDOW_SIZE - 1), - blockstamps[*crate::constants::FORK_WINDOW_SIZE - 1] + TreeNodeId(*DEFAULT_FORK_WINDOW_SIZE - 1), + blockstamps[*DEFAULT_FORK_WINDOW_SIZE - 1] )], fork_tree_db.read(|tree| tree.get_sheets())? ); @@ -136,17 +136,11 @@ mod test { // Insert blocks after FORK_WINDOW_SIZE (firsts blocks must be removed) assert_eq!( Ok(vec![blockstamps[0]]), - insert_new_head_block( - &fork_tree_db, - blockstamps[*crate::constants::FORK_WINDOW_SIZE] - ) + insert_new_head_block(&fork_tree_db, blockstamps[*DEFAULT_FORK_WINDOW_SIZE]) ); assert_eq!( Ok(vec![blockstamps[1]]), - insert_new_head_block( - &fork_tree_db, - blockstamps[*crate::constants::FORK_WINDOW_SIZE + 1] - ) + insert_new_head_block(&fork_tree_db, blockstamps[*DEFAULT_FORK_WINDOW_SIZE + 1]) ); Ok(()) @@ -155,9 +149,8 @@ mod test { #[test] fn test_insert_new_fork_block() -> Result<(), DALError> { // Create mock datas - let blockstamps = dubp_documents_tests_tools::mocks::generate_blockstamps( - *crate::constants::FORK_WINDOW_SIZE + 3, - ); + let blockstamps = + dubp_documents_tests_tools::mocks::generate_blockstamps(*DEFAULT_FORK_WINDOW_SIZE + 3); let fork_tree_db = open_db::<ForksTreeV10Datas>(None, "")?; // Insert 4 main blocks @@ -216,7 +209,7 @@ mod test { )); // Insert FORK_WINDOW_SIZE blocks - for i in 4..*crate::constants::FORK_WINDOW_SIZE { + for i in 4..*DEFAULT_FORK_WINDOW_SIZE { assert_eq!( Ok(vec![]), insert_new_head_block(&fork_tree_db, blockstamps[i]) @@ -225,14 +218,14 @@ mod test { // Check tree state assert_eq!( - *crate::constants::FORK_WINDOW_SIZE + 2, + *DEFAULT_FORK_WINDOW_SIZE + 2, fork_tree_db.read(|tree| tree.size())? ); assert!(durs_common_tests_tools::collections::slice_same_elems( &vec![ ( - TreeNodeId(*crate::constants::FORK_WINDOW_SIZE + 1), - blockstamps[*crate::constants::FORK_WINDOW_SIZE - 1] + TreeNodeId(*DEFAULT_FORK_WINDOW_SIZE + 1), + blockstamps[*DEFAULT_FORK_WINDOW_SIZE - 1] ), (TreeNodeId(5), fork_blockstamp_2) ], @@ -243,31 +236,25 @@ mod test { for i in 0..2 { assert_eq!( Ok(vec![blockstamps[i]]), - insert_new_head_block( - &fork_tree_db, - blockstamps[*crate::constants::FORK_WINDOW_SIZE + i] - ) + insert_new_head_block(&fork_tree_db, blockstamps[*DEFAULT_FORK_WINDOW_SIZE + i]) ); } // Insert one new main block (fork branch must be removed) assert_eq!( Ok(vec![blockstamps[2], fork_blockstamp_2, fork_blockstamp]), - insert_new_head_block( - &fork_tree_db, - blockstamps[*crate::constants::FORK_WINDOW_SIZE + 2] - ) + insert_new_head_block(&fork_tree_db, blockstamps[*DEFAULT_FORK_WINDOW_SIZE + 2]) ); // Check tree state assert_eq!( - *crate::constants::FORK_WINDOW_SIZE, + *DEFAULT_FORK_WINDOW_SIZE, fork_tree_db.read(|tree| tree.size())? ); assert_eq!( vec![( - TreeNodeId(*crate::constants::FORK_WINDOW_SIZE + 4), - blockstamps[*crate::constants::FORK_WINDOW_SIZE + 2] + TreeNodeId(*DEFAULT_FORK_WINDOW_SIZE + 4), + blockstamps[*DEFAULT_FORK_WINDOW_SIZE + 2] )], fork_tree_db.read(|tree| tree.get_sheets())? ); diff --git a/lib/modules/blockchain/blockchain-dal/src/writers/identity.rs b/lib/modules/blockchain/blockchain-dal/src/writers/identity.rs index 4be0b0b0..2cbfa69e 100644 --- a/lib/modules/blockchain/blockchain-dal/src/writers/identity.rs +++ b/lib/modules/blockchain/blockchain-dal/src/writers/identity.rs @@ -13,13 +13,13 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -use crate::entities::currency_params::CurrencyParameters; use crate::entities::identity::{DALIdentity, DALIdentityState}; use crate::{BinDB, DALError, IdentitiesV10Datas, MsExpirV10Datas}; use dubp_documents::documents::identity::IdentityDocument; use dubp_documents::Document; use dubp_documents::{BlockNumber, Blockstamp}; use dup_crypto::keys::PubKey; +use dup_currency_params::CurrencyParameters; use durs_common_tools::fatal_error; use durs_wot::NodeId; diff --git a/lib/modules/blockchain/blockchain-dal/src/writers/requests.rs b/lib/modules/blockchain/blockchain-dal/src/writers/requests.rs index 9cf73d9b..22fb9a3f 100644 --- a/lib/modules/blockchain/blockchain-dal/src/writers/requests.rs +++ b/lib/modules/blockchain/blockchain-dal/src/writers/requests.rs @@ -14,7 +14,6 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use crate::entities::block::DALBlock; -use crate::entities::currency_params::CurrencyParameters; use crate::entities::sources::SourceAmount; use crate::writers::transaction::DALTxV10; use crate::*; @@ -23,6 +22,7 @@ use dubp_documents::documents::certification::CompactCertificationDocument; use dubp_documents::documents::identity::IdentityDocument; use dubp_documents::Blockstamp; use dup_crypto::keys::PubKey; +use dup_currency_params::CurrencyParameters; use durs_wot::NodeId; use std::ops::Deref; @@ -59,6 +59,7 @@ impl BlocksDBsWriteQuery { self, blockchain_db: &BinDB<LocalBlockchainV10Datas>, forks_db: &ForksDBs, + fork_window_size: usize, sync_target: Option<Blockstamp>, ) -> Result<(), DALError> { match self { @@ -66,7 +67,7 @@ impl BlocksDBsWriteQuery { let dal_block: DALBlock = dal_block; trace!("BlocksDBsWriteQuery::WriteBlock..."); if sync_target.is_none() - || dal_block.blockstamp().id.0 + *crate::constants::FORK_WINDOW_SIZE as u32 + || dal_block.blockstamp().id.0 + fork_window_size as u32 >= sync_target.expect("safe unwrap").id.0 { super::block::insert_new_head_block(blockchain_db, forks_db, dal_block)?; diff --git a/lib/modules/blockchain/blockchain/Cargo.toml b/lib/modules/blockchain/blockchain/Cargo.toml index 58eb201d..a4b78d42 100644 --- a/lib/modules/blockchain/blockchain/Cargo.toml +++ b/lib/modules/blockchain/blockchain/Cargo.toml @@ -13,6 +13,7 @@ path = "src/lib.rs" dirs = "1.0.2" durs-conf = { path = "../../../core/conf" } dup-crypto = { path = "../../../tools/crypto" } +dup-currency-params = { path = "../../../tools/currency-params" } durs-blockchain-dal = { path = "../blockchain-dal" } dubp-documents= { path = "../../../tools/documents" } durs-common-tools = { path = "../../../tools/common-tools" } diff --git a/lib/modules/blockchain/blockchain/clippy.toml b/lib/modules/blockchain/blockchain/clippy.toml index 5ed4fe84..7833baea 100644 --- a/lib/modules/blockchain/blockchain/clippy.toml +++ b/lib/modules/blockchain/blockchain/clippy.toml @@ -1 +1,2 @@ -cyclomatic-complexity-threshold = 42 \ No newline at end of file +cyclomatic-complexity-threshold = 42 +too-many-arguments-threshold = 8 \ No newline at end of file diff --git a/lib/modules/blockchain/blockchain/src/dubp/mod.rs b/lib/modules/blockchain/blockchain/src/dubp/mod.rs index 0cb4208c..8497532b 100644 --- a/lib/modules/blockchain/blockchain/src/dubp/mod.rs +++ b/lib/modules/blockchain/blockchain/src/dubp/mod.rs @@ -109,7 +109,7 @@ pub fn check_and_apply_block( } else if !already_have_block && (block_doc.number.0 >= bc.current_blockstamp.id.0 || (bc.current_blockstamp.id.0 - block_doc.number.0) - < *durs_blockchain_dal::constants::FORK_WINDOW_SIZE as u32) + < bc.currency_params.fork_window_size as u32) { debug!( "stackable_block : block {} not chainable, store this for future !", diff --git a/lib/modules/blockchain/blockchain/src/dunp/receiver.rs b/lib/modules/blockchain/blockchain/src/dunp/receiver.rs index 6ed74cd5..9ec45acb 100644 --- a/lib/modules/blockchain/blockchain/src/dunp/receiver.rs +++ b/lib/modules/blockchain/blockchain/src/dunp/receiver.rs @@ -46,7 +46,12 @@ pub fn receive_blocks(bc: &mut BlockchainModule, blocks: Vec<BlockDocument>) { bc.current_blockstamp = new_current_block.blockstamp(); // Apply db requests bc_db_query - .apply(&bc.blocks_databases.blockchain_db, &bc.forks_dbs, None) + .apply( + &bc.blocks_databases.blockchain_db, + &bc.forks_dbs, + bc.currency_params.fork_window_size, + None, + ) .expect("Fatal error : Fail to apply DBWriteRequest !"); for query in &wot_dbs_queries { query @@ -74,6 +79,7 @@ pub fn receive_blocks(bc: &mut BlockchainModule, blocks: Vec<BlockDocument>) { info!("new fork block({})", blockstamp); if let Ok(Some(new_bc_branch)) = fork_algo::fork_resolution_algo( &bc.forks_dbs, + bc.currency_params.fork_window_size, bc.current_blockstamp, &bc.invalid_forks, ) { diff --git a/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs b/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs index 224dcb2b..d0d9b178 100644 --- a/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs +++ b/lib/modules/blockchain/blockchain/src/fork/fork_algo.rs @@ -25,6 +25,7 @@ pub static ADVANCE_TIME: &'static u64 = &900; pub fn fork_resolution_algo( forks_dbs: &ForksDBs, + fork_window_size: usize, current_blockstamp: Blockstamp, invalid_blocks: &HashSet<Blockstamp>, ) -> Result<Option<Vec<Blockstamp>>, DALError> { @@ -58,8 +59,7 @@ pub fn fork_resolution_algo( })?; if branch_head_blockstamp.id.0 >= current_blockstamp.id.0 + *ADVANCE_BLOCKS && branch_head_median_time >= current_bc_time + *ADVANCE_TIME - && branch[0].id.0 + *durs_blockchain_dal::constants::FORK_WINDOW_SIZE as u32 - > current_blockstamp.id.0 + && branch[0].id.0 + fork_window_size as u32 > current_blockstamp.id.0 { let mut valid_branch = true; for blockstamp in &branch { @@ -91,7 +91,7 @@ mod tests { #[test] fn test_fork_resolution_algo() -> Result<(), DALError> { // Get FORK_WINDOW_SIZE value - let fork_window_size = *durs_blockchain_dal::constants::FORK_WINDOW_SIZE; + let fork_window_size = *dup_currency_params::constants::DEFAULT_FORK_WINDOW_SIZE; // Open empty databases in memory mode let bc_dbs = BlocksV10DBs::open(None); @@ -163,7 +163,12 @@ mod tests { // Must not fork assert_eq!( None, - fork_resolution_algo(&forks_dbs, current_blockstamp, &invalid_blocks)? + fork_resolution_algo( + &forks_dbs, + fork_window_size, + current_blockstamp, + &invalid_blocks + )? ); // Add the determining fork block @@ -194,7 +199,12 @@ mod tests { fork_blocks[2].blockstamp(), determining_blockstamp, ]), - fork_resolution_algo(&forks_dbs, current_blockstamp, &invalid_blocks)? + fork_resolution_algo( + &forks_dbs, + fork_window_size, + current_blockstamp, + &invalid_blocks + )? ); current_blockstamp = determining_blockstamp; @@ -220,7 +230,12 @@ mod tests { // Must refork assert_eq!( Some(new_main_blocks.iter().map(|b| b.blockstamp()).collect()), - fork_resolution_algo(&forks_dbs, current_blockstamp, &invalid_blocks)? + fork_resolution_algo( + &forks_dbs, + fork_window_size, + current_blockstamp, + &invalid_blocks + )? ); //current_blockstamp = new_main_blocks.last().expect("safe unwrap").blockstamp(); diff --git a/lib/modules/blockchain/blockchain/src/fork/rollback.rs b/lib/modules/blockchain/blockchain/src/fork/rollback.rs index 74d833ee..7cacbe9d 100644 --- a/lib/modules/blockchain/blockchain/src/fork/rollback.rs +++ b/lib/modules/blockchain/blockchain/src/fork/rollback.rs @@ -49,7 +49,12 @@ pub fn apply_rollback(bc: &mut BlockchainModule, new_bc_branch: Vec<Blockstamp>) let blockstamp = dal_block.block.blockstamp(); // Apply db requests bc_db_query - .apply(&bc.blocks_databases.blockchain_db, &bc.forks_dbs, None) + .apply( + &bc.blocks_databases.blockchain_db, + &bc.forks_dbs, + bc.currency_params.fork_window_size, + None, + ) .expect("Fatal error : Fail to apply DBWriteRequest !"); for query in &wot_dbs_queries { query @@ -83,7 +88,12 @@ pub fn apply_rollback(bc: &mut BlockchainModule, new_bc_branch: Vec<Blockstamp>) bc.current_blockstamp = *blockstamp; // Apply db requests bc_db_query - .apply(&bc.blocks_databases.blockchain_db, &bc.forks_dbs, None) + .apply( + &bc.blocks_databases.blockchain_db, + &bc.forks_dbs, + bc.currency_params.fork_window_size, + None, + ) .expect("Fatal error : Fail to apply DBWriteRequest !"); for query in &wot_dbs_queries { query diff --git a/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs b/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs index f04b80af..9a4ca491 100644 --- a/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs +++ b/lib/modules/blockchain/blockchain/src/fork/stackable_blocks.rs @@ -43,7 +43,12 @@ pub fn apply_stackable_blocks(bc: &mut BlockchainModule) { let blockstamp = new_current_block.blockstamp(); // Apply db requests bc_db_query - .apply(&bc.blocks_databases.blockchain_db, &bc.forks_dbs, None) + .apply( + &bc.blocks_databases.blockchain_db, + &bc.forks_dbs, + bc.currency_params.fork_window_size, + None, + ) .expect("Fatal error : Fail to apply DBWriteRequest !"); for query in &wot_dbs_queries { query diff --git a/lib/modules/blockchain/blockchain/src/lib.rs b/lib/modules/blockchain/blockchain/src/lib.rs index 5f701efc..64d05dc4 100644 --- a/lib/modules/blockchain/blockchain/src/lib.rs +++ b/lib/modules/blockchain/blockchain/src/lib.rs @@ -57,7 +57,7 @@ use crate::fork::*; use dubp_documents::documents::block::BlockDocument; use dubp_documents::*; use dup_crypto::keys::*; -use durs_blockchain_dal::entities::currency_params::CurrencyParameters; +use dup_currency_params::CurrencyParameters; use durs_blockchain_dal::*; use durs_common_tools::fatal_error; use durs_message::events::*; @@ -103,8 +103,8 @@ pub struct BlockchainModule { pub wot_databases: WotsV10DBs, /// Currency databases currency_databases: CurrencyV10DBs, - // Currency parameters - currency_params: CurrencyParameters, + /// Currency parameters + pub currency_params: CurrencyParameters, /// Current blockstamp pub current_blockstamp: Blockstamp, /// network consensus blockstamp diff --git a/lib/modules/blockchain/blockchain/src/sync/apply/blocks_worker.rs b/lib/modules/blockchain/blockchain/src/sync/apply/blocks_worker.rs index 5c543ae3..9dbba076 100644 --- a/lib/modules/blockchain/blockchain/src/sync/apply/blocks_worker.rs +++ b/lib/modules/blockchain/blockchain/src/sync/apply/blocks_worker.rs @@ -23,6 +23,7 @@ pub fn execute( recv: mpsc::Receiver<SyncJobsMess>, blocks_dbs: BlocksV10DBs, forks_db: ForksDBs, + fork_window_size: usize, target_blockstamp: Blockstamp, mut apply_pb: ProgressBar<std::io::Stdout>, ) { @@ -41,6 +42,7 @@ pub fn execute( req.apply( &blocks_dbs.blockchain_db, &forks_db, + fork_window_size, Some(target_blockstamp), ) .expect("Fatal error : Fail to apply DBWriteRequest !"); diff --git a/lib/modules/blockchain/blockchain/src/sync/mod.rs b/lib/modules/blockchain/blockchain/src/sync/mod.rs index 93b4a208..78da0a69 100644 --- a/lib/modules/blockchain/blockchain/src/sync/mod.rs +++ b/lib/modules/blockchain/blockchain/src/sync/mod.rs @@ -20,7 +20,7 @@ use crate::dubp::apply::apply_valid_block; use crate::*; use dubp_documents::{BlockHash, BlockNumber}; use dup_crypto::keys::*; -use durs_blockchain_dal::entities::currency_params::CurrencyParameters; +use dup_currency_params::CurrencyParameters; use durs_blockchain_dal::writers::requests::*; use durs_common_tools::fatal_error; use durs_wot::NodeId; @@ -226,6 +226,9 @@ pub fn local_sync<DC: DursConfTrait>(profile_path: PathBuf, conf: &DC, sync_opts current_blockstamp.id.0, target_blockstamp.id.0 ); + // Instantiate currency parameters + let mut currency_params = CurrencyParameters::default(); + // Createprogess bar let mut apply_pb = ProgressBar::new(count_chunks.into()); apply_pb.format("╢▌▌░╟"); @@ -242,6 +245,7 @@ pub fn local_sync<DC: DursConfTrait>(profile_path: PathBuf, conf: &DC, sync_opts recv_blocks_thread, blocks_dbs, forks_dbs, + currency_params.fork_window_size, target_blockstamp, apply_pb, ); @@ -278,7 +282,6 @@ pub fn local_sync<DC: DursConfTrait>(profile_path: PathBuf, conf: &DC, sync_opts let mut last_block_expiring: isize = -1; let certs_db = BinDB::Mem(open_memory_db::<CertsExpirV10Datas>().expect("Fail to create memory certs_db")); - let mut currency_params = CurrencyParameters::default(); let mut get_currency_params = false; let mut certs_count = 0; diff --git a/lib/tools/currency-params/Cargo.toml b/lib/tools/currency-params/Cargo.toml new file mode 100644 index 00000000..7a3c2bd2 --- /dev/null +++ b/lib/tools/currency-params/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "dup-currency-params" +version = "0.1.0" +authors = ["elois <elois@ifee.fr>"] +description = "Duniter protocol currency parameters" +repository = "https://git.duniter.org/nodes/rust/duniter-rs" +keywords = ["duniter", "blockchain", "cryptocurrency"] +license = "AGPL-3.0" +edition = "2018" + +[lib] +path = "src/lib.rs" + +[dependencies] +dubp-documents= { path = "../documents" } diff --git a/lib/tools/currency-params/src/constants.rs b/lib/tools/currency-params/src/constants.rs new file mode 100644 index 00000000..af9b3e24 --- /dev/null +++ b/lib/tools/currency-params/src/constants.rs @@ -0,0 +1,27 @@ +// Copyright (C) 2018 The Durs Project Developers. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +//! Currency parameters constants. + +/// Default currency name +pub const DEFAULT_CURRENCY: &str = "default_currency"; +/// Default value for sig_renew_period parameter +pub static DEFAULT_SIG_RENEW_PERIOD: &'static u64 = &5_259_600; +/// Default value for ms_period parameter +pub static DEFAULT_MS_PERIOD: &'static u64 = &5_259_600; +/// Default value for tx_window parameter +pub static DEFAULT_TX_WINDOW: &'static u64 = &604_800; +/// Default maximum roolback length +pub static DEFAULT_FORK_WINDOW_SIZE: &'static usize = &200; diff --git a/lib/modules/blockchain/blockchain-dal/src/entities/currency_params.rs b/lib/tools/currency-params/src/lib.rs similarity index 88% rename from lib/modules/blockchain/blockchain-dal/src/entities/currency_params.rs rename to lib/tools/currency-params/src/lib.rs index 28a65fc1..04108301 100644 --- a/lib/modules/blockchain/blockchain-dal/src/entities/currency_params.rs +++ b/lib/tools/currency-params/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright (C) 2018 The Duniter Project Developers. +// Copyright (C) 2018 The Durs Project Developers. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as @@ -13,13 +13,16 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. +//! Duniter protocol currency parameters + +pub mod constants; + use crate::constants::*; -use crate::*; use dubp_documents::documents::block::BlockV10Parameters; use dubp_documents::CurrencyName; #[derive(Debug, Copy, Clone)] -/// Curerncy parameters +/// Currency parameters pub struct CurrencyParameters { /// Protocol version pub protocol_version: usize, @@ -71,29 +74,37 @@ pub struct CurrencyParameters { pub ud_reeval_time0: u64, /// Time period between two re-evaluation of the UD. pub dt_reeval: u64, + /// Maximum roolback length + pub fork_window_size: usize, } impl From<(CurrencyName, BlockV10Parameters)> for CurrencyParameters { fn from(source: (CurrencyName, BlockV10Parameters)) -> CurrencyParameters { let (currency_name, block_params) = source; let sig_renew_period = match currency_name.0.as_str() { - "default_currency" => *DEFAULT_SIG_RENEW_PERIOD, + DEFAULT_CURRENCY => *DEFAULT_SIG_RENEW_PERIOD, "g1" => 5_259_600, "g1-test" => 5_259_600 / 5, _ => *DEFAULT_SIG_RENEW_PERIOD, }; let ms_period = match currency_name.0.as_str() { - "default_currency" => *DEFAULT_MS_PERIOD, + DEFAULT_CURRENCY => *DEFAULT_MS_PERIOD, "g1" => 5_259_600, "g1-test" => 5_259_600 / 5, _ => *DEFAULT_MS_PERIOD, }; let tx_window = match currency_name.0.as_str() { - "default_currency" => *DEFAULT_TX_WINDOW, + DEFAULT_CURRENCY => *DEFAULT_TX_WINDOW, "g1" => 604_800, "g1-test" => 604_800, _ => *DEFAULT_TX_WINDOW, }; + let fork_window_size = match currency_name.0.as_str() { + DEFAULT_CURRENCY => *DEFAULT_FORK_WINDOW_SIZE, + "g1" => 200, + "g1-test" => 200, + _ => *DEFAULT_FORK_WINDOW_SIZE, + }; CurrencyParameters { protocol_version: 10, c: block_params.c, @@ -119,6 +130,7 @@ impl From<(CurrencyName, BlockV10Parameters)> for CurrencyParameters { ud_time0: block_params.ud_time0, ud_reeval_time0: block_params.ud_reeval_time0, dt_reeval: block_params.dt_reeval, + fork_window_size, } } } @@ -126,7 +138,7 @@ impl From<(CurrencyName, BlockV10Parameters)> for CurrencyParameters { impl Default for CurrencyParameters { fn default() -> CurrencyParameters { CurrencyParameters::from(( - CurrencyName(String::from("default_currency")), + CurrencyName(String::from(DEFAULT_CURRENCY)), BlockV10Parameters::default(), )) } -- GitLab