diff --git a/lib/modules/blockchain/blockchain/src/constants.rs b/lib/modules/blockchain/blockchain/src/constants.rs index f1b946a44f6228bf3efb74f2c7949fc2c32aa17a..dd2b0d2a59de0c012a2358c53a0995bf02442db4 100644 --- a/lib/modules/blockchain/blockchain/src/constants.rs +++ b/lib/modules/blockchain/blockchain/src/constants.rs @@ -26,10 +26,13 @@ pub static CHUNK_FILE_NAME_BEGIN: &'static str = "chunk_"; pub static CHUNK_FILE_NAME_END: &'static str = "-250.json"; /// Low requency of request of main blocks -pub static REQUEST_MAIN_BLOCKS_LOW_FREQUENCY_IN_SEC: &'static u64 = &90; +pub static REQUEST_MAIN_BLOCKS_LOW_FREQUENCY_IN_SEC: &'static u64 = &240; /// High frequency of request of the main blocks -pub static REQUEST_MAIN_BLOCKS_HIGH_FREQUENCY_IN_SEC: &'static u64 = &20; +pub static REQUEST_MAIN_BLOCKS_HIGH_FREQUENCY_IN_SEC: &'static u64 = &30; + +/// Frequency of request fork blocks (=request all blocks on fork window) +pub static REQUEST_FORK_BLOCKS_FREQUENCY_IN_SEC: &'static u64 = &180; /// Blocks Delay threshold pub static BLOCKS_DELAY_THRESHOLD: &'static u32 = &5; diff --git a/lib/modules/blockchain/blockchain/src/dunp/queries.rs b/lib/modules/blockchain/blockchain/src/dunp/queries.rs index 75253f76e8a737b52ed24bd74ab5a038a85f5fab..a4179395ffff14960c021e92765928c4c82e3eec 100644 --- a/lib/modules/blockchain/blockchain/src/dunp/queries.rs +++ b/lib/modules/blockchain/blockchain/src/dunp/queries.rs @@ -75,7 +75,7 @@ pub fn request_blocks_to( } else { to.0 }; - request_blocks_from_to(bc, from, real_to) + request_blocks_from_to(bc, BlockNumber(from), BlockNumber(real_to)) } else { HashMap::with_capacity(0) } @@ -100,11 +100,15 @@ pub fn request_orphan_previous( HashMap::with_capacity(0) } -fn request_blocks_from_to( +/// Requests blocks from `from` to `to` +pub fn request_blocks_from_to( bc: &BlockchainModule, - mut from: u32, - to: u32, + from: BlockNumber, + to: BlockNumber, ) -> HashMap<ModuleReqId, OldNetworkRequest> { + info!("BlockchainModule : request_blocks_from_to({}-{})", from, to); + let mut from = from.0; + let to = to.0; let mut requests_ids = HashMap::new(); while from <= to { let mut req_id = ModuleReqId(0); diff --git a/lib/modules/blockchain/blockchain/src/lib.rs b/lib/modules/blockchain/blockchain/src/lib.rs index d75a32d0abfc392dcc1766c0a4583892c102ce71..600324c7698ea885916a41244ef89fe51f3a6f7d 100644 --- a/lib/modules/blockchain/blockchain/src/lib.rs +++ b/lib/modules/blockchain/blockchain/src/lib.rs @@ -117,6 +117,8 @@ pub struct BlockchainModule { pub pending_network_requests: HashMap<ModuleReqId, OldNetworkRequest>, /// Last request blocks pub last_request_blocks: SystemTime, + /// Last request fork blocks (=all blocks in fork window size) + last_request_fork_blocks: SystemTime, } #[derive(Debug, Clone)] @@ -229,6 +231,7 @@ impl BlockchainModule { invalid_forks: HashSet::new(), pending_network_requests: HashMap::new(), last_request_blocks: UNIX_EPOCH, + last_request_fork_blocks: UNIX_EPOCH, } } /// Databases explorer @@ -276,10 +279,15 @@ impl BlockchainModule { let mut last_get_stackables_blocks = UNIX_EPOCH; loop { + let now = SystemTime::now(); // Request Consensus requests::sent::request_network_consensus(self); // Request next main blocks - requests::sent::request_next_main_blocks(self); + requests::sent::request_next_main_blocks(self, now); + // Request fork blocks + requests::sent::request_fork_blocks(self, now); + + // Listen received messages match blockchain_receiver.recv_timeout(Duration::from_millis(1000)) { Ok(durs_message) => { match durs_message { diff --git a/lib/modules/blockchain/blockchain/src/requests/sent.rs b/lib/modules/blockchain/blockchain/src/requests/sent.rs index 946c1182fdc6a18e51ac538dae873fca6f5da4d6..4ed53650ffcb315eab6e9b30e2340846e99ea6ed 100644 --- a/lib/modules/blockchain/blockchain/src/requests/sent.rs +++ b/lib/modules/blockchain/blockchain/src/requests/sent.rs @@ -39,7 +39,31 @@ pub fn request_orphan_previous(bc: &mut BlockchainModule, orphan_blockstamp: Blo } } -pub fn request_next_main_blocks(bc: &mut BlockchainModule) { +pub fn request_fork_blocks(bc: &mut BlockchainModule, now: SystemTime) { + if now + .duration_since(bc.last_request_fork_blocks) + .expect("duration_since error") + > Duration::from_secs(*REQUEST_FORK_BLOCKS_FREQUENCY_IN_SEC) + { + bc.last_request_fork_blocks = now; + // Request all blocks in fork window size + if let Some(currency_params) = bc.currency_params { + let fork_window_size = currency_params.fork_window_size as u32; + let from = if bc.current_blockstamp.id.0 > fork_window_size { + BlockNumber(bc.current_blockstamp.id.0 - fork_window_size) + } else { + BlockNumber(0) + }; + let to = bc.current_blockstamp.id; + let new_pending_network_requests = dunp::queries::request_blocks_from_to(bc, from, to); + for (new_req_id, new_req) in new_pending_network_requests { + bc.pending_network_requests.insert(new_req_id, new_req); + } + } + } +} + +pub fn request_next_main_blocks(bc: &mut BlockchainModule, now: SystemTime) { // Choose frequency let frequency = if bc.consensus.id.0 == 0 || bc.consensus.id.0 > bc.current_blockstamp.id.0 + *BLOCKS_DELAY_THRESHOLD @@ -50,7 +74,6 @@ pub fn request_next_main_blocks(bc: &mut BlockchainModule) { }; // Apply frequency - let now = SystemTime::now(); if now .duration_since(bc.last_request_blocks) .expect("duration_since error")