Skip to content
Snippets Groups Projects
Commit c2af081a authored by Éloïs's avatar Éloïs
Browse files

[fix] #154 bc: request of fork blocks to fill fork tree holes

parent 239d958b
No related branches found
No related tags found
1 merge request!166Resolve "WS2Pv1: Intelligent request of previous blocks to fill fork tree holes"
......@@ -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;
......@@ -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);
......
......@@ -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 {
......
......@@ -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")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment