From c2af081a19d3b88279816465bdae46eacd45c73f Mon Sep 17 00:00:00 2001 From: librelois <elois@ifee.fr> Date: Thu, 15 Aug 2019 19:38:02 +0200 Subject: [PATCH] [fix] #154 bc: request of fork blocks to fill fork tree holes --- .../blockchain/blockchain/src/constants.rs | 7 +++-- .../blockchain/blockchain/src/dunp/queries.rs | 12 ++++++--- lib/modules/blockchain/blockchain/src/lib.rs | 10 ++++++- .../blockchain/src/requests/sent.rs | 27 +++++++++++++++++-- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/lib/modules/blockchain/blockchain/src/constants.rs b/lib/modules/blockchain/blockchain/src/constants.rs index f1b946a4..dd2b0d2a 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 75253f76..a4179395 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 d75a32d0..600324c7 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 946c1182..4ed53650 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") -- GitLab